/src/libjpeg-turbo.main/fuzz/decompress.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 "../src/turbojpeg.h" |
30 | | #include <stdlib.h> |
31 | | #include <stdint.h> |
32 | | #include <string.h> |
33 | | |
34 | | |
35 | 30.5k | #define NUMPF 5 |
36 | | |
37 | | |
38 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
39 | 6.56k | { |
40 | 6.56k | tjhandle handle = NULL; |
41 | 6.56k | void *dstBuf = NULL; |
42 | 6.56k | int width = 0, height = 0, precision, sampleSize, pfi; |
43 | | /* TJPF_RGB-TJPF_BGR share the same code paths, as do TJPF_RGBX-TJPF_XRGB and |
44 | | TJPF_RGBA-TJPF_ARGB. Thus, the pixel formats below should be the minimum |
45 | | necessary to achieve full coverage. */ |
46 | 6.56k | enum TJPF pixelFormats[NUMPF] = |
47 | 6.56k | { TJPF_RGB, TJPF_BGRX, TJPF_ABGR, TJPF_GRAY, TJPF_CMYK }; |
48 | | |
49 | 6.56k | if ((handle = tj3Init(TJINIT_DECOMPRESS)) == NULL) |
50 | 0 | goto bailout; |
51 | | |
52 | | /* We ignore the return value of tj3DecompressHeader(), because malformed |
53 | | JPEG images that might expose issues in libjpeg-turbo might also have |
54 | | header errors that cause tj3DecompressHeader() to fail. */ |
55 | 6.56k | tj3DecompressHeader(handle, data, size); |
56 | 6.56k | width = tj3Get(handle, TJPARAM_JPEGWIDTH); |
57 | 6.56k | height = tj3Get(handle, TJPARAM_JPEGHEIGHT); |
58 | 6.56k | precision = tj3Get(handle, TJPARAM_PRECISION); |
59 | 6.56k | sampleSize = (precision > 8 ? 2 : 1); |
60 | | |
61 | | /* Ignore 0-pixel images and images larger than 1 Megapixel, as Google's |
62 | | OSS-Fuzz target for libjpeg-turbo did. Casting width to (uint64_t) |
63 | | prevents integer overflow if width * height > INT_MAX. */ |
64 | 6.56k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
65 | 1.45k | goto bailout; |
66 | | |
67 | 5.10k | tj3Set(handle, TJPARAM_SCANLIMIT, 500); |
68 | | |
69 | 30.5k | for (pfi = 0; pfi < NUMPF; pfi++) { |
70 | 25.4k | int w = width, h = height; |
71 | 25.4k | int pf = pixelFormats[pfi], i; |
72 | 25.4k | int64_t sum = 0; |
73 | | |
74 | | /* Test non-default decompression options on the first iteration. */ |
75 | 25.4k | tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0); |
76 | 25.4k | tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0); |
77 | | |
78 | 25.4k | if (!tj3Get(handle, TJPARAM_LOSSLESS)) { |
79 | 18.3k | tj3Set(handle, TJPARAM_FASTDCT, pfi == 0); |
80 | | |
81 | | /* Test IDCT scaling on the second and third iterations. */ |
82 | 18.3k | if (pfi == 1 || pfi == 2) { |
83 | 7.33k | tjscalingfactor sf = { 1, pfi == 1 ? 2 : 8 }; |
84 | 7.33k | tj3SetScalingFactor(handle, sf); |
85 | 7.33k | w = TJSCALED(width, sf); |
86 | 7.33k | h = TJSCALED(height, sf); |
87 | 7.33k | } else |
88 | 11.0k | tj3SetScalingFactor(handle, TJUNSCALED); |
89 | | |
90 | | /* Test partial image decompression on the second and fourth iterations, |
91 | | if the image is large enough. */ |
92 | 18.3k | if ((pfi == 1 || pfi == 3) && w >= 97 && h >= 75) { |
93 | 1.40k | tjregion cr = { 32, 16, 65, 59 }; |
94 | 1.40k | tj3SetCroppingRegion(handle, cr); |
95 | 1.40k | } else |
96 | 16.9k | tj3SetCroppingRegion(handle, TJUNCROPPED); |
97 | 18.3k | } |
98 | | |
99 | 25.4k | if ((dstBuf = tj3Alloc(w * h * tjPixelSize[pf] * sampleSize)) == NULL) |
100 | 0 | goto bailout; |
101 | | |
102 | 25.4k | if (precision == 8) { |
103 | 13.5k | if (tj3Decompress8(handle, data, size, (unsigned char *)dstBuf, 0, |
104 | 13.5k | pf) == 0) { |
105 | | /* Touch all of the output pixels in order to catch uninitialized reads |
106 | | when using MemorySanitizer. */ |
107 | 119M | for (i = 0; i < w * h * tjPixelSize[pf]; i++) |
108 | 119M | sum += ((unsigned char *)dstBuf)[i]; |
109 | 13.2k | } else if (!strcmp(tj3GetErrorStr(handle), |
110 | 13.2k | "Progressive JPEG image has more than 500 scans")) |
111 | 6 | goto bailout; |
112 | 13.5k | } else if (precision == 12) { |
113 | 8.44k | if (tj3Decompress12(handle, data, size, (short *)dstBuf, 0, pf) == 0) { |
114 | | /* Touch all of the output pixels in order to catch uninitialized reads |
115 | | when using MemorySanitizer. */ |
116 | 247M | for (i = 0; i < w * h * tjPixelSize[pf]; i++) |
117 | 247M | sum += ((short *)dstBuf)[i]; |
118 | 7.37k | } else if (!strcmp(tj3GetErrorStr(handle), |
119 | 7.37k | "Progressive JPEG image has more than 500 scans")) |
120 | 5 | goto bailout; |
121 | 8.44k | } else { |
122 | 3.45k | if (tj3Decompress16(handle, data, size, (unsigned short *)dstBuf, 0, |
123 | 3.45k | pf) == 0) { |
124 | | /* Touch all of the output pixels in order to catch uninitialized reads |
125 | | when using MemorySanitizer. */ |
126 | 214k | for (i = 0; i < w * h * tjPixelSize[pf]; i++) |
127 | 214k | sum += ((unsigned short *)dstBuf)[i]; |
128 | 3.36k | } else if (!strcmp(tj3GetErrorStr(handle), |
129 | 3.36k | "Progressive JPEG image has more than 500 scans")) |
130 | 1 | goto bailout; |
131 | 3.45k | } |
132 | | |
133 | 25.4k | tj3Free(dstBuf); |
134 | 25.4k | dstBuf = NULL; |
135 | | |
136 | | /* Prevent the sum above from being optimized out. This test should never |
137 | | be true, but the compiler doesn't know that. */ |
138 | 25.4k | if (sum > ((1LL << precision) - 1LL) * 1048576LL * tjPixelSize[pf]) |
139 | 0 | goto bailout; |
140 | 25.4k | } |
141 | | |
142 | 6.56k | bailout: |
143 | 6.56k | tj3Free(dstBuf); |
144 | 6.56k | tj3Destroy(handle); |
145 | 6.56k | return 0; |
146 | 5.10k | } |