2 * Copyright (C)2021-2023 D. R. Commander. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
29 #include <turbojpeg.h>
37 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39 tjhandle handle = NULL;
41 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 enum TJPF pixelFormats[NUMPF] =
46 { TJPF_RGB, TJPF_BGRX, TJPF_GRAY, TJPF_CMYK };
47 #if defined(__has_feature) && __has_feature(memory_sanitizer)
48 char env[18] = "JSIMD_FORCENONE=1";
50 /* The libjpeg-turbo SIMD extensions produce false positives with
55 if ((handle = tj3Init(TJINIT_DECOMPRESS)) == NULL)
58 /* We ignore the return value of tj3DecompressHeader(), because malformed
59 JPEG images that might expose issues in libjpeg-turbo might also have
60 header errors that cause tj3DecompressHeader() to fail. */
61 tj3DecompressHeader(handle, data, size);
62 width = tj3Get(handle, TJPARAM_JPEGWIDTH);
63 height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
64 precision = tj3Get(handle, TJPARAM_PRECISION);
65 sampleSize = (precision > 8 ? 2 : 1);
67 /* Ignore 0-pixel images and images larger than 1 Megapixel, as Google's
68 OSS-Fuzz target for libjpeg-turbo did. Casting width to (uint64_t)
69 prevents integer overflow if width * height > INT_MAX. */
70 if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
73 tj3Set(handle, TJPARAM_SCANLIMIT, 500);
75 for (pfi = 0; pfi < NUMPF; pfi++) {
76 int w = width, h = height;
77 int pf = pixelFormats[pfi], i;
80 /* Test non-default decompression options on the first iteration. */
81 tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
82 tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
84 if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
85 tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
87 /* Test IDCT scaling on the second iteration. */
89 tjscalingfactor sf = { 1, 2 };
90 tj3SetScalingFactor(handle, sf);
91 w = TJSCALED(width, sf);
92 h = TJSCALED(height, sf);
94 tj3SetScalingFactor(handle, TJUNSCALED);
96 /* Test partial image decompression on the fourth iteration, if the image
98 if (pfi == 3 && w >= 97 && h >= 75) {
99 tjregion cr = { 32, 16, 65, 59 };
100 tj3SetCroppingRegion(handle, cr);
102 tj3SetCroppingRegion(handle, TJUNCROPPED);
105 if ((dstBuf = malloc(w * h * tjPixelSize[pf] * sampleSize)) == NULL)
108 if (precision == 8) {
109 if (tj3Decompress8(handle, data, size, (unsigned char *)dstBuf, 0,
111 /* Touch all of the output pixels in order to catch uninitialized reads
112 when using MemorySanitizer. */
113 for (i = 0; i < w * h * tjPixelSize[pf]; i++)
114 sum += ((unsigned char *)dstBuf)[i];
117 } else if (precision == 12) {
118 if (tj3Decompress12(handle, data, size, (short *)dstBuf, 0, pf) == 0) {
119 /* Touch all of the output pixels in order to catch uninitialized reads
120 when using MemorySanitizer. */
121 for (i = 0; i < w * h * tjPixelSize[pf]; i++)
122 sum += ((short *)dstBuf)[i];
126 if (tj3Decompress16(handle, data, size, (unsigned short *)dstBuf, 0,
128 /* Touch all of the output pixels in order to catch uninitialized reads
129 when using MemorySanitizer. */
130 for (i = 0; i < w * h * tjPixelSize[pf]; i++)
131 sum += ((unsigned short *)dstBuf)[i];
139 /* Prevent the code above from being optimized out. This test should never
140 be true, but the compiler doesn't know that. */
141 if (sum > ((1LL << precision) - 1LL) * 1048576LL * tjPixelSize[pf])