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;
40 unsigned char *dstBuf = NULL, *yuvBuf = NULL;
41 int width = 0, height = 0, jpegSubsamp, 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_BGR, TJPF_XRGB, TJPF_GRAY };
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 jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
66 /* Ignore 0-pixel images and images larger than 1 Megapixel. Casting width
67 to (uint64_t) prevents integer overflow if width * height > INT_MAX. */
68 if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
71 tj3Set(handle, TJPARAM_SCANLIMIT, 500);
73 for (pfi = 0; pfi < NUMPF; pfi++) {
74 int w = width, h = height;
75 int pf = pixelFormats[pfi], i, sum = 0;
77 /* Test non-default decompression options on the first iteration. */
78 if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
79 tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
80 tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
81 tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
83 /* Test IDCT scaling on the second iteration. */
85 tjscalingfactor sf = { 3, 4 };
86 tj3SetScalingFactor(handle, sf);
87 w = TJSCALED(width, sf);
88 h = TJSCALED(height, sf);
90 tj3SetScalingFactor(handle, TJUNSCALED);
93 if ((dstBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL)
96 (unsigned char *)malloc(tj3YUVBufSize(w, 1, h, jpegSubsamp))) == NULL)
99 if (tj3DecompressToYUV8(handle, data, size, yuvBuf, 1) == 0 &&
100 tj3DecodeYUV8(handle, yuvBuf, 1, dstBuf, w, 0, h, pf) == 0) {
101 /* Touch all of the output pixels in order to catch uninitialized reads
102 when using MemorySanitizer. */
103 for (i = 0; i < w * h * tjPixelSize[pf]; i++)
113 /* Prevent the code above from being optimized out. This test should never
114 be true, but the compiler doesn't know that. */
115 if (sum > 255 * 1048576 * tjPixelSize[pf])