blob: 0b8995c5d7bd4abc4ebc15673b4f792697b19a8e [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Colletd7883a22016-08-12 16:48:02 +020010
Yann Colletd7883a22016-08-12 16:48:02 +020011
12/*-************************************
13* Compiler specific
14**************************************/
15#ifdef _MSC_VER /* Visual Studio */
Yann Colleta5ffe3d2017-05-12 16:29:19 -070016# define _CRT_SECURE_NO_WARNINGS /* fgets */
17# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
18# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
Yann Colletd7883a22016-08-12 16:48:02 +020019#endif
20
21
22/*-************************************
23* Includes
24**************************************/
25#include <stdlib.h> /* free */
26#include <stdio.h> /* fgets, sscanf */
Yann Colletef9999f2016-09-01 16:44:48 -070027#include <time.h> /* clock_t, clock() */
Yann Colletd7883a22016-08-12 16:48:02 +020028#include <string.h> /* strcmp */
Yann Collet01743a32017-06-16 17:56:41 -070029#include <assert.h> /* assert */
Yann Colletd7883a22016-08-12 16:48:02 +020030#include "mem.h"
Yann Colleta5ffe3d2017-05-12 16:29:19 -070031#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */
Yann Colletd7883a22016-08-12 16:48:02 +020032#include "zstd.h" /* ZSTD_compressBound */
Yann Collete795c8a2016-12-13 16:39:36 +010033#include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */
Yann Collet736788f2017-01-19 12:12:50 -080034#include "zstdmt_compress.h"
Yann Collet33fce032017-01-16 19:46:22 -080035#include "zdict.h" /* ZDICT_trainFromBuffer */
Yann Colletd7883a22016-08-12 16:48:02 +020036#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070037#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020038#include "xxhash.h" /* XXH64_* */
39
40
41/*-************************************
42* Constants
43**************************************/
44#define KB *(1U<<10)
45#define MB *(1U<<20)
46#define GB *(1U<<30)
47
48static const U32 nbTestsDefault = 10000;
Yann Colletf99c2c12017-06-21 23:35:58 -070049static const U32 g_cLevelMax_smallTests = 10;
Yann Colletd7883a22016-08-12 16:48:02 +020050#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
51#define FUZ_COMPRESSIBILITY_DEFAULT 50
Yann Collet33fce032017-01-16 19:46:22 -080052static const U32 prime32 = 2654435761U;
Nick Terrellc233bdb2017-09-22 14:04:39 -070053static const U32 windowLogMax = 27;
Yann Colletd7883a22016-08-12 16:48:02 +020054
55
Yann Colletd7883a22016-08-12 16:48:02 +020056/*-************************************
57* Display Macros
58**************************************/
59#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
Yann Collet0be6fd32017-05-08 16:08:01 -070060#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
61 DISPLAY(__VA_ARGS__); \
62 if (g_displayLevel>=4) fflush(stderr); }
Yann Colletd7883a22016-08-12 16:48:02 +020063static U32 g_displayLevel = 2;
64
65#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070066 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
67 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Collet0be6fd32017-05-08 16:08:01 -070068 if (g_displayLevel>=4) fflush(stderr); } }
Yann Colletb3060f72016-09-09 16:44:16 +020069static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070070static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020071
Yann Colletef9999f2016-09-01 16:44:48 -070072static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020073
74
75/*-*******************************************************
76* Fuzzer functions
77*********************************************************/
78#define MAX(a,b) ((a)>(b)?(a):(b))
79
Yann Colletef9999f2016-09-01 16:44:48 -070080static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020081{
Yann Colletef9999f2016-09-01 16:44:48 -070082 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020083}
84
85/*! FUZ_rand() :
86 @return : a 27 bits random value, from a 32-bits `seed`.
87 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070088#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020089unsigned int FUZ_rand(unsigned int* seedPtr)
90{
Yann Collet33fce032017-01-16 19:46:22 -080091 static const U32 prime2 = 2246822519U;
Yann Colletd7883a22016-08-12 16:48:02 +020092 U32 rand32 = *seedPtr;
Yann Collet33fce032017-01-16 19:46:22 -080093 rand32 *= prime32;
Yann Colletd7883a22016-08-12 16:48:02 +020094 rand32 += prime2;
95 rand32 = FUZ_rotl32(rand32, 13);
96 *seedPtr = rand32;
97 return rand32 >> 5;
98}
99
Yann Colletcb327632016-08-23 00:30:31 +0200100
101/*======================================================
102* Basic Unit tests
103======================================================*/
104
Yann Collet33fce032017-01-16 19:46:22 -0800105typedef struct {
106 void* start;
107 size_t size;
108 size_t filled;
109} buffer_t;
110
111static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
112
113static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
114{
115 buffer_t dict = { NULL, 0, 0 };
116 size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
117 size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
118 if (!blockSizes) return dict;
119 dict.start = malloc(requestedDictSize);
120 if (!dict.start) { free(blockSizes); return dict; }
121 { size_t nb;
122 for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
123 blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
124 }
125 { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
126 free(blockSizes);
Yann Collet2c5514c2017-04-18 22:52:41 -0700127 if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
Yann Collet33fce032017-01-16 19:46:22 -0800128 dict.size = requestedDictSize;
129 dict.filled = dictSize;
130 return dict; /* how to return dictSize ? */
131 }
132}
133
134static void FUZ_freeDictionary(buffer_t dict)
135{
136 free(dict.start);
137}
138
139
Yann Colletd7883a22016-08-12 16:48:02 +0200140static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
141{
Yann Colletb3060f72016-09-09 16:44:16 +0200142 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200143 void* CNBuffer = malloc(CNBufferSize);
144 size_t const skippableFrameSize = 11;
145 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
146 void* compressedBuffer = malloc(compressedBufferSize);
147 size_t const decodedBufferSize = CNBufferSize;
148 void* decodedBuffer = malloc(decodedBufferSize);
149 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200150 int testResult = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700151 U32 testNb = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200152 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
153 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Stella Lau90a31bf2017-08-30 14:36:54 -0700154 ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2);
155
Yann Collet9ffbeea2016-12-02 18:37:38 -0800156 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200157 ZSTD_outBuffer outBuff;
Yann Collet33fce032017-01-16 19:46:22 -0800158 buffer_t dictionary = g_nullBuffer;
Yann Collet30ab64e2017-05-10 11:30:19 -0700159 size_t const dictSize = 128 KB;
Yann Collet33fce032017-01-16 19:46:22 -0800160 unsigned dictID = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200161
162 /* Create compressible test buffer */
163 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
Yann Collet33fce032017-01-16 19:46:22 -0800164 DISPLAY("Not enough memory, aborting \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200165 goto _output_error;
166 }
167 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
168
Yann Collet33fce032017-01-16 19:46:22 -0800169 /* Create dictionary */
170 MEM_STATIC_ASSERT(COMPRESSIBLE_NOISE_LENGTH >= 4 MB);
171 dictionary = FUZ_createDictionary(CNBuffer, 4 MB, 4 KB, 40 KB);
172 if (!dictionary.start) {
173 DISPLAY("Error creating dictionary, aborting \n");
174 goto _output_error;
175 }
176 dictID = ZDICT_getDictID(dictionary.start, dictionary.filled);
177
Yann Colletd7883a22016-08-12 16:48:02 +0200178 /* generate skippable frame */
179 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
180 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
181 cSize = skippableFrameSize + 8;
182
183 /* Basic compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800184 DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet009d6042017-05-19 10:17:59 -0700185 { size_t const r = ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1);
186 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200187 outBuff.dst = (char*)(compressedBuffer)+cSize;
188 outBuff.size = compressedBufferSize;
189 outBuff.pos = 0;
190 inBuff.src = CNBuffer;
191 inBuff.size = CNBufferSize;
192 inBuff.pos = 0;
193 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200194 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200195 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
196 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200197 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200198 cSize += outBuff.pos;
Yann Collet736788f2017-01-19 12:12:50 -0800199 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Colletd7883a22016-08-12 16:48:02 +0200200
Yann Collet30ab64e2017-05-10 11:30:19 -0700201 /* context size functions */
202 DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++);
203 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize);
Stella Laub5b92752017-08-29 10:49:29 -0700204 size_t const s = ZSTD_estimateCStreamSize_advanced_usingCParams(cParams)
Yann Collet25989e32017-05-25 15:07:37 -0700205 /* uses ZSTD_initCStream_usingDict() */
Stella Lauc88fb922017-08-29 11:55:02 -0700206 + ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy);
Yann Collet30ab64e2017-05-10 11:30:19 -0700207 if (ZSTD_isError(s)) goto _output_error;
208 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
209 }
210
211 DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++);
Yann Colletdce78922017-06-21 15:53:42 -0700212 { size_t const s = ZSTD_sizeof_CStream(zc);
213 if (ZSTD_isError(s)) goto _output_error;
214 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200215 }
216
Yann Collet4b987ad2017-04-10 17:50:44 -0700217 /* Attempt bad compression parameters */
218 DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++);
219 { size_t r;
220 ZSTD_parameters params = ZSTD_getParams(1, 0, 0);
221 params.cParams.searchLength = 2;
222 r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
223 if (!ZSTD_isError(r)) goto _output_error;
224 DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r));
225 }
226
Yann Colletd7883a22016-08-12 16:48:02 +0200227 /* skippable frame test */
Yann Collet736788f2017-01-19 12:12:50 -0800228 DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++);
Yann Colletdce78922017-06-21 15:53:42 -0700229 if (ZSTD_isError( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) ))
230 goto _output_error;
Yann Collet53e17fb2016-08-17 01:39:22 +0200231 inBuff.src = compressedBuffer;
232 inBuff.size = cSize;
233 inBuff.pos = 0;
234 outBuff.dst = decodedBuffer;
235 outBuff.size = CNBufferSize;
236 outBuff.pos = 0;
Yann Colletdce78922017-06-21 15:53:42 -0700237 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
238 DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r);
239 if (r != 0) goto _output_error;
240 }
Yann Colleta33ae642017-02-28 01:15:28 -0800241 if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */
Yann Collet736788f2017-01-19 12:12:50 -0800242 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200243
244 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800245 inBuff2 = inBuff;
Yann Collet736788f2017-01-19 12:12:50 -0800246 DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collet30ab64e2017-05-10 11:30:19 -0700247 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800248 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000); /* large limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200249 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800250 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
251 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
252 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
253 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800254 DISPLAYLEVEL(3, "OK \n");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800255
256 /* Re-use without init */
Yann Collet736788f2017-01-19 12:12:50 -0800257 DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800258 outBuff.pos = 0;
259 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
260 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 +0200261 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
262 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800263 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200264
265 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800266 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200267 { size_t i;
268 for (i=0; i<CNBufferSize; i++) {
269 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
270 } }
Yann Collet736788f2017-01-19 12:12:50 -0800271 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200272
Yann Colletf16f4492017-05-09 16:18:17 -0700273 /* context size functions */
274 DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++);
275 { ZSTD_frameHeader fhi;
276 const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8);
277 size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize);
278 if (gfhError!=0) goto _output_error;
Yann Colletdde10b22017-06-26 17:44:26 -0700279 DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize);
280 { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize)
Yann Collet25989e32017-05-25 15:07:37 -0700281 /* uses ZSTD_initDStream_usingDict() */
Stella Lauc88fb922017-08-29 11:55:02 -0700282 + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy);
Yann Colletf16f4492017-05-09 16:18:17 -0700283 if (ZSTD_isError(s)) goto _output_error;
284 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
285 } }
286
287 DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200288 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200289 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800290 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Colletcb327632016-08-23 00:30:31 +0200291 }
292
Yann Colletd7883a22016-08-12 16:48:02 +0200293 /* Byte-by-byte decompression test */
Yann Collet736788f2017-01-19 12:12:50 -0800294 DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200295 { /* skippable frame */
296 size_t r = 1;
Yann Collet30ab64e2017-05-10 11:30:19 -0700297 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200298 inBuff.src = compressedBuffer;
299 outBuff.dst = decodedBuffer;
300 inBuff.pos = 0;
301 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200302 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200303 inBuff.size = inBuff.pos + 1;
304 outBuff.size = outBuff.pos + 1;
305 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200306 if (ZSTD_isError(r)) goto _output_error;
307 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200308 /* normal frame */
Yann Collet30ab64e2017-05-10 11:30:19 -0700309 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200310 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200311 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200312 inBuff.size = inBuff.pos + 1;
313 outBuff.size = outBuff.pos + 1;
314 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200315 if (ZSTD_isError(r)) goto _output_error;
316 }
317 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200318 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
319 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Collet736788f2017-01-19 12:12:50 -0800320 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200321
322 /* check regenerated data is byte exact */
Yann Collet736788f2017-01-19 12:12:50 -0800323 DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++);
Yann Colletd7883a22016-08-12 16:48:02 +0200324 { size_t i;
325 for (i=0; i<CNBufferSize; i++) {
326 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
327 } }
Yann Collet736788f2017-01-19 12:12:50 -0800328 DISPLAYLEVEL(3, "OK \n");
Yann Colletd7883a22016-08-12 16:48:02 +0200329
Yann Collete795c8a2016-12-13 16:39:36 +0100330 /* _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800331 DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
Yann Collete795c8a2016-12-13 16:39:36 +0100332 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
Yann Colletd564faa2016-12-18 21:39:15 +0100333 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100334 outBuff.size = compressedBufferSize;
335 outBuff.pos = 0;
336 inBuff.src = CNBuffer;
337 inBuff.size = CNBufferSize;
338 inBuff.pos = 0;
339 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
340 if (ZSTD_isError(r)) goto _output_error; }
341 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
342 { size_t const r = ZSTD_endStream(zc, &outBuff);
343 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Sean Purcell4e709712017-02-07 13:50:09 -0800344 { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos);
Yann Colletd564faa2016-12-18 21:39:15 +0100345 if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */
Yann Collet736788f2017-01-19 12:12:50 -0800346 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
Yann Collete795c8a2016-12-13 16:39:36 +0100347
348 /* wrong _srcSize compression test */
Yann Collet736788f2017-01-19 12:12:50 -0800349 DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
Yann Collete795c8a2016-12-13 16:39:36 +0100350 ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1);
Yann Colletd564faa2016-12-18 21:39:15 +0100351 outBuff.dst = (char*)(compressedBuffer);
Yann Collete795c8a2016-12-13 16:39:36 +0100352 outBuff.size = compressedBufferSize;
353 outBuff.pos = 0;
354 inBuff.src = CNBuffer;
355 inBuff.size = CNBufferSize;
356 inBuff.pos = 0;
357 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
358 if (ZSTD_isError(r)) goto _output_error; }
359 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
360 { size_t const r = ZSTD_endStream(zc, &outBuff);
361 if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
Yann Collet736788f2017-01-19 12:12:50 -0800362 DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); }
Yann Collete795c8a2016-12-13 16:39:36 +0100363
Yann Collet12083a42016-09-06 15:01:51 +0200364 /* Complex context re-use scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800365 DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200366 ZSTD_freeCStream(zc);
367 zc = ZSTD_createCStream_advanced(customMem);
368 if (zc==NULL) goto _output_error; /* memory allocation issue */
369 /* use 1 */
370 { size_t const inSize = 513;
Yann Collet0be6fd32017-05-08 16:08:01 -0700371 DISPLAYLEVEL(5, "use1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200372 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
373 inBuff.src = CNBuffer;
374 inBuff.size = inSize;
375 inBuff.pos = 0;
376 outBuff.dst = (char*)(compressedBuffer)+cSize;
377 outBuff.size = ZSTD_compressBound(inSize);
378 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700379 DISPLAYLEVEL(5, "compress1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200380 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
381 if (ZSTD_isError(r)) goto _output_error; }
382 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700383 DISPLAYLEVEL(5, "end1 ");
Yann Collet12083a42016-09-06 15:01:51 +0200384 { size_t const r = ZSTD_endStream(zc, &outBuff);
385 if (r != 0) goto _output_error; } /* error, or some data not flushed */
386 }
387 /* use 2 */
388 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
Yann Collet0be6fd32017-05-08 16:08:01 -0700389 DISPLAYLEVEL(5, "use2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200390 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
391 inBuff.src = CNBuffer;
392 inBuff.size = inSize;
393 inBuff.pos = 0;
394 outBuff.dst = (char*)(compressedBuffer)+cSize;
395 outBuff.size = ZSTD_compressBound(inSize);
396 outBuff.pos = 0;
Yann Collet0be6fd32017-05-08 16:08:01 -0700397 DISPLAYLEVEL(5, "compress2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200398 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
399 if (ZSTD_isError(r)) goto _output_error; }
400 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Yann Collet0be6fd32017-05-08 16:08:01 -0700401 DISPLAYLEVEL(5, "end2 ");
Yann Collet12083a42016-09-06 15:01:51 +0200402 { size_t const r = ZSTD_endStream(zc, &outBuff);
403 if (r != 0) goto _output_error; } /* error, or some data not flushed */
404 }
Yann Collet736788f2017-01-19 12:12:50 -0800405 DISPLAYLEVEL(3, "OK \n");
Yann Collet12083a42016-09-06 15:01:51 +0200406
Yann Collet95162342016-10-25 16:19:52 -0700407 /* CDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800408 DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++);
Yann Collet2e427422017-06-27 17:09:12 -0700409 { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ );
Yann Collet95162342016-10-25 16:19:52 -0700410 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
411 if (ZSTD_isError(initError)) goto _output_error;
412 cSize = 0;
413 outBuff.dst = compressedBuffer;
414 outBuff.size = compressedBufferSize;
415 outBuff.pos = 0;
416 inBuff.src = CNBuffer;
417 inBuff.size = CNBufferSize;
418 inBuff.pos = 0;
419 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
420 if (ZSTD_isError(r)) goto _output_error; }
421 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
422 { size_t const r = ZSTD_endStream(zc, &outBuff);
423 if (r != 0) goto _output_error; } /* error, or some data not flushed */
424 cSize = outBuff.pos;
425 ZSTD_freeCDict(cdict);
Yann Collet736788f2017-01-19 12:12:50 -0800426 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet95162342016-10-25 16:19:52 -0700427 }
428
Yann Collet736788f2017-01-19 12:12:50 -0800429 DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++);
Yann Collet12083a42016-09-06 15:01:51 +0200430 { size_t const s = ZSTD_sizeof_CStream(zc);
431 if (ZSTD_isError(s)) goto _output_error;
Yann Collet736788f2017-01-19 12:12:50 -0800432 DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s);
Yann Collet12083a42016-09-06 15:01:51 +0200433 }
434
Yann Collet33fce032017-01-16 19:46:22 -0800435 DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++);
436 { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
437 if (dID != dictID) goto _output_error;
438 DISPLAYLEVEL(4, "OK (%u) \n", dID);
439 }
440
Yann Collet335ad5d2016-10-25 17:47:02 -0700441 /* DDict scenario */
Yann Collet736788f2017-01-19 12:12:50 -0800442 DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
Yann Collet33fce032017-01-16 19:46:22 -0800443 { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled);
Yann Collet335ad5d2016-10-25 17:47:02 -0700444 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
445 if (ZSTD_isError(initError)) goto _output_error;
446 inBuff.src = compressedBuffer;
447 inBuff.size = cSize;
448 inBuff.pos = 0;
449 outBuff.dst = decodedBuffer;
450 outBuff.size = CNBufferSize;
451 outBuff.pos = 0;
452 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
453 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
454 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
455 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
456 ZSTD_freeDDict(ddict);
Yann Collet736788f2017-01-19 12:12:50 -0800457 DISPLAYLEVEL(3, "OK \n");
Yann Collet335ad5d2016-10-25 17:47:02 -0700458 }
459
Yann Collet12083a42016-09-06 15:01:51 +0200460 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet736788f2017-01-19 12:12:50 -0800461 DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
Yann Collet17e482e2016-08-23 16:58:10 +0200462 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
463 if (!ZSTD_isError(r)) goto _output_error; }
Yann Collet736788f2017-01-19 12:12:50 -0800464 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200465
466 /* Memory restriction */
Yann Collet736788f2017-01-19 12:12:50 -0800467 DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++);
Yann Collet30ab64e2017-05-10 11:30:19 -0700468 ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
Yann Colletbb002742017-01-25 16:25:38 -0800469 { size_t const r = ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000); /* too small limit */
Yann Collet17e482e2016-08-23 16:58:10 +0200470 if (ZSTD_isError(r)) goto _output_error; }
471 inBuff.src = compressedBuffer;
472 inBuff.size = cSize;
473 inBuff.pos = 0;
474 outBuff.dst = decodedBuffer;
475 outBuff.size = CNBufferSize;
476 outBuff.pos = 0;
477 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
478 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
Yann Collet736788f2017-01-19 12:12:50 -0800479 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
Yann Collet17e482e2016-08-23 16:58:10 +0200480
Yann Collet7d283cd2017-04-27 14:48:34 -0700481 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++);
482 { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled);
483 ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */};
Stella Lauc88fb922017-08-29 11:55:02 -0700484 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, customMem);
Yann Collet8c910d22017-06-03 01:15:02 -0700485 size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
Yann Collet7d283cd2017-04-27 14:48:34 -0700486 if (ZSTD_isError(initError)) goto _output_error;
Nick Terrell62ecad32017-04-03 20:56:39 -0700487 cSize = 0;
488 outBuff.dst = compressedBuffer;
489 outBuff.size = compressedBufferSize;
490 outBuff.pos = 0;
491 inBuff.src = CNBuffer;
492 inBuff.size = CNBufferSize;
493 inBuff.pos = 0;
494 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
495 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet8c910d22017-06-03 01:15:02 -0700496 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
Nick Terrell62ecad32017-04-03 20:56:39 -0700497 { size_t const r = ZSTD_endStream(zc, &outBuff);
498 if (r != 0) goto _output_error; } /* error, or some data not flushed */
499 cSize = outBuff.pos;
500 ZSTD_freeCDict(cdict);
501 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
Yann Collet4ee6b152017-04-11 11:59:44 -0700502 }
Nick Terrell62ecad32017-04-03 20:56:39 -0700503
Yann Colleta92cbb72017-04-27 15:08:56 -0700504 DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++);
505 { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize);
506 if (did != 0) goto _output_error;
507 }
508 DISPLAYLEVEL(3, "OK (not detected) \n");
509
Yann Collet4ee6b152017-04-11 11:59:44 -0700510 DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++);
511 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
512 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
513 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
Nick Terrell62ecad32017-04-03 20:56:39 -0700514 }
515
Yann Collet62f7efc2017-06-28 16:25:13 -0700516 DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++);
517 { size_t const refErr = ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled);
518 if (ZSTD_isError(refErr)) goto _output_error; }
519 outBuff.dst = compressedBuffer;
520 outBuff.size = compressedBufferSize;
521 outBuff.pos = 0;
522 inBuff.src = CNBuffer;
523 inBuff.size = CNBufferSize;
524 inBuff.pos = 0;
525 { size_t const r = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
526 if (ZSTD_isError(r)) goto _output_error; }
527 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
528 cSize = outBuff.pos;
529 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
530
531 DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++);
532 { size_t const r = ZSTD_decompress_usingDict(zd,
533 decodedBuffer, CNBufferSize,
534 compressedBuffer, cSize,
535 dictionary.start, dictionary.filled);
536 if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
537 DISPLAYLEVEL(3, "OK \n");
538 }
539
540 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++);
541 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
542 if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
543 DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r));
544 }
545
546 DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++);
547 outBuff.dst = compressedBuffer;
548 outBuff.size = compressedBufferSize;
549 outBuff.pos = 0;
550 inBuff.src = CNBuffer;
551 inBuff.size = CNBufferSize;
552 inBuff.pos = 0;
553 { size_t const r = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
554 if (ZSTD_isError(r)) goto _output_error; }
555 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
556 cSize = outBuff.pos;
557 DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
558
559 DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++);
560 { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
561 if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
562 DISPLAYLEVEL(3, "OK \n");
563 }
564
Yann Collet0bb381d2017-04-18 15:08:52 -0700565 /* Empty srcSize */
566 DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++);
567 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
568 params.fParams.contentSizeFlag = 1;
569 ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0);
570 } /* cstream advanced shall write content size = 0 */
571 inBuff.src = CNBuffer;
572 inBuff.size = 0;
573 inBuff.pos = 0;
574 outBuff.dst = compressedBuffer;
575 outBuff.size = compressedBufferSize;
576 outBuff.pos = 0;
577 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
578 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
579 cSize = outBuff.pos;
580 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
581 DISPLAYLEVEL(3, "OK \n");
582
Sean Purcell2db72492017-02-09 10:50:43 -0800583 DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
584 { ZSTD_parameters params = ZSTD_getParams(5, 0, 0);
585 params.fParams.contentSizeFlag = 1;
Yann Collet4ee6b152017-04-11 11:59:44 -0700586 ZSTD_initCStream_advanced(zc, NULL, 0, params, 0);
587 } /* cstream advanced shall write content size = 0 */
Sean Purcell2db72492017-02-09 10:50:43 -0800588 inBuff.src = CNBuffer;
589 inBuff.size = 0;
590 inBuff.pos = 0;
591 outBuff.dst = compressedBuffer;
592 outBuff.size = compressedBufferSize;
593 outBuff.pos = 0;
594 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
595 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
596 cSize = outBuff.pos;
597 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error;
598
599 ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */
600 inBuff.src = CNBuffer;
601 inBuff.size = 0;
602 inBuff.pos = 0;
603 outBuff.dst = compressedBuffer;
604 outBuff.size = compressedBufferSize;
605 outBuff.pos = 0;
606 if (ZSTD_isError(ZSTD_compressStream(zc, &outBuff, &inBuff))) goto _output_error;
607 if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error;
608 cSize = outBuff.pos;
609 if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error;
610 DISPLAYLEVEL(3, "OK \n");
Yann Collet17e482e2016-08-23 16:58:10 +0200611
Stella Lau90a31bf2017-08-30 14:36:54 -0700612 /* Basic multithreading compression test */
613 DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
614 { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
615 size_t const r = ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize);
616 if (ZSTD_isError(r)) goto _output_error; }
617 outBuff.dst = (char*)(compressedBuffer);
618 outBuff.size = compressedBufferSize;
619 outBuff.pos = 0;
620 inBuff.src = CNBuffer;
621 inBuff.size = CNBufferSize;
622 inBuff.pos = 0;
623 { size_t const r = ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end);
624 if (ZSTD_isError(r)) goto _output_error; }
625 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
626 { size_t const r = ZSTDMT_endStream(mtctx, &outBuff);
627 if (r != 0) goto _output_error; } /* error, or some data not flushed */
628 DISPLAYLEVEL(3, "OK \n");
629
630
Sean Purcell887eaa92017-02-15 16:43:45 -0800631 /* Overlen overwriting window data bug */
632 DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
Sean Purcell0ed39012017-02-16 13:29:47 -0800633 { /* This test has a window size of 1024 bytes and consists of 3 blocks:
634 1. 'a' repeated 517 times
635 2. 'b' repeated 516 times
636 3. a compressed block with no literals and 3 sequence commands:
637 litlength = 0, offset = 24, match length = 24
638 litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3)
639 litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */
640
641 const char* testCase =
642 "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A"
643 "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00"
644 "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0";
Sean Purcell887eaa92017-02-15 16:43:45 -0800645 ZSTD_DStream* zds = ZSTD_createDStream();
646
647 ZSTD_initDStream(zds);
648 inBuff.src = testCase;
Sean Purcell0ed39012017-02-16 13:29:47 -0800649 inBuff.size = 47;
Sean Purcell887eaa92017-02-15 16:43:45 -0800650 inBuff.pos = 0;
651 outBuff.dst = decodedBuffer;
652 outBuff.size = CNBufferSize;
653 outBuff.pos = 0;
654
655 while (inBuff.pos < inBuff.size) {
656 size_t const r = ZSTD_decompressStream(zds, &outBuff, &inBuff);
657 /* Bug will cause checksum to fail */
658 if (ZSTD_isError(r)) goto _output_error;
659 }
Przemyslaw Skibinski684858e2017-02-21 18:17:24 +0100660
661 ZSTD_freeDStream(zds);
Sean Purcell887eaa92017-02-15 16:43:45 -0800662 }
663 DISPLAYLEVEL(3, "OK \n");
664
Yann Colletd7883a22016-08-12 16:48:02 +0200665_end:
Yann Collet33fce032017-01-16 19:46:22 -0800666 FUZ_freeDictionary(dictionary);
Yann Colletd7883a22016-08-12 16:48:02 +0200667 ZSTD_freeCStream(zc);
668 ZSTD_freeDStream(zd);
Stella Lau90a31bf2017-08-30 14:36:54 -0700669 ZSTDMT_freeCCtx(mtctx);
Yann Colletd7883a22016-08-12 16:48:02 +0200670 free(CNBuffer);
671 free(compressedBuffer);
672 free(decodedBuffer);
673 return testResult;
674
675_output_error:
676 testResult = 1;
677 DISPLAY("Error detected in Unit tests ! \n");
678 goto _end;
679}
680
681
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200682/* ====== Fuzzer tests ====== */
683
Yann Colletd7883a22016-08-12 16:48:02 +0200684static size_t findDiff(const void* buf1, const void* buf2, size_t max)
685{
686 const BYTE* b1 = (const BYTE*)buf1;
687 const BYTE* b2 = (const BYTE*)buf2;
688 size_t u;
689 for (u=0; u<max; u++) {
690 if (b1[u] != b2[u]) break;
691 }
Yann Collet736788f2017-01-19 12:12:50 -0800692 DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max);
Yann Colletbb002742017-01-25 16:25:38 -0800693 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
694 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]);
695 DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n",
696 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 +0200697 return u;
698}
699
700static size_t FUZ_rLogLength(U32* seed, U32 logLength)
701{
702 size_t const lengthMask = ((size_t)1 << logLength) - 1;
703 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
704}
705
706static size_t FUZ_randomLength(U32* seed, U32 maxLog)
707{
708 U32 const logLength = FUZ_rand(seed) % maxLog;
709 return FUZ_rLogLength(seed, logLength);
710}
711
712#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
713
Stella Lau9e406022017-09-06 08:39:46 -0700714/* Return value in range minVal <= v <= maxVal */
715static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal)
716{
717 U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal;
718 return (U32)((FUZ_rand(seed) % mod) + minVal);
719}
720
Yann Collet01743a32017-06-16 17:56:41 -0700721#define CHECK(cond, ...) { \
722 if (cond) { \
723 DISPLAY("Error => "); \
724 DISPLAY(__VA_ARGS__); \
725 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
726 goto _output_error; \
727} }
728
729#define CHECK_Z(f) { \
730 size_t const err = f; \
731 if (ZSTD_isError(err)) { \
732 DISPLAY("Error => %s : %s ", \
733 #f, ZSTD_getErrorName(err)); \
734 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); \
735 goto _output_error; \
736} }
Yann Colletd7883a22016-08-12 16:48:02 +0200737
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700738static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Colletd7883a22016-08-12 16:48:02 +0200739{
Yann Colletf99c2c12017-06-21 23:35:58 -0700740 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Colletd7883a22016-08-12 16:48:02 +0200741 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200742 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200743 BYTE* cNoiseBuffer[5];
Yann Colletbc32b402017-09-27 17:27:38 -0700744 size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200745 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
Yann Colletbc32b402017-09-27 17:27:38 -0700746 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200747 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
748 size_t const dstBufferSize = srcBufferSize;
749 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200750 U32 result = 0;
751 U32 testNb = 0;
752 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -0700753 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */
754 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200755 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
756 clock_t const startClock = clock();
Yann Colletbc32b402017-09-27 17:27:38 -0700757 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200758 size_t dictSize = 0;
759 U32 oldTestLog = 0;
Yann Collet49f84592017-06-21 18:43:39 -0700760 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests;
Yann Colletd7883a22016-08-12 16:48:02 +0200761
762 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200763 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
764 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
765 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
766 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
767 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200768 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200769 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200770 "Not enough memory, fuzzer tests cancelled");
771
772 /* Create initial samples */
773 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
774 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
775 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
776 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
777 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
778 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200779 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200780
781 /* catch up testNb */
782 for (testNb=1; testNb < startTest; testNb++)
783 FUZ_rand(&coreSeed);
784
785 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700786 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200787 U32 lseed;
788 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200789 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200790 XXH64_state_t xxhState;
791 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200792 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200793 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200794
795 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700796 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
797 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200798 FUZ_rand(&coreSeed);
Yann Collet33fce032017-01-16 19:46:22 -0800799 lseed = coreSeed ^ prime32;
Yann Colletd7883a22016-08-12 16:48:02 +0200800
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200801 /* states full reset (deliberately not synchronized) */
802 /* some issues can only happen when reusing states */
Yann Colleted1d0392017-06-19 11:07:33 -0700803 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
804 ZSTD_freeCStream(zc);
805 zc = ZSTD_createCStream();
Yann Colletdce78922017-06-21 15:53:42 -0700806 CHECK(zc==NULL, "ZSTD_createCStream : allocation error");
Yann Colleted1d0392017-06-19 11:07:33 -0700807 resetAllowed=0;
808 }
809 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
810 ZSTD_freeDStream(zd);
811 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -0700812 CHECK(zd==NULL, "ZSTD_createDStream : allocation error");
813 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Colleted1d0392017-06-19 11:07:33 -0700814 }
Yann Colletd7883a22016-08-12 16:48:02 +0200815
816 /* srcBuffer selection [0-4] */
817 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
818 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
819 else {
820 buffNb >>= 3;
821 if (buffNb & 7) {
822 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
823 buffNb = tnb[buffNb >> 3];
824 } else {
825 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
826 buffNb = tnb[buffNb >> 3];
827 } }
828 srcBuffer = cNoiseBuffer[buffNb];
829 }
830
831 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200832 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
833 && oldTestLog /* at least one test happened */ && resetAllowed) {
834 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
Yann Colletbc32b402017-09-27 17:27:38 -0700835 maxTestSize = MIN(maxTestSize, srcBufferSize-16);
Yann Colletcf409a72016-09-26 16:41:05 +0200836 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
Yann Collet01743a32017-06-16 17:56:41 -0700837 CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) );
Yann Colletcf409a72016-09-26 16:41:05 +0200838 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200839 } else {
840 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -0700841 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -0700842 U32 const cLevelCandidate = ( FUZ_rand(&lseed) %
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700843 (ZSTD_maxCLevel() -
Yann Colletbfc2f002017-06-21 17:57:14 -0700844 (MAX(testLog, dictLog) / 3)))
Yann Colletce800982017-04-05 16:34:09 -0700845 + 1;
Yann Colletbfc2f002017-06-21 17:57:14 -0700846 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Colletd7883a22016-08-12 16:48:02 +0200847 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200848 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200849 /* random dictionary selection */
Yann Colletf99c2c12017-06-21 23:35:58 -0700850 dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200851 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
852 dict = srcBuffer + dictStart;
853 }
Yann Colletcf409a72016-09-26 16:41:05 +0200854 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
855 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200856 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
857 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Collet01743a32017-06-16 17:56:41 -0700858 CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
859 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200860
861 /* multi-segments compression test */
862 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200863 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200864 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200865 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Collete795c8a2016-12-13 16:39:36 +0100866 /* compress random chunks into randomly sized dst buffers */
Yann Collet2f263942016-09-26 14:06:08 +0200867 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
868 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
869 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200870 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
871 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200872 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200873 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200874
Yann Colleted1d0392017-06-19 11:07:33 -0700875 CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) );
Yann Colletd7883a22016-08-12 16:48:02 +0200876
Yann Collet53e17fb2016-08-17 01:39:22 +0200877 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
878 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
879 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200880 }
881
882 /* random flush operation, to mess around */
883 if ((FUZ_rand(&lseed) & 15) == 0) {
884 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200885 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
886 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Colleted1d0392017-06-19 11:07:33 -0700887 CHECK_Z( ZSTD_flushStream(zc, &outBuff) );
888 } }
Yann Colletd7883a22016-08-12 16:48:02 +0200889
890 /* final frame epilogue */
891 { size_t remainingToFlush = (size_t)(-1);
892 while (remainingToFlush) {
893 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
894 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200895 outBuff.size = outBuff.pos + adjustedDstSize;
896 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Collet009d6042017-05-19 10:17:59 -0700897 CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Colletd7883a22016-08-12 16:48:02 +0200898 } }
899 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200900 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200901 }
902
903 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200904 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -0700905 CHECK_Z ( ZSTD_resetDStream(zd) );
Yann Collet9ffbeea2016-12-02 18:37:38 -0800906 } else {
Yann Colletdce78922017-06-21 15:53:42 -0700907 CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet9ffbeea2016-12-02 18:37:38 -0800908 }
Yann Colletd7883a22016-08-12 16:48:02 +0200909 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200910 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
911 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
912 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200913 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
914 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
915 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200916 inBuff.size = inBuff.pos + readCSrcSize;
917 outBuff.size = inBuff.pos + dstBuffSize;
918 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletb3f33cc2017-09-09 14:37:28 -0700919 if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) {
920 DISPLAY("checksum error : \n");
921 findDiff(copyBuffer, dstBuffer, totalTestSize);
922 }
923 CHECK( ZSTD_isError(decompressionResult), "decompression error : %s",
924 ZSTD_getErrorName(decompressionResult) );
Yann Colletd7883a22016-08-12 16:48:02 +0200925 }
926 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Colletb3f33cc2017-09-09 14:37:28 -0700927 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)",
928 (U32)outBuff.pos, (U32)totalTestSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200929 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200930 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
931 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
932 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
933 } }
934
935 /*===== noisy/erroneous src decompression test =====*/
936
937 /* add some noise */
938 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
939 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
940 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
941 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
942 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
943 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
944 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
945 } }
946
947 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -0700948 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200949 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
950 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
951 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200952 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
953 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200954 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Yann Collet64bf8ff2017-01-27 17:25:07 -0800955 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200956 outBuff.size = outBuff.pos + adjustedDstSize;
Yann Collet64bf8ff2017-01-27 17:25:07 -0800957 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet53e17fb2016-08-17 01:39:22 +0200958 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200959 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Collet64bf8ff2017-01-27 17:25:07 -0800960 /* No forward progress possible */
961 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Colletd7883a22016-08-12 16:48:02 +0200962 } } } }
963 DISPLAY("\r%u fuzzer tests completed \n", testNb);
964
965_cleanup:
966 ZSTD_freeCStream(zc);
967 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200968 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200969 free(cNoiseBuffer[0]);
970 free(cNoiseBuffer[1]);
971 free(cNoiseBuffer[2]);
972 free(cNoiseBuffer[3]);
973 free(cNoiseBuffer[4]);
974 free(copyBuffer);
975 free(cBuffer);
976 free(dstBuffer);
977 return result;
978
979_output_error:
980 result = 1;
981 goto _cleanup;
982}
983
984
Yann Collet736788f2017-01-19 12:12:50 -0800985/* Multi-threading version of fuzzer Tests */
Sean Purcell7ebf2de2017-03-20 11:25:00 -0700986static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
Yann Collet736788f2017-01-19 12:12:50 -0800987{
Yann Colletf99c2c12017-06-21 23:35:58 -0700988 const U32 maxSrcLog = bigTests ? 24 : 22;
Yann Collet736788f2017-01-19 12:12:50 -0800989 static const U32 maxSampleLog = 19;
990 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
991 BYTE* cNoiseBuffer[5];
992 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
993 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
994 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
995 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
996 size_t const dstBufferSize = srcBufferSize;
997 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
998 U32 result = 0;
999 U32 testNb = 0;
1000 U32 coreSeed = seed;
Yann Colletbc32b402017-09-27 17:27:38 -07001001 U32 nbThreads = 2;
1002 ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */
Yann Collet736788f2017-01-19 12:12:50 -08001003 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1004 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
1005 clock_t const startClock = clock();
1006 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
1007 size_t dictSize = 0;
1008 U32 oldTestLog = 0;
Yann Colletbc32b402017-09-27 17:27:38 -07001009 int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1010 U32 const nbThreadsMax = bigTests ? 4 : 2;
Yann Collet736788f2017-01-19 12:12:50 -08001011
1012 /* allocations */
1013 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1014 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1015 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1016 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1017 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1018 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1019 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1020 "Not enough memory, fuzzer tests cancelled");
1021
1022 /* Create initial samples */
1023 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1024 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1025 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1026 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1027 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1028 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
1029 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1030
1031 /* catch up testNb */
1032 for (testNb=1; testNb < startTest; testNb++)
1033 FUZ_rand(&coreSeed);
1034
1035 /* test loop */
1036 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
1037 U32 lseed;
1038 const BYTE* srcBuffer;
1039 size_t totalTestSize, totalGenSize, cSize;
1040 XXH64_state_t xxhState;
1041 U64 crcOrig;
1042 U32 resetAllowed = 1;
1043 size_t maxTestSize;
1044
1045 /* init */
Yann Colletbc32b402017-09-27 17:27:38 -07001046 if (testNb < nbTests) {
1047 DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests);
1048 } else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Collet736788f2017-01-19 12:12:50 -08001049 FUZ_rand(&coreSeed);
Yann Colletd7e3cb52017-01-20 16:44:50 -08001050 lseed = coreSeed ^ prime32;
Yann Collet736788f2017-01-19 12:12:50 -08001051
1052 /* states full reset (deliberately not synchronized) */
1053 /* some issues can only happen when reusing states */
1054 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
Yann Colletbfc2f002017-06-21 17:57:14 -07001055 U32 const nbThreadsCandidate = (FUZ_rand(&lseed) % 6) + 1;
Yann Colletbc32b402017-09-27 17:27:38 -07001056 nbThreads = MIN(nbThreadsCandidate, nbThreadsMax);
Yann Collet30c76982017-03-31 18:27:03 -07001057 DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads);
Yann Collet736788f2017-01-19 12:12:50 -08001058 ZSTDMT_freeCCtx(zc);
1059 zc = ZSTDMT_createCCtx(nbThreads);
Yann Colletdce78922017-06-21 15:53:42 -07001060 CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001061 resetAllowed=0;
1062 }
1063 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1064 ZSTD_freeDStream(zd);
1065 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001066 CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error")
Yann Collet736788f2017-01-19 12:12:50 -08001067 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1068 }
1069
1070 /* srcBuffer selection [0-4] */
1071 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1072 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1073 else {
1074 buffNb >>= 3;
1075 if (buffNb & 7) {
1076 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1077 buffNb = tnb[buffNb >> 3];
1078 } else {
1079 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1080 buffNb = tnb[buffNb >> 3];
1081 } }
1082 srcBuffer = cNoiseBuffer[buffNb];
1083 }
1084
1085 /* compression init */
1086 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1087 && oldTestLog /* at least one test happened */ && resetAllowed) {
1088 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1089 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
1090 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Yann Colletdce78922017-06-21 15:53:42 -07001091 CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) );
Yann Collet736788f2017-01-19 12:12:50 -08001092 }
1093 } else {
1094 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Sean Purcellf5e50512017-03-15 15:04:54 -07001095 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbc32b402017-09-27 17:27:38 -07001096 int const cLevelCandidate = ( FUZ_rand(&lseed)
1097 % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) )
1098 + 1;
1099 int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */
1100 int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */
1101 int const cLevel = MIN(cLevelMin, cLevelMax);
Yann Collet736788f2017-01-19 12:12:50 -08001102 maxTestSize = FUZ_rLogLength(&lseed, testLog);
1103 oldTestLog = testLog;
1104 /* random dictionary selection */
Sean Purcellf5e50512017-03-15 15:04:54 -07001105 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
Yann Collet19d670b2017-01-19 15:32:07 -08001106 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1107 dict = srcBuffer + dictStart;
1108 }
1109 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
1110 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletce800982017-04-05 16:34:09 -07001111 DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n",
1112 params.cParams.windowLog, (U32)pledgedSrcSize);
Yann Collet19d670b2017-01-19 15:32:07 -08001113 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
1114 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletce800982017-04-05 16:34:09 -07001115 params.fParams.contentSizeFlag = pledgedSrcSize>0;
Yann Collet2c5514c2017-04-18 22:52:41 -07001116 DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag);
Yann Colletdce78922017-06-21 15:53:42 -07001117 CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) );
1118 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) );
1119 CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) );
Yann Colletcd23dd22017-01-30 12:46:35 -08001120 } }
Yann Collet736788f2017-01-19 12:12:50 -08001121
1122 /* multi-segments compression test */
1123 XXH64_reset(&xxhState, 0);
1124 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
1125 U32 n;
1126 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
1127 /* compress random chunks into randomly sized dst buffers */
1128 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1129 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
1130 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
1131 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1132 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1133 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1134 outBuff.size = outBuff.pos + dstBuffSize;
1135
1136 DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize);
Yann Colletdce78922017-06-21 15:53:42 -07001137 CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) );
Yann Collet19d670b2017-01-19 15:32:07 -08001138 DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos);
Yann Collet736788f2017-01-19 12:12:50 -08001139
1140 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1141 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1142 totalTestSize += inBuff.pos;
1143 }
1144
1145 /* random flush operation, to mess around */
1146 if ((FUZ_rand(&lseed) & 15) == 0) {
1147 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1148 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1149 outBuff.size = outBuff.pos + adjustedDstSize;
1150 DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize);
Yann Colletdce78922017-06-21 15:53:42 -07001151 CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) );
1152 } }
Yann Collet736788f2017-01-19 12:12:50 -08001153
1154 /* final frame epilogue */
1155 { size_t remainingToFlush = (size_t)(-1);
1156 while (remainingToFlush) {
1157 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1158 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1159 outBuff.size = outBuff.pos + adjustedDstSize;
1160 DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize);
1161 remainingToFlush = ZSTDMT_endStream(zc, &outBuff);
Yann Collet30c76982017-03-31 18:27:03 -07001162 CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush));
Yann Collet736788f2017-01-19 12:12:50 -08001163 DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush);
1164 } }
Yann Collet736788f2017-01-19 12:12:50 -08001165 crcOrig = XXH64_digest(&xxhState);
1166 cSize = outBuff.pos;
Yann Collet2c5514c2017-04-18 22:52:41 -07001167 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001168 }
1169
1170 /* multi - fragments decompression test */
1171 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Colletdce78922017-06-21 15:53:42 -07001172 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet736788f2017-01-19 12:12:50 -08001173 } else {
Yann Colletdce78922017-06-21 15:53:42 -07001174 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet736788f2017-01-19 12:12:50 -08001175 }
1176 { size_t decompressionResult = 1;
1177 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1178 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1179 for (totalGenSize = 0 ; decompressionResult ; ) {
1180 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1181 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1182 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1183 inBuff.size = inBuff.pos + readCSrcSize;
1184 outBuff.size = inBuff.pos + dstBuffSize;
Yann Colletce800982017-04-05 16:34:09 -07001185 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001186 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1187 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Collet2c5514c2017-04-18 22:52:41 -07001188 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001189 }
Yann Colletbb002742017-01-25 16:25:38 -08001190 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1191 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
Yann Collet736788f2017-01-19 12:12:50 -08001192 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1193 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1194 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1195 } }
1196
1197 /*===== noisy/erroneous src decompression test =====*/
1198
1199 /* add some noise */
1200 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1201 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1202 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1203 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1204 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1205 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1206 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1207 } }
1208
1209 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001210 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet736788f2017-01-19 12:12:50 -08001211 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1212 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1213 while (outBuff.pos < dstBufferSize) {
1214 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1215 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1216 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
Nick Terrelld98bf492017-01-27 15:42:36 -08001217 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
Yann Collet736788f2017-01-19 12:12:50 -08001218 outBuff.size = outBuff.pos + adjustedDstSize;
Nick Terrelld98bf492017-01-27 15:42:36 -08001219 inBuff.size = inBuff.pos + adjustedCSrcSize;
Yann Collet736788f2017-01-19 12:12:50 -08001220 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1221 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Nick Terrelld98bf492017-01-27 15:42:36 -08001222 /* No forward progress possible */
1223 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
Yann Collet736788f2017-01-19 12:12:50 -08001224 } } } }
1225 DISPLAY("\r%u fuzzer tests completed \n", testNb);
1226
1227_cleanup:
1228 ZSTDMT_freeCCtx(zc);
1229 ZSTD_freeDStream(zd);
1230 ZSTD_freeDStream(zd_noise);
1231 free(cNoiseBuffer[0]);
1232 free(cNoiseBuffer[1]);
1233 free(cNoiseBuffer[2]);
1234 free(cNoiseBuffer[3]);
1235 free(cNoiseBuffer[4]);
1236 free(copyBuffer);
1237 free(cBuffer);
1238 free(dstBuffer);
1239 return result;
1240
1241_output_error:
1242 result = 1;
1243 goto _cleanup;
1244}
1245
Stella Lau73c73bf2017-08-21 12:41:19 -07001246/** If useOpaqueAPI, sets param in cctxParams.
1247 * Otherwise, sets the param in zc. */
1248static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
1249 ZSTD_cParameter param, unsigned value,
1250 U32 useOpaqueAPI)
1251{
1252 if (useOpaqueAPI) {
1253 return ZSTD_CCtxParam_setParameter(cctxParams, param, value);
1254 } else {
1255 return ZSTD_CCtx_setParameter(zc, param, value);
1256 }
1257}
Yann Collet736788f2017-01-19 12:12:50 -08001258
Yann Colletd7a3bff2017-06-19 11:53:01 -07001259/* Tests for ZSTD_compress_generic() API */
Stella Lau73c73bf2017-08-21 12:41:19 -07001260static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI)
Yann Collet01743a32017-06-16 17:56:41 -07001261{
Yann Colletf99c2c12017-06-21 23:35:58 -07001262 U32 const maxSrcLog = bigTests ? 24 : 22;
Yann Collet01743a32017-06-16 17:56:41 -07001263 static const U32 maxSampleLog = 19;
1264 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
1265 BYTE* cNoiseBuffer[5];
1266 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
1267 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
1268 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
1269 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
1270 size_t const dstBufferSize = srcBufferSize;
1271 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
1272 U32 result = 0;
1273 U32 testNb = 0;
1274 U32 coreSeed = seed;
1275 ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */
1276 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
1277 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
1278 clock_t const startClock = clock();
Yann Colletd7a3bff2017-06-19 11:53:01 -07001279 const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */
Yann Collet01743a32017-06-16 17:56:41 -07001280 size_t dictSize = 0;
1281 U32 oldTestLog = 0;
Yann Colletaa800c42017-09-27 18:00:15 -07001282 U32 windowLogMalus = 0; /* can survive between 2 loops */
1283 U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests;
1284 U32 const nbThreadsMax = bigTests ? 4 : 2;
Stella Lau023b24e2017-08-20 22:55:07 -07001285 ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
Yann Collet01743a32017-06-16 17:56:41 -07001286
1287 /* allocations */
1288 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
1289 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
1290 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
1291 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
1292 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
1293 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
1294 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
1295 "Not enough memory, fuzzer tests cancelled");
1296
1297 /* Create initial samples */
1298 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
1299 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
1300 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
1301 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
1302 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
1303 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Colletdce78922017-06-21 15:53:42 -07001304 CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */
Yann Collet01743a32017-06-16 17:56:41 -07001305
1306 /* catch up testNb */
1307 for (testNb=1; testNb < startTest; testNb++)
1308 FUZ_rand(&coreSeed);
1309
1310 /* test loop */
1311 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
1312 U32 lseed;
1313 const BYTE* srcBuffer;
1314 size_t totalTestSize, totalGenSize, cSize;
1315 XXH64_state_t xxhState;
1316 U64 crcOrig;
1317 U32 resetAllowed = 1;
1318 size_t maxTestSize;
1319
1320 /* init */
1321 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
1322 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
1323 FUZ_rand(&coreSeed);
1324 lseed = coreSeed ^ prime32;
Yann Collet9b5b47a2017-09-28 01:25:40 -07001325 DISPLAYLEVEL(5, " *** Test %u *** \n", testNb);
Yann Collet01743a32017-06-16 17:56:41 -07001326
1327 /* states full reset (deliberately not synchronized) */
1328 /* some issues can only happen when reusing states */
1329 if ((FUZ_rand(&lseed) & 0xFF) == 131) {
1330 DISPLAYLEVEL(5, "Creating new context \n");
1331 ZSTD_freeCCtx(zc);
1332 zc = ZSTD_createCCtx();
Yann Colletdce78922017-06-21 15:53:42 -07001333 CHECK(zc==NULL, "ZSTD_createCCtx allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001334 resetAllowed=0;
1335 }
1336 if ((FUZ_rand(&lseed) & 0xFF) == 132) {
1337 ZSTD_freeDStream(zd);
1338 zd = ZSTD_createDStream();
Yann Colletdce78922017-06-21 15:53:42 -07001339 CHECK(zd==NULL, "ZSTD_createDStream allocation error");
Yann Collet01743a32017-06-16 17:56:41 -07001340 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
1341 }
1342
1343 /* srcBuffer selection [0-4] */
1344 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
1345 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
1346 else {
1347 buffNb >>= 3;
1348 if (buffNb & 7) {
1349 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
1350 buffNb = tnb[buffNb >> 3];
1351 } else {
1352 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
1353 buffNb = tnb[buffNb >> 3];
1354 } }
1355 srcBuffer = cNoiseBuffer[buffNb];
1356 }
1357
1358 /* compression init */
Yann Colletb7372932017-06-27 15:49:12 -07001359 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/
Yann Collet01743a32017-06-16 17:56:41 -07001360 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
1361 && oldTestLog /* at least one test happened */ && resetAllowed) {
1362 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
1363 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
Yann Collet01743a32017-06-16 17:56:41 -07001364 { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
Stella Lau73c73bf2017-08-21 12:41:19 -07001365 CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001366 }
1367 } else {
1368 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
1369 U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletbfc2f002017-06-21 17:57:14 -07001370 U32 const cLevelCandidate = (FUZ_rand(&lseed) %
Yann Collet01743a32017-06-16 17:56:41 -07001371 (ZSTD_maxCLevel() -
Yann Colletbfc2f002017-06-21 17:57:14 -07001372 (MAX(testLog, dictLog) / 3))) +
Yann Collet01743a32017-06-16 17:56:41 -07001373 1;
Yann Colletbfc2f002017-06-21 17:57:14 -07001374 U32 const cLevel = MIN(cLevelCandidate, cLevelMax);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001375 DISPLAYLEVEL(5, "t%u: cLevel : %u \n", testNb, cLevel);
Yann Collet01743a32017-06-16 17:56:41 -07001376 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Collet9b5b47a2017-09-28 01:25:40 -07001377 DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize);
Yann Collet01743a32017-06-16 17:56:41 -07001378 oldTestLog = testLog;
1379 /* random dictionary selection */
1380 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0;
1381 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
1382 dict = srcBuffer + dictStart;
Yann Colletd7a3bff2017-06-19 11:53:01 -07001383 if (!dictSize) dict=NULL;
Yann Collet01743a32017-06-16 17:56:41 -07001384 }
1385 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
1386 ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
1387
1388 /* mess with compression parameters */
1389 cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
Nick Terrellc233bdb2017-09-22 14:04:39 -07001390 cParams.windowLog = MIN(windowLogMax, cParams.windowLog);
Yann Collet01743a32017-06-16 17:56:41 -07001391 cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
1392 cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
1393 cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
1394 cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001395 cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
Yann Collet01743a32017-06-16 17:56:41 -07001396 cParams = ZSTD_adjustCParams(cParams, 0, 0);
1397
Yann Colletaa800c42017-09-27 18:00:15 -07001398 if (FUZ_rand(&lseed) & 1) {
1399 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) );
1400 assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */
1401 windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5;
Yann Collet9b5b47a2017-09-28 01:25:40 -07001402 DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog);
Yann Colletaa800c42017-09-27 18:00:15 -07001403 }
Yann Collet9b5b47a2017-09-28 01:25:40 -07001404 if (FUZ_rand(&lseed) & 1) {
1405 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) );
1406 DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog);
1407 }
1408 if (FUZ_rand(&lseed) & 1) {
1409 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) );
1410 DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog);
1411 }
Stella Lau73c73bf2017-08-21 12:41:19 -07001412 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) );
1413 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) );
1414 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) );
Yann Collet01743a32017-06-16 17:56:41 -07001415
Stella Lau9e406022017-09-06 08:39:46 -07001416 /* mess with long distance matching parameters */
Stella Lau67d4a612017-09-02 21:10:36 -07001417 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) );
Stella Lau3d8e3132017-09-11 17:21:28 -07001418 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) );
Stella Lau9e406022017-09-06 08:39:46 -07001419 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) );
1420 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) );
1421 if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) );
Stella Lau6a546ef2017-07-28 15:51:33 -07001422
Yann Collet01743a32017-06-16 17:56:41 -07001423 /* mess with frame parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001424 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1425 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
1426 if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Yann Colletf5deae82017-06-18 23:41:38 -07001427 if (FUZ_rand(&lseed) & 1) CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001428 DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize);
Yann Collet01743a32017-06-16 17:56:41 -07001429
1430 /* multi-threading parameters */
Yann Colletbfc2f002017-06-21 17:57:14 -07001431 { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
Yann Colletaa800c42017-09-27 18:00:15 -07001432 U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1;
1433 U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax);
Stella Lau73c73bf2017-08-21 12:41:19 -07001434 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) );
Yann Collet9b5b47a2017-09-28 01:25:40 -07001435 DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads);
Yann Collet01743a32017-06-16 17:56:41 -07001436 if (nbThreads > 1) {
Yann Colleted1d0392017-06-19 11:07:33 -07001437 U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
Stella Lau73c73bf2017-08-21 12:41:19 -07001438 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) );
1439 CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001440 }
1441 }
1442
Stella Lau73c73bf2017-08-21 12:41:19 -07001443 if (FUZ_rand(&lseed) & 1) CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
Stella Lau023b24e2017-08-20 22:55:07 -07001444
1445 /* Apply parameters */
Stella Lau73c73bf2017-08-21 12:41:19 -07001446 if (useOpaqueAPI) {
Stella Lau82d636b2017-08-29 18:03:06 -07001447 CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) );
Stella Lau73c73bf2017-08-21 12:41:19 -07001448 }
Stella Lau023b24e2017-08-20 22:55:07 -07001449
1450 if (FUZ_rand(&lseed) & 1) {
Stella Laueb7bbab2017-08-25 10:48:07 -07001451 if (FUZ_rand(&lseed) & 1) {
1452 CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) );
1453 } else {
1454 CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) );
1455 }
Stella Lau023b24e2017-08-20 22:55:07 -07001456 if (dict && dictSize) {
1457 /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */
Stella Lau73c73bf2017-08-21 12:41:19 -07001458 if (useOpaqueAPI) {
Stella Lau82d636b2017-08-29 18:03:06 -07001459 size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams);
1460 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed");
Stella Lau73c73bf2017-08-21 12:41:19 -07001461 } else {
1462 size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1);
1463 CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed");
1464 }
1465 }
1466 } else {
Stella Lau023b24e2017-08-20 22:55:07 -07001467 CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) );
1468 }
1469 } }
Yann Collet01743a32017-06-16 17:56:41 -07001470
1471 /* multi-segments compression test */
1472 XXH64_reset(&xxhState, 0);
1473 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet052a95f2017-07-11 17:18:26 -07001474 for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) {
Yann Collet01743a32017-06-16 17:56:41 -07001475 /* compress random chunks into randomly sized dst buffers */
Yann Colleted1d0392017-06-19 11:07:33 -07001476 size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1477 size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize);
1478 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Collet052a95f2017-07-11 17:18:26 -07001479 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Colleted1d0392017-06-19 11:07:33 -07001480 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
1481 ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush;
1482 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
1483 outBuff.size = outBuff.pos + dstBuffSize;
Yann Collet01743a32017-06-16 17:56:41 -07001484
Yann Colleted1d0392017-06-19 11:07:33 -07001485 CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) );
1486 DISPLAYLEVEL(5, "compress consumed %u bytes (total : %u) \n",
1487 (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos));
Yann Collet01743a32017-06-16 17:56:41 -07001488
Yann Colleted1d0392017-06-19 11:07:33 -07001489 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
1490 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
1491 totalTestSize += inBuff.pos;
1492 }
Yann Collet01743a32017-06-16 17:56:41 -07001493
1494 /* final frame epilogue */
1495 { size_t remainingToFlush = (size_t)(-1);
1496 while (remainingToFlush) {
1497 ZSTD_inBuffer inBuff = { NULL, 0, 0 };
Yann Collet052a95f2017-07-11 17:18:26 -07001498 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1);
Yann Collet01743a32017-06-16 17:56:41 -07001499 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
1500 outBuff.size = outBuff.pos + adjustedDstSize;
1501 DISPLAYLEVEL(5, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize);
1502 remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end);
Yann Colletd7a3bff2017-06-19 11:53:01 -07001503 CHECK(ZSTD_isError(remainingToFlush),
1504 "ZSTD_compress_generic w/ ZSTD_e_end error : %s",
1505 ZSTD_getErrorName(remainingToFlush) );
Yann Collet01743a32017-06-16 17:56:41 -07001506 } }
1507 crcOrig = XXH64_digest(&xxhState);
1508 cSize = outBuff.pos;
1509 DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
1510 }
1511
1512 /* multi - fragments decompression test */
1513 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet33a66392017-06-28 11:09:43 -07001514 DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict);
Yann Colletdce78922017-06-21 15:53:42 -07001515 CHECK_Z( ZSTD_resetDStream(zd) );
Yann Collet01743a32017-06-16 17:56:41 -07001516 } else {
Yann Collet33a66392017-06-28 11:09:43 -07001517 DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize);
Yann Colletdce78922017-06-21 15:53:42 -07001518 CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
Yann Collet01743a32017-06-16 17:56:41 -07001519 }
1520 { size_t decompressionResult = 1;
1521 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1522 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1523 for (totalGenSize = 0 ; decompressionResult ; ) {
1524 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1525 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1526 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
1527 inBuff.size = inBuff.pos + readCSrcSize;
1528 outBuff.size = inBuff.pos + dstBuffSize;
Yann Collet33a66392017-06-28 11:09:43 -07001529 DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n",
1530 (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize);
Yann Collet01743a32017-06-16 17:56:41 -07001531 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1532 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
1533 DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize);
1534 }
1535 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize);
1536 CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize);
1537 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
1538 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
1539 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
1540 } }
1541
1542 /*===== noisy/erroneous src decompression test =====*/
1543
1544 /* add some noise */
1545 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
1546 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
1547 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
1548 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
1549 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
1550 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
1551 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
1552 } }
1553
1554 /* try decompression on noisy data */
Yann Colletdce78922017-06-21 15:53:42 -07001555 CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */
Yann Collet01743a32017-06-16 17:56:41 -07001556 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
1557 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
1558 while (outBuff.pos < dstBufferSize) {
1559 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
1560 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
1561 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
1562 size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize);
1563 outBuff.size = outBuff.pos + adjustedDstSize;
1564 inBuff.size = inBuff.pos + adjustedCSrcSize;
1565 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
1566 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
Yann Colleted1d0392017-06-19 11:07:33 -07001567 /* Good so far, but no more progress possible */
Yann Collet01743a32017-06-16 17:56:41 -07001568 if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break;
1569 } } } }
Yann Collet8dee0ec2017-06-18 23:25:15 -07001570 DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
Yann Collet01743a32017-06-16 17:56:41 -07001571
1572_cleanup:
1573 ZSTD_freeCCtx(zc);
1574 ZSTD_freeDStream(zd);
1575 ZSTD_freeDStream(zd_noise);
Stella Lau023b24e2017-08-20 22:55:07 -07001576 ZSTD_freeCCtxParams(cctxParams);
Yann Collet01743a32017-06-16 17:56:41 -07001577 free(cNoiseBuffer[0]);
1578 free(cNoiseBuffer[1]);
1579 free(cNoiseBuffer[2]);
1580 free(cNoiseBuffer[3]);
1581 free(cNoiseBuffer[4]);
1582 free(copyBuffer);
1583 free(cBuffer);
1584 free(dstBuffer);
1585 return result;
1586
1587_output_error:
1588 result = 1;
1589 goto _cleanup;
1590}
1591
Yann Colletd7883a22016-08-12 16:48:02 +02001592/*-*******************************************************
1593* Command line
1594*********************************************************/
1595int FUZ_usage(const char* programName)
1596{
1597 DISPLAY( "Usage :\n");
1598 DISPLAY( " %s [args]\n", programName);
1599 DISPLAY( "\n");
1600 DISPLAY( "Arguments :\n");
1601 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
1602 DISPLAY( " -s# : Select seed (default:prompt user)\n");
1603 DISPLAY( " -t# : Select starting test number (default:0)\n");
1604 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
1605 DISPLAY( " -v : verbose\n");
1606 DISPLAY( " -p : pause at the end\n");
1607 DISPLAY( " -h : display help and exit\n");
1608 return 0;
1609}
1610
Yann Collet01743a32017-06-16 17:56:41 -07001611typedef enum { simple_api, mt_api, advanced_api } e_api;
Yann Colletd7883a22016-08-12 16:48:02 +02001612
1613int main(int argc, const char** argv)
1614{
1615 U32 seed=0;
1616 int seedset=0;
1617 int argNb;
1618 int nbTests = nbTestsDefault;
1619 int testNb = 0;
1620 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
1621 int result=0;
Yann Collet19d670b2017-01-19 15:32:07 -08001622 int mainPause = 0;
Yann Colletafb0aca2017-06-29 18:19:09 -07001623 int bigTests = (sizeof(size_t) == 8);
Yann Collet01743a32017-06-16 17:56:41 -07001624 e_api selected_api = simple_api;
Yann Collet19d670b2017-01-19 15:32:07 -08001625 const char* const programName = argv[0];
Yann Colletd7a3bff2017-06-19 11:53:01 -07001626 ZSTD_customMem const customNULL = ZSTD_defaultCMem;
Stella Lau73c73bf2017-08-21 12:41:19 -07001627 U32 useOpaqueAPI = 0;
Yann Colletd7883a22016-08-12 16:48:02 +02001628
1629 /* Check command line */
1630 for(argNb=1; argNb<argc; argNb++) {
1631 const char* argument = argv[argNb];
1632 if(!argument) continue; /* Protection if argument empty */
1633
1634 /* Parsing commands. Aggregated commands are allowed */
1635 if (argument[0]=='-') {
Yann Colletd7883a22016-08-12 16:48:02 +02001636
Yann Collet01743a32017-06-16 17:56:41 -07001637 if (!strcmp(argument, "--mt")) { selected_api=mt_api; continue; }
1638 if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; continue; }
Stella Lau73c73bf2017-08-21 12:41:19 -07001639 if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; useOpaqueAPI = 1; continue; }
Sean Purcell7ebf2de2017-03-20 11:25:00 -07001640 if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
Yann Collet19d670b2017-01-19 15:32:07 -08001641
1642 argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001643 while (*argument!=0) {
1644 switch(*argument)
1645 {
1646 case 'h':
1647 return FUZ_usage(programName);
Yann Collet736788f2017-01-19 12:12:50 -08001648
Yann Colletd7883a22016-08-12 16:48:02 +02001649 case 'v':
1650 argument++;
Yann Collet736788f2017-01-19 12:12:50 -08001651 g_displayLevel++;
Yann Colletd7883a22016-08-12 16:48:02 +02001652 break;
Yann Collet736788f2017-01-19 12:12:50 -08001653
Yann Colletd7883a22016-08-12 16:48:02 +02001654 case 'q':
1655 argument++;
1656 g_displayLevel--;
1657 break;
Yann Collet736788f2017-01-19 12:12:50 -08001658
Yann Colletd7883a22016-08-12 16:48:02 +02001659 case 'p': /* pause at the end */
1660 argument++;
1661 mainPause = 1;
1662 break;
1663
Yann Collet736788f2017-01-19 12:12:50 -08001664 case 'i': /* limit tests by nb of iterations (default) */
Yann Colletd7883a22016-08-12 16:48:02 +02001665 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001666 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001667 while ((*argument>='0') && (*argument<='9')) {
1668 nbTests *= 10;
1669 nbTests += *argument - '0';
1670 argument++;
1671 }
1672 break;
1673
Yann Collet736788f2017-01-19 12:12:50 -08001674 case 'T': /* limit tests by time */
Yann Colletd7883a22016-08-12 16:48:02 +02001675 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001676 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +02001677 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -07001678 g_clockTime *= 10;
1679 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +02001680 argument++;
1681 }
Yann Colletef9999f2016-09-01 16:44:48 -07001682 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +02001683 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -07001684 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +02001685 break;
1686
Yann Collet736788f2017-01-19 12:12:50 -08001687 case 's': /* manually select seed */
Yann Colletd7883a22016-08-12 16:48:02 +02001688 argument++;
1689 seed=0;
1690 seedset=1;
1691 while ((*argument>='0') && (*argument<='9')) {
1692 seed *= 10;
1693 seed += *argument - '0';
1694 argument++;
1695 }
1696 break;
1697
Yann Collet736788f2017-01-19 12:12:50 -08001698 case 't': /* select starting test number */
Yann Colletd7883a22016-08-12 16:48:02 +02001699 argument++;
1700 testNb=0;
1701 while ((*argument>='0') && (*argument<='9')) {
1702 testNb *= 10;
1703 testNb += *argument - '0';
1704 argument++;
1705 }
1706 break;
1707
1708 case 'P': /* compressibility % */
1709 argument++;
1710 proba=0;
1711 while ((*argument>='0') && (*argument<='9')) {
1712 proba *= 10;
1713 proba += *argument - '0';
1714 argument++;
1715 }
1716 if (proba<0) proba=0;
1717 if (proba>100) proba=100;
1718 break;
1719
1720 default:
1721 return FUZ_usage(programName);
1722 }
1723 } } } /* for(argNb=1; argNb<argc; argNb++) */
1724
1725 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +02001726 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +02001727
Yann Colletef9999f2016-09-01 16:44:48 -07001728 if (!seedset) {
1729 time_t const t = time(NULL);
1730 U32 const h = XXH32(&t, sizeof(t), 1);
1731 seed = h % 10000;
1732 }
1733
Yann Colletd7883a22016-08-12 16:48:02 +02001734 DISPLAY("Seed = %u\n", seed);
1735 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
1736
1737 if (nbTests<=0) nbTests=1;
1738
1739 if (testNb==0) {
1740 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
Yann Collet4616fad2017-07-10 17:16:41 -07001741 }
Yann Colletd7883a22016-08-12 16:48:02 +02001742
Yann Collet01743a32017-06-16 17:56:41 -07001743 if (!result) {
1744 switch(selected_api)
1745 {
1746 case simple_api :
1747 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1748 break;
1749 case mt_api :
1750 result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
1751 break;
1752 case advanced_api :
Stella Lau73c73bf2017-08-21 12:41:19 -07001753 result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI);
Yann Collet01743a32017-06-16 17:56:41 -07001754 break;
1755 default :
1756 assert(0); /* impossible */
1757 }
1758 }
Yann Colletd7883a22016-08-12 16:48:02 +02001759
1760 if (mainPause) {
1761 int unused;
1762 DISPLAY("Press Enter \n");
1763 unused = getchar();
1764 (void)unused;
1765 }
1766 return result;
1767}