/src/libjpeg-turbo.2.0.x/fuzz/transform.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C)2021 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 | | #include <string.h> |
33 | | |
34 | | |
35 | 84.7k | #define NUMXFORMS 3 |
36 | | |
37 | | |
38 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
39 | 10.8k | { |
40 | 10.8k | tjhandle handle = NULL; |
41 | 10.8k | unsigned char *dstBufs[NUMXFORMS] = { NULL, NULL, NULL }; |
42 | 10.8k | unsigned long dstSizes[NUMXFORMS] = { 0, 0, 0 }, maxBufSize; |
43 | 10.8k | int width = 0, height = 0, jpegSubsamp, jpegColorspace, i, t; |
44 | 10.8k | tjtransform transforms[NUMXFORMS]; |
45 | | #if defined(__has_feature) && __has_feature(memory_sanitizer) |
46 | | char env[18] = "JSIMD_FORCENONE=1"; |
47 | | |
48 | | /* The libjpeg-turbo SIMD extensions produce false positives with |
49 | | MemorySanitizer. */ |
50 | | putenv(env); |
51 | | #endif |
52 | | |
53 | 10.8k | if ((handle = tjInitTransform()) == NULL) |
54 | 0 | goto bailout; |
55 | | |
56 | | /* We ignore the return value of tjDecompressHeader3(), because some JPEG |
57 | | images may have unusual subsampling configurations that the TurboJPEG API |
58 | | cannot identify but can still transform. */ |
59 | 10.8k | tjDecompressHeader3(handle, data, size, &width, &height, &jpegSubsamp, |
60 | 10.8k | &jpegColorspace); |
61 | | |
62 | | /* Ignore 0-pixel images and images larger than 1 Megapixel. Casting width |
63 | | to (uint64_t) prevents integer overflow if width * height > INT_MAX. */ |
64 | 10.8k | if (width < 1 || height < 1 || (uint64_t)width * height > 1048576) |
65 | 2.56k | goto bailout; |
66 | | |
67 | 8.24k | if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP) |
68 | 184 | jpegSubsamp = TJSAMP_444; |
69 | | |
70 | 32.9k | for (t = 0; t < NUMXFORMS; t++) |
71 | 24.7k | memset(&transforms[t], 0, sizeof(tjtransform)); |
72 | | |
73 | 8.24k | transforms[0].op = TJXOP_NONE; |
74 | 8.24k | transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE; |
75 | 8.24k | dstBufs[0] = (unsigned char *)malloc(tjBufSize(width, height, jpegSubsamp)); |
76 | 8.24k | if (!dstBufs[0]) |
77 | 0 | goto bailout; |
78 | | |
79 | 8.24k | transforms[1].r.w = (width + 1) / 2; |
80 | 8.24k | transforms[1].r.h = (height + 1) / 2; |
81 | 8.24k | transforms[1].op = TJXOP_TRANSPOSE; |
82 | 8.24k | transforms[1].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE; |
83 | 8.24k | dstBufs[1] = |
84 | 8.24k | (unsigned char *)malloc(tjBufSize((width + 1) / 2, (height + 1) / 2, |
85 | 8.24k | TJSAMP_GRAY)); |
86 | 8.24k | if (!dstBufs[1]) |
87 | 0 | goto bailout; |
88 | | |
89 | 8.24k | transforms[2].op = TJXOP_ROT90; |
90 | 8.24k | transforms[2].options = TJXOPT_TRIM | TJXOPT_COPYNONE; |
91 | 8.24k | dstBufs[2] = (unsigned char *)malloc(tjBufSize(height, width, jpegSubsamp)); |
92 | 8.24k | if (!dstBufs[2]) |
93 | 0 | goto bailout; |
94 | | |
95 | 8.24k | maxBufSize = tjBufSize(width, height, jpegSubsamp); |
96 | | |
97 | 8.24k | if (tjTransform(handle, data, size, NUMXFORMS, dstBufs, dstSizes, transforms, |
98 | 8.24k | TJFLAG_LIMITSCANS | TJFLAG_NOREALLOC) == 0) { |
99 | | /* Touch all of the output pixels in order to catch uninitialized reads |
100 | | when using MemorySanitizer. */ |
101 | 220 | for (t = 0; t < NUMXFORMS; t++) { |
102 | 165 | int sum = 0; |
103 | | |
104 | 813k | for (i = 0; i < dstSizes[t]; i++) |
105 | 813k | sum += dstBufs[t][i]; |
106 | | |
107 | | /* Prevent the code above from being optimized out. This test should |
108 | | never be true, but the compiler doesn't know that. */ |
109 | 165 | if (sum > 255 * maxBufSize) |
110 | 0 | goto bailout; |
111 | 165 | } |
112 | 55 | } |
113 | | |
114 | 8.24k | transforms[0].options &= ~TJXOPT_COPYNONE; |
115 | 8.24k | free(dstBufs[0]); |
116 | 8.24k | dstBufs[0] = NULL; |
117 | 8.24k | dstSizes[0] = 0; |
118 | | |
119 | 8.24k | if (tjTransform(handle, data, size, 1, dstBufs, dstSizes, transforms, |
120 | 8.24k | TJFLAG_LIMITSCANS) == 0) { |
121 | 56 | int sum = 0; |
122 | | |
123 | 234k | for (i = 0; i < dstSizes[0]; i++) |
124 | 234k | sum += dstBufs[0][i]; |
125 | | |
126 | 56 | if (sum > 255 * maxBufSize) |
127 | 0 | goto bailout; |
128 | 56 | } |
129 | | |
130 | 10.8k | bailout: |
131 | 43.2k | for (t = 0; t < NUMXFORMS; t++) |
132 | 32.4k | free(dstBufs[t]); |
133 | 10.8k | if (handle) tjDestroy(handle); |
134 | 10.8k | return 0; |
135 | 8.24k | } |