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