Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/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
40.8k
#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
5.83k
{
55
5.83k
  tjhandle handle = NULL;
56
5.83k
  unsigned char *imgBuf = NULL, *srcBuf, *dstBuf = NULL, *yuvBuf = NULL;
57
5.83k
  int width = 0, height = 0, ti;
58
5.83k
  FILE *file = NULL;
59
5.83k
  struct test tests[NUMTESTS] = {
60
    /*
61
      BU Pixel      Subsampling  Fst Qual Opt Prg Ari Rst
62
         Format     Level        DCT                  Blks */
63
5.83k
    { 0, TJPF_XBGR, TJSAMP_444,  1,  100, 0,  0,  0,  0    },
64
5.83k
    { 0, TJPF_XRGB, TJSAMP_422,  0,  90,  0,  1,  0,  4    },
65
5.83k
    { 0, TJPF_BGR,  TJSAMP_420,  0,  75,  0,  0,  0,  0    },
66
5.83k
    { 0, TJPF_RGB,  TJSAMP_411,  0,  50,  1,  0,  0,  0    },
67
5.83k
    { 0, TJPF_BGR,  TJSAMP_GRAY, 0,  25,  0,  0,  1,  0    },
68
5.83k
    { 1, TJPF_GRAY, TJSAMP_GRAY, 1,  10,  0,  1,  1,  4    }
69
5.83k
  };
70
71
5.83k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
72
0
    goto bailout;
73
74
5.83k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
75
0
    goto bailout;
76
77
40.8k
  for (ti = 0; ti < NUMTESTS; ti++) {
78
35.0k
    int pf = tests[ti].pf;
79
35.0k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
80
81
    /* Test non-default compression options on specific iterations. */
82
35.0k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
83
35.0k
    tj3Set(handle, TJPARAM_FASTDCT, tests[ti].fastDCT);
84
35.0k
    tj3Set(handle, TJPARAM_OPTIMIZE, tests[ti].optimize);
85
35.0k
    tj3Set(handle, TJPARAM_PROGRESSIVE, tests[ti].progressive);
86
35.0k
    tj3Set(handle, TJPARAM_ARITHMETIC, tests[ti].arithmetic);
87
35.0k
    tj3Set(handle, TJPARAM_NOREALLOC, 1);
88
35.0k
    tj3Set(handle, TJPARAM_RESTARTBLOCKS, tests[ti].restartBlocks);
89
90
35.0k
    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
35.0k
    fseek(file, 0, SEEK_SET);
94
35.0k
    if ((imgBuf = _tj3LoadImageFromFileHandle8(handle, file, &width, 1,
95
35.0k
                                               &height, &pf)) == NULL) {
96
19.5k
      if (size < 2)
97
192
        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
19.4k
      width = ((data[0] % 4) + 1) * 32;   /* 32-128, multiple of 32 */
103
19.4k
      height = ((data[1] % 8) + 1) * 16;  /* 16-128, multiple of 16 */
104
105
19.4k
      size_t required_size = 2 + (size_t)width * height *
106
19.4k
                             tjPixelSize[tests[ti].pf];
107
19.4k
      if (size < required_size) {
108
        /* Not enough data - try smaller dimensions */
109
18.8k
        width = 32;
110
18.8k
        height = 16;
111
18.8k
        required_size = 2 + (size_t)width * height *
112
18.8k
                        tjPixelSize[tests[ti].pf];
113
18.8k
        if (size < required_size)
114
18.3k
          continue;
115
18.8k
      }
116
117
      /* Skip header bytes. */
118
1.09k
      srcBuf = (unsigned char *)data + 2;
119
1.09k
    } else
120
15.4k
      srcBuf = imgBuf;
121
122
16.5k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, tests[ti].subsamp);
123
16.5k
    if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
124
0
      goto bailout;
125
16.5k
    if ((yuvBuf =
126
16.5k
         (unsigned char *)malloc(tj3YUVBufSize(width, 1, height,
127
16.5k
                                               tests[ti].subsamp))) == NULL)
128
0
      goto bailout;
129
130
16.5k
    tj3Set(handle, TJPARAM_SUBSAMP, tests[ti].subsamp);
131
16.5k
    tj3Set(handle, TJPARAM_QUALITY, tests[ti].quality);
132
16.5k
    if (tj3EncodeYUV8(handle, srcBuf, width, 0, height, pf, yuvBuf, 1) == 0 &&
133
16.4k
        tj3CompressFromYUV8(handle, yuvBuf, width, 1, height, &dstBuf,
134
16.4k
                            &dstSize) == 0) {
135
      /* Touch all of the output data in order to catch uninitialized reads
136
         when using MemorySanitizer. */
137
227M
      for (i = 0; i < dstSize; i++)
138
227M
        sum += dstBuf[i];
139
16.4k
    }
140
141
16.5k
    tj3Free(dstBuf);
142
16.5k
    dstBuf = NULL;
143
16.5k
    free(yuvBuf);
144
16.5k
    yuvBuf = NULL;
145
16.5k
    tj3Free(imgBuf);
146
16.5k
    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
16.5k
    if (sum > 255 * maxBufSize)
151
0
      goto bailout;
152
16.5k
  }
153
154
5.83k
bailout:
155
5.83k
  tj3Free(dstBuf);
156
5.83k
  free(yuvBuf);
157
5.83k
  tj3Free(imgBuf);
158
5.83k
  if (file) fclose(file);
159
5.83k
  tj3Destroy(handle);
160
5.83k
  return 0;
161
5.83k
}