| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | Fuzzer test tool for zstd |
| 3 | Copyright (C) Yann Collet 2014-2105 |
| 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 source repository : https://github.com/Cyan4973/zstd |
| 23 | - ZSTD public forum : https://groups.google.com/forum/#!forum/lz4c |
| 24 | */ |
| 25 | |
| 26 | /************************************** |
| 27 | * Compiler specific |
| 28 | **************************************/ |
| 29 | #ifdef _MSC_VER /* Visual Studio */ |
| 30 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 31 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 32 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
| 33 | #endif |
| 34 | |
| 35 | #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 36 | #ifdef __GNUC__ |
| 37 | # pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ |
| 38 | # pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */ |
| 39 | #endif |
| 40 | |
| 41 | |
| 42 | /************************************** |
| 43 | * Includes |
| 44 | **************************************/ |
| 45 | #include <stdlib.h> /* free */ |
| 46 | #include <stdio.h> /* fgets, sscanf */ |
| 47 | #include <sys/timeb.h> /* timeb */ |
| 48 | #include <string.h> /* strcmp */ |
| 49 | #include "zstd_static.h" |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 50 | #include "datagen.h" /* RDG_genBuffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 51 | #include "xxhash.h" /* XXH64 */ |
| Yann Collet | 353c5d2 | 2015-10-21 14:39:26 +0100 | [diff] [blame] | 52 | #include "mem.h" |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | /************************************** |
| 56 | Constants |
| 57 | **************************************/ |
| 58 | #ifndef ZSTD_VERSION |
| 59 | # define ZSTD_VERSION "" |
| 60 | #endif |
| 61 | |
| 62 | #define KB *(1U<<10) |
| 63 | #define MB *(1U<<20) |
| 64 | #define GB *(1U<<30) |
| 65 | |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 66 | static const U32 nbTestsDefault = 30000; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 68 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
| 69 | static const U32 prime1 = 2654435761U; |
| 70 | static const U32 prime2 = 2246822519U; |
| 71 | |
| 72 | |
| 73 | |
| 74 | /************************************** |
| 75 | * Display Macros |
| 76 | **************************************/ |
| 77 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 78 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 79 | static U32 g_displayLevel = 2; |
| 80 | |
| 81 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 82 | if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 83 | { g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 84 | if (g_displayLevel>=4) fflush(stdout); } } |
| 85 | static const U32 g_refreshRate = 150; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 86 | static U32 g_displayTime = 0; |
| 87 | |
| 88 | static U32 g_testTime = 0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 89 | |
| 90 | |
| 91 | /********************************************************* |
| 92 | * Fuzzer functions |
| 93 | *********************************************************/ |
| Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 94 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 95 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 96 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 97 | static U32 FUZ_GetMilliStart(void) |
| 98 | { |
| 99 | struct timeb tb; |
| 100 | U32 nCount; |
| 101 | ftime( &tb ); |
| 102 | nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm); |
| 103 | return nCount; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static U32 FUZ_GetMilliSpan(U32 nTimeStart) |
| 108 | { |
| 109 | U32 nCurrent = FUZ_GetMilliStart(); |
| 110 | U32 nSpan = nCurrent - nTimeStart; |
| 111 | if (nTimeStart > nCurrent) |
| 112 | nSpan += 0x100000 * 1000; |
| 113 | return nSpan; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 118 | unsigned int FUZ_rand(unsigned int* src) |
| 119 | { |
| 120 | U32 rand32 = *src; |
| 121 | rand32 *= prime1; |
| 122 | rand32 += prime2; |
| 123 | rand32 = FUZ_rotl32(rand32, 13); |
| 124 | *src = rand32; |
| 125 | return rand32 >> 5; |
| 126 | } |
| 127 | |
| 128 | |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 129 | static unsigned FUZ_highbit32(U32 v32) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 130 | { |
| 131 | unsigned nbBits = 0; |
| 132 | if (v32==0) return 0; |
| 133 | while (v32) |
| 134 | { |
| 135 | v32 >>= 1; |
| 136 | nbBits ++; |
| 137 | } |
| 138 | return nbBits; |
| 139 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 140 | |
| 141 | |
| 142 | static int basicUnitTests(U32 seed, double compressibility) |
| 143 | { |
| 144 | int testResult = 0; |
| 145 | void* CNBuffer; |
| 146 | void* compressedBuffer; |
| 147 | void* decodedBuffer; |
| 148 | U32 randState = seed; |
| 149 | size_t result, cSize; |
| 150 | U32 testNb=0; |
| 151 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 152 | /* Create compressible test buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 153 | CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| 154 | compressedBuffer = malloc(ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)); |
| 155 | decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); |
| Yann Collet | 94f998b | 2015-07-04 23:10:40 -0800 | [diff] [blame] | 156 | if (!CNBuffer || !compressedBuffer || !decodedBuffer) |
| 157 | { |
| 158 | DISPLAY("Not enough memory, aborting\n"); |
| 159 | testResult = 1; |
| 160 | goto _end; |
| 161 | } |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 162 | RDG_genBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, 0., randState); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 163 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 164 | /* Basic tests */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 165 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 166 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 167 | if (ZSTD_isError(result)) goto _output_error; |
| 168 | cSize = result; |
| 169 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 170 | |
| 171 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 172 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 173 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 4ec2998 | 2016-03-04 19:09:28 +0100 | [diff] [blame] | 174 | if (result != COMPRESSIBLE_NOISE_LENGTH) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 175 | DISPLAYLEVEL(4, "OK \n"); |
| 176 | |
| 177 | { |
| 178 | size_t i; |
| 179 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 180 | for (i=0; i<COMPRESSIBLE_NOISE_LENGTH; i++) |
| 181 | { |
| 182 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 183 | } |
| 184 | DISPLAYLEVEL(4, "OK \n"); |
| 185 | } |
| 186 | |
| 187 | DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); |
| 188 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize-1); |
| 189 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 190 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 191 | DISPLAYLEVEL(4, "OK \n"); |
| 192 | |
| 193 | DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); |
| 194 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize+1); |
| 195 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 196 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 197 | DISPLAYLEVEL(4, "OK \n"); |
| 198 | |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 199 | /* Dictionary and Duplication tests */ |
| 200 | { |
| 201 | ZSTD_CCtx* ctxOrig = ZSTD_createCCtx(); |
| 202 | ZSTD_CCtx* ctxDuplicated = ZSTD_createCCtx(); |
| 203 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 204 | const size_t dictSize = 500; |
| 205 | size_t cSizeOrig; |
| 206 | |
| 207 | DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 208 | result = ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2); |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 209 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 210 | result = ZSTD_copyCCtx(ctxDuplicated, ctxOrig); |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 211 | if (ZSTD_isError(result)) goto _output_error; |
| 212 | DISPLAYLEVEL(4, "OK \n"); |
| 213 | |
| 214 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 215 | cSize = 0; |
| 216 | result = ZSTD_compressContinue(ctxOrig, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize); |
| 217 | if (ZSTD_isError(result)) goto _output_error; |
| 218 | cSize += result; |
| 219 | result = ZSTD_compressEnd(ctxOrig, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize); |
| 220 | if (ZSTD_isError(result)) goto _output_error; |
| 221 | cSize += result; |
| 222 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 223 | |
| 224 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 225 | result = ZSTD_decompress_usingDict(dctx, |
| 226 | decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, |
| 227 | compressedBuffer, cSize, |
| 228 | CNBuffer, dictSize); |
| 229 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 230 | if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 231 | ZSTD_freeCCtx(ctxOrig); /* if ctxOrig is read, will produce segfault */ |
| 232 | DISPLAYLEVEL(4, "OK \n"); |
| 233 | |
| 234 | DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); |
| 235 | cSizeOrig = cSize; |
| 236 | cSize = 0; |
| 237 | result = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize); |
| 238 | if (ZSTD_isError(result)) goto _output_error; |
| 239 | cSize += result; |
| 240 | result = ZSTD_compressEnd(ctxDuplicated, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize); |
| 241 | if (ZSTD_isError(result)) goto _output_error; |
| 242 | cSize += result; |
| 243 | if (cSize != cSizeOrig) goto _output_error; /* should be identical == have same size */ |
| 244 | ZSTD_freeCCtx(ctxDuplicated); |
| 245 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
| 246 | |
| 247 | DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); |
| 248 | result = ZSTD_decompress_usingDict(dctx, |
| 249 | decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, |
| 250 | compressedBuffer, cSize, |
| 251 | CNBuffer, dictSize); |
| 252 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 253 | if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; |
| Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 254 | ZSTD_freeDCtx(dctx); |
| 255 | DISPLAYLEVEL(4, "OK \n"); |
| 256 | } |
| 257 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 258 | /* Decompression defense tests */ |
| 259 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
| 260 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 3); |
| 261 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 262 | if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 263 | DISPLAYLEVEL(4, "OK \n"); |
| 264 | |
| 265 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 266 | ((char*)(CNBuffer))[0] = 1; |
| 267 | result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, CNBuffer, 4); |
| 268 | if (!ZSTD_isError(result)) goto _output_error; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 269 | DISPLAYLEVEL(4, "OK \n"); |
| 270 | |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 271 | /* block API tests */ |
| 272 | { |
| 273 | ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 274 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 275 | const size_t blockSize = 100 KB; |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 276 | const size_t dictSize = 16 KB; |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 277 | |
| 278 | /* basic block compression */ |
| 279 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
| 280 | result = ZSTD_compressBegin(cctx, 5); |
| 281 | if (ZSTD_isError(result)) goto _output_error; |
| 282 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 283 | if (ZSTD_isError(cSize)) goto _output_error; |
| 284 | DISPLAYLEVEL(4, "OK \n"); |
| 285 | |
| 286 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 287 | result = ZSTD_decompressBegin(dctx); |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 288 | if (ZSTD_isError(result)) goto _output_error; |
| 289 | result = ZSTD_decompressBlock(dctx, decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 290 | if (ZSTD_isError(result)) goto _output_error; |
| 291 | if (result != blockSize) goto _output_error; |
| 292 | DISPLAYLEVEL(4, "OK \n"); |
| 293 | |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 294 | /* dictionary block compression */ |
| 295 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 296 | result = ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5); |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 297 | if (ZSTD_isError(result)) goto _output_error; |
| 298 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 299 | if (ZSTD_isError(cSize)) goto _output_error; |
| 300 | DISPLAYLEVEL(4, "OK \n"); |
| 301 | |
| 302 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 303 | result = ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize); |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 304 | if (ZSTD_isError(result)) goto _output_error; |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 305 | result = ZSTD_decompressBlock(dctx, decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); |
| 306 | if (ZSTD_isError(result)) goto _output_error; |
| 307 | if (result != blockSize) goto _output_error; |
| 308 | DISPLAYLEVEL(4, "OK \n"); |
| 309 | |
| Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 310 | ZSTD_freeCCtx(cctx); |
| 311 | ZSTD_freeDCtx(dctx); |
| 312 | } |
| 313 | |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 314 | /* long rle test */ |
| 315 | { |
| 316 | size_t sampleSize = 0; |
| 317 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 318 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., randState); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 319 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 320 | sampleSize += 256 KB - 1; |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 321 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., randState); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 322 | sampleSize += 96 KB; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 323 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 324 | if (ZSTD_isError(cSize)) goto _output_error; |
| 325 | result = ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize); |
| 326 | if (ZSTD_isError(result)) goto _output_error; |
| 327 | if (result!=sampleSize) goto _output_error; |
| 328 | DISPLAYLEVEL(4, "OK \n"); |
| 329 | } |
| 330 | |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 331 | /* All zeroes test (#137 verif) */ |
| 332 | #define ZEROESLENGTH 100 |
| 333 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 334 | memset(CNBuffer, 0, ZEROESLENGTH); |
| 335 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1); |
| 336 | if (ZSTD_isError(result)) goto _output_error; |
| 337 | cSize = result; |
| 338 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 339 | |
| 340 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 341 | result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize); |
| 342 | if (ZSTD_isError(result)) goto _output_error; |
| 343 | if (result != ZEROESLENGTH) goto _output_error; |
| 344 | DISPLAYLEVEL(4, "OK \n"); |
| 345 | |
| 346 | /* nbSeq limit test */ |
| 347 | { |
| 348 | #define _3BYTESTESTLENGTH 131000 |
| 349 | #define NB3BYTESSEQLOG 9 |
| 350 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 351 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
| 352 | BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
| 353 | U32 r = 1; |
| 354 | int i; |
| 355 | |
| 356 | for (i=0; i < NB3BYTESSEQ; i++) { |
| 357 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&r) & 255); |
| 358 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&r) & 255); |
| 359 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&r) & 255); |
| 360 | } |
| 361 | |
| Yann Collet | dd54bbc | 2016-03-08 02:35:34 +0100 | [diff] [blame] | 362 | for (i=0; i < _3BYTESTESTLENGTH; ) { |
| Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 363 | U32 id = FUZ_rand(&r) & NB3BYTESSEQMASK; |
| 364 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 365 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 366 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 367 | i += 3; |
| 368 | } |
| 369 | |
| 370 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
| 371 | result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), CNBuffer, _3BYTESTESTLENGTH, 19); |
| 372 | if (ZSTD_isError(result)) goto _output_error; |
| 373 | cSize = result; |
| 374 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); |
| 375 | |
| 376 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
| 377 | result = ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize); |
| 378 | if (ZSTD_isError(result)) goto _output_error; |
| 379 | if (result != _3BYTESTESTLENGTH) goto _output_error; |
| 380 | DISPLAYLEVEL(4, "OK \n"); |
| 381 | } |
| 382 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 383 | _end: |
| 384 | free(CNBuffer); |
| 385 | free(compressedBuffer); |
| 386 | free(decodedBuffer); |
| 387 | return testResult; |
| 388 | |
| 389 | _output_error: |
| 390 | testResult = 1; |
| 391 | DISPLAY("Error detected in Unit tests ! \n"); |
| 392 | goto _end; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 397 | { |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 398 | const BYTE* b1 = (const BYTE*)buf1; |
| 399 | const BYTE* b2 = (const BYTE*)buf2; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 400 | size_t i; |
| 401 | for (i=0; i<max; i++) |
| 402 | { |
| 403 | if (b1[i] != b2[i]) break; |
| 404 | } |
| 405 | return i; |
| 406 | } |
| 407 | |
| 408 | # define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 409 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
| 410 | |
| 411 | static const U32 maxSrcLog = 23; |
| 412 | static const U32 maxSampleLog = 22; |
| 413 | |
| 414 | int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) |
| 415 | { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 416 | BYTE* cNoiseBuffer[5]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 417 | BYTE* srcBuffer; |
| 418 | BYTE* cBuffer; |
| 419 | BYTE* dstBuffer; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 420 | BYTE* mirrorBuffer; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 421 | size_t srcBufferSize = (size_t)1<<maxSrcLog; |
| 422 | size_t dstBufferSize = (size_t)1<<maxSampleLog; |
| 423 | size_t cBufferSize = ZSTD_compressBound(dstBufferSize); |
| 424 | U32 result = 0; |
| 425 | U32 testNb = 0; |
| 426 | U32 coreSeed = seed, lseed = 0; |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 427 | ZSTD_CCtx* refCtx; |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 428 | ZSTD_CCtx* ctx; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 429 | ZSTD_DCtx* dctx; |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 430 | U32 startTime = FUZ_GetMilliStart(); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 431 | |
| 432 | /* allocation */ |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 433 | refCtx = ZSTD_createCCtx(); |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 434 | ctx = ZSTD_createCCtx(); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 435 | dctx= ZSTD_createDCtx(); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 436 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 437 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 438 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 439 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 440 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 441 | dstBuffer = (BYTE*)malloc (dstBufferSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 442 | mirrorBuffer = (BYTE*)malloc (dstBufferSize); |
| Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 443 | cBuffer = (BYTE*)malloc (cBufferSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 444 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 445 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 446 | "Not enough memory, fuzzer tests cancelled"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 447 | |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 448 | /* Create initial samples */ |
| 449 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 450 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 451 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 452 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 453 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 454 | srcBuffer = cNoiseBuffer[2]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 455 | |
| 456 | /* catch up testNb */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 457 | for (testNb=1; testNb < startTest; testNb++) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 458 | FUZ_rand(&coreSeed); |
| 459 | |
| 460 | /* test loop */ |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 461 | for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime); testNb++ ) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 462 | { |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 463 | size_t sampleSize, sampleStart, maxTestSize, totalTestSize; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 464 | size_t cSize, dSize, dSupSize, errorCode, totalCSize, totalGenSize; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 465 | U32 sampleSizeLog, buffNb, cLevelMod, nbChunks, n; |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 466 | XXH64_CREATESTATE_STATIC(xxh64); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 467 | U64 crcOrig, crcDest; |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 468 | int cLevel; |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 469 | BYTE* sampleBuffer; |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 470 | const BYTE* dict; |
| 471 | size_t dictSize; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 472 | |
| 473 | /* init */ |
| Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 474 | if (nbTests >= testNb) |
| 475 | { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 476 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
| 477 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 478 | FUZ_rand(&coreSeed); |
| 479 | lseed = coreSeed ^ prime1; |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 480 | buffNb = FUZ_rand(&lseed) & 127; |
| 481 | if (buffNb & 7) buffNb=2; |
| 482 | else |
| 483 | { |
| 484 | buffNb >>= 3; |
| 485 | if (buffNb & 7) |
| 486 | { |
| 487 | const U32 tnb[2] = { 1, 3 }; |
| 488 | buffNb = tnb[buffNb >> 3]; |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | const U32 tnb[2] = { 0, 4 }; |
| 493 | buffNb = tnb[buffNb >> 3]; |
| 494 | } |
| 495 | } |
| 496 | srcBuffer = cNoiseBuffer[buffNb]; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 497 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| Yann Collet | f4ce891 | 2015-08-11 14:18:45 +0100 | [diff] [blame] | 498 | sampleSize = (size_t)1 << sampleSizeLog; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 499 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 500 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 501 | |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 502 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 503 | sampleBuffer = (BYTE*)malloc(sampleSize); |
| 504 | CHECK (sampleBuffer==NULL, "not enough memory for sample buffer"); |
| 505 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); |
| 506 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 507 | |
| 508 | /* compression test */ |
| Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 509 | //cLevelMod = MAX(1, 38 - (int)(MAX(9, sampleSizeLog) * 2)); /* high levels only for small samples, for manageable speed */ |
| 510 | cLevelMod = MIN( ZSTD_maxCLevel(), (U32)MAX(1, 55 - 3*(int)sampleSizeLog) ); /* high levels only for small samples, for manageable speed */ |
| Yann Collet | 4c7aae3 | 2015-11-08 14:24:59 +0100 | [diff] [blame] | 511 | cLevel = (FUZ_rand(&lseed) % cLevelMod) +1; |
| Yann Collet | dc5e3e9 | 2015-11-20 09:23:56 +0100 | [diff] [blame] | 512 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 513 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 514 | |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 515 | /* compression failure test : too small dest buffer */ |
| Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 516 | if (cSize > 3) { |
| Yann Collet | f4ce891 | 2015-08-11 14:18:45 +0100 | [diff] [blame] | 517 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 518 | const size_t tooSmallSize = cSize - missing; |
| Yann Collet | 602834f | 2015-08-20 07:46:10 +0100 | [diff] [blame] | 519 | static const U32 endMark = 0x4DC2B1A9; |
| 520 | U32 endCheck; |
| 521 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| Yann Collet | dc5e3e9 | 2015-11-20 09:23:56 +0100 | [diff] [blame] | 522 | errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 523 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); |
| Yann Collet | 602834f | 2015-08-20 07:46:10 +0100 | [diff] [blame] | 524 | memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 525 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); |
| Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 528 | /* decompression header test */ |
| 529 | { ZSTD_frameParams dParams; |
| 530 | size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize); |
| 531 | CHECK(ZSTD_isError(check), "Frame Parameters extraction failed"); |
| 532 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 533 | } |
| 534 | |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 535 | /* successfull decompression tests*/ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 536 | dSupSize = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; |
| 537 | dSize = ZSTD_decompress(dstBuffer, sampleSize + dSupSize, cBuffer, cSize); |
| Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 538 | CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 539 | crcDest = XXH64(dstBuffer, sampleSize, 0); |
| Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 540 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 541 | |
| 542 | free(sampleBuffer); /* no longer useful after this point */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 543 | |
| 544 | /* truncated src decompression test */ |
| 545 | { |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 546 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 547 | const size_t tooSmallSize = cSize - missing; |
| 548 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch overflows */ |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 549 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 550 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 551 | errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 552 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); |
| 553 | free(cBufferTooSmall); |
| 554 | } |
| 555 | |
| 556 | /* too small dst decompression test */ |
| 557 | if (sampleSize > 3) |
| 558 | { |
| Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 559 | const size_t missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 560 | const size_t tooSmallSize = sampleSize - missing; |
| 561 | static const BYTE token = 0xA9; |
| 562 | dstBuffer[tooSmallSize] = token; |
| 563 | errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 564 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); |
| 565 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 566 | } |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 567 | |
| 568 | /* noisy src decompression test */ |
| 569 | if (cSize > 6) |
| 570 | { |
| 571 | const U32 maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 572 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 573 | U32 nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 574 | size_t mask = (1<<nbBits) - 1; |
| 575 | size_t skipLength = FUZ_rand(&lseed) & mask; |
| 576 | pos += skipLength; |
| 577 | |
| 578 | while (pos < cSize) |
| 579 | { |
| 580 | /* add noise */ |
| 581 | size_t noiseStart, noiseLength; |
| 582 | nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 583 | if (nbBits>0) nbBits--; |
| 584 | mask = (1<<nbBits) - 1; |
| 585 | noiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 586 | if ( pos+noiseLength > cSize ) noiseLength = cSize-pos; |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 587 | noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
| 588 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 589 | pos += noiseLength; |
| 590 | |
| 591 | /* keep some original src */ |
| 592 | nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 593 | mask = (1<<nbBits) - 1; |
| 594 | skipLength = FUZ_rand(&lseed) & mask; |
| 595 | pos += skipLength; |
| 596 | } |
| 597 | |
| 598 | /* decompress noisy source */ |
| 599 | { |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 600 | U32 noiseSrc = FUZ_rand(&lseed) % 5; |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 601 | const U32 endMark = 0xA9B1C3D6; |
| 602 | U32 endCheck; |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 603 | srcBuffer = cNoiseBuffer[noiseSrc]; |
| Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 604 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
| 605 | errorCode = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| 606 | /* result *may* be an unlikely success, but even then, it must strictly respect dest buffer boundaries */ |
| 607 | CHECK((!ZSTD_isError(errorCode)) && (errorCode>sampleSize), |
| 608 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)errorCode, (U32)sampleSize); |
| 609 | memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 610 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); |
| 611 | } |
| 612 | } |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 613 | |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 614 | /* Streaming compression of scattered segments test */ |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 615 | XXH64_reset(xxh64, 0); |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 616 | nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 617 | sampleSizeLog = FUZ_rand(&lseed) % maxSrcLog; |
| 618 | maxTestSize = (size_t)1 << sampleSizeLog; |
| 619 | maxTestSize += FUZ_rand(&lseed) & (maxTestSize-1); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 620 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 621 | |
| 622 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| 623 | sampleSize = (size_t)1 << sampleSizeLog; |
| 624 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 625 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 626 | dict = srcBuffer + sampleStart; |
| 627 | dictSize = sampleSize; |
| 628 | |
| Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 629 | errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, (FUZ_rand(&lseed) % (20 - (sampleSizeLog/3))) + 1); |
| 630 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 631 | errorCode = ZSTD_copyCCtx(ctx, refCtx); |
| Yann Collet | 464fa99 | 2016-02-03 01:09:46 +0100 | [diff] [blame] | 632 | CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 633 | totalTestSize = 0; cSize = 0; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 634 | for (n=0; n<nbChunks; n++) |
| 635 | { |
| 636 | sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; |
| 637 | sampleSize = (size_t)1 << sampleSizeLog; |
| 638 | sampleSize += FUZ_rand(&lseed) & (sampleSize-1); |
| 639 | sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 640 | |
| 641 | if (cBufferSize-cSize < ZSTD_compressBound(sampleSize)) |
| 642 | /* avoid invalid dstBufferTooSmall */ |
| 643 | break; |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 644 | if (totalTestSize+sampleSize > maxTestSize) break; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 645 | |
| 646 | errorCode = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+sampleStart, sampleSize); |
| 647 | CHECK (ZSTD_isError(errorCode), "multi-segments compression error : %s", ZSTD_getErrorName(errorCode)); |
| 648 | cSize += errorCode; |
| 649 | |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 650 | XXH64_update(xxh64, srcBuffer+sampleStart, sampleSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 651 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+sampleStart, sampleSize); |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 652 | totalTestSize += sampleSize; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 653 | } |
| 654 | errorCode = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize); |
| 655 | CHECK (ZSTD_isError(errorCode), "multi-segments epilogue error : %s", ZSTD_getErrorName(errorCode)); |
| 656 | cSize += errorCode; |
| Yann Collet | 5835e1b | 2016-01-05 01:44:36 +0100 | [diff] [blame] | 657 | crcOrig = XXH64_digest(xxh64); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 658 | |
| 659 | /* streaming decompression test */ |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 660 | errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 661 | CHECK (ZSTD_isError(errorCode), "cannot init DCtx : %s", ZSTD_getErrorName(errorCode)); |
| 662 | totalCSize = 0; |
| 663 | totalGenSize = 0; |
| 664 | while (totalCSize < cSize) |
| 665 | { |
| 666 | size_t inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 667 | size_t genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize); |
| 668 | CHECK (ZSTD_isError(genSize), "streaming decompression error : %s", ZSTD_getErrorName(genSize)); |
| 669 | totalGenSize += genSize; |
| 670 | totalCSize += inSize; |
| 671 | } |
| 672 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
| 673 | CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size") |
| 674 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
| 675 | crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 676 | if (crcDest!=crcOrig) |
| 677 | errorCode = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
| 678 | CHECK (crcDest!=crcOrig, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
| 679 | (U32)errorCode, (U32)totalTestSize, dstBuffer[errorCode], mirrorBuffer[errorCode]); |
| 680 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 681 | } |
| Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 682 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 683 | |
| 684 | _cleanup: |
| Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 685 | ZSTD_freeCCtx(refCtx); |
| Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 686 | ZSTD_freeCCtx(ctx); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 687 | ZSTD_freeDCtx(dctx); |
| Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 688 | free(cNoiseBuffer[0]); |
| 689 | free(cNoiseBuffer[1]); |
| 690 | free(cNoiseBuffer[2]); |
| 691 | free(cNoiseBuffer[3]); |
| 692 | free(cNoiseBuffer[4]); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 693 | free(cBuffer); |
| 694 | free(dstBuffer); |
| Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 695 | free(mirrorBuffer); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 696 | return result; |
| 697 | |
| 698 | _output_error: |
| 699 | result = 1; |
| 700 | goto _cleanup; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /********************************************************* |
| 705 | * Command line |
| 706 | *********************************************************/ |
| 707 | int FUZ_usage(char* programName) |
| 708 | { |
| 709 | DISPLAY( "Usage :\n"); |
| 710 | DISPLAY( " %s [args]\n", programName); |
| 711 | DISPLAY( "\n"); |
| 712 | DISPLAY( "Arguments :\n"); |
| 713 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 714 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 715 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 716 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 717 | DISPLAY( " -v : verbose\n"); |
| Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 718 | DISPLAY( " -p : pause at the end\n"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 719 | DISPLAY( " -h : display help and exit\n"); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | int main(int argc, char** argv) |
| 725 | { |
| 726 | U32 seed=0; |
| 727 | int seedset=0; |
| 728 | int argNb; |
| 729 | int nbTests = nbTestsDefault; |
| 730 | int testNb = 0; |
| 731 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
| 732 | int result=0; |
| 733 | U32 mainPause = 0; |
| 734 | char* programName; |
| 735 | |
| 736 | /* Check command line */ |
| 737 | programName = argv[0]; |
| 738 | for(argNb=1; argNb<argc; argNb++) |
| 739 | { |
| 740 | char* argument = argv[argNb]; |
| 741 | |
| 742 | if(!argument) continue; /* Protection if argument empty */ |
| 743 | |
| 744 | /* Handle commands. Aggregated commands are allowed */ |
| 745 | if (argument[0]=='-') |
| 746 | { |
| 747 | argument++; |
| 748 | |
| 749 | while (*argument!=0) |
| 750 | { |
| 751 | switch(*argument) |
| 752 | { |
| 753 | case 'h': |
| 754 | return FUZ_usage(programName); |
| 755 | case 'v': |
| 756 | argument++; |
| 757 | g_displayLevel=4; |
| 758 | break; |
| 759 | case 'q': |
| 760 | argument++; |
| 761 | g_displayLevel--; |
| 762 | break; |
| 763 | case 'p': /* pause at the end */ |
| 764 | argument++; |
| 765 | mainPause = 1; |
| 766 | break; |
| 767 | |
| 768 | case 'i': |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 769 | argument++; g_testTime=0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 770 | nbTests=0; |
| 771 | while ((*argument>='0') && (*argument<='9')) |
| 772 | { |
| 773 | nbTests *= 10; |
| 774 | nbTests += *argument - '0'; |
| 775 | argument++; |
| 776 | } |
| 777 | break; |
| 778 | |
| Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 779 | case 'T': |
| 780 | argument++; |
| 781 | nbTests=0; g_testTime=0; |
| 782 | while ((*argument>='0') && (*argument<='9')) |
| 783 | { |
| 784 | g_testTime *= 10; |
| 785 | g_testTime += *argument - '0'; |
| 786 | argument++; |
| 787 | } |
| 788 | if (*argument=='m') g_testTime *=60, argument++; |
| 789 | if (*argument=='n') argument++; |
| 790 | g_testTime *= 1000; |
| 791 | break; |
| 792 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 793 | case 's': |
| 794 | argument++; |
| 795 | seed=0; |
| 796 | seedset=1; |
| 797 | while ((*argument>='0') && (*argument<='9')) |
| 798 | { |
| 799 | seed *= 10; |
| 800 | seed += *argument - '0'; |
| 801 | argument++; |
| 802 | } |
| 803 | break; |
| 804 | |
| 805 | case 't': |
| 806 | argument++; |
| 807 | testNb=0; |
| 808 | while ((*argument>='0') && (*argument<='9')) |
| 809 | { |
| 810 | testNb *= 10; |
| 811 | testNb += *argument - '0'; |
| 812 | argument++; |
| 813 | } |
| 814 | break; |
| 815 | |
| 816 | case 'P': /* compressibility % */ |
| 817 | argument++; |
| 818 | proba=0; |
| 819 | while ((*argument>='0') && (*argument<='9')) |
| 820 | { |
| 821 | proba *= 10; |
| 822 | proba += *argument - '0'; |
| 823 | argument++; |
| 824 | } |
| 825 | if (proba<0) proba=0; |
| 826 | if (proba>100) proba=100; |
| 827 | break; |
| 828 | |
| 829 | default: |
| 830 | return FUZ_usage(programName); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /* Get Seed */ |
| 837 | DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION); |
| 838 | |
| 839 | if (!seedset) seed = FUZ_GetMilliStart() % 10000; |
| 840 | DISPLAY("Seed = %u\n", seed); |
| 841 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 842 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 843 | if (testNb==0) result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
| 844 | if (!result) |
| 845 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); |
| 846 | if (mainPause) |
| 847 | { |
| Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 848 | int unused; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 849 | DISPLAY("Press Enter \n"); |
| Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 850 | unused = getchar(); |
| 851 | (void)unused; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 852 | } |
| 853 | return result; |
| 854 | } |