1 /*
2 * Copyright (c) 2016-2020, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11 #define ZSTD_STATIC_LINKING_ONLY
12 #define ZDICT_STATIC_LINKING_ONLY
13
14 #include <string.h>
15
16 #include "zstd_helpers.h"
17 #include "fuzz_helpers.h"
18 #include "zstd.h"
19 #include "zdict.h"
20
21 const int kMinClevel = -3;
22 const int kMaxClevel = 19;
23
set(ZSTD_CCtx * cctx,ZSTD_cParameter param,int value)24 static void set(ZSTD_CCtx *cctx, ZSTD_cParameter param, int value)
25 {
26 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, param, value));
27 }
28
setRand(ZSTD_CCtx * cctx,ZSTD_cParameter param,unsigned min,unsigned max,FUZZ_dataProducer_t * producer)29 static void setRand(ZSTD_CCtx *cctx, ZSTD_cParameter param, unsigned min,
30 unsigned max, FUZZ_dataProducer_t *producer) {
31 unsigned const value = FUZZ_dataProducer_uint32Range(producer, min, max);
32 set(cctx, param, value);
33 }
34
FUZZ_randomCParams(size_t srcSize,FUZZ_dataProducer_t * producer)35 ZSTD_compressionParameters FUZZ_randomCParams(size_t srcSize, FUZZ_dataProducer_t *producer)
36 {
37 /* Select compression parameters */
38 ZSTD_compressionParameters cParams;
39 cParams.windowLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_WINDOWLOG_MIN, 15);
40 cParams.hashLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_HASHLOG_MIN, 15);
41 cParams.chainLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_CHAINLOG_MIN, 16);
42 cParams.searchLog = FUZZ_dataProducer_uint32Range(producer, ZSTD_SEARCHLOG_MIN, 9);
43 cParams.minMatch = FUZZ_dataProducer_uint32Range(producer, ZSTD_MINMATCH_MIN,
44 ZSTD_MINMATCH_MAX);
45 cParams.targetLength = FUZZ_dataProducer_uint32Range(producer, 0, 512);
46 cParams.strategy = FUZZ_dataProducer_uint32Range(producer, ZSTD_STRATEGY_MIN, ZSTD_STRATEGY_MAX);
47 return ZSTD_adjustCParams(cParams, srcSize, 0);
48 }
49
FUZZ_randomFParams(FUZZ_dataProducer_t * producer)50 ZSTD_frameParameters FUZZ_randomFParams(FUZZ_dataProducer_t *producer)
51 {
52 /* Select frame parameters */
53 ZSTD_frameParameters fParams;
54 fParams.contentSizeFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
55 fParams.checksumFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
56 fParams.noDictIDFlag = FUZZ_dataProducer_uint32Range(producer, 0, 1);
57 return fParams;
58 }
59
FUZZ_randomParams(size_t srcSize,FUZZ_dataProducer_t * producer)60 ZSTD_parameters FUZZ_randomParams(size_t srcSize, FUZZ_dataProducer_t *producer)
61 {
62 ZSTD_parameters params;
63 params.cParams = FUZZ_randomCParams(srcSize, producer);
64 params.fParams = FUZZ_randomFParams(producer);
65 return params;
66 }
67
FUZZ_setRandomParameters(ZSTD_CCtx * cctx,size_t srcSize,FUZZ_dataProducer_t * producer)68 void FUZZ_setRandomParameters(ZSTD_CCtx *cctx, size_t srcSize, FUZZ_dataProducer_t *producer)
69 {
70 ZSTD_compressionParameters cParams = FUZZ_randomCParams(srcSize, producer);
71 set(cctx, ZSTD_c_windowLog, cParams.windowLog);
72 set(cctx, ZSTD_c_hashLog, cParams.hashLog);
73 set(cctx, ZSTD_c_chainLog, cParams.chainLog);
74 set(cctx, ZSTD_c_searchLog, cParams.searchLog);
75 set(cctx, ZSTD_c_minMatch, cParams.minMatch);
76 set(cctx, ZSTD_c_targetLength, cParams.targetLength);
77 set(cctx, ZSTD_c_strategy, cParams.strategy);
78 /* Select frame parameters */
79 setRand(cctx, ZSTD_c_contentSizeFlag, 0, 1, producer);
80 setRand(cctx, ZSTD_c_checksumFlag, 0, 1, producer);
81 setRand(cctx, ZSTD_c_dictIDFlag, 0, 1, producer);
82 /* Select long distance matching parameters */
83 setRand(cctx, ZSTD_c_enableLongDistanceMatching, 0, 1, producer);
84 setRand(cctx, ZSTD_c_ldmHashLog, ZSTD_HASHLOG_MIN, 16, producer);
85 setRand(cctx, ZSTD_c_ldmMinMatch, ZSTD_LDM_MINMATCH_MIN,
86 ZSTD_LDM_MINMATCH_MAX, producer);
87 setRand(cctx, ZSTD_c_ldmBucketSizeLog, 0, ZSTD_LDM_BUCKETSIZELOG_MAX,
88 producer);
89 setRand(cctx, ZSTD_c_ldmHashRateLog, ZSTD_LDM_HASHRATELOG_MIN,
90 ZSTD_LDM_HASHRATELOG_MAX, producer);
91 /* Set misc parameters */
92 setRand(cctx, ZSTD_c_nbWorkers, 0, 2, producer);
93 setRand(cctx, ZSTD_c_rsyncable, 0, 1, producer);
94 setRand(cctx, ZSTD_c_forceMaxWindow, 0, 1, producer);
95 setRand(cctx, ZSTD_c_literalCompressionMode, 0, 2, producer);
96 setRand(cctx, ZSTD_c_forceAttachDict, 0, 2, producer);
97 if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
98 setRand(cctx, ZSTD_c_srcSizeHint, ZSTD_SRCSIZEHINT_MIN, 2 * srcSize, producer);
99 }
100 if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
101 setRand(cctx, ZSTD_c_targetCBlockSize, ZSTD_TARGETCBLOCKSIZE_MIN, ZSTD_TARGETCBLOCKSIZE_MAX, producer);
102 }
103 }
104
FUZZ_train(void const * src,size_t srcSize,FUZZ_dataProducer_t * producer)105 FUZZ_dict_t FUZZ_train(void const* src, size_t srcSize, FUZZ_dataProducer_t *producer)
106 {
107 size_t const dictSize = MAX(srcSize / 8, 1024);
108 size_t const totalSampleSize = dictSize * 11;
109 FUZZ_dict_t dict = { FUZZ_malloc(dictSize), dictSize };
110 char* const samples = (char*)FUZZ_malloc(totalSampleSize);
111 unsigned nbSamples = 100;
112 size_t* const samplesSizes = (size_t*)FUZZ_malloc(sizeof(size_t) * nbSamples);
113 size_t pos = 0;
114 size_t sample = 0;
115 ZDICT_fastCover_params_t params;
116
117 for (sample = 0; sample < nbSamples; ++sample) {
118 size_t const remaining = totalSampleSize - pos;
119 size_t const offset = FUZZ_dataProducer_uint32Range(producer, 0, MAX(srcSize, 1) - 1);
120 size_t const limit = MIN(srcSize - offset, remaining);
121 size_t const toCopy = MIN(limit, remaining / (nbSamples - sample));
122 memcpy(samples + pos, src + offset, toCopy);
123 pos += toCopy;
124 samplesSizes[sample] = toCopy;
125 }
126 memset(samples + pos, 0, totalSampleSize - pos);
127
128 memset(¶ms, 0, sizeof(params));
129 params.accel = 5;
130 params.k = 40;
131 params.d = 8;
132 params.f = 14;
133 params.zParams.compressionLevel = 1;
134 dict.size = ZDICT_trainFromBuffer_fastCover(dict.buff, dictSize,
135 samples, samplesSizes, nbSamples, params);
136 if (ZSTD_isError(dict.size)) {
137 free(dict.buff);
138 memset(&dict, 0, sizeof(dict));
139 }
140
141 free(samplesSizes);
142 free(samples);
143
144 return dict;
145 }
146