/src/libjpeg-turbo.3.0.x/fuzz/transform.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2021-2026 D. R. Commander. All Rights Reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions are met: |
6 | | * |
7 | | * - Redistributions of source code must retain the above copyright notice, |
8 | | * this list of conditions and the following disclaimer. |
9 | | * - Redistributions in binary form must reproduce the above copyright notice, |
10 | | * this list of conditions and the following disclaimer in the documentation |
11 | | * and/or other materials provided with the distribution. |
12 | | * - Neither the name of the libjpeg-turbo Project nor the names of its |
13 | | * contributors may be used to endorse or promote products derived from this |
14 | | * software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
17 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
20 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | | * POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #include "../turbojpeg.h" |
30 | | #include <stdlib.h> |
31 | | #include <stdint.h> |
32 | | #include <string.h> |
33 | | |
34 | | |
35 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
36 | 5.61k | { |
37 | 5.61k | tjhandle handle = NULL; |
38 | 5.61k | unsigned char *dstBufs[2] = { NULL, NULL }; |
39 | 5.61k | size_t dstSizes[2] = { 0, 0 }, maxBufSize, i; |
40 | 5.61k | int width = 0, height = 0, jpegSubsamp, dstSubsamp; |
41 | 5.61k | tjtransform transforms[2]; |
42 | | |
43 | 5.61k | if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL) |
44 | 0 | goto bailout; |
45 | | |
46 | | /* We ignore the return value of tj3DecompressHeader(), because malformed |
47 | | JPEG images that might expose issues in libjpeg-turbo might also have |
48 | | header errors that cause tj3DecompressHeader() to fail. */ |
49 | 5.61k | tj3DecompressHeader(handle, data, size); |
50 | 5.61k | width = tj3Get(handle, TJPARAM_JPEGWIDTH); |
51 | 5.61k | height = tj3Get(handle, TJPARAM_JPEGHEIGHT); |
52 | 5.61k | jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP); |
53 | | /* Let the transform options dictate the entropy coding algorithm. */ |
54 | 5.61k | tj3Set(handle, TJPARAM_ARITHMETIC, 0); |
55 | 5.61k | tj3Set(handle, TJPARAM_PROGRESSIVE, 0); |
56 | 5.61k | tj3Set(handle, TJPARAM_OPTIMIZE, 0); |
57 | | |
58 | | /* Ignore 0-pixel images and images larger than 1 Megapixel. Casting width |
59 | | to (uint64_t) prevents integer overflow if width * height > INT_MAX. */ |
60 | 5.61k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
61 | 1.19k | goto bailout; |
62 | | |
63 | 4.41k | tj3Set(handle, TJPARAM_SCANLIMIT, 100); |
64 | | |
65 | 4.41k | if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP) |
66 | 1.11k | jpegSubsamp = TJSAMP_444; |
67 | | |
68 | 4.41k | memset(&transforms[0], 0, sizeof(tjtransform) * 2); |
69 | | |
70 | 4.41k | transforms[0].op = TJXOP_NONE; |
71 | 4.41k | transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE; |
72 | 4.41k | dstBufs[0] = |
73 | 4.41k | (unsigned char *)tj3Alloc(tj3JPEGBufSize(width, height, jpegSubsamp)); |
74 | 4.41k | if (!dstBufs[0]) |
75 | 0 | goto bailout; |
76 | | |
77 | 4.41k | maxBufSize = tj3JPEGBufSize(width, height, jpegSubsamp); |
78 | | |
79 | 4.41k | tj3Set(handle, TJPARAM_NOREALLOC, 1); |
80 | 4.41k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
81 | 4.41k | transforms) == 0) { |
82 | | /* Touch all of the output data in order to catch uninitialized reads when |
83 | | using MemorySanitizer. */ |
84 | 55 | size_t sum = 0; |
85 | | |
86 | 299k | for (i = 0; i < dstSizes[0]; i++) |
87 | 299k | sum += dstBufs[0][i]; |
88 | | |
89 | | /* Prevent the sum above from being optimized out. This test should never |
90 | | be true, but the compiler doesn't know that. */ |
91 | 55 | if (sum > 255 * maxBufSize) |
92 | 0 | goto bailout; |
93 | 4.35k | } else if (!strcmp(tj3GetErrorStr(handle), |
94 | 4.35k | "Progressive JPEG image has more than 100 scans")) |
95 | 541 | goto bailout; |
96 | | |
97 | | |
98 | 3.87k | tj3Free(dstBufs[0]); |
99 | 3.87k | dstBufs[0] = NULL; |
100 | | |
101 | 3.87k | transforms[0].r.w = (height + 1) / 2; |
102 | 3.87k | transforms[0].r.h = (width + 1) / 2; |
103 | 3.87k | transforms[0].op = TJXOP_TRANSPOSE; |
104 | 3.87k | transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE | |
105 | 3.87k | TJXOPT_OPTIMIZE; |
106 | 3.87k | dstBufs[0] = |
107 | 3.87k | (unsigned char *)tj3Alloc(tj3JPEGBufSize((height + 1) / 2, (width + 1) / 2, |
108 | 3.87k | TJSAMP_GRAY)); |
109 | 3.87k | if (!dstBufs[0]) |
110 | 0 | goto bailout; |
111 | | |
112 | 3.87k | maxBufSize = tj3JPEGBufSize((height + 1) / 2, (width + 1) / 2, TJSAMP_GRAY); |
113 | | |
114 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
115 | 3.87k | transforms) == 0) { |
116 | 38 | size_t sum = 0; |
117 | | |
118 | 61.3k | for (i = 0; i < dstSizes[0]; i++) |
119 | 61.3k | sum += dstBufs[0][i]; |
120 | | |
121 | 38 | if (sum > 255 * maxBufSize) |
122 | 0 | goto bailout; |
123 | 3.83k | } else if (!strcmp(tj3GetErrorStr(handle), |
124 | 3.83k | "Progressive JPEG image has more than 100 scans")) |
125 | 0 | goto bailout; |
126 | | |
127 | 3.87k | tj3Free(dstBufs[0]); |
128 | 3.87k | dstBufs[0] = NULL; |
129 | | |
130 | 3.87k | transforms[0].op = TJXOP_ROT90; |
131 | 3.87k | transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC; |
132 | 3.87k | dstSubsamp = jpegSubsamp; |
133 | 3.87k | if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440; |
134 | 3.79k | else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422; |
135 | 3.68k | else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441; |
136 | 3.67k | else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411; |
137 | 3.87k | dstBufs[0] = |
138 | 3.87k | (unsigned char *)tj3Alloc(tj3JPEGBufSize(height, width, dstSubsamp)); |
139 | 3.87k | if (!dstBufs[0]) |
140 | 0 | goto bailout; |
141 | | |
142 | 3.87k | maxBufSize = tj3JPEGBufSize(height, width, dstSubsamp); |
143 | | |
144 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
145 | 3.87k | transforms) == 0) { |
146 | 64 | size_t sum = 0; |
147 | | |
148 | 305k | for (i = 0; i < dstSizes[0]; i++) |
149 | 305k | sum += dstBufs[0][i]; |
150 | | |
151 | 64 | if (sum > 255 * maxBufSize) |
152 | 0 | goto bailout; |
153 | 3.80k | } else if (!strcmp(tj3GetErrorStr(handle), |
154 | 3.80k | "Progressive JPEG image has more than 100 scans")) |
155 | 0 | goto bailout; |
156 | | |
157 | 3.87k | tj3Free(dstBufs[0]); |
158 | 3.87k | dstBufs[0] = NULL; |
159 | | |
160 | 3.87k | transforms[0].op = TJXOP_NONE; |
161 | 3.87k | transforms[0].options = TJXOPT_PROGRESSIVE; |
162 | 3.87k | if ((dstBufs[0] = (unsigned char *)tj3Alloc(100)) == NULL) |
163 | 0 | goto bailout; |
164 | 3.87k | dstSizes[0] = 100; |
165 | | |
166 | 3.87k | transforms[1].op = TJXOP_ROT180; |
167 | 3.87k | transforms[1].options = TJXOPT_TRIM; |
168 | 3.87k | if ((dstBufs[1] = (unsigned char *)tj3Alloc(50)) == NULL) |
169 | 0 | goto bailout; |
170 | 3.87k | dstSizes[1] = 50; |
171 | | |
172 | 3.87k | maxBufSize = dstSizes[0] + dstSizes[1]; |
173 | | |
174 | 3.87k | tj3Set(handle, TJPARAM_NOREALLOC, 0); |
175 | 3.87k | if (tj3Transform(handle, data, size, 2, dstBufs, dstSizes, |
176 | 3.87k | transforms) == 0) { |
177 | 55 | size_t sum = 0; |
178 | | |
179 | 857k | for (i = 0; i < dstSizes[0]; i++) |
180 | 857k | sum += dstBufs[0][i]; |
181 | 1.10M | for (i = 0; i < dstSizes[1]; i++) |
182 | 1.10M | sum += dstBufs[1][i]; |
183 | | |
184 | 55 | if (sum > 255 * maxBufSize) |
185 | 55 | goto bailout; |
186 | 3.81k | } else if (!strcmp(tj3GetErrorStr(handle), |
187 | 3.81k | "Progressive JPEG image has more than 100 scans")) |
188 | 0 | goto bailout; |
189 | | |
190 | 5.61k | bailout: |
191 | 5.61k | tj3Free(dstBufs[0]); |
192 | 5.61k | tj3Free(dstBufs[1]); |
193 | 5.61k | tj3Destroy(handle); |
194 | 5.61k | return 0; |
195 | 3.87k | } |