Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/oss-fuzz/tiff_read_rgba_fuzzer.cc

raw
1 /* Copyright (c) 1988-1997 Sam Leffler
2 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the names of
8 * Sam Leffler and Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Sam Leffler and Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
13 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
14 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
17 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
18 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
20 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24 #include <cstdint>
25 #include <cstdlib>
26 #include <cstring>
27 #include <sstream>
28 #include <tiffio.h>
29 #include <tiffio.hxx>
30
31 /* stolen from tiffiop.h, which is a private header so we can't just include it
32 */
33 /* safe multiply returns either the multiplied value or 0 if it overflowed */
34 #define __TIFFSafeMultiply(t, v, m) \
35 ((((t)(m) != (t)0) && (((t)(((v) * (m)) / (m))) == (t)(v))) \
36 ? (t)((v) * (m)) \
37 : (t)0)
38
39 const uint64_t MAX_SIZE = 500000000;
40
41 extern "C" void handle_error(const char *unused, const char *unused2,
42 va_list unused3)
43 {
44 return;
45 }
46
47 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
48 {
49 #ifndef STANDALONE
50 TIFFSetErrorHandler(handle_error);
51 TIFFSetWarningHandler(handle_error);
52 #endif
53 #if defined(__has_feature)
54 #if __has_feature(memory_sanitizer)
55 // libjpeg-turbo has issues with MSAN and SIMD code
56 // See https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=7547
57 // and https://github.com/libjpeg-turbo/libjpeg-turbo/pull/365
58 setenv("JSIMD_FORCENONE", "1", 1);
59 #endif
60 #endif
61 std::istringstream s(std::string(Data, Data + Size));
62 TIFF *tif = TIFFStreamOpen("MemTIFF", &s);
63 if (!tif)
64 {
65 return 0;
66 }
67 uint32_t w, h;
68 uint32_t *raster;
69
70 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
71 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
72 /* don't continue if file size is ludicrous */
73 if (TIFFTileSize64(tif) > MAX_SIZE)
74 {
75 TIFFClose(tif);
76 return 0;
77 }
78 uint64_t bufsize = TIFFTileSize64(tif);
79 /* don't continue if the buffer size greater than the max allowed by the
80 * fuzzer */
81 if (bufsize > MAX_SIZE || bufsize == 0)
82 {
83 TIFFClose(tif);
84 return 0;
85 }
86 /* another hack to work around an OOM in tif_fax3.c */
87 uint32_t tilewidth = 0;
88 uint32_t imagewidth = 0;
89 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tilewidth);
90 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imagewidth);
91 tilewidth = __TIFFSafeMultiply(uint32_t, tilewidth, 2);
92 imagewidth = __TIFFSafeMultiply(uint32_t, imagewidth, 2);
93 if (tilewidth * 2 > MAX_SIZE || imagewidth * 2 > MAX_SIZE ||
94 tilewidth == 0 || imagewidth == 0)
95 {
96 TIFFClose(tif);
97 return 0;
98 }
99 uint32_t size = __TIFFSafeMultiply(uint32_t, w, h);
100 if (size > MAX_SIZE || size == 0)
101 {
102 TIFFClose(tif);
103 return 0;
104 }
105 raster = (uint32_t *)_TIFFmalloc(size * sizeof(uint32_t));
106 if (raster != NULL)
107 {
108 TIFFReadRGBAImage(tif, w, h, raster, 0);
109 _TIFFfree(raster);
110 }
111 TIFFClose(tif);
112
113 return 0;
114 }
115
116 #ifdef STANDALONE
117
118 template <class T> static void CPL_IGNORE_RET_VAL(T) {}
119
120 static void Usage(int, char *argv[])
121 {
122 fprintf(stderr, "%s [--help] [-repeat N] filename.\n", argv[0]);
123 exit(1);
124 }
125
126 int main(int argc, char *argv[])
127 {
128 int nRet = 0;
129 void *buf = NULL;
130 int nLen = 0;
131 int nLoops = 1;
132 const char *pszFilename = NULL;
133
134 for (int i = 1; i < argc; i++)
135 {
136 if (i + 1 < argc && strcmp(argv[i], "-repeat") == 0)
137 {
138 nLoops = atoi(argv[i + 1]);
139 i++;
140 }
141 else if (strcmp(argv[i], "-dummy") == 0)
142 {
143 uint8_t dummy = ' ';
144 return LLVMFuzzerTestOneInput(&dummy, 1);
145 }
146 else if (strcmp(argv[i], "--help") == 0)
147 {
148 Usage(argc, argv);
149 }
150 else if (argv[i][0] == '-')
151 {
152 fprintf(stderr, "Unrecognized option: %s", argv[i]);
153 Usage(argc, argv);
154 }
155 else
156 {
157 pszFilename = argv[i];
158 }
159 }
160 if (pszFilename == nullptr)
161 {
162 fprintf(stderr, "No filename specified\n");
163 Usage(argc, argv);
164 }
165 FILE *f = fopen(pszFilename, "rb");
166 if (!f)
167 {
168 fprintf(stderr, "%s does not exist.\n", pszFilename);
169 exit(1);
170 }
171 fseek(f, 0, SEEK_END);
172 nLen = (int)ftell(f);
173 fseek(f, 0, SEEK_SET);
174 buf = malloc(nLen);
175 if (!buf)
176 {
177 fprintf(stderr, "malloc failed.\n");
178 fclose(f);
179 exit(1);
180 }
181 CPL_IGNORE_RET_VAL(fread(buf, nLen, 1, f));
182 fclose(f);
183 for (int i = 0; i < nLoops; i++)
184 {
185 nRet = LLVMFuzzerTestOneInput(static_cast<const uint8_t *>(buf), nLen);
186 if (nRet != 0)
187 break;
188 }
189 free(buf);
190 return nRet;
191 }
192
193 #endif