/src/libjpeg-turbo.main/fuzz/transform.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C)2011, 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 "../src/turbojpeg.h" |
30 | | #include <stdlib.h> |
31 | | #include <stdint.h> |
32 | | #include <string.h> |
33 | | |
34 | | |
35 | | static int dummyDCTFilter(short *coeffs, tjregion arrayRegion, |
36 | | tjregion planeRegion, int componentIndex, |
37 | | int transformIndex, tjtransform *transform) |
38 | 1.88M | { |
39 | 1.88M | int i; |
40 | | |
41 | 1.06G | for (i = 0; i < arrayRegion.w * arrayRegion.h; i++) |
42 | 1.05G | coeffs[i] = -coeffs[i]; |
43 | 1.88M | return 0; |
44 | 1.88M | } |
45 | | |
46 | | |
47 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
48 | 5.54k | { |
49 | 5.54k | tjhandle handle = NULL; |
50 | 5.54k | unsigned char *dstBufs[1] = { NULL }; |
51 | 5.54k | size_t dstSizes[1] = { 0 }, maxBufSize, i; |
52 | 5.54k | int width = 0, height = 0, jpegSubsamp; |
53 | 5.54k | tjtransform transforms[1]; |
54 | | |
55 | 5.54k | if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL) |
56 | 0 | goto bailout; |
57 | | |
58 | | /* We ignore the return value of tj3DecompressHeader(), because malformed |
59 | | JPEG images that might expose issues in libjpeg-turbo might also have |
60 | | header errors that cause tj3DecompressHeader() to fail. */ |
61 | 5.54k | tj3DecompressHeader(handle, data, size); |
62 | 5.54k | width = tj3Get(handle, TJPARAM_JPEGWIDTH); |
63 | 5.54k | height = tj3Get(handle, TJPARAM_JPEGHEIGHT); |
64 | 5.54k | jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP); |
65 | | /* Let the transform options dictate the entropy coding algorithm. */ |
66 | 5.54k | tj3Set(handle, TJPARAM_ARITHMETIC, 0); |
67 | 5.54k | tj3Set(handle, TJPARAM_PROGRESSIVE, 0); |
68 | 5.54k | tj3Set(handle, TJPARAM_OPTIMIZE, 0); |
69 | | |
70 | | /* Ignore 0-pixel images and images larger than 1 Megapixel. Casting width |
71 | | to (uint64_t) prevents integer overflow if width * height > INT_MAX. */ |
72 | 5.54k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
73 | 1.67k | goto bailout; |
74 | | |
75 | 3.87k | tj3Set(handle, TJPARAM_SCANLIMIT, 500); |
76 | | |
77 | 3.87k | if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP) |
78 | 470 | jpegSubsamp = TJSAMP_444; |
79 | | |
80 | 3.87k | memset(&transforms[0], 0, sizeof(tjtransform)); |
81 | | |
82 | 3.87k | transforms[0].op = TJXOP_NONE; |
83 | 3.87k | transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE; |
84 | 3.87k | dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]); |
85 | 3.87k | if (dstSizes[0] == 0 || |
86 | 3.87k | (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL) |
87 | 0 | goto bailout; |
88 | | |
89 | 3.87k | if (size >= 34) |
90 | 3.70k | tj3SetICCProfile(handle, (unsigned char *)&data[2], 32); |
91 | | |
92 | 3.87k | tj3Set(handle, TJPARAM_NOREALLOC, 1); |
93 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
94 | 3.87k | transforms) == 0) { |
95 | | /* Touch all of the output data in order to catch uninitialized reads when |
96 | | using MemorySanitizer. */ |
97 | 57 | size_t sum = 0; |
98 | | |
99 | 462k | for (i = 0; i < dstSizes[0]; i++) |
100 | 462k | sum += dstBufs[0][i]; |
101 | | |
102 | | /* Prevent the sum above from being optimized out. This test should never |
103 | | be true, but the compiler doesn't know that. */ |
104 | 57 | if (sum > 255 * maxBufSize) |
105 | 0 | goto bailout; |
106 | 57 | } |
107 | | |
108 | 3.87k | tj3Free(dstBufs[0]); |
109 | 3.87k | dstBufs[0] = NULL; |
110 | | |
111 | 3.87k | transforms[0].r.w = (height + 1) / 2; |
112 | 3.87k | transforms[0].r.h = (width + 1) / 2; |
113 | 3.87k | transforms[0].op = TJXOP_TRANSPOSE; |
114 | 3.87k | transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE | |
115 | 3.87k | TJXOPT_OPTIMIZE; |
116 | 3.87k | dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]); |
117 | 3.87k | if (dstSizes[0] == 0 || |
118 | 3.87k | (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL) |
119 | 0 | goto bailout; |
120 | | |
121 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
122 | 3.87k | transforms) == 0) { |
123 | 40 | size_t sum = 0; |
124 | | |
125 | 162k | for (i = 0; i < dstSizes[0]; i++) |
126 | 162k | sum += dstBufs[0][i]; |
127 | | |
128 | 40 | if (sum > 255 * maxBufSize) |
129 | 0 | goto bailout; |
130 | 40 | } |
131 | | |
132 | 3.87k | tj3Free(dstBufs[0]); |
133 | 3.87k | dstBufs[0] = NULL; |
134 | | |
135 | 3.87k | transforms[0].op = TJXOP_ROT90; |
136 | 3.87k | transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC; |
137 | 3.87k | dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]); |
138 | 3.87k | if (dstSizes[0] == 0 || |
139 | 3.87k | (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL) |
140 | 0 | goto bailout; |
141 | | |
142 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
143 | 3.87k | transforms) == 0) { |
144 | 79 | size_t sum = 0; |
145 | | |
146 | 1.69M | for (i = 0; i < dstSizes[0]; i++) |
147 | 1.69M | sum += dstBufs[0][i]; |
148 | | |
149 | 79 | if (sum > 255 * maxBufSize) |
150 | 0 | goto bailout; |
151 | 79 | } |
152 | | |
153 | 3.87k | tj3Free(dstBufs[0]); |
154 | 3.87k | dstBufs[0] = NULL; |
155 | | |
156 | 3.87k | transforms[0].op = TJXOP_NONE; |
157 | 3.87k | transforms[0].options = TJXOPT_PROGRESSIVE; |
158 | 3.87k | transforms[0].customFilter = dummyDCTFilter; |
159 | 3.87k | dstSizes[0] = 0; |
160 | | |
161 | 3.87k | tj3Set(handle, TJPARAM_NOREALLOC, 0); |
162 | 3.87k | if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes, |
163 | 3.87k | transforms) == 0) { |
164 | 56 | size_t sum = 0; |
165 | | |
166 | 1.01M | for (i = 0; i < dstSizes[0]; i++) |
167 | 1.01M | sum += dstBufs[0][i]; |
168 | | |
169 | 56 | if (sum > 255 * maxBufSize) |
170 | 0 | goto bailout; |
171 | 56 | } |
172 | | |
173 | 5.54k | bailout: |
174 | 5.54k | tj3Free(dstBufs[0]); |
175 | 5.54k | tj3Destroy(handle); |
176 | 5.54k | return 0; |
177 | 3.87k | } |