Coverage Report

Created: 2026-02-26 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/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
35.5k
#define NUMPF  5
36
37
38
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
39
7.25k
{
40
7.25k
  tjhandle handle = NULL;
41
7.25k
  void *dstBuf = NULL;
42
7.25k
  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
7.25k
  enum TJPF pixelFormats[NUMPF] =
47
7.25k
    { TJPF_RGB, TJPF_BGRX, TJPF_ABGR, TJPF_GRAY, TJPF_CMYK };
48
49
7.25k
  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
7.25k
  tj3DecompressHeader(handle, data, size);
56
7.25k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
57
7.25k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
58
7.25k
  precision = tj3Get(handle, TJPARAM_PRECISION);
59
7.25k
  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
7.25k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
65
1.31k
    goto bailout;
66
67
5.94k
  tj3Set(handle, TJPARAM_SCANLIMIT, 100);
68
69
35.5k
  for (pfi = 0; pfi < NUMPF; pfi++) {
70
29.5k
    int w = width, h = height;
71
29.5k
    int pf = pixelFormats[pfi], i;
72
29.5k
    int64_t sum = 0;
73
74
    /* Test non-default decompression options on the first iteration. */
75
29.5k
    tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
76
29.5k
    tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
77
78
29.5k
    if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
79
22.1k
      tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
80
81
      /* Test IDCT scaling on the second and third iterations. */
82
22.1k
      if (pfi == 1 || pfi == 2) {
83
8.85k
        tjscalingfactor sf = { 1, pfi == 1 ? 2 : 8 };
84
8.85k
        tj3SetScalingFactor(handle, sf);
85
8.85k
        w = TJSCALED(width, sf);
86
8.85k
        h = TJSCALED(height, sf);
87
8.85k
      } else
88
13.3k
        tj3SetScalingFactor(handle, TJUNSCALED);
89
90
      /* Test partial image decompression on the second and fourth iterations,
91
         if the image is large enough. */
92
22.1k
      if ((pfi == 1 || pfi == 3) && w >= 97 && h >= 75) {
93
2.32k
        tjregion cr = { 32, 16, 65, 59 };
94
2.32k
        tj3SetCroppingRegion(handle, cr);
95
2.32k
      } else
96
19.8k
        tj3SetCroppingRegion(handle, TJUNCROPPED);
97
22.1k
    }
98
99
29.5k
    if ((dstBuf = tj3Alloc(w * h * tjPixelSize[pf] * sampleSize)) == NULL)
100
0
      goto bailout;
101
102
29.5k
    if (precision == 8) {
103
16.1k
      if (tj3Decompress8(handle, data, size, (unsigned char *)dstBuf, 0,
104
16.1k
                         pf) == 0) {
105
        /* Touch all of the output pixels in order to catch uninitialized reads
106
           when using MemorySanitizer. */
107
97.1M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
108
97.1M
          sum += ((unsigned char *)dstBuf)[i];
109
15.8k
      } else if (!strcmp(tj3GetErrorStr(handle),
110
15.8k
                         "Progressive JPEG image has more than 100 scans"))
111
25
        goto bailout;
112
16.1k
    } else if (precision == 12) {
113
10.1k
      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
128M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
117
128M
          sum += ((short *)dstBuf)[i];
118
9.68k
      } else if (!strcmp(tj3GetErrorStr(handle),
119
9.68k
                         "Progressive JPEG image has more than 100 scans"))
120
6
        goto bailout;
121
10.1k
    } else {
122
3.36k
      if (tj3Decompress16(handle, data, size, (unsigned short *)dstBuf, 0,
123
3.36k
                          pf) == 0) {
124
        /* Touch all of the output pixels in order to catch uninitialized reads
125
           when using MemorySanitizer. */
126
102k
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
127
102k
          sum += ((unsigned short *)dstBuf)[i];
128
3.32k
      } else if (!strcmp(tj3GetErrorStr(handle),
129
3.32k
                         "Progressive JPEG image has more than 100 scans"))
130
4
        goto bailout;
131
3.36k
    }
132
133
29.5k
    tj3Free(dstBuf);
134
29.5k
    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
29.5k
    if (sum > ((1LL << precision) - 1LL) * 1048576LL * tjPixelSize[pf])
139
0
      goto bailout;
140
29.5k
  }
141
142
7.25k
bailout:
143
7.25k
  tj3Free(dstBuf);
144
7.25k
  tj3Destroy(handle);
145
7.25k
  return 0;
146
5.94k
}