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>
46 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48 tjhandle handle = NULL;
49 unsigned short *srcBuf = NULL;
50 unsigned char *dstBuf = NULL;
51 int width = 0, height = 0, fd = -1, i, ti;
52 char filename[FILENAME_MAX] = { 0 };
53 struct test tests[NUMTESTS] = {
62 #if defined(__has_feature) && __has_feature(memory_sanitizer)
63 char env[18] = "JSIMD_FORCENONE=1";
65 /* The libjpeg-turbo SIMD extensions produce false positives with
70 snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_fuzz.XXXXXX");
71 if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0)
74 if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
77 for (ti = 0; ti < NUMTESTS; ti++) {
78 int sum = 0, pf = tests[ti].pf;
79 size_t dstSize = 0, maxBufSize;
81 /* Test non-default compression options on specific iterations. */
82 tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0);
83 tj3Set(handle, TJPARAM_NOREALLOC, ti != 2);
84 tj3Set(handle, TJPARAM_RESTARTROWS, ti == 0 || ti == 6 ? 1 : 0);
86 tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
87 /* tj3LoadImage16() will refuse to load images larger than 1 Megapixel, so
88 we don't need to check the width and height here. */
89 if ((srcBuf = tj3LoadImage16(handle, filename, &width, 1, &height,
93 maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
94 if (tj3Get(handle, TJPARAM_NOREALLOC)) {
95 if ((dstBuf = (unsigned char *)malloc(maxBufSize)) == NULL)
100 tj3Set(handle, TJPARAM_LOSSLESS, 1);
101 tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
102 tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
103 if (tj3Compress16(handle, srcBuf, width, 0, height, pf, &dstBuf,
105 /* Touch all of the output pixels in order to catch uninitialized reads
106 when using MemorySanitizer. */
107 for (i = 0; i < dstSize; i++)
116 /* Prevent the code above from being optimized out. This test should never
117 be true, but the compiler doesn't know that. */
118 if (sum > 255 * maxBufSize)
127 if (strlen(filename) > 0) unlink(filename);