blob: f02197491eaf86d1acb4b2efd220822ebfb0437a [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 */
26#include <sys/timeb.h> /* timeb */
27#include <string.h> /* strcmp */
28#include "mem.h"
29#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */
30#include "zstd.h" /* ZSTD_compressBound */
31#include "datagen.h" /* RDG_genBuffer */
32#define XXH_STATIC_LINKING_ONLY
33#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) { \
58 if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \
59 { g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \
60 if (g_displayLevel>=4) fflush(stdout); } }
61static const U32 g_refreshRate = 150;
62static U32 g_displayTime = 0;
63
64static U32 g_testTime = 0;
65
66
67/*-*******************************************************
68* Fuzzer functions
69*********************************************************/
70#define MAX(a,b) ((a)>(b)?(a):(b))
71
72static U32 FUZ_GetMilliStart(void)
73{
74 struct timeb tb;
75 U32 nCount;
76 ftime( &tb );
77 nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm);
78 return nCount;
79}
80
Yann Colletd7883a22016-08-12 16:48:02 +020081static U32 FUZ_GetMilliSpan(U32 nTimeStart)
82{
83 U32 const nCurrent = FUZ_GetMilliStart();
84 U32 nSpan = nCurrent - nTimeStart;
85 if (nTimeStart > nCurrent)
86 nSpan += 0x100000 * 1000;
87 return nSpan;
88}
89
90/*! FUZ_rand() :
91 @return : a 27 bits random value, from a 32-bits `seed`.
92 `seed` is also modified */
93# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
94unsigned int FUZ_rand(unsigned int* seedPtr)
95{
96 U32 rand32 = *seedPtr;
97 rand32 *= prime1;
98 rand32 += prime2;
99 rand32 = FUZ_rotl32(rand32, 13);
100 *seedPtr = rand32;
101 return rand32 >> 5;
102}
103
Yann Colletd7883a22016-08-12 16:48:02 +0200104/*
105static unsigned FUZ_highbit32(U32 v32)
106{
107 unsigned nbBits = 0;
108 if (v32==0) return 0;
109 for ( ; v32 ; v32>>=1) nbBits++;
110 return nbBits;
111}
112*/
113
114static void* allocFunction(void* opaque, size_t size)
115{
116 void* address = malloc(size);
117 (void)opaque;
118 return address;
119}
120
121static void freeFunction(void* opaque, void* address)
122{
123 (void)opaque;
124 free(address);
125}
126
Yann Colletcb327632016-08-23 00:30:31 +0200127
128/*======================================================
129* Basic Unit tests
130======================================================*/
131
Yann Colletd7883a22016-08-12 16:48:02 +0200132static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
133{
134 int testResult = 0;
135 size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
136 void* CNBuffer = malloc(CNBufferSize);
137 size_t const skippableFrameSize = 11;
138 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
139 void* compressedBuffer = malloc(compressedBufferSize);
140 size_t const decodedBufferSize = CNBufferSize;
141 void* decodedBuffer = malloc(decodedBufferSize);
142 size_t cSize;
143 U32 testNb=0;
144 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
145 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet53e17fb2016-08-17 01:39:22 +0200146 ZSTD_inBuffer inBuff;
147 ZSTD_outBuffer outBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200148
149 /* Create compressible test buffer */
150 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
151 DISPLAY("Not enough memory, aborting\n");
152 goto _output_error;
153 }
154 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
155
156 /* generate skippable frame */
157 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
158 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
159 cSize = skippableFrameSize + 8;
160
161 /* Basic compression test */
162 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
163 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200164 outBuff.dst = (char*)(compressedBuffer)+cSize;
165 outBuff.size = compressedBufferSize;
166 outBuff.pos = 0;
167 inBuff.src = CNBuffer;
168 inBuff.size = CNBufferSize;
169 inBuff.pos = 0;
170 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200171 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200172 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
173 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200174 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200175 cSize += outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200176 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
177
Yann Colletcb327632016-08-23 00:30:31 +0200178 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200179 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200180 if (ZSTD_isError(s)) goto _output_error;
181 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
182 }
183
Yann Colletd7883a22016-08-12 16:48:02 +0200184 /* skippable frame test */
185 DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
186 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200187 inBuff.src = compressedBuffer;
188 inBuff.size = cSize;
189 inBuff.pos = 0;
190 outBuff.dst = decodedBuffer;
191 outBuff.size = CNBufferSize;
192 outBuff.pos = 0;
193 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200194 if (r != 0) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200195 if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */
Yann Colletd7883a22016-08-12 16:48:02 +0200196 DISPLAYLEVEL(4, "OK \n");
197
198 /* Basic decompression test */
199 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
200 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet17e482e2016-08-23 16:58:10 +0200201 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */
202 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200203 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200204 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 +0200205 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
206 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200207 DISPLAYLEVEL(4, "OK \n");
208
209 /* check regenerated data is byte exact */
210 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
211 { size_t i;
212 for (i=0; i<CNBufferSize; i++) {
213 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
214 } }
215 DISPLAYLEVEL(4, "OK \n");
216
Yann Colletcb327632016-08-23 00:30:31 +0200217 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200218 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200219 if (ZSTD_isError(s)) goto _output_error;
220 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
221 }
222
Yann Colletd7883a22016-08-12 16:48:02 +0200223 /* Byte-by-byte decompression test */
224 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
225 { size_t r = 1;
226 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200227 inBuff.src = compressedBuffer;
228 outBuff.dst = decodedBuffer;
229 inBuff.pos = 0;
230 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200231 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200232 inBuff.size = inBuff.pos + 1;
233 outBuff.size = outBuff.pos + 1;
234 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200235 if (ZSTD_isError(r)) goto _output_error;
236 }
237 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
238 r=1;
239 while (r) { /* normal frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200240 inBuff.size = inBuff.pos + 1;
241 outBuff.size = outBuff.pos + 1;
242 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200243 if (ZSTD_isError(r)) goto _output_error;
244 }
245 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200246 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
247 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200248 DISPLAYLEVEL(4, "OK \n");
249
250 /* check regenerated data is byte exact */
251 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
252 { size_t i;
253 for (i=0; i<CNBufferSize; i++) {
254 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
255 } }
256 DISPLAYLEVEL(4, "OK \n");
257
Yann Collet17e482e2016-08-23 16:58:10 +0200258 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
259 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
260 if (!ZSTD_isError(r)) goto _output_error; }
261 DISPLAYLEVEL(4, "OK \n");
262
263 /* Memory restriction */
264 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
265 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
266 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
267 if (ZSTD_isError(r)) goto _output_error; }
268 inBuff.src = compressedBuffer;
269 inBuff.size = cSize;
270 inBuff.pos = 0;
271 outBuff.dst = decodedBuffer;
272 outBuff.size = CNBufferSize;
273 outBuff.pos = 0;
274 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
275 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
276 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
277
278
Yann Colletd7883a22016-08-12 16:48:02 +0200279_end:
280 ZSTD_freeCStream(zc);
281 ZSTD_freeDStream(zd);
282 free(CNBuffer);
283 free(compressedBuffer);
284 free(decodedBuffer);
285 return testResult;
286
287_output_error:
288 testResult = 1;
289 DISPLAY("Error detected in Unit tests ! \n");
290 goto _end;
291}
292
293
294static size_t findDiff(const void* buf1, const void* buf2, size_t max)
295{
296 const BYTE* b1 = (const BYTE*)buf1;
297 const BYTE* b2 = (const BYTE*)buf2;
298 size_t u;
299 for (u=0; u<max; u++) {
300 if (b1[u] != b2[u]) break;
301 }
302 return u;
303}
304
305static size_t FUZ_rLogLength(U32* seed, U32 logLength)
306{
307 size_t const lengthMask = ((size_t)1 << logLength) - 1;
308 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
309}
310
311static size_t FUZ_randomLength(U32* seed, U32 maxLog)
312{
313 U32 const logLength = FUZ_rand(seed) % maxLog;
314 return FUZ_rLogLength(seed, logLength);
315}
316
317#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
318
319#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
320 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
321
322static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
323{
324 static const U32 maxSrcLog = 24;
325 static const U32 maxSampleLog = 19;
326 BYTE* cNoiseBuffer[5];
327 size_t srcBufferSize = (size_t)1<<maxSrcLog;
328 BYTE* copyBuffer;
329 size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog);
330 BYTE* cBuffer;
331 size_t cBufferSize = ZSTD_compressBound(srcBufferSize);
332 BYTE* dstBuffer;
333 size_t dstBufferSize = srcBufferSize;
334 U32 result = 0;
335 U32 testNb = 0;
336 U32 coreSeed = seed;
337 ZSTD_CStream* zc;
338 ZSTD_DStream* zd;
339 U32 startTime = FUZ_GetMilliStart();
340
341 /* allocations */
342 zc = ZSTD_createCStream();
343 zd = ZSTD_createDStream();
344 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
345 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
346 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
347 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
348 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
349 copyBuffer= (BYTE*)malloc (copyBufferSize);
350 dstBuffer = (BYTE*)malloc (dstBufferSize);
351 cBuffer = (BYTE*)malloc (cBufferSize);
352 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
353 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd,
354 "Not enough memory, fuzzer tests cancelled");
355
356 /* Create initial samples */
357 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
358 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
359 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
360 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
361 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
362 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
363
364 /* catch up testNb */
365 for (testNb=1; testNb < startTest; testNb++)
366 FUZ_rand(&coreSeed);
367
368 /* test loop */
369 for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime) ; testNb++ ) {
370 U32 lseed;
371 const BYTE* srcBuffer;
372 const BYTE* dict;
373 size_t maxTestSize, dictSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200374 size_t cSize, totalTestSize, totalGenSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200375 U32 n, nbChunks;
376 XXH64_state_t xxhState;
377 U64 crcOrig;
378
379 /* init */
380 DISPLAYUPDATE(2, "\r%6u", testNb);
381 if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
382 FUZ_rand(&coreSeed);
383 lseed = coreSeed ^ prime1;
384
385 /* states full reset (unsynchronized) */
386 /* some issues only happen when reusing states in a specific sequence of parameters */
387 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); }
388 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); }
389
390 /* srcBuffer selection [0-4] */
391 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
392 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
393 else {
394 buffNb >>= 3;
395 if (buffNb & 7) {
396 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
397 buffNb = tnb[buffNb >> 3];
398 } else {
399 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
400 buffNb = tnb[buffNb >> 3];
401 } }
402 srcBuffer = cNoiseBuffer[buffNb];
403 }
404
405 /* compression init */
406 { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
407 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
408 maxTestSize = FUZ_rLogLength(&lseed, testLog);
409 dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
410 /* random dictionary selection */
411 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
412 dict = srcBuffer + dictStart;
413 }
414 { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize);
415 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
416 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
417 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0);
418 CHECK (ZSTD_isError(initError),"init error : %s", ZSTD_getErrorName(initError));
419 } } }
420
421 /* multi-segments compression test */
422 XXH64_reset(&xxhState, 0);
423 nbChunks = (FUZ_rand(&lseed) & 127) + 2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200424 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Colletd7883a22016-08-12 16:48:02 +0200425 for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
426 /* compress random chunk into random size dst buffer */
Yann Collet53e17fb2016-08-17 01:39:22 +0200427 { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Colletd7883a22016-08-12 16:48:02 +0200428 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
429 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
430 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200431 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 };
432 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200433
Yann Collet53e17fb2016-08-17 01:39:22 +0200434 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200435 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200436
Yann Collet53e17fb2016-08-17 01:39:22 +0200437 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
438 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
439 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200440 }
441
442 /* random flush operation, to mess around */
443 if ((FUZ_rand(&lseed) & 15) == 0) {
444 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200445 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
446 outBuff.size = outBuff.pos + adjustedDstSize;
447 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200448 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
449 } } }
450
451 /* final frame epilogue */
452 { size_t remainingToFlush = (size_t)(-1);
453 while (remainingToFlush) {
454 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
455 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
456 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200457 outBuff.size = outBuff.pos + adjustedDstSize;
458 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200459 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
460 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
461 } }
462 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200463 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200464 }
465
466 /* multi - fragments decompression test */
467 ZSTD_initDStream_usingDict(zd, dict, dictSize);
468 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200469 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
470 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
471 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200472 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
473 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
474 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200475 inBuff.size = inBuff.pos + readCSrcSize;
476 outBuff.size = inBuff.pos + dstBuffSize;
477 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200478 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200479 }
480 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200481 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
482 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200483 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
484 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
485 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
486 } }
487
488 /*===== noisy/erroneous src decompression test =====*/
489
490 /* add some noise */
491 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
492 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
493 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
494 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
495 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
496 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
497 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
498 } }
499
500 /* try decompression on noisy data */
501 ZSTD_initDStream(zd);
Yann Collet53e17fb2016-08-17 01:39:22 +0200502 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
503 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
504 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200505 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
506 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200507 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
508 outBuff.size = outBuff.pos + adjustedDstSize;
509 inBuff.size = inBuff.pos + randomCSrcSize;
510 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200511 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
512 } } } }
513 DISPLAY("\r%u fuzzer tests completed \n", testNb);
514
515_cleanup:
516 ZSTD_freeCStream(zc);
517 ZSTD_freeDStream(zd);
518 free(cNoiseBuffer[0]);
519 free(cNoiseBuffer[1]);
520 free(cNoiseBuffer[2]);
521 free(cNoiseBuffer[3]);
522 free(cNoiseBuffer[4]);
523 free(copyBuffer);
524 free(cBuffer);
525 free(dstBuffer);
526 return result;
527
528_output_error:
529 result = 1;
530 goto _cleanup;
531}
532
533
534/*-*******************************************************
535* Command line
536*********************************************************/
537int FUZ_usage(const char* programName)
538{
539 DISPLAY( "Usage :\n");
540 DISPLAY( " %s [args]\n", programName);
541 DISPLAY( "\n");
542 DISPLAY( "Arguments :\n");
543 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
544 DISPLAY( " -s# : Select seed (default:prompt user)\n");
545 DISPLAY( " -t# : Select starting test number (default:0)\n");
546 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
547 DISPLAY( " -v : verbose\n");
548 DISPLAY( " -p : pause at the end\n");
549 DISPLAY( " -h : display help and exit\n");
550 return 0;
551}
552
553
554int main(int argc, const char** argv)
555{
556 U32 seed=0;
557 int seedset=0;
558 int argNb;
559 int nbTests = nbTestsDefault;
560 int testNb = 0;
561 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
562 int result=0;
563 U32 mainPause = 0;
564 const char* programName = argv[0];
565 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
566 ZSTD_customMem customNULL = { NULL, NULL, NULL };
567
568 /* Check command line */
569 for(argNb=1; argNb<argc; argNb++) {
570 const char* argument = argv[argNb];
571 if(!argument) continue; /* Protection if argument empty */
572
573 /* Parsing commands. Aggregated commands are allowed */
574 if (argument[0]=='-') {
575 argument++;
576
577 while (*argument!=0) {
578 switch(*argument)
579 {
580 case 'h':
581 return FUZ_usage(programName);
582 case 'v':
583 argument++;
584 g_displayLevel=4;
585 break;
586 case 'q':
587 argument++;
588 g_displayLevel--;
589 break;
590 case 'p': /* pause at the end */
591 argument++;
592 mainPause = 1;
593 break;
594
595 case 'i':
596 argument++;
597 nbTests=0; g_testTime=0;
598 while ((*argument>='0') && (*argument<='9')) {
599 nbTests *= 10;
600 nbTests += *argument - '0';
601 argument++;
602 }
603 break;
604
605 case 'T':
606 argument++;
607 nbTests=0; g_testTime=0;
608 while ((*argument>='0') && (*argument<='9')) {
609 g_testTime *= 10;
610 g_testTime += *argument - '0';
611 argument++;
612 }
613 if (*argument=='m') g_testTime *=60, argument++;
614 if (*argument=='n') argument++;
615 g_testTime *= 1000;
616 break;
617
618 case 's':
619 argument++;
620 seed=0;
621 seedset=1;
622 while ((*argument>='0') && (*argument<='9')) {
623 seed *= 10;
624 seed += *argument - '0';
625 argument++;
626 }
627 break;
628
629 case 't':
630 argument++;
631 testNb=0;
632 while ((*argument>='0') && (*argument<='9')) {
633 testNb *= 10;
634 testNb += *argument - '0';
635 argument++;
636 }
637 break;
638
639 case 'P': /* compressibility % */
640 argument++;
641 proba=0;
642 while ((*argument>='0') && (*argument<='9')) {
643 proba *= 10;
644 proba += *argument - '0';
645 argument++;
646 }
647 if (proba<0) proba=0;
648 if (proba>100) proba=100;
649 break;
650
651 default:
652 return FUZ_usage(programName);
653 }
654 } } } /* for(argNb=1; argNb<argc; argNb++) */
655
656 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200657 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200658
659 if (!seedset) seed = FUZ_GetMilliStart() % 10000;
660 DISPLAY("Seed = %u\n", seed);
661 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
662
663 if (nbTests<=0) nbTests=1;
664
665 if (testNb==0) {
666 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
667 if (!result) {
668 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
669 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
670 } }
671
672 if (!result)
673 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
674
675 if (mainPause) {
676 int unused;
677 DISPLAY("Press Enter \n");
678 unused = getchar();
679 (void)unused;
680 }
681 return result;
682}