Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | Fuzzer test tool for zstd streaming API |
| 3 | Copyright (C) Yann Collet 2016 |
| 4 | |
| 5 | GPL v2 License |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | You can contact the author at : |
| 22 | - ZSTD homepage : https://www.zstd.net/ |
| 23 | */ |
| 24 | |
| 25 | /*-************************************ |
| 26 | * Compiler specific |
| 27 | **************************************/ |
| 28 | #ifdef _MSC_VER /* Visual Studio */ |
| 29 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 30 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 31 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 32 | #endif |
| 33 | |
| 34 | |
| 35 | /*-************************************ |
| 36 | * Includes |
| 37 | **************************************/ |
| 38 | #include <stdlib.h> /* free */ |
| 39 | #include <stdio.h> /* fgets, sscanf */ |
| 40 | #include <sys/timeb.h> /* timeb */ |
| 41 | #include <string.h> /* strcmp */ |
| 42 | #include "mem.h" |
| 43 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */ |
| 44 | #include "zstd.h" /* ZSTD_compressBound */ |
| 45 | #include "datagen.h" /* RDG_genBuffer */ |
| 46 | #define XXH_STATIC_LINKING_ONLY |
| 47 | #include "xxhash.h" /* XXH64_* */ |
| 48 | |
| 49 | |
| 50 | /*-************************************ |
| 51 | * Constants |
| 52 | **************************************/ |
| 53 | #define KB *(1U<<10) |
| 54 | #define MB *(1U<<20) |
| 55 | #define GB *(1U<<30) |
| 56 | |
| 57 | static const U32 nbTestsDefault = 10000; |
| 58 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 59 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
| 60 | static const U32 prime1 = 2654435761U; |
| 61 | static const U32 prime2 = 2246822519U; |
| 62 | |
| 63 | |
| 64 | |
| 65 | /*-************************************ |
| 66 | * Display Macros |
| 67 | **************************************/ |
| 68 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 69 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 70 | static U32 g_displayLevel = 2; |
| 71 | |
| 72 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 73 | if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 74 | { g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ |
| 75 | if (g_displayLevel>=4) fflush(stdout); } } |
| 76 | static const U32 g_refreshRate = 150; |
| 77 | static U32 g_displayTime = 0; |
| 78 | |
| 79 | static U32 g_testTime = 0; |
| 80 | |
| 81 | |
| 82 | /*-******************************************************* |
| 83 | * Fuzzer functions |
| 84 | *********************************************************/ |
| 85 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 86 | |
| 87 | static U32 FUZ_GetMilliStart(void) |
| 88 | { |
| 89 | struct timeb tb; |
| 90 | U32 nCount; |
| 91 | ftime( &tb ); |
| 92 | nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); |
| 93 | return nCount; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static U32 FUZ_GetMilliSpan(U32 nTimeStart) |
| 98 | { |
| 99 | U32 const nCurrent = FUZ_GetMilliStart(); |
| 100 | U32 nSpan = nCurrent - nTimeStart; |
| 101 | if (nTimeStart > nCurrent) |
| 102 | nSpan += 0x100000 * 1000; |
| 103 | return nSpan; |
| 104 | } |
| 105 | |
| 106 | /*! FUZ_rand() : |
| 107 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 108 | `seed` is also modified */ |
| 109 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 110 | unsigned int FUZ_rand(unsigned int* seedPtr) |
| 111 | { |
| 112 | U32 rand32 = *seedPtr; |
| 113 | rand32 *= prime1; |
| 114 | rand32 += prime2; |
| 115 | rand32 = FUZ_rotl32(rand32, 13); |
| 116 | *seedPtr = rand32; |
| 117 | return rand32 >> 5; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | static unsigned FUZ_highbit32(U32 v32) |
| 123 | { |
| 124 | unsigned nbBits = 0; |
| 125 | if (v32==0) return 0; |
| 126 | for ( ; v32 ; v32>>=1) nbBits++; |
| 127 | return nbBits; |
| 128 | } |
| 129 | */ |
| 130 | |
| 131 | static void* allocFunction(void* opaque, size_t size) |
| 132 | { |
| 133 | void* address = malloc(size); |
| 134 | (void)opaque; |
| 135 | return address; |
| 136 | } |
| 137 | |
| 138 | static void freeFunction(void* opaque, void* address) |
| 139 | { |
| 140 | (void)opaque; |
| 141 | free(address); |
| 142 | } |
| 143 | |
| 144 | static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem) |
| 145 | { |
| 146 | int testResult = 0; |
| 147 | size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
| 148 | void* CNBuffer = malloc(CNBufferSize); |
| 149 | size_t const skippableFrameSize = 11; |
| 150 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 151 | void* compressedBuffer = malloc(compressedBufferSize); |
| 152 | size_t const decodedBufferSize = CNBufferSize; |
| 153 | void* decodedBuffer = malloc(decodedBufferSize); |
| 154 | size_t cSize; |
| 155 | U32 testNb=0; |
| 156 | ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem); |
| 157 | ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 158 | ZSTD_inBuffer inBuff; |
| 159 | ZSTD_outBuffer outBuff; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 160 | |
| 161 | /* Create compressible test buffer */ |
| 162 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
| 163 | DISPLAY("Not enough memory, aborting\n"); |
| 164 | goto _output_error; |
| 165 | } |
| 166 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 167 | |
| 168 | /* generate skippable frame */ |
| 169 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 170 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 171 | cSize = skippableFrameSize + 8; |
| 172 | |
| 173 | /* Basic compression test */ |
| 174 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 175 | ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 176 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 177 | outBuff.size = compressedBufferSize; |
| 178 | outBuff.pos = 0; |
| 179 | inBuff.src = CNBuffer; |
| 180 | inBuff.size = CNBufferSize; |
| 181 | inBuff.pos = 0; |
| 182 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 183 | if (ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 184 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 185 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 186 | if (r != 0) goto _output_error; } /*< error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 187 | cSize += outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 188 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 189 | |
| 190 | /* skippable frame test */ |
| 191 | DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++); |
| 192 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 193 | inBuff.src = compressedBuffer; |
| 194 | inBuff.size = cSize; |
| 195 | inBuff.pos = 0; |
| 196 | outBuff.dst = decodedBuffer; |
| 197 | outBuff.size = CNBufferSize; |
| 198 | outBuff.pos = 0; |
| 199 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 200 | if (r != 0) goto _output_error; } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 201 | if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 202 | DISPLAYLEVEL(4, "OK \n"); |
| 203 | |
| 204 | /* Basic decompression test */ |
| 205 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 206 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 207 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 208 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 209 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 210 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 211 | DISPLAYLEVEL(4, "OK \n"); |
| 212 | |
| 213 | /* check regenerated data is byte exact */ |
| 214 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 215 | { size_t i; |
| 216 | for (i=0; i<CNBufferSize; i++) { |
| 217 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 218 | } } |
| 219 | DISPLAYLEVEL(4, "OK \n"); |
| 220 | |
| 221 | /* Byte-by-byte decompression test */ |
| 222 | DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++); |
| 223 | { size_t r = 1; |
| 224 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 225 | inBuff.src = compressedBuffer; |
| 226 | outBuff.dst = decodedBuffer; |
| 227 | inBuff.pos = 0; |
| 228 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 229 | while (r) { /* skippable frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 230 | inBuff.size = inBuff.pos + 1; |
| 231 | outBuff.size = outBuff.pos + 1; |
| 232 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 233 | if (ZSTD_isError(r)) goto _output_error; |
| 234 | } |
| 235 | ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB); |
| 236 | r=1; |
| 237 | while (r) { /* normal frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 238 | inBuff.size = inBuff.pos + 1; |
| 239 | outBuff.size = outBuff.pos + 1; |
| 240 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 241 | if (ZSTD_isError(r)) goto _output_error; |
| 242 | } |
| 243 | } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 244 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 245 | if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 246 | DISPLAYLEVEL(4, "OK \n"); |
| 247 | |
| 248 | /* check regenerated data is byte exact */ |
| 249 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 250 | { size_t i; |
| 251 | for (i=0; i<CNBufferSize; i++) { |
| 252 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 253 | } } |
| 254 | DISPLAYLEVEL(4, "OK \n"); |
| 255 | |
| 256 | _end: |
| 257 | ZSTD_freeCStream(zc); |
| 258 | ZSTD_freeDStream(zd); |
| 259 | free(CNBuffer); |
| 260 | free(compressedBuffer); |
| 261 | free(decodedBuffer); |
| 262 | return testResult; |
| 263 | |
| 264 | _output_error: |
| 265 | testResult = 1; |
| 266 | DISPLAY("Error detected in Unit tests ! \n"); |
| 267 | goto _end; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 272 | { |
| 273 | const BYTE* b1 = (const BYTE*)buf1; |
| 274 | const BYTE* b2 = (const BYTE*)buf2; |
| 275 | size_t u; |
| 276 | for (u=0; u<max; u++) { |
| 277 | if (b1[u] != b2[u]) break; |
| 278 | } |
| 279 | return u; |
| 280 | } |
| 281 | |
| 282 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 283 | { |
| 284 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 285 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 286 | } |
| 287 | |
| 288 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 289 | { |
| 290 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 291 | return FUZ_rLogLength(seed, logLength); |
| 292 | } |
| 293 | |
| 294 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
| 295 | |
| 296 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 297 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| 298 | |
| 299 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) |
| 300 | { |
| 301 | static const U32 maxSrcLog = 24; |
| 302 | static const U32 maxSampleLog = 19; |
| 303 | BYTE* cNoiseBuffer[5]; |
| 304 | size_t srcBufferSize = (size_t)1<<maxSrcLog; |
| 305 | BYTE* copyBuffer; |
| 306 | size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 307 | BYTE* cBuffer; |
| 308 | size_t cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 309 | BYTE* dstBuffer; |
| 310 | size_t dstBufferSize = srcBufferSize; |
| 311 | U32 result = 0; |
| 312 | U32 testNb = 0; |
| 313 | U32 coreSeed = seed; |
| 314 | ZSTD_CStream* zc; |
| 315 | ZSTD_DStream* zd; |
| 316 | U32 startTime = FUZ_GetMilliStart(); |
| 317 | |
| 318 | /* allocations */ |
| 319 | zc = ZSTD_createCStream(); |
| 320 | zd = ZSTD_createDStream(); |
| 321 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 322 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 323 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 324 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 325 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 326 | copyBuffer= (BYTE*)malloc (copyBufferSize); |
| 327 | dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 328 | cBuffer = (BYTE*)malloc (cBufferSize); |
| 329 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 330 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd, |
| 331 | "Not enough memory, fuzzer tests cancelled"); |
| 332 | |
| 333 | /* Create initial samples */ |
| 334 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 335 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 336 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 337 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 338 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 339 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
| 340 | |
| 341 | /* catch up testNb */ |
| 342 | for (testNb=1; testNb < startTest; testNb++) |
| 343 | FUZ_rand(&coreSeed); |
| 344 | |
| 345 | /* test loop */ |
| 346 | for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime) ; testNb++ ) { |
| 347 | U32 lseed; |
| 348 | const BYTE* srcBuffer; |
| 349 | const BYTE* dict; |
| 350 | size_t maxTestSize, dictSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 351 | size_t cSize, totalTestSize, totalGenSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 352 | U32 n, nbChunks; |
| 353 | XXH64_state_t xxhState; |
| 354 | U64 crcOrig; |
| 355 | |
| 356 | /* init */ |
| 357 | DISPLAYUPDATE(2, "\r%6u", testNb); |
| 358 | if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests); |
| 359 | FUZ_rand(&coreSeed); |
| 360 | lseed = coreSeed ^ prime1; |
| 361 | |
| 362 | /* states full reset (unsynchronized) */ |
| 363 | /* some issues only happen when reusing states in a specific sequence of parameters */ |
| 364 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); } |
| 365 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); } |
| 366 | |
| 367 | /* srcBuffer selection [0-4] */ |
| 368 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 369 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 370 | else { |
| 371 | buffNb >>= 3; |
| 372 | if (buffNb & 7) { |
| 373 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 374 | buffNb = tnb[buffNb >> 3]; |
| 375 | } else { |
| 376 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 377 | buffNb = tnb[buffNb >> 3]; |
| 378 | } } |
| 379 | srcBuffer = cNoiseBuffer[buffNb]; |
| 380 | } |
| 381 | |
| 382 | /* compression init */ |
| 383 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 384 | U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
| 385 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 386 | dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0; |
| 387 | /* random dictionary selection */ |
| 388 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 389 | dict = srcBuffer + dictStart; |
| 390 | } |
| 391 | { ZSTD_parameters params = ZSTD_getParams(cLevel, 0, dictSize); |
| 392 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 393 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
| 394 | { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, 0); |
| 395 | CHECK (ZSTD_isError(initError),"init error : %s", ZSTD_getErrorName(initError)); |
| 396 | } } } |
| 397 | |
| 398 | /* multi-segments compression test */ |
| 399 | XXH64_reset(&xxhState, 0); |
| 400 | nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 401 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 402 | for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) { |
| 403 | /* compress random chunk into random size dst buffer */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 404 | { size_t const readChunkSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 405 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize); |
| 406 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 407 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 408 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, readChunkSize, 0 }; |
| 409 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 410 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 411 | { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 412 | CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 413 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 414 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 415 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 416 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* random flush operation, to mess around */ |
| 420 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 421 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 422 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 423 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 424 | { size_t const flushError = ZSTD_flushStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 425 | CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError)); |
| 426 | } } } |
| 427 | |
| 428 | /* final frame epilogue */ |
| 429 | { size_t remainingToFlush = (size_t)(-1); |
| 430 | while (remainingToFlush) { |
| 431 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 432 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 433 | U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 434 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 435 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 436 | CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush)); |
| 437 | CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush); |
| 438 | } } |
| 439 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 440 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* multi - fragments decompression test */ |
| 444 | ZSTD_initDStream_usingDict(zd, dict, dictSize); |
| 445 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 446 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 447 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 448 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 449 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 450 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 451 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 452 | inBuff.size = inBuff.pos + readCSrcSize; |
| 453 | outBuff.size = inBuff.pos + dstBuffSize; |
| 454 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 455 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 456 | } |
| 457 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 458 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size") |
| 459 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 460 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 461 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 462 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 463 | } } |
| 464 | |
| 465 | /*===== noisy/erroneous src decompression test =====*/ |
| 466 | |
| 467 | /* add some noise */ |
| 468 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 469 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 470 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 471 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 472 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 473 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 474 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 475 | } } |
| 476 | |
| 477 | /* try decompression on noisy data */ |
| 478 | ZSTD_initDStream(zd); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 479 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 480 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 481 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 482 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 483 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame^] | 484 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 485 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 486 | inBuff.size = inBuff.pos + randomCSrcSize; |
| 487 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 488 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
| 489 | } } } } |
| 490 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 491 | |
| 492 | _cleanup: |
| 493 | ZSTD_freeCStream(zc); |
| 494 | ZSTD_freeDStream(zd); |
| 495 | free(cNoiseBuffer[0]); |
| 496 | free(cNoiseBuffer[1]); |
| 497 | free(cNoiseBuffer[2]); |
| 498 | free(cNoiseBuffer[3]); |
| 499 | free(cNoiseBuffer[4]); |
| 500 | free(copyBuffer); |
| 501 | free(cBuffer); |
| 502 | free(dstBuffer); |
| 503 | return result; |
| 504 | |
| 505 | _output_error: |
| 506 | result = 1; |
| 507 | goto _cleanup; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /*-******************************************************* |
| 512 | * Command line |
| 513 | *********************************************************/ |
| 514 | int FUZ_usage(const char* programName) |
| 515 | { |
| 516 | DISPLAY( "Usage :\n"); |
| 517 | DISPLAY( " %s [args]\n", programName); |
| 518 | DISPLAY( "\n"); |
| 519 | DISPLAY( "Arguments :\n"); |
| 520 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 521 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 522 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 523 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 524 | DISPLAY( " -v : verbose\n"); |
| 525 | DISPLAY( " -p : pause at the end\n"); |
| 526 | DISPLAY( " -h : display help and exit\n"); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | int main(int argc, const char** argv) |
| 532 | { |
| 533 | U32 seed=0; |
| 534 | int seedset=0; |
| 535 | int argNb; |
| 536 | int nbTests = nbTestsDefault; |
| 537 | int testNb = 0; |
| 538 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 539 | int result=0; |
| 540 | U32 mainPause = 0; |
| 541 | const char* programName = argv[0]; |
| 542 | ZSTD_customMem customMem = { allocFunction, freeFunction, NULL }; |
| 543 | ZSTD_customMem customNULL = { NULL, NULL, NULL }; |
| 544 | |
| 545 | /* Check command line */ |
| 546 | for(argNb=1; argNb<argc; argNb++) { |
| 547 | const char* argument = argv[argNb]; |
| 548 | if(!argument) continue; /* Protection if argument empty */ |
| 549 | |
| 550 | /* Parsing commands. Aggregated commands are allowed */ |
| 551 | if (argument[0]=='-') { |
| 552 | argument++; |
| 553 | |
| 554 | while (*argument!=0) { |
| 555 | switch(*argument) |
| 556 | { |
| 557 | case 'h': |
| 558 | return FUZ_usage(programName); |
| 559 | case 'v': |
| 560 | argument++; |
| 561 | g_displayLevel=4; |
| 562 | break; |
| 563 | case 'q': |
| 564 | argument++; |
| 565 | g_displayLevel--; |
| 566 | break; |
| 567 | case 'p': /* pause at the end */ |
| 568 | argument++; |
| 569 | mainPause = 1; |
| 570 | break; |
| 571 | |
| 572 | case 'i': |
| 573 | argument++; |
| 574 | nbTests=0; g_testTime=0; |
| 575 | while ((*argument>='0') && (*argument<='9')) { |
| 576 | nbTests *= 10; |
| 577 | nbTests += *argument - '0'; |
| 578 | argument++; |
| 579 | } |
| 580 | break; |
| 581 | |
| 582 | case 'T': |
| 583 | argument++; |
| 584 | nbTests=0; g_testTime=0; |
| 585 | while ((*argument>='0') && (*argument<='9')) { |
| 586 | g_testTime *= 10; |
| 587 | g_testTime += *argument - '0'; |
| 588 | argument++; |
| 589 | } |
| 590 | if (*argument=='m') g_testTime *=60, argument++; |
| 591 | if (*argument=='n') argument++; |
| 592 | g_testTime *= 1000; |
| 593 | break; |
| 594 | |
| 595 | case 's': |
| 596 | argument++; |
| 597 | seed=0; |
| 598 | seedset=1; |
| 599 | while ((*argument>='0') && (*argument<='9')) { |
| 600 | seed *= 10; |
| 601 | seed += *argument - '0'; |
| 602 | argument++; |
| 603 | } |
| 604 | break; |
| 605 | |
| 606 | case 't': |
| 607 | argument++; |
| 608 | testNb=0; |
| 609 | while ((*argument>='0') && (*argument<='9')) { |
| 610 | testNb *= 10; |
| 611 | testNb += *argument - '0'; |
| 612 | argument++; |
| 613 | } |
| 614 | break; |
| 615 | |
| 616 | case 'P': /* compressibility % */ |
| 617 | argument++; |
| 618 | proba=0; |
| 619 | while ((*argument>='0') && (*argument<='9')) { |
| 620 | proba *= 10; |
| 621 | proba += *argument - '0'; |
| 622 | argument++; |
| 623 | } |
| 624 | if (proba<0) proba=0; |
| 625 | if (proba>100) proba=100; |
| 626 | break; |
| 627 | |
| 628 | default: |
| 629 | return FUZ_usage(programName); |
| 630 | } |
| 631 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 632 | |
| 633 | /* Get Seed */ |
| 634 | DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
| 635 | |
| 636 | if (!seedset) seed = FUZ_GetMilliStart() % 10000; |
| 637 | DISPLAY("Seed = %u\n", seed); |
| 638 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 639 | |
| 640 | if (nbTests<=0) nbTests=1; |
| 641 | |
| 642 | if (testNb==0) { |
| 643 | result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */ |
| 644 | if (!result) { |
| 645 | DISPLAYLEVEL(4, "Unit tests using customMem :\n") |
| 646 | result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */ |
| 647 | } } |
| 648 | |
| 649 | if (!result) |
| 650 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
| 651 | |
| 652 | if (mainPause) { |
| 653 | int unused; |
| 654 | DISPLAY("Press Enter \n"); |
| 655 | unused = getchar(); |
| 656 | (void)unused; |
| 657 | } |
| 658 | return result; |
| 659 | } |