/src/libjpeg-turbo.2.0.x/fuzz/decompress.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C)2021, 2023 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 | 10.4k | #define NUMPF 4 |
35 | | |
36 | | |
37 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
38 | 10.4k | { |
39 | 10.4k | tjhandle handle = NULL; |
40 | 10.4k | unsigned char *dstBuf = NULL; |
41 | 10.4k | int width = 0, height = 0, jpegSubsamp, jpegColorspace, 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 | 10.4k | enum TJPF pixelFormats[NUMPF] = |
46 | 10.4k | { TJPF_RGB, TJPF_BGRX, TJPF_GRAY, TJPF_CMYK }; |
47 | | #if defined(__has_feature) && __has_feature(memory_sanitizer) |
48 | | char env[18] = "JSIMD_FORCENONE=1"; |
49 | | |
50 | | /* The libjpeg-turbo SIMD extensions produce false positives with |
51 | | MemorySanitizer. */ |
52 | | putenv(env); |
53 | | #endif |
54 | | |
55 | 10.4k | if ((handle = tjInitDecompress()) == NULL) |
56 | 0 | goto bailout; |
57 | | |
58 | | /* We ignore the return value of tjDecompressHeader3(), because some JPEG |
59 | | images may have unusual subsampling configurations that the TurboJPEG API |
60 | | cannot identify but can still decompress. */ |
61 | 10.4k | tjDecompressHeader3(handle, data, size, &width, &height, &jpegSubsamp, |
62 | 10.4k | &jpegColorspace); |
63 | | |
64 | | /* Ignore 0-pixel images and images larger than 1 Megapixel, as Google's |
65 | | OSS-Fuzz target for libjpeg-turbo did. Casting width to (uint64_t) |
66 | | prevents integer overflow if width * height > INT_MAX. */ |
67 | 10.4k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
68 | 2.78k | goto bailout; |
69 | | |
70 | 10.4k | for (pfi = 0; pfi < NUMPF; pfi++) { |
71 | 10.4k | int pf = pixelFormats[pfi], flags = TJFLAG_LIMITSCANS, i, sum = 0; |
72 | 10.4k | int w = width, h = height; |
73 | | |
74 | | /* Test non-default decompression options on the first iteration. */ |
75 | 10.4k | if (pfi == 0) |
76 | 7.65k | flags |= TJFLAG_BOTTOMUP | TJFLAG_FASTUPSAMPLE | TJFLAG_FASTDCT; |
77 | | /* Test IDCT scaling on the second iteration. */ |
78 | 2.77k | else if (pfi == 1) { |
79 | 932 | w = (width + 1) / 2; |
80 | 932 | h = (height + 1) / 2; |
81 | 932 | } |
82 | | |
83 | 10.4k | if ((dstBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL) |
84 | 0 | goto bailout; |
85 | | |
86 | 10.4k | if (tjDecompress2(handle, data, size, dstBuf, w, 0, h, pf, flags) == 0) { |
87 | | /* Touch all of the output pixels in order to catch uninitialized reads |
88 | | when using MemorySanitizer. */ |
89 | 583M | for (i = 0; i < w * h * tjPixelSize[pf]; i++) |
90 | 583M | sum += dstBuf[i]; |
91 | 2.77k | } else |
92 | 7.65k | goto bailout; |
93 | | |
94 | 2.77k | free(dstBuf); |
95 | 2.77k | dstBuf = NULL; |
96 | | |
97 | | /* Prevent the code above from being optimized out. This test should never |
98 | | be true, but the compiler doesn't know that. */ |
99 | 2.77k | if (sum > 255 * 1048576 * tjPixelSize[pf]) |
100 | 0 | goto bailout; |
101 | 2.77k | } |
102 | | |
103 | 10.4k | bailout: |
104 | 10.4k | free(dstBuf); |
105 | 10.4k | if (handle) tjDestroy(handle); |
106 | 10.4k | return 0; |
107 | 7.65k | } |