Coverage Report

Created: 2026-05-30 07:16

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
5.28M
{
39
5.28M
  int i;
40
41
1.91G
  for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
42
1.91G
    coeffs[i] = -coeffs[i];
43
5.28M
  return 0;
44
5.28M
}
45
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
12.2k
{
49
12.2k
  tjhandle handle = NULL;
50
12.2k
  unsigned char *dstBufs[2] = { NULL, NULL };
51
12.2k
  size_t dstSizes[2] = { 0, 0 }, maxBufSize, i;
52
12.2k
  int width = 0, height = 0, jpegSubsamp;
53
12.2k
  tjtransform transforms[2];
54
55
12.2k
  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
12.2k
  tj3DecompressHeader(handle, data, size);
62
12.2k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
63
12.2k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
64
12.2k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
65
  /* Let the transform options dictate the entropy coding algorithm. */
66
12.2k
  tj3Set(handle, TJPARAM_ARITHMETIC, 0);
67
12.2k
  tj3Set(handle, TJPARAM_PROGRESSIVE, 0);
68
12.2k
  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
12.2k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
73
2.77k
    goto bailout;
74
75
9.43k
  tj3Set(handle, TJPARAM_SCANLIMIT, 100);
76
77
9.43k
  if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
78
2.91k
    jpegSubsamp = TJSAMP_444;
79
80
9.43k
  memset(&transforms[0], 0, sizeof(tjtransform) * 2);
81
82
9.43k
  transforms[0].op = TJXOP_NONE;
83
9.43k
  transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
84
9.43k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
85
9.43k
  if (dstSizes[0] == 0 ||
86
9.43k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
87
0
    goto bailout;
88
89
9.43k
  if (size >= 34)
90
9.18k
    tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
91
92
9.43k
  tj3Set(handle, TJPARAM_NOREALLOC, 1);
93
9.43k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
94
9.43k
                   transforms) == 0) {
95
    /* Touch all of the output data in order to catch uninitialized reads when
96
       using MemorySanitizer. */
97
158
    size_t sum = 0;
98
99
724k
    for (i = 0; i < dstSizes[0]; i++)
100
724k
      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
158
    if (sum > 255 * maxBufSize)
105
0
      goto bailout;
106
9.28k
  } else if (!strcmp(tj3GetErrorStr(handle),
107
9.28k
                     "Progressive JPEG image has more than 100 scans"))
108
1.12k
    goto bailout;
109
110
111
8.31k
  tj3Free(dstBufs[0]);
112
8.31k
  dstBufs[0] = NULL;
113
114
8.31k
  transforms[0].r.w = (height + 1) / 2;
115
8.31k
  transforms[0].r.h = (width + 1) / 2;
116
8.31k
  transforms[0].op = TJXOP_TRANSPOSE;
117
8.31k
  transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE |
118
8.31k
                          TJXOPT_OPTIMIZE;
119
8.31k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
120
8.31k
  if (dstSizes[0] == 0 ||
121
8.31k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
122
0
    goto bailout;
123
124
8.31k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
125
8.31k
                   transforms) == 0) {
126
140
    size_t sum = 0;
127
128
433k
    for (i = 0; i < dstSizes[0]; i++)
129
433k
      sum += dstBufs[0][i];
130
131
140
    if (sum > 255 * maxBufSize)
132
0
      goto bailout;
133
8.17k
  } else if (!strcmp(tj3GetErrorStr(handle),
134
8.17k
                     "Progressive JPEG image has more than 100 scans"))
135
0
    goto bailout;
136
137
8.31k
  tj3Free(dstBufs[0]);
138
8.31k
  dstBufs[0] = NULL;
139
140
8.31k
  transforms[0].op = TJXOP_ROT90;
141
8.31k
  transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC;
142
8.31k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
143
8.31k
  if (dstSizes[0] == 0 ||
144
8.31k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
145
0
    goto bailout;
146
147
8.31k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
148
8.31k
                   transforms) == 0) {
149
192
    size_t sum = 0;
150
151
1.42M
    for (i = 0; i < dstSizes[0]; i++)
152
1.42M
      sum += dstBufs[0][i];
153
154
192
    if (sum > 255 * maxBufSize)
155
0
      goto bailout;
156
8.12k
  } else if (!strcmp(tj3GetErrorStr(handle),
157
8.12k
                     "Progressive JPEG image has more than 100 scans"))
158
0
    goto bailout;
159
160
8.31k
  tj3Free(dstBufs[0]);
161
8.31k
  dstBufs[0] = NULL;
162
163
8.31k
  transforms[0].op = TJXOP_NONE;
164
8.31k
  transforms[0].options = TJXOPT_PROGRESSIVE;
165
8.31k
  transforms[0].customFilter = dummyDCTFilter;
166
8.31k
  if ((dstBufs[0] = (unsigned char *)tj3Alloc(100)) == NULL)
167
0
    goto bailout;
168
8.31k
  dstSizes[0] = 100;
169
170
8.31k
  transforms[1].op = TJXOP_ROT180;
171
8.31k
  transforms[1].options = TJXOPT_TRIM;
172
8.31k
  if ((dstBufs[1] = (unsigned char *)tj3Alloc(50)) == NULL)
173
0
    goto bailout;
174
8.31k
  dstSizes[1] = 50;
175
176
8.31k
  maxBufSize = dstSizes[0] + dstSizes[1];
177
178
8.31k
  tj3Set(handle, TJPARAM_NOREALLOC, 0);
179
8.31k
  if (tj3Transform(handle, data, size, 2, dstBufs, dstSizes,
180
8.31k
                   transforms) == 0) {
181
144
    size_t sum = 0;
182
183
1.51M
    for (i = 0; i < dstSizes[0]; i++)
184
1.51M
      sum += dstBufs[0][i];
185
1.83M
    for (i = 0; i < dstSizes[1]; i++)
186
1.83M
      sum += dstBufs[1][i];
187
188
144
    if (sum > 255 * maxBufSize)
189
143
      goto bailout;
190
8.16k
  } else if (!strcmp(tj3GetErrorStr(handle),
191
8.16k
                     "Progressive JPEG image has more than 100 scans"))
192
0
    goto bailout;
193
194
12.2k
bailout:
195
12.2k
  tj3Free(dstBufs[0]);
196
12.2k
  tj3Free(dstBufs[1]);
197
12.2k
  tj3Destroy(handle);
198
12.2k
  return 0;
199
8.31k
}