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_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
152k
#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
19.0k
{
54
19.0k
  tjhandle handle = NULL;
55
19.0k
  unsigned char *imgBuf = NULL, *srcBuf, *dstBuf = NULL;
56
19.0k
  int width = 0, height = 0, ti;
57
19.0k
  FILE *file = NULL;
58
19.0k
  struct test tests[NUMTESTS] = {
59
    /*
60
      BU Pixel      Data PSV Pt No    Rst
61
         Format     Prec        Realc Rows */
62
19.0k
    { 0, TJPF_RGB,  8,   1,  0, 1,    1    },
63
19.0k
    { 0, TJPF_BGR,  7,   2,  5, 1,    0    },
64
19.0k
    { 0, TJPF_RGBX, 6,   3,  4, 0,    0    },
65
19.0k
    { 0, TJPF_BGRA, 5,   4,  1, 1,    0    },
66
19.0k
    { 1, TJPF_XRGB, 4,   5,  3, 1,    0    },
67
19.0k
    { 0, TJPF_GRAY, 3,   6,  2, 1,    0    },
68
19.0k
    { 0, TJPF_CMYK, 2,   7,  0, 1,    1    }
69
19.0k
  };
70
71
19.0k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
72
0
    goto bailout;
73
74
19.0k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
75
0
    goto bailout;
76
77
152k
  for (ti = 0; ti < NUMTESTS; ti++) {
78
133k
    int pf = tests[ti].pf;
79
133k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
80
81
    /* Test non-default compression options on specific iterations. */
82
133k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
83
133k
    tj3Set(handle, TJPARAM_NOREALLOC, tests[ti].noRealloc);
84
133k
    tj3Set(handle, TJPARAM_PRECISION, tests[ti].precision);
85
133k
    tj3Set(handle, TJPARAM_RESTARTROWS, tests[ti].restartRows);
86
87
133k
    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
133k
    fseek(file, 0, SEEK_SET);
91
133k
    if ((imgBuf = _tj3LoadImageFromFileHandle8(handle, file, &width, 1,
92
133k
                                               &height, &pf)) == NULL) {
93
82.8k
      if (size < 2)
94
924
        continue;
95
96
      /* Derive image dimensions from input data.  Use first 2 bytes to
97
         influence width/height. */
98
81.8k
      width = (data[0] % 64) + 8;   /* 8-71 */
99
81.8k
      height = (data[1] % 64) + 8;  /* 8-71 */
100
101
81.8k
      size_t required_size = 2 + (size_t)width * height *
102
81.8k
                             tjPixelSize[tests[ti].pf];
103
81.8k
      if (size < required_size) {
104
        /* Not enough data - try smaller dimensions */
105
77.7k
        width = 8;
106
77.7k
        height = 8;
107
77.7k
        required_size = 2 + (size_t)width * height *
108
77.7k
                        tjPixelSize[tests[ti].pf];
109
77.7k
        if (size < required_size)
110
70.2k
          continue;
111
77.7k
      }
112
113
      /* Skip header bytes. */
114
11.6k
      srcBuf = (unsigned char *)data + 2;
115
11.6k
    } else
116
50.4k
      srcBuf = imgBuf;
117
118
62.0k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
119
62.0k
    if (tj3Get(handle, TJPARAM_NOREALLOC)) {
120
34.4k
      if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
121
0
        goto bailout;
122
34.4k
    } else
123
27.6k
      dstBuf = NULL;
124
125
62.0k
    if (size >= 34)
126
48.3k
      tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
127
128
62.0k
    tj3Set(handle, TJPARAM_LOSSLESS, 1);
129
62.0k
    tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
130
62.0k
    tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
131
62.0k
    if (tj3Compress8(handle, srcBuf, width, 0, height, pf, &dstBuf,
132
62.0k
                     &dstSize) == 0) {
133
      /* Touch all of the output data in order to catch uninitialized reads
134
         when using MemorySanitizer. */
135
1.70G
      for (i = 0; i < dstSize; i++)
136
1.70G
        sum += dstBuf[i];
137
61.5k
    }
138
139
62.0k
    tj3Free(dstBuf);
140
62.0k
    dstBuf = NULL;
141
62.0k
    tj3Free(imgBuf);
142
62.0k
    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
62.0k
    if (sum > 255 * maxBufSize)
147
0
      goto bailout;
148
62.0k
  }
149
150
19.0k
bailout:
151
19.0k
  tj3Free(dstBuf);
152
19.0k
  tj3Free(imgBuf);
153
19.0k
  if (file) fclose(file);
154
19.0k
  tj3Destroy(handle);
155
19.0k
  return 0;
156
19.0k
}