2 * Copyright (C)2021 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>
38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
40 tjhandle handle = NULL;
41 unsigned char *dstBufs[NUMXFORMS] = { NULL, NULL, NULL };
42 unsigned long dstSizes[NUMXFORMS] = { 0, 0, 0 }, maxBufSize;
43 int width = 0, height = 0, jpegSubsamp, jpegColorspace, i, t;
44 tjtransform transforms[NUMXFORMS];
45 #if defined(__has_feature) && __has_feature(memory_sanitizer)
46 char env[18] = "JSIMD_FORCENONE=1";
48 /* The libjpeg-turbo SIMD extensions produce false positives with
53 if ((handle = tjInitTransform()) == NULL)
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 tjDecompressHeader3(handle, data, size, &width, &height, &jpegSubsamp,
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 if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
67 if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
68 jpegSubsamp = TJSAMP_444;
70 for (t = 0; t < NUMXFORMS; t++)
71 memset(&transforms[t], 0, sizeof(tjtransform));
73 transforms[0].op = TJXOP_NONE;
74 transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
75 dstBufs[0] = (unsigned char *)malloc(tjBufSize(width, height, jpegSubsamp));
79 transforms[1].r.w = (width + 1) / 2;
80 transforms[1].r.h = (height + 1) / 2;
81 transforms[1].op = TJXOP_TRANSPOSE;
82 transforms[1].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE;
84 (unsigned char *)malloc(tjBufSize((width + 1) / 2, (height + 1) / 2,
89 transforms[2].op = TJXOP_ROT90;
90 transforms[2].options = TJXOPT_TRIM | TJXOPT_COPYNONE;
91 dstBufs[2] = (unsigned char *)malloc(tjBufSize(height, width, jpegSubsamp));
95 maxBufSize = tjBufSize(width, height, jpegSubsamp);
97 if (tjTransform(handle, data, size, NUMXFORMS, dstBufs, dstSizes, transforms,
98 TJFLAG_LIMITSCANS | TJFLAG_NOREALLOC) == 0) {
99 /* Touch all of the output pixels in order to catch uninitialized reads
100 when using MemorySanitizer. */
101 for (t = 0; t < NUMXFORMS; t++) {
104 for (i = 0; i < dstSizes[t]; i++)
105 sum += dstBufs[t][i];
107 /* Prevent the code above from being optimized out. This test should
108 never be true, but the compiler doesn't know that. */
109 if (sum > 255 * maxBufSize)
114 transforms[0].options &= ~TJXOPT_COPYNONE;
119 if (tjTransform(handle, data, size, 1, dstBufs, dstSizes, transforms,
120 TJFLAG_LIMITSCANS) == 0) {
123 for (i = 0; i < dstSizes[0]; i++)
124 sum += dstBufs[0][i];
126 if (sum > 255 * maxBufSize)
131 for (t = 0; t < NUMXFORMS; t++)
133 if (handle) tjDestroy(handle);