/src/libjpeg-turbo.main/fuzz/decompress_yuv.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 | 19.6k | #define NUMPF 4 |
36 | | |
37 | | |
38 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
39 | 5.60k | { |
40 | 5.60k | tjhandle handle = NULL; |
41 | 5.60k | unsigned char *dstBuf = NULL, *yuvBuf = NULL; |
42 | 5.60k | int width = 0, height = 0, jpegSubsamp, 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 | 5.60k | enum TJPF pixelFormats[NUMPF] = |
47 | 5.60k | { TJPF_BGR, TJPF_RGBA, TJPF_XRGB, TJPF_GRAY }; |
48 | | |
49 | 5.60k | 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 | 5.60k | tj3DecompressHeader(handle, data, size); |
56 | 5.60k | width = tj3Get(handle, TJPARAM_JPEGWIDTH); |
57 | 5.60k | height = tj3Get(handle, TJPARAM_JPEGHEIGHT); |
58 | 5.60k | jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP); |
59 | | |
60 | | /* Ignore 0-pixel images and images larger than 1 Megapixel. Casting width |
61 | | to (uint64_t) prevents integer overflow if width * height > INT_MAX. */ |
62 | 5.60k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
63 | 1.56k | goto bailout; |
64 | | |
65 | 4.03k | tj3Set(handle, TJPARAM_SCANLIMIT, 500); |
66 | | |
67 | 19.6k | for (pfi = 0; pfi < NUMPF; pfi++) { |
68 | 15.7k | int w = width, h = height; |
69 | 15.7k | int pf = pixelFormats[pfi], i, sum = 0; |
70 | | |
71 | | /* Test non-default decompression options on the first iteration. */ |
72 | 15.7k | if (!tj3Get(handle, TJPARAM_LOSSLESS)) { |
73 | 11.2k | tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0); |
74 | 11.2k | tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0); |
75 | 11.2k | tj3Set(handle, TJPARAM_FASTDCT, pfi == 0); |
76 | | |
77 | | /* Test IDCT scaling on the second and third iteration. */ |
78 | 11.2k | if (pfi == 1 || pfi == 2) { |
79 | 5.58k | tjscalingfactor sf = { pfi == 1 ? 3 : 1, 4 }; |
80 | 5.58k | tj3SetScalingFactor(handle, sf); |
81 | 5.58k | w = TJSCALED(width, sf); |
82 | 5.58k | h = TJSCALED(height, sf); |
83 | 5.58k | } else |
84 | 5.71k | tj3SetScalingFactor(handle, TJUNSCALED); |
85 | 11.2k | } |
86 | | |
87 | 15.7k | if ((dstBuf = (unsigned char *)tj3Alloc(w * h * tjPixelSize[pf])) == NULL) |
88 | 0 | goto bailout; |
89 | 15.7k | if ((yuvBuf = |
90 | 15.7k | (unsigned char *)tj3Alloc(tj3YUVBufSize(w, 1, h, |
91 | 15.7k | jpegSubsamp))) == NULL) |
92 | 0 | goto bailout; |
93 | | |
94 | 15.7k | if (tj3DecompressToYUV8(handle, data, size, yuvBuf, 1) == 0 && |
95 | 728 | tj3DecodeYUV8(handle, yuvBuf, 1, dstBuf, w, 0, h, pf) == 0) { |
96 | | /* Touch all of the output pixels in order to catch uninitialized reads |
97 | | when using MemorySanitizer. */ |
98 | 387M | for (i = 0; i < w * h * tjPixelSize[pf]; i++) |
99 | 387M | sum += dstBuf[i]; |
100 | 15.0k | } else if (!strcmp(tj3GetErrorStr(handle), |
101 | 15.0k | "Progressive JPEG image has more than 500 scans")) |
102 | 129 | goto bailout; |
103 | | |
104 | 15.6k | tj3Free(dstBuf); |
105 | 15.6k | dstBuf = NULL; |
106 | 15.6k | tj3Free(yuvBuf); |
107 | 15.6k | yuvBuf = NULL; |
108 | | |
109 | | /* Prevent the sum above from being optimized out. This test should never |
110 | | be true, but the compiler doesn't know that. */ |
111 | 15.6k | if (sum > 255 * 1048576 * tjPixelSize[pf]) |
112 | 0 | goto bailout; |
113 | 15.6k | } |
114 | | |
115 | 5.60k | bailout: |
116 | 5.60k | tj3Free(dstBuf); |
117 | 5.60k | tj3Free(yuvBuf); |
118 | 5.60k | tj3Destroy(handle); |
119 | 5.60k | return 0; |
120 | 4.03k | } |