/src/libjpeg-turbo.3.0.x/fuzz/compress_yuv.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C)2021-2026 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 | | extern "C" unsigned char * |
37 | | _tj3LoadImageFromFileHandle8(tjhandle handle, FILE *file, int *width, |
38 | | int align, int *height, int *pixelFormat); |
39 | | |
40 | | |
41 | 24.6k | #define NUMTESTS 6 |
42 | | |
43 | | |
44 | | struct test { |
45 | | enum TJPF pf; |
46 | | enum TJSAMP subsamp; |
47 | | int quality; |
48 | | }; |
49 | | |
50 | | |
51 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
52 | 3.51k | { |
53 | 3.51k | tjhandle handle = NULL; |
54 | 3.51k | unsigned char *srcBuf = NULL, *dstBuf = NULL, *yuvBuf = NULL; |
55 | 3.51k | int width = 0, height = 0, ti; |
56 | 3.51k | FILE *file = NULL; |
57 | 3.51k | struct test tests[NUMTESTS] = { |
58 | 3.51k | { TJPF_XBGR, TJSAMP_444, 100 }, |
59 | 3.51k | { TJPF_XRGB, TJSAMP_422, 90 }, |
60 | 3.51k | { TJPF_BGR, TJSAMP_420, 80 }, |
61 | 3.51k | { TJPF_RGB, TJSAMP_411, 70 }, |
62 | 3.51k | { TJPF_BGR, TJSAMP_GRAY, 60 }, |
63 | 3.51k | { TJPF_GRAY, TJSAMP_GRAY, 50 } |
64 | 3.51k | }; |
65 | | |
66 | 3.51k | if ((file = fmemopen((void *)data, size, "r")) == NULL) |
67 | 0 | goto bailout; |
68 | | |
69 | 3.51k | if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL) |
70 | 0 | goto bailout; |
71 | | |
72 | 24.6k | for (ti = 0; ti < NUMTESTS; ti++) { |
73 | 21.1k | int pf = tests[ti].pf; |
74 | 21.1k | size_t dstSize = 0, maxBufSize, i, sum = 0; |
75 | | |
76 | | /* Test non-default compression options on specific iterations. */ |
77 | 21.1k | tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0); |
78 | 21.1k | tj3Set(handle, TJPARAM_FASTDCT, ti == 1); |
79 | 21.1k | tj3Set(handle, TJPARAM_OPTIMIZE, ti == 4); |
80 | 21.1k | tj3Set(handle, TJPARAM_PROGRESSIVE, ti == 1 || ti == 3); |
81 | 21.1k | tj3Set(handle, TJPARAM_ARITHMETIC, ti == 2 || ti == 3); |
82 | 21.1k | tj3Set(handle, TJPARAM_NOREALLOC, 1); |
83 | 21.1k | tj3Set(handle, TJPARAM_RESTARTBLOCKS, ti == 3 || ti == 4 ? 4 : 0); |
84 | | |
85 | 21.1k | 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 | 21.1k | fseek(file, 0, SEEK_SET); |
89 | 21.1k | if ((srcBuf = _tj3LoadImageFromFileHandle8(handle, file, &width, 1, |
90 | 21.1k | &height, &pf)) == NULL) |
91 | 9.77k | continue; |
92 | | |
93 | 11.3k | maxBufSize = tj3JPEGBufSize(width, height, tests[ti].subsamp); |
94 | 11.3k | if ((dstBuf = (unsigned char *)tj3Alloc(maxBufSize)) == NULL) |
95 | 0 | goto bailout; |
96 | 11.3k | if ((yuvBuf = |
97 | 11.3k | (unsigned char *)malloc(tj3YUVBufSize(width, 1, height, |
98 | 11.3k | tests[ti].subsamp))) == NULL) |
99 | 0 | goto bailout; |
100 | | |
101 | 11.3k | tj3Set(handle, TJPARAM_SUBSAMP, tests[ti].subsamp); |
102 | 11.3k | tj3Set(handle, TJPARAM_QUALITY, tests[ti].quality); |
103 | 11.3k | if (tj3EncodeYUV8(handle, srcBuf, width, 0, height, pf, yuvBuf, 1) == 0 && |
104 | 11.2k | tj3CompressFromYUV8(handle, yuvBuf, width, 1, height, &dstBuf, |
105 | 11.2k | &dstSize) == 0) { |
106 | | /* Touch all of the output data in order to catch uninitialized reads |
107 | | when using MemorySanitizer. */ |
108 | 126M | for (i = 0; i < dstSize; i++) |
109 | 126M | sum += dstBuf[i]; |
110 | 11.2k | } |
111 | | |
112 | 11.3k | tj3Free(dstBuf); |
113 | 11.3k | dstBuf = NULL; |
114 | | |
115 | 11.3k | free(yuvBuf); |
116 | 11.3k | yuvBuf = NULL; |
117 | 11.3k | tj3Free(srcBuf); |
118 | 11.3k | srcBuf = NULL; |
119 | | |
120 | | /* Prevent the sum above from being optimized out. This test should never |
121 | | be true, but the compiler doesn't know that. */ |
122 | 11.3k | if (sum > 255 * maxBufSize) |
123 | 0 | goto bailout; |
124 | 11.3k | } |
125 | | |
126 | 3.51k | bailout: |
127 | 3.51k | tj3Free(dstBuf); |
128 | 3.51k | free(yuvBuf); |
129 | 3.51k | tj3Free(srcBuf); |
130 | 3.51k | if (file) fclose(file); |
131 | 3.51k | tj3Destroy(handle); |
132 | 3.51k | return 0; |
133 | 3.51k | } |