Coverage Report

Created: 2025-10-10 07:05

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)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 "../src/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
10.5k
{
37
10.5k
  tjhandle handle = NULL;
38
10.5k
  unsigned char *dstBufs[1] = { NULL };
39
10.5k
  size_t dstSizes[1] = { 0 }, maxBufSize, i;
40
10.5k
  int width = 0, height = 0, jpegSubsamp;
41
10.5k
  tjtransform transforms[1];
42
43
10.5k
  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
10.5k
  tj3DecompressHeader(handle, data, size);
50
10.5k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
51
10.5k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
52
10.5k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
53
  /* Let the transform options dictate the entropy coding algorithm. */
54
10.5k
  tj3Set(handle, TJPARAM_ARITHMETIC, 0);
55
10.5k
  tj3Set(handle, TJPARAM_PROGRESSIVE, 0);
56
10.5k
  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
10.5k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
61
2.89k
    goto bailout;
62
63
7.66k
  tj3Set(handle, TJPARAM_SCANLIMIT, 500);
64
65
7.66k
  if (jpegSubsamp < 0 || jpegSubsamp >= TJ_NUMSAMP)
66
1.11k
    jpegSubsamp = TJSAMP_444;
67
68
7.66k
  memset(&transforms[0], 0, sizeof(tjtransform));
69
70
7.66k
  transforms[0].op = TJXOP_NONE;
71
7.66k
  transforms[0].options = TJXOPT_PROGRESSIVE | TJXOPT_COPYNONE;
72
7.66k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
73
7.66k
  if (dstSizes[0] == 0 ||
74
7.66k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
75
0
    goto bailout;
76
77
7.66k
  tj3Set(handle, TJPARAM_NOREALLOC, 1);
78
7.66k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
79
7.66k
                   transforms) == 0) {
80
    /* Touch all of the output pixels in order to catch uninitialized reads
81
       when using MemorySanitizer. */
82
129
    size_t sum = 0;
83
84
932k
    for (i = 0; i < dstSizes[0]; i++)
85
932k
      sum += dstBufs[0][i];
86
87
    /* Prevent the code above from being optimized out.  This test should
88
       never be true, but the compiler doesn't know that. */
89
129
    if (sum > 255 * maxBufSize)
90
0
      goto bailout;
91
129
  }
92
93
7.66k
  free(dstBufs[0]);
94
7.66k
  dstBufs[0] = NULL;
95
96
7.66k
  transforms[0].r.w = (height + 1) / 2;
97
7.66k
  transforms[0].r.h = (width + 1) / 2;
98
7.66k
  transforms[0].op = TJXOP_TRANSPOSE;
99
7.66k
  transforms[0].options = TJXOPT_GRAY | TJXOPT_CROP | TJXOPT_COPYNONE |
100
7.66k
                          TJXOPT_OPTIMIZE;
101
7.66k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
102
7.66k
  if (dstSizes[0] == 0 ||
103
7.66k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
104
0
    goto bailout;
105
106
7.66k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
107
7.66k
                   transforms) == 0) {
108
76
    size_t sum = 0;
109
110
217k
    for (i = 0; i < dstSizes[0]; i++)
111
217k
      sum += dstBufs[0][i];
112
113
76
    if (sum > 255 * maxBufSize)
114
0
      goto bailout;
115
76
  }
116
117
7.66k
  free(dstBufs[0]);
118
7.66k
  dstBufs[0] = NULL;
119
120
7.66k
  transforms[0].op = TJXOP_ROT90;
121
7.66k
  transforms[0].options = TJXOPT_TRIM | TJXOPT_ARITHMETIC;
122
7.66k
  dstSizes[0] = maxBufSize = tj3TransformBufSize(handle, &transforms[0]);
123
7.66k
  if (dstSizes[0] == 0 ||
124
7.66k
      (dstBufs[0] = (unsigned char *)tj3Alloc(dstSizes[0])) == NULL)
125
0
    goto bailout;
126
127
7.66k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
128
7.66k
                   transforms) == 0) {
129
143
    size_t sum = 0;
130
131
1.73M
    for (i = 0; i < dstSizes[0]; i++)
132
1.73M
      sum += dstBufs[0][i];
133
134
143
    if (sum > 255 * maxBufSize)
135
0
      goto bailout;
136
143
  }
137
138
7.66k
  free(dstBufs[0]);
139
7.66k
  dstBufs[0] = NULL;
140
141
7.66k
  transforms[0].op = TJXOP_NONE;
142
7.66k
  transforms[0].options = TJXOPT_PROGRESSIVE;
143
7.66k
  dstSizes[0] = 0;
144
145
7.66k
  tj3Set(handle, TJPARAM_NOREALLOC, 0);
146
7.66k
  if (tj3Transform(handle, data, size, 1, dstBufs, dstSizes,
147
7.66k
                   transforms) == 0) {
148
128
    size_t sum = 0;
149
150
938k
    for (i = 0; i < dstSizes[0]; i++)
151
938k
      sum += dstBufs[0][i];
152
153
128
    if (sum > 255 * maxBufSize)
154
0
      goto bailout;
155
128
  }
156
157
10.5k
bailout:
158
10.5k
  free(dstBufs[0]);
159
10.5k
  tj3Destroy(handle);
160
10.5k
  return 0;
161
7.66k
}