seekable_roundtrip.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "zstd.h"
  11. #include "zstd_seekable.h"
  12. #include "fuzz_helpers.h"
  13. #include "fuzz_data_producer.h"
  14. static ZSTD_seekable *stream = NULL;
  15. static ZSTD_seekable_CStream *zscs = NULL;
  16. static const size_t kSeekableOverheadSize = ZSTD_seekTableFooterSize;
  17. int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
  18. {
  19. /* Give a random portion of src data to the producer, to use for
  20. parameter generation. The rest will be used for (de)compression */
  21. FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
  22. size = FUZZ_dataProducer_reserveDataPrefix(producer);
  23. size_t const compressedBufferSize = ZSTD_compressBound(size) + kSeekableOverheadSize;
  24. uint8_t* compressedBuffer = (uint8_t*)malloc(compressedBufferSize);
  25. uint8_t* decompressedBuffer = (uint8_t*)malloc(size);
  26. int const cLevel = FUZZ_dataProducer_int32Range(producer, ZSTD_minCLevel(), ZSTD_maxCLevel());
  27. unsigned const checksumFlag = FUZZ_dataProducer_int32Range(producer, 0, 1);
  28. size_t const uncompressedSize = FUZZ_dataProducer_uint32Range(producer, 0, size);
  29. size_t const offset = FUZZ_dataProducer_uint32Range(producer, 0, size - uncompressedSize);
  30. size_t seekSize;
  31. if (!zscs) {
  32. zscs = ZSTD_seekable_createCStream();
  33. FUZZ_ASSERT(zscs);
  34. }
  35. if (!stream) {
  36. stream = ZSTD_seekable_create();
  37. FUZZ_ASSERT(stream);
  38. }
  39. { /* Perform a compression */
  40. size_t const initStatus = ZSTD_seekable_initCStream(zscs, cLevel, checksumFlag, size);
  41. size_t endStatus;
  42. ZSTD_outBuffer out = { .dst=compressedBuffer, .pos=0, .size=compressedBufferSize };
  43. ZSTD_inBuffer in = { .src=src, .pos=0, .size=size };
  44. FUZZ_ASSERT(!ZSTD_isError(initStatus));
  45. do {
  46. size_t cSize = ZSTD_seekable_compressStream(zscs, &out, &in);
  47. FUZZ_ASSERT(!ZSTD_isError(cSize));
  48. } while (in.pos != in.size);
  49. FUZZ_ASSERT(in.pos == in.size);
  50. endStatus = ZSTD_seekable_endStream(zscs, &out);
  51. FUZZ_ASSERT(!ZSTD_isError(endStatus));
  52. seekSize = out.pos;
  53. }
  54. { /* Decompress at an offset */
  55. size_t const initStatus = ZSTD_seekable_initBuff(stream, compressedBuffer, seekSize);
  56. size_t decompressedBytesTotal = 0;
  57. size_t dSize;
  58. FUZZ_ZASSERT(initStatus);
  59. do {
  60. dSize = ZSTD_seekable_decompress(stream, decompressedBuffer, uncompressedSize, offset);
  61. FUZZ_ASSERT(!ZSTD_isError(dSize));
  62. decompressedBytesTotal += dSize;
  63. } while (decompressedBytesTotal < uncompressedSize && dSize > 0);
  64. FUZZ_ASSERT(decompressedBytesTotal == uncompressedSize);
  65. }
  66. FUZZ_ASSERT_MSG(!FUZZ_memcmp(src+offset, decompressedBuffer, uncompressedSize), "Corruption!");
  67. free(decompressedBuffer);
  68. free(compressedBuffer);
  69. FUZZ_dataProducer_free(producer);
  70. #ifndef STATEFUL_FUZZING
  71. ZSTD_seekable_free(stream); stream = NULL;
  72. ZSTD_seekable_freeCStream(zscs); zscs = NULL;
  73. #endif
  74. return 0;
  75. }