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/compress_lossless.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
21.1k
#define NUMTESTS  7
43
44
45
struct test {
46
  int bottomUp;
47
  enum TJPF pf;
48
  int precision, psv, pt, noRealloc, restartRows;
49
};
50
51
52
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53
2.64k
{
54
2.64k
  tjhandle handle = NULL;
55
2.64k
  unsigned char *imgBuf = NULL, *srcBuf, *dstBuf = NULL;
56
2.64k
  int width = 0, height = 0, ti;
57
2.64k
  FILE *file = NULL;
58
2.64k
  struct test tests[NUMTESTS] = {
59
    /*
60
      BU Pixel      Data PSV Pt No    Rst
61
         Format     Prec        Realc Rows */
62
2.64k
    { 0, TJPF_RGB,  8,   1,  0, 1,    1    },
63
2.64k
    { 0, TJPF_BGR,  7,   2,  5, 1,    0    },
64
2.64k
    { 0, TJPF_RGBX, 6,   3,  4, 0,    0    },
65
2.64k
    { 0, TJPF_BGRA, 5,   4,  1, 1,    0    },
66
2.64k
    { 1, TJPF_XRGB, 4,   5,  3, 1,    0    },
67
2.64k
    { 0, TJPF_GRAY, 3,   6,  2, 1,    0    },
68
2.64k
    { 0, TJPF_CMYK, 2,   7,  0, 1,    1    }
69
2.64k
  };
70
71
2.64k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
72
0
    goto bailout;
73
74
2.64k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
75
0
    goto bailout;
76
77
21.1k
  for (ti = 0; ti < NUMTESTS; ti++) {
78
18.5k
    int pf = tests[ti].pf;
79
18.5k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
80
81
    /* Test non-default compression options on specific iterations. */
82
18.5k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
83
18.5k
    tj3Set(handle, TJPARAM_NOREALLOC, tests[ti].noRealloc);
84
18.5k
    tj3Set(handle, TJPARAM_PRECISION, tests[ti].precision);
85
18.5k
    tj3Set(handle, TJPARAM_RESTARTROWS, tests[ti].restartRows);
86
87
18.5k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
88
    /* tj3LoadImage8() will refuse to load images larger than 1 Megapixel, so
89
       we don't need to check the width and height here. */
90
18.5k
    fseek(file, 0, SEEK_SET);
91
18.5k
    if ((imgBuf = _tj3LoadImageFromFileHandle8(handle, file, &width, 1,
92
18.5k
                                               &height, &pf)) == NULL) {
93
14.2k
      if (size < 2)
94
112
        continue;
95
96
      /* Derive image dimensions from input data.  Use first 2 bytes to
97
         influence width/height. */
98
14.1k
      width = (data[0] % 64) + 8;   /* 8-71 */
99
14.1k
      height = (data[1] % 64) + 8;  /* 8-71 */
100
101
14.1k
      size_t required_size = 2 + (size_t)width * height *
102
14.1k
                             tjPixelSize[tests[ti].pf];
103
14.1k
      if (size < required_size) {
104
        /* Not enough data - try smaller dimensions */
105
13.5k
        width = 8;
106
13.5k
        height = 8;
107
13.5k
        required_size = 2 + (size_t)width * height *
108
13.5k
                        tjPixelSize[tests[ti].pf];
109
13.5k
        if (size < required_size)
110
12.0k
          continue;
111
13.5k
      }
112
113
      /* Skip header bytes. */
114
2.09k
      srcBuf = (unsigned char *)data + 2;
115
2.09k
    } else
116
4.29k
      srcBuf = imgBuf;
117
118
6.39k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
119
6.39k
    if (tj3Get(handle, TJPARAM_NOREALLOC)) {
120
5.52k
      if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
121
0
        goto bailout;
122
5.52k
    } else
123
870
      dstBuf = NULL;
124
125
6.39k
    if (size >= 34)
126
4.97k
      tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
127
128
6.39k
    tj3Set(handle, TJPARAM_LOSSLESS, 1);
129
6.39k
    tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
130
6.39k
    tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
131
6.39k
    if (tj3Compress8(handle, srcBuf, width, 0, height, pf, &dstBuf,
132
6.39k
                     &dstSize) == 0) {
133
      /* Touch all of the output data in order to catch uninitialized reads
134
         when using MemorySanitizer. */
135
124M
      for (i = 0; i < dstSize; i++)
136
124M
        sum += dstBuf[i];
137
6.31k
    }
138
139
6.39k
    tj3Free(dstBuf);
140
6.39k
    dstBuf = NULL;
141
6.39k
    tj3Free(imgBuf);
142
6.39k
    imgBuf = NULL;
143
144
    /* Prevent the sum above from being optimized out.  This test should never
145
       be true, but the compiler doesn't know that. */
146
6.39k
    if (sum > 255 * maxBufSize)
147
0
      goto bailout;
148
6.39k
  }
149
150
2.64k
bailout:
151
2.64k
  tj3Free(dstBuf);
152
2.64k
  tj3Free(imgBuf);
153
2.64k
  if (file) fclose(file);
154
2.64k
  tj3Destroy(handle);
155
2.64k
  return 0;
156
2.64k
}