Imported Upstream version 3.0.1
[platform/upstream/libjpeg-turbo.git] / fuzz / compress_lossless.cc
1 /*
2  * Copyright (C)2021-2023 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 #define NUMTESTS  7
38
39
40 struct test {
41   enum TJPF pf;
42   int psv, pt;
43 };
44
45
46 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47 {
48   tjhandle handle = NULL;
49   unsigned char *srcBuf = NULL, *dstBuf = NULL;
50   int width = 0, height = 0, fd = -1, i, ti;
51   char filename[FILENAME_MAX] = { 0 };
52   struct test tests[NUMTESTS] = {
53     { TJPF_RGB, 1, 0 },
54     { TJPF_BGR, 2, 2 },
55     { TJPF_RGBX, 3, 4 },
56     { TJPF_BGRA, 4, 7 },
57     { TJPF_XRGB, 5, 5 },
58     { TJPF_GRAY, 6, 3 },
59     { TJPF_CMYK, 7, 0 }
60   };
61 #if defined(__has_feature) && __has_feature(memory_sanitizer)
62   char env[18] = "JSIMD_FORCENONE=1";
63
64   /* The libjpeg-turbo SIMD extensions produce false positives with
65      MemorySanitizer. */
66   putenv(env);
67 #endif
68
69   snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_fuzz.XXXXXX");
70   if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0)
71     goto bailout;
72
73   if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
74     goto bailout;
75
76   for (ti = 0; ti < NUMTESTS; ti++) {
77     int sum = 0, pf = tests[ti].pf;
78     size_t dstSize = 0, maxBufSize;
79
80     /* Test non-default compression options on specific iterations. */
81     tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0);
82     tj3Set(handle, TJPARAM_NOREALLOC, ti != 2);
83     tj3Set(handle, TJPARAM_RESTARTROWS, ti == 0 || ti == 6 ? 1 : 0);
84
85     tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
86     /* tj3LoadImage8() will refuse to load images larger than 1 Megapixel, so
87        we don't need to check the width and height here. */
88     if ((srcBuf = tj3LoadImage8(handle, filename, &width, 1, &height,
89                                 &pf)) == NULL)
90       continue;
91
92     maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
93     if (tj3Get(handle, TJPARAM_NOREALLOC)) {
94       if ((dstBuf = (unsigned char *)malloc(maxBufSize)) == NULL)
95         goto bailout;
96     } else
97       dstBuf = NULL;
98
99     tj3Set(handle, TJPARAM_LOSSLESS, 1);
100     tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
101     tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
102     if (tj3Compress8(handle, srcBuf, width, 0, height, pf, &dstBuf,
103                      &dstSize) == 0) {
104       /* Touch all of the output pixels in order to catch uninitialized reads
105          when using MemorySanitizer. */
106       for (i = 0; i < dstSize; i++)
107         sum += dstBuf[i];
108     }
109
110     free(dstBuf);
111     dstBuf = NULL;
112     tj3Free(srcBuf);
113     srcBuf = NULL;
114
115     /* Prevent the code above from being optimized out.  This test should never
116        be true, but the compiler doesn't know that. */
117     if (sum > 255 * maxBufSize)
118       goto bailout;
119   }
120
121 bailout:
122   free(dstBuf);
123   tj3Free(srcBuf);
124   if (fd >= 0) {
125     close(fd);
126     if (strlen(filename) > 0) unlink(filename);
127   }
128   tj3Destroy(handle);
129   return 0;
130 }