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"
|
2017-06-29 23:53:52 +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 uint32_t seed;
|
|
|
|
|
|
|
|
static size_t roundTripTest(void *result, size_t resultCapacity,
|
|
|
|
void *compressed, size_t compressedCapacity,
|
|
|
|
const void *src, size_t srcSize)
|
|
|
|
{
|
2017-09-14 21:41:49 +00:00
|
|
|
size_t cSize;
|
|
|
|
if (FUZZ_rand(&seed) & 1) {
|
|
|
|
ZSTD_inBuffer in = {src, srcSize, 0};
|
|
|
|
ZSTD_outBuffer out = {compressed, compressedCapacity, 0};
|
2017-09-25 20:28:45 +00:00
|
|
|
size_t err;
|
2017-09-14 21:41:49 +00:00
|
|
|
|
2018-11-16 00:53:45 +00:00
|
|
|
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_setRandomParameters(cctx, srcSize, &seed);
|
2018-11-30 19:16:26 +00:00
|
|
|
err = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_ZASSERT(err);
|
|
|
|
FUZZ_ASSERT(err == 0);
|
2017-09-14 21:41:49 +00:00
|
|
|
cSize = out.pos;
|
|
|
|
} else {
|
|
|
|
int const cLevel = FUZZ_rand(&seed) % kMaxClevel;
|
|
|
|
cSize = ZSTD_compressCCtx(
|
|
|
|
cctx, compressed, compressedCapacity, src, srcSize, cLevel);
|
|
|
|
}
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_ZASSERT(cSize);
|
2017-09-14 21:41:49 +00:00
|
|
|
return ZSTD_decompressDCtx(dctx, result, resultCapacity, compressed, cSize);
|
2017-06-29 23:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
|
|
|
|
{
|
2017-09-13 03:20:27 +00:00
|
|
|
size_t neededBufSize;
|
2017-06-29 23:53:52 +00:00
|
|
|
|
2017-09-13 03:20:27 +00:00
|
|
|
seed = FUZZ_seed(&src, &size);
|
|
|
|
neededBufSize = ZSTD_compressBound(size);
|
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 = 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 =
|
|
|
|
roundTripTest(rBuf, neededBufSize, cBuf, neededBufSize, src, size);
|
2017-09-26 21:03:43 +00:00
|
|
|
FUZZ_ZASSERT(result);
|
2017-06-29 23:53:52 +00:00
|
|
|
FUZZ_ASSERT_MSG(result == size, "Incorrect regenerated size");
|
|
|
|
FUZZ_ASSERT_MSG(!memcmp(src, rBuf, size), "Corruption!");
|
|
|
|
}
|
2017-09-13 03:20:27 +00:00
|
|
|
#ifndef STATEFUL_FUZZING
|
2017-06-29 23:53:52 +00:00
|
|
|
ZSTD_freeCCtx(cctx); cctx = NULL;
|
|
|
|
ZSTD_freeDCtx(dctx); dctx = NULL;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|