/src/libjpeg-turbo.main/fuzz/compress16_lossless.cc
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 33.3k  | #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  | 4.16k  | { | 
48  | 4.16k  |   tjhandle handle = NULL;  | 
49  | 4.16k  |   unsigned short *srcBuf = NULL;  | 
50  | 4.16k  |   unsigned char *dstBuf = NULL;  | 
51  | 4.16k  |   int width = 0, height = 0, fd = -1, i, ti;  | 
52  | 4.16k  |   char filename[FILENAME_MAX] = { 0 }; | 
53  | 4.16k  |   struct test tests[NUMTESTS] = { | 
54  | 4.16k  |     { TJPF_RGB, 1, 0 }, | 
55  | 4.16k  |     { TJPF_BGR, 2, 2 }, | 
56  | 4.16k  |     { TJPF_RGBX, 3, 4 }, | 
57  | 4.16k  |     { TJPF_BGRA, 4, 7 }, | 
58  | 4.16k  |     { TJPF_XRGB, 5, 5 }, | 
59  | 4.16k  |     { TJPF_GRAY, 6, 3 }, | 
60  | 4.16k  |     { TJPF_CMYK, 7, 0 } | 
61  | 4.16k  |   };  | 
62  |  | #if defined(__has_feature) && __has_feature(memory_sanitizer)  | 
63  |  |   char env[18] = "JSIMD_FORCENONE=1";  | 
64  |  |  | 
65  |  |   /* The libjpeg-turbo SIMD extensions produce false positives with  | 
66  |  |      MemorySanitizer. */  | 
67  |  |   putenv(env);  | 
68  |  | #endif  | 
69  |  |  | 
70  | 4.16k  |   snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_fuzz.XXXXXX");  | 
71  | 4.16k  |   if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0)  | 
72  | 0  |     goto bailout;  | 
73  |  |  | 
74  | 4.16k  |   if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)  | 
75  | 0  |     goto bailout;  | 
76  |  |  | 
77  | 33.3k  |   for (ti = 0; ti < NUMTESTS; ti++) { | 
78  | 29.1k  |     int sum = 0, pf = tests[ti].pf;  | 
79  | 29.1k  |     size_t dstSize = 0, maxBufSize;  | 
80  |  |  | 
81  |  |     /* Test non-default compression options on specific iterations. */  | 
82  | 29.1k  |     tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0);  | 
83  | 29.1k  |     tj3Set(handle, TJPARAM_NOREALLOC, ti != 2);  | 
84  | 29.1k  |     tj3Set(handle, TJPARAM_RESTARTROWS, ti == 0 || ti == 6 ? 1 : 0);  | 
85  |  |  | 
86  | 29.1k  |     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  | 29.1k  |     if ((srcBuf = tj3LoadImage16(handle, filename, &width, 1, &height,  | 
90  | 29.1k  |                                  &pf)) == NULL)  | 
91  | 20.0k  |       continue;  | 
92  |  |  | 
93  | 9.13k  |     maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);  | 
94  | 9.13k  |     if (tj3Get(handle, TJPARAM_NOREALLOC)) { | 
95  | 7.80k  |       if ((dstBuf = (unsigned char *)malloc(maxBufSize)) == NULL)  | 
96  | 0  |         goto bailout;  | 
97  | 7.80k  |     } else  | 
98  | 1.32k  |       dstBuf = NULL;  | 
99  |  |  | 
100  | 9.13k  |     tj3Set(handle, TJPARAM_LOSSLESS, 1);  | 
101  | 9.13k  |     tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);  | 
102  | 9.13k  |     tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);  | 
103  | 9.13k  |     if (tj3Compress16(handle, srcBuf, width, 0, height, pf, &dstBuf,  | 
104  | 9.13k  |                       &dstSize) == 0) { | 
105  |  |       /* Touch all of the output pixels in order to catch uninitialized reads  | 
106  |  |          when using MemorySanitizer. */  | 
107  | 263M  |       for (i = 0; i < dstSize; i++)  | 
108  | 263M  |         sum += dstBuf[i];  | 
109  | 8.96k  |     }  | 
110  |  |  | 
111  | 9.13k  |     free(dstBuf);  | 
112  | 9.13k  |     dstBuf = NULL;  | 
113  | 9.13k  |     tj3Free(srcBuf);  | 
114  | 9.13k  |     srcBuf = NULL;  | 
115  |  |  | 
116  |  |     /* Prevent the code above from being optimized out.  This test should never  | 
117  |  |        be true, but the compiler doesn't know that. */  | 
118  | 9.13k  |     if (sum > 255 * maxBufSize)  | 
119  | 0  |       goto bailout;  | 
120  | 9.13k  |   }  | 
121  |  |  | 
122  | 4.16k  | bailout:  | 
123  | 4.16k  |   free(dstBuf);  | 
124  | 4.16k  |   tj3Free(srcBuf);  | 
125  | 4.16k  |   if (fd >= 0) { | 
126  | 4.16k  |     close(fd);  | 
127  | 4.16k  |     if (strlen(filename) > 0) unlink(filename);  | 
128  | 4.16k  |   }  | 
129  | 4.16k  |   tj3Destroy(handle);  | 
130  | 4.16k  |   return 0;  | 
131  | 4.16k  | }  |