blob: 5c166bfb8d12d9ca398f3eea3094f2c73cfedb76 [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__)
Yann Collet0be6fd32017-05-08 16:08:01 -070056#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
57 DISPLAY(__VA_ARGS__); \
58 if (g_displayLevel>=4) fflush(stderr); }
Yann Colletd7883a22016-08-12 16:48:02 +020059static U32 g_displayLevel = 2;
60
61#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070062 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
63 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Collet0be6fd32017-05-08 16:08:01 -070064 if (g_displayLevel>=4) fflush(stderr); } }
Yann Colletb3060f72016-09-09 16:44:16 +020065static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070066static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020067
Yann Colletef9999f2016-09-01 16:44:48 -070068static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020069
70
71/*-*******************************************************
72* Fuzzer functions
73*********************************************************/
74#define MAX(a,b) ((a)>(b)?(a):(b))
75
Yann Colletef9999f2016-09-01 16:44:48 -070076static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020077{
Yann Colletef9999f2016-09-01 16:44:48 -070078 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020079}
80
81/*! FUZ_rand() :
82 @return : a 27 bits random value, from a 32-bits `seed`.
83 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070084#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020085unsigned int FUZ_rand(unsigned int* seedPtr)
86{
Yann Collet33fce032017-01-16 19:46:22 -080087 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020088 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080089 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020090 rand32 += prime2;
91 rand32 = FUZ_rotl32(rand32, 13);
92 *seedPtr = rand32;
93 return rand32 >> 5;
94}
95
Yann Colletd7883a22016-08-12 16:48:02 +020096static void* allocFunction(void* opaque, size_t size)
97{
98 void* address = malloc(size);
99 (void)opaque;
100 return address;
101}
102
103static void freeFunction(void* opaque, void* address)
104{
105 (void)opaque;
106 free(address);
107}
108
Yann Colletcb327632016-08-23 00:30:31 +0200109
110/*======================================================
111* Basic Unit tests
112======================================================*/
113
Yann Collet33fce032017-01-16 19:46:22 -0800114typedef struct {
115 void* start;
116 size_t size;
117 size_t filled;
118} buffer_t;
119
120static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
121
122static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
123{
124 buffer_t dict = { NULL, 0, 0 };
125 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
126 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
127 if (!blockSizes) return dict;
128 dict.start = malloc(requestedDictSize);
129 if (!dict.start) { free(blockSizes); return dict; }
130 { size_t nb;
131 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
132 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
133 }
134 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
135 free(blockSizes);
Yann Collet2c5514c2017-04-18 22:52:41 -0700136 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
Yann Collet33fce032017-01-16 19:46:22 -0800137 dict.size = requestedDictSize;
138 dict.filled = dictSize;
139 return dict; /* how to return dictSize ? */
140 }
141}
142
143static void FUZ_freeDictionary(buffer_t dict)
144{
145 free(dict.start);
146}
147
148
Yann Colletd7883a22016-08-12 16:48:02 +0200149static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
150{
Yann Colletb3060f72016-09-09 16:44:16 +0200151 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200152 void* CNBuffer = malloc(CNBufferSize);
153 size_t const skippableFrameSize = 11;
154 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
155 void* compressedBuffer = malloc(compressedBufferSize);
156 size_t const decodedBufferSize = CNBufferSize;
157 void* decodedBuffer = malloc(decodedBufferSize);
158 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200159 int testResult = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700160 U32 testNb = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200161 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
162 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800163 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200164 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800165 buffer_t dictionary = g_nullBuffer;
Yann Collet30ab64e2017-05-10 11:30:19 -0700166 size_t const dictSize = 128 KB;
Yann Collet33fce032017-01-16 19:46:22 -0800167 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200168
169 /* Create compressible test buffer */
170 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800171 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200172 goto _output_error;
173 }
174 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
175
Yann Collet33fce032017-01-16 19:46:22 -0800176 /* Create dictionary */
177 MEM_STATIC_ASSERT(COMPRESSIBLE_NOISE_LENGTH >= 4 MB);
178 dictionary = FUZ_createDictionary(CNBuffer, 4 MB, 4 KB, 40 KB);
179 if (!dictionary.start) {
180 DISPLAY("Error creating dictionary, aborting \n");
181 goto _output_error;
182 }
183 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
184
Yann Colletd7883a22016-08-12 16:48:02 +0200185 /* generate skippable frame */
186 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
187 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
188 cSize = skippableFrameSize + 8;
189
190 /* Basic compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800191 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700192 ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200193 outBuff.dst = (char*)(compressedBuffer)+cSize;
194 outBuff.size = compressedBufferSize;
195 outBuff.pos = 0;
196 inBuff.src = CNBuffer;
197 inBuff.size = CNBufferSize;
198 inBuff.pos = 0;
199 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200200 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200201 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
202 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200203 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200204 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800205 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200206
Yann Collet30ab64e2017-05-10 11:30:19 -0700207 /* context size functions */
208 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
209 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
210 size_t const s = ZSTD_estimateCStreamSize(cParams)
211 + ZSTD_estimateCDictSize(cParams, dictSize); /* uses ZSTD_initCStream_usingDict() */
212 if (ZSTD_isError(s)) goto _output_error;
213 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
214 }
215
216 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200217 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200218 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800219 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200220 }
221
Yann Collet4b987ad2017-04-10 17:50:44 -0700222 /* Attempt bad compression parameters */
223 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
224 { size_t r;
225 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
226 params.cParams.searchLength = 2;
227 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
228 if (!ZSTD_isError(r)) goto _output_error;
229 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
230 }
231
Yann Colletd7883a22016-08-12 16:48:02 +0200232 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800233 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700234 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200235 inBuff.src = compressedBuffer;
236 inBuff.size = cSize;
237 inBuff.pos = 0;
238 outBuff.dst = decodedBuffer;
239 outBuff.size = CNBufferSize;
240 outBuff.pos = 0;
241 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200242 if (r != 0) goto _output_error; }
Yann Colleta33ae642017-02-28 01:15:28 -0800243 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800244 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200245
246 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800247 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800248 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700249 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800250 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000); /* large limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200251 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800252 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
253 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
254 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
255 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800256 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800257
258 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800259 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800260 outBuff.pos = 0;
261 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
262 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 +0200263 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
264 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800265 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200266
267 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800268 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200269 { size_t i;
270 for (i=0; i<CNBufferSize; i++) {
271 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
272 } }
Yann Collet736788f2017-01-19 12:12:50 -0800273 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200274
Yann Colletf16f4492017-05-09 16:18:17 -0700275 /* context size functions */
276 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
277 { ZSTD_frameHeader fhi;
278 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
279 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
280 if (gfhError!=0) goto _output_error;
281 DISPLAYLEVEL(5, " (windowSize : %u) ", fhi.windowSize);
282 { size_t const s = ZSTD_estimateDStreamSize(fhi)
Yann Collet30ab64e2017-05-10 11:30:19 -0700283 + ZSTD_estimateDDictSize(dictSize); /* uses ZSTD_initDStream_usingDict() */
Yann Colletf16f4492017-05-09 16:18:17 -0700284 if (ZSTD_isError(s)) goto _output_error;
285 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
286 } }
287
288 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200289 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200290 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800291 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200292 }
293
Yann Colletd7883a22016-08-12 16:48:02 +0200294 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800295 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200296 { /* skippable frame */
297 size_t r = 1;
Yann Collet30ab64e2017-05-10 11:30:19 -0700298 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200299 inBuff.src = compressedBuffer;
300 outBuff.dst = decodedBuffer;
301 inBuff.pos = 0;
302 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200303 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200304 inBuff.size = inBuff.pos + 1;
305 outBuff.size = outBuff.pos + 1;
306 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200307 if (ZSTD_isError(r)) goto _output_error;
308 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200309 /* normal frame */
Yann Collet30ab64e2017-05-10 11:30:19 -0700310 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200311 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200312 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200313 inBuff.size = inBuff.pos + 1;
314 outBuff.size = outBuff.pos + 1;
315 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200316 if (ZSTD_isError(r)) goto _output_error;
317 }
318 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200319 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
320 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800321 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200322
323 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800324 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200325 { size_t i;
326 for (i=0; i<CNBufferSize; i++) {
327 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
328 } }
Yann Collet736788f2017-01-19 12:12:50 -0800329 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200330
Yann Collete795c8a2016-12-13 16:39:36 +0100331 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800332 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100333 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100334 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100335 outBuff.size = compressedBufferSize;
336 outBuff.pos = 0;
337 inBuff.src = CNBuffer;
338 inBuff.size = CNBufferSize;
339 inBuff.pos = 0;
340 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
341 if (ZSTD_isError(r)) goto _output_error; }
342 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
343 { size_t const r = ZSTD_endStream(zc, &outBuff);
344 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Sean Purcell4e709712017-02-07 13:50:09 -0800345 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100346 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800347 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100348
349 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800350 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100351 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100352 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100353 outBuff.size = compressedBufferSize;
354 outBuff.pos = 0;
355 inBuff.src = CNBuffer;
356 inBuff.size = CNBufferSize;
357 inBuff.pos = 0;
358 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
359 if (ZSTD_isError(r)) goto _output_error; }
360 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
361 { size_t const r = ZSTD_endStream(zc, &outBuff);
362 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800363 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100364
Yann Collet12083a42016-09-06 15:01:51 +0200365 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800366 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200367 ZSTD_freeCStream(zc);
368 zc = ZSTD_createCStream_advanced(customMem);
369 if (zc==NULL) goto _output_error; /* memory allocation issue */
370 /* use 1 */
371 { size_t const inSize = 513;
Yann Collet0be6fd32017-05-08 16:08:01 -0700372 DISPLAYLEVEL(5, "use1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200373 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
374 inBuff.src = CNBuffer;
375 inBuff.size = inSize;
376 inBuff.pos = 0;
377 outBuff.dst = (char*)(compressedBuffer)+cSize;
378 outBuff.size = ZSTD_compressBound(inSize);
379 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700380 DISPLAYLEVEL(5, "compress1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200381 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
382 if (ZSTD_isError(r)) goto _output_error; }
383 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700384 DISPLAYLEVEL(5, "end1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200385 { size_t const r = ZSTD_endStream(zc, &outBuff);
386 if (r != 0) goto _output_error; } /* error, or some data not flushed */
387 }
388 /* use 2 */
389 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
Yann Collet0be6fd32017-05-08 16:08:01 -0700390 DISPLAYLEVEL(5, "use2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200391 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
392 inBuff.src = CNBuffer;
393 inBuff.size = inSize;
394 inBuff.pos = 0;
395 outBuff.dst = (char*)(compressedBuffer)+cSize;
396 outBuff.size = ZSTD_compressBound(inSize);
397 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700398 DISPLAYLEVEL(5, "compress2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200399 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
400 if (ZSTD_isError(r)) goto _output_error; }
401 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700402 DISPLAYLEVEL(5, "end2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200403 { size_t const r = ZSTD_endStream(zc, &outBuff);
404 if (r != 0) goto _output_error; } /* error, or some data not flushed */
405 }
Yann Collet736788f2017-01-19 12:12:50 -0800406 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200407
Yann Collet95162342016-10-25 16:19:52 -0700408 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800409 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet33fce032017-01-16 19:46:22 -0800410 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1);
Yann Collet95162342016-10-25 16:19:52 -0700411 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
412 if (ZSTD_isError(initError)) goto _output_error;
413 cSize = 0;
414 outBuff.dst = compressedBuffer;
415 outBuff.size = compressedBufferSize;
416 outBuff.pos = 0;
417 inBuff.src = CNBuffer;
418 inBuff.size = CNBufferSize;
419 inBuff.pos = 0;
420 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
421 if (ZSTD_isError(r)) goto _output_error; }
422 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
423 { size_t const r = ZSTD_endStream(zc, &outBuff);
424 if (r != 0) goto _output_error; } /* error, or some data not flushed */
425 cSize = outBuff.pos;
426 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800427 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700428 }
429
Yann Collet736788f2017-01-19 12:12:50 -0800430 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200431 { size_t const s = ZSTD_sizeof_CStream(zc);
432 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800433 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200434 }
435
Yann Collet33fce032017-01-16 19:46:22 -0800436 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
437 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
438 if (dID != dictID) goto _output_error;
439 DISPLAYLEVEL(4, "OK (%u) \n", dID);
440 }
441
Yann Collet335ad5d2016-10-25 17:47:02 -0700442 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800443 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800444 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700445 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
446 if (ZSTD_isError(initError)) goto _output_error;
447 inBuff.src = compressedBuffer;
448 inBuff.size = cSize;
449 inBuff.pos = 0;
450 outBuff.dst = decodedBuffer;
451 outBuff.size = CNBufferSize;
452 outBuff.pos = 0;
453 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
454 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
455 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
456 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
457 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800458 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700459 }
460
Yann Collet12083a42016-09-06 15:01:51 +0200461 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800462 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200463 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
464 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800465 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200466
467 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800468 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700469 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800470 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000); /* too small limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200471 if (ZSTD_isError(r)) goto _output_error; }
472 inBuff.src = compressedBuffer;
473 inBuff.size = cSize;
474 inBuff.pos = 0;
475 outBuff.dst = decodedBuffer;
476 outBuff.size = CNBufferSize;
477 outBuff.pos = 0;
478 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
479 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800480 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200481
Yann Collet7d283cd2017-04-27 14:48:34 -0700482 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
483 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
484 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
Yann Colleta92cbb72017-04-27 15:08:56 -0700485 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, 1 /* byReference */, cParams, customMem);
Yann Collet7d283cd2017-04-27 14:48:34 -0700486 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, CNBufferSize, fParams);
487 if (ZSTD_isError(initError)) goto _output_error;
Nick Terrell62ecad32017-04-03 20:56:39 -0700488 cSize = 0;
489 outBuff.dst = compressedBuffer;
490 outBuff.size = compressedBufferSize;
491 outBuff.pos = 0;
492 inBuff.src = CNBuffer;
493 inBuff.size = CNBufferSize;
494 inBuff.pos = 0;
495 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
496 if (ZSTD_isError(r)) goto _output_error; }
497 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
498 { size_t const r = ZSTD_endStream(zc, &outBuff);
499 if (r != 0) goto _output_error; } /* error, or some data not flushed */
500 cSize = outBuff.pos;
501 ZSTD_freeCDict(cdict);
502 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet4ee6b152017-04-11 11:59:44 -0700503 }
Nick Terrell62ecad32017-04-03 20:56:39 -0700504
Yann Colleta92cbb72017-04-27 15:08:56 -0700505 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
506 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
507 if (did != 0) goto _output_error;
508 }
509 DISPLAYLEVEL(3, "OK (not detected) \n");
510
Yann Collet4ee6b152017-04-11 11:59:44 -0700511 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
512 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
513 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
514 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
Nick Terrell62ecad32017-04-03 20:56:39 -0700515 }
516
Yann Collet0bb381d2017-04-18 15:08:52 -0700517 /* Empty srcSize */
518 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
519 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
520 params.fParams.contentSizeFlag = 1;
521 ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0);
522 } /* cstream advanced shall write content size = 0 */
523 inBuff.src = CNBuffer;
524 inBuff.size = 0;
525 inBuff.pos = 0;
526 outBuff.dst = compressedBuffer;
527 outBuff.size = compressedBufferSize;
528 outBuff.pos = 0;
529 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
530 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
531 cSize = outBuff.pos;
532 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
533 DISPLAYLEVEL(3, "OK \n");
534
Sean Purcell2db72492017-02-09 10:50:43 -0800535 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
536 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
537 params.fParams.contentSizeFlag = 1;
Yann Collet4ee6b152017-04-11 11:59:44 -0700538 ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
539 } /* cstream advanced shall write content size = 0 */
Sean Purcell2db72492017-02-09 10:50:43 -0800540 inBuff.src = CNBuffer;
541 inBuff.size = 0;
542 inBuff.pos = 0;
543 outBuff.dst = compressedBuffer;
544 outBuff.size = compressedBufferSize;
545 outBuff.pos = 0;
546 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
547 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
548 cSize = outBuff.pos;
549 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
550
551 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
552 inBuff.src = CNBuffer;
553 inBuff.size = 0;
554 inBuff.pos = 0;
555 outBuff.dst = compressedBuffer;
556 outBuff.size = compressedBufferSize;
557 outBuff.pos = 0;
558 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
559 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
560 cSize = outBuff.pos;
561 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
562 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200563
Sean Purcell887eaa92017-02-15 16:43:45 -0800564 /* Overlen overwriting window data bug */
565 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800566 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
567 1. 'a' repeated 517 times
568 2. 'b' repeated 516 times
569 3. a compressed block with no literals and 3 sequence commands:
570 litlength = 0, offset = 24, match length = 24
571 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
572 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
573
574 const char* testCase =
575 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
576 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
577 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Sean Purcell887eaa92017-02-15 16:43:45 -0800578 ZSTD_DStream* zds = ZSTD_createDStream();
579
580 ZSTD_initDStream(zds);
581 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800582 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800583 inBuff.pos = 0;
584 outBuff.dst = decodedBuffer;
585 outBuff.size = CNBufferSize;
586 outBuff.pos = 0;
587
588 while (inBuff.pos < inBuff.size) {
589 size_t const r = ZSTD_decompressStream(zds, &outBuff, &inBuff);
590 /* Bug will cause checksum to fail */
591 if (ZSTD_isError(r)) goto _output_error;
592 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100593
594 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800595 }
596 DISPLAYLEVEL(3, "OK \n");
597
Yann Colletd7883a22016-08-12 16:48:02 +0200598_end:
Yann Collet33fce032017-01-16 19:46:22 -0800599 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200600 ZSTD_freeCStream(zc);
601 ZSTD_freeDStream(zd);
602 free(CNBuffer);
603 free(compressedBuffer);
604 free(decodedBuffer);
605 return testResult;
606
607_output_error:
608 testResult = 1;
609 DISPLAY("Error detected in Unit tests ! \n");
610 goto _end;
611}
612
613
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200614/* ====== Fuzzer tests ====== */
615
Yann Colletd7883a22016-08-12 16:48:02 +0200616static size_t findDiff(const void* buf1, const void* buf2, size_t max)
617{
618 const BYTE* b1 = (const BYTE*)buf1;
619 const BYTE* b2 = (const BYTE*)buf2;
620 size_t u;
621 for (u=0; u<max; u++) {
622 if (b1[u] != b2[u]) break;
623 }
Yann Collet736788f2017-01-19 12:12:50 -0800624 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800625 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
626 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]);
627 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
628 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 +0200629 return u;
630}
631
632static size_t FUZ_rLogLength(U32* seed, U32 logLength)
633{
634 size_t const lengthMask = ((size_t)1 << logLength) - 1;
635 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
636}
637
638static size_t FUZ_randomLength(U32* seed, U32 maxLog)
639{
640 U32 const logLength = FUZ_rand(seed) % maxLog;
641 return FUZ_rLogLength(seed, logLength);
642}
643
644#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
645
646#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
647 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
648
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700649static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200650{
651 static const U32 maxSrcLog = 24;
652 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200653 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200654 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200655 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
656 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
657 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
658 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
659 size_t const dstBufferSize = srcBufferSize;
660 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200661 U32 result = 0;
662 U32 testNb = 0;
663 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200664 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
665 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
666 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
667 clock_t const startClock = clock();
668 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200669 size_t dictSize = 0;
670 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700671 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Colletd7883a22016-08-12 16:48:02 +0200672
673 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200674 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
675 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
676 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
677 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
678 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200679 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200680 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200681 "Not enough memory, fuzzer tests cancelled");
682
683 /* Create initial samples */
684 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
685 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
686 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
687 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
688 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
689 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200690 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200691
692 /* catch up testNb */
693 for (testNb=1; testNb < startTest; testNb++)
694 FUZ_rand(&coreSeed);
695
696 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700697 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200698 U32 lseed;
699 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200700 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200701 XXH64_state_t xxhState;
702 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200703 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200704 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200705
706 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700707 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
708 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200709 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800710 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200711
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200712 /* states full reset (deliberately not synchronized) */
713 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200714 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
715 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 +0200716
717 /* srcBuffer selection [0-4] */
718 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
719 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
720 else {
721 buffNb >>= 3;
722 if (buffNb & 7) {
723 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
724 buffNb = tnb[buffNb >> 3];
725 } else {
726 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
727 buffNb = tnb[buffNb >> 3];
728 } }
729 srcBuffer = cNoiseBuffer[buffNb];
730 }
731
732 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200733 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
734 && oldTestLog /* at least one test happened */ && resetAllowed) {
735 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
Yann Colletce800982017-04-05 16:34:09 -0700736 if (maxTestSize >= srcBufferSize)
737 maxTestSize = srcBufferSize-1;
Yann Colletcf409a72016-09-26 16:41:05 +0200738 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
739 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
740 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
741 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200742 } else {
743 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700744 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletce800982017-04-05 16:34:09 -0700745 U32 const cLevel = ( FUZ_rand(&lseed) %
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700746 (ZSTD_maxCLevel() -
Yann Colletce800982017-04-05 16:34:09 -0700747 (MAX(testLog, dictLog) / cLevelLimiter)))
748 + 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200749 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200750 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200751 /* random dictionary selection */
Yann Colletce800982017-04-05 16:34:09 -0700752 dictSize = ((FUZ_rand(&lseed)&1)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200753 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
754 dict = srcBuffer + dictStart;
755 }
Yann Colletcf409a72016-09-26 16:41:05 +0200756 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
757 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200758 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
759 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200760 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200761 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200762 } } }
763
764 /* multi-segments compression test */
765 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200766 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200767 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200768 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100769 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200770 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
771 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
772 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200773 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
774 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200775 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200776 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200777
Yann Collet53e17fb2016-08-17 01:39:22 +0200778 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200779 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200780
Yann Collet53e17fb2016-08-17 01:39:22 +0200781 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
782 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
783 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200784 }
785
786 /* random flush operation, to mess around */
787 if ((FUZ_rand(&lseed) & 15) == 0) {
788 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200789 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
790 outBuff.size = outBuff.pos + adjustedDstSize;
791 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200792 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
793 } } }
794
795 /* final frame epilogue */
796 { size_t remainingToFlush = (size_t)(-1);
797 while (remainingToFlush) {
798 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
799 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
800 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200801 outBuff.size = outBuff.pos + adjustedDstSize;
802 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200803 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet0be6fd32017-05-08 16:08:01 -0700804 CHECK (enoughDstSize && remainingToFlush,
805 "ZSTD_endStream() not fully flushed (%u remaining), but enough space available (%u)",
806 (U32)remainingToFlush, (U32)adjustedDstSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200807 } }
808 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200809 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200810 }
811
812 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200813 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700814 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800815 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200816 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800817 }
Yann Colletd7883a22016-08-12 16:48:02 +0200818 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200819 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
820 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
821 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200822 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
823 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
824 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200825 inBuff.size = inBuff.pos + readCSrcSize;
826 outBuff.size = inBuff.pos + dstBuffSize;
827 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200828 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200829 }
830 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200831 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
832 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200833 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
834 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
835 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
836 } }
837
838 /*===== noisy/erroneous src decompression test =====*/
839
840 /* add some noise */
841 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
842 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
843 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
844 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
845 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
846 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
847 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
848 } }
849
850 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200851 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200852 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
853 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
854 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200855 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
856 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200857 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -0800858 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200859 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -0800860 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200861 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200862 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -0800863 /* No forward progress possible */
864 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +0200865 } } } }
866 DISPLAY("\r%u fuzzer tests completed \n", testNb);
867
868_cleanup:
869 ZSTD_freeCStream(zc);
870 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200871 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200872 free(cNoiseBuffer[0]);
873 free(cNoiseBuffer[1]);
874 free(cNoiseBuffer[2]);
875 free(cNoiseBuffer[3]);
876 free(cNoiseBuffer[4]);
877 free(copyBuffer);
878 free(cBuffer);
879 free(dstBuffer);
880 return result;
881
882_output_error:
883 result = 1;
884 goto _cleanup;
885}
886
887
Yann Collet736788f2017-01-19 12:12:50 -0800888/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700889static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -0800890{
891 static const U32 maxSrcLog = 24;
892 static const U32 maxSampleLog = 19;
893 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
894 BYTE* cNoiseBuffer[5];
895 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
896 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
897 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
898 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
899 size_t const dstBufferSize = srcBufferSize;
900 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
901 U32 result = 0;
902 U32 testNb = 0;
903 U32 coreSeed = seed;
904 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(2); /* will be reset sometimes */
905 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
906 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
907 clock_t const startClock = clock();
908 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
909 size_t dictSize = 0;
910 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700911 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Collet736788f2017-01-19 12:12:50 -0800912
913 /* allocations */
914 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
915 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
916 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
917 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
918 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
919 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
920 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
921 "Not enough memory, fuzzer tests cancelled");
922
923 /* Create initial samples */
924 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
925 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
926 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
927 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
928 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
929 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
930 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
931
932 /* catch up testNb */
933 for (testNb=1; testNb < startTest; testNb++)
934 FUZ_rand(&coreSeed);
935
936 /* test loop */
937 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
938 U32 lseed;
939 const BYTE* srcBuffer;
940 size_t totalTestSize, totalGenSize, cSize;
941 XXH64_state_t xxhState;
942 U64 crcOrig;
943 U32 resetAllowed = 1;
944 size_t maxTestSize;
945
946 /* init */
947 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
948 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
949 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -0800950 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -0800951
952 /* states full reset (deliberately not synchronized) */
953 /* some issues can only happen when reusing states */
954 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
955 U32 const nbThreads = (FUZ_rand(&lseed) % 6) + 1;
Yann Collet30c76982017-03-31 18:27:03 -0700956 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
Yann Collet736788f2017-01-19 12:12:50 -0800957 ZSTDMT_freeCCtx(zc);
958 zc = ZSTDMT_createCCtx(nbThreads);
959 resetAllowed=0;
960 }
961 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
962 ZSTD_freeDStream(zd);
963 zd = ZSTD_createDStream();
964 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
965 }
966
967 /* srcBuffer selection [0-4] */
968 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
969 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
970 else {
971 buffNb >>= 3;
972 if (buffNb & 7) {
973 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
974 buffNb = tnb[buffNb >> 3];
975 } else {
976 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
977 buffNb = tnb[buffNb >> 3];
978 } }
979 srcBuffer = cNoiseBuffer[buffNb];
980 }
981
982 /* compression init */
983 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
984 && oldTestLog /* at least one test happened */ && resetAllowed) {
985 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
986 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
987 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
988 size_t const resetError = ZSTDMT_initCStream(zc, compressionLevel);
Yann Collet19d670b2017-01-19 15:32:07 -0800989 CHECK(ZSTD_isError(resetError), "ZSTDMT_initCStream error : %s", ZSTD_getErrorName(resetError));
Yann Collet736788f2017-01-19 12:12:50 -0800990 }
991 } else {
992 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700993 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700994 U32 const cLevel = (FUZ_rand(&lseed) %
995 (ZSTD_maxCLevel() -
996 (MAX(testLog, dictLog) / cLevelLimiter))) +
997 1;
Yann Collet736788f2017-01-19 12:12:50 -0800998 maxTestSize = FUZ_rLogLength(&lseed, testLog);
999 oldTestLog = testLog;
1000 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -07001001 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001002 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1003 dict = srcBuffer + dictStart;
1004 }
1005 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
1006 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletce800982017-04-05 16:34:09 -07001007 DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n",
1008 params.cParams.windowLog, (U32)pledgedSrcSize);
Yann Collet19d670b2017-01-19 15:32:07 -08001009 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1010 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletce800982017-04-05 16:34:09 -07001011 params.fParams.contentSizeFlag = pledgedSrcSize>0;
Yann Collet2c5514c2017-04-18 22:52:41 -07001012 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
Yann Colletcd23dd22017-01-30 12:46:35 -08001013 { size_t const initError = ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
1014 CHECK (ZSTD_isError(initError),"ZSTDMT_initCStream_advanced error : %s", ZSTD_getErrorName(initError)); }
1015 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12);
Yann Collet92c98a52017-01-30 12:50:31 -08001016 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1));
Yann Colletcd23dd22017-01-30 12:46:35 -08001017 } }
Yann Collet736788f2017-01-19 12:12:50 -08001018
1019 /* multi-segments compression test */
1020 XXH64_reset(&xxhState, 0);
1021 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1022 U32 n;
1023 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1024 /* compress random chunks into randomly sized dst buffers */
1025 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1026 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1027 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1028 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1029 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1030 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1031 outBuff.size = outBuff.pos + dstBuffSize;
1032
1033 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
1034 { size_t const compressionError = ZSTDMT_compressStream(zc, &outBuff, &inBuff);
1035 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Collet19d670b2017-01-19 15:32:07 -08001036 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -08001037
1038 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1039 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1040 totalTestSize += inBuff.pos;
1041 }
1042
1043 /* random flush operation, to mess around */
1044 if ((FUZ_rand(&lseed) & 15) == 0) {
1045 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1046 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1047 outBuff.size = outBuff.pos + adjustedDstSize;
1048 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
1049 { size_t const flushError = ZSTDMT_flushStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001050 CHECK (ZSTD_isError(flushError), "ZSTDMT_flushStream error : %s", ZSTD_getErrorName(flushError));
Yann Collet736788f2017-01-19 12:12:50 -08001051 } } }
1052
1053 /* final frame epilogue */
1054 { size_t remainingToFlush = (size_t)(-1);
1055 while (remainingToFlush) {
1056 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1057 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1058 outBuff.size = outBuff.pos + adjustedDstSize;
1059 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1060 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001061 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet736788f2017-01-19 12:12:50 -08001062 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1063 } }
Yann Collet736788f2017-01-19 12:12:50 -08001064 crcOrig = XXH64_digest(&xxhState);
1065 cSize = outBuff.pos;
Yann Collet2c5514c2017-04-18 22:52:41 -07001066 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001067 }
1068
1069 /* multi - fragments decompression test */
1070 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1071 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
1072 } else {
1073 ZSTD_initDStream_usingDict(zd, dict, dictSize);
1074 }
1075 { size_t decompressionResult = 1;
1076 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1077 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1078 for (totalGenSize = 0 ; decompressionResult ; ) {
1079 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1080 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1081 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1082 inBuff.size = inBuff.pos + readCSrcSize;
1083 outBuff.size = inBuff.pos + dstBuffSize;
Yann Colletce800982017-04-05 16:34:09 -07001084 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001085 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1086 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Collet2c5514c2017-04-18 22:52:41 -07001087 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001088 }
Yann Colletbb002742017-01-25 16:25:38 -08001089 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1090 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001091 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1092 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1093 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1094 } }
1095
1096 /*===== noisy/erroneous src decompression test =====*/
1097
1098 /* add some noise */
1099 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1100 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1101 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1102 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1103 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1104 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1105 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1106 } }
1107
1108 /* try decompression on noisy data */
1109 ZSTD_initDStream(zd_noise); /* note : no dictionary */
1110 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1111 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1112 while (outBuff.pos < dstBufferSize) {
1113 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1114 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1115 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001116 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001117 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001118 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001119 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1120 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001121 /* No forward progress possible */
1122 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001123 } } } }
1124 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1125
1126_cleanup:
1127 ZSTDMT_freeCCtx(zc);
1128 ZSTD_freeDStream(zd);
1129 ZSTD_freeDStream(zd_noise);
1130 free(cNoiseBuffer[0]);
1131 free(cNoiseBuffer[1]);
1132 free(cNoiseBuffer[2]);
1133 free(cNoiseBuffer[3]);
1134 free(cNoiseBuffer[4]);
1135 free(copyBuffer);
1136 free(cBuffer);
1137 free(dstBuffer);
1138 return result;
1139
1140_output_error:
1141 result = 1;
1142 goto _cleanup;
1143}
1144
1145
Yann Colletd7883a22016-08-12 16:48:02 +02001146/*-*******************************************************
1147* Command line
1148*********************************************************/
1149int FUZ_usage(const char* programName)
1150{
1151 DISPLAY( "Usage :\n");
1152 DISPLAY( " %s [args]\n", programName);
1153 DISPLAY( "\n");
1154 DISPLAY( "Arguments :\n");
1155 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1156 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1157 DISPLAY( " -t# : Select starting test number (default:0)\n");
1158 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1159 DISPLAY( " -v : verbose\n");
1160 DISPLAY( " -p : pause at the end\n");
1161 DISPLAY( " -h : display help and exit\n");
1162 return 0;
1163}
1164
1165
1166int main(int argc, const char** argv)
1167{
1168 U32 seed=0;
1169 int seedset=0;
1170 int argNb;
1171 int nbTests = nbTestsDefault;
1172 int testNb = 0;
1173 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
1174 int result=0;
Yann Collet19d670b2017-01-19 15:32:07 -08001175 int mainPause = 0;
1176 int mtOnly = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001177 int bigTests = 1;
Yann Collet19d670b2017-01-19 15:32:07 -08001178 const char* const programName = argv[0];
Yann Collet8dafb1a2017-01-25 17:01:13 -08001179 ZSTD_customMem const customMem = { allocFunction, freeFunction, NULL };
1180 ZSTD_customMem const customNULL = { NULL, NULL, NULL };
Yann Colletd7883a22016-08-12 16:48:02 +02001181
1182 /* Check command line */
1183 for(argNb=1; argNb<argc; argNb++) {
1184 const char* argument = argv[argNb];
1185 if(!argument) continue; /* Protection if argument empty */
1186
1187 /* Parsing commands. Aggregated commands are allowed */
1188 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001189
Yann Collet19d670b2017-01-19 15:32:07 -08001190 if (!strcmp(argument, "--mt")) { mtOnly=1; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001191 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001192
1193 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001194 while (*argument!=0) {
1195 switch(*argument)
1196 {
1197 case 'h':
1198 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001199
Yann Colletd7883a22016-08-12 16:48:02 +02001200 case 'v':
1201 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001202 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001203 break;
Yann Collet736788f2017-01-19 12:12:50 -08001204
Yann Colletd7883a22016-08-12 16:48:02 +02001205 case 'q':
1206 argument++;
1207 g_displayLevel--;
1208 break;
Yann Collet736788f2017-01-19 12:12:50 -08001209
Yann Colletd7883a22016-08-12 16:48:02 +02001210 case 'p': /* pause at the end */
1211 argument++;
1212 mainPause = 1;
1213 break;
1214
Yann Collet736788f2017-01-19 12:12:50 -08001215 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001216 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001217 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001218 while ((*argument>='0') && (*argument<='9')) {
1219 nbTests *= 10;
1220 nbTests += *argument - '0';
1221 argument++;
1222 }
1223 break;
1224
Yann Collet736788f2017-01-19 12:12:50 -08001225 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001226 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001227 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001228 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001229 g_clockTime *= 10;
1230 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001231 argument++;
1232 }
Yann Colletef9999f2016-09-01 16:44:48 -07001233 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001234 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001235 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +02001236 break;
1237
Yann Collet736788f2017-01-19 12:12:50 -08001238 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001239 argument++;
1240 seed=0;
1241 seedset=1;
1242 while ((*argument>='0') && (*argument<='9')) {
1243 seed *= 10;
1244 seed += *argument - '0';
1245 argument++;
1246 }
1247 break;
1248
Yann Collet736788f2017-01-19 12:12:50 -08001249 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001250 argument++;
1251 testNb=0;
1252 while ((*argument>='0') && (*argument<='9')) {
1253 testNb *= 10;
1254 testNb += *argument - '0';
1255 argument++;
1256 }
1257 break;
1258
1259 case 'P': /* compressibility % */
1260 argument++;
1261 proba=0;
1262 while ((*argument>='0') && (*argument<='9')) {
1263 proba *= 10;
1264 proba += *argument - '0';
1265 argument++;
1266 }
1267 if (proba<0) proba=0;
1268 if (proba>100) proba=100;
1269 break;
1270
1271 default:
1272 return FUZ_usage(programName);
1273 }
1274 } } } /* for(argNb=1; argNb<argc; argNb++) */
1275
1276 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001277 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001278
Yann Colletef9999f2016-09-01 16:44:48 -07001279 if (!seedset) {
1280 time_t const t = time(NULL);
1281 U32 const h = XXH32(&t, sizeof(t), 1);
1282 seed = h % 10000;
1283 }
1284
Yann Colletd7883a22016-08-12 16:48:02 +02001285 DISPLAY("Seed = %u\n", seed);
1286 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1287
1288 if (nbTests<=0) nbTests=1;
1289
1290 if (testNb==0) {
1291 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
1292 if (!result) {
Yann Collet736788f2017-01-19 12:12:50 -08001293 DISPLAYLEVEL(3, "Unit tests using customMem :\n")
Yann Colletd7883a22016-08-12 16:48:02 +02001294 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
1295 } }
1296
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001297 if (!result && !mtOnly) result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1298 if (!result) result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
Yann Colletd7883a22016-08-12 16:48:02 +02001299
1300 if (mainPause) {
1301 int unused;
1302 DISPLAY("Press Enter \n");
1303 unused = getchar();
1304 (void)unused;
1305 }
1306 return result;
1307}