Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/fuzz/compress12.cc
Line
Count
Source
1
/*
2
 * Copyright (C)2021, 2023-2026 D. R. Commander.  All Rights Reserved.
3
 * Copyright (C)2025 Leslie P. Polzer.  All Rights Reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * - Redistributions of source code must retain the above copyright notice,
9
 *   this list of conditions and the following disclaimer.
10
 * - Redistributions in binary form must reproduce the above copyright notice,
11
 *   this list of conditions and the following disclaimer in the documentation
12
 *   and/or other materials provided with the distribution.
13
 * - Neither the name of the libjpeg-turbo Project nor the names of its
14
 *   contributors may be used to endorse or promote products derived from this
15
 *   software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#include "../src/turbojpeg.h"
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <stdint.h>
34
#include <string.h>
35
#include <unistd.h>
36
37
extern "C" short *
38
_tj3LoadImageFromFileHandle12(tjhandle handle, FILE *file, int *width,
39
                              int align, int *height, int *pixelFormat);
40
41
42
21.5k
#define NUMTESTS  7
43
44
45
struct test {
46
  int bottomUp;
47
  enum TJPF pf;
48
  int colorspace;
49
  enum TJSAMP subsamp;
50
  int fastDCT, quality, progressive, arithmetic, noRealloc, restartRows;
51
};
52
53
54
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
55
2.69k
{
56
2.69k
  tjhandle handle = NULL;
57
2.69k
  short *imgBuf = NULL, *srcBuf;
58
2.69k
  unsigned char *dstBuf = NULL;
59
2.69k
  int width = 0, height = 0, ti;
60
2.69k
  FILE *file = NULL;
61
2.69k
  struct test tests[NUMTESTS] = {
62
    /*
63
      BU Pixel      JPEG        Subsampling  Fst Qual Prg Ari No    Rst
64
         Format     Colorspace  Level        DCT              Realc Rows */
65
2.69k
    { 0, TJPF_RGB,  TJCS_YCbCr, TJSAMP_444,  1,  100, 0,  0,  1,    0    },
66
2.69k
    { 0, TJPF_BGR,  TJCS_YCbCr, TJSAMP_422,  0,  90,  0,  0,  0,    0    },
67
2.69k
    { 0, TJPF_RGBX, TJCS_RGB,   TJSAMP_420,  0,  75,  0,  1,  0,    1    },
68
2.69k
    { 0, TJPF_BGRA, TJCS_YCbCr, TJSAMP_411,  0,  50,  0,  0,  0,    0    },
69
2.69k
    { 0, TJPF_XRGB, TJCS_GRAY,  TJSAMP_GRAY, 0,  25,  0,  0,  0,    0    },
70
2.69k
    { 0, TJPF_GRAY, TJCS_GRAY,  TJSAMP_GRAY, 0,  10,  1,  0,  0,    0    },
71
2.69k
    { 1, TJPF_CMYK, TJCS_YCCK,  TJSAMP_440,  0,  1,   1,  1,  0,    1    }
72
2.69k
  };
73
74
2.69k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
75
0
    goto bailout;
76
77
2.69k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
78
0
    goto bailout;
79
80
21.5k
  for (ti = 0; ti < NUMTESTS; ti++) {
81
18.8k
    int pf = tests[ti].pf;
82
18.8k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
83
84
    /* Test non-default compression options on specific iterations. */
85
18.8k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
86
18.8k
    tj3Set(handle, TJPARAM_COLORSPACE, tests[ti].colorspace);
87
18.8k
    tj3Set(handle, TJPARAM_FASTDCT, tests[ti].fastDCT);
88
18.8k
    tj3Set(handle, TJPARAM_PROGRESSIVE, tests[ti].progressive);
89
18.8k
    tj3Set(handle, TJPARAM_ARITHMETIC, tests[ti].arithmetic);
90
18.8k
    tj3Set(handle, TJPARAM_NOREALLOC, tests[ti].noRealloc);
91
18.8k
    tj3Set(handle, TJPARAM_RESTARTROWS, tests[ti].restartRows);
92
93
18.8k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
94
    /* tj3LoadImage12() will refuse to load images larger than 1 Megapixel, so
95
       we don't need to check the width and height here. */
96
18.8k
    fseek(file, 0, SEEK_SET);
97
18.8k
    if ((imgBuf = _tj3LoadImageFromFileHandle12(handle, file, &width, 1,
98
18.8k
                                                &height, &pf)) == NULL) {
99
9.06k
      if (size < 2)
100
112
        continue;
101
102
      /* Derive image dimensions from input data.  Use first 2 bytes to
103
         influence width/height. */
104
8.95k
      width = (data[0] % 64) + 8;   /* 8-71 */
105
8.95k
      height = (data[1] % 64) + 8;  /* 8-71 */
106
107
8.95k
      size_t required_size = 2 + (size_t)width * height *
108
8.95k
                             tjPixelSize[tests[ti].pf] * 2;
109
8.95k
      if (size < required_size) {
110
        /* Not enough data - try smaller dimensions */
111
8.25k
        width = 8;
112
8.25k
        height = 8;
113
8.25k
        required_size = 2 + (size_t)width * height *
114
8.25k
                        tjPixelSize[tests[ti].pf] * 2;
115
8.25k
        if (size < required_size)
116
7.26k
          continue;
117
8.25k
      }
118
119
      /* Skip header bytes. */
120
1.69k
      srcBuf = (short *)(data + 2);
121
1.69k
    } else
122
9.79k
      srcBuf = imgBuf;
123
124
11.4k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, tests[ti].subsamp);
125
11.4k
    if (tj3Get(handle, TJPARAM_NOREALLOC)) {
126
1.65k
      if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
127
0
        goto bailout;
128
1.65k
    } else
129
9.83k
      dstBuf = NULL;
130
131
11.4k
    if (size >= 34)
132
9.07k
      tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
133
134
11.4k
    tj3Set(handle, TJPARAM_SUBSAMP, tests[ti].subsamp);
135
11.4k
    tj3Set(handle, TJPARAM_QUALITY, tests[ti].quality);
136
11.4k
    if (tj3Compress12(handle, srcBuf, width, 0, height, pf, &dstBuf,
137
11.4k
                      &dstSize) == 0) {
138
      /* Touch all of the output data in order to catch uninitialized reads
139
         when using MemorySanitizer. */
140
396M
      for (i = 0; i < dstSize; i++)
141
396M
        sum += dstBuf[i];
142
11.4k
    }
143
144
11.4k
    tj3Free(dstBuf);
145
11.4k
    dstBuf = NULL;
146
11.4k
    tj3Free(imgBuf);
147
11.4k
    imgBuf = NULL;
148
149
    /* Prevent the sum above from being optimized out.  This test should never
150
       be true, but the compiler doesn't know that. */
151
11.4k
    if (sum > 255 * maxBufSize)
152
0
      goto bailout;
153
11.4k
  }
154
155
2.69k
bailout:
156
2.69k
  tj3Free(dstBuf);
157
2.69k
  tj3Free(imgBuf);
158
2.69k
  if (file) fclose(file);
159
2.69k
  tj3Destroy(handle);
160
2.69k
  return 0;
161
2.69k
}