2019-04-09 04:07:28 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Facebook, Inc.
|
2019-04-09 04:07:28 +00:00
|
|
|
* 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).
|
2020-03-26 22:19:05 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2019-04-09 04:07:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This fuzz target performs a zstd round-trip test (compress & decompress) with
|
|
|
|
* a dictionary, compares the result with the original, and calls abort() on
|
|
|
|
* corruption.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "fuzz_helpers.h"
|
|
|
|
#include "zstd_helpers.h"
|
2019-09-10 23:14:43 +00:00
|
|
|
#include "fuzz_data_producer.h"
|
2019-04-09 04:07:28 +00:00
|
|
|
|
|
|
|
static ZSTD_CCtx *cctx = NULL;
|
|
|
|
static ZSTD_DCtx *dctx = NULL;
|
|
|
|
|
|
|
|
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,
|
|
|
|
FUZZ_dataProducer_t *producer)
|
2019-04-09 04:07:28 +00:00
|
|
|
{
|
2019-04-10 01:02:22 +00:00
|
|
|
ZSTD_dictContentType_e dictContentType = ZSTD_dct_auto;
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_dict_t dict = FUZZ_train(src, srcSize, producer);
|
2020-04-07 05:57:49 +00:00
|
|
|
int const refPrefix = FUZZ_dataProducer_uint32Range(producer, 0, 1) != 0;
|
2019-04-09 04:07:28 +00:00
|
|
|
size_t cSize;
|
2019-09-12 19:40:12 +00:00
|
|
|
if (FUZZ_dataProducer_uint32Range(producer, 0, 15) == 0) {
|
|
|
|
int const cLevel = FUZZ_dataProducer_int32Range(producer, kMinClevel, kMaxClevel);
|
2019-04-09 04:07:28 +00:00
|
|
|
|
|
|
|
cSize = ZSTD_compress_usingDict(cctx,
|
|
|
|
compressed, compressedCapacity,
|
|
|
|
src, srcSize,
|
|
|
|
dict.buff, dict.size,
|
|
|
|
cLevel);
|
|
|
|
} else {
|
2019-09-10 23:14:43 +00:00
|
|
|
dictContentType = FUZZ_dataProducer_uint32Range(producer, 0, 2);
|
|
|
|
FUZZ_setRandomParameters(cctx, srcSize, producer);
|
2019-04-09 23:47:59 +00:00
|
|
|
/* Disable checksum so we can use sizes smaller than compress bound. */
|
|
|
|
FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0));
|
2020-04-07 05:57:49 +00:00
|
|
|
if (refPrefix)
|
|
|
|
FUZZ_ZASSERT(ZSTD_CCtx_refPrefix_advanced(
|
|
|
|
cctx, dict.buff, dict.size,
|
|
|
|
dictContentType));
|
|
|
|
else
|
|
|
|
FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary_advanced(
|
2019-04-10 01:02:22 +00:00
|
|
|
cctx, dict.buff, dict.size,
|
2019-09-10 23:14:43 +00:00
|
|
|
(ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
|
2019-04-10 01:02:22 +00:00
|
|
|
dictContentType));
|
2019-04-09 04:07:28 +00:00
|
|
|
cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
|
|
|
|
}
|
|
|
|
FUZZ_ZASSERT(cSize);
|
2020-04-07 05:57:49 +00:00
|
|
|
if (refPrefix)
|
|
|
|
FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
|
|
|
|
dctx, dict.buff, dict.size,
|
|
|
|
dictContentType));
|
|
|
|
else
|
|
|
|
FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
|
|
|
|
dctx, dict.buff, dict.size,
|
|
|
|
(ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
|
|
|
|
dictContentType));
|
2019-04-09 04:07:28 +00:00
|
|
|
{
|
2019-04-10 01:02:22 +00:00
|
|
|
size_t const ret = ZSTD_decompressDCtx(
|
|
|
|
dctx, result, resultCapacity, compressed, cSize);
|
2019-04-09 04:07:28 +00:00
|
|
|
free(dict.buff);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
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-04-09 23:47:59 +00:00
|
|
|
size_t const rBufSize = size;
|
|
|
|
void* rBuf = malloc(rBufSize);
|
2019-11-05 20:54:05 +00:00
|
|
|
size_t cBufSize = ZSTD_compressBound(size) * 2;
|
2019-09-10 23:52:38 +00:00
|
|
|
void *cBuf;
|
2019-04-09 23:47:59 +00:00
|
|
|
/* Half of the time fuzz with a 1 byte smaller output size.
|
|
|
|
* This will still succeed because we force the checksum to be disabled,
|
|
|
|
* giving us 4 bytes of overhead.
|
|
|
|
*/
|
2019-09-10 23:14:43 +00:00
|
|
|
cBufSize -= FUZZ_dataProducer_uint32Range(producer, 0, 1);
|
2019-04-09 23:47:59 +00:00
|
|
|
cBuf = malloc(cBufSize);
|
2019-04-09 04:07:28 +00:00
|
|
|
|
|
|
|
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, rBufSize, cBuf, cBufSize, src, size, producer);
|
2019-04-09 04:07:28 +00:00
|
|
|
FUZZ_ZASSERT(result);
|
|
|
|
FUZZ_ASSERT_MSG(result == size, "Incorrect regenerated size");
|
|
|
|
FUZZ_ASSERT_MSG(!memcmp(src, rBuf, size), "Corruption!");
|
|
|
|
}
|
2019-04-09 23:47:59 +00:00
|
|
|
free(rBuf);
|
|
|
|
free(cBuf);
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_dataProducer_free(producer);
|
2019-04-09 04:07:28 +00:00
|
|
|
#ifndef STATEFUL_FUZZING
|
|
|
|
ZSTD_freeCCtx(cctx); cctx = NULL;
|
|
|
|
ZSTD_freeDCtx(dctx); dctx = NULL;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|