opj_decompress_fuzzer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2017, IntoPix SA <contact@intopix.com>
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <string.h>
  34. #include <limits.h>
  35. #include "openjpeg.h"
  36. extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv);
  37. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
  38. typedef struct {
  39. const uint8_t* pabyData;
  40. size_t nCurPos;
  41. size_t nLength;
  42. } MemFile;
  43. static void ErrorCallback(const char * msg, void *)
  44. {
  45. (void)msg;
  46. //fprintf(stderr, "%s\n", msg);
  47. }
  48. static void WarningCallback(const char *, void *)
  49. {
  50. }
  51. static void InfoCallback(const char *, void *)
  52. {
  53. }
  54. static OPJ_SIZE_T ReadCallback(void* pBuffer, OPJ_SIZE_T nBytes,
  55. void *pUserData)
  56. {
  57. MemFile* memFile = (MemFile*)pUserData;
  58. //printf("want to read %d bytes at %d\n", (int)memFile->nCurPos, (int)nBytes);
  59. if (memFile->nCurPos >= memFile->nLength) {
  60. return -1;
  61. }
  62. if (memFile->nCurPos + nBytes >= memFile->nLength) {
  63. size_t nToRead = memFile->nLength - memFile->nCurPos;
  64. memcpy(pBuffer, memFile->pabyData + memFile->nCurPos, nToRead);
  65. memFile->nCurPos = memFile->nLength;
  66. return nToRead;
  67. }
  68. if (nBytes == 0) {
  69. return -1;
  70. }
  71. memcpy(pBuffer, memFile->pabyData + memFile->nCurPos, nBytes);
  72. memFile->nCurPos += nBytes;
  73. return nBytes;
  74. }
  75. static OPJ_BOOL SeekCallback(OPJ_OFF_T nBytes, void * pUserData)
  76. {
  77. MemFile* memFile = (MemFile*)pUserData;
  78. //printf("seek to %d\n", (int)nBytes);
  79. memFile->nCurPos = nBytes;
  80. return OPJ_TRUE;
  81. }
  82. static OPJ_OFF_T SkipCallback(OPJ_OFF_T nBytes, void * pUserData)
  83. {
  84. MemFile* memFile = (MemFile*)pUserData;
  85. memFile->nCurPos += nBytes;
  86. return nBytes;
  87. }
  88. int LLVMFuzzerInitialize(int* /*argc*/, char*** argv)
  89. {
  90. return 0;
  91. }
  92. static const unsigned char jpc_header[] = {0xff, 0x4f};
  93. static const unsigned char jp2_box_jp[] = {0x6a, 0x50, 0x20, 0x20}; /* 'jP ' */
  94. int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
  95. {
  96. OPJ_CODEC_FORMAT eCodecFormat;
  97. if (len >= sizeof(jpc_header) &&
  98. memcmp(buf, jpc_header, sizeof(jpc_header)) == 0) {
  99. eCodecFormat = OPJ_CODEC_J2K;
  100. } else if (len >= 4 + sizeof(jp2_box_jp) &&
  101. memcmp(buf + 4, jp2_box_jp, sizeof(jp2_box_jp)) == 0) {
  102. eCodecFormat = OPJ_CODEC_JP2;
  103. } else {
  104. return 0;
  105. }
  106. opj_codec_t* pCodec = opj_create_decompress(eCodecFormat);
  107. opj_set_info_handler(pCodec, InfoCallback, NULL);
  108. opj_set_warning_handler(pCodec, WarningCallback, NULL);
  109. opj_set_error_handler(pCodec, ErrorCallback, NULL);
  110. opj_dparameters_t parameters;
  111. opj_set_default_decoder_parameters(&parameters);
  112. opj_setup_decoder(pCodec, &parameters);
  113. opj_stream_t *pStream = opj_stream_create(1024, OPJ_TRUE);
  114. MemFile memFile;
  115. memFile.pabyData = buf;
  116. memFile.nLength = len;
  117. memFile.nCurPos = 0;
  118. opj_stream_set_user_data_length(pStream, len);
  119. opj_stream_set_read_function(pStream, ReadCallback);
  120. opj_stream_set_seek_function(pStream, SeekCallback);
  121. opj_stream_set_skip_function(pStream, SkipCallback);
  122. opj_stream_set_user_data(pStream, &memFile, NULL);
  123. opj_image_t * psImage = NULL;
  124. if (!opj_read_header(pStream, pCodec, &psImage)) {
  125. opj_destroy_codec(pCodec);
  126. opj_stream_destroy(pStream);
  127. opj_image_destroy(psImage);
  128. return 0;
  129. }
  130. OPJ_UINT32 width = psImage->x1 - psImage->x0;
  131. OPJ_UINT32 height = psImage->y1 - psImage->y0;
  132. #if 0
  133. // Reject too big images since that will require allocating a lot of
  134. // memory
  135. if (width != 0 && psImage->numcomps != 0 &&
  136. (width > INT_MAX / psImage->numcomps ||
  137. height > INT_MAX / (width * psImage->numcomps * sizeof(OPJ_UINT32)))) {
  138. opj_stream_destroy(pStream);
  139. opj_destroy_codec(pCodec);
  140. opj_image_destroy(psImage);
  141. return 0;
  142. }
  143. // Also reject too big tiles.
  144. // TODO: remove this limitation when subtile decoding no longer imply
  145. // allocation memory for whole tile
  146. opj_codestream_info_v2_t* pCodeStreamInfo = opj_get_cstr_info(pCodec);
  147. OPJ_UINT32 nTileW, nTileH;
  148. nTileW = pCodeStreamInfo->tdx;
  149. nTileH = pCodeStreamInfo->tdy;
  150. opj_destroy_cstr_info(&pCodeStreamInfo);
  151. if (nTileW > 2048 || nTileH > 2048) {
  152. opj_stream_destroy(pStream);
  153. opj_destroy_codec(pCodec);
  154. opj_image_destroy(psImage);
  155. return 0;
  156. }
  157. #endif
  158. OPJ_UINT32 width_to_read = width;
  159. if (width_to_read > 1024) {
  160. width_to_read = 1024;
  161. }
  162. OPJ_UINT32 height_to_read = height;
  163. if (height_to_read > 1024) {
  164. height_to_read = 1024;
  165. }
  166. if (opj_set_decode_area(pCodec, psImage,
  167. psImage->x0, psImage->y0,
  168. psImage->x0 + width_to_read,
  169. psImage->y0 + height_to_read)) {
  170. if (opj_decode(pCodec, pStream, psImage)) {
  171. //printf("success\n");
  172. }
  173. }
  174. opj_end_decompress(pCodec, pStream);
  175. opj_stream_destroy(pStream);
  176. opj_destroy_codec(pCodec);
  177. opj_image_destroy(psImage);
  178. return 0;
  179. }