Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/fuzz/compress12_lossless.cc
Line
Count
Source
1
/*
2
 * Copyright (C)2021-2025 D. R. Commander.  All Rights Reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * - Redistributions of source code must retain the above copyright notice,
8
 *   this list of conditions and the following disclaimer.
9
 * - Redistributions in binary form must reproduce the above copyright notice,
10
 *   this list of conditions and the following disclaimer in the documentation
11
 *   and/or other materials provided with the distribution.
12
 * - Neither the name of the libjpeg-turbo Project nor the names of its
13
 *   contributors may be used to endorse or promote products derived from this
14
 *   software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
#include "../src/turbojpeg.h"
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <stdint.h>
33
#include <string.h>
34
#include <unistd.h>
35
36
37
135k
#define NUMTESTS  7
38
39
40
struct test {
41
  enum TJPF pf;
42
  int precision, psv, pt;
43
};
44
45
46
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47
16.9k
{
48
16.9k
  tjhandle handle = NULL;
49
16.9k
  short *srcBuf = NULL;
50
16.9k
  unsigned char *dstBuf = NULL;
51
16.9k
  int width = 0, height = 0, fd = -1, ti;
52
16.9k
  char filename[FILENAME_MAX] = { 0 };
53
16.9k
  struct test tests[NUMTESTS] = {
54
16.9k
    { TJPF_RGB, 12, 1, 0 },
55
16.9k
    { TJPF_BGR, 11, 2, 2 },
56
16.9k
    { TJPF_RGBX, 10, 3, 4 },
57
16.9k
    { TJPF_BGRA, 9, 4, 7 },
58
16.9k
    { TJPF_XRGB, 12, 5, 5 },
59
16.9k
    { TJPF_GRAY, 12, 6, 3 },
60
16.9k
    { TJPF_CMYK, 12, 7, 0 }
61
16.9k
  };
62
63
16.9k
  snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_fuzz.XXXXXX");
64
16.9k
  if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0)
65
0
    goto bailout;
66
67
16.9k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
68
0
    goto bailout;
69
70
135k
  for (ti = 0; ti < NUMTESTS; ti++) {
71
118k
    int pf = tests[ti].pf;
72
118k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
73
74
    /* Test non-default compression options on specific iterations. */
75
118k
    tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0);
76
118k
    tj3Set(handle, TJPARAM_NOREALLOC, ti != 2);
77
118k
    tj3Set(handle, TJPARAM_PRECISION, tests[ti].precision);
78
118k
    tj3Set(handle, TJPARAM_RESTARTROWS, ti == 0 || ti == 6 ? 1 : 0);
79
80
118k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
81
    /* tj3LoadImage12() will refuse to load images larger than 1 Megapixel, so
82
       we don't need to check the width and height here. */
83
118k
    if ((srcBuf = tj3LoadImage12(handle, filename, &width, 1, &height,
84
118k
                                 &pf)) == NULL)
85
81.4k
      continue;
86
87
37.0k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444);
88
37.0k
    if (tj3Get(handle, TJPARAM_NOREALLOC)) {
89
31.6k
      if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
90
0
        goto bailout;
91
31.6k
    } else
92
5.41k
      dstBuf = NULL;
93
94
37.0k
    tj3Set(handle, TJPARAM_LOSSLESS, 1);
95
37.0k
    tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv);
96
37.0k
    tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt);
97
37.0k
    if (tj3Compress12(handle, srcBuf, width, 0, height, pf, &dstBuf,
98
37.0k
                      &dstSize) == 0) {
99
      /* Touch all of the output pixels in order to catch uninitialized reads
100
         when using MemorySanitizer. */
101
980M
      for (i = 0; i < dstSize; i++)
102
980M
        sum += dstBuf[i];
103
36.4k
    }
104
105
37.0k
    free(dstBuf);
106
37.0k
    dstBuf = NULL;
107
37.0k
    tj3Free(srcBuf);
108
37.0k
    srcBuf = NULL;
109
110
    /* Prevent the code above from being optimized out.  This test should never
111
       be true, but the compiler doesn't know that. */
112
37.0k
    if (sum > 255 * maxBufSize)
113
0
      goto bailout;
114
37.0k
  }
115
116
16.9k
bailout:
117
16.9k
  free(dstBuf);
118
16.9k
  tj3Free(srcBuf);
119
16.9k
  if (fd >= 0) {
120
16.9k
    close(fd);
121
16.9k
    if (strlen(filename) > 0) unlink(filename);
122
16.9k
  }
123
16.9k
  tj3Destroy(handle);
124
16.9k
  return 0;
125
16.9k
}