|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * 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). |
| 8 | + * You may select, at your option, one of the above-listed licenses. |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * This fuzz target performs a zstd round-trip test (compress & decompress), |
| 13 | + * compares the result with the original, and calls abort() on corruption. |
| 14 | + */ |
| 15 | + |
| 16 | +#define HUF_STATIC_LINKING_ONLY |
| 17 | + |
| 18 | +#include <stddef.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <string.h> |
| 22 | +#include "common/cpu.h" |
| 23 | +#include "compress/hist.h" |
| 24 | +#include "common/huf.h" |
| 25 | +#include "fuzz_helpers.h" |
| 26 | +#include "fuzz_data_producer.h" |
| 27 | + |
| 28 | +static size_t adjustTableLog(size_t tableLog, size_t maxSymbol) |
| 29 | +{ |
| 30 | + size_t const alphabetSize = maxSymbol + 1; |
| 31 | + size_t minTableLog = BIT_highbit32(alphabetSize) + 1; |
| 32 | + if ((alphabetSize & (alphabetSize - 1)) != 0) { |
| 33 | + ++minTableLog; |
| 34 | + } |
| 35 | + assert(minTableLog <= 9); |
| 36 | + if (tableLog < minTableLog) |
| 37 | + return minTableLog; |
| 38 | + else |
| 39 | + return tableLog; |
| 40 | +} |
| 41 | + |
| 42 | +int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size) |
| 43 | +{ |
| 44 | + FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size); |
| 45 | + /* Select random parameters: #streams, X1 or X2 decoding, bmi2 */ |
| 46 | + int const streams = FUZZ_dataProducer_int32Range(producer, 0, 1); |
| 47 | + int const symbols = FUZZ_dataProducer_int32Range(producer, 0, 1); |
| 48 | + int const bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid()) && FUZZ_dataProducer_int32Range(producer, 0, 1); |
| 49 | + /* Select a random cBufSize - it may be too small */ |
| 50 | + size_t const cBufSize = FUZZ_dataProducer_uint32Range(producer, 0, 4 * size); |
| 51 | + /* Select a random tableLog - we'll adjust it up later */ |
| 52 | + size_t tableLog = FUZZ_dataProducer_uint32Range(producer, 1, 12); |
| 53 | + size_t const kMaxSize = 256 * 1024; |
| 54 | + size = FUZZ_dataProducer_remainingBytes(producer); |
| 55 | + if (size > kMaxSize) |
| 56 | + size = kMaxSize; |
| 57 | + |
| 58 | + if (size <= 1) { |
| 59 | + FUZZ_dataProducer_free(producer); |
| 60 | + return 0; |
| 61 | + } |
| 62 | + |
| 63 | + uint32_t maxSymbol = 255; |
| 64 | + |
| 65 | + U32 count[256]; |
| 66 | + size_t const mostFrequent = HIST_count(count, &maxSymbol, src, size); |
| 67 | + FUZZ_ZASSERT(mostFrequent); |
| 68 | + if (mostFrequent == size) { |
| 69 | + /* RLE */ |
| 70 | + FUZZ_dataProducer_free(producer); |
| 71 | + return 0; |
| 72 | + |
| 73 | + } |
| 74 | + FUZZ_ASSERT(maxSymbol <= 255); |
| 75 | + tableLog = adjustTableLog(tableLog, maxSymbol); |
| 76 | + |
| 77 | + size_t const wkspSize = HUF_WORKSPACE_SIZE; |
| 78 | + void* wksp = FUZZ_malloc(wkspSize); |
| 79 | + void* rBuf = FUZZ_malloc(size); |
| 80 | + void* cBuf = FUZZ_malloc(cBufSize); |
| 81 | + HUF_CElt* ct = (HUF_CElt*)FUZZ_malloc(HUF_CTABLE_SIZE(maxSymbol)); |
| 82 | + HUF_DTable* dt = (HUF_DTable*)FUZZ_malloc(HUF_DTABLE_SIZE(tableLog) * sizeof(HUF_DTable)); |
| 83 | + dt[0] = tableLog * 0x01000001; |
| 84 | + |
| 85 | + tableLog = HUF_optimalTableLog(tableLog, size, maxSymbol); |
| 86 | + FUZZ_ASSERT(tableLog <= 12); |
| 87 | + tableLog = HUF_buildCTable_wksp(ct, count, maxSymbol, tableLog, wksp, wkspSize); |
| 88 | + FUZZ_ZASSERT(tableLog); |
| 89 | + size_t const tableSize = HUF_writeCTable_wksp(cBuf, cBufSize, ct, maxSymbol, tableLog, wksp, wkspSize); |
| 90 | + if (ERR_isError(tableSize)) { |
| 91 | + /* Errors on uncompressible data or cBufSize too small */ |
| 92 | + goto _out; |
| 93 | + } |
| 94 | + FUZZ_ZASSERT(tableSize); |
| 95 | + if (symbols == 0) { |
| 96 | + FUZZ_ZASSERT(HUF_readDTableX1_wksp_bmi2(dt, cBuf, tableSize, wksp, wkspSize, bmi2)); |
| 97 | + } else { |
| 98 | + size_t const ret = HUF_readDTableX2_wksp(dt, cBuf, tableSize, wksp, wkspSize); |
| 99 | + if (ERR_getErrorCode(ret) == ZSTD_error_tableLog_tooLarge) { |
| 100 | + FUZZ_ZASSERT(HUF_readDTableX1_wksp_bmi2(dt, cBuf, tableSize, wksp, wkspSize, bmi2)); |
| 101 | + } else { |
| 102 | + FUZZ_ZASSERT(ret); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + size_t cSize; |
| 107 | + size_t rSize; |
| 108 | + if (streams == 0) { |
| 109 | + cSize = HUF_compress1X_usingCTable_bmi2(cBuf, cBufSize, src, size, ct, bmi2); |
| 110 | + FUZZ_ZASSERT(cSize); |
| 111 | + if (cSize != 0) |
| 112 | + rSize = HUF_decompress1X_usingDTable_bmi2(rBuf, size, cBuf, cSize, dt, bmi2); |
| 113 | + } else { |
| 114 | + cSize = HUF_compress4X_usingCTable_bmi2(cBuf, cBufSize, src, size, ct, bmi2); |
| 115 | + FUZZ_ZASSERT(cSize); |
| 116 | + if (cSize != 0) |
| 117 | + rSize = HUF_decompress4X_usingDTable_bmi2(rBuf, size, cBuf, cSize, dt, bmi2); |
| 118 | + } |
| 119 | + if (cSize != 0) { |
| 120 | + FUZZ_ZASSERT(rSize); |
| 121 | + FUZZ_ASSERT_MSG(rSize == size, "Incorrect regenerated size"); |
| 122 | + FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, rBuf, size), "Corruption!"); |
| 123 | + } |
| 124 | +_out: |
| 125 | + free(rBuf); |
| 126 | + free(cBuf); |
| 127 | + free(ct); |
| 128 | + free(dt); |
| 129 | + free(wksp); |
| 130 | + FUZZ_dataProducer_free(producer); |
| 131 | + return 0; |
| 132 | +} |
0 commit comments