Coverage Report

Created: 2026-04-12 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/fuzz/decompress.cc
Line
Count
Source
1
/*
2
 * Copyright (C) 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
70.1k
#define NUMPF  5
36
37
38
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39
14.1k
{
40
14.1k
  tjhandle handle = NULL;
41
14.1k
  void *dstBuf = NULL;
42
14.1k
  int width = 0, height = 0, precision, sampleSize, pfi;
43
  /* TJPF_RGB-TJPF_BGR share the same code paths, as do TJPF_RGBX-TJPF_XRGB and
44
     TJPF_RGBA-TJPF_ARGB.  Thus, the pixel formats below should be the minimum
45
     necessary to achieve full coverage. */
46
14.1k
  enum TJPF pixelFormats[NUMPF] =
47
14.1k
    { TJPF_RGB, TJPF_BGRX, TJPF_ABGR, TJPF_GRAY, TJPF_CMYK };
48
49
14.1k
  if ((handle = tj3Init(TJINIT_DECOMPRESS)) == NULL)
50
0
    goto bailout;
51
52
  /* We ignore the return value of tj3DecompressHeader(), because malformed
53
     JPEG images that might expose issues in libjpeg-turbo might also have
54
     header errors that cause tj3DecompressHeader() to fail. */
55
14.1k
  tj3DecompressHeader(handle, data, size);
56
14.1k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
57
14.1k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
58
14.1k
  precision = tj3Get(handle, TJPARAM_PRECISION);
59
14.1k
  sampleSize = (precision > 8 ? 2 : 1);
60
61
  /* Ignore 0-pixel images and images larger than 1 Megapixel, as Google's
62
     OSS-Fuzz target for libjpeg-turbo did.  Casting width to (uint64_t)
63
     prevents integer overflow if width * height > INT_MAX. */
64
14.1k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
65
2.39k
    goto bailout;
66
67
70.1k
  for (pfi = 0; pfi < NUMPF; pfi++) {
68
58.5k
    int w = width, h = height;
69
58.5k
    int pf = pixelFormats[pfi], i;
70
58.5k
    int64_t sum = 0;
71
72
    /* Test non-default decompression options on the first iteration. */
73
58.5k
    tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
74
58.5k
    tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
75
76
58.5k
    if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
77
43.3k
      tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
78
79
      /* Test IDCT scaling on the second and third iterations. */
80
43.3k
      if (pfi == 1 || pfi == 2) {
81
17.3k
        tjscalingfactor sf = { 1, pfi == 1 ? 2 : 8 };
82
17.3k
        tj3SetScalingFactor(handle, sf);
83
17.3k
        w = TJSCALED(width, sf);
84
17.3k
        h = TJSCALED(height, sf);
85
17.3k
      } else
86
26.0k
        tj3SetScalingFactor(handle, TJUNSCALED);
87
88
      /* Test partial image decompression on the second and fourth iterations,
89
         if the image is large enough. */
90
43.3k
      if ((pfi == 1 || pfi == 3) && w >= 97 && h >= 75) {
91
4.92k
        tjregion cr = { 32, 16, 65, 59 };
92
4.92k
        tj3SetCroppingRegion(handle, cr);
93
4.92k
      } else
94
38.4k
        tj3SetCroppingRegion(handle, TJUNCROPPED);
95
43.3k
      tj3Set(handle, TJPARAM_SCANLIMIT, 100);
96
43.3k
    } else
97
15.1k
      tj3Set(handle, TJPARAM_SCANLIMIT, 50);
98
99
58.5k
    if ((dstBuf = tj3Alloc(w * h * tjPixelSize[pf] * sampleSize)) == NULL)
100
0
      goto bailout;
101
102
58.5k
    if (precision == 8) {
103
28.2k
      if (tj3Decompress8(handle, data, size, (unsigned char *)dstBuf, 0,
104
28.2k
                         pf) == 0) {
105
        /* Touch all of the output pixels in order to catch uninitialized reads
106
           when using MemorySanitizer. */
107
101M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
108
101M
          sum += ((unsigned char *)dstBuf)[i];
109
28.0k
      } else if (!strcmp(tj3GetErrorStr(handle),
110
28.0k
                         "Progressive JPEG image has more than 100 scans"))
111
51
        goto bailout;
112
30.2k
    } else if (precision == 12) {
113
23.2k
      if (tj3Decompress12(handle, data, size, (short *)dstBuf, 0, pf) == 0) {
114
        /* Touch all of the output pixels in order to catch uninitialized reads
115
           when using MemorySanitizer. */
116
123M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
117
123M
          sum += ((short *)dstBuf)[i];
118
22.6k
      } else if (!strcmp(tj3GetErrorStr(handle),
119
22.6k
                         "Progressive JPEG image has more than 100 scans"))
120
29
        goto bailout;
121
23.2k
    } else {
122
7.04k
      if (tj3Decompress16(handle, data, size, (unsigned short *)dstBuf, 0,
123
7.04k
                          pf) == 0) {
124
        /* Touch all of the output pixels in order to catch uninitialized reads
125
           when using MemorySanitizer. */
126
710k
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
127
710k
          sum += ((unsigned short *)dstBuf)[i];
128
6.98k
      } else if (!strcmp(tj3GetErrorStr(handle),
129
6.98k
                         "Progressive JPEG image has more than 100 scans"))
130
0
        goto bailout;
131
7.04k
    }
132
133
58.4k
    tj3Free(dstBuf);
134
58.4k
    dstBuf = NULL;
135
136
    /* Prevent the sum above from being optimized out.  This test should never
137
       be true, but the compiler doesn't know that. */
138
58.4k
    if (sum > ((1LL << precision) - 1LL) * 1048576LL * tjPixelSize[pf])
139
0
      goto bailout;
140
58.4k
  }
141
142
14.1k
bailout:
143
14.1k
  tj3Free(dstBuf);
144
14.1k
  tj3Destroy(handle);
145
14.1k
  return 0;
146
11.7k
}