blob: aad9cba71deabf92491c7f91b1000571363f74a0 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Colletd7883a22016-08-12 16:48:02 +020010
Yann Colletd7883a22016-08-12 16:48:02 +020011
12/*-************************************
13* Compiler specific
14**************************************/
15#ifdef _MSC_VER /* Visual Studio */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070016# define _CRT_SECURE_NO_WARNINGS /* fgets */
17# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
18# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
Yann Colletd7883a22016-08-12 16:48:02 +020019#endif
20
21
22/*-************************************
23* Includes
24**************************************/
25#include <stdlib.h> /* free */
26#include <stdio.h> /* fgets, sscanf */
Yann Colletd7883a22016-08-12 16:48:02 +020027#include <string.h> /* strcmp */
Yann Collet01743a32017-06-16 17:56:41 -070028#include <assert.h> /* assert */
Yann Colletd7883a22016-08-12 16:48:02 +020029#include "mem.h"
Yann Colleta5ffe3d2017-05-12 16:29:19 -070030#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
Yann Colletd7883a22016-08-12 16:48:02 +020031#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010032#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Collet736788f2017-01-19 12:12:50 -080033#include "zstdmt_compress.h"
Yann Collet33fce032017-01-16 19:46:22 -080034#include "zdict.h" /* ZDICT_trainFromBuffer */
Yann Colletd7883a22016-08-12 16:48:02 +020035#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070036#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020037#include "xxhash.h" /* XXH64_* */
Nick Terrelle600b5d2017-10-16 17:18:43 -070038#include "seqgen.h"
Nick Terrell9a2f6f42017-11-29 19:11:12 -080039#include "util.h"
Yann Colletd7883a22016-08-12 16:48:02 +020040
41
42/*-************************************
43* Constants
44**************************************/
45#define KB *(1U<<10)
46#define MB *(1U<<20)
47#define GB *(1U<<30)
48
49static const U32 nbTestsDefault = 10000;
Yann Colletf99c2c12017-06-21 23:35:58 -070050static const U32 g_cLevelMax_smallTests = 10;
Yann Colletd7883a22016-08-12 16:48:02 +020051#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
52#define FUZ_COMPRESSIBILITY_DEFAULT 50
Yann Collet33fce032017-01-16 19:46:22 -080053static const U32 prime32 = 2654435761U;
Yann Colletd7883a22016-08-12 16:48:02 +020054
55
Yann Colletd7883a22016-08-12 16:48:02 +020056/*-************************************
57* Display Macros
58**************************************/
59#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet0be6fd32017-05-08 16:08:01 -070060#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
61 DISPLAY(__VA_ARGS__); \
62 if (g_displayLevel>=4) fflush(stderr); }
Yann Colletd7883a22016-08-12 16:48:02 +020063static U32 g_displayLevel = 2;
64
Nick Terrell9a2f6f42017-11-29 19:11:12 -080065#define SEC_TO_MICRO 1000000
66static const U64 g_refreshRate = SEC_TO_MICRO / 6;
67static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
Yann Colletd7883a22016-08-12 16:48:02 +020068
Nick Terrell9a2f6f42017-11-29 19:11:12 -080069#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
70 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
71 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
72 if (g_displayLevel>=4) fflush(stderr); } }
73
74static U64 g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020075
76
77/*-*******************************************************
78* Fuzzer functions
79*********************************************************/
Nick Terrell9a2f6f42017-11-29 19:11:12 -080080#undef MIN
81#undef MAX
82#define MIN(a,b) ((a)<(b)?(a):(b))
Yann Colletd7883a22016-08-12 16:48:02 +020083#define MAX(a,b) ((a)>(b)?(a):(b))
Yann Colletd7883a22016-08-12 16:48:02 +020084/*! FUZ_rand() :
85 @return : a 27 bits random value, from a 32-bits `seed`.
86 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070087#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020088unsigned int FUZ_rand(unsigned int* seedPtr)
89{
Yann Collet33fce032017-01-16 19:46:22 -080090 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020091 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080092 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020093 rand32 += prime2;
94 rand32 = FUZ_rotl32(rand32, 13);
95 *seedPtr = rand32;
96 return rand32 >> 5;
97}
98
Nick Terrelle600b5d2017-10-16 17:18:43 -070099#define CHECK(cond, ...) { \
100 if (cond) { \
101 DISPLAY("Error => "); \
102 DISPLAY(__VA_ARGS__); \
103 DISPLAY(" (seed %u, test nb %u, line %u) \n", \
104 seed, testNb, __LINE__); \
Yann Colletd6770f82017-09-28 02:14:48 -0700105 goto _output_error; \
106} }
107
Nick Terrelle600b5d2017-10-16 17:18:43 -0700108#define CHECK_Z(f) { \
109 size_t const err = f; \
110 CHECK(ZSTD_isError(err), "%s : %s ", \
111 #f, ZSTD_getErrorName(err)); \
112}
113
Yann Colletcb327632016-08-23 00:30:31 +0200114
115/*======================================================
116* Basic Unit tests
117======================================================*/
118
Yann Collet33fce032017-01-16 19:46:22 -0800119typedef struct {
120 void* start;
121 size_t size;
122 size_t filled;
123} buffer_t;
124
125static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
126
127static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
128{
129 buffer_t dict = { NULL, 0, 0 };
130 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
131 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
132 if (!blockSizes) return dict;
133 dict.start = malloc(requestedDictSize);
134 if (!dict.start) { free(blockSizes); return dict; }
135 { size_t nb;
136 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
137 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
138 }
139 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
140 free(blockSizes);
Yann Collet2c5514c2017-04-18 22:52:41 -0700141 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
Yann Collet33fce032017-01-16 19:46:22 -0800142 dict.size = requestedDictSize;
143 dict.filled = dictSize;
144 return dict; /* how to return dictSize ? */
145 }
146}
147
148static void FUZ_freeDictionary(buffer_t dict)
149{
150 free(dict.start);
151}
152
Nick Terrelle600b5d2017-10-16 17:18:43 -0700153/* Round trips data and updates xxh with the decompressed data produced */
154static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
155 XXH64_state_t* xxh, void* data, size_t size,
156 ZSTD_EndDirective endOp)
157{
158 static BYTE compressed[1024];
159 static BYTE uncompressed[1024];
160
161 ZSTD_inBuffer cin = {data, size, 0};
162 size_t cret;
163
164 do {
165 ZSTD_outBuffer cout = {compressed, sizeof(compressed), 0};
166 ZSTD_inBuffer din = {compressed, 0, 0};
167 ZSTD_outBuffer dout = {uncompressed, 0, 0};
168
169 cret = ZSTD_compress_generic(cctx, &cout, &cin, endOp);
170 if (ZSTD_isError(cret))
171 return cret;
172
173 din.size = cout.pos;
174 while (din.pos < din.size || (endOp == ZSTD_e_end && cret == 0)) {
175 size_t dret;
176
177 dout.pos = 0;
178 dout.size = sizeof(uncompressed);
179 dret = ZSTD_decompressStream(dctx, &dout, &din);
180 if (ZSTD_isError(dret))
181 return dret;
182 XXH64_update(xxh, dout.dst, dout.pos);
183 if (dret == 0)
184 break;
185 }
186 } while (cin.pos < cin.size || (endOp != ZSTD_e_continue && cret != 0));
187 return 0;
188}
189
190/* Generates some data and round trips it */
191static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
192 XXH64_state_t* xxh, SEQ_stream* seq,
193 SEQ_gen_type type, unsigned value)
194{
195 static BYTE data[1024];
196 size_t gen;
197
198 do {
199 SEQ_outBuffer sout = {data, sizeof(data), 0};
200 size_t ret;
201 gen = SEQ_gen(seq, type, value, &sout);
202
203 ret = SEQ_roundTrip(cctx, dctx, xxh, sout.dst, sout.pos, ZSTD_e_continue);
204 if (ZSTD_isError(ret))
205 return ret;
206 } while (gen != 0);
207
208 return 0;
209}
Yann Collet33fce032017-01-16 19:46:22 -0800210
Yann Colletbd88f632017-11-27 12:15:23 -0800211static int basicUnitTests(U32 seed, double compressibility)
Yann Colletd7883a22016-08-12 16:48:02 +0200212{
Yann Colletb3060f72016-09-09 16:44:16 +0200213 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200214 void* CNBuffer = malloc(CNBufferSize);
Nick Terrelle19b0822017-11-01 13:10:03 -0700215 size_t const skippableFrameSize = 200 KB;
Yann Colletd7883a22016-08-12 16:48:02 +0200216 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
217 void* compressedBuffer = malloc(compressedBufferSize);
218 size_t const decodedBufferSize = CNBufferSize;
219 void* decodedBuffer = malloc(decodedBufferSize);
220 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200221 int testResult = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700222 U32 testNb = 1;
Yann Colletbd88f632017-11-27 12:15:23 -0800223 ZSTD_CStream* zc = ZSTD_createCStream();
224 ZSTD_DStream* zd = ZSTD_createDStream();
Stella Lau90a31bf2017-08-30 14:36:54 -0700225 ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
226
Yann Collet9ffbeea2016-12-02 18:37:38 -0800227 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200228 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800229 buffer_t dictionary = g_nullBuffer;
Yann Collet30ab64e2017-05-10 11:30:19 -0700230 size_t const dictSize = 128 KB;
Yann Collet33fce032017-01-16 19:46:22 -0800231 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200232
233 /* Create compressible test buffer */
234 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800235 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200236 goto _output_error;
237 }
238 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
239
Yann Collet33fce032017-01-16 19:46:22 -0800240 /* Create dictionary */
Yann Collete0065cf2017-09-28 18:34:38 -0700241 DISPLAYLEVEL(3, "creating dictionary for unit tests \n");
242 dictionary = FUZ_createDictionary(CNBuffer, CNBufferSize / 2, 8 KB, 40 KB);
Yann Collet33fce032017-01-16 19:46:22 -0800243 if (!dictionary.start) {
244 DISPLAY("Error creating dictionary, aborting \n");
245 goto _output_error;
246 }
247 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
248
Yann Colletd7883a22016-08-12 16:48:02 +0200249 /* generate skippable frame */
250 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
251 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
252 cSize = skippableFrameSize + 8;
253
254 /* Basic compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800255 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet3c1e3f82017-10-13 18:32:06 -0700256 CHECK_Z( ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1 /* cLevel */) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200257 outBuff.dst = (char*)(compressedBuffer)+cSize;
Yann Collet3c1e3f82017-10-13 18:32:06 -0700258 assert(compressedBufferSize > cSize);
259 outBuff.size = compressedBufferSize - cSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200260 outBuff.pos = 0;
261 inBuff.src = CNBuffer;
262 inBuff.size = CNBufferSize;
263 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700264 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200265 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
266 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200267 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200268 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800269 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200270
Yann Collet30ab64e2017-05-10 11:30:19 -0700271 /* context size functions */
272 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
273 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
Yann Collet96f0cde2017-09-24 16:47:02 -0700274 size_t const cstreamSize = ZSTD_estimateCStreamSize_usingCParams(cParams);
275 size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); /* uses ZSTD_initCStream_usingDict() */
276 if (ZSTD_isError(cstreamSize)) goto _output_error;
277 if (ZSTD_isError(cdictSize)) goto _output_error;
278 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)(cstreamSize + cdictSize));
Yann Collet30ab64e2017-05-10 11:30:19 -0700279 }
280
281 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
Yann Colletdce78922017-06-21 15:53:42 -0700282 { size_t const s = ZSTD_sizeof_CStream(zc);
283 if (ZSTD_isError(s)) goto _output_error;
284 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200285 }
286
Yann Collet4b987ad2017-04-10 17:50:44 -0700287 /* Attempt bad compression parameters */
288 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
289 { size_t r;
290 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
291 params.cParams.searchLength = 2;
292 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
293 if (!ZSTD_isError(r)) goto _output_error;
294 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
295 }
296
Yann Colletd7883a22016-08-12 16:48:02 +0200297 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800298 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700299 CHECK_Z( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) );
Yann Collet53e17fb2016-08-17 01:39:22 +0200300 inBuff.src = compressedBuffer;
301 inBuff.size = cSize;
302 inBuff.pos = 0;
303 outBuff.dst = decodedBuffer;
304 outBuff.size = CNBufferSize;
305 outBuff.pos = 0;
Yann Colletdce78922017-06-21 15:53:42 -0700306 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
307 DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r);
308 if (r != 0) goto _output_error;
309 }
Yann Colleta33ae642017-02-28 01:15:28 -0800310 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800311 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200312
313 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800314 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800315 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700316 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd6770f82017-09-28 02:14:48 -0700317 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000) ); /* large limit */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800318 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
319 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
320 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
321 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800322 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800323
324 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800325 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800326 outBuff.pos = 0;
327 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
328 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200329 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
330 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800331 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200332
333 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800334 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200335 { size_t i;
336 for (i=0; i<CNBufferSize; i++) {
337 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
338 } }
Yann Collet736788f2017-01-19 12:12:50 -0800339 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200340
Yann Colletf16f4492017-05-09 16:18:17 -0700341 /* context size functions */
342 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
343 { ZSTD_frameHeader fhi;
344 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
345 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
346 if (gfhError!=0) goto _output_error;
Yann Colletdde10b22017-06-26 17:44:26 -0700347 DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize);
348 { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize)
Yann Collet25989e32017-05-25 15:07:37 -0700349 /* uses ZSTD_initDStream_usingDict() */
Stella Lauc88fb922017-08-29 11:55:02 -0700350 + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
Yann Colletf16f4492017-05-09 16:18:17 -0700351 if (ZSTD_isError(s)) goto _output_error;
352 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
353 } }
354
355 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200356 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200357 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800358 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200359 }
360
Yann Colletd7883a22016-08-12 16:48:02 +0200361 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800362 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200363 { /* skippable frame */
364 size_t r = 1;
Yann Collet30ab64e2017-05-10 11:30:19 -0700365 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200366 inBuff.src = compressedBuffer;
367 outBuff.dst = decodedBuffer;
368 inBuff.pos = 0;
369 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200370 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200371 inBuff.size = inBuff.pos + 1;
372 outBuff.size = outBuff.pos + 1;
373 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200374 if (ZSTD_isError(r)) goto _output_error;
375 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200376 /* normal frame */
Yann Collet30ab64e2017-05-10 11:30:19 -0700377 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200378 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200379 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200380 inBuff.size = inBuff.pos + 1;
381 outBuff.size = outBuff.pos + 1;
382 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200383 if (ZSTD_isError(r)) goto _output_error;
384 }
385 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200386 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
387 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800388 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200389
390 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800391 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200392 { size_t i;
393 for (i=0; i<CNBufferSize; i++) {
394 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
395 } }
Yann Collet736788f2017-01-19 12:12:50 -0800396 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200397
Yann Collete795c8a2016-12-13 16:39:36 +0100398 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800399 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100400 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100401 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100402 outBuff.size = compressedBufferSize;
403 outBuff.pos = 0;
404 inBuff.src = CNBuffer;
405 inBuff.size = CNBufferSize;
406 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700407 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collete795c8a2016-12-13 16:39:36 +0100408 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
409 { size_t const r = ZSTD_endStream(zc, &outBuff);
410 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Sean Purcell4e709712017-02-07 13:50:09 -0800411 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100412 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800413 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100414
415 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800416 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100417 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100418 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100419 outBuff.size = compressedBufferSize;
420 outBuff.pos = 0;
421 inBuff.src = CNBuffer;
422 inBuff.size = CNBufferSize;
423 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700424 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collete795c8a2016-12-13 16:39:36 +0100425 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
426 { size_t const r = ZSTD_endStream(zc, &outBuff);
427 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800428 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100429
Yann Collet12083a42016-09-06 15:01:51 +0200430 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800431 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200432 ZSTD_freeCStream(zc);
Yann Colletbd88f632017-11-27 12:15:23 -0800433 zc = ZSTD_createCStream();
Yann Collet12083a42016-09-06 15:01:51 +0200434 if (zc==NULL) goto _output_error; /* memory allocation issue */
435 /* use 1 */
436 { size_t const inSize = 513;
Yann Collet0be6fd32017-05-08 16:08:01 -0700437 DISPLAYLEVEL(5, "use1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200438 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
439 inBuff.src = CNBuffer;
440 inBuff.size = inSize;
441 inBuff.pos = 0;
442 outBuff.dst = (char*)(compressedBuffer)+cSize;
443 outBuff.size = ZSTD_compressBound(inSize);
444 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700445 DISPLAYLEVEL(5, "compress1 ");
Yann Colletd6770f82017-09-28 02:14:48 -0700446 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet12083a42016-09-06 15:01:51 +0200447 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700448 DISPLAYLEVEL(5, "end1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200449 { size_t const r = ZSTD_endStream(zc, &outBuff);
450 if (r != 0) goto _output_error; } /* error, or some data not flushed */
451 }
452 /* use 2 */
453 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
Yann Collet0be6fd32017-05-08 16:08:01 -0700454 DISPLAYLEVEL(5, "use2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200455 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
456 inBuff.src = CNBuffer;
457 inBuff.size = inSize;
458 inBuff.pos = 0;
459 outBuff.dst = (char*)(compressedBuffer)+cSize;
460 outBuff.size = ZSTD_compressBound(inSize);
461 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700462 DISPLAYLEVEL(5, "compress2 ");
Yann Colletd6770f82017-09-28 02:14:48 -0700463 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet12083a42016-09-06 15:01:51 +0200464 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700465 DISPLAYLEVEL(5, "end2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200466 { size_t const r = ZSTD_endStream(zc, &outBuff);
467 if (r != 0) goto _output_error; } /* error, or some data not flushed */
468 }
Yann Collet736788f2017-01-19 12:12:50 -0800469 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200470
Yann Collet95162342016-10-25 16:19:52 -0700471 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800472 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet2e427422017-06-27 17:09:12 -0700473 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ );
Yann Collet95162342016-10-25 16:19:52 -0700474 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
Yann Collet15768ca2017-11-16 15:02:28 -0800475 DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (U32)initError);
Yann Collet95162342016-10-25 16:19:52 -0700476 if (ZSTD_isError(initError)) goto _output_error;
477 cSize = 0;
478 outBuff.dst = compressedBuffer;
479 outBuff.size = compressedBufferSize;
480 outBuff.pos = 0;
481 inBuff.src = CNBuffer;
482 inBuff.size = CNBufferSize;
483 inBuff.pos = 0;
Yann Collet15768ca2017-11-16 15:02:28 -0800484 DISPLAYLEVEL(5, "- starting ZSTD_compressStream ");
Yann Colletd6770f82017-09-28 02:14:48 -0700485 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet95162342016-10-25 16:19:52 -0700486 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet15768ca2017-11-16 15:02:28 -0800487 { size_t const r = ZSTD_endStream(zc, &outBuff);
488 DISPLAYLEVEL(5, "- ZSTD_endStream result : %u ", (U32)r);
489 if (r != 0) goto _output_error; /* error, or some data not flushed */
490 }
Yann Collet95162342016-10-25 16:19:52 -0700491 cSize = outBuff.pos;
492 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800493 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700494 }
495
Yann Collet736788f2017-01-19 12:12:50 -0800496 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200497 { size_t const s = ZSTD_sizeof_CStream(zc);
498 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800499 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200500 }
501
Yann Collet33fce032017-01-16 19:46:22 -0800502 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
503 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
504 if (dID != dictID) goto _output_error;
505 DISPLAYLEVEL(4, "OK (%u) \n", dID);
506 }
507
Yann Collet335ad5d2016-10-25 17:47:02 -0700508 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800509 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800510 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700511 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
512 if (ZSTD_isError(initError)) goto _output_error;
513 inBuff.src = compressedBuffer;
514 inBuff.size = cSize;
515 inBuff.pos = 0;
516 outBuff.dst = decodedBuffer;
517 outBuff.size = CNBufferSize;
518 outBuff.pos = 0;
519 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
520 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
521 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
522 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
523 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800524 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700525 }
526
Yann Collet12083a42016-09-06 15:01:51 +0200527 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800528 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200529 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
530 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800531 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200532
533 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800534 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700535 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd6770f82017-09-28 02:14:48 -0700536 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000) ); /* too small limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200537 inBuff.src = compressedBuffer;
538 inBuff.size = cSize;
539 inBuff.pos = 0;
540 outBuff.dst = decodedBuffer;
541 outBuff.size = CNBufferSize;
542 outBuff.pos = 0;
543 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
544 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800545 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200546
Yann Collet7d283cd2017-04-27 14:48:34 -0700547 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
548 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
549 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
Yann Colletbd88f632017-11-27 12:15:23 -0800550 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem);
Yann Collet8c910d22017-06-03 01:15:02 -0700551 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
Yann Collet7d283cd2017-04-27 14:48:34 -0700552 if (ZSTD_isError(initError)) goto _output_error;
Nick Terrell62ecad32017-04-03 20:56:39 -0700553 cSize = 0;
554 outBuff.dst = compressedBuffer;
555 outBuff.size = compressedBufferSize;
556 outBuff.pos = 0;
557 inBuff.src = CNBuffer;
558 inBuff.size = CNBufferSize;
559 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700560 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet8c910d22017-06-03 01:15:02 -0700561 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Nick Terrell62ecad32017-04-03 20:56:39 -0700562 { size_t const r = ZSTD_endStream(zc, &outBuff);
563 if (r != 0) goto _output_error; } /* error, or some data not flushed */
564 cSize = outBuff.pos;
565 ZSTD_freeCDict(cdict);
566 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet4ee6b152017-04-11 11:59:44 -0700567 }
Nick Terrell62ecad32017-04-03 20:56:39 -0700568
Yann Colleta92cbb72017-04-27 15:08:56 -0700569 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
570 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
571 if (did != 0) goto _output_error;
572 }
573 DISPLAYLEVEL(3, "OK (not detected) \n");
574
Yann Collet4ee6b152017-04-11 11:59:44 -0700575 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
576 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
577 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
578 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
Nick Terrell62ecad32017-04-03 20:56:39 -0700579 }
580
Yann Collet62f7efc2017-06-28 16:25:13 -0700581 DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700582 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700583 outBuff.dst = compressedBuffer;
584 outBuff.size = compressedBufferSize;
585 outBuff.pos = 0;
586 inBuff.src = CNBuffer;
587 inBuff.size = CNBufferSize;
588 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700589 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700590 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
591 cSize = outBuff.pos;
592 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
593
594 DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++);
595 { size_t const r = ZSTD_decompress_usingDict(zd,
596 decodedBuffer, CNBufferSize,
597 compressedBuffer, cSize,
598 dictionary.start, dictionary.filled);
599 if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
600 DISPLAYLEVEL(3, "OK \n");
601 }
602
603 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++);
604 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
605 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
606 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
607 }
608
609 DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++);
610 outBuff.dst = compressedBuffer;
611 outBuff.size = compressedBufferSize;
612 outBuff.pos = 0;
613 inBuff.src = CNBuffer;
614 inBuff.size = CNBufferSize;
615 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700616 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
Yann Collet62f7efc2017-06-28 16:25:13 -0700617 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
618 cSize = outBuff.pos;
619 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
620
621 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++);
Yann Colletd6770f82017-09-28 02:14:48 -0700622 CHECK_Z( ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize) );
623 DISPLAYLEVEL(3, "OK \n");
Yann Collet62f7efc2017-06-28 16:25:13 -0700624
Yann Collet0bb381d2017-04-18 15:08:52 -0700625 /* Empty srcSize */
626 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
627 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
628 params.fParams.contentSizeFlag = 1;
Yann Collet213ef3b2017-10-13 19:01:58 -0700629 CHECK_Z( ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0 /* pledgedSrcSize==0 means "empty" when params.fParams.contentSizeFlag is set */) );
Yann Collet0bb381d2017-04-18 15:08:52 -0700630 } /* cstream advanced shall write content size = 0 */
631 inBuff.src = CNBuffer;
632 inBuff.size = 0;
633 inBuff.pos = 0;
634 outBuff.dst = compressedBuffer;
635 outBuff.size = compressedBufferSize;
636 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700637 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Collet0bb381d2017-04-18 15:08:52 -0700638 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
639 cSize = outBuff.pos;
640 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
641 DISPLAYLEVEL(3, "OK \n");
642
Sean Purcell2db72492017-02-09 10:50:43 -0800643 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
644 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
645 params.fParams.contentSizeFlag = 1;
Yann Colletd6770f82017-09-28 02:14:48 -0700646 CHECK_Z( ZSTD_initCStream_advanced(zc, NULL, 0, params, 0) );
Yann Collet4ee6b152017-04-11 11:59:44 -0700647 } /* cstream advanced shall write content size = 0 */
Sean Purcell2db72492017-02-09 10:50:43 -0800648 inBuff.src = CNBuffer;
649 inBuff.size = 0;
650 inBuff.pos = 0;
651 outBuff.dst = compressedBuffer;
652 outBuff.size = compressedBufferSize;
653 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700654 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Sean Purcell2db72492017-02-09 10:50:43 -0800655 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
656 cSize = outBuff.pos;
657 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
658
659 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
660 inBuff.src = CNBuffer;
661 inBuff.size = 0;
662 inBuff.pos = 0;
663 outBuff.dst = compressedBuffer;
664 outBuff.size = compressedBufferSize;
665 outBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700666 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Sean Purcell2db72492017-02-09 10:50:43 -0800667 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
668 cSize = outBuff.pos;
669 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
670 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200671
Stella Lau90a31bf2017-08-30 14:36:54 -0700672 /* Basic multithreading compression test */
673 DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Colletd6770f82017-09-28 02:14:48 -0700674 { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
675 CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) );
676 }
Stella Lau90a31bf2017-08-30 14:36:54 -0700677 outBuff.dst = (char*)(compressedBuffer);
678 outBuff.size = compressedBufferSize;
679 outBuff.pos = 0;
680 inBuff.src = CNBuffer;
681 inBuff.size = CNBufferSize;
682 inBuff.pos = 0;
Yann Colletd6770f82017-09-28 02:14:48 -0700683 CHECK_Z( ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end) );
Stella Lau90a31bf2017-08-30 14:36:54 -0700684 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
685 { size_t const r = ZSTDMT_endStream(mtctx, &outBuff);
686 if (r != 0) goto _output_error; } /* error, or some data not flushed */
687 DISPLAYLEVEL(3, "OK \n");
688
Nick Terrelle600b5d2017-10-16 17:18:43 -0700689 DISPLAYLEVEL(3, "test%3i : check dictionary FSE tables can represent every code : ", testNb++);
690 { unsigned const kMaxWindowLog = 24;
691 unsigned value;
692 ZSTD_compressionParameters cParams = ZSTD_getCParams(3, 1U << kMaxWindowLog, 1024);
693 ZSTD_CDict* cdict;
694 ZSTD_DDict* ddict;
695 SEQ_stream seq = SEQ_initStream(0x87654321);
696 SEQ_gen_type type;
697 XXH64_state_t xxh;
698
699 XXH64_reset(&xxh, 0);
700 cParams.windowLog = kMaxWindowLog;
701 cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem);
702 ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
703
704 if (!cdict || !ddict) goto _output_error;
705
706 ZSTD_CCtx_reset(zc);
707 ZSTD_resetDStream(zd);
708 CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
709 CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict));
710 CHECK_Z(ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1U << kMaxWindowLog));
711 /* Test all values < 300 */
712 for (value = 0; value < 300; ++value) {
713 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
714 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
715 }
716 }
717 /* Test values 2^8 to 2^17 */
718 for (value = (1 << 8); value < (1 << 17); value <<= 1) {
719 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
720 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
721 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value + (value >> 2)));
722 }
723 }
724 /* Test offset values up to the max window log */
725 for (value = 8; value <= kMaxWindowLog; ++value) {
726 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, SEQ_gen_of, (1U << value) - 1));
727 }
728
729 CHECK_Z(SEQ_roundTrip(zc, zd, &xxh, NULL, 0, ZSTD_e_end));
730 CHECK(SEQ_digest(&seq) != XXH64_digest(&xxh), "SEQ XXH64 does not match");
731
732 ZSTD_freeCDict(cdict);
733 ZSTD_freeDDict(ddict);
734 }
735 DISPLAYLEVEL(3, "OK \n");
Stella Lau90a31bf2017-08-30 14:36:54 -0700736
Sean Purcell887eaa92017-02-15 16:43:45 -0800737 /* Overlen overwriting window data bug */
738 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800739 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
740 1. 'a' repeated 517 times
741 2. 'b' repeated 516 times
742 3. a compressed block with no literals and 3 sequence commands:
743 litlength = 0, offset = 24, match length = 24
744 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
745 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
746
747 const char* testCase =
748 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
749 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
750 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Yann Colletd6770f82017-09-28 02:14:48 -0700751 ZSTD_DStream* const zds = ZSTD_createDStream();
752 if (zds==NULL) goto _output_error;
Sean Purcell887eaa92017-02-15 16:43:45 -0800753
Yann Colletd6770f82017-09-28 02:14:48 -0700754 CHECK_Z( ZSTD_initDStream(zds) );
Sean Purcell887eaa92017-02-15 16:43:45 -0800755 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800756 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800757 inBuff.pos = 0;
758 outBuff.dst = decodedBuffer;
759 outBuff.size = CNBufferSize;
760 outBuff.pos = 0;
761
762 while (inBuff.pos < inBuff.size) {
Yann Colletd6770f82017-09-28 02:14:48 -0700763 CHECK_Z( ZSTD_decompressStream(zds, &outBuff, &inBuff) );
Sean Purcell887eaa92017-02-15 16:43:45 -0800764 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100765
766 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800767 }
768 DISPLAYLEVEL(3, "OK \n");
769
Yann Colletd7883a22016-08-12 16:48:02 +0200770_end:
Yann Collet33fce032017-01-16 19:46:22 -0800771 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200772 ZSTD_freeCStream(zc);
773 ZSTD_freeDStream(zd);
Stella Lau90a31bf2017-08-30 14:36:54 -0700774 ZSTDMT_freeCCtx(mtctx);
Yann Colletd7883a22016-08-12 16:48:02 +0200775 free(CNBuffer);
776 free(compressedBuffer);
777 free(decodedBuffer);
778 return testResult;
779
780_output_error:
781 testResult = 1;
782 DISPLAY("Error detected in Unit tests ! \n");
783 goto _end;
784}
785
786
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200787/* ====== Fuzzer tests ====== */
788
Yann Colletd7883a22016-08-12 16:48:02 +0200789static size_t findDiff(const void* buf1, const void* buf2, size_t max)
790{
791 const BYTE* b1 = (const BYTE*)buf1;
792 const BYTE* b2 = (const BYTE*)buf2;
793 size_t u;
794 for (u=0; u<max; u++) {
795 if (b1[u] != b2[u]) break;
796 }
Yann Collet736788f2017-01-19 12:12:50 -0800797 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800798 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
799 b1[u-3], b1[u-2], b1[u-1], b1[u-0], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]);
800 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
801 b2[u-3], b2[u-2], b2[u-1], b2[u-0], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]);
Yann Colletd7883a22016-08-12 16:48:02 +0200802 return u;
803}
804
805static size_t FUZ_rLogLength(U32* seed, U32 logLength)
806{
807 size_t const lengthMask = ((size_t)1 << logLength) - 1;
808 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
809}
810
811static size_t FUZ_randomLength(U32* seed, U32 maxLog)
812{
813 U32 const logLength = FUZ_rand(seed) % maxLog;
814 return FUZ_rLogLength(seed, logLength);
815}
816
Stella Lau9e406022017-09-06 08:39:46 -0700817/* Return value in range minVal <= v <= maxVal */
818static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal)
819{
820 U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal;
821 return (U32)((FUZ_rand(seed) % mod) + minVal);
822}
823
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700824static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200825{
Yann Colletf99c2c12017-06-21 23:35:58 -0700826 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Colletd7883a22016-08-12 16:48:02 +0200827 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200828 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200829 BYTE* cNoiseBuffer[5];
Yann Colletbc32b402017-09-27 17:27:38 -0700830 size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200831 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
Yann Colletbc32b402017-09-27 17:27:38 -0700832 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200833 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
834 size_t const dstBufferSize = srcBufferSize;
835 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200836 U32 result = 0;
837 U32 testNb = 0;
838 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -0700839 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */
840 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200841 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -0800842 UTIL_time_t const startClock = UTIL_getTime();
Yann Colletbc32b402017-09-27 17:27:38 -0700843 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200844 size_t dictSize = 0;
845 U32 oldTestLog = 0;
Yann Collet49f84592017-06-21 18:43:39 -0700846 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests;
Yann Colletd7883a22016-08-12 16:48:02 +0200847
848 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200849 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
850 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
851 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
852 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
853 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200854 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200855 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200856 "Not enough memory, fuzzer tests cancelled");
857
858 /* Create initial samples */
859 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
860 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
861 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
862 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
863 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
864 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200865 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200866
867 /* catch up testNb */
868 for (testNb=1; testNb < startTest; testNb++)
869 FUZ_rand(&coreSeed);
870
871 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -0800872 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200873 U32 lseed;
874 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200875 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200876 XXH64_state_t xxhState;
877 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200878 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200879 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200880
881 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700882 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
883 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200884 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800885 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200886
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200887 /* states full reset (deliberately not synchronized) */
888 /* some issues can only happen when reusing states */
Yann Colleted1d0392017-06-19 11:07:33 -0700889 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
890 ZSTD_freeCStream(zc);
891 zc = ZSTD_createCStream();
Yann Colletdce78922017-06-21 15:53:42 -0700892 CHECK(zc==NULL, "ZSTD_createCStream : allocation error");
Yann Colleted1d0392017-06-19 11:07:33 -0700893 resetAllowed=0;
894 }
895 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
896 ZSTD_freeDStream(zd);
897 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -0700898 CHECK(zd==NULL, "ZSTD_createDStream : allocation error");
899 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Colleted1d0392017-06-19 11:07:33 -0700900 }
Yann Colletd7883a22016-08-12 16:48:02 +0200901
902 /* srcBuffer selection [0-4] */
903 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
904 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
905 else {
906 buffNb >>= 3;
907 if (buffNb & 7) {
908 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
909 buffNb = tnb[buffNb >> 3];
910 } else {
911 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
912 buffNb = tnb[buffNb >> 3];
913 } }
914 srcBuffer = cNoiseBuffer[buffNb];
915 }
916
917 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200918 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
919 && oldTestLog /* at least one test happened */ && resetAllowed) {
920 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
Yann Colletbc32b402017-09-27 17:27:38 -0700921 maxTestSize = MIN(maxTestSize, srcBufferSize-16);
Yann Colletcf409a72016-09-26 16:41:05 +0200922 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
Yann Collet01743a32017-06-16 17:56:41 -0700923 CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) );
Yann Colletcf409a72016-09-26 16:41:05 +0200924 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200925 } else {
926 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700927 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -0700928 U32 const cLevelCandidate = ( FUZ_rand(&lseed) %
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700929 (ZSTD_maxCLevel() -
Yann Colletbfc2f002017-06-21 17:57:14 -0700930 (MAX(testLog, dictLog) / 3)))
Yann Colletce800982017-04-05 16:34:09 -0700931 + 1;
Yann Colletbfc2f002017-06-21 17:57:14 -0700932 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Colletd7883a22016-08-12 16:48:02 +0200933 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200934 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200935 /* random dictionary selection */
Yann Colletf99c2c12017-06-21 23:35:58 -0700936 dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200937 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
938 dict = srcBuffer + dictStart;
939 }
Yann Collet213ef3b2017-10-13 19:01:58 -0700940 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
Yann Colletcf409a72016-09-26 16:41:05 +0200941 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200942 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
943 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Collet213ef3b2017-10-13 19:01:58 -0700944 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
Yann Collet01743a32017-06-16 17:56:41 -0700945 CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
946 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200947
948 /* multi-segments compression test */
949 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200950 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200951 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200952 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100953 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200954 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet150354c2017-11-01 16:57:48 -0700955 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
Yann Collet2f263942016-09-26 14:06:08 +0200956 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200957 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
958 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200959 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200960 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200961
Yann Colleted1d0392017-06-19 11:07:33 -0700962 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Colletd7883a22016-08-12 16:48:02 +0200963
Yann Collet53e17fb2016-08-17 01:39:22 +0200964 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
965 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
966 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200967 }
968
969 /* random flush operation, to mess around */
970 if ((FUZ_rand(&lseed) & 15) == 0) {
971 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200972 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
973 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colleted1d0392017-06-19 11:07:33 -0700974 CHECK_Z( ZSTD_flushStream(zc, &outBuff) );
975 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200976
977 /* final frame epilogue */
978 { size_t remainingToFlush = (size_t)(-1);
979 while (remainingToFlush) {
980 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
981 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200982 outBuff.size = outBuff.pos + adjustedDstSize;
983 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Collet009d6042017-05-19 10:17:59 -0700984 CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Colletd7883a22016-08-12 16:48:02 +0200985 } }
986 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200987 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200988 }
989
990 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200991 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -0700992 CHECK_Z ( ZSTD_resetDStream(zd) );
Yann Collet9ffbeea2016-12-02 18:37:38 -0800993 } else {
Yann Colletdce78922017-06-21 15:53:42 -0700994 CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet9ffbeea2016-12-02 18:37:38 -0800995 }
Yann Colletd7883a22016-08-12 16:48:02 +0200996 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200997 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
998 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
999 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +02001000 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1001 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1002 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001003 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletca306c12017-09-27 00:39:41 -07001004 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet53e17fb2016-08-17 01:39:22 +02001005 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletb3f33cc2017-09-09 14:37:28 -07001006 if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) {
1007 DISPLAY("checksum error : \n");
1008 findDiff(copyBuffer, dstBuffer, totalTestSize);
1009 }
1010 CHECK( ZSTD_isError(decompressionResult), "decompression error : %s",
1011 ZSTD_getErrorName(decompressionResult) );
Yann Colletd7883a22016-08-12 16:48:02 +02001012 }
1013 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Colletb3f33cc2017-09-09 14:37:28 -07001014 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)",
1015 (U32)outBuff.pos, (U32)totalTestSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001016 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +02001017 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1018 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1019 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1020 } }
1021
1022 /*===== noisy/erroneous src decompression test =====*/
1023
1024 /* add some noise */
1025 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1026 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1027 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1028 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1029 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1030 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1031 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1032 } }
1033
1034 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001035 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +02001036 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1037 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1038 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +02001039 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1040 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +02001041 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -08001042 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +02001043 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -08001044 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +02001045 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +02001046 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -08001047 /* No forward progress possible */
1048 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +02001049 } } } }
1050 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1051
1052_cleanup:
1053 ZSTD_freeCStream(zc);
1054 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +02001055 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +02001056 free(cNoiseBuffer[0]);
1057 free(cNoiseBuffer[1]);
1058 free(cNoiseBuffer[2]);
1059 free(cNoiseBuffer[3]);
1060 free(cNoiseBuffer[4]);
1061 free(copyBuffer);
1062 free(cBuffer);
1063 free(dstBuffer);
1064 return result;
1065
1066_output_error:
1067 result = 1;
1068 goto _cleanup;
1069}
1070
1071
Yann Collet736788f2017-01-19 12:12:50 -08001072/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001073static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -08001074{
Yann Colletf99c2c12017-06-21 23:35:58 -07001075 const U32 maxSrcLog = bigTests ? 24 : 22;
Yann Collet736788f2017-01-19 12:12:50 -08001076 static const U32 maxSampleLog = 19;
1077 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1078 BYTE* cNoiseBuffer[5];
1079 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1080 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1081 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1082 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1083 size_t const dstBufferSize = srcBufferSize;
1084 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1085 U32 result = 0;
1086 U32 testNb = 0;
1087 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -07001088 U32 nbThreads = 2;
1089 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */
Yann Collet736788f2017-01-19 12:12:50 -08001090 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1091 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001092 UTIL_time_t const startClock = UTIL_getTime();
Yann Collet736788f2017-01-19 12:12:50 -08001093 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
1094 size_t dictSize = 0;
1095 U32 oldTestLog = 0;
Yann Colletbc32b402017-09-27 17:27:38 -07001096 int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1097 U32 const nbThreadsMax = bigTests ? 4 : 2;
Yann Collet736788f2017-01-19 12:12:50 -08001098
1099 /* allocations */
1100 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1101 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1102 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1103 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1104 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1105 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1106 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1107 "Not enough memory, fuzzer tests cancelled");
1108
1109 /* Create initial samples */
1110 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1111 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1112 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1113 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1114 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1115 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1116 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1117
1118 /* catch up testNb */
1119 for (testNb=1; testNb < startTest; testNb++)
1120 FUZ_rand(&coreSeed);
1121
1122 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001123 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Collet736788f2017-01-19 12:12:50 -08001124 U32 lseed;
1125 const BYTE* srcBuffer;
1126 size_t totalTestSize, totalGenSize, cSize;
1127 XXH64_state_t xxhState;
1128 U64 crcOrig;
1129 U32 resetAllowed = 1;
1130 size_t maxTestSize;
1131
1132 /* init */
Yann Colletbc32b402017-09-27 17:27:38 -07001133 if (testNb < nbTests) {
1134 DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests);
1135 } else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet736788f2017-01-19 12:12:50 -08001136 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -08001137 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -08001138
1139 /* states full reset (deliberately not synchronized) */
1140 /* some issues can only happen when reusing states */
1141 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
Yann Collet9fe50ed2017-09-28 01:42:06 -07001142 nbThreads = (FUZ_rand(&lseed) % nbThreadsMax) + 1;
Yann Collet30c76982017-03-31 18:27:03 -07001143 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
Yann Collet736788f2017-01-19 12:12:50 -08001144 ZSTDMT_freeCCtx(zc);
1145 zc = ZSTDMT_createCCtx(nbThreads);
Yann Colletdce78922017-06-21 15:53:42 -07001146 CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001147 resetAllowed=0;
1148 }
1149 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1150 ZSTD_freeDStream(zd);
1151 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001152 CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001153 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1154 }
1155
1156 /* srcBuffer selection [0-4] */
1157 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1158 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1159 else {
1160 buffNb >>= 3;
1161 if (buffNb & 7) {
1162 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1163 buffNb = tnb[buffNb >> 3];
1164 } else {
1165 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1166 buffNb = tnb[buffNb >> 3];
1167 } }
1168 srcBuffer = cNoiseBuffer[buffNb];
1169 }
1170
1171 /* compression init */
1172 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1173 && oldTestLog /* at least one test happened */ && resetAllowed) {
1174 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1175 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1176 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Yann Colletdce78922017-06-21 15:53:42 -07001177 CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) );
Yann Collet736788f2017-01-19 12:12:50 -08001178 }
1179 } else {
1180 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001181 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbc32b402017-09-27 17:27:38 -07001182 int const cLevelCandidate = ( FUZ_rand(&lseed)
1183 % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) )
1184 + 1;
1185 int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */
1186 int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */
1187 int const cLevel = MIN(cLevelMin, cLevelMax);
Yann Collet736788f2017-01-19 12:12:50 -08001188 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1189 oldTestLog = testLog;
1190 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -07001191 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001192 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1193 dict = srcBuffer + dictStart;
1194 }
Yann Colletbeb9b4b2017-10-13 19:09:30 -07001195 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
Yann Collet19d670b2017-01-19 15:32:07 -08001196 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletce800982017-04-05 16:34:09 -07001197 DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n",
1198 params.cParams.windowLog, (U32)pledgedSrcSize);
Yann Collet19d670b2017-01-19 15:32:07 -08001199 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1200 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletbeb9b4b2017-10-13 19:09:30 -07001201 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
Yann Collet2c5514c2017-04-18 22:52:41 -07001202 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
Yann Colletdce78922017-06-21 15:53:42 -07001203 CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
1204 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) );
1205 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) );
Yann Colletcd23dd22017-01-30 12:46:35 -08001206 } }
Yann Collet736788f2017-01-19 12:12:50 -08001207
1208 /* multi-segments compression test */
1209 XXH64_reset(&xxhState, 0);
1210 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1211 U32 n;
1212 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1213 /* compress random chunks into randomly sized dst buffers */
1214 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1215 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1216 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1217 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1218 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1219 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1220 outBuff.size = outBuff.pos + dstBuffSize;
1221
1222 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
Yann Colletdce78922017-06-21 15:53:42 -07001223 CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) );
Yann Collet19d670b2017-01-19 15:32:07 -08001224 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -08001225
1226 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1227 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1228 totalTestSize += inBuff.pos;
1229 }
1230
1231 /* random flush operation, to mess around */
1232 if ((FUZ_rand(&lseed) & 15) == 0) {
1233 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1234 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1235 outBuff.size = outBuff.pos + adjustedDstSize;
1236 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
Yann Colletdce78922017-06-21 15:53:42 -07001237 CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) );
1238 } }
Yann Collet736788f2017-01-19 12:12:50 -08001239
1240 /* final frame epilogue */
1241 { size_t remainingToFlush = (size_t)(-1);
1242 while (remainingToFlush) {
1243 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1244 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1245 outBuff.size = outBuff.pos + adjustedDstSize;
1246 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1247 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001248 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet736788f2017-01-19 12:12:50 -08001249 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1250 } }
Yann Collet736788f2017-01-19 12:12:50 -08001251 crcOrig = XXH64_digest(&xxhState);
1252 cSize = outBuff.pos;
Yann Collet2c5514c2017-04-18 22:52:41 -07001253 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001254 }
1255
1256 /* multi - fragments decompression test */
1257 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -07001258 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet736788f2017-01-19 12:12:50 -08001259 } else {
Yann Colletdce78922017-06-21 15:53:42 -07001260 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet736788f2017-01-19 12:12:50 -08001261 }
1262 { size_t decompressionResult = 1;
1263 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1264 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1265 for (totalGenSize = 0 ; decompressionResult ; ) {
1266 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1267 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1268 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1269 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletbfabd1d2017-09-27 01:01:11 -07001270 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletce800982017-04-05 16:34:09 -07001271 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001272 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1273 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Collet2c5514c2017-04-18 22:52:41 -07001274 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001275 }
Yann Colletbb002742017-01-25 16:25:38 -08001276 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1277 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001278 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1279 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1280 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1281 } }
1282
1283 /*===== noisy/erroneous src decompression test =====*/
1284
1285 /* add some noise */
1286 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1287 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1288 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1289 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1290 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1291 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1292 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1293 } }
1294
1295 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001296 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet736788f2017-01-19 12:12:50 -08001297 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1298 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1299 while (outBuff.pos < dstBufferSize) {
1300 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1301 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1302 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001303 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001304 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001305 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001306 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1307 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001308 /* No forward progress possible */
1309 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001310 } } } }
1311 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1312
1313_cleanup:
1314 ZSTDMT_freeCCtx(zc);
1315 ZSTD_freeDStream(zd);
1316 ZSTD_freeDStream(zd_noise);
1317 free(cNoiseBuffer[0]);
1318 free(cNoiseBuffer[1]);
1319 free(cNoiseBuffer[2]);
1320 free(cNoiseBuffer[3]);
1321 free(cNoiseBuffer[4]);
1322 free(copyBuffer);
1323 free(cBuffer);
1324 free(dstBuffer);
1325 return result;
1326
1327_output_error:
1328 result = 1;
1329 goto _cleanup;
1330}
1331
Stella Lau73c73bf2017-08-21 12:41:19 -07001332/** If useOpaqueAPI, sets param in cctxParams.
1333 * Otherwise, sets the param in zc. */
1334static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
1335 ZSTD_cParameter param, unsigned value,
1336 U32 useOpaqueAPI)
1337{
1338 if (useOpaqueAPI) {
1339 return ZSTD_CCtxParam_setParameter(cctxParams, param, value);
1340 } else {
1341 return ZSTD_CCtx_setParameter(zc, param, value);
1342 }
1343}
Yann Collet736788f2017-01-19 12:12:50 -08001344
Yann Colletd7a3bff2017-06-19 11:53:01 -07001345/* Tests for ZSTD_compress_generic() API */
Stella Lau73c73bf2017-08-21 12:41:19 -07001346static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI)
Yann Collet01743a32017-06-16 17:56:41 -07001347{
Yann Colletf99c2c12017-06-21 23:35:58 -07001348 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Collet01743a32017-06-16 17:56:41 -07001349 static const U32 maxSampleLog = 19;
1350 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1351 BYTE* cNoiseBuffer[5];
1352 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1353 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1354 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1355 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1356 size_t const dstBufferSize = srcBufferSize;
1357 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1358 U32 result = 0;
1359 U32 testNb = 0;
1360 U32 coreSeed = seed;
1361 ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */
1362 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1363 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001364 UTIL_time_t const startClock = UTIL_getTime();
Yann Colletd7a3bff2017-06-19 11:53:01 -07001365 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Collet01743a32017-06-16 17:56:41 -07001366 size_t dictSize = 0;
1367 U32 oldTestLog = 0;
Yann Colletaa800c42017-09-27 18:00:15 -07001368 U32 windowLogMalus = 0; /* can survive between 2 loops */
1369 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1370 U32 const nbThreadsMax = bigTests ? 4 : 2;
Stella Lau023b24e2017-08-20 22:55:07 -07001371 ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
Yann Collet01743a32017-06-16 17:56:41 -07001372
1373 /* allocations */
1374 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1375 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1376 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1377 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1378 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1379 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1380 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1381 "Not enough memory, fuzzer tests cancelled");
1382
1383 /* Create initial samples */
1384 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1385 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1386 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1387 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1388 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1389 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Colletdce78922017-06-21 15:53:42 -07001390 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Collet01743a32017-06-16 17:56:41 -07001391
1392 /* catch up testNb */
1393 for (testNb=1; testNb < startTest; testNb++)
1394 FUZ_rand(&coreSeed);
1395
1396 /* test loop */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001397 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
Yann Collet01743a32017-06-16 17:56:41 -07001398 U32 lseed;
1399 const BYTE* srcBuffer;
1400 size_t totalTestSize, totalGenSize, cSize;
1401 XXH64_state_t xxhState;
1402 U64 crcOrig;
1403 U32 resetAllowed = 1;
1404 size_t maxTestSize;
1405
1406 /* init */
1407 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
1408 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
1409 FUZ_rand(&coreSeed);
1410 lseed = coreSeed ^ prime32;
Yann Collet9b5b47a2017-09-28 01:25:40 -07001411 DISPLAYLEVEL(5, " *** Test %u *** \n", testNb);
Yann Collet01743a32017-06-16 17:56:41 -07001412
1413 /* states full reset (deliberately not synchronized) */
1414 /* some issues can only happen when reusing states */
1415 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1416 DISPLAYLEVEL(5, "Creating new context \n");
1417 ZSTD_freeCCtx(zc);
1418 zc = ZSTD_createCCtx();
Yann Colletdce78922017-06-21 15:53:42 -07001419 CHECK(zc==NULL, "ZSTD_createCCtx allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001420 resetAllowed=0;
1421 }
1422 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1423 ZSTD_freeDStream(zd);
1424 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001425 CHECK(zd==NULL, "ZSTD_createDStream allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001426 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1427 }
1428
1429 /* srcBuffer selection [0-4] */
1430 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1431 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1432 else {
1433 buffNb >>= 3;
1434 if (buffNb & 7) {
1435 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1436 buffNb = tnb[buffNb >> 3];
1437 } else {
1438 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1439 buffNb = tnb[buffNb >> 3];
1440 } }
1441 srcBuffer = cNoiseBuffer[buffNb];
1442 }
1443
1444 /* compression init */
Yann Colletb7372932017-06-27 15:49:12 -07001445 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/
Yann Collet01743a32017-06-16 17:56:41 -07001446 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1447 && oldTestLog /* at least one test happened */ && resetAllowed) {
1448 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1449 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
Yann Collet01743a32017-06-16 17:56:41 -07001450 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Stella Lau73c73bf2017-08-21 12:41:19 -07001451 CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001452 }
1453 } else {
1454 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1455 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -07001456 U32 const cLevelCandidate = (FUZ_rand(&lseed) %
Yann Collet01743a32017-06-16 17:56:41 -07001457 (ZSTD_maxCLevel() -
Yann Collet9fe50ed2017-09-28 01:42:06 -07001458 (MAX(testLog, dictLog) / 2))) +
Yann Collet01743a32017-06-16 17:56:41 -07001459 1;
Yann Colletbfc2f002017-06-21 17:57:14 -07001460 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001461 DISPLAYLEVEL(5, "t%u: cLevel : %u \n", testNb, cLevel);
Yann Collet01743a32017-06-16 17:56:41 -07001462 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001463 DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize);
Yann Collet01743a32017-06-16 17:56:41 -07001464 oldTestLog = testLog;
1465 /* random dictionary selection */
1466 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1467 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1468 dict = srcBuffer + dictStart;
Yann Colletd7a3bff2017-06-19 11:53:01 -07001469 if (!dictSize) dict=NULL;
Yann Collet01743a32017-06-16 17:56:41 -07001470 }
1471 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1472 ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletbbef0582017-09-28 11:48:45 -07001473 static const U32 windowLogMax = 24;
Yann Collet01743a32017-06-16 17:56:41 -07001474
1475 /* mess with compression parameters */
1476 cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
Nick Terrellc233bdb2017-09-22 14:04:39 -07001477 cParams.windowLog = MIN(windowLogMax, cParams.windowLog);
Yann Collet01743a32017-06-16 17:56:41 -07001478 cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
1479 cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
1480 cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
1481 cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001482 cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
Yann Collet01743a32017-06-16 17:56:41 -07001483 cParams = ZSTD_adjustCParams(cParams, 0, 0);
1484
Yann Colletaa800c42017-09-27 18:00:15 -07001485 if (FUZ_rand(&lseed) & 1) {
1486 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) );
1487 assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */
1488 windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5;
Yann Collet9b5b47a2017-09-28 01:25:40 -07001489 DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog);
Yann Colletaa800c42017-09-27 18:00:15 -07001490 }
Yann Collet9b5b47a2017-09-28 01:25:40 -07001491 if (FUZ_rand(&lseed) & 1) {
1492 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) );
1493 DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog);
1494 }
1495 if (FUZ_rand(&lseed) & 1) {
1496 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) );
1497 DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog);
1498 }
Stella Lau73c73bf2017-08-21 12:41:19 -07001499 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) );
1500 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) );
1501 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001502
Stella Lau9e406022017-09-06 08:39:46 -07001503 /* mess with long distance matching parameters */
Stella Lau67d4a612017-09-02 21:10:36 -07001504 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) );
Stella Lau3d8e3132017-09-11 17:21:28 -07001505 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) );
Stella Lau9e406022017-09-06 08:39:46 -07001506 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) );
1507 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) );
1508 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) );
Stella Lau6a546ef2017-07-28 15:51:33 -07001509
Yann Collet01743a32017-06-16 17:56:41 -07001510 /* mess with frame parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001511 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1512 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1513 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Yann Colletf5deae82017-06-18 23:41:38 -07001514 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001515 DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize);
Yann Collet01743a32017-06-16 17:56:41 -07001516
1517 /* multi-threading parameters */
Yann Colletbfc2f002017-06-21 17:57:14 -07001518 { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001519 U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1;
1520 U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax);
Stella Lau73c73bf2017-08-21 12:41:19 -07001521 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001522 DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads);
Yann Collet01743a32017-06-16 17:56:41 -07001523 if (nbThreads > 1) {
Yann Colleted1d0392017-06-19 11:07:33 -07001524 U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
Stella Lau73c73bf2017-08-21 12:41:19 -07001525 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) );
1526 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001527 }
1528 }
1529
Stella Lau73c73bf2017-08-21 12:41:19 -07001530 if (FUZ_rand(&lseed) & 1) CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001531
1532 /* Apply parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001533 if (useOpaqueAPI) {
Stella Lau82d636b2017-08-29 18:03:06 -07001534 CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) );
Stella Lau73c73bf2017-08-21 12:41:19 -07001535 }
Stella Lau023b24e2017-08-20 22:55:07 -07001536
1537 if (FUZ_rand(&lseed) & 1) {
Stella Laueb7bbab2017-08-25 10:48:07 -07001538 if (FUZ_rand(&lseed) & 1) {
1539 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) );
1540 } else {
1541 CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) );
1542 }
Stella Lau023b24e2017-08-20 22:55:07 -07001543 if (dict && dictSize) {
1544 /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */
Stella Lau73c73bf2017-08-21 12:41:19 -07001545 if (useOpaqueAPI) {
Stella Lau82d636b2017-08-29 18:03:06 -07001546 size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams);
1547 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed");
Stella Lau73c73bf2017-08-21 12:41:19 -07001548 } else {
1549 size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1);
1550 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed");
1551 }
1552 }
1553 } else {
Stella Lau023b24e2017-08-20 22:55:07 -07001554 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) );
1555 }
1556 } }
Yann Collet01743a32017-06-16 17:56:41 -07001557
1558 /* multi-segments compression test */
1559 XXH64_reset(&xxhState, 0);
1560 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet052a95f2017-07-11 17:18:26 -07001561 for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) {
Yann Collet01743a32017-06-16 17:56:41 -07001562 /* compress random chunks into randomly sized dst buffers */
Yann Colleted1d0392017-06-19 11:07:33 -07001563 size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1564 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1565 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Collet052a95f2017-07-11 17:18:26 -07001566 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Colleted1d0392017-06-19 11:07:33 -07001567 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1568 ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush;
1569 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1570 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet01743a32017-06-16 17:56:41 -07001571
Yann Colleted1d0392017-06-19 11:07:33 -07001572 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) );
Yann Colletd6770f82017-09-28 02:14:48 -07001573 DISPLAYLEVEL(6, "compress consumed %u bytes (total : %u) \n",
Yann Colleted1d0392017-06-19 11:07:33 -07001574 (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos));
Yann Collet01743a32017-06-16 17:56:41 -07001575
Yann Colleted1d0392017-06-19 11:07:33 -07001576 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1577 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1578 totalTestSize += inBuff.pos;
1579 }
Yann Collet01743a32017-06-16 17:56:41 -07001580
1581 /* final frame epilogue */
1582 { size_t remainingToFlush = (size_t)(-1);
1583 while (remainingToFlush) {
1584 ZSTD_inBuffer inBuff = { NULL, 0, 0 };
Yann Collet052a95f2017-07-11 17:18:26 -07001585 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Collet01743a32017-06-16 17:56:41 -07001586 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1587 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colletd6770f82017-09-28 02:14:48 -07001588 DISPLAYLEVEL(6, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize);
Yann Collet01743a32017-06-16 17:56:41 -07001589 remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
Yann Colletd6770f82017-09-28 02:14:48 -07001590 CHECK( ZSTD_isError(remainingToFlush),
1591 "ZSTD_compress_generic w/ ZSTD_e_end error : %s",
1592 ZSTD_getErrorName(remainingToFlush) );
Yann Collet01743a32017-06-16 17:56:41 -07001593 } }
1594 crcOrig = XXH64_digest(&xxhState);
1595 cSize = outBuff.pos;
1596 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
1597 }
1598
1599 /* multi - fragments decompression test */
1600 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet33a66392017-06-28 11:09:43 -07001601 DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict);
Yann Colletdce78922017-06-21 15:53:42 -07001602 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet01743a32017-06-16 17:56:41 -07001603 } else {
Yann Collet33a66392017-06-28 11:09:43 -07001604 DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize);
Yann Colletdce78922017-06-21 15:53:42 -07001605 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet01743a32017-06-16 17:56:41 -07001606 }
1607 { size_t decompressionResult = 1;
1608 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1609 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1610 for (totalGenSize = 0 ; decompressionResult ; ) {
1611 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1612 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1613 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1614 inBuff.size = inBuff.pos + readCSrcSize;
Yann Colletbfabd1d2017-09-27 01:01:11 -07001615 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd6770f82017-09-28 02:14:48 -07001616 DISPLAYLEVEL(6, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n",
Yann Collet33a66392017-06-28 11:09:43 -07001617 (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize);
Yann Collet01743a32017-06-16 17:56:41 -07001618 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1619 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd6770f82017-09-28 02:14:48 -07001620 DISPLAYLEVEL(6, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet01743a32017-06-16 17:56:41 -07001621 }
1622 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1623 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1624 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1625 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1626 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1627 } }
1628
1629 /*===== noisy/erroneous src decompression test =====*/
1630
1631 /* add some noise */
1632 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1633 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1634 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1635 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1636 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1637 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1638 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1639 } }
1640
1641 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001642 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet01743a32017-06-16 17:56:41 -07001643 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1644 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1645 while (outBuff.pos < dstBufferSize) {
1646 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1647 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1648 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1649 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1650 outBuff.size = outBuff.pos + adjustedDstSize;
1651 inBuff.size = inBuff.pos + adjustedCSrcSize;
1652 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1653 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Colleted1d0392017-06-19 11:07:33 -07001654 /* Good so far, but no more progress possible */
Yann Collet01743a32017-06-16 17:56:41 -07001655 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1656 } } } }
Yann Collet8dee0ec2017-06-18 23:25:15 -07001657 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet01743a32017-06-16 17:56:41 -07001658
1659_cleanup:
1660 ZSTD_freeCCtx(zc);
1661 ZSTD_freeDStream(zd);
1662 ZSTD_freeDStream(zd_noise);
Stella Lau023b24e2017-08-20 22:55:07 -07001663 ZSTD_freeCCtxParams(cctxParams);
Yann Collet01743a32017-06-16 17:56:41 -07001664 free(cNoiseBuffer[0]);
1665 free(cNoiseBuffer[1]);
1666 free(cNoiseBuffer[2]);
1667 free(cNoiseBuffer[3]);
1668 free(cNoiseBuffer[4]);
1669 free(copyBuffer);
1670 free(cBuffer);
1671 free(dstBuffer);
1672 return result;
1673
1674_output_error:
1675 result = 1;
1676 goto _cleanup;
1677}
1678
Yann Colletd7883a22016-08-12 16:48:02 +02001679/*-*******************************************************
1680* Command line
1681*********************************************************/
1682int FUZ_usage(const char* programName)
1683{
1684 DISPLAY( "Usage :\n");
1685 DISPLAY( " %s [args]\n", programName);
1686 DISPLAY( "\n");
1687 DISPLAY( "Arguments :\n");
1688 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1689 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1690 DISPLAY( " -t# : Select starting test number (default:0)\n");
1691 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1692 DISPLAY( " -v : verbose\n");
1693 DISPLAY( " -p : pause at the end\n");
1694 DISPLAY( " -h : display help and exit\n");
1695 return 0;
1696}
1697
Yann Collet01743a32017-06-16 17:56:41 -07001698typedef enum { simple_api, mt_api, advanced_api } e_api;
Yann Colletd7883a22016-08-12 16:48:02 +02001699
1700int main(int argc, const char** argv)
1701{
Yann Colletbd88f632017-11-27 12:15:23 -08001702 U32 seed = 0;
1703 int seedset = 0;
Yann Colletd7883a22016-08-12 16:48:02 +02001704 int nbTests = nbTestsDefault;
1705 int testNb = 0;
1706 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
Yann Colletbd88f632017-11-27 12:15:23 -08001707 int result = 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001708 int mainPause = 0;
Yann Colletafb0aca2017-06-29 18:19:09 -07001709 int bigTests = (sizeof(size_t) == 8);
Yann Collet01743a32017-06-16 17:56:41 -07001710 e_api selected_api = simple_api;
Yann Collet19d670b2017-01-19 15:32:07 -08001711 const char* const programName = argv[0];
Stella Lau73c73bf2017-08-21 12:41:19 -07001712 U32 useOpaqueAPI = 0;
Yann Colletbd88f632017-11-27 12:15:23 -08001713 int argNb;
Yann Colletd7883a22016-08-12 16:48:02 +02001714
1715 /* Check command line */
1716 for(argNb=1; argNb<argc; argNb++) {
1717 const char* argument = argv[argNb];
Yann Colletbd88f632017-11-27 12:15:23 -08001718 assert(argument != NULL);
Yann Colletd7883a22016-08-12 16:48:02 +02001719
1720 /* Parsing commands. Aggregated commands are allowed */
1721 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001722
Yann Collet47c6a952017-09-28 18:27:22 -07001723 if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; }
1724 if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; }
1725 if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; testNb += !testNb; useOpaqueAPI = 1; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001726 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001727
1728 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001729 while (*argument!=0) {
1730 switch(*argument)
1731 {
1732 case 'h':
1733 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001734
Yann Colletd7883a22016-08-12 16:48:02 +02001735 case 'v':
1736 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001737 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001738 break;
Yann Collet736788f2017-01-19 12:12:50 -08001739
Yann Colletd7883a22016-08-12 16:48:02 +02001740 case 'q':
1741 argument++;
1742 g_displayLevel--;
1743 break;
Yann Collet736788f2017-01-19 12:12:50 -08001744
Yann Colletd7883a22016-08-12 16:48:02 +02001745 case 'p': /* pause at the end */
1746 argument++;
1747 mainPause = 1;
1748 break;
1749
Yann Collet736788f2017-01-19 12:12:50 -08001750 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001751 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001752 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001753 while ((*argument>='0') && (*argument<='9')) {
1754 nbTests *= 10;
1755 nbTests += *argument - '0';
1756 argument++;
1757 }
1758 break;
1759
Yann Collet736788f2017-01-19 12:12:50 -08001760 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001761 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001762 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001763 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001764 g_clockTime *= 10;
1765 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001766 argument++;
1767 }
Yann Colletbd88f632017-11-27 12:15:23 -08001768 if (*argument=='m') { /* -T1m == -T60 */
1769 g_clockTime *=60, argument++;
1770 if (*argument=='n') argument++; /* -T1mn == -T60 */
1771 } else if (*argument=='s') argument++; /* -T10s == -T10 */
Nick Terrell9a2f6f42017-11-29 19:11:12 -08001772 g_clockTime *= SEC_TO_MICRO;
Yann Colletd7883a22016-08-12 16:48:02 +02001773 break;
1774
Yann Collet736788f2017-01-19 12:12:50 -08001775 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001776 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001777 seedset=1;
Yann Colletbd88f632017-11-27 12:15:23 -08001778 seed=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001779 while ((*argument>='0') && (*argument<='9')) {
1780 seed *= 10;
1781 seed += *argument - '0';
1782 argument++;
1783 }
1784 break;
1785
Yann Collet736788f2017-01-19 12:12:50 -08001786 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001787 argument++;
1788 testNb=0;
1789 while ((*argument>='0') && (*argument<='9')) {
1790 testNb *= 10;
1791 testNb += *argument - '0';
1792 argument++;
1793 }
1794 break;
1795
1796 case 'P': /* compressibility % */
1797 argument++;
1798 proba=0;
1799 while ((*argument>='0') && (*argument<='9')) {
1800 proba *= 10;
1801 proba += *argument - '0';
1802 argument++;
1803 }
1804 if (proba<0) proba=0;
1805 if (proba>100) proba=100;
1806 break;
1807
1808 default:
1809 return FUZ_usage(programName);
1810 }
1811 } } } /* for(argNb=1; argNb<argc; argNb++) */
1812
1813 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001814 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001815
Yann Colletef9999f2016-09-01 16:44:48 -07001816 if (!seedset) {
1817 time_t const t = time(NULL);
1818 U32 const h = XXH32(&t, sizeof(t), 1);
1819 seed = h % 10000;
1820 }
1821
Yann Colletd7883a22016-08-12 16:48:02 +02001822 DISPLAY("Seed = %u\n", seed);
1823 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1824
1825 if (nbTests<=0) nbTests=1;
1826
1827 if (testNb==0) {
Yann Colletbd88f632017-11-27 12:15:23 -08001828 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
Yann Collet4616fad2017-07-10 17:16:41 -07001829 }
Yann Colletd7883a22016-08-12 16:48:02 +02001830
Yann Collet01743a32017-06-16 17:56:41 -07001831 if (!result) {
1832 switch(selected_api)
1833 {
1834 case simple_api :
1835 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1836 break;
1837 case mt_api :
1838 result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1839 break;
1840 case advanced_api :
Stella Lau73c73bf2017-08-21 12:41:19 -07001841 result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI);
Yann Collet01743a32017-06-16 17:56:41 -07001842 break;
1843 default :
1844 assert(0); /* impossible */
1845 }
1846 }
Yann Colletd7883a22016-08-12 16:48:02 +02001847
1848 if (mainPause) {
1849 int unused;
1850 DISPLAY("Press Enter \n");
1851 unused = getchar();
1852 (void)unused;
1853 }
1854 return result;
1855}