2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /*-************************************
14 **************************************/
15 #ifdef _MSC_VER /* Visual Studio */
16 # 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 */
22 /*-************************************
24 **************************************/
25 #include <stdlib.h> /* free */
26 #include <stdio.h> /* fgets, sscanf */
27 #include <string.h> /* strcmp */
28 #include <assert.h> /* assert */
30 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
31 #include "zstd.h" /* ZSTD_compressBound */
32 #include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
33 #include "zstdmt_compress.h"
34 #include "zdict.h" /* ZDICT_trainFromBuffer */
35 #include "datagen.h" /* RDG_genBuffer */
36 #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
37 #include "xxhash.h" /* XXH64_* */
42 /*-************************************
44 **************************************/
49 static const U32 nbTestsDefault = 10000;
50 static const U32 g_cLevelMax_smallTests = 10;
51 #define COMPRESSIBLE_NOISE_LENGTH (10 MB)
52 #define FUZ_COMPRESSIBILITY_DEFAULT 50
53 static const U32 prime32 = 2654435761U;
56 /*-************************************
58 **************************************/
59 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
60 #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
61 DISPLAY(__VA_ARGS__); \
62 if (g_displayLevel>=4) fflush(stderr); }
63 static U32 g_displayLevel = 2;
65 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
66 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
68 #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
69 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
70 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
71 if (g_displayLevel>=4) fflush(stderr); } }
73 static U64 g_clockTime = 0;
76 /*-*******************************************************
78 *********************************************************/
81 #define MIN(a,b) ((a)<(b)?(a):(b))
82 #define MAX(a,b) ((a)>(b)?(a):(b))
84 @return : a 27 bits random value, from a 32-bits `seed`.
85 `seed` is also modified */
86 #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
87 unsigned int FUZ_rand(unsigned int* seedPtr)
89 static const U32 prime2 = 2246822519U;
90 U32 rand32 = *seedPtr;
93 rand32 = FUZ_rotl32(rand32, 13);
98 #define CHECK(cond, ...) { \
100 DISPLAY("Error => "); \
101 DISPLAY(__VA_ARGS__); \
102 DISPLAY(" (seed %u, test nb %u, line %u) \n", \
103 seed, testNb, __LINE__); \
104 goto _output_error; \
107 #define CHECK_Z(f) { \
108 size_t const err = f; \
109 CHECK(ZSTD_isError(err), "%s : %s ", \
110 #f, ZSTD_getErrorName(err)); \
114 /*======================================================
116 ======================================================*/
124 static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
126 static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
128 buffer_t dict = { NULL, 0, 0 };
129 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
130 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
131 if (!blockSizes) return dict;
132 dict.start = malloc(requestedDictSize);
133 if (!dict.start) { free(blockSizes); return dict; }
135 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
136 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
138 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
140 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
141 dict.size = requestedDictSize;
142 dict.filled = dictSize;
143 return dict; /* how to return dictSize ? */
147 static void FUZ_freeDictionary(buffer_t dict)
152 /* Round trips data and updates xxh with the decompressed data produced */
153 static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
154 XXH64_state_t* xxh, void* data, size_t size,
155 ZSTD_EndDirective endOp)
157 static BYTE compressed[1024];
158 static BYTE uncompressed[1024];
160 ZSTD_inBuffer cin = {data, size, 0};
164 ZSTD_outBuffer cout = {compressed, sizeof(compressed), 0};
165 ZSTD_inBuffer din = {compressed, 0, 0};
166 ZSTD_outBuffer dout = {uncompressed, 0, 0};
168 cret = ZSTD_compress_generic(cctx, &cout, &cin, endOp);
169 if (ZSTD_isError(cret))
173 while (din.pos < din.size || (endOp == ZSTD_e_end && cret == 0)) {
177 dout.size = sizeof(uncompressed);
178 dret = ZSTD_decompressStream(dctx, &dout, &din);
179 if (ZSTD_isError(dret))
181 XXH64_update(xxh, dout.dst, dout.pos);
185 } while (cin.pos < cin.size || (endOp != ZSTD_e_continue && cret != 0));
189 /* Generates some data and round trips it */
190 static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
191 XXH64_state_t* xxh, SEQ_stream* seq,
192 SEQ_gen_type type, unsigned value)
194 static BYTE data[1024];
198 SEQ_outBuffer sout = {data, sizeof(data), 0};
200 gen = SEQ_gen(seq, type, value, &sout);
202 ret = SEQ_roundTrip(cctx, dctx, xxh, sout.dst, sout.pos, ZSTD_e_continue);
203 if (ZSTD_isError(ret))
210 static int basicUnitTests(U32 seed, double compressibility)
212 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
213 void* CNBuffer = malloc(CNBufferSize);
214 size_t const skippableFrameSize = 200 KB;
215 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
216 void* compressedBuffer = malloc(compressedBufferSize);
217 size_t const decodedBufferSize = CNBufferSize;
218 void* decodedBuffer = malloc(decodedBufferSize);
222 U32 coreSeed = 0; /* this name to conform with CHECK_Z macro display */
223 ZSTD_CStream* zc = ZSTD_createCStream();
224 ZSTD_DStream* zd = ZSTD_createDStream();
225 ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
227 ZSTD_inBuffer inBuff, inBuff2;
228 ZSTD_outBuffer outBuff;
229 buffer_t dictionary = g_nullBuffer;
230 size_t const dictSize = 128 KB;
233 /* Create compressible test buffer */
234 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
235 DISPLAY("Not enough memory, aborting \n");
238 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
240 /* Create dictionary */
241 DISPLAYLEVEL(3, "creating dictionary for unit tests \n");
242 dictionary = FUZ_createDictionary(CNBuffer, CNBufferSize / 3, 16 KB, 48 KB);
243 if (!dictionary.start) {
244 DISPLAY("Error creating dictionary, aborting \n");
247 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
249 /* Basic compression test */
250 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
251 CHECK_Z( ZSTD_initCStream(zc, 1 /* cLevel */) );
252 outBuff.dst = (char*)(compressedBuffer);
253 outBuff.size = compressedBufferSize;
255 inBuff.src = CNBuffer;
256 inBuff.size = CNBufferSize;
258 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
259 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
260 { size_t const r = ZSTD_endStream(zc, &outBuff);
261 if (r != 0) goto _output_error; } /* error, or some data not flushed */
262 DISPLAYLEVEL(3, "OK (%u bytes)\n", (U32)outBuff.pos);
264 /* generate skippable frame */
265 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
266 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
267 cSize = skippableFrameSize + 8;
269 /* Basic compression test using dict */
270 DISPLAYLEVEL(3, "test%3i : skipframe + compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
271 CHECK_Z( ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1 /* cLevel */) );
272 outBuff.dst = (char*)(compressedBuffer)+cSize;
273 assert(compressedBufferSize > cSize);
274 outBuff.size = compressedBufferSize - cSize;
276 inBuff.src = CNBuffer;
277 inBuff.size = CNBufferSize;
279 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
280 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
281 { size_t const r = ZSTD_endStream(zc, &outBuff);
282 if (r != 0) goto _output_error; } /* error, or some data not flushed */
283 cSize += outBuff.pos;
284 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
286 /* context size functions */
287 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
288 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
289 size_t const cstreamSize = ZSTD_estimateCStreamSize_usingCParams(cParams);
290 size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); /* uses ZSTD_initCStream_usingDict() */
291 if (ZSTD_isError(cstreamSize)) goto _output_error;
292 if (ZSTD_isError(cdictSize)) goto _output_error;
293 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)(cstreamSize + cdictSize));
296 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
297 { size_t const s = ZSTD_sizeof_CStream(zc);
298 if (ZSTD_isError(s)) goto _output_error;
299 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
302 /* Attempt bad compression parameters */
303 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
305 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
306 params.cParams.searchLength = 2;
307 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
308 if (!ZSTD_isError(r)) goto _output_error;
309 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
312 /* skippable frame test */
313 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
314 CHECK_Z( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) );
315 inBuff.src = compressedBuffer;
318 outBuff.dst = decodedBuffer;
319 outBuff.size = CNBufferSize;
321 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
322 DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r);
323 if (r != 0) goto _output_error;
325 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
326 DISPLAYLEVEL(3, "OK \n");
328 /* Basic decompression test */
330 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
331 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
332 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000) ); /* large limit */
333 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
334 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
335 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
336 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
337 DISPLAYLEVEL(3, "OK \n");
339 /* Re-use without init */
340 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
342 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
343 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
344 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
345 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
346 DISPLAYLEVEL(3, "OK \n");
348 /* check regenerated data is byte exact */
349 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
351 for (i=0; i<CNBufferSize; i++) {
352 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
354 DISPLAYLEVEL(3, "OK \n");
356 /* context size functions */
357 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
358 { ZSTD_frameHeader fhi;
359 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
360 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
361 if (gfhError!=0) goto _output_error;
362 DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize);
363 { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize)
364 /* uses ZSTD_initDStream_usingDict() */
365 + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
366 if (ZSTD_isError(s)) goto _output_error;
367 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
370 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
371 { size_t const s = ZSTD_sizeof_DStream(zd);
372 if (ZSTD_isError(s)) goto _output_error;
373 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
376 /* Decompression by small increment */
377 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
378 { /* skippable frame */
380 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
381 inBuff.src = compressedBuffer;
382 outBuff.dst = decodedBuffer;
385 while (r) { /* skippable frame */
386 size_t const inSize = FUZ_rand(&coreSeed) & 15;
387 size_t const outSize = FUZ_rand(&coreSeed) & 15;
388 inBuff.size = inBuff.pos + inSize;
389 outBuff.size = outBuff.pos + outSize;
390 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
391 if (ZSTD_isError(r)) goto _output_error;
394 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
397 size_t const inSize = FUZ_rand(&coreSeed) & 15;
398 size_t const outSize = FUZ_rand(&coreSeed) & 15;
399 inBuff.size = inBuff.pos + inSize;
400 outBuff.size = outBuff.pos + outSize;
401 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
402 if (ZSTD_isError(r)) goto _output_error;
405 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
406 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
407 DISPLAYLEVEL(3, "OK \n");
409 /* check regenerated data is byte exact */
410 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
412 for (i=0; i<CNBufferSize; i++) {
413 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
415 DISPLAYLEVEL(3, "OK \n");
417 /* _srcSize compression test */
418 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
419 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
420 outBuff.dst = (char*)(compressedBuffer);
421 outBuff.size = compressedBufferSize;
423 inBuff.src = CNBuffer;
424 inBuff.size = CNBufferSize;
426 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
427 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
428 { size_t const r = ZSTD_endStream(zc, &outBuff);
429 if (r != 0) goto _output_error; } /* error, or some data not flushed */
430 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
431 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
432 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
434 /* wrong _srcSize compression test */
435 DISPLAYLEVEL(3, "test%3i : too large srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
436 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize+1);
437 outBuff.dst = (char*)(compressedBuffer);
438 outBuff.size = compressedBufferSize;
440 inBuff.src = CNBuffer;
441 inBuff.size = CNBufferSize;
443 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
444 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
445 { size_t const r = ZSTD_endStream(zc, &outBuff);
446 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
447 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
449 /* wrong _srcSize compression test */
450 DISPLAYLEVEL(3, "test%3i : too small srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
451 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
452 outBuff.dst = (char*)(compressedBuffer);
453 outBuff.size = compressedBufferSize;
455 inBuff.src = CNBuffer;
456 inBuff.size = CNBufferSize;
458 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
459 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
460 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r));
463 /* Complex context re-use scenario */
464 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
465 ZSTD_freeCStream(zc);
466 zc = ZSTD_createCStream();
467 if (zc==NULL) goto _output_error; /* memory allocation issue */
469 { size_t const inSize = 513;
470 DISPLAYLEVEL(5, "use1 ");
471 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
472 inBuff.src = CNBuffer;
473 inBuff.size = inSize;
475 outBuff.dst = (char*)(compressedBuffer)+cSize;
476 outBuff.size = ZSTD_compressBound(inSize);
478 DISPLAYLEVEL(5, "compress1 ");
479 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
480 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
481 DISPLAYLEVEL(5, "end1 ");
482 { size_t const r = ZSTD_endStream(zc, &outBuff);
483 if (r != 0) goto _output_error; } /* error, or some data not flushed */
486 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
487 DISPLAYLEVEL(5, "use2 ");
488 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
489 inBuff.src = CNBuffer;
490 inBuff.size = inSize;
492 outBuff.dst = (char*)(compressedBuffer)+cSize;
493 outBuff.size = ZSTD_compressBound(inSize);
495 DISPLAYLEVEL(5, "compress2 ");
496 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
497 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
498 DISPLAYLEVEL(5, "end2 ");
499 { size_t const r = ZSTD_endStream(zc, &outBuff);
500 if (r != 0) goto _output_error; } /* error, or some data not flushed */
502 DISPLAYLEVEL(3, "OK \n");
505 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
506 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ );
507 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
508 DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (U32)initError);
509 if (ZSTD_isError(initError)) goto _output_error;
511 outBuff.dst = compressedBuffer;
512 outBuff.size = compressedBufferSize;
514 inBuff.src = CNBuffer;
515 inBuff.size = CNBufferSize;
517 DISPLAYLEVEL(5, "- starting ZSTD_compressStream ");
518 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
519 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
520 { size_t const r = ZSTD_endStream(zc, &outBuff);
521 DISPLAYLEVEL(5, "- ZSTD_endStream result : %u ", (U32)r);
522 if (r != 0) goto _output_error; /* error, or some data not flushed */
525 ZSTD_freeCDict(cdict);
526 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
529 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
530 { size_t const s = ZSTD_sizeof_CStream(zc);
531 if (ZSTD_isError(s)) goto _output_error;
532 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
535 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
536 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
537 if (dID != dictID) goto _output_error;
538 DISPLAYLEVEL(4, "OK (%u) \n", dID);
542 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
543 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
544 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
545 if (ZSTD_isError(initError)) goto _output_error;
546 outBuff.dst = decodedBuffer;
547 outBuff.size = CNBufferSize;
549 inBuff.src = compressedBuffer;
552 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
553 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
554 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
555 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
556 ZSTD_freeDDict(ddict);
557 DISPLAYLEVEL(3, "OK \n");
560 /* test ZSTD_setDStreamParameter() resilience */
561 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
562 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
563 if (!ZSTD_isError(r)) goto _output_error; }
564 DISPLAYLEVEL(3, "OK \n");
566 /* Memory restriction */
567 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
568 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
569 CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000) ); /* too small limit */
570 outBuff.dst = decodedBuffer;
571 outBuff.size = CNBufferSize;
573 inBuff.src = compressedBuffer;
576 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
577 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
578 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
579 ZSTD_DCtx_reset(zd); /* leave zd in good shape for next tests */
581 DISPLAYLEVEL(3, "test%3i : dictionary source size and level : ", testNb++);
582 { ZSTD_DCtx* const dctx = ZSTD_createDCtx();
583 int const maxLevel = 16; /* first level with zstd_opt */
585 assert(maxLevel < ZSTD_maxCLevel());
586 CHECK_Z( ZSTD_DCtx_loadDictionary_byReference(dctx, dictionary.start, dictionary.filled) );
587 for (level = 1; level <= maxLevel; ++level) {
588 ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, level);
589 size_t const maxSize = MIN(1 MB, CNBufferSize);
591 for (size = 512; size <= maxSize; size <<= 1) {
592 U64 const crcOrig = XXH64(CNBuffer, size, 0);
593 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
594 outBuff.dst = compressedBuffer;
595 outBuff.size = compressedBufferSize;
597 inBuff.src = CNBuffer;
600 CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
601 CHECK_Z(ZSTD_compress_generic(cctx, &outBuff, &inBuff, ZSTD_e_end));
602 if (inBuff.pos != inBuff.size) goto _output_error;
603 { ZSTD_outBuffer decOut = {decodedBuffer, size, 0};
604 ZSTD_inBuffer decIn = {outBuff.dst, outBuff.pos, 0};
605 CHECK_Z( ZSTD_decompress_generic(dctx, &decOut, &decIn) );
606 if (decIn.pos != decIn.size) goto _output_error;
607 if (decOut.pos != size) goto _output_error;
608 { U64 const crcDec = XXH64(decOut.dst, decOut.pos, 0);
609 if (crcDec != crcOrig) goto _output_error;
613 ZSTD_freeCDict(cdict);
617 DISPLAYLEVEL(3, "OK\n");
619 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
620 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
621 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
622 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_auto, cParams, ZSTD_defaultCMem);
623 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
624 if (ZSTD_isError(initError)) goto _output_error;
626 outBuff.dst = compressedBuffer;
627 outBuff.size = compressedBufferSize;
629 inBuff.src = CNBuffer;
630 inBuff.size = CNBufferSize;
632 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
633 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
634 { size_t const r = ZSTD_endStream(zc, &outBuff);
635 if (r != 0) goto _output_error; } /* error, or some data not flushed */
637 ZSTD_freeCDict(cdict);
638 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
641 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
642 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
643 if (did != 0) goto _output_error;
645 DISPLAYLEVEL(3, "OK (not detected) \n");
647 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
648 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
649 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
650 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
653 DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++);
654 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled) );
655 outBuff.dst = compressedBuffer;
656 outBuff.size = compressedBufferSize;
658 inBuff.src = CNBuffer;
659 inBuff.size = CNBufferSize;
661 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
662 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
664 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
666 DISPLAYLEVEL(3, "test%3i : decompress with ZSTD_DCtx_refPrefix : ", testNb++);
667 CHECK_Z( ZSTD_DCtx_refPrefix(zd, dictionary.start, dictionary.filled) );
668 outBuff.dst = decodedBuffer;
669 outBuff.size = CNBufferSize;
671 inBuff.src = compressedBuffer;
674 CHECK_Z( ZSTD_decompress_generic(zd, &outBuff, &inBuff) );
675 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
676 if (outBuff.pos != CNBufferSize) goto _output_error; /* must regenerate whole input */
677 DISPLAYLEVEL(3, "OK \n");
679 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++);
680 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
681 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
682 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
685 DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++);
686 outBuff.dst = compressedBuffer;
687 outBuff.size = compressedBufferSize;
689 inBuff.src = CNBuffer;
690 inBuff.size = CNBufferSize;
692 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
693 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
695 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
697 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++);
698 CHECK_Z( ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize) );
699 DISPLAYLEVEL(3, "OK \n");
702 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
703 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
704 params.fParams.contentSizeFlag = 1;
705 CHECK_Z( ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0 /* pledgedSrcSize==0 means "empty" when params.fParams.contentSizeFlag is set */) );
706 } /* cstream advanced shall write content size = 0 */
707 outBuff.dst = compressedBuffer;
708 outBuff.size = compressedBufferSize;
710 inBuff.src = CNBuffer;
713 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
714 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
716 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
717 DISPLAYLEVEL(3, "OK \n");
719 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
720 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
721 params.fParams.contentSizeFlag = 1;
722 CHECK_Z( ZSTD_initCStream_advanced(zc, NULL, 0, params, 0) );
723 } /* cstream advanced shall write content size = 0 */
724 inBuff.src = CNBuffer;
727 outBuff.dst = compressedBuffer;
728 outBuff.size = compressedBufferSize;
730 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
731 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
733 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
735 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
736 outBuff.dst = compressedBuffer;
737 outBuff.size = compressedBufferSize;
739 inBuff.src = CNBuffer;
742 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
743 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
745 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
746 DISPLAYLEVEL(3, "OK \n");
748 /* Basic multithreading compression test */
749 DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
750 { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
751 CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) );
753 outBuff.dst = compressedBuffer;
754 outBuff.size = compressedBufferSize;
756 inBuff.src = CNBuffer;
757 inBuff.size = CNBufferSize;
759 { size_t const compressResult = ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end);
760 if (compressResult != 0) goto _output_error; /* compression must be completed in a single round */
762 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
763 { size_t const compressedSize = ZSTD_findFrameCompressedSize(compressedBuffer, outBuff.pos);
764 if (compressedSize != outBuff.pos) goto _output_error; /* must be a full valid frame */
766 DISPLAYLEVEL(3, "OK \n");
768 /* Complex multithreading + dictionary test */
769 { U32 const nbWorkers = 2;
770 size_t const jobSize = 4 * 1 MB;
771 size_t const srcSize = jobSize * nbWorkers; /* we want each job to have predictable size */
772 size_t const segLength = 2 KB;
773 size_t const offset = 600 KB; /* must be larger than window defined in cdict */
774 size_t const start = jobSize + (offset-1);
775 const BYTE* const srcToCopy = (const BYTE*)CNBuffer + start;
776 BYTE* const dst = (BYTE*)CNBuffer + start - offset;
777 DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads + dictionary : ", testNb++, (U32)srcSize);
778 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_compressionLevel, 3) );
779 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_nbWorkers, nbWorkers) );
780 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_jobSize, jobSize) );
781 assert(start > offset);
782 assert(start + segLength < COMPRESSIBLE_NOISE_LENGTH);
783 memcpy(dst, srcToCopy, segLength); /* create a long repetition at long distance for job 2 */
784 outBuff.dst = compressedBuffer;
785 outBuff.size = compressedBufferSize;
787 inBuff.src = CNBuffer;
788 inBuff.size = srcSize; assert(srcSize < COMPRESSIBLE_NOISE_LENGTH);
791 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, 4 KB, dictionary.filled); /* intentionnally lies on estimatedSrcSize, to push cdict into targeting a small window size */
792 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
793 DISPLAYLEVEL(5, "cParams.windowLog = %u : ", cParams.windowLog);
794 CHECK_Z( ZSTD_CCtx_refCDict(zc, cdict) );
795 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) );
796 CHECK_Z( ZSTD_CCtx_refCDict(zc, NULL) ); /* do not keep a reference to cdict, as its lifetime ends */
797 ZSTD_freeCDict(cdict);
799 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
801 DISPLAYLEVEL(3, "OK \n");
803 DISPLAYLEVEL(3, "test%3i : decompress large frame created from multiple threads + dictionary : ", testNb++);
804 { ZSTD_DStream* const dstream = ZSTD_createDCtx();
805 ZSTD_frameHeader zfh;
806 ZSTD_getFrameHeader(&zfh, compressedBuffer, cSize);
807 DISPLAYLEVEL(5, "frame windowsize = %u : ", (U32)zfh.windowSize);
808 outBuff.dst = decodedBuffer;
809 outBuff.size = CNBufferSize;
811 inBuff.src = compressedBuffer;
813 CHECK_Z( ZSTD_initDStream_usingDict(dstream, dictionary.start, dictionary.filled) );
814 inBuff.size = 1; /* avoid shortcut to single-pass mode */
815 CHECK_Z( ZSTD_decompressStream(dstream, &outBuff, &inBuff) );
817 CHECK_Z( ZSTD_decompressStream(dstream, &outBuff, &inBuff) );
818 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
819 ZSTD_freeDStream(dstream);
821 DISPLAYLEVEL(3, "OK \n");
823 DISPLAYLEVEL(3, "test%3i : check dictionary FSE tables can represent every code : ", testNb++);
824 { unsigned const kMaxWindowLog = 24;
826 ZSTD_compressionParameters cParams = ZSTD_getCParams(3, 1U << kMaxWindowLog, 1024);
829 SEQ_stream seq = SEQ_initStream(0x87654321);
833 XXH64_reset(&xxh, 0);
834 cParams.windowLog = kMaxWindowLog;
835 cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem);
836 ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
838 if (!cdict || !ddict) goto _output_error;
841 ZSTD_resetDStream(zd);
842 CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
843 CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict));
844 CHECK_Z(ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1U << kMaxWindowLog));
845 /* Test all values < 300 */
846 for (value = 0; value < 300; ++value) {
847 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
848 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
851 /* Test values 2^8 to 2^17 */
852 for (value = (1 << 8); value < (1 << 17); value <<= 1) {
853 for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) {
854 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value));
855 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value + (value >> 2)));
858 /* Test offset values up to the max window log */
859 for (value = 8; value <= kMaxWindowLog; ++value) {
860 CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, SEQ_gen_of, (1U << value) - 1));
863 CHECK_Z(SEQ_roundTrip(zc, zd, &xxh, NULL, 0, ZSTD_e_end));
864 CHECK(SEQ_digest(&seq) != XXH64_digest(&xxh), "SEQ XXH64 does not match");
866 ZSTD_freeCDict(cdict);
867 ZSTD_freeDDict(ddict);
869 DISPLAYLEVEL(3, "OK \n");
871 /* Overlen overwriting window data bug */
872 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
873 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
874 1. 'a' repeated 517 times
875 2. 'b' repeated 516 times
876 3. a compressed block with no literals and 3 sequence commands:
877 litlength = 0, offset = 24, match length = 24
878 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
879 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
881 const char* testCase =
882 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
883 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
884 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
885 ZSTD_DStream* const zds = ZSTD_createDStream();
886 if (zds==NULL) goto _output_error;
888 CHECK_Z( ZSTD_initDStream(zds) );
889 inBuff.src = testCase;
892 outBuff.dst = decodedBuffer;
893 outBuff.size = CNBufferSize;
896 while (inBuff.pos < inBuff.size) {
897 CHECK_Z( ZSTD_decompressStream(zds, &outBuff, &inBuff) );
900 ZSTD_freeDStream(zds);
902 DISPLAYLEVEL(3, "OK \n");
905 FUZ_freeDictionary(dictionary);
906 ZSTD_freeCStream(zc);
907 ZSTD_freeDStream(zd);
908 ZSTDMT_freeCCtx(mtctx);
910 free(compressedBuffer);
916 DISPLAY("Error detected in Unit tests ! \n");
921 /* ====== Fuzzer tests ====== */
923 static size_t findDiff(const void* buf1, const void* buf2, size_t max)
925 const BYTE* b1 = (const BYTE*)buf1;
926 const BYTE* b2 = (const BYTE*)buf2;
928 for (u=0; u<max; u++) {
929 if (b1[u] != b2[u]) break;
932 DISPLAY("=> No difference detected within %u bytes \n", (U32)max);
935 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
937 DISPLAY(" %02X %02X %02X ",
938 b1[u-3], b1[u-2], b1[u-1]);
939 DISPLAY(" :%02X: %02X %02X %02X %02X %02X \n",
940 b1[u], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]);
942 DISPLAY(" %02X %02X %02X ",
943 b2[u-3], b2[u-2], b2[u-1]);
944 DISPLAY(" :%02X: %02X %02X %02X %02X %02X \n",
945 b2[u], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]);
949 static size_t FUZ_rLogLength(U32* seed, U32 logLength)
951 size_t const lengthMask = ((size_t)1 << logLength) - 1;
952 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
955 static size_t FUZ_randomLength(U32* seed, U32 maxLog)
957 U32 const logLength = FUZ_rand(seed) % maxLog;
958 return FUZ_rLogLength(seed, logLength);
961 /* Return value in range minVal <= v <= maxVal */
962 static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal)
964 U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal;
965 return (U32)((FUZ_rand(seed) % mod) + minVal);
968 static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
970 U32 const maxSrcLog = bigTests ? 24 : 22;
971 static const U32 maxSampleLog = 19;
972 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
973 BYTE* cNoiseBuffer[5];
974 size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog);
975 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
976 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
977 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
978 size_t const dstBufferSize = srcBufferSize;
979 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
983 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */
984 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */
985 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
986 UTIL_time_t const startClock = UTIL_getTime();
987 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
990 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests;
993 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
994 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
995 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
996 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
997 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
998 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
999 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1000 "Not enough memory, fuzzer tests cancelled");
1002 /* Create initial samples */
1003 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1004 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1005 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1006 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1007 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1008 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1009 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1011 /* catch up testNb */
1012 for (testNb=1; testNb < startTest; testNb++)
1013 FUZ_rand(&coreSeed);
1016 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
1018 const BYTE* srcBuffer;
1019 size_t totalTestSize, totalGenSize, cSize;
1020 XXH64_state_t xxhState;
1022 U32 resetAllowed = 1;
1026 FUZ_rand(&coreSeed);
1027 lseed = coreSeed ^ prime32;
1028 if (nbTests >= testNb) {
1029 DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests);
1031 DISPLAYUPDATE(2, "\r%6u ", testNb);
1034 /* states full reset (deliberately not synchronized) */
1035 /* some issues can only happen when reusing states */
1036 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1037 ZSTD_freeCStream(zc);
1038 zc = ZSTD_createCStream();
1039 CHECK(zc==NULL, "ZSTD_createCStream : allocation error");
1042 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1043 ZSTD_freeDStream(zd);
1044 zd = ZSTD_createDStream();
1045 CHECK(zd==NULL, "ZSTD_createDStream : allocation error");
1046 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
1049 /* srcBuffer selection [0-4] */
1050 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1051 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1055 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1056 buffNb = tnb[buffNb >> 3];
1058 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1059 buffNb = tnb[buffNb >> 3];
1061 srcBuffer = cNoiseBuffer[buffNb];
1064 /* compression init */
1065 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1066 && oldTestLog /* at least one test happened */ && resetAllowed) {
1067 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1068 maxTestSize = MIN(maxTestSize, srcBufferSize-16);
1069 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
1070 CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) );
1073 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1074 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
1075 U32 const cLevelCandidate = ( FUZ_rand(&lseed) %
1077 (MAX(testLog, dictLog) / 3)))
1079 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
1080 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1081 oldTestLog = testLog;
1082 /* random dictionary selection */
1083 dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1084 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1085 dict = srcBuffer + dictStart;
1087 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1088 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
1089 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1090 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
1091 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
1092 CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
1095 /* multi-segments compression test */
1096 XXH64_reset(&xxhState, 0);
1097 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1099 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1100 /* compress random chunks into randomly sized dst buffers */
1101 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1102 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1103 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1104 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1105 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1106 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1107 outBuff.size = outBuff.pos + dstBuffSize;
1109 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
1111 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1112 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1113 totalTestSize += inBuff.pos;
1116 /* random flush operation, to mess around */
1117 if ((FUZ_rand(&lseed) & 15) == 0) {
1118 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1119 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1120 outBuff.size = outBuff.pos + adjustedDstSize;
1121 CHECK_Z( ZSTD_flushStream(zc, &outBuff) );
1124 /* final frame epilogue */
1125 { size_t remainingToFlush = (size_t)(-1);
1126 while (remainingToFlush) {
1127 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1128 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1129 outBuff.size = outBuff.pos + adjustedDstSize;
1130 remainingToFlush = ZSTD_endStream(zc, &outBuff);
1131 CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush));
1133 crcOrig = XXH64_digest(&xxhState);
1134 cSize = outBuff.pos;
1137 /* multi - fragments decompression test */
1138 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1139 CHECK_Z ( ZSTD_resetDStream(zd) );
1141 CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
1143 { size_t decompressionResult = 1;
1144 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1145 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1146 for (totalGenSize = 0 ; decompressionResult ; ) {
1147 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1148 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1149 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1150 inBuff.size = inBuff.pos + readCSrcSize;
1151 outBuff.size = outBuff.pos + dstBuffSize;
1152 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1153 if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) {
1154 DISPLAY("checksum error : \n");
1155 findDiff(copyBuffer, dstBuffer, totalTestSize);
1157 CHECK( ZSTD_isError(decompressionResult), "decompression error : %s",
1158 ZSTD_getErrorName(decompressionResult) );
1160 CHECK (decompressionResult != 0, "frame not fully decoded");
1161 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)",
1162 (U32)outBuff.pos, (U32)totalTestSize);
1163 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
1164 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1165 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1166 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1169 /*===== noisy/erroneous src decompression test =====*/
1171 /* add some noise */
1172 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1173 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1174 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1175 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1176 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1177 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1178 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1181 /* try decompression on noisy data */
1182 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
1183 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1184 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1185 while (outBuff.pos < dstBufferSize) {
1186 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1187 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1188 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1189 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1190 outBuff.size = outBuff.pos + adjustedDstSize;
1191 inBuff.size = inBuff.pos + adjustedCSrcSize;
1192 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1193 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
1194 /* No forward progress possible */
1195 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1197 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1200 ZSTD_freeCStream(zc);
1201 ZSTD_freeDStream(zd);
1202 ZSTD_freeDStream(zd_noise);
1203 free(cNoiseBuffer[0]);
1204 free(cNoiseBuffer[1]);
1205 free(cNoiseBuffer[2]);
1206 free(cNoiseBuffer[3]);
1207 free(cNoiseBuffer[4]);
1219 /* Multi-threading version of fuzzer Tests */
1220 static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
1222 const U32 maxSrcLog = bigTests ? 24 : 22;
1223 static const U32 maxSampleLog = 19;
1224 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1225 BYTE* cNoiseBuffer[5];
1226 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1227 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1228 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1229 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1230 size_t const dstBufferSize = srcBufferSize;
1231 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1234 U32 coreSeed = seed;
1236 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */
1237 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1238 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
1239 UTIL_time_t const startClock = UTIL_getTime();
1240 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
1241 size_t dictSize = 0;
1242 int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1243 U32 const nbThreadsMax = bigTests ? 4 : 2;
1246 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1247 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1248 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1249 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1250 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1251 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1252 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1253 "Not enough memory, fuzzer tests cancelled");
1255 /* Create initial samples */
1256 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1257 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1258 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1259 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1260 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1261 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1262 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1263 DISPLAYLEVEL(6, "Creating initial context with %u threads \n", nbThreads);
1265 /* catch up testNb */
1266 for (testNb=1; testNb < startTest; testNb++)
1267 FUZ_rand(&coreSeed);
1270 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
1272 const BYTE* srcBuffer;
1273 size_t totalTestSize, totalGenSize, cSize;
1274 XXH64_state_t xxhState;
1278 FUZ_rand(&coreSeed);
1279 if (nbTests >= testNb) {
1280 DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests);
1282 DISPLAYUPDATE(2, "\r%6u ", testNb);
1284 lseed = coreSeed ^ prime32;
1286 /* states full reset (deliberately not synchronized) */
1287 /* some issues can only happen when reusing states */
1288 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1289 nbThreads = (FUZ_rand(&lseed) % nbThreadsMax) + 1;
1290 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
1291 ZSTDMT_freeCCtx(zc);
1292 zc = ZSTDMT_createCCtx(nbThreads);
1293 CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error")
1295 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1296 ZSTD_freeDStream(zd);
1297 zd = ZSTD_createDStream();
1298 CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error")
1299 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1302 /* srcBuffer selection [0-4] */
1303 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1304 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1308 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1309 buffNb = tnb[buffNb >> 3];
1311 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1312 buffNb = tnb[buffNb >> 3];
1314 srcBuffer = cNoiseBuffer[buffNb];
1317 /* compression init */
1318 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1319 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
1320 int const cLevelCandidate = ( FUZ_rand(&lseed)
1321 % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) )
1323 int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */
1324 int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */
1325 int const cLevel = MIN(cLevelMin, cLevelMax);
1326 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1328 if (FUZ_rand(&lseed)&1) { /* simple init */
1329 int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
1330 DISPLAYLEVEL(5, "Init with compression level = %i \n", compressionLevel);
1331 CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) );
1332 } else { /* advanced init */
1333 /* random dictionary selection */
1334 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1335 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1336 dict = srcBuffer + dictStart;
1338 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1339 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
1340 DISPLAYLEVEL(5, "Init with windowLog = %u, pledgedSrcSize = %u, dictSize = %u \n",
1341 params.cParams.windowLog, (U32)pledgedSrcSize, (U32)dictSize);
1342 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1343 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
1344 params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1;
1345 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
1346 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) );
1347 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_jobSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) ); /* custome job size */
1348 CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
1351 /* multi-segments compression test */
1352 XXH64_reset(&xxhState, 0);
1353 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1355 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1356 /* compress random chunks into randomly sized dst buffers */
1357 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1358 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1359 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1360 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1361 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1362 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1363 outBuff.size = outBuff.pos + dstBuffSize;
1365 DISPLAYLEVEL(6, "Sending %u bytes to compress \n", (U32)srcSize);
1366 CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) );
1367 DISPLAYLEVEL(6, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
1369 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1370 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1371 totalTestSize += inBuff.pos;
1374 /* random flush operation, to mess around */
1375 if ((FUZ_rand(&lseed) & 15) == 0) {
1376 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1377 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1378 size_t const previousPos = outBuff.pos;
1379 outBuff.size = outBuff.pos + adjustedDstSize;
1380 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
1381 CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) );
1382 assert(outBuff.pos >= previousPos);
1383 DISPLAYLEVEL(6, "%u bytes flushed by ZSTDMT_flushStream \n", (U32)(outBuff.pos-previousPos));
1386 /* final frame epilogue */
1387 { size_t remainingToFlush = (size_t)(-1);
1388 while (remainingToFlush) {
1389 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1390 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1391 size_t const previousPos = outBuff.pos;
1392 outBuff.size = outBuff.pos + adjustedDstSize;
1393 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1394 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
1395 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
1396 assert(outBuff.pos >= previousPos);
1397 DISPLAYLEVEL(6, "%u bytes flushed by ZSTDMT_endStream \n", (U32)(outBuff.pos-previousPos));
1398 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1400 crcOrig = XXH64_digest(&xxhState);
1401 cSize = outBuff.pos;
1402 DISPLAYLEVEL(5, "Frame completed : %u bytes compressed into %u bytes \n",
1403 (U32)totalTestSize, (U32)cSize);
1406 /* multi - fragments decompression test */
1407 assert(totalTestSize < dstBufferSize);
1408 memset(dstBuffer, 170, totalTestSize); /* init dest area */
1409 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1410 CHECK_Z( ZSTD_resetDStream(zd) );
1412 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
1414 { size_t decompressionResult = 1;
1415 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1416 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1417 for (totalGenSize = 0 ; decompressionResult ; ) {
1418 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1419 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1420 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1421 inBuff.size = inBuff.pos + readCSrcSize;
1422 outBuff.size = outBuff.pos + dstBuffSize;
1423 DISPLAYLEVEL(6, "ZSTD_decompressStream input %u bytes into outBuff %u bytes \n",
1424 (U32)readCSrcSize, (U32)dstBuffSize);
1425 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1426 if (ZSTD_isError(decompressionResult)) {
1427 DISPLAY("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(decompressionResult));
1428 findDiff(copyBuffer, dstBuffer, totalTestSize);
1430 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
1431 DISPLAYLEVEL(6, "total ingested (inBuff.pos) = %u and produced (outBuff.pos) = %u \n",
1432 (U32)inBuff.pos, (U32)outBuff.pos);
1434 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1435 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1436 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1437 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1438 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1441 /*===== noisy/erroneous src decompression test =====*/
1443 /* add some noise */
1444 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1445 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1446 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1447 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1448 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1449 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1450 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1453 /* try decompression on noisy data */
1454 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
1455 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1456 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1457 while (outBuff.pos < dstBufferSize) {
1458 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1459 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1460 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1461 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1462 outBuff.size = outBuff.pos + adjustedDstSize;
1463 inBuff.size = inBuff.pos + adjustedCSrcSize;
1464 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1465 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
1466 /* No forward progress possible */
1467 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1469 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1472 ZSTDMT_freeCCtx(zc);
1473 ZSTD_freeDStream(zd);
1474 ZSTD_freeDStream(zd_noise);
1475 free(cNoiseBuffer[0]);
1476 free(cNoiseBuffer[1]);
1477 free(cNoiseBuffer[2]);
1478 free(cNoiseBuffer[3]);
1479 free(cNoiseBuffer[4]);
1490 /** If useOpaqueAPI, sets param in cctxParams.
1491 * Otherwise, sets the param in zc. */
1492 static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
1493 ZSTD_cParameter param, unsigned value,
1497 return ZSTD_CCtxParam_setParameter(cctxParams, param, value);
1499 return ZSTD_CCtx_setParameter(zc, param, value);
1503 /* Tests for ZSTD_compress_generic() API */
1504 static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI)
1506 U32 const maxSrcLog = bigTests ? 24 : 22;
1507 static const U32 maxSampleLog = 19;
1508 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1509 BYTE* cNoiseBuffer[5];
1510 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1511 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1512 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1513 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1514 size_t const dstBufferSize = srcBufferSize;
1515 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1518 U32 coreSeed = seed;
1519 ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */
1520 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1521 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
1522 UTIL_time_t const startClock = UTIL_getTime();
1523 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
1524 size_t dictSize = 0;
1526 U32 windowLogMalus = 0; /* can survive between 2 loops */
1527 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1528 U32 const nbThreadsMax = bigTests ? 4 : 2;
1529 ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
1532 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1533 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1534 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1535 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1536 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1537 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1538 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1539 "Not enough memory, fuzzer tests cancelled");
1541 /* Create initial samples */
1542 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1543 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1544 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1545 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1546 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1547 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1548 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
1550 /* catch up testNb */
1551 for (testNb=1; testNb < startTest; testNb++)
1552 FUZ_rand(&coreSeed);
1555 for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
1557 const BYTE* srcBuffer;
1558 size_t totalTestSize, totalGenSize, cSize;
1559 XXH64_state_t xxhState;
1561 U32 resetAllowed = 1;
1565 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
1566 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
1567 FUZ_rand(&coreSeed);
1568 lseed = coreSeed ^ prime32;
1569 DISPLAYLEVEL(5, " *** Test %u *** \n", testNb);
1571 /* states full reset (deliberately not synchronized) */
1572 /* some issues can only happen when reusing states */
1573 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1574 DISPLAYLEVEL(5, "Creating new context \n");
1576 zc = ZSTD_createCCtx();
1577 CHECK(zc==NULL, "ZSTD_createCCtx allocation error");
1580 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1581 ZSTD_freeDStream(zd);
1582 zd = ZSTD_createDStream();
1583 CHECK(zd==NULL, "ZSTD_createDStream allocation error");
1584 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1587 /* srcBuffer selection [0-4] */
1588 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1589 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1593 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1594 buffNb = tnb[buffNb >> 3];
1596 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1597 buffNb = tnb[buffNb >> 3];
1599 srcBuffer = cNoiseBuffer[buffNb];
1602 /* compression init */
1603 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/
1604 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1605 && oldTestLog /* at least one test happened */ && resetAllowed) {
1606 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1607 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1608 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
1609 CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) );
1612 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1613 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
1614 U32 const cLevelCandidate = (FUZ_rand(&lseed) %
1616 (MAX(testLog, dictLog) / 2))) +
1618 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
1619 DISPLAYLEVEL(5, "t%u: base cLevel : %u \n", testNb, cLevel);
1620 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1621 DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize);
1622 oldTestLog = testLog;
1623 /* random dictionary selection */
1624 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1625 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1626 dict = srcBuffer + dictStart;
1627 if (!dictSize) dict=NULL;
1629 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1630 ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
1631 static const U32 windowLogMax = 24;
1633 /* mess with compression parameters */
1634 cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
1635 cParams.windowLog = MIN(windowLogMax, cParams.windowLog);
1636 cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
1637 cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
1638 cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
1639 cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
1640 cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
1641 cParams = ZSTD_adjustCParams(cParams, 0, 0);
1643 if (FUZ_rand(&lseed) & 1) {
1644 DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog);
1645 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) );
1646 assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */
1647 windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5;
1649 if (FUZ_rand(&lseed) & 1) {
1650 DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog);
1651 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) );
1653 if (FUZ_rand(&lseed) & 1) {
1654 DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog);
1655 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) );
1657 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) );
1658 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) );
1659 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) );
1661 /* mess with long distance matching parameters */
1663 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) );
1664 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) );
1665 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) );
1666 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) );
1667 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) );
1670 /* mess with frame parameters */
1671 if (FUZ_rand(&lseed) & 1) {
1672 U32 const checksumFlag = FUZ_rand(&lseed) & 1;
1673 DISPLAYLEVEL(5, "t%u: frame checksum : %u \n", testNb, checksumFlag);
1674 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, checksumFlag, useOpaqueAPI) );
1676 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1677 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1678 if (FUZ_rand(&lseed) & 1) {
1679 DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize);
1680 CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
1683 /* multi-threading parameters */
1684 { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
1685 U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1;
1686 U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax);
1687 DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads);
1688 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbWorkers, nbThreads, useOpaqueAPI) );
1689 if (nbThreads > 1) {
1690 U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
1691 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) );
1692 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) );
1696 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1698 /* Apply parameters */
1700 DISPLAYLEVEL(6," t%u: applying CCtxParams \n", testNb);
1701 CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) );
1704 if (FUZ_rand(&lseed) & 1) {
1705 if (FUZ_rand(&lseed) & 1) {
1706 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) );
1708 CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) );
1710 if (dict && dictSize) {
1711 /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */
1713 size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams);
1714 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed");
1716 size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1);
1717 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed");
1721 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) );
1725 /* multi-segments compression test */
1726 XXH64_reset(&xxhState, 0);
1727 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1728 for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) {
1729 /* compress random chunks into randomly sized dst buffers */
1730 size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1731 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1732 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1733 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
1734 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1735 ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush;
1736 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1737 outBuff.size = outBuff.pos + dstBuffSize;
1739 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) );
1740 DISPLAYLEVEL(6, "t%u: compress consumed %u bytes (total : %u) ; flush: %u (total : %u) \n",
1741 testNb, (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos), (U32)flush, (U32)outBuff.pos);
1743 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1744 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1745 totalTestSize += inBuff.pos;
1748 /* final frame epilogue */
1749 { size_t remainingToFlush = 1;
1750 while (remainingToFlush) {
1751 ZSTD_inBuffer inBuff = { NULL, 0, 0 };
1752 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
1753 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1754 outBuff.size = outBuff.pos + adjustedDstSize;
1755 DISPLAYLEVEL(6, "t%u: End-flush into dst buffer of size %u \n", testNb, (U32)adjustedDstSize);
1756 remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
1757 DISPLAYLEVEL(6, "t%u: Total flushed so far : %u bytes \n", testNb, (U32)outBuff.pos);
1758 CHECK( ZSTD_isError(remainingToFlush),
1759 "ZSTD_compress_generic w/ ZSTD_e_end error : %s",
1760 ZSTD_getErrorName(remainingToFlush) );
1762 crcOrig = XXH64_digest(&xxhState);
1763 cSize = outBuff.pos;
1764 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
1767 /* multi - fragments decompression test */
1768 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1769 DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict);
1770 CHECK_Z( ZSTD_resetDStream(zd) );
1772 DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize);
1773 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
1775 { size_t decompressionResult = 1;
1776 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1777 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1778 for (totalGenSize = 0 ; decompressionResult ; ) {
1779 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1780 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1781 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1782 inBuff.size = inBuff.pos + readCSrcSize;
1783 outBuff.size = outBuff.pos + dstBuffSize;
1784 DISPLAYLEVEL(6, "decompression presented %u new bytes (pos:%u/%u)\n",
1785 (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize);
1786 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1787 DISPLAYLEVEL(6, "so far: consumed = %u, produced = %u \n",
1788 (U32)inBuff.pos, (U32)outBuff.pos);
1789 if (ZSTD_isError(decompressionResult)) {
1790 DISPLAY("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(decompressionResult));
1791 findDiff(copyBuffer, dstBuffer, totalTestSize);
1793 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
1794 CHECK (inBuff.pos > cSize, "ZSTD_decompressStream consumes too much input : %u > %u ", (U32)inBuff.pos, (U32)cSize);
1796 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1797 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1798 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1799 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1800 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1803 /*===== noisy/erroneous src decompression test =====*/
1805 /* add some noise */
1806 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1807 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1808 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1809 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1810 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1811 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1812 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1815 /* try decompression on noisy data */
1816 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
1817 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1818 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1819 while (outBuff.pos < dstBufferSize) {
1820 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1821 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1822 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1823 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1824 outBuff.size = outBuff.pos + adjustedDstSize;
1825 inBuff.size = inBuff.pos + adjustedCSrcSize;
1826 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1827 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
1828 /* Good so far, but no more progress possible */
1829 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1831 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
1835 ZSTD_freeDStream(zd);
1836 ZSTD_freeDStream(zd_noise);
1837 ZSTD_freeCCtxParams(cctxParams);
1838 free(cNoiseBuffer[0]);
1839 free(cNoiseBuffer[1]);
1840 free(cNoiseBuffer[2]);
1841 free(cNoiseBuffer[3]);
1842 free(cNoiseBuffer[4]);
1853 /*-*******************************************************
1855 *********************************************************/
1856 int FUZ_usage(const char* programName)
1858 DISPLAY( "Usage :\n");
1859 DISPLAY( " %s [args]\n", programName);
1861 DISPLAY( "Arguments :\n");
1862 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1863 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1864 DISPLAY( " -t# : Select starting test number (default:0)\n");
1865 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1866 DISPLAY( " -v : verbose\n");
1867 DISPLAY( " -p : pause at the end\n");
1868 DISPLAY( " -h : display help and exit\n");
1872 typedef enum { simple_api, mt_api, advanced_api } e_api;
1874 int main(int argc, const char** argv)
1878 int nbTests = nbTestsDefault;
1880 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
1883 int bigTests = (sizeof(size_t) == 8);
1884 e_api selected_api = simple_api;
1885 const char* const programName = argv[0];
1886 U32 useOpaqueAPI = 0;
1889 /* Check command line */
1890 for(argNb=1; argNb<argc; argNb++) {
1891 const char* argument = argv[argNb];
1892 assert(argument != NULL);
1894 /* Parsing commands. Aggregated commands are allowed */
1895 if (argument[0]=='-') {
1897 if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; }
1898 if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; }
1899 if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; testNb += !testNb; useOpaqueAPI = 1; continue; }
1900 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
1903 while (*argument!=0) {
1907 return FUZ_usage(programName);
1919 case 'p': /* pause at the end */
1924 case 'i': /* limit tests by nb of iterations (default) */
1926 nbTests=0; g_clockTime=0;
1927 while ((*argument>='0') && (*argument<='9')) {
1929 nbTests += *argument - '0';
1934 case 'T': /* limit tests by time */
1936 nbTests=0; g_clockTime=0;
1937 while ((*argument>='0') && (*argument<='9')) {
1939 g_clockTime += *argument - '0';
1942 if (*argument=='m') { /* -T1m == -T60 */
1943 g_clockTime *=60, argument++;
1944 if (*argument=='n') argument++; /* -T1mn == -T60 */
1945 } else if (*argument=='s') argument++; /* -T10s == -T10 */
1946 g_clockTime *= SEC_TO_MICRO;
1949 case 's': /* manually select seed */
1953 while ((*argument>='0') && (*argument<='9')) {
1955 seed += *argument - '0';
1960 case 't': /* select starting test number */
1963 while ((*argument>='0') && (*argument<='9')) {
1965 testNb += *argument - '0';
1970 case 'P': /* compressibility % */
1973 while ((*argument>='0') && (*argument<='9')) {
1975 proba += *argument - '0';
1978 if (proba<0) proba=0;
1979 if (proba>100) proba=100;
1983 return FUZ_usage(programName);
1985 } } } /* for(argNb=1; argNb<argc; argNb++) */
1988 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
1991 time_t const t = time(NULL);
1992 U32 const h = XXH32(&t, sizeof(t), 1);
1996 DISPLAY("Seed = %u\n", seed);
1997 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1999 if (nbTests<=0) nbTests=1;
2002 result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
2006 switch(selected_api)
2009 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
2012 result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
2015 result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI);
2018 assert(0); /* impossible */
2024 DISPLAY("Press Enter \n");