simple_decompress.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2016-2021, 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. * This fuzz target attempts to decompress the fuzzed data with the simple
  12. * decompression function to ensure the decompressor never crashes.
  13. */
  14. #include <stddef.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "fuzz_helpers.h"
  18. #include "zstd.h"
  19. #include "fuzz_data_producer.h"
  20. static ZSTD_DCtx *dctx = NULL;
  21. int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
  22. {
  23. /* Give a random portion of src data to the producer, to use for
  24. parameter generation. The rest will be used for (de)compression */
  25. FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
  26. size = FUZZ_dataProducer_reserveDataPrefix(producer);
  27. if (!dctx) {
  28. dctx = ZSTD_createDCtx();
  29. FUZZ_ASSERT(dctx);
  30. }
  31. size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
  32. void *rBuf = FUZZ_malloc(bufSize);
  33. ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
  34. free(rBuf);
  35. FUZZ_dataProducer_free(producer);
  36. #ifndef STATEFUL_FUZZING
  37. ZSTD_freeDCtx(dctx); dctx = NULL;
  38. #endif
  39. return 0;
  40. }