/src/libjpeg-turbo.2.1.x/fuzz/compress_yuv.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 <stdio.h> |
31 | | #include <stdlib.h> |
32 | | #include <stdint.h> |
33 | | #include <string.h> |
34 | | #include <unistd.h> |
35 | | |
36 | | |
37 | 47.4k | #define NUMTESTS 6 |
38 | | /* Private flag that triggers different TurboJPEG API behavior when fuzzing */ |
39 | 40.6k | #define TJFLAG_FUZZING (1 << 30) |
40 | | |
41 | | |
42 | | struct test { |
43 | | enum TJPF pf; |
44 | | enum TJSAMP subsamp; |
45 | | int quality; |
46 | | }; |
47 | | |
48 | | |
49 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
50 | 6.77k | { |
51 | 6.77k | tjhandle handle = NULL; |
52 | 6.77k | unsigned char *srcBuf = NULL, *dstBuf = NULL, *yuvBuf = NULL; |
53 | 6.77k | int width = 0, height = 0, fd = -1, i, ti; |
54 | 6.77k | char filename[FILENAME_MAX] = { 0 }; |
55 | 6.77k | struct test tests[NUMTESTS] = { |
56 | 6.77k | { TJPF_XBGR, TJSAMP_444, 100 }, |
57 | 6.77k | { TJPF_XRGB, TJSAMP_422, 90 }, |
58 | 6.77k | { TJPF_BGR, TJSAMP_420, 80 }, |
59 | 6.77k | { TJPF_RGB, TJSAMP_411, 70 }, |
60 | 6.77k | { TJPF_BGR, TJSAMP_GRAY, 60 }, |
61 | 6.77k | { TJPF_GRAY, TJSAMP_GRAY, 50 } |
62 | 6.77k | }; |
63 | 6.77k | char arithEnv[16] = "TJ_ARITHMETIC=0"; |
64 | 6.77k | char restartEnv[13] = "TJ_RESTART=0"; |
65 | | #if defined(__has_feature) && __has_feature(memory_sanitizer) |
66 | | char simdEnv[18] = "JSIMD_FORCENONE=1"; |
67 | | |
68 | | /* The libjpeg-turbo SIMD extensions produce false positives with |
69 | | MemorySanitizer. */ |
70 | | putenv(simdEnv); |
71 | | #endif |
72 | 6.77k | putenv(arithEnv); |
73 | 6.77k | putenv(restartEnv); |
74 | | |
75 | 6.77k | snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_yuv_fuzz.XXXXXX"); |
76 | 6.77k | if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0) |
77 | 0 | goto bailout; |
78 | | |
79 | 6.77k | if ((handle = tjInitCompress()) == NULL) |
80 | 0 | goto bailout; |
81 | | |
82 | 47.4k | for (ti = 0; ti < NUMTESTS; ti++) { |
83 | 40.6k | int flags = TJFLAG_FUZZING | TJFLAG_NOREALLOC, sum = 0, pf = tests[ti].pf; |
84 | 40.6k | unsigned long dstSize = 0, maxBufSize; |
85 | | |
86 | | /* Test non-default compression options on specific iterations. */ |
87 | 40.6k | if (ti == 0) |
88 | 6.77k | flags |= TJFLAG_BOTTOMUP | TJFLAG_ACCURATEDCT; |
89 | 33.8k | else if (ti == 1 || ti == 3) |
90 | 13.5k | flags |= TJFLAG_PROGRESSIVE; |
91 | 40.6k | if (ti == 2 || ti == 3) |
92 | 13.5k | arithEnv[14] = '1'; |
93 | 27.1k | else |
94 | 27.1k | arithEnv[14] = '0'; |
95 | 40.6k | if (ti == 1 || ti == 2) |
96 | 13.5k | restartEnv[11] = '2'; |
97 | 27.1k | else |
98 | 27.1k | restartEnv[11] = '0'; |
99 | | |
100 | | /* tjLoadImage() refuses to load images larger than 1 Megapixel when |
101 | | FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION is defined (yes, that's a dirty |
102 | | hack), so we don't need to check the width and height here. */ |
103 | 40.6k | if ((srcBuf = tjLoadImage(filename, &width, 1, &height, &pf, |
104 | 40.6k | flags)) == NULL) |
105 | 19.4k | continue; |
106 | | |
107 | 21.1k | maxBufSize = tjBufSize(width, height, tests[ti].subsamp); |
108 | 21.1k | if ((dstBuf = (unsigned char *)malloc(maxBufSize)) == NULL) |
109 | 0 | goto bailout; |
110 | 21.1k | if ((yuvBuf = |
111 | 21.1k | (unsigned char *)malloc(tjBufSizeYUV2(width, 1, height, |
112 | 21.1k | tests[ti].subsamp))) == NULL) |
113 | 0 | goto bailout; |
114 | | |
115 | 21.1k | if (tjEncodeYUV3(handle, srcBuf, width, 0, height, pf, yuvBuf, 1, |
116 | 21.1k | tests[ti].subsamp, flags) == 0 && |
117 | 21.1k | tjCompressFromYUV(handle, yuvBuf, width, 1, height, tests[ti].subsamp, |
118 | 21.1k | &dstBuf, &dstSize, tests[ti].quality, flags) == 0) { |
119 | | /* Touch all of the output pixels in order to catch uninitialized reads |
120 | | when using MemorySanitizer. */ |
121 | 139M | for (i = 0; i < dstSize; i++) |
122 | 139M | sum += dstBuf[i]; |
123 | 21.1k | } |
124 | | |
125 | 21.1k | free(dstBuf); |
126 | 21.1k | dstBuf = NULL; |
127 | 21.1k | free(yuvBuf); |
128 | 21.1k | yuvBuf = NULL; |
129 | 21.1k | tjFree(srcBuf); |
130 | 21.1k | srcBuf = 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 | 21.1k | if (sum > 255 * maxBufSize) |
135 | 0 | goto bailout; |
136 | 21.1k | } |
137 | | |
138 | 6.77k | bailout: |
139 | 6.77k | free(dstBuf); |
140 | 6.77k | free(yuvBuf); |
141 | 6.77k | tjFree(srcBuf); |
142 | 6.77k | if (fd >= 0) { |
143 | 6.77k | close(fd); |
144 | 6.77k | if (strlen(filename) > 0) unlink(filename); |
145 | 6.77k | } |
146 | 6.77k | if (handle) tjDestroy(handle); |
147 | 6.77k | return 0; |
148 | 6.77k | } |