Coverage Report

Created: 2025-07-01 06:26

/src/libjpeg-turbo.main/fuzz/decompress_yuv.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
5.69k
#define NUMPF  3
35
36
37
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
38
6.23k
{
39
6.23k
  tjhandle handle = NULL;
40
6.23k
  unsigned char *dstBuf = NULL, *yuvBuf = NULL;
41
6.23k
  int width = 0, height = 0, jpegSubsamp, 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.23k
  enum TJPF pixelFormats[NUMPF] =
46
6.23k
    { TJPF_BGR, TJPF_XRGB, TJPF_GRAY };
47
48
6.23k
  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.23k
  tj3DecompressHeader(handle, data, size);
55
6.23k
  width = tj3Get(handle, TJPARAM_JPEGWIDTH);
56
6.23k
  height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
57
6.23k
  jpegSubsamp = tj3Get(handle, TJPARAM_SUBSAMP);
58
59
  /* Ignore 0-pixel images and images larger than 1 Megapixel.  Casting width
60
     to (uint64_t) prevents integer overflow if width * height > INT_MAX. */
61
6.23k
  if (width < 1 || height < 1 || (uint64_t)width * height > 1048576)
62
1.55k
    goto bailout;
63
64
4.68k
  tj3Set(handle, TJPARAM_SCANLIMIT, 500);
65
66
5.69k
  for (pfi = 0; pfi < NUMPF; pfi++) {
67
5.36k
    int w = width, h = height;
68
5.36k
    int pf = pixelFormats[pfi], i, sum = 0;
69
70
    /* Test non-default decompression options on the first iteration. */
71
5.36k
    if (!tj3Get(handle, TJPARAM_LOSSLESS)) {
72
4.02k
      tj3Set(handle, TJPARAM_BOTTOMUP, pfi == 0);
73
4.02k
      tj3Set(handle, TJPARAM_FASTUPSAMPLE, pfi == 0);
74
4.02k
      tj3Set(handle, TJPARAM_FASTDCT, pfi == 0);
75
76
      /* Test IDCT scaling on the second iteration. */
77
4.02k
      if (pfi == 1) {
78
347
        tjscalingfactor sf = { 3, 4 };
79
347
        tj3SetScalingFactor(handle, sf);
80
347
        w = TJSCALED(width, sf);
81
347
        h = TJSCALED(height, sf);
82
347
      } else
83
3.67k
        tj3SetScalingFactor(handle, TJUNSCALED);
84
4.02k
    }
85
86
5.36k
    if ((dstBuf = (unsigned char *)tj3Alloc(w * h * tjPixelSize[pf])) == NULL)
87
0
      goto bailout;
88
5.36k
    if ((yuvBuf =
89
5.36k
         (unsigned char *)tj3Alloc(tj3YUVBufSize(w, 1, h,
90
5.36k
                                                 jpegSubsamp))) == NULL)
91
0
      goto bailout;
92
93
5.36k
    if (tj3DecompressToYUV8(handle, data, size, yuvBuf, 1) == 0 &&
94
5.36k
        tj3DecodeYUV8(handle, yuvBuf, 1, dstBuf, w, 0, h, pf) == 0) {
95
      /* Touch all of the output pixels in order to catch uninitialized reads
96
         when using MemorySanitizer. */
97
648M
      for (i = 0; i < w * h * tjPixelSize[pf]; i++)
98
648M
        sum += dstBuf[i];
99
1.01k
    } else
100
4.34k
      goto bailout;
101
102
1.01k
    free(dstBuf);
103
1.01k
    dstBuf = NULL;
104
1.01k
    free(yuvBuf);
105
1.01k
    yuvBuf = NULL;
106
107
    /* Prevent the code above from being optimized out.  This test should never
108
       be true, but the compiler doesn't know that. */
109
1.01k
    if (sum > 255 * 1048576 * tjPixelSize[pf])
110
0
      goto bailout;
111
1.01k
  }
112
113
6.23k
bailout:
114
6.23k
  free(dstBuf);
115
6.23k
  free(yuvBuf);
116
6.23k
  tj3Destroy(handle);
117
6.23k
  return 0;
118
4.68k
}