2017-08-31 19:11:57 +00:00
|
|
|
/*
|
2017-06-29 23:53:52 +00:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-31 19:11:57 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-06-29 23:53:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This fuzz target performs a zstd round-trip test (compress & decompress),
|
|
|
|
* compares the result with the original, and calls abort() on corruption.
|
|
|
|
*/
|
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
|
|
|
|
2017-06-29 23:53:52 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "fuzz_helpers.h"
|
2017-09-14 21:41:49 +00:00
|
|
|
#include "zstd_helpers.h"
|
2019-09-10 23:14:43 +00:00
|
|
|
#include "fuzz_data_producer.h"
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
ZSTD_CCtx *cctx = NULL;
|
2017-06-29 23:53:52 +00:00
|
|
|
static ZSTD_DCtx *dctx = NULL;
|
|
|
|
static uint8_t* cBuf = NULL;
|
|
|
|
static uint8_t* rBuf = NULL;
|
|
|
|
static size_t bufSize = 0;
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
static ZSTD_outBuffer makeOutBuffer(uint8_t *dst, size_t capacity,
|
|
|
|
FUZZ_dataProducer_t *producer)
|
2017-06-29 23:53:52 +00:00
|
|
|
{
|
2017-09-14 21:41:49 +00:00
|
|
|
ZSTD_outBuffer buffer = { dst, 0, 0 };
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ASSERT(capacity > 0);
|
2019-09-12 19:40:12 +00:00
|
|
|
buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, capacity));
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ASSERT(buffer.size <= capacity);
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
return buffer;
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
static ZSTD_inBuffer makeInBuffer(const uint8_t **src, size_t *size,
|
|
|
|
FUZZ_dataProducer_t *producer)
|
2017-06-29 23:53:52 +00:00
|
|
|
{
|
2017-09-14 21:41:49 +00:00
|
|
|
ZSTD_inBuffer buffer = { *src, 0, 0 };
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ASSERT(*size > 0);
|
2019-09-12 19:40:12 +00:00
|
|
|
buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, *size));
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ASSERT(buffer.size <= *size);
|
|
|
|
*src += buffer.size;
|
|
|
|
*size -= buffer.size;
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
return buffer;
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t compress(uint8_t *dst, size_t capacity,
|
2019-09-10 23:14:43 +00:00
|
|
|
const uint8_t *src, size_t srcSize,
|
|
|
|
FUZZ_dataProducer_t *producer)
|
2017-06-29 23:53:52 +00:00
|
|
|
{
|
|
|
|
size_t dstSize = 0;
|
2018-11-16 00:53:45 +00:00
|
|
|
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_setRandomParameters(cctx, srcSize, producer);
|
2017-06-29 23:53:52 +00:00
|
|
|
|
|
|
|
while (srcSize > 0) {
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_inBuffer in = makeInBuffer(&src, &srcSize, producer);
|
2017-06-29 23:53:52 +00:00
|
|
|
/* Mode controls the action. If mode == -1 we pick a new mode */
|
|
|
|
int mode = -1;
|
2019-04-09 04:06:19 +00:00
|
|
|
while (in.pos < in.size || mode != -1) {
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
|
2017-09-14 21:41:49 +00:00
|
|
|
/* Previous action finished, pick a new mode. */
|
2019-09-12 19:40:12 +00:00
|
|
|
if (mode == -1) mode = FUZZ_dataProducer_uint32Range(producer, 0, 9);
|
2017-09-14 21:41:49 +00:00
|
|
|
switch (mode) {
|
2019-09-10 23:14:43 +00:00
|
|
|
case 0: /* fall-through */
|
|
|
|
case 1: /* fall-through */
|
2017-09-14 21:41:49 +00:00
|
|
|
case 2: {
|
|
|
|
size_t const ret =
|
2018-11-30 19:16:26 +00:00
|
|
|
ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush);
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ZASSERT(ret);
|
|
|
|
if (ret == 0)
|
|
|
|
mode = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
size_t ret =
|
2018-11-30 19:16:26 +00:00
|
|
|
ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ZASSERT(ret);
|
|
|
|
/* Reset the compressor when the frame is finished */
|
|
|
|
if (ret == 0) {
|
2018-11-16 00:53:45 +00:00
|
|
|
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
|
2019-09-12 19:40:12 +00:00
|
|
|
if (FUZZ_dataProducer_uint32Range(producer, 0, 7) == 0) {
|
2017-09-26 21:03:43 +00:00
|
|
|
size_t const remaining = in.size - in.pos;
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_setRandomParameters(cctx, remaining, producer);
|
2017-09-26 21:03:43 +00:00
|
|
|
}
|
2017-09-14 21:41:49 +00:00
|
|
|
mode = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
size_t const ret =
|
2018-11-30 19:16:26 +00:00
|
|
|
ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ZASSERT(ret);
|
2017-06-29 23:53:52 +00:00
|
|
|
mode = -1;
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 21:41:49 +00:00
|
|
|
dst += out.pos;
|
|
|
|
dstSize += out.pos;
|
|
|
|
capacity -= out.pos;
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
2017-09-14 21:41:49 +00:00
|
|
|
ZSTD_inBuffer in = {NULL, 0, 0};
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
|
2018-11-30 19:16:26 +00:00
|
|
|
size_t const ret = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
|
2017-09-14 21:41:49 +00:00
|
|
|
FUZZ_ZASSERT(ret);
|
2017-06-29 23:53:52 +00:00
|
|
|
|
|
|
|
dst += out.pos;
|
|
|
|
dstSize += out.pos;
|
|
|
|
capacity -= out.pos;
|
2017-09-14 21:41:49 +00:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
return dstSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
|
|
|
|
{
|
2019-10-18 20:34:35 +00:00
|
|
|
size_t neededBufSize;
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
/* Give a random portion of src data to the producer, to use for
|
|
|
|
parameter generation. The rest will be used for (de)compression */
|
|
|
|
FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
|
2019-09-11 17:09:29 +00:00
|
|
|
size = FUZZ_dataProducer_reserveDataPrefix(producer);
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2019-10-18 20:34:35 +00:00
|
|
|
neededBufSize = ZSTD_compressBound(size) * 15;
|
2019-09-10 23:52:38 +00:00
|
|
|
|
2017-06-29 23:53:52 +00:00
|
|
|
/* Allocate all buffers and contexts if not already allocated */
|
|
|
|
if (neededBufSize > bufSize) {
|
|
|
|
free(cBuf);
|
|
|
|
free(rBuf);
|
|
|
|
cBuf = (uint8_t*)malloc(neededBufSize);
|
|
|
|
rBuf = (uint8_t*)malloc(neededBufSize);
|
|
|
|
bufSize = neededBufSize;
|
|
|
|
FUZZ_ASSERT(cBuf && rBuf);
|
|
|
|
}
|
2017-09-14 21:41:49 +00:00
|
|
|
if (!cctx) {
|
|
|
|
cctx = ZSTD_createCCtx();
|
|
|
|
FUZZ_ASSERT(cctx);
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
if (!dctx) {
|
|
|
|
dctx = ZSTD_createDCtx();
|
|
|
|
FUZZ_ASSERT(dctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-09-10 23:14:43 +00:00
|
|
|
size_t const cSize = compress(cBuf, neededBufSize, src, size, producer);
|
2017-06-29 23:53:52 +00:00
|
|
|
size_t const rSize =
|
|
|
|
ZSTD_decompressDCtx(dctx, rBuf, neededBufSize, cBuf, cSize);
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_ZASSERT(rSize);
|
2017-06-29 23:53:52 +00:00
|
|
|
FUZZ_ASSERT_MSG(rSize == size, "Incorrect regenerated size");
|
|
|
|
FUZZ_ASSERT_MSG(!memcmp(src, rBuf, size), "Corruption!");
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_dataProducer_free(producer);
|
2017-09-13 03:20:27 +00:00
|
|
|
#ifndef STATEFUL_FUZZING
|
2017-09-14 21:41:49 +00:00
|
|
|
ZSTD_freeCCtx(cctx); cctx = NULL;
|
2017-06-29 23:53:52 +00:00
|
|
|
ZSTD_freeDCtx(dctx); dctx = NULL;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|