| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 attempts to decompress the fuzzed data with the simple |
| 13 | * decompression function to ensure the decompressor never crashes. |
| 14 | */ |
| 15 | |
| 16 | #include "fuzz_data_producer.h" |
| 17 | #define ZSTD_STATIC_LINKING_ONLY |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include "fuzz_helpers.h" |
| 23 | #include "zstd.h" |
| 24 | |
| 25 | static ZSTD_DCtx *dctx = NULL; |
| 26 | static void* rBuf = NULL; |
| 27 | static size_t bufSize = 0; |
| 28 | |
| 29 | int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size) |
| 30 | { |
| 31 | size_t const neededBufSize = ZSTD_BLOCKSIZE_MAX; |
| 32 | FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size); |
| 33 | |
| 34 | /* Allocate all buffers and contexts if not already allocated */ |
| 35 | if (neededBufSize > bufSize) { |
| 36 | free(rBuf); |
| 37 | rBuf = FUZZ_malloc_rand(neededBufSize, producer); |
| 38 | bufSize = neededBufSize; |
| 39 | } |
| 40 | if (!dctx) { |
| 41 | dctx = ZSTD_createDCtx(); |
| 42 | FUZZ_ASSERT(dctx); |
| 43 | } |
| 44 | ZSTD_decompressBegin(dctx); |
| 45 | ZSTD_decompressBlock(dctx, rBuf, neededBufSize, src, size); |
| 46 | |
| 47 | FUZZ_dataProducer_free(producer); |
| 48 | |
| 49 | #ifndef STATEFUL_FUZZING |
| 50 | ZSTD_freeDCtx(dctx); dctx = NULL; |
| 51 | #endif |
| 52 | return 0; |
| 53 | } |