[fuzzer] Use ZSTD_DCtx_loadDictionary_advanced() half the time

This commit is contained in:
Nick Terrell 2019-04-09 18:02:22 -07:00
parent 10a3d4dca9
commit c45dec12c5
2 changed files with 22 additions and 8 deletions

View File

@ -25,6 +25,7 @@ static size_t bufSize = 0;
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size) int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
{ {
FUZZ_dict_t dict;
size_t neededBufSize; size_t neededBufSize;
uint32_t seed = FUZZ_seed(&src, &size); uint32_t seed = FUZZ_seed(&src, &size);
@ -41,15 +42,21 @@ int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
dctx = ZSTD_createDCtx(); dctx = ZSTD_createDCtx();
FUZZ_ASSERT(dctx); FUZZ_ASSERT(dctx);
} }
{ dict = FUZZ_train(src, size, &seed);
FUZZ_dict_t dict = FUZZ_train(src, size, &seed); if (FUZZ_rand32(&seed, 0, 1) == 0) {
ZSTD_decompress_usingDict(dctx, ZSTD_decompress_usingDict(dctx,
rBuf, neededBufSize, rBuf, neededBufSize,
src, size, src, size,
dict.buff, dict.size); dict.buff, dict.size);
free(dict.buff); } else {
FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
dctx, dict.buff, dict.size,
(ZSTD_dictLoadMethod_e)FUZZ_rand32(&seed, 0, 1),
(ZSTD_dictContentType_e)FUZZ_rand32(&seed, 0, 2)));
ZSTD_decompressDCtx(dctx, rBuf, neededBufSize, src, size);
} }
free(dict.buff);
#ifndef STATEFUL_FUZZING #ifndef STATEFUL_FUZZING
ZSTD_freeDCtx(dctx); dctx = NULL; ZSTD_freeDCtx(dctx); dctx = NULL;
#endif #endif

View File

@ -30,6 +30,7 @@ static size_t roundTripTest(void *result, size_t resultCapacity,
void *compressed, size_t compressedCapacity, void *compressed, size_t compressedCapacity,
const void *src, size_t srcSize) const void *src, size_t srcSize)
{ {
ZSTD_dictContentType_e dictContentType = ZSTD_dct_auto;
FUZZ_dict_t dict = FUZZ_train(src, srcSize, &seed); FUZZ_dict_t dict = FUZZ_train(src, srcSize, &seed);
size_t cSize; size_t cSize;
if ((FUZZ_rand(&seed) & 15) == 0) { if ((FUZZ_rand(&seed) & 15) == 0) {
@ -41,18 +42,24 @@ static size_t roundTripTest(void *result, size_t resultCapacity,
dict.buff, dict.size, dict.buff, dict.size,
cLevel); cLevel);
} else { } else {
dictContentType = FUZZ_rand32(&seed, 0, 2);
FUZZ_setRandomParameters(cctx, srcSize, &seed); FUZZ_setRandomParameters(cctx, srcSize, &seed);
/* Disable checksum so we can use sizes smaller than compress bound. */ /* Disable checksum so we can use sizes smaller than compress bound. */
FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0)); FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0));
FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary(cctx, dict.buff, dict.size)); FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary_advanced(
cctx, dict.buff, dict.size,
(ZSTD_dictLoadMethod_e)FUZZ_rand32(&seed, 0, 1),
dictContentType));
cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize); cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
} }
FUZZ_ZASSERT(cSize); FUZZ_ZASSERT(cSize);
FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
dctx, dict.buff, dict.size,
(ZSTD_dictLoadMethod_e)FUZZ_rand32(&seed, 0, 1),
dictContentType));
{ {
size_t const ret = ZSTD_decompress_usingDict(dctx, size_t const ret = ZSTD_decompressDCtx(
result, resultCapacity, dctx, result, resultCapacity, compressed, cSize);
compressed, cSize,
dict.buff, dict.size);
free(dict.buff); free(dict.buff);
return ret; return ret;
} }