Coverage Report

Created: 2025-07-01 06:27

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