Coverage Report

Created: 2026-06-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/fuzz/transform.cc
Line
Count
Source
1
/*
2
 * Copyright (C) 2011, 2021-2026 D. R. Commander
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 "../src/turbojpeg.h"
30
#include <stdlib.h>
31
#include <stdint.h>
32
#include <string.h>
33
34
35
static int dummyDCTFilter(short *coeffs, tjregion arrayRegion,
36
                          tjregion planeRegion, int componentIndex,
37
                          int transformIndex, tjtransform *transform)
38
1.97M
{
39
1.97M
  int i;
40
41
1.11G
  for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
42
1.10G
    coeffs[i] = -coeffs[i];
43
1.97M
  return 0;
44
1.97M
}
45
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
6.16k
{
49
6.16k
  tjhandle handle = NULL;
50
6.16k
  unsigned char *dstBufs[2] = { NULL, NULL };
51
6.16k
  size_t dstSizes[2] = { 0, 0 }, maxBufSize, i;
52
6.16k
  int width = 0, height = 0, jpegSubsamp;
53
6.16k
  tjtransform transforms[2];
54
55
6.16k
  if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL)
56
0
    goto bailout;
57
58
  /* We ignore the return value of tj3DecompressHeader(), because malformed
59
     JPEG images that might expose issues in libjpeg-turbo might also have
60
     header errors that cause tj3DecompressHeader() to fail. */
61
6.16k
  tj3DecompressHeader(handle, data, size);
62
6.16k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
63
6.16k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
64
6.16k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
65
  /* Let the transform options dictate the entropy coding algorithm. */
66
6.16k
  tj3Set(handle, TJPARAM_ARITHMETIC, 0);
67
6.16k
  tj3Set(handle, TJPARAM_PROGRESSIVE, 0);
68
6.16k
  tj3Set(handle, TJPARAM_OPTIMIZE, 0);
69
70
  /* Ignore 0-pixel images and images larger than 1 Megapixel.  Casting width
71
     to (uint64_t) prevents integer overflow if width * height > INT_MAX. */
72
6.16k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
73
1.56k
    goto bailout;
74
75
4.59k
  tj3Set(handle, TJPARAM_SCANLIMIT, 100);
76
77
4.59k
  if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
78
780
    jpegSubsamp = TJSAMP_444;
79
80
4.59k
  memset(&transforms[0], 0, sizeof(tjtransform) * 2);
81
82
4.59k
  transforms[0].op = TJXOP_NONE;
83
4.59k
  transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
84
4.59k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
85
4.59k
  if (dstSizes[0] == 0 ||
86
4.59k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
87
0
    goto bailout;
88
89
4.59k
  if (size >= 34)
90
4.41k
    tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
91
92
4.59k
  tj3Set(handle, TJPARAM_NOREALLOC, 1);
93
4.59k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
94
4.59k
                   transforms) == 0) {
95
    /* Touch all of the output data in order to catch uninitialized reads when
96
       using MemorySanitizer. */
97
51
    size_t sum = 0;
98
99
337k
    for (i = 0; i < dstSizes[0]; i++)
100
337k
      sum += dstBufs[0][i];
101
102
    /* Prevent the sum above from being optimized out.  This test should never
103
       be true, but the compiler doesn't know that. */
104
51
    if (sum > 255 * maxBufSize)
105
0
      goto bailout;
106
4.54k
  } else if (!strcmp(tj3GetErrorStr(handle),
107
4.54k
                     "Progressive JPEG image has more than 100 scans"))
108
570
    goto bailout;
109
110
111
4.02k
  tj3Free(dstBufs[0]);
112
4.02k
  dstBufs[0] = NULL;
113
114
4.02k
  transforms[0].r.w = (height + 1) / 2;
115
4.02k
  transforms[0].r.h = (width + 1) / 2;
116
4.02k
  transforms[0].op = TJXOP_TRANSPOSE;
117
4.02k
  transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE |
118
4.02k
                          TJXOPT_OPTIMIZE;
119
4.02k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
120
4.02k
  if (dstSizes[0] == 0 ||
121
4.02k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
122
0
    goto bailout;
123
124
4.02k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
125
4.02k
                   transforms) == 0) {
126
50
    size_t sum = 0;
127
128
310k
    for (i = 0; i < dstSizes[0]; i++)
129
310k
      sum += dstBufs[0][i];
130
131
50
    if (sum > 255 * maxBufSize)
132
0
      goto bailout;
133
3.97k
  } else if (!strcmp(tj3GetErrorStr(handle),
134
3.97k
                     "Progressive JPEG image has more than 100 scans"))
135
0
    goto bailout;
136
137
4.02k
  tj3Free(dstBufs[0]);
138
4.02k
  dstBufs[0] = NULL;
139
140
4.02k
  transforms[0].op = TJXOP_ROT90;
141
4.02k
  transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC;
142
4.02k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
143
4.02k
  if (dstSizes[0] == 0 ||
144
4.02k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
145
0
    goto bailout;
146
147
4.02k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
148
4.02k
                   transforms) == 0) {
149
61
    size_t sum = 0;
150
151
981k
    for (i = 0; i < dstSizes[0]; i++)
152
981k
      sum += dstBufs[0][i];
153
154
61
    if (sum > 255 * maxBufSize)
155
0
      goto bailout;
156
3.96k
  } else if (!strcmp(tj3GetErrorStr(handle),
157
3.96k
                     "Progressive JPEG image has more than 100 scans"))
158
0
    goto bailout;
159
160
4.02k
  tj3Free(dstBufs[0]);
161
4.02k
  dstBufs[0] = NULL;
162
163
4.02k
  transforms[0].op = TJXOP_NONE;
164
4.02k
  transforms[0].options = TJXOPT_PROGRESSIVE;
165
4.02k
  transforms[0].customFilter = dummyDCTFilter;
166
4.02k
  if ((dstBufs[0] = (unsigned char *)tj3Alloc(100)) == NULL)
167
0
    goto bailout;
168
4.02k
  dstSizes[0] = 100;
169
170
4.02k
  transforms[1].op = TJXOP_ROT180;
171
4.02k
  transforms[1].options = TJXOPT_TRIM;
172
4.02k
  if ((dstBufs[1] = (unsigned char *)tj3Alloc(50)) == NULL)
173
0
    goto bailout;
174
4.02k
  dstSizes[1] = 50;
175
176
4.02k
  maxBufSize = dstSizes[0] + dstSizes[1];
177
178
4.02k
  tj3Set(handle, TJPARAM_NOREALLOC, 0);
179
4.02k
  if (tj3Transform(handle, data, size, 2, dstBufs, dstSizes,
180
4.02k
                   transforms) == 0) {
181
39
    size_t sum = 0;
182
183
235k
    for (i = 0; i < dstSizes[0]; i++)
184
235k
      sum += dstBufs[0][i];
185
272k
    for (i = 0; i < dstSizes[1]; i++)
186
272k
      sum += dstBufs[1][i];
187
188
39
    if (sum > 255 * maxBufSize)
189
39
      goto bailout;
190
3.98k
  } else if (!strcmp(tj3GetErrorStr(handle),
191
3.98k
                     "Progressive JPEG image has more than 100 scans"))
192
0
    goto bailout;
193
194
6.16k
bailout:
195
6.16k
  tj3Free(dstBufs[0]);
196
6.16k
  tj3Free(dstBufs[1]);
197
6.16k
  tj3Destroy(handle);
198
6.16k
  return 0;
199
4.02k
}