blob: 370859ca93727cc80d0afb78a6c9855c4b16989d [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 Collet33fce032017-01-16 19:46:22 -080029#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
Yann Colletd7883a22016-08-12 16:48:02 +020030#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010031#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Collet736788f2017-01-19 12:12:50 -080032#include "zstdmt_compress.h"
Yann Collet33fce032017-01-16 19:46:22 -080033#include "zdict.h" /* ZDICT_trainFromBuffer */
Yann Colletd7883a22016-08-12 16:48:02 +020034#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070035#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020036#include "xxhash.h" /* XXH64_* */
37
38
39/*-************************************
40* Constants
41**************************************/
42#define KB *(1U<<10)
43#define MB *(1U<<20)
44#define GB *(1U<<30)
45
46static const U32 nbTestsDefault = 10000;
47#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
48#define FUZ_COMPRESSIBILITY_DEFAULT 50
Yann Collet33fce032017-01-16 19:46:22 -080049static const U32 prime32 = 2654435761U;
Yann Colletd7883a22016-08-12 16:48:02 +020050
51
Yann Colletd7883a22016-08-12 16:48:02 +020052/*-************************************
53* Display Macros
54**************************************/
55#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
56#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
57static U32 g_displayLevel = 2;
58
59#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070060 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
61 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -070062 if (g_displayLevel>=4) fflush(stderr); } }
Yann Colletb3060f72016-09-09 16:44:16 +020063static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070064static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020065
Yann Colletef9999f2016-09-01 16:44:48 -070066static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020067
68
69/*-*******************************************************
70* Fuzzer functions
71*********************************************************/
72#define MAX(a,b) ((a)>(b)?(a):(b))
73
Yann Colletef9999f2016-09-01 16:44:48 -070074static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020075{
Yann Colletef9999f2016-09-01 16:44:48 -070076 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020077}
78
79/*! FUZ_rand() :
80 @return : a 27 bits random value, from a 32-bits `seed`.
81 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070082#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020083unsigned int FUZ_rand(unsigned int* seedPtr)
84{
Yann Collet33fce032017-01-16 19:46:22 -080085 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020086 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080087 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020088 rand32 += prime2;
89 rand32 = FUZ_rotl32(rand32, 13);
90 *seedPtr = rand32;
91 return rand32 >> 5;
92}
93
Yann Colletd7883a22016-08-12 16:48:02 +020094static void* allocFunction(void* opaque, size_t size)
95{
96 void* address = malloc(size);
97 (void)opaque;
98 return address;
99}
100
101static void freeFunction(void* opaque, void* address)
102{
103 (void)opaque;
104 free(address);
105}
106
Yann Colletcb327632016-08-23 00:30:31 +0200107
108/*======================================================
109* Basic Unit tests
110======================================================*/
111
Yann Collet33fce032017-01-16 19:46:22 -0800112typedef struct {
113 void* start;
114 size_t size;
115 size_t filled;
116} buffer_t;
117
118static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
119
120static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
121{
122 buffer_t dict = { NULL, 0, 0 };
123 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
124 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
125 if (!blockSizes) return dict;
126 dict.start = malloc(requestedDictSize);
127 if (!dict.start) { free(blockSizes); return dict; }
128 { size_t nb;
129 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
130 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
131 }
132 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
133 free(blockSizes);
134 if (ZDICT_isError(dictSize)) { free(dict.start); return (buffer_t){ NULL, 0, 0 }; }
135 dict.size = requestedDictSize;
136 dict.filled = dictSize;
137 return dict; /* how to return dictSize ? */
138 }
139}
140
141static void FUZ_freeDictionary(buffer_t dict)
142{
143 free(dict.start);
144}
145
146
Yann Colletd7883a22016-08-12 16:48:02 +0200147static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
148{
Yann Colletb3060f72016-09-09 16:44:16 +0200149 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200150 void* CNBuffer = malloc(CNBufferSize);
151 size_t const skippableFrameSize = 11;
152 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
153 void* compressedBuffer = malloc(compressedBufferSize);
154 size_t const decodedBufferSize = CNBufferSize;
155 void* decodedBuffer = malloc(decodedBufferSize);
156 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200157 int testResult = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200158 U32 testNb=0;
159 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
160 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800161 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200162 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800163 buffer_t dictionary = g_nullBuffer;
164 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200165
166 /* Create compressible test buffer */
167 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800168 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200169 goto _output_error;
170 }
171 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
172
Yann Collet33fce032017-01-16 19:46:22 -0800173 /* Create dictionary */
174 MEM_STATIC_ASSERT(COMPRESSIBLE_NOISE_LENGTH >= 4 MB);
175 dictionary = FUZ_createDictionary(CNBuffer, 4 MB, 4 KB, 40 KB);
176 if (!dictionary.start) {
177 DISPLAY("Error creating dictionary, aborting \n");
178 goto _output_error;
179 }
180 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
181
Yann Colletd7883a22016-08-12 16:48:02 +0200182 /* generate skippable frame */
183 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
184 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
185 cSize = skippableFrameSize + 8;
186
187 /* Basic compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800188 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Colletd7883a22016-08-12 16:48:02 +0200189 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200190 outBuff.dst = (char*)(compressedBuffer)+cSize;
191 outBuff.size = compressedBufferSize;
192 outBuff.pos = 0;
193 inBuff.src = CNBuffer;
194 inBuff.size = CNBufferSize;
195 inBuff.pos = 0;
196 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200197 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200198 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
199 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200200 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200201 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800202 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200203
Yann Collet736788f2017-01-19 12:12:50 -0800204 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200205 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200206 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800207 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200208 }
209
Yann Colletd7883a22016-08-12 16:48:02 +0200210 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800211 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200212 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200213 inBuff.src = compressedBuffer;
214 inBuff.size = cSize;
215 inBuff.pos = 0;
216 outBuff.dst = decodedBuffer;
217 outBuff.size = CNBufferSize;
218 outBuff.pos = 0;
219 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200220 if (r != 0) goto _output_error; }
Yann Colleta33ae642017-02-28 01:15:28 -0800221 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800222 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200223
224 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800225 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800226 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Colletd7883a22016-08-12 16:48:02 +0200227 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Colletbb002742017-01-25 16:25:38 -0800228 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000); /* large limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200229 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800230 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
231 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
232 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
233 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800234 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800235
236 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800237 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800238 outBuff.pos = 0;
239 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
240 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200241 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
242 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800243 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200244
245 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800246 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200247 { size_t i;
248 for (i=0; i<CNBufferSize; i++) {
249 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
250 } }
Yann Collet736788f2017-01-19 12:12:50 -0800251 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200252
Yann Collet736788f2017-01-19 12:12:50 -0800253 DISPLAYLEVEL(3, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200254 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200255 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800256 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200257 }
258
Yann Colletd7883a22016-08-12 16:48:02 +0200259 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800260 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200261 { /* skippable frame */
262 size_t r = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200263 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200264 inBuff.src = compressedBuffer;
265 outBuff.dst = decodedBuffer;
266 inBuff.pos = 0;
267 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200268 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200269 inBuff.size = inBuff.pos + 1;
270 outBuff.size = outBuff.pos + 1;
271 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200272 if (ZSTD_isError(r)) goto _output_error;
273 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200274 /* normal frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200275 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
276 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200277 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200278 inBuff.size = inBuff.pos + 1;
279 outBuff.size = outBuff.pos + 1;
280 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200281 if (ZSTD_isError(r)) goto _output_error;
282 }
283 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200284 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
285 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800286 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200287
288 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800289 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200290 { size_t i;
291 for (i=0; i<CNBufferSize; i++) {
292 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
293 } }
Yann Collet736788f2017-01-19 12:12:50 -0800294 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200295
Yann Collete795c8a2016-12-13 16:39:36 +0100296 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800297 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100298 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100299 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100300 outBuff.size = compressedBufferSize;
301 outBuff.pos = 0;
302 inBuff.src = CNBuffer;
303 inBuff.size = CNBufferSize;
304 inBuff.pos = 0;
305 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
306 if (ZSTD_isError(r)) goto _output_error; }
307 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
308 { size_t const r = ZSTD_endStream(zc, &outBuff);
309 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Sean Purcell4e709712017-02-07 13:50:09 -0800310 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100311 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800312 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100313
314 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800315 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100316 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100317 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100318 outBuff.size = compressedBufferSize;
319 outBuff.pos = 0;
320 inBuff.src = CNBuffer;
321 inBuff.size = CNBufferSize;
322 inBuff.pos = 0;
323 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
324 if (ZSTD_isError(r)) goto _output_error; }
325 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
326 { size_t const r = ZSTD_endStream(zc, &outBuff);
327 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800328 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100329
Yann Collet12083a42016-09-06 15:01:51 +0200330 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800331 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200332 ZSTD_freeCStream(zc);
333 zc = ZSTD_createCStream_advanced(customMem);
334 if (zc==NULL) goto _output_error; /* memory allocation issue */
335 /* use 1 */
336 { size_t const inSize = 513;
337 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
338 inBuff.src = CNBuffer;
339 inBuff.size = inSize;
340 inBuff.pos = 0;
341 outBuff.dst = (char*)(compressedBuffer)+cSize;
342 outBuff.size = ZSTD_compressBound(inSize);
343 outBuff.pos = 0;
344 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
345 if (ZSTD_isError(r)) goto _output_error; }
346 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
347 { size_t const r = ZSTD_endStream(zc, &outBuff);
348 if (r != 0) goto _output_error; } /* error, or some data not flushed */
349 }
350 /* use 2 */
351 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
352 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
353 inBuff.src = CNBuffer;
354 inBuff.size = inSize;
355 inBuff.pos = 0;
356 outBuff.dst = (char*)(compressedBuffer)+cSize;
357 outBuff.size = ZSTD_compressBound(inSize);
358 outBuff.pos = 0;
359 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
360 if (ZSTD_isError(r)) goto _output_error; }
361 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
362 { size_t const r = ZSTD_endStream(zc, &outBuff);
363 if (r != 0) goto _output_error; } /* error, or some data not flushed */
364 }
Yann Collet736788f2017-01-19 12:12:50 -0800365 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200366
Yann Collet95162342016-10-25 16:19:52 -0700367 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800368 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet33fce032017-01-16 19:46:22 -0800369 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1);
Yann Collet95162342016-10-25 16:19:52 -0700370 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
371 if (ZSTD_isError(initError)) goto _output_error;
372 cSize = 0;
373 outBuff.dst = compressedBuffer;
374 outBuff.size = compressedBufferSize;
375 outBuff.pos = 0;
376 inBuff.src = CNBuffer;
377 inBuff.size = CNBufferSize;
378 inBuff.pos = 0;
379 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
380 if (ZSTD_isError(r)) goto _output_error; }
381 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
382 { size_t const r = ZSTD_endStream(zc, &outBuff);
383 if (r != 0) goto _output_error; } /* error, or some data not flushed */
384 cSize = outBuff.pos;
385 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800386 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700387 }
388
Yann Collet736788f2017-01-19 12:12:50 -0800389 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200390 { size_t const s = ZSTD_sizeof_CStream(zc);
391 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800392 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200393 }
394
Yann Collet33fce032017-01-16 19:46:22 -0800395 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
396 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
397 if (dID != dictID) goto _output_error;
398 DISPLAYLEVEL(4, "OK (%u) \n", dID);
399 }
400
Yann Collet335ad5d2016-10-25 17:47:02 -0700401 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800402 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800403 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700404 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
405 if (ZSTD_isError(initError)) goto _output_error;
406 inBuff.src = compressedBuffer;
407 inBuff.size = cSize;
408 inBuff.pos = 0;
409 outBuff.dst = decodedBuffer;
410 outBuff.size = CNBufferSize;
411 outBuff.pos = 0;
412 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
413 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
414 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
415 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
416 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800417 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700418 }
419
Yann Collet12083a42016-09-06 15:01:51 +0200420 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800421 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200422 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
423 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800424 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200425
426 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800427 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200428 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Colletbb002742017-01-25 16:25:38 -0800429 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000); /* too small limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200430 if (ZSTD_isError(r)) goto _output_error; }
431 inBuff.src = compressedBuffer;
432 inBuff.size = cSize;
433 inBuff.pos = 0;
434 outBuff.dst = decodedBuffer;
435 outBuff.size = CNBufferSize;
436 outBuff.pos = 0;
437 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
438 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800439 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200440
Sean Purcell2db72492017-02-09 10:50:43 -0800441 /* Unknown srcSize */
442 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
443 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
444 params.fParams.contentSizeFlag = 1;
445 ZSTD_initCStream_advanced(zc, NULL, 0, params, 0); } /* cstream advanced should write the 0 size field */
446 inBuff.src = CNBuffer;
447 inBuff.size = 0;
448 inBuff.pos = 0;
449 outBuff.dst = compressedBuffer;
450 outBuff.size = compressedBufferSize;
451 outBuff.pos = 0;
452 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
453 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
454 cSize = outBuff.pos;
455 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
456
457 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
458 inBuff.src = CNBuffer;
459 inBuff.size = 0;
460 inBuff.pos = 0;
461 outBuff.dst = compressedBuffer;
462 outBuff.size = compressedBufferSize;
463 outBuff.pos = 0;
464 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
465 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
466 cSize = outBuff.pos;
467 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
468 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200469
Sean Purcell887eaa92017-02-15 16:43:45 -0800470 /* Overlen overwriting window data bug */
471 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800472 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
473 1. 'a' repeated 517 times
474 2. 'b' repeated 516 times
475 3. a compressed block with no literals and 3 sequence commands:
476 litlength = 0, offset = 24, match length = 24
477 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
478 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
479
480 const char* testCase =
481 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
482 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
483 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Sean Purcell887eaa92017-02-15 16:43:45 -0800484 ZSTD_DStream* zds = ZSTD_createDStream();
485
486 ZSTD_initDStream(zds);
487 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800488 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800489 inBuff.pos = 0;
490 outBuff.dst = decodedBuffer;
491 outBuff.size = CNBufferSize;
492 outBuff.pos = 0;
493
494 while (inBuff.pos < inBuff.size) {
495 size_t const r = ZSTD_decompressStream(zds, &outBuff, &inBuff);
496 /* Bug will cause checksum to fail */
497 if (ZSTD_isError(r)) goto _output_error;
498 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100499
500 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800501 }
502 DISPLAYLEVEL(3, "OK \n");
503
Yann Colletd7883a22016-08-12 16:48:02 +0200504_end:
Yann Collet33fce032017-01-16 19:46:22 -0800505 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200506 ZSTD_freeCStream(zc);
507 ZSTD_freeDStream(zd);
508 free(CNBuffer);
509 free(compressedBuffer);
510 free(decodedBuffer);
511 return testResult;
512
513_output_error:
514 testResult = 1;
515 DISPLAY("Error detected in Unit tests ! \n");
516 goto _end;
517}
518
519
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200520/* ====== Fuzzer tests ====== */
521
Yann Colletd7883a22016-08-12 16:48:02 +0200522static size_t findDiff(const void* buf1, const void* buf2, size_t max)
523{
524 const BYTE* b1 = (const BYTE*)buf1;
525 const BYTE* b2 = (const BYTE*)buf2;
526 size_t u;
527 for (u=0; u<max; u++) {
528 if (b1[u] != b2[u]) break;
529 }
Yann Collet736788f2017-01-19 12:12:50 -0800530 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800531 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
532 b1[u-3], b1[u-2], b1[u-1], b1[u-0], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]);
533 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
534 b2[u-3], b2[u-2], b2[u-1], b2[u-0], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]);
Yann Colletd7883a22016-08-12 16:48:02 +0200535 return u;
536}
537
538static size_t FUZ_rLogLength(U32* seed, U32 logLength)
539{
540 size_t const lengthMask = ((size_t)1 << logLength) - 1;
541 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
542}
543
544static size_t FUZ_randomLength(U32* seed, U32 maxLog)
545{
546 U32 const logLength = FUZ_rand(seed) % maxLog;
547 return FUZ_rLogLength(seed, logLength);
548}
549
550#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
551
552#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
553 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
554
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700555static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200556{
557 static const U32 maxSrcLog = 24;
558 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200559 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200560 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200561 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
562 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
563 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
564 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
565 size_t const dstBufferSize = srcBufferSize;
566 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200567 U32 result = 0;
568 U32 testNb = 0;
569 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200570 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
571 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
572 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
573 clock_t const startClock = clock();
574 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200575 size_t dictSize = 0;
576 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700577 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Colletd7883a22016-08-12 16:48:02 +0200578
579 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200580 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
581 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
582 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
583 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
584 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200585 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200586 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200587 "Not enough memory, fuzzer tests cancelled");
588
589 /* Create initial samples */
590 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
591 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
592 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
593 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
594 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
595 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200596 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200597
598 /* catch up testNb */
599 for (testNb=1; testNb < startTest; testNb++)
600 FUZ_rand(&coreSeed);
601
602 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700603 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200604 U32 lseed;
605 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200606 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200607 XXH64_state_t xxhState;
608 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200609 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200610 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200611
612 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700613 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
614 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200615 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800616 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200617
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200618 /* states full reset (deliberately not synchronized) */
619 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200620 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
621 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 +0200622
623 /* srcBuffer selection [0-4] */
624 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
625 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
626 else {
627 buffNb >>= 3;
628 if (buffNb & 7) {
629 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
630 buffNb = tnb[buffNb >> 3];
631 } else {
632 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
633 buffNb = tnb[buffNb >> 3];
634 } }
635 srcBuffer = cNoiseBuffer[buffNb];
636 }
637
638 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200639 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
640 && oldTestLog /* at least one test happened */ && resetAllowed) {
641 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
642 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
643 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
644 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
645 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
646 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200647 } else {
648 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700649 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700650 U32 const cLevel = (FUZ_rand(&lseed) %
651 (ZSTD_maxCLevel() -
652 (MAX(testLog, dictLog) / cLevelLimiter))) +
653 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200654 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200655 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200656 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -0700657 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200658 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
659 dict = srcBuffer + dictStart;
660 }
Yann Colletcf409a72016-09-26 16:41:05 +0200661 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
662 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200663 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
664 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200665 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200666 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200667 } } }
668
669 /* multi-segments compression test */
670 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200671 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200672 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200673 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100674 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200675 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
676 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
677 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200678 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
679 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200680 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200681 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200682
Yann Collet53e17fb2016-08-17 01:39:22 +0200683 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200684 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200685
Yann Collet53e17fb2016-08-17 01:39:22 +0200686 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
687 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
688 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200689 }
690
691 /* random flush operation, to mess around */
692 if ((FUZ_rand(&lseed) & 15) == 0) {
693 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200694 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
695 outBuff.size = outBuff.pos + adjustedDstSize;
696 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200697 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
698 } } }
699
700 /* final frame epilogue */
701 { size_t remainingToFlush = (size_t)(-1);
702 while (remainingToFlush) {
703 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
704 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
705 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200706 outBuff.size = outBuff.pos + adjustedDstSize;
707 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200708 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
709 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
710 } }
711 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200712 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200713 }
714
715 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200716 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700717 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800718 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200719 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800720 }
Yann Colletd7883a22016-08-12 16:48:02 +0200721 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200722 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
723 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
724 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200725 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
726 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
727 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200728 inBuff.size = inBuff.pos + readCSrcSize;
729 outBuff.size = inBuff.pos + dstBuffSize;
730 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200731 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200732 }
733 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200734 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
735 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200736 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
737 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
738 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
739 } }
740
741 /*===== noisy/erroneous src decompression test =====*/
742
743 /* add some noise */
744 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
745 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
746 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
747 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
748 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
749 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
750 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
751 } }
752
753 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200754 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200755 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
756 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
757 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200758 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
759 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200760 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -0800761 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200762 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -0800763 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200764 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200765 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -0800766 /* No forward progress possible */
767 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +0200768 } } } }
769 DISPLAY("\r%u fuzzer tests completed \n", testNb);
770
771_cleanup:
772 ZSTD_freeCStream(zc);
773 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200774 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200775 free(cNoiseBuffer[0]);
776 free(cNoiseBuffer[1]);
777 free(cNoiseBuffer[2]);
778 free(cNoiseBuffer[3]);
779 free(cNoiseBuffer[4]);
780 free(copyBuffer);
781 free(cBuffer);
782 free(dstBuffer);
783 return result;
784
785_output_error:
786 result = 1;
787 goto _cleanup;
788}
789
790
Yann Collet736788f2017-01-19 12:12:50 -0800791/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700792static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -0800793{
794 static const U32 maxSrcLog = 24;
795 static const U32 maxSampleLog = 19;
796 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
797 BYTE* cNoiseBuffer[5];
798 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
799 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
800 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
801 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
802 size_t const dstBufferSize = srcBufferSize;
803 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
804 U32 result = 0;
805 U32 testNb = 0;
806 U32 coreSeed = seed;
807 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(2); /* will be reset sometimes */
808 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
809 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
810 clock_t const startClock = clock();
811 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
812 size_t dictSize = 0;
813 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700814 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Collet736788f2017-01-19 12:12:50 -0800815
816 /* allocations */
817 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
818 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
819 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
820 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
821 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
822 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
823 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
824 "Not enough memory, fuzzer tests cancelled");
825
826 /* Create initial samples */
827 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
828 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
829 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
830 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
831 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
832 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
833 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
834
835 /* catch up testNb */
836 for (testNb=1; testNb < startTest; testNb++)
837 FUZ_rand(&coreSeed);
838
839 /* test loop */
840 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
841 U32 lseed;
842 const BYTE* srcBuffer;
843 size_t totalTestSize, totalGenSize, cSize;
844 XXH64_state_t xxhState;
845 U64 crcOrig;
846 U32 resetAllowed = 1;
847 size_t maxTestSize;
848
849 /* init */
850 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
851 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
852 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -0800853 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -0800854
855 /* states full reset (deliberately not synchronized) */
856 /* some issues can only happen when reusing states */
857 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
858 U32 const nbThreads = (FUZ_rand(&lseed) % 6) + 1;
859 ZSTDMT_freeCCtx(zc);
860 zc = ZSTDMT_createCCtx(nbThreads);
861 resetAllowed=0;
862 }
863 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
864 ZSTD_freeDStream(zd);
865 zd = ZSTD_createDStream();
866 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
867 }
868
869 /* srcBuffer selection [0-4] */
870 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
871 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
872 else {
873 buffNb >>= 3;
874 if (buffNb & 7) {
875 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
876 buffNb = tnb[buffNb >> 3];
877 } else {
878 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
879 buffNb = tnb[buffNb >> 3];
880 } }
881 srcBuffer = cNoiseBuffer[buffNb];
882 }
883
884 /* compression init */
885 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
886 && oldTestLog /* at least one test happened */ && resetAllowed) {
887 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
888 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
889 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
890 size_t const resetError = ZSTDMT_initCStream(zc, compressionLevel);
Yann Collet19d670b2017-01-19 15:32:07 -0800891 CHECK(ZSTD_isError(resetError), "ZSTDMT_initCStream error : %s", ZSTD_getErrorName(resetError));
Yann Collet736788f2017-01-19 12:12:50 -0800892 }
893 } else {
894 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700895 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700896 U32 const cLevel = (FUZ_rand(&lseed) %
897 (ZSTD_maxCLevel() -
898 (MAX(testLog, dictLog) / cLevelLimiter))) +
899 1;
Yann Collet736788f2017-01-19 12:12:50 -0800900 maxTestSize = FUZ_rLogLength(&lseed, testLog);
901 oldTestLog = testLog;
902 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -0700903 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -0800904 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
905 dict = srcBuffer + dictStart;
906 }
907 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
908 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
909 DISPLAYLEVEL(5, "Init with windowLog = %u \n", params.cParams.windowLog);
910 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
911 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcd23dd22017-01-30 12:46:35 -0800912 { size_t const initError = ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
913 CHECK (ZSTD_isError(initError),"ZSTDMT_initCStream_advanced error : %s", ZSTD_getErrorName(initError)); }
914 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12);
Yann Collet92c98a52017-01-30 12:50:31 -0800915 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1));
Yann Colletcd23dd22017-01-30 12:46:35 -0800916 } }
Yann Collet736788f2017-01-19 12:12:50 -0800917
918 /* multi-segments compression test */
919 XXH64_reset(&xxhState, 0);
920 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
921 U32 n;
922 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
923 /* compress random chunks into randomly sized dst buffers */
924 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
925 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
926 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
927 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
928 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
929 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
930 outBuff.size = outBuff.pos + dstBuffSize;
931
932 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
933 { size_t const compressionError = ZSTDMT_compressStream(zc, &outBuff, &inBuff);
934 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Collet19d670b2017-01-19 15:32:07 -0800935 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -0800936
937 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
938 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
939 totalTestSize += inBuff.pos;
940 }
941
942 /* random flush operation, to mess around */
943 if ((FUZ_rand(&lseed) & 15) == 0) {
944 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
945 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
946 outBuff.size = outBuff.pos + adjustedDstSize;
947 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
948 { size_t const flushError = ZSTDMT_flushStream(zc, &outBuff);
949 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
950 } } }
951
952 /* final frame epilogue */
953 { size_t remainingToFlush = (size_t)(-1);
954 while (remainingToFlush) {
955 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
956 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
957 outBuff.size = outBuff.pos + adjustedDstSize;
958 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
959 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
960 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
961 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
962 } }
963 DISPLAYLEVEL(5, "Frame completed \n");
964 crcOrig = XXH64_digest(&xxhState);
965 cSize = outBuff.pos;
966 }
967
968 /* multi - fragments decompression test */
969 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
970 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
971 } else {
972 ZSTD_initDStream_usingDict(zd, dict, dictSize);
973 }
974 { size_t decompressionResult = 1;
975 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
976 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
977 for (totalGenSize = 0 ; decompressionResult ; ) {
978 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
979 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
980 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
981 inBuff.size = inBuff.pos + readCSrcSize;
982 outBuff.size = inBuff.pos + dstBuffSize;
983 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
984 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
985 }
Yann Colletbb002742017-01-25 16:25:38 -0800986 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
987 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -0800988 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
989 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
990 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
991 } }
992
993 /*===== noisy/erroneous src decompression test =====*/
994
995 /* add some noise */
996 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
997 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
998 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
999 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1000 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1001 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1002 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1003 } }
1004
1005 /* try decompression on noisy data */
1006 ZSTD_initDStream(zd_noise); /* note : no dictionary */
1007 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1008 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1009 while (outBuff.pos < dstBufferSize) {
1010 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1011 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1012 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001013 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001014 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001015 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001016 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1017 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001018 /* No forward progress possible */
1019 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001020 } } } }
1021 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1022
1023_cleanup:
1024 ZSTDMT_freeCCtx(zc);
1025 ZSTD_freeDStream(zd);
1026 ZSTD_freeDStream(zd_noise);
1027 free(cNoiseBuffer[0]);
1028 free(cNoiseBuffer[1]);
1029 free(cNoiseBuffer[2]);
1030 free(cNoiseBuffer[3]);
1031 free(cNoiseBuffer[4]);
1032 free(copyBuffer);
1033 free(cBuffer);
1034 free(dstBuffer);
1035 return result;
1036
1037_output_error:
1038 result = 1;
1039 goto _cleanup;
1040}
1041
1042
Yann Colletd7883a22016-08-12 16:48:02 +02001043/*-*******************************************************
1044* Command line
1045*********************************************************/
1046int FUZ_usage(const char* programName)
1047{
1048 DISPLAY( "Usage :\n");
1049 DISPLAY( " %s [args]\n", programName);
1050 DISPLAY( "\n");
1051 DISPLAY( "Arguments :\n");
1052 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1053 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1054 DISPLAY( " -t# : Select starting test number (default:0)\n");
1055 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1056 DISPLAY( " -v : verbose\n");
1057 DISPLAY( " -p : pause at the end\n");
1058 DISPLAY( " -h : display help and exit\n");
1059 return 0;
1060}
1061
1062
1063int main(int argc, const char** argv)
1064{
1065 U32 seed=0;
1066 int seedset=0;
1067 int argNb;
1068 int nbTests = nbTestsDefault;
1069 int testNb = 0;
1070 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
1071 int result=0;
Yann Collet19d670b2017-01-19 15:32:07 -08001072 int mainPause = 0;
1073 int mtOnly = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001074 int bigTests = 1;
Yann Collet19d670b2017-01-19 15:32:07 -08001075 const char* const programName = argv[0];
Yann Collet8dafb1a2017-01-25 17:01:13 -08001076 ZSTD_customMem const customMem = { allocFunction, freeFunction, NULL };
1077 ZSTD_customMem const customNULL = { NULL, NULL, NULL };
Yann Colletd7883a22016-08-12 16:48:02 +02001078
1079 /* Check command line */
1080 for(argNb=1; argNb<argc; argNb++) {
1081 const char* argument = argv[argNb];
1082 if(!argument) continue; /* Protection if argument empty */
1083
1084 /* Parsing commands. Aggregated commands are allowed */
1085 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001086
Yann Collet19d670b2017-01-19 15:32:07 -08001087 if (!strcmp(argument, "--mt")) { mtOnly=1; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001088 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001089
1090 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001091 while (*argument!=0) {
1092 switch(*argument)
1093 {
1094 case 'h':
1095 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001096
Yann Colletd7883a22016-08-12 16:48:02 +02001097 case 'v':
1098 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001099 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001100 break;
Yann Collet736788f2017-01-19 12:12:50 -08001101
Yann Colletd7883a22016-08-12 16:48:02 +02001102 case 'q':
1103 argument++;
1104 g_displayLevel--;
1105 break;
Yann Collet736788f2017-01-19 12:12:50 -08001106
Yann Colletd7883a22016-08-12 16:48:02 +02001107 case 'p': /* pause at the end */
1108 argument++;
1109 mainPause = 1;
1110 break;
1111
Yann Collet736788f2017-01-19 12:12:50 -08001112 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001113 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001114 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001115 while ((*argument>='0') && (*argument<='9')) {
1116 nbTests *= 10;
1117 nbTests += *argument - '0';
1118 argument++;
1119 }
1120 break;
1121
Yann Collet736788f2017-01-19 12:12:50 -08001122 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001123 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001124 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001125 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001126 g_clockTime *= 10;
1127 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001128 argument++;
1129 }
Yann Colletef9999f2016-09-01 16:44:48 -07001130 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001131 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001132 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +02001133 break;
1134
Yann Collet736788f2017-01-19 12:12:50 -08001135 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001136 argument++;
1137 seed=0;
1138 seedset=1;
1139 while ((*argument>='0') && (*argument<='9')) {
1140 seed *= 10;
1141 seed += *argument - '0';
1142 argument++;
1143 }
1144 break;
1145
Yann Collet736788f2017-01-19 12:12:50 -08001146 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001147 argument++;
1148 testNb=0;
1149 while ((*argument>='0') && (*argument<='9')) {
1150 testNb *= 10;
1151 testNb += *argument - '0';
1152 argument++;
1153 }
1154 break;
1155
1156 case 'P': /* compressibility % */
1157 argument++;
1158 proba=0;
1159 while ((*argument>='0') && (*argument<='9')) {
1160 proba *= 10;
1161 proba += *argument - '0';
1162 argument++;
1163 }
1164 if (proba<0) proba=0;
1165 if (proba>100) proba=100;
1166 break;
1167
1168 default:
1169 return FUZ_usage(programName);
1170 }
1171 } } } /* for(argNb=1; argNb<argc; argNb++) */
1172
1173 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001174 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001175
Yann Colletef9999f2016-09-01 16:44:48 -07001176 if (!seedset) {
1177 time_t const t = time(NULL);
1178 U32 const h = XXH32(&t, sizeof(t), 1);
1179 seed = h % 10000;
1180 }
1181
Yann Colletd7883a22016-08-12 16:48:02 +02001182 DISPLAY("Seed = %u\n", seed);
1183 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1184
1185 if (nbTests<=0) nbTests=1;
1186
1187 if (testNb==0) {
1188 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
1189 if (!result) {
Yann Collet736788f2017-01-19 12:12:50 -08001190 DISPLAYLEVEL(3, "Unit tests using customMem :\n")
Yann Colletd7883a22016-08-12 16:48:02 +02001191 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
1192 } }
1193
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001194 if (!result && !mtOnly) result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1195 if (!result) result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
Yann Colletd7883a22016-08-12 16:48:02 +02001196
1197 if (mainPause) {
1198 int unused;
1199 DISPLAY("Press Enter \n");
1200 unused = getchar();
1201 (void)unused;
1202 }
1203 return result;
1204}