blob: aa119e636a1df636bc986e3a4f2ba684a9b342e5 [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 Colletb3060f72016-09-09 16:44:16 +020061static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070062static 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 */
Yann Collet95162342016-10-25 16:19:52 -070080#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020081unsigned 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{
Yann Colletb3060f72016-09-09 16:44:16 +0200121 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200122 void* CNBuffer = malloc(CNBufferSize);
123 size_t const skippableFrameSize = 11;
124 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
125 void* compressedBuffer = malloc(compressedBufferSize);
126 size_t const decodedBufferSize = CNBufferSize;
127 void* decodedBuffer = malloc(decodedBufferSize);
128 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200129 int testResult = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200130 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++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200212 { /* skippable frame */
213 size_t r = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200214 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200215 inBuff.src = compressedBuffer;
216 outBuff.dst = decodedBuffer;
217 inBuff.pos = 0;
218 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200219 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200220 inBuff.size = inBuff.pos + 1;
221 outBuff.size = outBuff.pos + 1;
222 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200223 if (ZSTD_isError(r)) goto _output_error;
224 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200225 /* normal frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200226 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
227 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200228 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200229 inBuff.size = inBuff.pos + 1;
230 outBuff.size = outBuff.pos + 1;
231 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200232 if (ZSTD_isError(r)) goto _output_error;
233 }
234 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200235 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
236 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200237 DISPLAYLEVEL(4, "OK \n");
238
239 /* check regenerated data is byte exact */
240 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
241 { size_t i;
242 for (i=0; i<CNBufferSize; i++) {
243 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
244 } }
245 DISPLAYLEVEL(4, "OK \n");
246
Yann Collet12083a42016-09-06 15:01:51 +0200247 /* Complex context re-use scenario */
248 DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++);
249 ZSTD_freeCStream(zc);
250 zc = ZSTD_createCStream_advanced(customMem);
251 if (zc==NULL) goto _output_error; /* memory allocation issue */
252 /* use 1 */
253 { size_t const inSize = 513;
254 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
255 inBuff.src = CNBuffer;
256 inBuff.size = inSize;
257 inBuff.pos = 0;
258 outBuff.dst = (char*)(compressedBuffer)+cSize;
259 outBuff.size = ZSTD_compressBound(inSize);
260 outBuff.pos = 0;
261 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
262 if (ZSTD_isError(r)) goto _output_error; }
263 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
264 { size_t const r = ZSTD_endStream(zc, &outBuff);
265 if (r != 0) goto _output_error; } /* error, or some data not flushed */
266 }
267 /* use 2 */
268 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
269 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
270 inBuff.src = CNBuffer;
271 inBuff.size = inSize;
272 inBuff.pos = 0;
273 outBuff.dst = (char*)(compressedBuffer)+cSize;
274 outBuff.size = ZSTD_compressBound(inSize);
275 outBuff.pos = 0;
276 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
277 if (ZSTD_isError(r)) goto _output_error; }
278 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
279 { size_t const r = ZSTD_endStream(zc, &outBuff);
280 if (r != 0) goto _output_error; } /* error, or some data not flushed */
281 }
282 DISPLAYLEVEL(4, "OK \n");
283
Yann Collet95162342016-10-25 16:19:52 -0700284 /* CDict scenario */
285 DISPLAYLEVEL(4, "test%3i : digested dictionary : ", testNb++);
286 { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 128 KB, 1);
287 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
288 if (ZSTD_isError(initError)) goto _output_error;
289 cSize = 0;
290 outBuff.dst = compressedBuffer;
291 outBuff.size = compressedBufferSize;
292 outBuff.pos = 0;
293 inBuff.src = CNBuffer;
294 inBuff.size = CNBufferSize;
295 inBuff.pos = 0;
296 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
297 if (ZSTD_isError(r)) goto _output_error; }
298 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
299 { size_t const r = ZSTD_endStream(zc, &outBuff);
300 if (r != 0) goto _output_error; } /* error, or some data not flushed */
301 cSize = outBuff.pos;
302 ZSTD_freeCDict(cdict);
303 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
304 }
305
Yann Collet12083a42016-09-06 15:01:51 +0200306 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
307 { size_t const s = ZSTD_sizeof_CStream(zc);
308 if (ZSTD_isError(s)) goto _output_error;
309 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
310 }
311
Yann Collet335ad5d2016-10-25 17:47:02 -0700312 /* DDict scenario */
313 DISPLAYLEVEL(4, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
314 { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, 128 KB);
315 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
316 if (ZSTD_isError(initError)) goto _output_error;
317 inBuff.src = compressedBuffer;
318 inBuff.size = cSize;
319 inBuff.pos = 0;
320 outBuff.dst = decodedBuffer;
321 outBuff.size = CNBufferSize;
322 outBuff.pos = 0;
323 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
324 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
325 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
326 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
327 ZSTD_freeDDict(ddict);
328 DISPLAYLEVEL(4, "OK \n");
329 }
330
Yann Collet12083a42016-09-06 15:01:51 +0200331 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200332 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
333 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
334 if (!ZSTD_isError(r)) goto _output_error; }
335 DISPLAYLEVEL(4, "OK \n");
336
337 /* Memory restriction */
338 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
339 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
340 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
341 if (ZSTD_isError(r)) goto _output_error; }
342 inBuff.src = compressedBuffer;
343 inBuff.size = cSize;
344 inBuff.pos = 0;
345 outBuff.dst = decodedBuffer;
346 outBuff.size = CNBufferSize;
347 outBuff.pos = 0;
348 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
349 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
350 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
351
352
Yann Colletd7883a22016-08-12 16:48:02 +0200353_end:
354 ZSTD_freeCStream(zc);
355 ZSTD_freeDStream(zd);
356 free(CNBuffer);
357 free(compressedBuffer);
358 free(decodedBuffer);
359 return testResult;
360
361_output_error:
362 testResult = 1;
363 DISPLAY("Error detected in Unit tests ! \n");
364 goto _end;
365}
366
367
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200368/* ====== Fuzzer tests ====== */
369
Yann Colletd7883a22016-08-12 16:48:02 +0200370static size_t findDiff(const void* buf1, const void* buf2, size_t max)
371{
372 const BYTE* b1 = (const BYTE*)buf1;
373 const BYTE* b2 = (const BYTE*)buf2;
374 size_t u;
375 for (u=0; u<max; u++) {
376 if (b1[u] != b2[u]) break;
377 }
378 return u;
379}
380
381static size_t FUZ_rLogLength(U32* seed, U32 logLength)
382{
383 size_t const lengthMask = ((size_t)1 << logLength) - 1;
384 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
385}
386
387static size_t FUZ_randomLength(U32* seed, U32 maxLog)
388{
389 U32 const logLength = FUZ_rand(seed) % maxLog;
390 return FUZ_rLogLength(seed, logLength);
391}
392
393#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
394
395#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
396 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
397
398static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
399{
400 static const U32 maxSrcLog = 24;
401 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200402 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200403 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200404 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
405 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
406 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
407 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
408 size_t const dstBufferSize = srcBufferSize;
409 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200410 U32 result = 0;
411 U32 testNb = 0;
412 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200413 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
414 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
415 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
416 clock_t const startClock = clock();
417 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200418 size_t dictSize = 0;
419 U32 oldTestLog = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200420
421 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200422 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
423 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
424 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
425 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
426 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200427 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200428 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200429 "Not enough memory, fuzzer tests cancelled");
430
431 /* Create initial samples */
432 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
433 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
434 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
435 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
436 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
437 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200438 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200439
440 /* catch up testNb */
441 for (testNb=1; testNb < startTest; testNb++)
442 FUZ_rand(&coreSeed);
443
444 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700445 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200446 U32 lseed;
447 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200448 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200449 XXH64_state_t xxhState;
450 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200451 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200452 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200453
454 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700455 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
456 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200457 FUZ_rand(&coreSeed);
458 lseed = coreSeed ^ prime1;
459
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200460 /* states full reset (deliberately not synchronized) */
461 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200462 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
463 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ }
Yann Colletd7883a22016-08-12 16:48:02 +0200464
465 /* srcBuffer selection [0-4] */
466 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
467 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
468 else {
469 buffNb >>= 3;
470 if (buffNb & 7) {
471 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
472 buffNb = tnb[buffNb >> 3];
473 } else {
474 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
475 buffNb = tnb[buffNb >> 3];
476 } }
477 srcBuffer = cNoiseBuffer[buffNb];
478 }
479
480 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200481 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
482 && oldTestLog /* at least one test happened */ && resetAllowed) {
483 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
484 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
485 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
486 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
487 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
488 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200489 } else {
490 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200491 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
492 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200493 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200494 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200495 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200496 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
497 dict = srcBuffer + dictStart;
498 }
Yann Colletcf409a72016-09-26 16:41:05 +0200499 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
500 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200501 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
502 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200503 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200504 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200505 } } }
506
507 /* multi-segments compression test */
508 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200509 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200510 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200511 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Colletd7883a22016-08-12 16:48:02 +0200512 /* compress random chunk into random size dst buffer */
Yann Collet2f263942016-09-26 14:06:08 +0200513 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
514 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
515 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200516 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
517 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200518 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200519 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200520
Yann Collet53e17fb2016-08-17 01:39:22 +0200521 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200522 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200523
Yann Collet53e17fb2016-08-17 01:39:22 +0200524 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
525 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
526 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200527 }
528
529 /* random flush operation, to mess around */
530 if ((FUZ_rand(&lseed) & 15) == 0) {
531 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200532 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
533 outBuff.size = outBuff.pos + adjustedDstSize;
534 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200535 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
536 } } }
537
538 /* final frame epilogue */
539 { size_t remainingToFlush = (size_t)(-1);
540 while (remainingToFlush) {
541 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
542 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
543 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200544 outBuff.size = outBuff.pos + adjustedDstSize;
545 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200546 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
547 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
548 } }
549 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200550 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200551 }
552
553 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200554 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700555 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet58d5dfe2016-09-25 01:34:03 +0200556 } else
557 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200558 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200559 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
560 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
561 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200562 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
563 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
564 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200565 inBuff.size = inBuff.pos + readCSrcSize;
566 outBuff.size = inBuff.pos + dstBuffSize;
567 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200568 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200569 }
570 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200571 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
572 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200573 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
574 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
575 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
576 } }
577
578 /*===== noisy/erroneous src decompression test =====*/
579
580 /* add some noise */
581 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
582 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
583 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
584 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
585 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
586 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
587 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
588 } }
589
590 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200591 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200592 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
593 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
594 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200595 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
596 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200597 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
598 outBuff.size = outBuff.pos + adjustedDstSize;
599 inBuff.size = inBuff.pos + randomCSrcSize;
600 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200601 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
602 } } } }
603 DISPLAY("\r%u fuzzer tests completed \n", testNb);
604
605_cleanup:
606 ZSTD_freeCStream(zc);
607 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200608 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200609 free(cNoiseBuffer[0]);
610 free(cNoiseBuffer[1]);
611 free(cNoiseBuffer[2]);
612 free(cNoiseBuffer[3]);
613 free(cNoiseBuffer[4]);
614 free(copyBuffer);
615 free(cBuffer);
616 free(dstBuffer);
617 return result;
618
619_output_error:
620 result = 1;
621 goto _cleanup;
622}
623
624
625/*-*******************************************************
626* Command line
627*********************************************************/
628int FUZ_usage(const char* programName)
629{
630 DISPLAY( "Usage :\n");
631 DISPLAY( " %s [args]\n", programName);
632 DISPLAY( "\n");
633 DISPLAY( "Arguments :\n");
634 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
635 DISPLAY( " -s# : Select seed (default:prompt user)\n");
636 DISPLAY( " -t# : Select starting test number (default:0)\n");
637 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
638 DISPLAY( " -v : verbose\n");
639 DISPLAY( " -p : pause at the end\n");
640 DISPLAY( " -h : display help and exit\n");
641 return 0;
642}
643
644
645int main(int argc, const char** argv)
646{
647 U32 seed=0;
648 int seedset=0;
649 int argNb;
650 int nbTests = nbTestsDefault;
651 int testNb = 0;
652 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
653 int result=0;
654 U32 mainPause = 0;
655 const char* programName = argv[0];
656 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
657 ZSTD_customMem customNULL = { NULL, NULL, NULL };
658
659 /* Check command line */
660 for(argNb=1; argNb<argc; argNb++) {
661 const char* argument = argv[argNb];
662 if(!argument) continue; /* Protection if argument empty */
663
664 /* Parsing commands. Aggregated commands are allowed */
665 if (argument[0]=='-') {
666 argument++;
667
668 while (*argument!=0) {
669 switch(*argument)
670 {
671 case 'h':
672 return FUZ_usage(programName);
673 case 'v':
674 argument++;
675 g_displayLevel=4;
676 break;
677 case 'q':
678 argument++;
679 g_displayLevel--;
680 break;
681 case 'p': /* pause at the end */
682 argument++;
683 mainPause = 1;
684 break;
685
686 case 'i':
687 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700688 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200689 while ((*argument>='0') && (*argument<='9')) {
690 nbTests *= 10;
691 nbTests += *argument - '0';
692 argument++;
693 }
694 break;
695
696 case 'T':
697 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700698 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200699 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700700 g_clockTime *= 10;
701 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200702 argument++;
703 }
Yann Colletef9999f2016-09-01 16:44:48 -0700704 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200705 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700706 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200707 break;
708
709 case 's':
710 argument++;
711 seed=0;
712 seedset=1;
713 while ((*argument>='0') && (*argument<='9')) {
714 seed *= 10;
715 seed += *argument - '0';
716 argument++;
717 }
718 break;
719
720 case 't':
721 argument++;
722 testNb=0;
723 while ((*argument>='0') && (*argument<='9')) {
724 testNb *= 10;
725 testNb += *argument - '0';
726 argument++;
727 }
728 break;
729
730 case 'P': /* compressibility % */
731 argument++;
732 proba=0;
733 while ((*argument>='0') && (*argument<='9')) {
734 proba *= 10;
735 proba += *argument - '0';
736 argument++;
737 }
738 if (proba<0) proba=0;
739 if (proba>100) proba=100;
740 break;
741
742 default:
743 return FUZ_usage(programName);
744 }
745 } } } /* for(argNb=1; argNb<argc; argNb++) */
746
747 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200748 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200749
Yann Colletef9999f2016-09-01 16:44:48 -0700750 if (!seedset) {
751 time_t const t = time(NULL);
752 U32 const h = XXH32(&t, sizeof(t), 1);
753 seed = h % 10000;
754 }
755
Yann Colletd7883a22016-08-12 16:48:02 +0200756 DISPLAY("Seed = %u\n", seed);
757 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
758
759 if (nbTests<=0) nbTests=1;
760
761 if (testNb==0) {
762 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
763 if (!result) {
764 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
765 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
766 } }
767
768 if (!result)
769 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
770
771 if (mainPause) {
772 int unused;
773 DISPLAY("Press Enter \n");
774 unused = getchar();
775 (void)unused;
776 }
777 return result;
778}