blob: 085de81394da2ae39fbe4a952d98635ee7f25c25 [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 */
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{
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
284 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
285 { size_t const s = ZSTD_sizeof_CStream(zc);
286 if (ZSTD_isError(s)) goto _output_error;
287 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
288 }
289
290 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200291 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
292 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
293 if (!ZSTD_isError(r)) goto _output_error; }
294 DISPLAYLEVEL(4, "OK \n");
295
296 /* Memory restriction */
297 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
298 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
299 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
300 if (ZSTD_isError(r)) goto _output_error; }
301 inBuff.src = compressedBuffer;
302 inBuff.size = cSize;
303 inBuff.pos = 0;
304 outBuff.dst = decodedBuffer;
305 outBuff.size = CNBufferSize;
306 outBuff.pos = 0;
307 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
308 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
309 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
310
311
Yann Colletd7883a22016-08-12 16:48:02 +0200312_end:
313 ZSTD_freeCStream(zc);
314 ZSTD_freeDStream(zd);
315 free(CNBuffer);
316 free(compressedBuffer);
317 free(decodedBuffer);
318 return testResult;
319
320_output_error:
321 testResult = 1;
322 DISPLAY("Error detected in Unit tests ! \n");
323 goto _end;
324}
325
326
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200327/* ====== Fuzzer tests ====== */
328
Yann Colletd7883a22016-08-12 16:48:02 +0200329static size_t findDiff(const void* buf1, const void* buf2, size_t max)
330{
331 const BYTE* b1 = (const BYTE*)buf1;
332 const BYTE* b2 = (const BYTE*)buf2;
333 size_t u;
334 for (u=0; u<max; u++) {
335 if (b1[u] != b2[u]) break;
336 }
337 return u;
338}
339
340static size_t FUZ_rLogLength(U32* seed, U32 logLength)
341{
342 size_t const lengthMask = ((size_t)1 << logLength) - 1;
343 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
344}
345
346static size_t FUZ_randomLength(U32* seed, U32 maxLog)
347{
348 U32 const logLength = FUZ_rand(seed) % maxLog;
349 return FUZ_rLogLength(seed, logLength);
350}
351
352#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
353
354#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
355 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
356
357static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
358{
359 static const U32 maxSrcLog = 24;
360 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200361 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200362 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200363 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
364 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
365 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
366 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
367 size_t const dstBufferSize = srcBufferSize;
368 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200369 U32 result = 0;
370 U32 testNb = 0;
371 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200372 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
373 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
374 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
375 clock_t const startClock = clock();
376 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
377 size_t dictSize=0, maxTestSize=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200378
379 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200380 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
381 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
382 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
383 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
384 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200385 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200386 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200387 "Not enough memory, fuzzer tests cancelled");
388
389 /* Create initial samples */
390 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
391 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
392 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
393 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
394 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
395 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200396 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200397
398 /* catch up testNb */
399 for (testNb=1; testNb < startTest; testNb++)
400 FUZ_rand(&coreSeed);
401
402 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700403 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200404 U32 lseed;
405 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200406 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200407 XXH64_state_t xxhState;
408 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200409 U32 resetAllowed = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200410
411 /* init */
412 DISPLAYUPDATE(2, "\r%6u", testNb);
413 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
414 FUZ_rand(&coreSeed);
415 lseed = coreSeed ^ prime1;
416
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200417 /* states full reset (deliberately not synchronized) */
418 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200419 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
420 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 +0200421
422 /* srcBuffer selection [0-4] */
423 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
424 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
425 else {
426 buffNb >>= 3;
427 if (buffNb & 7) {
428 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
429 buffNb = tnb[buffNb >> 3];
430 } else {
431 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
432 buffNb = tnb[buffNb >> 3];
433 } }
434 srcBuffer = cNoiseBuffer[buffNb];
435 }
436
437 /* compression init */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200438 if (maxTestSize /* at least one test happened */ && resetAllowed && (FUZ_rand(&lseed)&1)) {
439 ZSTD_resetCStream(zc, 0);
440 } else {
441 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200442 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
443 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200444 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200445 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200446 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
447 dict = srcBuffer + dictStart;
448 }
449 { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
450 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
451 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
452 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0);
Yann Colletb3060f72016-09-09 16:44:16 +0200453 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200454 } } }
455
456 /* multi-segments compression test */
457 XXH64_reset(&xxhState, 0);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200458 { U32 const maxNbChunks = (FUZ_rand(&lseed) & 127) + 2;
459 ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
460 U32 n;
461 for (n=0, cSize=0, totalTestSize=0 ; (n<maxNbChunks) && (totalTestSize < maxTestSize) ; n++) {
Yann Colletd7883a22016-08-12 16:48:02 +0200462 /* compress random chunk into random size dst buffer */
Yann Collet53e17fb2016-08-17 01:39:22 +0200463 { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200464 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
465 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
466 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200467 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 };
468 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200469
Yann Collet53e17fb2016-08-17 01:39:22 +0200470 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200471 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200472
Yann Collet53e17fb2016-08-17 01:39:22 +0200473 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
474 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
475 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200476 }
477
478 /* random flush operation, to mess around */
479 if ((FUZ_rand(&lseed) & 15) == 0) {
480 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200481 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
482 outBuff.size = outBuff.pos + adjustedDstSize;
483 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200484 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
485 } } }
486
487 /* final frame epilogue */
488 { size_t remainingToFlush = (size_t)(-1);
489 while (remainingToFlush) {
490 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
491 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
492 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200493 outBuff.size = outBuff.pos + adjustedDstSize;
494 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200495 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
496 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
497 } }
498 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200499 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200500 }
501
502 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200503 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
504 CHECK( ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
505 } else
506 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200507 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200508 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
509 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
510 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200511 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
512 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
513 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200514 inBuff.size = inBuff.pos + readCSrcSize;
515 outBuff.size = inBuff.pos + dstBuffSize;
516 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200517 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200518 }
519 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200520 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
521 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200522 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
523 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
524 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
525 } }
526
527 /*===== noisy/erroneous src decompression test =====*/
528
529 /* add some noise */
530 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
531 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
532 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
533 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
534 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
535 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
536 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
537 } }
538
539 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200540 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200541 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
542 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
543 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200544 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
545 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200546 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
547 outBuff.size = outBuff.pos + adjustedDstSize;
548 inBuff.size = inBuff.pos + randomCSrcSize;
549 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200550 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
551 } } } }
552 DISPLAY("\r%u fuzzer tests completed \n", testNb);
553
554_cleanup:
555 ZSTD_freeCStream(zc);
556 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200557 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200558 free(cNoiseBuffer[0]);
559 free(cNoiseBuffer[1]);
560 free(cNoiseBuffer[2]);
561 free(cNoiseBuffer[3]);
562 free(cNoiseBuffer[4]);
563 free(copyBuffer);
564 free(cBuffer);
565 free(dstBuffer);
566 return result;
567
568_output_error:
569 result = 1;
570 goto _cleanup;
571}
572
573
574/*-*******************************************************
575* Command line
576*********************************************************/
577int FUZ_usage(const char* programName)
578{
579 DISPLAY( "Usage :\n");
580 DISPLAY( " %s [args]\n", programName);
581 DISPLAY( "\n");
582 DISPLAY( "Arguments :\n");
583 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
584 DISPLAY( " -s# : Select seed (default:prompt user)\n");
585 DISPLAY( " -t# : Select starting test number (default:0)\n");
586 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
587 DISPLAY( " -v : verbose\n");
588 DISPLAY( " -p : pause at the end\n");
589 DISPLAY( " -h : display help and exit\n");
590 return 0;
591}
592
593
594int main(int argc, const char** argv)
595{
596 U32 seed=0;
597 int seedset=0;
598 int argNb;
599 int nbTests = nbTestsDefault;
600 int testNb = 0;
601 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
602 int result=0;
603 U32 mainPause = 0;
604 const char* programName = argv[0];
605 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
606 ZSTD_customMem customNULL = { NULL, NULL, NULL };
607
608 /* Check command line */
609 for(argNb=1; argNb<argc; argNb++) {
610 const char* argument = argv[argNb];
611 if(!argument) continue; /* Protection if argument empty */
612
613 /* Parsing commands. Aggregated commands are allowed */
614 if (argument[0]=='-') {
615 argument++;
616
617 while (*argument!=0) {
618 switch(*argument)
619 {
620 case 'h':
621 return FUZ_usage(programName);
622 case 'v':
623 argument++;
624 g_displayLevel=4;
625 break;
626 case 'q':
627 argument++;
628 g_displayLevel--;
629 break;
630 case 'p': /* pause at the end */
631 argument++;
632 mainPause = 1;
633 break;
634
635 case 'i':
636 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700637 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200638 while ((*argument>='0') && (*argument<='9')) {
639 nbTests *= 10;
640 nbTests += *argument - '0';
641 argument++;
642 }
643 break;
644
645 case 'T':
646 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700647 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200648 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700649 g_clockTime *= 10;
650 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200651 argument++;
652 }
Yann Colletef9999f2016-09-01 16:44:48 -0700653 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200654 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700655 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200656 break;
657
658 case 's':
659 argument++;
660 seed=0;
661 seedset=1;
662 while ((*argument>='0') && (*argument<='9')) {
663 seed *= 10;
664 seed += *argument - '0';
665 argument++;
666 }
667 break;
668
669 case 't':
670 argument++;
671 testNb=0;
672 while ((*argument>='0') && (*argument<='9')) {
673 testNb *= 10;
674 testNb += *argument - '0';
675 argument++;
676 }
677 break;
678
679 case 'P': /* compressibility % */
680 argument++;
681 proba=0;
682 while ((*argument>='0') && (*argument<='9')) {
683 proba *= 10;
684 proba += *argument - '0';
685 argument++;
686 }
687 if (proba<0) proba=0;
688 if (proba>100) proba=100;
689 break;
690
691 default:
692 return FUZ_usage(programName);
693 }
694 } } } /* for(argNb=1; argNb<argc; argNb++) */
695
696 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200697 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200698
Yann Colletef9999f2016-09-01 16:44:48 -0700699 if (!seedset) {
700 time_t const t = time(NULL);
701 U32 const h = XXH32(&t, sizeof(t), 1);
702 seed = h % 10000;
703 }
704
Yann Colletd7883a22016-08-12 16:48:02 +0200705 DISPLAY("Seed = %u\n", seed);
706 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
707
708 if (nbTests<=0) nbTests=1;
709
710 if (testNb==0) {
711 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
712 if (!result) {
713 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
714 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
715 } }
716
717 if (!result)
718 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
719
720 if (mainPause) {
721 int unused;
722 DISPLAY("Press Enter \n");
723 unused = getchar();
724 (void)unused;
725 }
726 return result;
727}