Coverage Report

Created: 2025-11-24 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/fuzz/transform.cc
Line
Count
Source
1
/*
2
 * Copyright (C)2021-2025 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 "../turbojpeg.h"
30
#include <stdlib.h>
31
#include <stdint.h>
32
#include <string.h>
33
34
35
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
36
5.19k
{
37
5.19k
  tjhandle handle = NULL;
38
5.19k
  unsigned char *dstBufs[1] = { NULL };
39
5.19k
  size_t dstSizes[1] = { 0 }, maxBufSize, i;
40
5.19k
  int width = 0, height = 0, jpegSubsamp, dstSubsamp;
41
5.19k
  tjtransform transforms[1];
42
43
5.19k
  if ((handle = tj3Init(TJINIT_TRANSFORM)) == NULL)
44
0
    goto bailout;
45
46
  /* We ignore the return value of tj3DecompressHeader(), because malformed
47
     JPEG images that might expose issues in libjpeg-turbo might also have
48
     header errors that cause tj3DecompressHeader() to fail. */
49
5.19k
  tj3DecompressHeader(handle, data, size);
50
5.19k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
51
5.19k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
52
5.19k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
53
  /* Let the transform options dictate the entropy coding algorithm. */
54
5.19k
  tj3Set(handle, TJPARAM_ARITHMETIC, 0);
55
5.19k
  tj3Set(handle, TJPARAM_PROGRESSIVE, 0);
56
5.19k
  tj3Set(handle, TJPARAM_OPTIMIZE, 0);
57
58
  /* Ignore 0-pixel images and images larger than 1 Megapixel.  Casting width
59
     to (uint64_t) prevents integer overflow if width * height > INT_MAX. */
60
5.19k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
61
1.19k
    goto bailout;
62
63
3.99k
  tj3Set(handle, TJPARAM_SCANLIMIT, 500);
64
65
3.99k
  if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
66
884
    jpegSubsamp = TJSAMP_444;
67
68
3.99k
  memset(&transforms[0], 0, sizeof(tjtransform));
69
70
3.99k
  transforms[0].op = TJXOP_NONE;
71
3.99k
  transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
72
3.99k
  dstBufs[0] =
73
3.99k
    (unsigned char *)tj3Alloc(tj3JPEGBufSize(width, height, jpegSubsamp));
74
3.99k
  if (!dstBufs[0])
75
0
    goto bailout;
76
77
3.99k
  maxBufSize = tj3JPEGBufSize(width, height, jpegSubsamp);
78
79
3.99k
  tj3Set(handle, TJPARAM_NOREALLOC, 1);
80
3.99k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
81
3.99k
                   transforms) == 0) {
82
    /* Touch all of the output pixels in order to catch uninitialized reads
83
       when using MemorySanitizer. */
84
51
    size_t sum = 0;
85
86
250k
    for (i = 0; i < dstSizes[0]; i++)
87
250k
      sum += dstBufs[0][i];
88
89
    /* Prevent the code above from being optimized out.  This test should
90
       never be true, but the compiler doesn't know that. */
91
51
    if (sum > 255 * maxBufSize)
92
0
      goto bailout;
93
51
  }
94
95
3.99k
  free(dstBufs[0]);
96
3.99k
  dstBufs[0] = NULL;
97
98
3.99k
  transforms[0].r.w = (height + 1) / 2;
99
3.99k
  transforms[0].r.h = (width + 1) / 2;
100
3.99k
  transforms[0].op = TJXOP_TRANSPOSE;
101
3.99k
  transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE |
102
3.99k
                          TJXOPT_OPTIMIZE;
103
3.99k
  dstBufs[0] =
104
3.99k
    (unsigned char *)tj3Alloc(tj3JPEGBufSize((height + 1) / 2, (width + 1) / 2,
105
3.99k
                                             TJSAMP_GRAY));
106
3.99k
  if (!dstBufs[0])
107
0
    goto bailout;
108
109
3.99k
  maxBufSize = tj3JPEGBufSize((height + 1) / 2, (width + 1) / 2, TJSAMP_GRAY);
110
111
3.99k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
112
3.99k
                   transforms) == 0) {
113
36
    size_t sum = 0;
114
115
60.8k
    for (i = 0; i < dstSizes[0]; i++)
116
60.8k
      sum += dstBufs[0][i];
117
118
36
    if (sum > 255 * maxBufSize)
119
0
      goto bailout;
120
36
  }
121
122
3.99k
  free(dstBufs[0]);
123
3.99k
  dstBufs[0] = NULL;
124
125
3.99k
  transforms[0].op = TJXOP_ROT90;
126
3.99k
  transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC;
127
3.99k
  dstSubsamp = jpegSubsamp;
128
3.99k
  if (dstSubsamp == TJSAMP_422) dstSubsamp = TJSAMP_440;
129
3.92k
  else if (dstSubsamp == TJSAMP_440) dstSubsamp = TJSAMP_422;
130
3.78k
  else if (dstSubsamp == TJSAMP_411) dstSubsamp = TJSAMP_441;
131
3.76k
  else if (dstSubsamp == TJSAMP_441) dstSubsamp = TJSAMP_411;
132
3.99k
  dstBufs[0] =
133
3.99k
    (unsigned char *)tj3Alloc(tj3JPEGBufSize(height, width, dstSubsamp));
134
3.99k
  if (!dstBufs[0])
135
0
    goto bailout;
136
137
3.99k
  maxBufSize = tj3JPEGBufSize(height, width, dstSubsamp);
138
139
3.99k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
140
3.99k
                   transforms) == 0) {
141
57
    size_t sum = 0;
142
143
224k
    for (i = 0; i < dstSizes[0]; i++)
144
224k
      sum += dstBufs[0][i];
145
146
57
    if (sum > 255 * maxBufSize)
147
0
      goto bailout;
148
57
  }
149
150
3.99k
  free(dstBufs[0]);
151
3.99k
  dstBufs[0] = NULL;
152
153
3.99k
  transforms[0].op = TJXOP_NONE;
154
3.99k
  transforms[0].options = TJXOPT_PROGRESSIVE;
155
3.99k
  dstSizes[0] = 0;
156
157
3.99k
  tj3Set(handle, TJPARAM_NOREALLOC, 0);
158
3.99k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
159
3.99k
                   transforms) == 0) {
160
52
    size_t sum = 0;
161
162
250k
    for (i = 0; i < dstSizes[0]; i++)
163
250k
      sum += dstBufs[0][i];
164
165
52
    if (sum > 255 * maxBufSize)
166
0
      goto bailout;
167
52
  }
168
169
5.19k
bailout:
170
5.19k
  free(dstBufs[0]);
171
5.19k
  tj3Destroy(handle);
172
5.19k
  return 0;
173
3.99k
}