diff options
Diffstat (limited to 'programs/fuzzer.c')
-rw-r--r-- | programs/fuzzer.c | 109 |
1 files changed, 40 insertions, 69 deletions
diff --git a/programs/fuzzer.c b/programs/fuzzer.c index 6d3b077..3d3cf8e 100644 --- a/programs/fuzzer.c +++ b/programs/fuzzer.c @@ -25,7 +25,7 @@ */ /************************************** -* Remove Visual warning messages +* Compiler options **************************************/ #ifdef _MSC_VER /* Visual Studio */ # define _CRT_SECURE_NO_WARNINGS /* fgets */ @@ -34,21 +34,32 @@ # pragma warning(disable : 4310) /* disable: C4310: constant char value > 127 */ #endif +/* S_ISREG & gettimeofday() are not supported by MSVC */ +#if defined(_MSC_VER) || defined(_WIN32) +# define FUZ_LEGACY_TIMER 1 +#endif + /************************************** -* Includes +* Includes **************************************/ #include <stdlib.h> #include <stdio.h> /* fgets, sscanf */ -#include <sys/timeb.h> /* timeb */ #include <string.h> /* strcmp */ #include "lz4.h" #include "lz4hc.h" #include "xxhash.h" +/* Use ftime() if gettimeofday() is not available on your target */ +#if defined(FUZ_LEGACY_TIMER) +# include <sys/timeb.h> /* timeb, ftime */ +#else +# include <sys/time.h> /* gettimeofday */ +#endif + /************************************** -* Basic Types +* Basic Types **************************************/ #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ # include <stdint.h> @@ -67,7 +78,7 @@ typedef unsigned long long U64; /************************************** -* Constants +* Constants **************************************/ #ifndef LZ4_VERSION # define LZ4_VERSION "" @@ -88,7 +99,7 @@ typedef unsigned long long U64; /***************************************** -* Macros +* Macros *****************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } @@ -98,8 +109,10 @@ static U32 g_time = 0; /********************************************************* - Fuzzer functions +* Fuzzer functions *********************************************************/ +#if defined(FUZ_LEGACY_TIMER) + static U32 FUZ_GetMilliStart(void) { struct timeb tb; @@ -109,6 +122,20 @@ static U32 FUZ_GetMilliStart(void) return nCount; } +#else + +static U32 FUZ_GetMilliStart(void) +{ + struct timeval tv; + U32 nCount; + gettimeofday(&tv, NULL); + nCount = (U32) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); + return nCount; +} + +#endif + + static U32 FUZ_GetMilliSpan(U32 nTimeStart) { U32 nCurrent = FUZ_GetMilliStart(); @@ -177,7 +204,7 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, dou #define BLOCKSIZE_I134 (32 MB) static int FUZ_AddressOverflow(void) { - char* buffers[MAX_NB_BUFF_I134+1] = {0}; + char* buffers[MAX_NB_BUFF_I134+1]; int i, nbBuff=0; int highAddress = 0; @@ -296,6 +323,7 @@ static int FUZ_test(U32 seed, const U32 nbCycles, const U32 startCycle, const do U32 crcOrig, crcCheck; U32 coreRandState = seed; U32 randState = coreRandState ^ PRIME3; + int result = 0; // init @@ -661,7 +689,6 @@ static int FUZ_test(U32 seed, const U32 nbCycles, const U32 startCycle, const do // unalloc { - int result = 0; _exit: free(CNBuffer); free(compressedBuffer); @@ -753,7 +780,7 @@ static void FUZ_unitTests(void) FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe() test failed"); XXH64_update(&xxhNew, testVerify + dNext, messageSize); - crcNew = crcOrig = XXH64_digest(&xxhNew); + crcNew = XXH64_digest(&xxhNew); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); // prepare next message @@ -916,7 +943,7 @@ static void FUZ_unitTests(void) FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe() test failed"); XXH64_update(&xxhNew, testVerify + dNext, messageSize); - crcNew = crcOrig = XXH64_digest(&xxhNew); + crcNew = XXH64_digest(&xxhNew); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); // prepare next message @@ -959,10 +986,10 @@ static void FUZ_unitTests(void) FUZ_CHECKTEST(result!=(int)messageSize, "ringBuffer : LZ4_decompress_safe() test failed"); XXH64_update(&xxhNew, testVerify + dNext, messageSize); - crcNew = crcOrig = XXH64_digest(&xxhNew); + crcNew = XXH64_digest(&xxhNew); FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe() decompression corruption"); - // prepare next message + /* prepare next message */ dNext += messageSize; totalMessageSize += messageSize; messageSize = (FUZ_rand(&randState) & maxMessageSizeMask) + 1; @@ -970,62 +997,6 @@ static void FUZ_unitTests(void) if (dNext + messageSize > dBufferSize) dNext = 0; } } - - // long stream test ; Warning : very long test ! - if (1) - { - XXH64_state_t crcOrigState; - XXH64_state_t crcNewState; - const U64 totalTestSize = 6ULL << 30; - U64 totalTestDone = 0; - size_t oldStart = 0; - size_t oldSize = 0; - U32 segNb = 1; - - DISPLAY("Long HC streaming test (%u MB)\n", (U32)(totalTestSize >> 20)); - LZ4_resetStreamHC(&sHC, 0); - - XXH64_reset(&crcOrigState, 0); - XXH64_reset(&crcNewState, 0); - - while (totalTestDone < totalTestSize) - { - size_t testSize = (FUZ_rand(&randState) & 65535) + 1; - size_t testStart = FUZ_rand(&randState) & 65535; - - FUZ_displayUpdate((U32)(totalTestDone >> 20)); - - if (testStart == oldStart + oldSize) // Corner case not covered by this test (LZ4_decompress_safe_usingDict() limitation) - testStart++; - - XXH64_update(&crcOrigState, testInput + testStart, testSize); - crcOrig = XXH64_digest(&crcOrigState); - - result = LZ4_compressHC_limitedOutput_continue(&sHC, testInput + testStart, testCompressed, (int)testSize, LZ4_compressBound((int)testSize)); - FUZ_CHECKTEST(result==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result); - - result = LZ4_decompress_safe_usingDict(testCompressed, testVerify, result, (int)testSize, testInput + oldStart, (int)oldSize); - FUZ_CHECKTEST(result!=(int)testSize, "LZ4_decompress_safe_usingDict() dictionary decompression part %u failed", segNb); - - XXH64_update(&crcNewState, testVerify, testSize); - crcNew = XXH64_digest(&crcNewState); - if (crcOrig!=crcNew) - { - size_t c=0; - while (testVerify[c] == testInput[testStart+c]) c++; - DISPLAY("Bad decompression at %u / %u \n", (U32)c, (U32)testSize); - } - FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %u corruption", segNb); - - oldStart = testStart; - oldSize = testSize; - totalTestDone += testSize; - - segNb ++; - } - - DISPLAY("\r"); - } } printf("All unit tests completed successfully \n"); |