stream_round_trip.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 performs a zstd round-trip test (compress & decompress),
  12. * compares the result with the original, and calls abort() on corruption.
  13. */
  14. #define ZSTD_STATIC_LINKING_ONLY
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "fuzz_helpers.h"
  20. #include "zstd_helpers.h"
  21. #include "fuzz_data_producer.h"
  22. ZSTD_CCtx *cctx = NULL;
  23. static ZSTD_DCtx *dctx = NULL;
  24. static uint8_t* cBuf = NULL;
  25. static uint8_t* rBuf = NULL;
  26. static size_t bufSize = 0;
  27. static ZSTD_outBuffer makeOutBuffer(uint8_t *dst, size_t capacity,
  28. FUZZ_dataProducer_t *producer)
  29. {
  30. ZSTD_outBuffer buffer = { dst, 0, 0 };
  31. FUZZ_ASSERT(capacity > 0);
  32. buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, capacity));
  33. FUZZ_ASSERT(buffer.size <= capacity);
  34. return buffer;
  35. }
  36. static ZSTD_inBuffer makeInBuffer(const uint8_t **src, size_t *size,
  37. FUZZ_dataProducer_t *producer)
  38. {
  39. ZSTD_inBuffer buffer = { *src, 0, 0 };
  40. FUZZ_ASSERT(*size > 0);
  41. buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, *size));
  42. FUZZ_ASSERT(buffer.size <= *size);
  43. *src += buffer.size;
  44. *size -= buffer.size;
  45. return buffer;
  46. }
  47. static size_t compress(uint8_t *dst, size_t capacity,
  48. const uint8_t *src, size_t srcSize,
  49. FUZZ_dataProducer_t *producer)
  50. {
  51. size_t dstSize = 0;
  52. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  53. FUZZ_setRandomParameters(cctx, srcSize, producer);
  54. while (srcSize > 0) {
  55. ZSTD_inBuffer in = makeInBuffer(&src, &srcSize, producer);
  56. /* Mode controls the action. If mode == -1 we pick a new mode */
  57. int mode = -1;
  58. while (in.pos < in.size || mode != -1) {
  59. ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
  60. /* Previous action finished, pick a new mode. */
  61. if (mode == -1) mode = FUZZ_dataProducer_uint32Range(producer, 0, 9);
  62. switch (mode) {
  63. case 0: /* fall-through */
  64. case 1: /* fall-through */
  65. case 2: {
  66. size_t const ret =
  67. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush);
  68. FUZZ_ZASSERT(ret);
  69. if (ret == 0)
  70. mode = -1;
  71. break;
  72. }
  73. case 3: {
  74. size_t ret =
  75. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  76. FUZZ_ZASSERT(ret);
  77. /* Reset the compressor when the frame is finished */
  78. if (ret == 0) {
  79. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  80. if (FUZZ_dataProducer_uint32Range(producer, 0, 7) == 0) {
  81. size_t const remaining = in.size - in.pos;
  82. FUZZ_setRandomParameters(cctx, remaining, producer);
  83. }
  84. mode = -1;
  85. }
  86. break;
  87. }
  88. case 4: {
  89. ZSTD_inBuffer nullIn = { NULL, 0, 0 };
  90. ZSTD_outBuffer nullOut = { NULL, 0, 0 };
  91. size_t const ret = ZSTD_compressStream2(cctx, &nullOut, &nullIn, ZSTD_e_continue);
  92. FUZZ_ZASSERT(ret);
  93. }
  94. /* fall-through */
  95. default: {
  96. size_t const ret =
  97. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
  98. FUZZ_ZASSERT(ret);
  99. mode = -1;
  100. }
  101. }
  102. dst += out.pos;
  103. dstSize += out.pos;
  104. capacity -= out.pos;
  105. }
  106. }
  107. for (;;) {
  108. ZSTD_inBuffer in = {NULL, 0, 0};
  109. ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
  110. size_t const ret = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  111. FUZZ_ZASSERT(ret);
  112. dst += out.pos;
  113. dstSize += out.pos;
  114. capacity -= out.pos;
  115. if (ret == 0)
  116. break;
  117. }
  118. return dstSize;
  119. }
  120. int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
  121. {
  122. size_t neededBufSize;
  123. /* Give a random portion of src data to the producer, to use for
  124. parameter generation. The rest will be used for (de)compression */
  125. FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
  126. size = FUZZ_dataProducer_reserveDataPrefix(producer);
  127. neededBufSize = ZSTD_compressBound(size) * 15;
  128. /* Allocate all buffers and contexts if not already allocated */
  129. if (neededBufSize > bufSize) {
  130. free(cBuf);
  131. free(rBuf);
  132. cBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
  133. rBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
  134. bufSize = neededBufSize;
  135. }
  136. if (!cctx) {
  137. cctx = ZSTD_createCCtx();
  138. FUZZ_ASSERT(cctx);
  139. }
  140. if (!dctx) {
  141. dctx = ZSTD_createDCtx();
  142. FUZZ_ASSERT(dctx);
  143. }
  144. {
  145. size_t const cSize = compress(cBuf, neededBufSize, src, size, producer);
  146. size_t const rSize =
  147. ZSTD_decompressDCtx(dctx, rBuf, neededBufSize, cBuf, cSize);
  148. FUZZ_ZASSERT(rSize);
  149. FUZZ_ASSERT_MSG(rSize == size, "Incorrect regenerated size");
  150. FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, rBuf, size), "Corruption!");
  151. }
  152. FUZZ_dataProducer_free(producer);
  153. #ifndef STATEFUL_FUZZING
  154. ZSTD_freeCCtx(cctx); cctx = NULL;
  155. ZSTD_freeDCtx(dctx); dctx = NULL;
  156. #endif
  157. return 0;
  158. }