blob: 7229f7031e14e53f1f4abc2e2e5950a24a4be3bf [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 */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070015# 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 */
Yann Colletd7883a22016-08-12 16:48:02 +020018#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 */
Yann Collet01743a32017-06-16 17:56:41 -070028#include <assert.h> /* assert */
Yann Colletd7883a22016-08-12 16:48:02 +020029#include "mem.h"
Yann Colleta5ffe3d2017-05-12 16:29:19 -070030#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
Yann Colletd7883a22016-08-12 16:48:02 +020031#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010032#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Collet736788f2017-01-19 12:12:50 -080033#include "zstdmt_compress.h"
Yann Collet33fce032017-01-16 19:46:22 -080034#include "zdict.h" /* ZDICT_trainFromBuffer */
Yann Colletd7883a22016-08-12 16:48:02 +020035#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070036#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020037#include "xxhash.h" /* XXH64_* */
38
39
40/*-************************************
41* Constants
42**************************************/
43#define KB *(1U<<10)
44#define MB *(1U<<20)
45#define GB *(1U<<30)
46
47static const U32 nbTestsDefault = 10000;
48#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
49#define FUZ_COMPRESSIBILITY_DEFAULT 50
Yann Collet33fce032017-01-16 19:46:22 -080050static const U32 prime32 = 2654435761U;
Yann Colletd7883a22016-08-12 16:48:02 +020051
52
Yann Colletd7883a22016-08-12 16:48:02 +020053/*-************************************
54* Display Macros
55**************************************/
56#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet0be6fd32017-05-08 16:08:01 -070057#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
58 DISPLAY(__VA_ARGS__); \
59 if (g_displayLevel>=4) fflush(stderr); }
Yann Colletd7883a22016-08-12 16:48:02 +020060static U32 g_displayLevel = 2;
61
62#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070063 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
64 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Collet0be6fd32017-05-08 16:08:01 -070065 if (g_displayLevel>=4) fflush(stderr); } }
Yann Colletb3060f72016-09-09 16:44:16 +020066static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070067static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020068
Yann Colletef9999f2016-09-01 16:44:48 -070069static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020070
71
72/*-*******************************************************
73* Fuzzer functions
74*********************************************************/
75#define MAX(a,b) ((a)>(b)?(a):(b))
76
Yann Colletef9999f2016-09-01 16:44:48 -070077static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020078{
Yann Colletef9999f2016-09-01 16:44:48 -070079 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020080}
81
82/*! FUZ_rand() :
83 @return : a 27 bits random value, from a 32-bits `seed`.
84 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070085#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020086unsigned int FUZ_rand(unsigned int* seedPtr)
87{
Yann Collet33fce032017-01-16 19:46:22 -080088 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020089 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080090 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020091 rand32 += prime2;
92 rand32 = FUZ_rotl32(rand32, 13);
93 *seedPtr = rand32;
94 return rand32 >> 5;
95}
96
Yann Colletd7883a22016-08-12 16:48:02 +020097static void* allocFunction(void* opaque, size_t size)
98{
99 void* address = malloc(size);
100 (void)opaque;
101 return address;
102}
103
104static void freeFunction(void* opaque, void* address)
105{
106 (void)opaque;
107 free(address);
108}
109
Yann Colletcb327632016-08-23 00:30:31 +0200110
111/*======================================================
112* Basic Unit tests
113======================================================*/
114
Yann Collet33fce032017-01-16 19:46:22 -0800115typedef struct {
116 void* start;
117 size_t size;
118 size_t filled;
119} buffer_t;
120
121static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
122
123static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
124{
125 buffer_t dict = { NULL, 0, 0 };
126 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
127 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
128 if (!blockSizes) return dict;
129 dict.start = malloc(requestedDictSize);
130 if (!dict.start) { free(blockSizes); return dict; }
131 { size_t nb;
132 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
133 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
134 }
135 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
136 free(blockSizes);
Yann Collet2c5514c2017-04-18 22:52:41 -0700137 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
Yann Collet33fce032017-01-16 19:46:22 -0800138 dict.size = requestedDictSize;
139 dict.filled = dictSize;
140 return dict; /* how to return dictSize ? */
141 }
142}
143
144static void FUZ_freeDictionary(buffer_t dict)
145{
146 free(dict.start);
147}
148
149
Yann Colletd7883a22016-08-12 16:48:02 +0200150static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
151{
Yann Colletb3060f72016-09-09 16:44:16 +0200152 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200153 void* CNBuffer = malloc(CNBufferSize);
154 size_t const skippableFrameSize = 11;
155 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
156 void* compressedBuffer = malloc(compressedBufferSize);
157 size_t const decodedBufferSize = CNBufferSize;
158 void* decodedBuffer = malloc(decodedBufferSize);
159 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200160 int testResult = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700161 U32 testNb = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200162 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
163 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800164 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200165 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800166 buffer_t dictionary = g_nullBuffer;
Yann Collet30ab64e2017-05-10 11:30:19 -0700167 size_t const dictSize = 128 KB;
Yann Collet33fce032017-01-16 19:46:22 -0800168 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200169
170 /* Create compressible test buffer */
171 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800172 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200173 goto _output_error;
174 }
175 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
176
Yann Collet33fce032017-01-16 19:46:22 -0800177 /* Create dictionary */
178 MEM_STATIC_ASSERT(COMPRESSIBLE_NOISE_LENGTH >= 4 MB);
179 dictionary = FUZ_createDictionary(CNBuffer, 4 MB, 4 KB, 40 KB);
180 if (!dictionary.start) {
181 DISPLAY("Error creating dictionary, aborting \n");
182 goto _output_error;
183 }
184 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
185
Yann Colletd7883a22016-08-12 16:48:02 +0200186 /* generate skippable frame */
187 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
188 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
189 cSize = skippableFrameSize + 8;
190
191 /* Basic compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800192 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet009d6042017-05-19 10:17:59 -0700193 { size_t const r = ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1);
194 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200195 outBuff.dst = (char*)(compressedBuffer)+cSize;
196 outBuff.size = compressedBufferSize;
197 outBuff.pos = 0;
198 inBuff.src = CNBuffer;
199 inBuff.size = CNBufferSize;
200 inBuff.pos = 0;
201 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200202 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200203 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
204 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200205 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200206 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800207 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200208
Yann Collet30ab64e2017-05-10 11:30:19 -0700209 /* context size functions */
210 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
211 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
212 size_t const s = ZSTD_estimateCStreamSize(cParams)
Yann Collet25989e32017-05-25 15:07:37 -0700213 /* uses ZSTD_initCStream_usingDict() */
214 + ZSTD_estimateCDictSize(cParams, dictSize, 0);
Yann Collet30ab64e2017-05-10 11:30:19 -0700215 if (ZSTD_isError(s)) goto _output_error;
216 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
217 }
218
219 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200220 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200221 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800222 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200223 }
224
Yann Collet4b987ad2017-04-10 17:50:44 -0700225 /* Attempt bad compression parameters */
226 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
227 { size_t r;
228 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
229 params.cParams.searchLength = 2;
230 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
231 if (!ZSTD_isError(r)) goto _output_error;
232 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
233 }
234
Yann Colletd7883a22016-08-12 16:48:02 +0200235 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800236 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700237 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200238 inBuff.src = compressedBuffer;
239 inBuff.size = cSize;
240 inBuff.pos = 0;
241 outBuff.dst = decodedBuffer;
242 outBuff.size = CNBufferSize;
243 outBuff.pos = 0;
244 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200245 if (r != 0) goto _output_error; }
Yann Colleta33ae642017-02-28 01:15:28 -0800246 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800247 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200248
249 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800250 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800251 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700252 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800253 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000); /* large limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200254 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800255 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
256 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
257 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
258 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800259 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800260
261 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800262 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800263 outBuff.pos = 0;
264 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
265 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 +0200266 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
267 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800268 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200269
270 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800271 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200272 { size_t i;
273 for (i=0; i<CNBufferSize; i++) {
274 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
275 } }
Yann Collet736788f2017-01-19 12:12:50 -0800276 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200277
Yann Colletf16f4492017-05-09 16:18:17 -0700278 /* context size functions */
279 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
280 { ZSTD_frameHeader fhi;
281 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
282 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
283 if (gfhError!=0) goto _output_error;
284 DISPLAYLEVEL(5, " (windowSize : %u) ", fhi.windowSize);
285 { size_t const s = ZSTD_estimateDStreamSize(fhi)
Yann Collet25989e32017-05-25 15:07:37 -0700286 /* uses ZSTD_initDStream_usingDict() */
287 + ZSTD_estimateDDictSize(dictSize, 0);
Yann Colletf16f4492017-05-09 16:18:17 -0700288 if (ZSTD_isError(s)) goto _output_error;
289 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
290 } }
291
292 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200293 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200294 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800295 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200296 }
297
Yann Colletd7883a22016-08-12 16:48:02 +0200298 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800299 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200300 { /* skippable frame */
301 size_t r = 1;
Yann Collet30ab64e2017-05-10 11:30:19 -0700302 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200303 inBuff.src = compressedBuffer;
304 outBuff.dst = decodedBuffer;
305 inBuff.pos = 0;
306 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200307 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200308 inBuff.size = inBuff.pos + 1;
309 outBuff.size = outBuff.pos + 1;
310 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200311 if (ZSTD_isError(r)) goto _output_error;
312 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200313 /* normal frame */
Yann Collet30ab64e2017-05-10 11:30:19 -0700314 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200315 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200316 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200317 inBuff.size = inBuff.pos + 1;
318 outBuff.size = outBuff.pos + 1;
319 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200320 if (ZSTD_isError(r)) goto _output_error;
321 }
322 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200323 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
324 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800325 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200326
327 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800328 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200329 { size_t i;
330 for (i=0; i<CNBufferSize; i++) {
331 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
332 } }
Yann Collet736788f2017-01-19 12:12:50 -0800333 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200334
Yann Collete795c8a2016-12-13 16:39:36 +0100335 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800336 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100337 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100338 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100339 outBuff.size = compressedBufferSize;
340 outBuff.pos = 0;
341 inBuff.src = CNBuffer;
342 inBuff.size = CNBufferSize;
343 inBuff.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 */
Sean Purcell4e709712017-02-07 13:50:09 -0800349 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100350 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800351 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100352
353 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800354 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100355 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100356 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100357 outBuff.size = compressedBufferSize;
358 outBuff.pos = 0;
359 inBuff.src = CNBuffer;
360 inBuff.size = CNBufferSize;
361 inBuff.pos = 0;
362 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
363 if (ZSTD_isError(r)) goto _output_error; }
364 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
365 { size_t const r = ZSTD_endStream(zc, &outBuff);
366 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800367 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100368
Yann Collet12083a42016-09-06 15:01:51 +0200369 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800370 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200371 ZSTD_freeCStream(zc);
372 zc = ZSTD_createCStream_advanced(customMem);
373 if (zc==NULL) goto _output_error; /* memory allocation issue */
374 /* use 1 */
375 { size_t const inSize = 513;
Yann Collet0be6fd32017-05-08 16:08:01 -0700376 DISPLAYLEVEL(5, "use1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200377 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
378 inBuff.src = CNBuffer;
379 inBuff.size = inSize;
380 inBuff.pos = 0;
381 outBuff.dst = (char*)(compressedBuffer)+cSize;
382 outBuff.size = ZSTD_compressBound(inSize);
383 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700384 DISPLAYLEVEL(5, "compress1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200385 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
386 if (ZSTD_isError(r)) goto _output_error; }
387 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700388 DISPLAYLEVEL(5, "end1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200389 { size_t const r = ZSTD_endStream(zc, &outBuff);
390 if (r != 0) goto _output_error; } /* error, or some data not flushed */
391 }
392 /* use 2 */
393 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
Yann Collet0be6fd32017-05-08 16:08:01 -0700394 DISPLAYLEVEL(5, "use2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200395 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
396 inBuff.src = CNBuffer;
397 inBuff.size = inSize;
398 inBuff.pos = 0;
399 outBuff.dst = (char*)(compressedBuffer)+cSize;
400 outBuff.size = ZSTD_compressBound(inSize);
401 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700402 DISPLAYLEVEL(5, "compress2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200403 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
404 if (ZSTD_isError(r)) goto _output_error; }
405 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700406 DISPLAYLEVEL(5, "end2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200407 { size_t const r = ZSTD_endStream(zc, &outBuff);
408 if (r != 0) goto _output_error; } /* error, or some data not flushed */
409 }
Yann Collet736788f2017-01-19 12:12:50 -0800410 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200411
Yann Collet95162342016-10-25 16:19:52 -0700412 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800413 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet33fce032017-01-16 19:46:22 -0800414 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1);
Yann Collet95162342016-10-25 16:19:52 -0700415 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
416 if (ZSTD_isError(initError)) goto _output_error;
417 cSize = 0;
418 outBuff.dst = compressedBuffer;
419 outBuff.size = compressedBufferSize;
420 outBuff.pos = 0;
421 inBuff.src = CNBuffer;
422 inBuff.size = CNBufferSize;
423 inBuff.pos = 0;
424 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
425 if (ZSTD_isError(r)) goto _output_error; }
426 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
427 { size_t const r = ZSTD_endStream(zc, &outBuff);
428 if (r != 0) goto _output_error; } /* error, or some data not flushed */
429 cSize = outBuff.pos;
430 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800431 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700432 }
433
Yann Collet736788f2017-01-19 12:12:50 -0800434 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200435 { size_t const s = ZSTD_sizeof_CStream(zc);
436 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800437 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200438 }
439
Yann Collet33fce032017-01-16 19:46:22 -0800440 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
441 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
442 if (dID != dictID) goto _output_error;
443 DISPLAYLEVEL(4, "OK (%u) \n", dID);
444 }
445
Yann Collet335ad5d2016-10-25 17:47:02 -0700446 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800447 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800448 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700449 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
450 if (ZSTD_isError(initError)) goto _output_error;
451 inBuff.src = compressedBuffer;
452 inBuff.size = cSize;
453 inBuff.pos = 0;
454 outBuff.dst = decodedBuffer;
455 outBuff.size = CNBufferSize;
456 outBuff.pos = 0;
457 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
458 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
459 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
460 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
461 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800462 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700463 }
464
Yann Collet12083a42016-09-06 15:01:51 +0200465 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800466 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200467 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
468 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800469 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200470
471 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800472 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700473 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800474 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000); /* too small limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200475 if (ZSTD_isError(r)) goto _output_error; }
476 inBuff.src = compressedBuffer;
477 inBuff.size = cSize;
478 inBuff.pos = 0;
479 outBuff.dst = decodedBuffer;
480 outBuff.size = CNBufferSize;
481 outBuff.pos = 0;
482 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
483 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800484 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200485
Yann Collet7d283cd2017-04-27 14:48:34 -0700486 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
487 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
488 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
Yann Collet7bd1a292017-06-21 11:50:33 -0700489 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, 1 /* byReference */, ZSTD_dm_auto, cParams, customMem);
Yann Collet8c910d22017-06-03 01:15:02 -0700490 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
Yann Collet7d283cd2017-04-27 14:48:34 -0700491 if (ZSTD_isError(initError)) goto _output_error;
Nick Terrell62ecad32017-04-03 20:56:39 -0700492 cSize = 0;
493 outBuff.dst = compressedBuffer;
494 outBuff.size = compressedBufferSize;
495 outBuff.pos = 0;
496 inBuff.src = CNBuffer;
497 inBuff.size = CNBufferSize;
498 inBuff.pos = 0;
499 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
500 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet8c910d22017-06-03 01:15:02 -0700501 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Nick Terrell62ecad32017-04-03 20:56:39 -0700502 { size_t const r = ZSTD_endStream(zc, &outBuff);
503 if (r != 0) goto _output_error; } /* error, or some data not flushed */
504 cSize = outBuff.pos;
505 ZSTD_freeCDict(cdict);
506 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet4ee6b152017-04-11 11:59:44 -0700507 }
Nick Terrell62ecad32017-04-03 20:56:39 -0700508
Yann Colleta92cbb72017-04-27 15:08:56 -0700509 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
510 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
511 if (did != 0) goto _output_error;
512 }
513 DISPLAYLEVEL(3, "OK (not detected) \n");
514
Yann Collet4ee6b152017-04-11 11:59:44 -0700515 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
516 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
517 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
518 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
Nick Terrell62ecad32017-04-03 20:56:39 -0700519 }
520
Yann Collet0bb381d2017-04-18 15:08:52 -0700521 /* Empty srcSize */
522 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
523 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
524 params.fParams.contentSizeFlag = 1;
525 ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0);
526 } /* cstream advanced shall write content size = 0 */
527 inBuff.src = CNBuffer;
528 inBuff.size = 0;
529 inBuff.pos = 0;
530 outBuff.dst = compressedBuffer;
531 outBuff.size = compressedBufferSize;
532 outBuff.pos = 0;
533 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
534 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
535 cSize = outBuff.pos;
536 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
537 DISPLAYLEVEL(3, "OK \n");
538
Sean Purcell2db72492017-02-09 10:50:43 -0800539 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
540 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
541 params.fParams.contentSizeFlag = 1;
Yann Collet4ee6b152017-04-11 11:59:44 -0700542 ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
543 } /* cstream advanced shall write content size = 0 */
Sean Purcell2db72492017-02-09 10:50:43 -0800544 inBuff.src = CNBuffer;
545 inBuff.size = 0;
546 inBuff.pos = 0;
547 outBuff.dst = compressedBuffer;
548 outBuff.size = compressedBufferSize;
549 outBuff.pos = 0;
550 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
551 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
552 cSize = outBuff.pos;
553 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
554
555 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
556 inBuff.src = CNBuffer;
557 inBuff.size = 0;
558 inBuff.pos = 0;
559 outBuff.dst = compressedBuffer;
560 outBuff.size = compressedBufferSize;
561 outBuff.pos = 0;
562 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
563 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
564 cSize = outBuff.pos;
565 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
566 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200567
Sean Purcell887eaa92017-02-15 16:43:45 -0800568 /* Overlen overwriting window data bug */
569 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800570 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
571 1. 'a' repeated 517 times
572 2. 'b' repeated 516 times
573 3. a compressed block with no literals and 3 sequence commands:
574 litlength = 0, offset = 24, match length = 24
575 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
576 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
577
578 const char* testCase =
579 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
580 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
581 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Sean Purcell887eaa92017-02-15 16:43:45 -0800582 ZSTD_DStream* zds = ZSTD_createDStream();
583
584 ZSTD_initDStream(zds);
585 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800586 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800587 inBuff.pos = 0;
588 outBuff.dst = decodedBuffer;
589 outBuff.size = CNBufferSize;
590 outBuff.pos = 0;
591
592 while (inBuff.pos < inBuff.size) {
593 size_t const r = ZSTD_decompressStream(zds, &outBuff, &inBuff);
594 /* Bug will cause checksum to fail */
595 if (ZSTD_isError(r)) goto _output_error;
596 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100597
598 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800599 }
600 DISPLAYLEVEL(3, "OK \n");
601
Yann Colletd7883a22016-08-12 16:48:02 +0200602_end:
Yann Collet33fce032017-01-16 19:46:22 -0800603 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200604 ZSTD_freeCStream(zc);
605 ZSTD_freeDStream(zd);
606 free(CNBuffer);
607 free(compressedBuffer);
608 free(decodedBuffer);
609 return testResult;
610
611_output_error:
612 testResult = 1;
613 DISPLAY("Error detected in Unit tests ! \n");
614 goto _end;
615}
616
617
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200618/* ====== Fuzzer tests ====== */
619
Yann Colletd7883a22016-08-12 16:48:02 +0200620static size_t findDiff(const void* buf1, const void* buf2, size_t max)
621{
622 const BYTE* b1 = (const BYTE*)buf1;
623 const BYTE* b2 = (const BYTE*)buf2;
624 size_t u;
625 for (u=0; u<max; u++) {
626 if (b1[u] != b2[u]) break;
627 }
Yann Collet736788f2017-01-19 12:12:50 -0800628 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800629 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
630 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]);
631 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
632 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 +0200633 return u;
634}
635
636static size_t FUZ_rLogLength(U32* seed, U32 logLength)
637{
638 size_t const lengthMask = ((size_t)1 << logLength) - 1;
639 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
640}
641
642static size_t FUZ_randomLength(U32* seed, U32 maxLog)
643{
644 U32 const logLength = FUZ_rand(seed) % maxLog;
645 return FUZ_rLogLength(seed, logLength);
646}
647
648#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
649
Yann Collet01743a32017-06-16 17:56:41 -0700650#define CHECK(cond, ...) { \
651 if (cond) { \
652 DISPLAY("Error => "); \
653 DISPLAY(__VA_ARGS__); \
654 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
655 goto _output_error; \
656} }
657
658#define CHECK_Z(f) { \
659 size_t const err = f; \
660 if (ZSTD_isError(err)) { \
661 DISPLAY("Error => %s : %s ", \
662 #f, ZSTD_getErrorName(err)); \
663 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
664 goto _output_error; \
665} }
Yann Colletd7883a22016-08-12 16:48:02 +0200666
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700667static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200668{
669 static const U32 maxSrcLog = 24;
670 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200671 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200672 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200673 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
674 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
675 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
676 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
677 size_t const dstBufferSize = srcBufferSize;
678 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200679 U32 result = 0;
680 U32 testNb = 0;
681 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200682 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
683 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
684 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
685 clock_t const startClock = clock();
686 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200687 size_t dictSize = 0;
688 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700689 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Colletd7883a22016-08-12 16:48:02 +0200690
691 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200692 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
693 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
694 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
695 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
696 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200697 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200698 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200699 "Not enough memory, fuzzer tests cancelled");
700
701 /* Create initial samples */
702 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
703 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
704 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
705 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
706 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
707 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200708 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200709
710 /* catch up testNb */
711 for (testNb=1; testNb < startTest; testNb++)
712 FUZ_rand(&coreSeed);
713
714 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700715 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200716 U32 lseed;
717 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200718 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200719 XXH64_state_t xxhState;
720 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200721 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200722 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200723
724 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700725 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
726 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200727 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800728 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200729
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200730 /* states full reset (deliberately not synchronized) */
731 /* some issues can only happen when reusing states */
Yann Colleted1d0392017-06-19 11:07:33 -0700732 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
733 ZSTD_freeCStream(zc);
734 zc = ZSTD_createCStream();
735 resetAllowed=0;
736 }
737 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
738 ZSTD_freeDStream(zd);
739 zd = ZSTD_createDStream();
740 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
741 }
Yann Colletd7883a22016-08-12 16:48:02 +0200742
743 /* srcBuffer selection [0-4] */
744 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
745 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
746 else {
747 buffNb >>= 3;
748 if (buffNb & 7) {
749 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
750 buffNb = tnb[buffNb >> 3];
751 } else {
752 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
753 buffNb = tnb[buffNb >> 3];
754 } }
755 srcBuffer = cNoiseBuffer[buffNb];
756 }
757
758 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200759 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
760 && oldTestLog /* at least one test happened */ && resetAllowed) {
761 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
Yann Colletce800982017-04-05 16:34:09 -0700762 if (maxTestSize >= srcBufferSize)
763 maxTestSize = srcBufferSize-1;
Yann Colletcf409a72016-09-26 16:41:05 +0200764 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
Yann Collet01743a32017-06-16 17:56:41 -0700765 CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) );
Yann Colletcf409a72016-09-26 16:41:05 +0200766 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200767 } else {
768 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700769 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletce800982017-04-05 16:34:09 -0700770 U32 const cLevel = ( FUZ_rand(&lseed) %
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700771 (ZSTD_maxCLevel() -
Yann Colletce800982017-04-05 16:34:09 -0700772 (MAX(testLog, dictLog) / cLevelLimiter)))
773 + 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200774 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200775 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200776 /* random dictionary selection */
Yann Colletce800982017-04-05 16:34:09 -0700777 dictSize = ((FUZ_rand(&lseed)&1)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200778 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
779 dict = srcBuffer + dictStart;
780 }
Yann Colletcf409a72016-09-26 16:41:05 +0200781 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
782 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200783 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
784 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Collet01743a32017-06-16 17:56:41 -0700785 CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
786 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200787
788 /* multi-segments compression test */
789 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200790 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200791 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200792 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100793 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200794 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
795 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
796 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200797 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
798 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200799 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200800 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200801
Yann Colleted1d0392017-06-19 11:07:33 -0700802 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Colletd7883a22016-08-12 16:48:02 +0200803
Yann Collet53e17fb2016-08-17 01:39:22 +0200804 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
805 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
806 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200807 }
808
809 /* random flush operation, to mess around */
810 if ((FUZ_rand(&lseed) & 15) == 0) {
811 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200812 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
813 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colleted1d0392017-06-19 11:07:33 -0700814 CHECK_Z( ZSTD_flushStream(zc, &outBuff) );
815 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200816
817 /* final frame epilogue */
818 { size_t remainingToFlush = (size_t)(-1);
819 while (remainingToFlush) {
820 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
821 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200822 outBuff.size = outBuff.pos + adjustedDstSize;
823 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Collet009d6042017-05-19 10:17:59 -0700824 CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Colletd7883a22016-08-12 16:48:02 +0200825 } }
826 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200827 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200828 }
829
830 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200831 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700832 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800833 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200834 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800835 }
Yann Colletd7883a22016-08-12 16:48:02 +0200836 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200837 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
838 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
839 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200840 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
841 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
842 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200843 inBuff.size = inBuff.pos + readCSrcSize;
844 outBuff.size = inBuff.pos + dstBuffSize;
845 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200846 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200847 }
848 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200849 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
850 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200851 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
852 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
853 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
854 } }
855
856 /*===== noisy/erroneous src decompression test =====*/
857
858 /* add some noise */
859 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
860 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
861 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
862 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
863 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
864 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
865 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
866 } }
867
868 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200869 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200870 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
871 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
872 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200873 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
874 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200875 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -0800876 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200877 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -0800878 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200879 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200880 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -0800881 /* No forward progress possible */
882 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +0200883 } } } }
884 DISPLAY("\r%u fuzzer tests completed \n", testNb);
885
886_cleanup:
887 ZSTD_freeCStream(zc);
888 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200889 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200890 free(cNoiseBuffer[0]);
891 free(cNoiseBuffer[1]);
892 free(cNoiseBuffer[2]);
893 free(cNoiseBuffer[3]);
894 free(cNoiseBuffer[4]);
895 free(copyBuffer);
896 free(cBuffer);
897 free(dstBuffer);
898 return result;
899
900_output_error:
901 result = 1;
902 goto _cleanup;
903}
904
905
Yann Collet736788f2017-01-19 12:12:50 -0800906/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700907static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -0800908{
909 static const U32 maxSrcLog = 24;
910 static const U32 maxSampleLog = 19;
911 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
912 BYTE* cNoiseBuffer[5];
913 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
914 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
915 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
916 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
917 size_t const dstBufferSize = srcBufferSize;
918 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
919 U32 result = 0;
920 U32 testNb = 0;
921 U32 coreSeed = seed;
922 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(2); /* will be reset sometimes */
923 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
924 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
925 clock_t const startClock = clock();
926 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
927 size_t dictSize = 0;
928 U32 oldTestLog = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700929 int const cLevelLimiter = bigTests ? 3 : 2;
Yann Collet736788f2017-01-19 12:12:50 -0800930
931 /* allocations */
932 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
933 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
934 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
935 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
936 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
937 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
938 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
939 "Not enough memory, fuzzer tests cancelled");
940
941 /* Create initial samples */
942 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
943 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
944 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
945 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
946 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
947 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
948 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
949
950 /* catch up testNb */
951 for (testNb=1; testNb < startTest; testNb++)
952 FUZ_rand(&coreSeed);
953
954 /* test loop */
955 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
956 U32 lseed;
957 const BYTE* srcBuffer;
958 size_t totalTestSize, totalGenSize, cSize;
959 XXH64_state_t xxhState;
960 U64 crcOrig;
961 U32 resetAllowed = 1;
962 size_t maxTestSize;
963
964 /* init */
965 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
966 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
967 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -0800968 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -0800969
970 /* states full reset (deliberately not synchronized) */
971 /* some issues can only happen when reusing states */
972 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
973 U32 const nbThreads = (FUZ_rand(&lseed) % 6) + 1;
Yann Collet30c76982017-03-31 18:27:03 -0700974 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
Yann Collet736788f2017-01-19 12:12:50 -0800975 ZSTDMT_freeCCtx(zc);
976 zc = ZSTDMT_createCCtx(nbThreads);
977 resetAllowed=0;
978 }
979 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
980 ZSTD_freeDStream(zd);
981 zd = ZSTD_createDStream();
982 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
983 }
984
985 /* srcBuffer selection [0-4] */
986 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
987 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
988 else {
989 buffNb >>= 3;
990 if (buffNb & 7) {
991 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
992 buffNb = tnb[buffNb >> 3];
993 } else {
994 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
995 buffNb = tnb[buffNb >> 3];
996 } }
997 srcBuffer = cNoiseBuffer[buffNb];
998 }
999
1000 /* compression init */
1001 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1002 && oldTestLog /* at least one test happened */ && resetAllowed) {
1003 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1004 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1005 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
1006 size_t const resetError = ZSTDMT_initCStream(zc, compressionLevel);
Yann Collet19d670b2017-01-19 15:32:07 -08001007 CHECK(ZSTD_isError(resetError), "ZSTDMT_initCStream error : %s", ZSTD_getErrorName(resetError));
Yann Collet736788f2017-01-19 12:12:50 -08001008 }
1009 } else {
1010 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001011 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001012 U32 const cLevel = (FUZ_rand(&lseed) %
Yann Collet8c910d22017-06-03 01:15:02 -07001013 (ZSTD_maxCLevel() -
1014 (MAX(testLog, dictLog) / cLevelLimiter))) +
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001015 1;
Yann Collet736788f2017-01-19 12:12:50 -08001016 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1017 oldTestLog = testLog;
1018 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -07001019 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001020 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1021 dict = srcBuffer + dictStart;
1022 }
1023 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
1024 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletce800982017-04-05 16:34:09 -07001025 DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n",
1026 params.cParams.windowLog, (U32)pledgedSrcSize);
Yann Collet19d670b2017-01-19 15:32:07 -08001027 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1028 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletce800982017-04-05 16:34:09 -07001029 params.fParams.contentSizeFlag = pledgedSrcSize>0;
Yann Collet2c5514c2017-04-18 22:52:41 -07001030 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
Yann Colletcd23dd22017-01-30 12:46:35 -08001031 { size_t const initError = ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
1032 CHECK (ZSTD_isError(initError),"ZSTDMT_initCStream_advanced error : %s", ZSTD_getErrorName(initError)); }
1033 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12);
Yann Collet92c98a52017-01-30 12:50:31 -08001034 ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1));
Yann Colletcd23dd22017-01-30 12:46:35 -08001035 } }
Yann Collet736788f2017-01-19 12:12:50 -08001036
1037 /* multi-segments compression test */
1038 XXH64_reset(&xxhState, 0);
1039 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1040 U32 n;
1041 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1042 /* compress random chunks into randomly sized dst buffers */
1043 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1044 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1045 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1046 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1047 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1048 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1049 outBuff.size = outBuff.pos + dstBuffSize;
1050
1051 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
1052 { size_t const compressionError = ZSTDMT_compressStream(zc, &outBuff, &inBuff);
1053 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Collet19d670b2017-01-19 15:32:07 -08001054 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -08001055
1056 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1057 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1058 totalTestSize += inBuff.pos;
1059 }
1060
1061 /* random flush operation, to mess around */
1062 if ((FUZ_rand(&lseed) & 15) == 0) {
1063 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1064 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1065 outBuff.size = outBuff.pos + adjustedDstSize;
1066 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
1067 { size_t const flushError = ZSTDMT_flushStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001068 CHECK (ZSTD_isError(flushError), "ZSTDMT_flushStream error : %s", ZSTD_getErrorName(flushError));
Yann Collet736788f2017-01-19 12:12:50 -08001069 } } }
1070
1071 /* final frame epilogue */
1072 { size_t remainingToFlush = (size_t)(-1);
1073 while (remainingToFlush) {
1074 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1075 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1076 outBuff.size = outBuff.pos + adjustedDstSize;
1077 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1078 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001079 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet736788f2017-01-19 12:12:50 -08001080 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1081 } }
Yann Collet736788f2017-01-19 12:12:50 -08001082 crcOrig = XXH64_digest(&xxhState);
1083 cSize = outBuff.pos;
Yann Collet2c5514c2017-04-18 22:52:41 -07001084 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001085 }
1086
1087 /* multi - fragments decompression test */
1088 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1089 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
1090 } else {
1091 ZSTD_initDStream_usingDict(zd, dict, dictSize);
1092 }
1093 { size_t decompressionResult = 1;
1094 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1095 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1096 for (totalGenSize = 0 ; decompressionResult ; ) {
1097 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1098 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1099 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1100 inBuff.size = inBuff.pos + readCSrcSize;
1101 outBuff.size = inBuff.pos + dstBuffSize;
Yann Colletce800982017-04-05 16:34:09 -07001102 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001103 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1104 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Collet2c5514c2017-04-18 22:52:41 -07001105 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001106 }
Yann Colletbb002742017-01-25 16:25:38 -08001107 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1108 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001109 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1110 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1111 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1112 } }
1113
1114 /*===== noisy/erroneous src decompression test =====*/
1115
1116 /* add some noise */
1117 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1118 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1119 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1120 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1121 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1122 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1123 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1124 } }
1125
1126 /* try decompression on noisy data */
1127 ZSTD_initDStream(zd_noise); /* note : no dictionary */
1128 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1129 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1130 while (outBuff.pos < dstBufferSize) {
1131 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1132 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1133 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001134 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001135 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001136 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001137 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1138 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001139 /* No forward progress possible */
1140 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001141 } } } }
1142 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1143
1144_cleanup:
1145 ZSTDMT_freeCCtx(zc);
1146 ZSTD_freeDStream(zd);
1147 ZSTD_freeDStream(zd_noise);
1148 free(cNoiseBuffer[0]);
1149 free(cNoiseBuffer[1]);
1150 free(cNoiseBuffer[2]);
1151 free(cNoiseBuffer[3]);
1152 free(cNoiseBuffer[4]);
1153 free(copyBuffer);
1154 free(cBuffer);
1155 free(dstBuffer);
1156 return result;
1157
1158_output_error:
1159 result = 1;
1160 goto _cleanup;
1161}
1162
1163
Yann Colletd7a3bff2017-06-19 11:53:01 -07001164/* Tests for ZSTD_compress_generic() API */
Yann Collet01743a32017-06-16 17:56:41 -07001165static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
1166{
1167 static const U32 maxSrcLog = 24;
1168 static const U32 maxSampleLog = 19;
1169 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1170 BYTE* cNoiseBuffer[5];
1171 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1172 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1173 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1174 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1175 size_t const dstBufferSize = srcBufferSize;
1176 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1177 U32 result = 0;
1178 U32 testNb = 0;
1179 U32 coreSeed = seed;
1180 ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */
1181 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1182 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
1183 clock_t const startClock = clock();
Yann Colletd7a3bff2017-06-19 11:53:01 -07001184 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Collet01743a32017-06-16 17:56:41 -07001185 size_t dictSize = 0;
1186 U32 oldTestLog = 0;
1187 int const cLevelLimiter = bigTests ? 3 : 2;
1188
1189 /* allocations */
1190 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1191 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1192 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1193 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1194 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1195 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1196 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1197 "Not enough memory, fuzzer tests cancelled");
1198
1199 /* Create initial samples */
1200 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1201 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1202 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1203 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1204 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1205 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet8dee0ec2017-06-18 23:25:15 -07001206 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Collet01743a32017-06-16 17:56:41 -07001207
1208 /* catch up testNb */
1209 for (testNb=1; testNb < startTest; testNb++)
1210 FUZ_rand(&coreSeed);
1211
1212 /* test loop */
1213 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
1214 U32 lseed;
1215 const BYTE* srcBuffer;
1216 size_t totalTestSize, totalGenSize, cSize;
1217 XXH64_state_t xxhState;
1218 U64 crcOrig;
1219 U32 resetAllowed = 1;
1220 size_t maxTestSize;
1221
1222 /* init */
1223 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
1224 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
1225 FUZ_rand(&coreSeed);
1226 lseed = coreSeed ^ prime32;
1227
1228 /* states full reset (deliberately not synchronized) */
1229 /* some issues can only happen when reusing states */
1230 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1231 DISPLAYLEVEL(5, "Creating new context \n");
1232 ZSTD_freeCCtx(zc);
1233 zc = ZSTD_createCCtx();
1234 CHECK(zc==NULL, "allocation error");
1235 resetAllowed=0;
1236 }
1237 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1238 ZSTD_freeDStream(zd);
1239 zd = ZSTD_createDStream();
1240 CHECK(zd==NULL, "allocation error");
1241 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1242 }
1243
1244 /* srcBuffer selection [0-4] */
1245 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1246 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1247 else {
1248 buffNb >>= 3;
1249 if (buffNb & 7) {
1250 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1251 buffNb = tnb[buffNb >> 3];
1252 } else {
1253 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1254 buffNb = tnb[buffNb >> 3];
1255 } }
1256 srcBuffer = cNoiseBuffer[buffNb];
1257 }
1258
1259 /* compression init */
1260 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1261 && oldTestLog /* at least one test happened */ && resetAllowed) {
1262 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1263 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1264 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) );
1265 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
1266 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_compressionLevel, compressionLevel) );
1267 }
1268 } else {
1269 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1270 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
1271 U32 const cLevel = (FUZ_rand(&lseed) %
1272 (ZSTD_maxCLevel() -
1273 (MAX(testLog, dictLog) / cLevelLimiter))) +
1274 1;
1275 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1276 oldTestLog = testLog;
1277 /* random dictionary selection */
1278 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1279 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1280 dict = srcBuffer + dictStart;
Yann Colletd7a3bff2017-06-19 11:53:01 -07001281 if (!dictSize) dict=NULL;
Yann Collet01743a32017-06-16 17:56:41 -07001282 }
1283 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1284 ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
1285
1286 /* mess with compression parameters */
Yann Colletd7a3bff2017-06-19 11:53:01 -07001287 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* always cancel previous dict, to make user it's possible to pass compression parameters */
Yann Collet01743a32017-06-16 17:56:41 -07001288 cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
1289 cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
1290 cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
1291 cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
1292 cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
Yann Colletf5deae82017-06-18 23:41:38 -07001293 cParams.targetLength = (U32)(cParams.targetLength * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
Yann Collet01743a32017-06-16 17:56:41 -07001294 cParams = ZSTD_adjustCParams(cParams, 0, 0);
1295
Yann Collet8dee0ec2017-06-18 23:25:15 -07001296 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog) );
1297 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_hashLog, cParams.hashLog) );
1298 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_chainLog, cParams.chainLog) );
1299 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_searchLog, cParams.searchLog) );
1300 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_minMatch, cParams.searchLength) );
1301 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_targetLength, cParams.targetLength) );
Yann Collet01743a32017-06-16 17:56:41 -07001302
Yann Colletd7a3bff2017-06-19 11:53:01 -07001303 /* unconditionally set, to be sync with decoder */
1304 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) );
Yann Collet01743a32017-06-16 17:56:41 -07001305
Yann Colletd7a3bff2017-06-19 11:53:01 -07001306 if (dict && dictSize) {
1307 /* test that compression parameters are correctly rejected after setting a dictionary */
Yann Colletd7a3bff2017-06-19 11:53:01 -07001308 size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1) ;
1309 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed");
1310 }
Yann Collet01743a32017-06-16 17:56:41 -07001311
1312 /* mess with frame parameters */
Yann Colletf5deae82017-06-18 23:41:38 -07001313 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1) );
1314 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1) );
1315 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1) );
1316 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
Yann Collet01743a32017-06-16 17:56:41 -07001317 DISPLAYLEVEL(5, "pledgedSrcSize : %u \n", (U32)pledgedSrcSize);
1318
1319 /* multi-threading parameters */
1320 { U32 const nbThreads = (FUZ_rand(&lseed) & 3) + 1;
1321 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_nbThreads, nbThreads) );
1322 if (nbThreads > 1) {
Yann Colleted1d0392017-06-19 11:07:33 -07001323 U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
Yann Collet01743a32017-06-16 17:56:41 -07001324 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10) );
Yann Colleted1d0392017-06-19 11:07:33 -07001325 CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog)) );
Yann Collet01743a32017-06-16 17:56:41 -07001326 } } } }
1327
1328 /* multi-segments compression test */
1329 XXH64_reset(&xxhState, 0);
1330 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1331 U32 n;
1332 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1333 /* compress random chunks into randomly sized dst buffers */
Yann Colleted1d0392017-06-19 11:07:33 -07001334 size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1335 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1336 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1337 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1338 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1339 ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush;
1340 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1341 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet01743a32017-06-16 17:56:41 -07001342
Yann Colleted1d0392017-06-19 11:07:33 -07001343 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) );
1344 DISPLAYLEVEL(5, "compress consumed %u bytes (total : %u) \n",
1345 (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos));
Yann Collet01743a32017-06-16 17:56:41 -07001346
Yann Colleted1d0392017-06-19 11:07:33 -07001347 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1348 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1349 totalTestSize += inBuff.pos;
1350 }
Yann Collet01743a32017-06-16 17:56:41 -07001351
1352 /* final frame epilogue */
1353 { size_t remainingToFlush = (size_t)(-1);
1354 while (remainingToFlush) {
1355 ZSTD_inBuffer inBuff = { NULL, 0, 0 };
1356 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1357 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1358 outBuff.size = outBuff.pos + adjustedDstSize;
1359 DISPLAYLEVEL(5, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize);
1360 remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
Yann Colletd7a3bff2017-06-19 11:53:01 -07001361 CHECK(ZSTD_isError(remainingToFlush),
1362 "ZSTD_compress_generic w/ ZSTD_e_end error : %s",
1363 ZSTD_getErrorName(remainingToFlush) );
Yann Collet01743a32017-06-16 17:56:41 -07001364 } }
1365 crcOrig = XXH64_digest(&xxhState);
1366 cSize = outBuff.pos;
1367 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
1368 }
1369
1370 /* multi - fragments decompression test */
1371 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
1372 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
1373 } else {
1374 ZSTD_initDStream_usingDict(zd, dict, dictSize);
1375 }
1376 { size_t decompressionResult = 1;
1377 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1378 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1379 for (totalGenSize = 0 ; decompressionResult ; ) {
1380 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1381 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1382 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1383 inBuff.size = inBuff.pos + readCSrcSize;
1384 outBuff.size = inBuff.pos + dstBuffSize;
1385 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
1386 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1387 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
1388 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
1389 }
1390 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1391 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1392 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1393 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1394 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1395 } }
1396
1397 /*===== noisy/erroneous src decompression test =====*/
1398
1399 /* add some noise */
1400 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1401 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1402 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1403 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1404 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1405 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1406 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1407 } }
1408
1409 /* try decompression on noisy data */
1410 ZSTD_initDStream(zd_noise); /* note : no dictionary */
1411 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1412 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1413 while (outBuff.pos < dstBufferSize) {
1414 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1415 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1416 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1417 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1418 outBuff.size = outBuff.pos + adjustedDstSize;
1419 inBuff.size = inBuff.pos + adjustedCSrcSize;
1420 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1421 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Colleted1d0392017-06-19 11:07:33 -07001422 /* Good so far, but no more progress possible */
Yann Collet01743a32017-06-16 17:56:41 -07001423 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1424 } } } }
Yann Collet8dee0ec2017-06-18 23:25:15 -07001425 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet01743a32017-06-16 17:56:41 -07001426
1427_cleanup:
1428 ZSTD_freeCCtx(zc);
1429 ZSTD_freeDStream(zd);
1430 ZSTD_freeDStream(zd_noise);
1431 free(cNoiseBuffer[0]);
1432 free(cNoiseBuffer[1]);
1433 free(cNoiseBuffer[2]);
1434 free(cNoiseBuffer[3]);
1435 free(cNoiseBuffer[4]);
1436 free(copyBuffer);
1437 free(cBuffer);
1438 free(dstBuffer);
1439 return result;
1440
1441_output_error:
1442 result = 1;
1443 goto _cleanup;
1444}
1445
1446
Yann Colletd7883a22016-08-12 16:48:02 +02001447/*-*******************************************************
1448* Command line
1449*********************************************************/
1450int FUZ_usage(const char* programName)
1451{
1452 DISPLAY( "Usage :\n");
1453 DISPLAY( " %s [args]\n", programName);
1454 DISPLAY( "\n");
1455 DISPLAY( "Arguments :\n");
1456 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1457 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1458 DISPLAY( " -t# : Select starting test number (default:0)\n");
1459 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1460 DISPLAY( " -v : verbose\n");
1461 DISPLAY( " -p : pause at the end\n");
1462 DISPLAY( " -h : display help and exit\n");
1463 return 0;
1464}
1465
Yann Collet01743a32017-06-16 17:56:41 -07001466typedef enum { simple_api, mt_api, advanced_api } e_api;
Yann Colletd7883a22016-08-12 16:48:02 +02001467
1468int main(int argc, const char** argv)
1469{
1470 U32 seed=0;
1471 int seedset=0;
1472 int argNb;
1473 int nbTests = nbTestsDefault;
1474 int testNb = 0;
1475 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
1476 int result=0;
Yann Collet19d670b2017-01-19 15:32:07 -08001477 int mainPause = 0;
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001478 int bigTests = 1;
Yann Collet01743a32017-06-16 17:56:41 -07001479 e_api selected_api = simple_api;
Yann Collet19d670b2017-01-19 15:32:07 -08001480 const char* const programName = argv[0];
Yann Collet8dafb1a2017-01-25 17:01:13 -08001481 ZSTD_customMem const customMem = { allocFunction, freeFunction, NULL };
Yann Colletd7a3bff2017-06-19 11:53:01 -07001482 ZSTD_customMem const customNULL = ZSTD_defaultCMem;
Yann Colletd7883a22016-08-12 16:48:02 +02001483
1484 /* Check command line */
1485 for(argNb=1; argNb<argc; argNb++) {
1486 const char* argument = argv[argNb];
1487 if(!argument) continue; /* Protection if argument empty */
1488
1489 /* Parsing commands. Aggregated commands are allowed */
1490 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001491
Yann Collet01743a32017-06-16 17:56:41 -07001492 if (!strcmp(argument, "--mt")) { selected_api=mt_api; continue; }
1493 if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001494 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001495
1496 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001497 while (*argument!=0) {
1498 switch(*argument)
1499 {
1500 case 'h':
1501 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001502
Yann Colletd7883a22016-08-12 16:48:02 +02001503 case 'v':
1504 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001505 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001506 break;
Yann Collet736788f2017-01-19 12:12:50 -08001507
Yann Colletd7883a22016-08-12 16:48:02 +02001508 case 'q':
1509 argument++;
1510 g_displayLevel--;
1511 break;
Yann Collet736788f2017-01-19 12:12:50 -08001512
Yann Colletd7883a22016-08-12 16:48:02 +02001513 case 'p': /* pause at the end */
1514 argument++;
1515 mainPause = 1;
1516 break;
1517
Yann Collet736788f2017-01-19 12:12:50 -08001518 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001519 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001520 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001521 while ((*argument>='0') && (*argument<='9')) {
1522 nbTests *= 10;
1523 nbTests += *argument - '0';
1524 argument++;
1525 }
1526 break;
1527
Yann Collet736788f2017-01-19 12:12:50 -08001528 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001529 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001530 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001531 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001532 g_clockTime *= 10;
1533 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001534 argument++;
1535 }
Yann Colletef9999f2016-09-01 16:44:48 -07001536 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001537 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001538 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +02001539 break;
1540
Yann Collet736788f2017-01-19 12:12:50 -08001541 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001542 argument++;
1543 seed=0;
1544 seedset=1;
1545 while ((*argument>='0') && (*argument<='9')) {
1546 seed *= 10;
1547 seed += *argument - '0';
1548 argument++;
1549 }
1550 break;
1551
Yann Collet736788f2017-01-19 12:12:50 -08001552 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001553 argument++;
1554 testNb=0;
1555 while ((*argument>='0') && (*argument<='9')) {
1556 testNb *= 10;
1557 testNb += *argument - '0';
1558 argument++;
1559 }
1560 break;
1561
1562 case 'P': /* compressibility % */
1563 argument++;
1564 proba=0;
1565 while ((*argument>='0') && (*argument<='9')) {
1566 proba *= 10;
1567 proba += *argument - '0';
1568 argument++;
1569 }
1570 if (proba<0) proba=0;
1571 if (proba>100) proba=100;
1572 break;
1573
1574 default:
1575 return FUZ_usage(programName);
1576 }
1577 } } } /* for(argNb=1; argNb<argc; argNb++) */
1578
1579 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001580 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001581
Yann Colletef9999f2016-09-01 16:44:48 -07001582 if (!seedset) {
1583 time_t const t = time(NULL);
1584 U32 const h = XXH32(&t, sizeof(t), 1);
1585 seed = h % 10000;
1586 }
1587
Yann Colletd7883a22016-08-12 16:48:02 +02001588 DISPLAY("Seed = %u\n", seed);
1589 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1590
1591 if (nbTests<=0) nbTests=1;
1592
1593 if (testNb==0) {
1594 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
1595 if (!result) {
Yann Collet736788f2017-01-19 12:12:50 -08001596 DISPLAYLEVEL(3, "Unit tests using customMem :\n")
Yann Colletd7883a22016-08-12 16:48:02 +02001597 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
1598 } }
1599
Yann Collet01743a32017-06-16 17:56:41 -07001600 if (!result) {
1601 switch(selected_api)
1602 {
1603 case simple_api :
1604 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1605 break;
1606 case mt_api :
1607 result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1608 break;
1609 case advanced_api :
1610 result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1611 break;
1612 default :
1613 assert(0); /* impossible */
1614 }
1615 }
Yann Colletd7883a22016-08-12 16:48:02 +02001616
1617 if (mainPause) {
1618 int unused;
1619 DISPLAY("Press Enter \n");
1620 unused = getchar();
1621 (void)unused;
1622 }
1623 return result;
1624}