Coverage Report

Created: 2026-01-25 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/fuzz/compress16_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 short *
38
_tj3LoadImageFromFileHandle16(tjhandle handle, FILE *file, int *width,
39
                              int align, int *height, int *pixelFormat);
40
41
42
14.9k
#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
1.86k
{
54
1.86k
  tjhandle handle = NULL;
55
1.86k
  unsigned short *imgBuf = NULL, *srcBuf;
56
1.86k
  unsigned char *dstBuf = NULL;
57
1.86k
  int width = 0, height = 0, ti;
58
1.86k
  FILE *file = NULL;
59
1.86k
  struct test tests[NUMTESTS] = {
60
    /*
61
      BU Pixel      Data PSV Pt No    Rst
62
         Format     Prec        Realc Rows */
63
1.86k
    { 1, TJPF_RGB,  16,  1,  0, 1,    1    },
64
1.86k
    { 0, TJPF_BGR,  15,  2,  2, 1,    0    },
65
1.86k
    { 0, TJPF_RGBX, 14,  3,  4, 0,    0    },
66
1.86k
    { 0, TJPF_BGRA, 13,  4,  7, 1,    0    },
67
1.86k
    { 0, TJPF_XRGB, 16,  5,  5, 1,    0    },
68
1.86k
    { 0, TJPF_GRAY, 16,  6,  3, 1,    0    },
69
1.86k
    { 0, TJPF_CMYK, 16,  7,  0, 1,    1    }
70
1.86k
  };
71
72
1.86k
  if ((file = fmemopen((void *)data, size, "r")) == NULL)
73
0
    goto bailout;
74
75
1.86k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
76
0
    goto bailout;
77
78
14.9k
  for (ti = 0; ti < NUMTESTS; ti++) {
79
13.0k
    int pf = tests[ti].pf;
80
13.0k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
81
82
    /* Test non-default compression options on specific iterations. */
83
13.0k
    tj3Set(handle, TJPARAM_BOTTOMUP, tests[ti].bottomUp);
84
13.0k
    tj3Set(handle, TJPARAM_NOREALLOC, tests[ti].noRealloc);
85
13.0k
    tj3Set(handle, TJPARAM_PRECISION, tests[ti].precision);
86
13.0k
    tj3Set(handle, TJPARAM_RESTARTROWS, tests[ti].restartRows);
87
88
13.0k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
89
    /* tj3LoadImage16() will refuse to load images larger than 1 Megapixel, so
90
       we don't need to check the width and height here. */
91
13.0k
    fseek(file, 0, SEEK_SET);
92
13.0k
    if ((imgBuf = _tj3LoadImageFromFileHandle16(handle, file, &width, 1,
93
13.0k
                                                &height, &pf)) == NULL) {
94
8.58k
      if (size < 2)
95
105
        continue;
96
97
      /* Derive image dimensions from input data.  Use first 2 bytes to
98
         influence width/height. */
99
8.47k
      width = (data[0] % 64) + 8;   /* 8-71 */
100
8.47k
      height = (data[1] % 64) + 8;  /* 8-71 */
101
102
8.47k
      size_t required_size = 2 + (size_t)width * height *
103
8.47k
                             tjPixelSize[tests[ti].pf] * 2;
104
8.47k
      if (size < required_size) {
105
        /* Not enough data - try smaller dimensions */
106
8.19k
        width = 8;
107
8.19k
        height = 8;
108
8.19k
        required_size = 2 + (size_t)width * height *
109
8.19k
                        tjPixelSize[tests[ti].pf] * 2;
110
8.19k
        if (size < required_size)
111
7.50k
          continue;
112
8.19k
      }
113
114
      /* Skip header bytes. */
115
967
      srcBuf = (unsigned short *)(data + 2);
116
967
    } else
117
4.48k
      srcBuf = imgBuf;
118
119
5.45k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
120
5.45k
    if (tj3Get(handle, TJPARAM_NOREALLOC)) {
121
4.69k
      if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
122
0
        goto bailout;
123
4.69k
    } else
124
761
      dstBuf = NULL;
125
126
5.45k
    if (size >= 34)
127
4.10k
      tj3SetICCProfile(handle, (unsigned char *)&data[2], 32);
128
129
5.45k
    tj3Set(handle, TJPARAM_LOSSLESS, 1);
130
5.45k
    tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
131
5.45k
    tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
132
5.45k
    if (tj3Compress16(handle, srcBuf, width, 0, height, pf, &dstBuf,
133
5.45k
                      &dstSize) == 0) {
134
      /* Touch all of the output data in order to catch uninitialized reads
135
         when using MemorySanitizer. */
136
160M
      for (i = 0; i < dstSize; i++)
137
160M
        sum += dstBuf[i];
138
5.36k
    }
139
140
5.45k
    tj3Free(dstBuf);
141
5.45k
    dstBuf = NULL;
142
5.45k
    tj3Free(imgBuf);
143
5.45k
    imgBuf = NULL;
144
145
    /* Prevent the sum above from being optimized out.  This test should never
146
       be true, but the compiler doesn't know that. */
147
5.45k
    if (sum > 255 * maxBufSize)
148
0
      goto bailout;
149
5.45k
  }
150
151
1.86k
bailout:
152
1.86k
  tj3Free(dstBuf);
153
1.86k
  tj3Free(imgBuf);
154
1.86k
  if (file) fclose(file);
155
1.86k
  tj3Destroy(handle);
156
1.86k
  return 0;
157
1.86k
}