blob: 963078d8a45e0197c6910c60fd96c6fce98fe02d [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
312 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200313 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
314 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
315 if (!ZSTD_isError(r)) goto _output_error; }
316 DISPLAYLEVEL(4, "OK \n");
317
318 /* Memory restriction */
319 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
320 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
321 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
322 if (ZSTD_isError(r)) goto _output_error; }
323 inBuff.src = compressedBuffer;
324 inBuff.size = cSize;
325 inBuff.pos = 0;
326 outBuff.dst = decodedBuffer;
327 outBuff.size = CNBufferSize;
328 outBuff.pos = 0;
329 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
330 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
331 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
332
333
Yann Colletd7883a22016-08-12 16:48:02 +0200334_end:
335 ZSTD_freeCStream(zc);
336 ZSTD_freeDStream(zd);
337 free(CNBuffer);
338 free(compressedBuffer);
339 free(decodedBuffer);
340 return testResult;
341
342_output_error:
343 testResult = 1;
344 DISPLAY("Error detected in Unit tests ! \n");
345 goto _end;
346}
347
348
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200349/* ====== Fuzzer tests ====== */
350
Yann Colletd7883a22016-08-12 16:48:02 +0200351static size_t findDiff(const void* buf1, const void* buf2, size_t max)
352{
353 const BYTE* b1 = (const BYTE*)buf1;
354 const BYTE* b2 = (const BYTE*)buf2;
355 size_t u;
356 for (u=0; u<max; u++) {
357 if (b1[u] != b2[u]) break;
358 }
359 return u;
360}
361
362static size_t FUZ_rLogLength(U32* seed, U32 logLength)
363{
364 size_t const lengthMask = ((size_t)1 << logLength) - 1;
365 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
366}
367
368static size_t FUZ_randomLength(U32* seed, U32 maxLog)
369{
370 U32 const logLength = FUZ_rand(seed) % maxLog;
371 return FUZ_rLogLength(seed, logLength);
372}
373
374#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
375
376#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
377 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
378
379static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
380{
381 static const U32 maxSrcLog = 24;
382 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200383 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200384 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200385 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
386 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
387 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
388 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
389 size_t const dstBufferSize = srcBufferSize;
390 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200391 U32 result = 0;
392 U32 testNb = 0;
393 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200394 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
395 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
396 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
397 clock_t const startClock = clock();
398 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200399 size_t dictSize = 0;
400 U32 oldTestLog = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200401
402 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200403 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
404 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
405 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
406 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
407 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200408 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200409 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200410 "Not enough memory, fuzzer tests cancelled");
411
412 /* Create initial samples */
413 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
414 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
415 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
416 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
417 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
418 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200419 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200420
421 /* catch up testNb */
422 for (testNb=1; testNb < startTest; testNb++)
423 FUZ_rand(&coreSeed);
424
425 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700426 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200427 U32 lseed;
428 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200429 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200430 XXH64_state_t xxhState;
431 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200432 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200433 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200434
435 /* init */
436 DISPLAYUPDATE(2, "\r%6u", testNb);
437 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
438 FUZ_rand(&coreSeed);
439 lseed = coreSeed ^ prime1;
440
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200441 /* states full reset (deliberately not synchronized) */
442 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200443 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
444 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 +0200445
446 /* srcBuffer selection [0-4] */
447 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
448 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
449 else {
450 buffNb >>= 3;
451 if (buffNb & 7) {
452 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
453 buffNb = tnb[buffNb >> 3];
454 } else {
455 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
456 buffNb = tnb[buffNb >> 3];
457 } }
458 srcBuffer = cNoiseBuffer[buffNb];
459 }
460
461 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200462 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
463 && oldTestLog /* at least one test happened */ && resetAllowed) {
464 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
465 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
466 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
467 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
468 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
469 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200470 } else {
471 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200472 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
473 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200474 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200475 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200476 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200477 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
478 dict = srcBuffer + dictStart;
479 }
Yann Colletcf409a72016-09-26 16:41:05 +0200480 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
481 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200482 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
483 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200484 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200485 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200486 } } }
487
488 /* multi-segments compression test */
489 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200490 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200491 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200492 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Colletd7883a22016-08-12 16:48:02 +0200493 /* compress random chunk into random size dst buffer */
Yann Collet2f263942016-09-26 14:06:08 +0200494 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
495 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
496 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200497 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
498 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200499 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200500 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200501
Yann Collet53e17fb2016-08-17 01:39:22 +0200502 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200503 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200504
Yann Collet53e17fb2016-08-17 01:39:22 +0200505 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
506 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
507 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200508 }
509
510 /* random flush operation, to mess around */
511 if ((FUZ_rand(&lseed) & 15) == 0) {
512 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200513 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
514 outBuff.size = outBuff.pos + adjustedDstSize;
515 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200516 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
517 } } }
518
519 /* final frame epilogue */
520 { size_t remainingToFlush = (size_t)(-1);
521 while (remainingToFlush) {
522 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
523 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
524 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200525 outBuff.size = outBuff.pos + adjustedDstSize;
526 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200527 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
528 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
529 } }
530 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200531 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200532 }
533
534 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200535 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700536 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet58d5dfe2016-09-25 01:34:03 +0200537 } else
538 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200539 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200540 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
541 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
542 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200543 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
544 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
545 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200546 inBuff.size = inBuff.pos + readCSrcSize;
547 outBuff.size = inBuff.pos + dstBuffSize;
548 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200549 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200550 }
551 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200552 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
553 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200554 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
555 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
556 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
557 } }
558
559 /*===== noisy/erroneous src decompression test =====*/
560
561 /* add some noise */
562 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
563 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
564 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
565 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
566 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
567 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
568 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
569 } }
570
571 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200572 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200573 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
574 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
575 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200576 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
577 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200578 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
579 outBuff.size = outBuff.pos + adjustedDstSize;
580 inBuff.size = inBuff.pos + randomCSrcSize;
581 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200582 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
583 } } } }
584 DISPLAY("\r%u fuzzer tests completed \n", testNb);
585
586_cleanup:
587 ZSTD_freeCStream(zc);
588 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200589 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200590 free(cNoiseBuffer[0]);
591 free(cNoiseBuffer[1]);
592 free(cNoiseBuffer[2]);
593 free(cNoiseBuffer[3]);
594 free(cNoiseBuffer[4]);
595 free(copyBuffer);
596 free(cBuffer);
597 free(dstBuffer);
598 return result;
599
600_output_error:
601 result = 1;
602 goto _cleanup;
603}
604
605
606/*-*******************************************************
607* Command line
608*********************************************************/
609int FUZ_usage(const char* programName)
610{
611 DISPLAY( "Usage :\n");
612 DISPLAY( " %s [args]\n", programName);
613 DISPLAY( "\n");
614 DISPLAY( "Arguments :\n");
615 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
616 DISPLAY( " -s# : Select seed (default:prompt user)\n");
617 DISPLAY( " -t# : Select starting test number (default:0)\n");
618 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
619 DISPLAY( " -v : verbose\n");
620 DISPLAY( " -p : pause at the end\n");
621 DISPLAY( " -h : display help and exit\n");
622 return 0;
623}
624
625
626int main(int argc, const char** argv)
627{
628 U32 seed=0;
629 int seedset=0;
630 int argNb;
631 int nbTests = nbTestsDefault;
632 int testNb = 0;
633 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
634 int result=0;
635 U32 mainPause = 0;
636 const char* programName = argv[0];
637 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
638 ZSTD_customMem customNULL = { NULL, NULL, NULL };
639
640 /* Check command line */
641 for(argNb=1; argNb<argc; argNb++) {
642 const char* argument = argv[argNb];
643 if(!argument) continue; /* Protection if argument empty */
644
645 /* Parsing commands. Aggregated commands are allowed */
646 if (argument[0]=='-') {
647 argument++;
648
649 while (*argument!=0) {
650 switch(*argument)
651 {
652 case 'h':
653 return FUZ_usage(programName);
654 case 'v':
655 argument++;
656 g_displayLevel=4;
657 break;
658 case 'q':
659 argument++;
660 g_displayLevel--;
661 break;
662 case 'p': /* pause at the end */
663 argument++;
664 mainPause = 1;
665 break;
666
667 case 'i':
668 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700669 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200670 while ((*argument>='0') && (*argument<='9')) {
671 nbTests *= 10;
672 nbTests += *argument - '0';
673 argument++;
674 }
675 break;
676
677 case 'T':
678 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700679 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200680 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700681 g_clockTime *= 10;
682 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200683 argument++;
684 }
Yann Colletef9999f2016-09-01 16:44:48 -0700685 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200686 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700687 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200688 break;
689
690 case 's':
691 argument++;
692 seed=0;
693 seedset=1;
694 while ((*argument>='0') && (*argument<='9')) {
695 seed *= 10;
696 seed += *argument - '0';
697 argument++;
698 }
699 break;
700
701 case 't':
702 argument++;
703 testNb=0;
704 while ((*argument>='0') && (*argument<='9')) {
705 testNb *= 10;
706 testNb += *argument - '0';
707 argument++;
708 }
709 break;
710
711 case 'P': /* compressibility % */
712 argument++;
713 proba=0;
714 while ((*argument>='0') && (*argument<='9')) {
715 proba *= 10;
716 proba += *argument - '0';
717 argument++;
718 }
719 if (proba<0) proba=0;
720 if (proba>100) proba=100;
721 break;
722
723 default:
724 return FUZ_usage(programName);
725 }
726 } } } /* for(argNb=1; argNb<argc; argNb++) */
727
728 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200729 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200730
Yann Colletef9999f2016-09-01 16:44:48 -0700731 if (!seedset) {
732 time_t const t = time(NULL);
733 U32 const h = XXH32(&t, sizeof(t), 1);
734 seed = h % 10000;
735 }
736
Yann Colletd7883a22016-08-12 16:48:02 +0200737 DISPLAY("Seed = %u\n", seed);
738 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
739
740 if (nbTests<=0) nbTests=1;
741
742 if (testNb==0) {
743 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
744 if (!result) {
745 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
746 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
747 } }
748
749 if (!result)
750 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
751
752 if (mainPause) {
753 int unused;
754 DISPLAY("Press Enter \n");
755 unused = getchar();
756 (void)unused;
757 }
758 return result;
759}