Coverage Report

Created: 2026-02-26 07:09

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