dictionary_stream_round_trip.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. const uint8_t* dict, size_t dictSize,
  50. FUZZ_dataProducer_t *producer, int refPrefix,
  51. ZSTD_dictContentType_e dictContentType)
  52. {
  53. size_t dstSize = 0;
  54. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  55. FUZZ_setRandomParameters(cctx, srcSize, producer);
  56. /* Disable checksum so we can use sizes smaller than compress bound. */
  57. FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0));
  58. if (refPrefix)
  59. FUZZ_ZASSERT(ZSTD_CCtx_refPrefix_advanced(
  60. cctx, dict, dictSize,
  61. dictContentType));
  62. else
  63. FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary_advanced(
  64. cctx, dict, dictSize,
  65. (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
  66. dictContentType));
  67. while (srcSize > 0) {
  68. ZSTD_inBuffer in = makeInBuffer(&src, &srcSize, producer);
  69. /* Mode controls the action. If mode == -1 we pick a new mode */
  70. int mode = -1;
  71. while (in.pos < in.size || mode != -1) {
  72. ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
  73. /* Previous action finished, pick a new mode. */
  74. if (mode == -1) mode = FUZZ_dataProducer_uint32Range(producer, 0, 9);
  75. switch (mode) {
  76. case 0: /* fall-through */
  77. case 1: /* fall-through */
  78. case 2: {
  79. size_t const ret =
  80. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush);
  81. FUZZ_ZASSERT(ret);
  82. if (ret == 0)
  83. mode = -1;
  84. break;
  85. }
  86. case 3: {
  87. size_t ret =
  88. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  89. FUZZ_ZASSERT(ret);
  90. /* Reset the compressor when the frame is finished */
  91. if (ret == 0) {
  92. ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
  93. if (FUZZ_dataProducer_uint32Range(producer, 0, 7) == 0) {
  94. size_t const remaining = in.size - in.pos;
  95. FUZZ_setRandomParameters(cctx, remaining, producer);
  96. }
  97. mode = -1;
  98. }
  99. break;
  100. }
  101. case 4: {
  102. ZSTD_inBuffer nullIn = { NULL, 0, 0 };
  103. ZSTD_outBuffer nullOut = { NULL, 0, 0 };
  104. size_t const ret = ZSTD_compressStream2(cctx, &nullOut, &nullIn, ZSTD_e_continue);
  105. FUZZ_ZASSERT(ret);
  106. }
  107. /* fall-through */
  108. default: {
  109. size_t const ret =
  110. ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
  111. FUZZ_ZASSERT(ret);
  112. mode = -1;
  113. }
  114. }
  115. dst += out.pos;
  116. dstSize += out.pos;
  117. capacity -= out.pos;
  118. }
  119. }
  120. for (;;) {
  121. ZSTD_inBuffer in = {NULL, 0, 0};
  122. ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
  123. size_t const ret = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
  124. FUZZ_ZASSERT(ret);
  125. dst += out.pos;
  126. dstSize += out.pos;
  127. capacity -= out.pos;
  128. if (ret == 0)
  129. break;
  130. }
  131. return dstSize;
  132. }
  133. int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
  134. {
  135. size_t neededBufSize;
  136. /* Give a random portion of src data to the producer, to use for
  137. parameter generation. The rest will be used for (de)compression */
  138. FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
  139. size = FUZZ_dataProducer_reserveDataPrefix(producer);
  140. neededBufSize = ZSTD_compressBound(size) * 15;
  141. /* Allocate all buffers and contexts if not already allocated */
  142. if (neededBufSize > bufSize) {
  143. free(cBuf);
  144. free(rBuf);
  145. cBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
  146. rBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
  147. bufSize = neededBufSize;
  148. }
  149. if (!cctx) {
  150. cctx = ZSTD_createCCtx();
  151. FUZZ_ASSERT(cctx);
  152. }
  153. if (!dctx) {
  154. dctx = ZSTD_createDCtx();
  155. FUZZ_ASSERT(dctx);
  156. }
  157. {
  158. ZSTD_dictContentType_e dictContentType = FUZZ_dataProducer_uint32Range(producer, 0, 2);
  159. FUZZ_dict_t dict = FUZZ_train(src, size, producer);
  160. int const refPrefix = FUZZ_dataProducer_uint32Range(producer, 0, 1) != 0;
  161. size_t const cSize = compress(cBuf, neededBufSize, src, size, dict.buff, dict.size, producer, refPrefix, dictContentType);
  162. if (refPrefix)
  163. FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
  164. dctx, dict.buff, dict.size,
  165. dictContentType));
  166. else
  167. FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
  168. dctx, dict.buff, dict.size,
  169. (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
  170. dictContentType));
  171. size_t const rSize =
  172. ZSTD_decompressDCtx(dctx, rBuf, neededBufSize, cBuf, cSize);
  173. FUZZ_ZASSERT(rSize);
  174. FUZZ_ASSERT_MSG(rSize == size, "Incorrect regenerated size");
  175. FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, rBuf, size), "Corruption!");
  176. free(dict.buff);
  177. }
  178. FUZZ_dataProducer_free(producer);
  179. #ifndef STATEFUL_FUZZING
  180. ZSTD_freeCCtx(cctx); cctx = NULL;
  181. ZSTD_freeDCtx(dctx); dctx = NULL;
  182. #endif
  183. return 0;
  184. }