Coverage Report

Created: 2026-02-26 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/fuzz/compress_yuv.cc
Line
Count
Source
1
/*
2
 * Copyright (C) 2021-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" unsigned char *
38
_tj3LoadImageFromFileHandle8(tjhandle handle, FILE *file, int *width,
39
                             int align, int *height, int *pixelFormat);
40
41
42
23.0k
#define NUMTESTS  6
43
44
45
struct test {
46
  int bottomUp;
47
  enum TJPF pf;
48
  enum TJSAMP subsamp;
49
  int fastDCT, quality, optimize, progressive, arithmetic, restartBlocks;
50
};
51
52
53
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
54
3.29k
{
55
3.29k
  tjhandle handle = NULL;
56
3.29k
  unsigned char *imgBuf = NULL, *srcBuf, *dstBuf = NULL, *yuvBuf = NULL;
57
3.29k
  int width = 0, height = 0, ti;
58
3.29k
  FILE *file = NULL;
59
3.29k
  struct test tests[NUMTESTS] = {
60
    /*
61
      BU Pixel      Subsampling  Fst Qual Opt Prg Ari Rst
62
         Format     Level        DCT                  Blks */
63
3.29k
    { 0, TJPF_XBGR, TJSAMP_444,  1,  100, 0,  0,  0,  0    },
64
3.29k
    { 0, TJPF_XRGB, TJSAMP_422,  0,  90,  0,  1,  0,  4    },
65
3.29k
    { 0, TJPF_BGR,  TJSAMP_420,  0,  75,  0,  0,  0,  0    },
66
3.29k
    { 0, TJPF_RGB,  TJSAMP_411,  0,  50,  1,  0,  0,  0    },
67
3.29k
    { 0, TJPF_BGR,  TJSAMP_GRAY, 0,  25,  0,  0,  1,  0    },
68
3.29k
    { 1, TJPF_GRAY, TJSAMP_GRAY, 1,  10,  0,  1,  1,  4    }
69
3.29k
  };
70
71
3.29k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
72
0
    goto bailout;
73
74
3.29k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
75
0
    goto bailout;
76
77
23.0k
  for (ti = 0; ti < NUMTESTS; ti++) {
78
19.7k
    int pf = tests[ti].pf;
79
19.7k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
80
81
    /* Test non-default compression options on specific iterations. */
82
19.7k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
83
19.7k
    tj3Set(handle, TJPARAM_FASTDCT, tests[ti].fastDCT);
84
19.7k
    tj3Set(handle, TJPARAM_OPTIMIZE, tests[ti].optimize);
85
19.7k
    tj3Set(handle, TJPARAM_PROGRESSIVE, tests[ti].progressive);
86
19.7k
    tj3Set(handle, TJPARAM_ARITHMETIC, tests[ti].arithmetic);
87
19.7k
    tj3Set(handle, TJPARAM_NOREALLOC, 1);
88
19.7k
    tj3Set(handle, TJPARAM_RESTARTBLOCKS, tests[ti].restartBlocks);
89
90
19.7k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
91
    /* tj3LoadImage8() will refuse to load images larger than 1 Megapixel, so
92
       we don't need to check the width and height here. */
93
19.7k
    fseek(file, 0, SEEK_SET);
94
19.7k
    if ((imgBuf = _tj3LoadImageFromFileHandle8(handle, file, &width, 1,
95
19.7k
                                               &height, &pf)) == NULL) {
96
10.0k
      if (size < 2)
97
96
        continue;
98
99
      /* Derive image dimensions from input data.  Use first 2 bytes to
100
         influence width/height.  These must be multiples of the maximum iMCU
101
         size for the subsampling levels we plan to test. */
102
9.92k
      width = ((data[0] % 4) + 1) * 32;   /* 32-128, multiple of 32 */
103
9.92k
      height = ((data[1] % 8) + 1) * 16;  /* 16-128, multiple of 16 */
104
105
9.92k
      size_t required_size = 2 + (size_t)width * height *
106
9.92k
                             tjPixelSize[tests[ti].pf];
107
9.92k
      if (size < required_size) {
108
        /* Not enough data - try smaller dimensions */
109
9.67k
        width = 32;
110
9.67k
        height = 16;
111
9.67k
        required_size = 2 + (size_t)width * height *
112
9.67k
                        tjPixelSize[tests[ti].pf];
113
9.67k
        if (size < required_size)
114
9.41k
          continue;
115
9.67k
      }
116
117
      /* Skip header bytes. */
118
508
      srcBuf = (unsigned char *)data + 2;
119
508
    } else
120
9.77k
      srcBuf = imgBuf;
121
122
10.2k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, tests[ti].subsamp);
123
10.2k
    if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
124
0
      goto bailout;
125
10.2k
    if ((yuvBuf =
126
10.2k
         (unsigned char *)malloc(tj3YUVBufSize(width, 1, height,
127
10.2k
                                               tests[ti].subsamp))) == NULL)
128
0
      goto bailout;
129
130
10.2k
    tj3Set(handle, TJPARAM_SUBSAMP, tests[ti].subsamp);
131
10.2k
    tj3Set(handle, TJPARAM_QUALITY, tests[ti].quality);
132
10.2k
    if (tj3EncodeYUV8(handle, srcBuf, width, 0, height, pf, yuvBuf, 1) == 0 &&
133
10.2k
        tj3CompressFromYUV8(handle, yuvBuf, width, 1, height, &dstBuf,
134
10.2k
                            &dstSize) == 0) {
135
      /* Touch all of the output data in order to catch uninitialized reads
136
         when using MemorySanitizer. */
137
115M
      for (i = 0; i < dstSize; i++)
138
115M
        sum += dstBuf[i];
139
10.2k
    }
140
141
10.2k
    tj3Free(dstBuf);
142
10.2k
    dstBuf = NULL;
143
10.2k
    free(yuvBuf);
144
10.2k
    yuvBuf = NULL;
145
10.2k
    tj3Free(imgBuf);
146
10.2k
    imgBuf = NULL;
147
148
    /* Prevent the sum above from being optimized out.  This test should never
149
       be true, but the compiler doesn't know that. */
150
10.2k
    if (sum > 255 * maxBufSize)
151
0
      goto bailout;
152
10.2k
  }
153
154
3.29k
bailout:
155
3.29k
  tj3Free(dstBuf);
156
3.29k
  free(yuvBuf);
157
3.29k
  tj3Free(imgBuf);
158
3.29k
  if (file) fclose(file);
159
3.29k
  tj3Destroy(handle);
160
3.29k
  return 0;
161
3.29k
}