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