2017-09-14 21:41:49 +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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
2019-04-09 04:07:28 +00:00
|
|
|
#define ZDICT_STATIC_LINKING_ONLY
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-09-14 21:41:49 +00:00
|
|
|
|
|
|
|
#include "zstd_helpers.h"
|
|
|
|
#include "fuzz_helpers.h"
|
|
|
|
#include "zstd.h"
|
2019-04-09 04:07:28 +00:00
|
|
|
#include "zdict.h"
|
2017-09-14 21:41:49 +00:00
|
|
|
|
2019-09-12 19:40:12 +00:00
|
|
|
const int kMinClevel = -3;
|
|
|
|
const int kMaxClevel = 19;
|
|
|
|
|
2018-12-06 23:00:52 +00:00
|
|
|
static void set(ZSTD_CCtx *cctx, ZSTD_cParameter param, int value)
|
2017-09-26 21:03:43 +00:00
|
|
|
{
|
|
|
|
FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, param, value));
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:41:49 +00:00
|
|
|
static void setRand(ZSTD_CCtx *cctx, ZSTD_cParameter param, unsigned min,
|
2019-09-10 23:14:43 +00:00
|
|
|
unsigned max, FUZZ_dataProducer_t *producer) {
|
|
|
|
unsigned const value = FUZZ_dataProducer_uint32Range(producer, min, max);
|
2017-09-26 21:03:43 +00:00
|
|
|
set(cctx, param, value);
|
2017-09-14 21:41:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_compressionParameters FUZZ_randomCParams(size_t srcSize, FUZZ_dataProducer_t *producer)
|
2017-09-14 21:41:49 +00:00
|
|
|
{
|
2017-09-26 21:03:43 +00:00
|
|
|
/* Select compression parameters */
|
|
|
|
ZSTD_compressionParameters cParams;
|
2019-09-10 23:14:43 +00:00
|
|
|
cParams.windowLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_WINDOWLOG_MIN, 15);
|
|
|
|
cParams.hashLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_HASHLOG_MIN, 15);
|
|
|
|
cParams.chainLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_CHAINLOG_MIN, 16);
|
|
|
|
cParams.searchLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_SEARCHLOG_MIN, 9);
|
|
|
|
cParams.minMatch = FUZZ_dataProducer_uint32Range(producer, ZSTD_MINMATCH_MIN,
|
2018-11-20 23:13:27 +00:00
|
|
|
ZSTD_MINMATCH_MAX);
|
2019-09-10 23:14:43 +00:00
|
|
|
cParams.targetLength = FUZZ_dataProducer_uint32Range(producer, 0, 512);
|
|
|
|
cParams.strategy = FUZZ_dataProducer_uint32Range(producer, ZSTD_STRATEGY_MIN, ZSTD_STRATEGY_MAX);
|
2017-09-26 21:03:43 +00:00
|
|
|
return ZSTD_adjustCParams(cParams, srcSize, 0);
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_frameParameters FUZZ_randomFParams(FUZZ_dataProducer_t *producer)
|
2017-09-26 21:03:43 +00:00
|
|
|
{
|
|
|
|
/* Select frame parameters */
|
|
|
|
ZSTD_frameParameters fParams;
|
2019-09-10 23:14:43 +00:00
|
|
|
fParams.contentSizeFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
|
|
|
|
fParams.checksumFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
|
|
|
|
fParams.noDictIDFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
|
2017-09-26 21:03:43 +00:00
|
|
|
return fParams;
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_parameters FUZZ_randomParams(size_t srcSize, FUZZ_dataProducer_t *producer)
|
2017-09-26 21:03:43 +00:00
|
|
|
{
|
|
|
|
ZSTD_parameters params;
|
2019-09-10 23:14:43 +00:00
|
|
|
params.cParams = FUZZ_randomCParams(srcSize, producer);
|
|
|
|
params.fParams = FUZZ_randomFParams(producer);
|
2017-09-26 21:03:43 +00:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
void FUZZ_setRandomParameters(ZSTD_CCtx *cctx, size_t srcSize, FUZZ_dataProducer_t *producer)
|
2017-09-26 21:03:43 +00:00
|
|
|
{
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_compressionParameters cParams = FUZZ_randomCParams(srcSize, producer);
|
2018-12-06 01:26:02 +00:00
|
|
|
set(cctx, ZSTD_c_windowLog, cParams.windowLog);
|
|
|
|
set(cctx, ZSTD_c_hashLog, cParams.hashLog);
|
|
|
|
set(cctx, ZSTD_c_chainLog, cParams.chainLog);
|
|
|
|
set(cctx, ZSTD_c_searchLog, cParams.searchLog);
|
|
|
|
set(cctx, ZSTD_c_minMatch, cParams.minMatch);
|
|
|
|
set(cctx, ZSTD_c_targetLength, cParams.targetLength);
|
2018-12-06 23:00:52 +00:00
|
|
|
set(cctx, ZSTD_c_strategy, cParams.strategy);
|
2017-09-14 21:41:49 +00:00
|
|
|
/* Select frame parameters */
|
2019-09-10 23:14:43 +00:00
|
|
|
setRand(cctx, ZSTD_c_contentSizeFlag, 0, 1, producer);
|
|
|
|
setRand(cctx, ZSTD_c_checksumFlag, 0, 1, producer);
|
|
|
|
setRand(cctx, ZSTD_c_dictIDFlag, 0, 1, producer);
|
2019-04-12 18:18:11 +00:00
|
|
|
/* Select long distance matching parameters */
|
2019-09-10 23:14:43 +00:00
|
|
|
setRand(cctx, ZSTD_c_enableLongDistanceMatching, 0, 1, producer);
|
|
|
|
setRand(cctx, ZSTD_c_ldmHashLog, ZSTD_HASHLOG_MIN, 16, producer);
|
2018-12-06 01:26:02 +00:00
|
|
|
setRand(cctx, ZSTD_c_ldmMinMatch, ZSTD_LDM_MINMATCH_MIN,
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_LDM_MINMATCH_MAX, producer);
|
2018-12-06 01:26:02 +00:00
|
|
|
setRand(cctx, ZSTD_c_ldmBucketSizeLog, 0, ZSTD_LDM_BUCKETSIZELOG_MAX,
|
2019-09-10 23:14:43 +00:00
|
|
|
producer);
|
2018-12-06 01:26:02 +00:00
|
|
|
setRand(cctx, ZSTD_c_ldmHashRateLog, ZSTD_LDM_HASHRATELOG_MIN,
|
2019-09-10 23:14:43 +00:00
|
|
|
ZSTD_LDM_HASHRATELOG_MAX, producer);
|
2019-04-09 04:07:28 +00:00
|
|
|
/* Set misc parameters */
|
2019-09-10 23:14:43 +00:00
|
|
|
setRand(cctx, ZSTD_c_nbWorkers, 0, 2, producer);
|
|
|
|
setRand(cctx, ZSTD_c_rsyncable, 0, 1, producer);
|
|
|
|
setRand(cctx, ZSTD_c_forceMaxWindow, 0, 1, producer);
|
|
|
|
setRand(cctx, ZSTD_c_literalCompressionMode, 0, 2, producer);
|
|
|
|
setRand(cctx, ZSTD_c_forceAttachDict, 0, 2, producer);
|
|
|
|
if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
|
|
|
|
setRand(cctx, ZSTD_c_srcSizeHint, ZSTD_SRCSIZEHINT_MIN, 2 * srcSize, producer);
|
2019-08-19 22:12:24 +00:00
|
|
|
}
|
2019-04-09 04:07:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 23:14:43 +00:00
|
|
|
FUZZ_dict_t FUZZ_train(void const* src, size_t srcSize, FUZZ_dataProducer_t *producer)
|
2019-04-09 04:07:28 +00:00
|
|
|
{
|
|
|
|
size_t const dictSize = MAX(srcSize / 8, 1024);
|
|
|
|
size_t const totalSampleSize = dictSize * 11;
|
|
|
|
FUZZ_dict_t dict = { malloc(dictSize), dictSize };
|
|
|
|
char* const samples = (char*)malloc(totalSampleSize);
|
|
|
|
unsigned nbSamples = 100;
|
|
|
|
size_t* const samplesSizes = (size_t*)malloc(sizeof(size_t) * nbSamples);
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t sample = 0;
|
|
|
|
ZDICT_fastCover_params_t params;
|
|
|
|
FUZZ_ASSERT(dict.buff && samples && samplesSizes);
|
|
|
|
|
|
|
|
for (sample = 0; sample < nbSamples; ++sample) {
|
|
|
|
size_t const remaining = totalSampleSize - pos;
|
2019-09-10 23:14:43 +00:00
|
|
|
size_t const offset = FUZZ_dataProducer_uint32Range(producer, 0, MAX(srcSize, 1) - 1);
|
2019-04-09 04:07:28 +00:00
|
|
|
size_t const limit = MIN(srcSize - offset, remaining);
|
|
|
|
size_t const toCopy = MIN(limit, remaining / (nbSamples - sample));
|
|
|
|
memcpy(samples + pos, src + offset, toCopy);
|
|
|
|
pos += toCopy;
|
|
|
|
samplesSizes[sample] = toCopy;
|
|
|
|
|
|
|
|
}
|
|
|
|
memset(samples + pos, 0, totalSampleSize - pos);
|
|
|
|
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
params.accel = 5;
|
|
|
|
params.k = 40;
|
|
|
|
params.d = 8;
|
|
|
|
params.f = 14;
|
|
|
|
params.zParams.compressionLevel = 1;
|
|
|
|
dict.size = ZDICT_trainFromBuffer_fastCover(dict.buff, dictSize,
|
|
|
|
samples, samplesSizes, nbSamples, params);
|
|
|
|
if (ZSTD_isError(dict.size)) {
|
|
|
|
free(dict.buff);
|
|
|
|
memset(&dict, 0, sizeof(dict));
|
|
|
|
}
|
|
|
|
|
|
|
|
free(samplesSizes);
|
|
|
|
free(samples);
|
|
|
|
|
|
|
|
return dict;
|
2017-09-14 21:41:49 +00:00
|
|
|
}
|