Coverage Report

Created: 2025-07-14 06:09

/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.28k
#define NUMPF  4
35
36
37
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
38
7.13k
{
39
7.13k
  tjhandle handle = NULL;
40
7.13k
  void *dstBuf = NULL;
41
7.13k
  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
7.13k
  enum TJPF pixelFormats[NUMPF] =
46
7.13k
    { TJPF_RGB, TJPF_BGRX, TJPF_GRAY, TJPF_CMYK };
47
48
7.13k
  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
7.13k
  tj3DecompressHeader(handle, data, size);
55
7.13k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
56
7.13k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
57
7.13k
  precision = tj3Get(handle, TJPARAM_PRECISION);
58
7.13k
  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
7.13k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
64
1.43k
    goto bailout;
65
66
5.69k
  tj3Set(handle, TJPARAM_SCANLIMIT, 500);
67
68
8.28k
  for (pfi = 0; pfi < NUMPF; pfi++) {
69
8.28k
    int w = width, h = height;
70
8.28k
    int pf = pixelFormats[pfi], i;
71
8.28k
    int64_t sum = 0;
72
73
    /* Test non-default decompression options on the first iteration. */
74
8.28k
    tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
75
8.28k
    tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
76
77
8.28k
    if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
78
6.47k
      tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
79
80
      /* Test IDCT scaling on the second iteration. */
81
6.47k
      if (pfi == 1) {
82
784
        tjscalingfactor sf = { 1, 2 };
83
784
        tj3SetScalingFactor(handle, sf);
84
784
        w = TJSCALED(width, sf);
85
784
        h = TJSCALED(height, sf);
86
784
      } else
87
5.69k
        tj3SetScalingFactor(handle, TJUNSCALED);
88
89
      /* Test partial image decompression on the fourth iteration, if the image
90
         is large enough. */
91
6.47k
      if (pfi == 3 && w >= 97 && h >= 75) {
92
124
        tjregion cr = { 32, 16, 65, 59 };
93
124
        tj3SetCroppingRegion(handle, cr);
94
124
      } else
95
6.35k
        tj3SetCroppingRegion(handle, TJUNCROPPED);
96
6.47k
    }
97
98
8.28k
    if ((dstBuf = tj3Alloc(w * h * tjPixelSize[pf] * sampleSize)) == NULL)
99
0
      goto bailout;
100
101
8.28k
    if (precision == 8) {
102
3.54k
      if (tj3Decompress8(handle, data, size, (unsigned char *)dstBuf, 0,
103
3.54k
                         pf) == 0) {
104
        /* Touch all of the output pixels in order to catch uninitialized reads
105
           when using MemorySanitizer. */
106
294M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
107
294M
          sum += ((unsigned char *)dstBuf)[i];
108
581
      } else
109
2.96k
        goto bailout;
110
4.73k
    } else if (precision == 12) {
111
3.93k
      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
567M
        for (i = 0; i < w * h * tjPixelSize[pf]; i++)
115
567M
          sum += ((short *)dstBuf)[i];
116
1.91k
      } else
117
2.02k
        goto bailout;
118
3.93k
    } else {
119
806
      if (tj3Decompress16(handle, data, size, (unsigned short *)dstBuf, 0,
120
806
                          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
708
        goto bailout;
127
806
    }
128
129
2.59k
    free(dstBuf);
130
2.59k
    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.59k
    if (sum > ((1LL << precision) - 1LL) * 1048576LL * tjPixelSize[pf])
135
0
      goto bailout;
136
2.59k
  }
137
138
7.13k
bailout:
139
7.13k
  free(dstBuf);
140
7.13k
  tj3Destroy(handle);
141
7.13k
  return 0;
142
5.69k
}