blob: 305363877b0cbe526521998c34efcc865c3cb54d [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Colletd7883a22016-08-12 16:48:02 +02009
Yann Colletd7883a22016-08-12 16:48:02 +020010
11/*-************************************
12* Compiler specific
13**************************************/
14#ifdef _MSC_VER /* Visual Studio */
15# define _CRT_SECURE_NO_WARNINGS /* fgets */
16# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
17# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
18#endif
19
20
21/*-************************************
22* Includes
23**************************************/
24#include <stdlib.h> /* free */
25#include <stdio.h> /* fgets, sscanf */
Yann Colletef9999f2016-09-01 16:44:48 -070026#include <time.h> /* clock_t, clock() */
Yann Colletd7883a22016-08-12 16:48:02 +020027#include <string.h> /* strcmp */
28#include "mem.h"
Yann Colletef9999f2016-09-01 16:44:48 -070029#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem */
Yann Colletd7883a22016-08-12 16:48:02 +020030#include "zstd.h" /* ZSTD_compressBound */
31#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070032#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020033#include "xxhash.h" /* XXH64_* */
34
35
36/*-************************************
37* Constants
38**************************************/
39#define KB *(1U<<10)
40#define MB *(1U<<20)
41#define GB *(1U<<30)
42
43static const U32 nbTestsDefault = 10000;
44#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
45#define FUZ_COMPRESSIBILITY_DEFAULT 50
46static const U32 prime1 = 2654435761U;
47static const U32 prime2 = 2246822519U;
48
49
Yann Colletd7883a22016-08-12 16:48:02 +020050/*-************************************
51* Display Macros
52**************************************/
53#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
54#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
55static U32 g_displayLevel = 2;
56
57#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070058 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
59 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Colletd7883a22016-08-12 16:48:02 +020060 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletef9999f2016-09-01 16:44:48 -070061static const clock_t g_refreshRate = CLOCKS_PER_SEC * 15 / 100;
62static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020063
Yann Colletef9999f2016-09-01 16:44:48 -070064static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020065
66
67/*-*******************************************************
68* Fuzzer functions
69*********************************************************/
70#define MAX(a,b) ((a)>(b)?(a):(b))
71
Yann Colletef9999f2016-09-01 16:44:48 -070072static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020073{
Yann Colletef9999f2016-09-01 16:44:48 -070074 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020075}
76
77/*! FUZ_rand() :
78 @return : a 27 bits random value, from a 32-bits `seed`.
79 `seed` is also modified */
80# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
81unsigned int FUZ_rand(unsigned int* seedPtr)
82{
83 U32 rand32 = *seedPtr;
84 rand32 *= prime1;
85 rand32 += prime2;
86 rand32 = FUZ_rotl32(rand32, 13);
87 *seedPtr = rand32;
88 return rand32 >> 5;
89}
90
Yann Colletd7883a22016-08-12 16:48:02 +020091/*
92static unsigned FUZ_highbit32(U32 v32)
93{
94 unsigned nbBits = 0;
95 if (v32==0) return 0;
96 for ( ; v32 ; v32>>=1) nbBits++;
97 return nbBits;
98}
99*/
100
101static void* allocFunction(void* opaque, size_t size)
102{
103 void* address = malloc(size);
104 (void)opaque;
105 return address;
106}
107
108static void freeFunction(void* opaque, void* address)
109{
110 (void)opaque;
111 free(address);
112}
113
Yann Colletcb327632016-08-23 00:30:31 +0200114
115/*======================================================
116* Basic Unit tests
117======================================================*/
118
Yann Colletd7883a22016-08-12 16:48:02 +0200119static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
120{
121 int testResult = 0;
122 size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
123 void* CNBuffer = malloc(CNBufferSize);
124 size_t const skippableFrameSize = 11;
125 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
126 void* compressedBuffer = malloc(compressedBufferSize);
127 size_t const decodedBufferSize = CNBufferSize;
128 void* decodedBuffer = malloc(decodedBufferSize);
129 size_t cSize;
130 U32 testNb=0;
131 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
132 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet53e17fb2016-08-17 01:39:22 +0200133 ZSTD_inBuffer inBuff;
134 ZSTD_outBuffer outBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200135
136 /* Create compressible test buffer */
137 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
138 DISPLAY("Not enough memory, aborting\n");
139 goto _output_error;
140 }
141 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
142
143 /* generate skippable frame */
144 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
145 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
146 cSize = skippableFrameSize + 8;
147
148 /* Basic compression test */
149 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
150 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200151 outBuff.dst = (char*)(compressedBuffer)+cSize;
152 outBuff.size = compressedBufferSize;
153 outBuff.pos = 0;
154 inBuff.src = CNBuffer;
155 inBuff.size = CNBufferSize;
156 inBuff.pos = 0;
157 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200158 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200159 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
160 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200161 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200162 cSize += outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200163 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
164
Yann Colletcb327632016-08-23 00:30:31 +0200165 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200166 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200167 if (ZSTD_isError(s)) goto _output_error;
168 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
169 }
170
Yann Colletd7883a22016-08-12 16:48:02 +0200171 /* skippable frame test */
172 DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
173 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200174 inBuff.src = compressedBuffer;
175 inBuff.size = cSize;
176 inBuff.pos = 0;
177 outBuff.dst = decodedBuffer;
178 outBuff.size = CNBufferSize;
179 outBuff.pos = 0;
180 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200181 if (r != 0) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200182 if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */
Yann Colletd7883a22016-08-12 16:48:02 +0200183 DISPLAYLEVEL(4, "OK \n");
184
185 /* Basic decompression test */
186 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
187 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet17e482e2016-08-23 16:58:10 +0200188 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */
189 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200190 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200191 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200192 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
193 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200194 DISPLAYLEVEL(4, "OK \n");
195
196 /* check regenerated data is byte exact */
197 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
198 { size_t i;
199 for (i=0; i<CNBufferSize; i++) {
200 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
201 } }
202 DISPLAYLEVEL(4, "OK \n");
203
Yann Colletcb327632016-08-23 00:30:31 +0200204 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200205 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200206 if (ZSTD_isError(s)) goto _output_error;
207 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
208 }
209
Yann Colletd7883a22016-08-12 16:48:02 +0200210 /* Byte-by-byte decompression test */
211 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
212 { size_t r = 1;
213 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200214 inBuff.src = compressedBuffer;
215 outBuff.dst = decodedBuffer;
216 inBuff.pos = 0;
217 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200218 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200219 inBuff.size = inBuff.pos + 1;
220 outBuff.size = outBuff.pos + 1;
221 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200222 if (ZSTD_isError(r)) goto _output_error;
223 }
224 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
225 r=1;
226 while (r) { /* normal frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200227 inBuff.size = inBuff.pos + 1;
228 outBuff.size = outBuff.pos + 1;
229 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200230 if (ZSTD_isError(r)) goto _output_error;
231 }
232 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200233 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
234 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200235 DISPLAYLEVEL(4, "OK \n");
236
237 /* check regenerated data is byte exact */
238 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
239 { size_t i;
240 for (i=0; i<CNBufferSize; i++) {
241 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
242 } }
243 DISPLAYLEVEL(4, "OK \n");
244
Yann Collet12083a42016-09-06 15:01:51 +0200245 /* Complex context re-use scenario */
246 DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++);
247 ZSTD_freeCStream(zc);
248 zc = ZSTD_createCStream_advanced(customMem);
249 if (zc==NULL) goto _output_error; /* memory allocation issue */
250 /* use 1 */
251 { size_t const inSize = 513;
252 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
253 inBuff.src = CNBuffer;
254 inBuff.size = inSize;
255 inBuff.pos = 0;
256 outBuff.dst = (char*)(compressedBuffer)+cSize;
257 outBuff.size = ZSTD_compressBound(inSize);
258 outBuff.pos = 0;
259 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
260 if (ZSTD_isError(r)) goto _output_error; }
261 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
262 { size_t const r = ZSTD_endStream(zc, &outBuff);
263 if (r != 0) goto _output_error; } /* error, or some data not flushed */
264 }
265 /* use 2 */
266 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
267 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
268 inBuff.src = CNBuffer;
269 inBuff.size = inSize;
270 inBuff.pos = 0;
271 outBuff.dst = (char*)(compressedBuffer)+cSize;
272 outBuff.size = ZSTD_compressBound(inSize);
273 outBuff.pos = 0;
274 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
275 if (ZSTD_isError(r)) goto _output_error; }
276 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
277 { size_t const r = ZSTD_endStream(zc, &outBuff);
278 if (r != 0) goto _output_error; } /* error, or some data not flushed */
279 }
280 DISPLAYLEVEL(4, "OK \n");
281
282 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
283 { size_t const s = ZSTD_sizeof_CStream(zc);
284 if (ZSTD_isError(s)) goto _output_error;
285 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
286 }
287
288 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200289 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
290 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
291 if (!ZSTD_isError(r)) goto _output_error; }
292 DISPLAYLEVEL(4, "OK \n");
293
294 /* Memory restriction */
295 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
296 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
297 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
298 if (ZSTD_isError(r)) goto _output_error; }
299 inBuff.src = compressedBuffer;
300 inBuff.size = cSize;
301 inBuff.pos = 0;
302 outBuff.dst = decodedBuffer;
303 outBuff.size = CNBufferSize;
304 outBuff.pos = 0;
305 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
306 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
307 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
308
309
Yann Colletd7883a22016-08-12 16:48:02 +0200310_end:
311 ZSTD_freeCStream(zc);
312 ZSTD_freeDStream(zd);
313 free(CNBuffer);
314 free(compressedBuffer);
315 free(decodedBuffer);
316 return testResult;
317
318_output_error:
319 testResult = 1;
320 DISPLAY("Error detected in Unit tests ! \n");
321 goto _end;
322}
323
324
325static size_t findDiff(const void* buf1, const void* buf2, size_t max)
326{
327 const BYTE* b1 = (const BYTE*)buf1;
328 const BYTE* b2 = (const BYTE*)buf2;
329 size_t u;
330 for (u=0; u<max; u++) {
331 if (b1[u] != b2[u]) break;
332 }
333 return u;
334}
335
336static size_t FUZ_rLogLength(U32* seed, U32 logLength)
337{
338 size_t const lengthMask = ((size_t)1 << logLength) - 1;
339 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
340}
341
342static size_t FUZ_randomLength(U32* seed, U32 maxLog)
343{
344 U32 const logLength = FUZ_rand(seed) % maxLog;
345 return FUZ_rLogLength(seed, logLength);
346}
347
348#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
349
350#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
351 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
352
353static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
354{
355 static const U32 maxSrcLog = 24;
356 static const U32 maxSampleLog = 19;
357 BYTE* cNoiseBuffer[5];
358 size_t srcBufferSize = (size_t)1<<maxSrcLog;
359 BYTE* copyBuffer;
360 size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog);
361 BYTE* cBuffer;
362 size_t cBufferSize = ZSTD_compressBound(srcBufferSize);
363 BYTE* dstBuffer;
364 size_t dstBufferSize = srcBufferSize;
365 U32 result = 0;
366 U32 testNb = 0;
367 U32 coreSeed = seed;
368 ZSTD_CStream* zc;
369 ZSTD_DStream* zd;
Yann Colletef9999f2016-09-01 16:44:48 -0700370 clock_t startClock = clock();
Yann Colletd7883a22016-08-12 16:48:02 +0200371
372 /* allocations */
373 zc = ZSTD_createCStream();
374 zd = ZSTD_createDStream();
375 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
376 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
377 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
378 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
379 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
380 copyBuffer= (BYTE*)malloc (copyBufferSize);
381 dstBuffer = (BYTE*)malloc (dstBufferSize);
382 cBuffer = (BYTE*)malloc (cBufferSize);
383 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
384 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd,
385 "Not enough memory, fuzzer tests cancelled");
386
387 /* Create initial samples */
388 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
389 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
390 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
391 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
392 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
393 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
394
395 /* catch up testNb */
396 for (testNb=1; testNb < startTest; testNb++)
397 FUZ_rand(&coreSeed);
398
399 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700400 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200401 U32 lseed;
402 const BYTE* srcBuffer;
403 const BYTE* dict;
404 size_t maxTestSize, dictSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200405 size_t cSize, totalTestSize, totalGenSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200406 U32 n, nbChunks;
407 XXH64_state_t xxhState;
408 U64 crcOrig;
409
410 /* init */
411 DISPLAYUPDATE(2, "\r%6u", testNb);
412 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
413 FUZ_rand(&coreSeed);
414 lseed = coreSeed ^ prime1;
415
416 /* states full reset (unsynchronized) */
417 /* some issues only happen when reusing states in a specific sequence of parameters */
418 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); }
419 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); }
420
421 /* srcBuffer selection [0-4] */
422 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
423 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
424 else {
425 buffNb >>= 3;
426 if (buffNb & 7) {
427 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
428 buffNb = tnb[buffNb >> 3];
429 } else {
430 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
431 buffNb = tnb[buffNb >> 3];
432 } }
433 srcBuffer = cNoiseBuffer[buffNb];
434 }
435
436 /* compression init */
437 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
438 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
439 maxTestSize = FUZ_rLogLength(&lseed, testLog);
440 dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
441 /* random dictionary selection */
442 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
443 dict = srcBuffer + dictStart;
444 }
445 { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
446 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
447 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
448 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0);
449 CHECK (ZSTD_isError(initError),"init error : %s", ZSTD_getErrorName(initError));
450 } } }
451
452 /* multi-segments compression test */
453 XXH64_reset(&xxhState, 0);
454 nbChunks = (FUZ_rand(&lseed) & 127) + 2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200455 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Colletd7883a22016-08-12 16:48:02 +0200456 for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
457 /* compress random chunk into random size dst buffer */
Yann Collet53e17fb2016-08-17 01:39:22 +0200458 { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200459 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
460 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
461 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200462 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 };
463 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200464
Yann Collet53e17fb2016-08-17 01:39:22 +0200465 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200466 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200467
Yann Collet53e17fb2016-08-17 01:39:22 +0200468 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
469 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
470 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200471 }
472
473 /* random flush operation, to mess around */
474 if ((FUZ_rand(&lseed) & 15) == 0) {
475 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200476 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
477 outBuff.size = outBuff.pos + adjustedDstSize;
478 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200479 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
480 } } }
481
482 /* final frame epilogue */
483 { size_t remainingToFlush = (size_t)(-1);
484 while (remainingToFlush) {
485 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
486 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
487 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200488 outBuff.size = outBuff.pos + adjustedDstSize;
489 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200490 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
491 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
492 } }
493 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200494 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200495 }
496
497 /* multi - fragments decompression test */
498 ZSTD_initDStream_usingDict(zd, dict, dictSize);
499 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200500 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
501 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
502 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200503 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
504 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
505 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200506 inBuff.size = inBuff.pos + readCSrcSize;
507 outBuff.size = inBuff.pos + dstBuffSize;
508 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200509 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200510 }
511 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200512 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
513 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200514 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
515 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
516 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
517 } }
518
519 /*===== noisy/erroneous src decompression test =====*/
520
521 /* add some noise */
522 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
523 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
524 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
525 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
526 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
527 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
528 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
529 } }
530
531 /* try decompression on noisy data */
532 ZSTD_initDStream(zd);
Yann Collet53e17fb2016-08-17 01:39:22 +0200533 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
534 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
535 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200536 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
537 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200538 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
539 outBuff.size = outBuff.pos + adjustedDstSize;
540 inBuff.size = inBuff.pos + randomCSrcSize;
541 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200542 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
543 } } } }
544 DISPLAY("\r%u fuzzer tests completed \n", testNb);
545
546_cleanup:
547 ZSTD_freeCStream(zc);
548 ZSTD_freeDStream(zd);
549 free(cNoiseBuffer[0]);
550 free(cNoiseBuffer[1]);
551 free(cNoiseBuffer[2]);
552 free(cNoiseBuffer[3]);
553 free(cNoiseBuffer[4]);
554 free(copyBuffer);
555 free(cBuffer);
556 free(dstBuffer);
557 return result;
558
559_output_error:
560 result = 1;
561 goto _cleanup;
562}
563
564
565/*-*******************************************************
566* Command line
567*********************************************************/
568int FUZ_usage(const char* programName)
569{
570 DISPLAY( "Usage :\n");
571 DISPLAY( " %s [args]\n", programName);
572 DISPLAY( "\n");
573 DISPLAY( "Arguments :\n");
574 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
575 DISPLAY( " -s# : Select seed (default:prompt user)\n");
576 DISPLAY( " -t# : Select starting test number (default:0)\n");
577 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
578 DISPLAY( " -v : verbose\n");
579 DISPLAY( " -p : pause at the end\n");
580 DISPLAY( " -h : display help and exit\n");
581 return 0;
582}
583
584
585int main(int argc, const char** argv)
586{
587 U32 seed=0;
588 int seedset=0;
589 int argNb;
590 int nbTests = nbTestsDefault;
591 int testNb = 0;
592 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
593 int result=0;
594 U32 mainPause = 0;
595 const char* programName = argv[0];
596 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
597 ZSTD_customMem customNULL = { NULL, NULL, NULL };
598
599 /* Check command line */
600 for(argNb=1; argNb<argc; argNb++) {
601 const char* argument = argv[argNb];
602 if(!argument) continue; /* Protection if argument empty */
603
604 /* Parsing commands. Aggregated commands are allowed */
605 if (argument[0]=='-') {
606 argument++;
607
608 while (*argument!=0) {
609 switch(*argument)
610 {
611 case 'h':
612 return FUZ_usage(programName);
613 case 'v':
614 argument++;
615 g_displayLevel=4;
616 break;
617 case 'q':
618 argument++;
619 g_displayLevel--;
620 break;
621 case 'p': /* pause at the end */
622 argument++;
623 mainPause = 1;
624 break;
625
626 case 'i':
627 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700628 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200629 while ((*argument>='0') && (*argument<='9')) {
630 nbTests *= 10;
631 nbTests += *argument - '0';
632 argument++;
633 }
634 break;
635
636 case 'T':
637 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700638 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200639 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700640 g_clockTime *= 10;
641 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200642 argument++;
643 }
Yann Colletef9999f2016-09-01 16:44:48 -0700644 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200645 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700646 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200647 break;
648
649 case 's':
650 argument++;
651 seed=0;
652 seedset=1;
653 while ((*argument>='0') && (*argument<='9')) {
654 seed *= 10;
655 seed += *argument - '0';
656 argument++;
657 }
658 break;
659
660 case 't':
661 argument++;
662 testNb=0;
663 while ((*argument>='0') && (*argument<='9')) {
664 testNb *= 10;
665 testNb += *argument - '0';
666 argument++;
667 }
668 break;
669
670 case 'P': /* compressibility % */
671 argument++;
672 proba=0;
673 while ((*argument>='0') && (*argument<='9')) {
674 proba *= 10;
675 proba += *argument - '0';
676 argument++;
677 }
678 if (proba<0) proba=0;
679 if (proba>100) proba=100;
680 break;
681
682 default:
683 return FUZ_usage(programName);
684 }
685 } } } /* for(argNb=1; argNb<argc; argNb++) */
686
687 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200688 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200689
Yann Colletef9999f2016-09-01 16:44:48 -0700690 if (!seedset) {
691 time_t const t = time(NULL);
692 U32 const h = XXH32(&t, sizeof(t), 1);
693 seed = h % 10000;
694 }
695
Yann Colletd7883a22016-08-12 16:48:02 +0200696 DISPLAY("Seed = %u\n", seed);
697 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
698
699 if (nbTests<=0) nbTests=1;
700
701 if (testNb==0) {
702 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
703 if (!result) {
704 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
705 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
706 } }
707
708 if (!result)
709 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
710
711 if (mainPause) {
712 int unused;
713 DISPLAY("Press Enter \n");
714 unused = getchar();
715 (void)unused;
716 }
717 return result;
718}