2017-09-14 00:44:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This fuzz target performs a zstd round-trip test (compress & decompress),
|
|
|
|
* compares the result with the original, and calls abort() on corruption.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "fuzz_helpers.h"
|
|
|
|
#include "zstd.h"
|
2019-09-10 23:14:43 +00:00
|
|
|
#include "fuzz_data_producer.h"
|
2017-09-14 00:44:41 +00:00
|
|
|
|
|
|
|
static const int kMaxClevel = 19;
|
|
|
|
|
|
|
|
static ZSTD_CCtx *cctx = NULL;
|
|
|
|
static ZSTD_DCtx *dctx = NULL;
|
|
|
|
static void* cBuf = NULL;
|
|
|
|
static void* rBuf = NULL;
|
|
|
|
static size_t bufSize = 0;
|
|
|
|
|
|
|
|
static size_t roundTripTest(void *result, size_t resultCapacity,
|
|
|
|
void *compressed, size_t compressedCapacity,
|
2019-09-10 23:14:43 +00:00
|
|
|
const void *src, size_t srcSize,
|
|
|
|
int cLevel)
|
2017-09-14 00:44:41 +00:00
|
|
|
{
|
2017-09-26 21:03:43 +00:00
|
|
|
ZSTD_parameters const params = ZSTD_getParams(cLevel, srcSize, 0);
|
|
|
|
size_t ret = ZSTD_compressBegin_advanced(cctx, NULL, 0, params, srcSize);
|
|
|
|
FUZZ_ZASSERT(ret);
|
2017-09-14 00:44:41 +00:00
|
|
|
|
2017-09-26 21:03:43 +00:00
|
|
|
ret = ZSTD_compressBlock(cctx, compressed, compressedCapacity, src, srcSize);
|
|
|
|
FUZZ_ZASSERT(ret);
|
|
|
|
if (ret == 0) {
|
2017-09-14 00:44:41 +00:00
|
|
|
FUZZ_ASSERT(resultCapacity >= srcSize);
|
|
|
|
memcpy(result, src, srcSize);
|
|
|
|
return srcSize;
|
2017-09-26 21:03:43 +00:00
|
|
|
}
|
|
|
|
ZSTD_decompressBegin(dctx);
|
|
|
|
return ZSTD_decompressBlock(dctx, result, resultCapacity, compressed, ret);
|
2017-09-14 00:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
|
|
|
|
{
|
2019-09-10 23:52:38 +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 */
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
|
2019-09-11 17:09:29 +00:00
|
|
|
size = FUZZ_dataProducer_reserveDataPrefix(producer);
|
2019-09-10 23:52:38 +00:00
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
int cLevel = FUZZ_dataProducer_uint32(producer) % kMaxClevel;
|
2017-09-14 00:44:41 +00:00
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
size_t neededBufSize = size;
|
2017-09-14 00:44:41 +00:00
|
|
|
if (size > ZSTD_BLOCKSIZE_MAX)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Allocate all buffers and contexts if not already allocated */
|
|
|
|
if (neededBufSize > bufSize || !cBuf || !rBuf) {
|
|
|
|
free(cBuf);
|
|
|
|
free(rBuf);
|
|
|
|
cBuf = malloc(neededBufSize);
|
|
|
|
rBuf = malloc(neededBufSize);
|
|
|
|
bufSize = neededBufSize;
|
|
|
|
FUZZ_ASSERT(cBuf && rBuf);
|
|
|
|
}
|
|
|
|
if (!cctx) {
|
|
|
|
cctx = ZSTD_createCCtx();
|
|
|
|
FUZZ_ASSERT(cctx);
|
|
|
|
}
|
|
|
|
if (!dctx) {
|
|
|
|
dctx = ZSTD_createDCtx();
|
|
|
|
FUZZ_ASSERT(dctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
size_t const result =
|
2019-09-10 23:14:43 +00:00
|
|
|
roundTripTest(rBuf, neededBufSize, cBuf, neededBufSize, src, size,
|
|
|
|
cLevel);
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_ZASSERT(result);
|
2017-09-14 00:44:41 +00:00
|
|
|
FUZZ_ASSERT_MSG(result == 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-14 00:44:41 +00:00
|
|
|
#ifndef STATEFUL_FUZZING
|
|
|
|
ZSTD_freeCCtx(cctx); cctx = NULL;
|
|
|
|
ZSTD_freeDCtx(dctx); dctx = NULL;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|