Coverage Report

Created: 2025-07-11 07:02

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