Coverage Report

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