Separate fuzz targets to increase coverage (#1416)
[openjpeg.git] / tests / fuzzers / opj_decompress_fuzzer_J2K.cpp
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
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <limits.h>
36
37 #include "openjpeg.h"
38
39 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv);
40 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
41
42 typedef struct {
43     const uint8_t* pabyData;
44     size_t         nCurPos;
45     size_t         nLength;
46 } MemFile;
47
48
49 static void ErrorCallback(const char * msg, void *)
50 {
51     (void)msg;
52     //fprintf(stderr, "%s\n", msg);
53 }
54
55
56 static void WarningCallback(const char *, void *)
57 {
58 }
59
60 static void InfoCallback(const char *, void *)
61 {
62 }
63
64 static OPJ_SIZE_T ReadCallback(void* pBuffer, OPJ_SIZE_T nBytes,
65                                void *pUserData)
66 {
67     MemFile* memFile = (MemFile*)pUserData;
68     //printf("want to read %d bytes at %d\n", (int)memFile->nCurPos, (int)nBytes);
69     if (memFile->nCurPos >= memFile->nLength) {
70         return -1;
71     }
72     if (memFile->nCurPos + nBytes >= memFile->nLength) {
73         size_t nToRead = memFile->nLength - memFile->nCurPos;
74         memcpy(pBuffer, memFile->pabyData + memFile->nCurPos, nToRead);
75         memFile->nCurPos = memFile->nLength;
76         return nToRead;
77     }
78     if (nBytes == 0) {
79         return -1;
80     }
81     memcpy(pBuffer, memFile->pabyData + memFile->nCurPos, nBytes);
82     memFile->nCurPos += nBytes;
83     return nBytes;
84 }
85
86 static OPJ_BOOL SeekCallback(OPJ_OFF_T nBytes, void * pUserData)
87 {
88     MemFile* memFile = (MemFile*)pUserData;
89     //printf("seek to %d\n", (int)nBytes);
90     memFile->nCurPos = nBytes;
91     return OPJ_TRUE;
92 }
93
94 static OPJ_OFF_T SkipCallback(OPJ_OFF_T nBytes, void * pUserData)
95 {
96     MemFile* memFile = (MemFile*)pUserData;
97     memFile->nCurPos += nBytes;
98     return nBytes;
99 }
100
101
102 int LLVMFuzzerInitialize(int* /*argc*/, char*** argv)
103 {
104     return 0;
105 }
106
107 static const unsigned char jpc_header[] = {0xff, 0x4f};
108 static const unsigned char jp2_box_jp[] = {0x6a, 0x50, 0x20, 0x20}; /* 'jP  ' */
109
110 int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
111 {
112
113     OPJ_CODEC_FORMAT eCodecFormat;
114     if (len >= sizeof(jpc_header) &&
115             memcmp(buf, jpc_header, sizeof(jpc_header)) == 0) {
116         eCodecFormat = OPJ_CODEC_J2K;
117     } else {
118         return 0;
119     }
120
121     opj_codec_t* pCodec = opj_create_decompress(eCodecFormat);
122     opj_set_info_handler(pCodec, InfoCallback, NULL);
123     opj_set_warning_handler(pCodec, WarningCallback, NULL);
124     opj_set_error_handler(pCodec, ErrorCallback, NULL);
125
126     opj_dparameters_t parameters;
127     opj_set_default_decoder_parameters(&parameters);
128
129     opj_setup_decoder(pCodec, &parameters);
130
131     opj_stream_t *pStream = opj_stream_create(1024, OPJ_TRUE);
132     MemFile memFile;
133     memFile.pabyData = buf;
134     memFile.nLength = len;
135     memFile.nCurPos = 0;
136     opj_stream_set_user_data_length(pStream, len);
137     opj_stream_set_read_function(pStream, ReadCallback);
138     opj_stream_set_seek_function(pStream, SeekCallback);
139     opj_stream_set_skip_function(pStream, SkipCallback);
140     opj_stream_set_user_data(pStream, &memFile, NULL);
141
142     opj_image_t * psImage = NULL;
143     if (!opj_read_header(pStream, pCodec, &psImage)) {
144         opj_destroy_codec(pCodec);
145         opj_stream_destroy(pStream);
146         opj_image_destroy(psImage);
147         return 0;
148     }
149
150     OPJ_UINT32 width = psImage->x1 - psImage->x0;
151     OPJ_UINT32 height = psImage->y1 - psImage->y0;
152
153 #if 0
154     // Reject too big images since that will require allocating a lot of
155     // memory
156     if (width != 0 && psImage->numcomps != 0 &&
157             (width > INT_MAX / psImage->numcomps ||
158              height > INT_MAX / (width * psImage->numcomps * sizeof(OPJ_UINT32)))) {
159         opj_stream_destroy(pStream);
160         opj_destroy_codec(pCodec);
161         opj_image_destroy(psImage);
162
163         return 0;
164     }
165
166     // Also reject too big tiles.
167     // TODO: remove this limitation when subtile decoding no longer imply
168     // allocation memory for whole tile
169     opj_codestream_info_v2_t* pCodeStreamInfo = opj_get_cstr_info(pCodec);
170     OPJ_UINT32 nTileW, nTileH;
171     nTileW = pCodeStreamInfo->tdx;
172     nTileH = pCodeStreamInfo->tdy;
173     opj_destroy_cstr_info(&pCodeStreamInfo);
174     if (nTileW > 2048 || nTileH > 2048) {
175         opj_stream_destroy(pStream);
176         opj_destroy_codec(pCodec);
177         opj_image_destroy(psImage);
178
179         return 0;
180     }
181 #endif
182
183     OPJ_UINT32 width_to_read = width;
184     if (width_to_read > 1024) {
185         width_to_read = 1024;
186     }
187     OPJ_UINT32 height_to_read = height;
188     if (height_to_read > 1024) {
189         height_to_read = 1024;
190     }
191
192     if (opj_set_decode_area(pCodec, psImage,
193                             psImage->x0, psImage->y0,
194                             psImage->x0 + width_to_read,
195                             psImage->y0 + height_to_read)) {
196         if (opj_decode(pCodec, pStream, psImage)) {
197             //printf("success\n");
198         }
199     }
200
201     opj_end_decompress(pCodec, pStream);
202     opj_stream_destroy(pStream);
203     opj_destroy_codec(pCodec);
204     opj_image_destroy(psImage);
205
206     return 0;
207 }