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