Coverage Report

Created: 2025-07-01 06:27

/src/libjpeg-turbo.main/fuzz/compress_yuv.cc
Line
Count
Source (jump to first uncovered line)
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 <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
49.2k
#define NUMTESTS  6
38
39
40
struct test {
41
  enum TJPF pf;
42
  enum TJSAMP subsamp;
43
  int quality;
44
};
45
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
7.03k
{
49
7.03k
  tjhandle handle = NULL;
50
7.03k
  unsigned char *srcBuf = NULL, *dstBuf = NULL, *yuvBuf = NULL;
51
7.03k
  int width = 0, height = 0, fd = -1, ti;
52
7.03k
  char filename[FILENAME_MAX] = { 0 };
53
7.03k
  struct test tests[NUMTESTS] = {
54
7.03k
    { TJPF_XBGR, TJSAMP_444, 100 },
55
7.03k
    { TJPF_XRGB, TJSAMP_422, 90 },
56
7.03k
    { TJPF_BGR, TJSAMP_420, 80 },
57
7.03k
    { TJPF_RGB, TJSAMP_411, 70 },
58
7.03k
    { TJPF_BGR, TJSAMP_GRAY, 60 },
59
7.03k
    { TJPF_GRAY, TJSAMP_GRAY, 50 }
60
7.03k
  };
61
62
7.03k
  snprintf(filename, FILENAME_MAX, "/tmp/libjpeg-turbo_compress_yuv_fuzz.XXXXXX");
63
7.03k
  if ((fd = mkstemp(filename)) < 0 || write(fd, data, size) < 0)
64
0
    goto bailout;
65
66
7.03k
  if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL)
67
0
    goto bailout;
68
69
49.2k
  for (ti = 0; ti < NUMTESTS; ti++) {
70
42.1k
    int pf = tests[ti].pf;
71
42.1k
    size_t dstSize = 0, maxBufSize, i, sum = 0;
72
73
    /* Test non-default compression options on specific iterations. */
74
42.1k
    tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0);
75
42.1k
    tj3Set(handle, TJPARAM_FASTDCT, ti == 1);
76
42.1k
    tj3Set(handle, TJPARAM_OPTIMIZE, ti == 4);
77
42.1k
    tj3Set(handle, TJPARAM_PROGRESSIVE, ti == 1 || ti == 3);
78
42.1k
    tj3Set(handle, TJPARAM_ARITHMETIC, ti == 2 || ti == 3);
79
42.1k
    tj3Set(handle, TJPARAM_NOREALLOC, 1);
80
42.1k
    tj3Set(handle, TJPARAM_RESTARTBLOCKS, ti == 3 || ti == 4 ? 4 : 0);
81
82
42.1k
    tj3Set(handle, TJPARAM_MAXPIXELS, 1048576);
83
    /* tj3LoadImage8() will refuse to load images larger than 1 Megapixel, so
84
       we don't need to check the width and height here. */
85
42.1k
    if ((srcBuf = tj3LoadImage8(handle, filename, &width, 1, &height,
86
42.1k
                                &pf)) == NULL)
87
20.0k
      continue;
88
89
22.1k
    dstSize = maxBufSize = tj3JPEGBufSize(width, height, tests[ti].subsamp);
90
22.1k
    if ((dstBuf = (unsigned char *)tj3Alloc(dstSize)) == NULL)
91
0
      goto bailout;
92
22.1k
    if ((yuvBuf =
93
22.1k
         (unsigned char *)malloc(tj3YUVBufSize(width, 1, height,
94
22.1k
                                               tests[ti].subsamp))) == NULL)
95
0
      goto bailout;
96
97
22.1k
    tj3Set(handle, TJPARAM_SUBSAMP, tests[ti].subsamp);
98
22.1k
    tj3Set(handle, TJPARAM_QUALITY, tests[ti].quality);
99
22.1k
    if (tj3EncodeYUV8(handle, srcBuf, width, 0, height, pf, yuvBuf, 1) == 0 &&
100
22.1k
        tj3CompressFromYUV8(handle, yuvBuf, width, 1, height, &dstBuf,
101
22.0k
                            &dstSize) == 0) {
102
      /* Touch all of the output pixels in order to catch uninitialized reads
103
         when using MemorySanitizer. */
104
222M
      for (i = 0; i < dstSize; i++)
105
222M
        sum += dstBuf[i];
106
22.0k
    }
107
108
22.1k
    free(dstBuf);
109
22.1k
    dstBuf = NULL;
110
22.1k
    free(yuvBuf);
111
22.1k
    yuvBuf = NULL;
112
22.1k
    tj3Free(srcBuf);
113
22.1k
    srcBuf = NULL;
114
115
    /* Prevent the code above from being optimized out.  This test should never
116
       be true, but the compiler doesn't know that. */
117
22.1k
    if (sum > 255 * maxBufSize)
118
0
      goto bailout;
119
22.1k
  }
120
121
7.03k
bailout:
122
7.03k
  free(dstBuf);
123
7.03k
  free(yuvBuf);
124
7.03k
  tj3Free(srcBuf);
125
7.03k
  if (fd >= 0) {
126
7.03k
    close(fd);
127
7.03k
    if (strlen(filename) > 0) unlink(filename);
128
7.03k
  }
129
7.03k
  tj3Destroy(handle);
130
7.03k
  return 0;
131
7.03k
}