Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/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
4.84M
{
39
4.84M
  int i;
40
41
2.32G
  for (i = 0; i < arrayRegion.w * arrayRegion.h; i++)
42
2.31G
    coeffs[i] = -coeffs[i];
43
4.84M
  return 0;
44
4.84M
}
45
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
10.9k
{
49
10.9k
  tjhandle handle = NULL;
50
10.9k
  unsigned char *dstBufs[1] = { NULL };
51
10.9k
  size_t dstSizes[1] = { 0 }, maxBufSize, i;
52
10.9k
  int width = 0, height = 0, jpegSubsamp;
53
10.9k
  tjtransform transforms[1];
54
55
10.9k
  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
10.9k
  tj3DecompressHeader(handle, data, size);
62
10.9k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
63
10.9k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
64
10.9k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
65
  /* Let the transform options dictate the entropy coding algorithm. */
66
10.9k
  tj3Set(handle, TJPARAM_ARITHMETIC, 0);
67
10.9k
  tj3Set(handle, TJPARAM_PROGRESSIVE, 0);
68
10.9k
  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
10.9k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
73
3.11k
    goto bailout;
74
75
7.86k
  tj3Set(handle, TJPARAM_SCANLIMIT, 500);
76
77
7.86k
  if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
78
1.19k
    jpegSubsamp = TJSAMP_444;
79
80
7.86k
  memset(&transforms[0], 0, sizeof(tjtransform));
81
82
7.86k
  transforms[0].op = TJXOP_NONE;
83
7.86k
  transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
84
7.86k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
85
7.86k
  if (dstSizes[0] == 0 ||
86
7.86k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
87
0
    goto bailout;
88
89
7.86k
  if (size >= 34)
90
7.55k
    tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
91
92
7.86k
  tj3Set(handle, TJPARAM_NOREALLOC, 1);
93
7.86k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
94
7.86k
                   transforms) == 0) {
95
    /* Touch all of the output data in order to catch uninitialized reads when
96
       using MemorySanitizer. */
97
116
    size_t sum = 0;
98
99
856k
    for (i = 0; i < dstSizes[0]; i++)
100
856k
      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
116
    if (sum > 255 * maxBufSize)
105
0
      goto bailout;
106
116
  }
107
108
7.86k
  tj3Free(dstBufs[0]);
109
7.86k
  dstBufs[0] = NULL;
110
111
7.86k
  transforms[0].r.w = (height + 1) / 2;
112
7.86k
  transforms[0].r.h = (width + 1) / 2;
113
7.86k
  transforms[0].op = TJXOP_TRANSPOSE;
114
7.86k
  transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE |
115
7.86k
                          TJXOPT_OPTIMIZE;
116
7.86k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
117
7.86k
  if (dstSizes[0] == 0 ||
118
7.86k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
119
0
    goto bailout;
120
121
7.86k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
122
7.86k
                   transforms) == 0) {
123
77
    size_t sum = 0;
124
125
224k
    for (i = 0; i < dstSizes[0]; i++)
126
224k
      sum += dstBufs[0][i];
127
128
77
    if (sum > 255 * maxBufSize)
129
0
      goto bailout;
130
77
  }
131
132
7.86k
  tj3Free(dstBufs[0]);
133
7.86k
  dstBufs[0] = NULL;
134
135
7.86k
  transforms[0].op = TJXOP_ROT90;
136
7.86k
  transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC;
137
7.86k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
138
7.86k
  if (dstSizes[0] == 0 ||
139
7.86k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
140
0
    goto bailout;
141
142
7.86k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
143
7.86k
                   transforms) == 0) {
144
144
    size_t sum = 0;
145
146
2.95M
    for (i = 0; i < dstSizes[0]; i++)
147
2.95M
      sum += dstBufs[0][i];
148
149
144
    if (sum > 255 * maxBufSize)
150
0
      goto bailout;
151
144
  }
152
153
7.86k
  tj3Free(dstBufs[0]);
154
7.86k
  dstBufs[0] = NULL;
155
156
7.86k
  transforms[0].op = TJXOP_NONE;
157
7.86k
  transforms[0].options = TJXOPT_PROGRESSIVE;
158
7.86k
  transforms[0].customFilter = dummyDCTFilter;
159
7.86k
  dstSizes[0] = 0;
160
161
7.86k
  tj3Set(handle, TJPARAM_NOREALLOC, 0);
162
7.86k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
163
7.86k
                   transforms) == 0) {
164
115
    size_t sum = 0;
165
166
1.96M
    for (i = 0; i < dstSizes[0]; i++)
167
1.96M
      sum += dstBufs[0][i];
168
169
115
    if (sum > 255 * maxBufSize)
170
0
      goto bailout;
171
115
  }
172
173
10.9k
bailout:
174
10.9k
  tj3Free(dstBufs[0]);
175
10.9k
  tj3Destroy(handle);
176
10.9k
  return 0;
177
7.86k
}