Coverage Report

Created: 2026-04-09 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gs_fuzzlib.h
Line
Count
Source
1
/*
2
# Copyright 2019 The Chromium OS Authors.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
################################################################################
17
*/
18
19
#include <base/gserrors.h>
20
#include <psi/iapi.h>
21
22
#include <limits.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <stdint.h>
26
27
static const unsigned char *g_data;
28
static size_t g_size;
29
30
int fuzz_gs_device(
31
  const unsigned char *buf,
32
  size_t size,
33
  int color_scheme,
34
  const char *device_target,
35
  const char *output_file,
36
  int do_interpolation
37
);
38
39
2.49M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.24M
{
43
1.24M
  size_t to_copy = min(len, g_size);
44
1.24M
  to_copy = min(INT_MAX, to_copy);
45
46
1.24M
  memcpy(buf, g_data, to_copy);
47
48
1.24M
  g_data += to_copy;
49
1.24M
  g_size -= to_copy;
50
51
1.24M
  return to_copy;
52
1.24M
}
gs_device_pdfwrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
8.36k
{
43
8.36k
  size_t to_copy = min(len, g_size);
44
8.36k
  to_copy = min(INT_MAX, to_copy);
45
46
8.36k
  memcpy(buf, g_data, to_copy);
47
48
8.36k
  g_data += to_copy;
49
8.36k
  g_size -= to_copy;
50
51
8.36k
  return to_copy;
52
8.36k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
74.0k
{
43
74.0k
  size_t to_copy = min(len, g_size);
44
74.0k
  to_copy = min(INT_MAX, to_copy);
45
46
74.0k
  memcpy(buf, g_data, to_copy);
47
48
74.0k
  g_data += to_copy;
49
74.0k
  g_size -= to_copy;
50
51
74.0k
  return to_copy;
52
74.0k
}
gs_device_ps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
110k
{
43
110k
  size_t to_copy = min(len, g_size);
44
110k
  to_copy = min(INT_MAX, to_copy);
45
46
110k
  memcpy(buf, g_data, to_copy);
47
48
110k
  g_data += to_copy;
49
110k
  g_size -= to_copy;
50
51
110k
  return to_copy;
52
110k
}
gs_device_png16m_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
52.7k
{
43
52.7k
  size_t to_copy = min(len, g_size);
44
52.7k
  to_copy = min(INT_MAX, to_copy);
45
46
52.7k
  memcpy(buf, g_data, to_copy);
47
48
52.7k
  g_data += to_copy;
49
52.7k
  g_size -= to_copy;
50
51
52.7k
  return to_copy;
52
52.7k
}
gs_device_pgmraw_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
55.4k
{
43
55.4k
  size_t to_copy = min(len, g_size);
44
55.4k
  to_copy = min(INT_MAX, to_copy);
45
46
55.4k
  memcpy(buf, g_data, to_copy);
47
48
55.4k
  g_data += to_copy;
49
55.4k
  g_size -= to_copy;
50
51
55.4k
  return to_copy;
52
55.4k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
177k
{
43
177k
  size_t to_copy = min(len, g_size);
44
177k
  to_copy = min(INT_MAX, to_copy);
45
46
177k
  memcpy(buf, g_data, to_copy);
47
48
177k
  g_data += to_copy;
49
177k
  g_size -= to_copy;
50
51
177k
  return to_copy;
52
177k
}
gs_device_ljet4_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
30.7k
{
43
30.7k
  size_t to_copy = min(len, g_size);
44
30.7k
  to_copy = min(INT_MAX, to_copy);
45
46
30.7k
  memcpy(buf, g_data, to_copy);
47
48
30.7k
  g_data += to_copy;
49
30.7k
  g_size -= to_copy;
50
51
30.7k
  return to_copy;
52
30.7k
}
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
15.4k
{
43
15.4k
  size_t to_copy = min(len, g_size);
44
15.4k
  to_copy = min(INT_MAX, to_copy);
45
46
15.4k
  memcpy(buf, g_data, to_copy);
47
48
15.4k
  g_data += to_copy;
49
15.4k
  g_size -= to_copy;
50
51
15.4k
  return to_copy;
52
15.4k
}
gs_device_xpswrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
55.2k
{
43
55.2k
  size_t to_copy = min(len, g_size);
44
55.2k
  to_copy = min(INT_MAX, to_copy);
45
46
55.2k
  memcpy(buf, g_data, to_copy);
47
48
55.2k
  g_data += to_copy;
49
55.2k
  g_size -= to_copy;
50
51
55.2k
  return to_copy;
52
55.2k
}
gstoraster_ps_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
23.8k
{
43
23.8k
  size_t to_copy = min(len, g_size);
44
23.8k
  to_copy = min(INT_MAX, to_copy);
45
46
23.8k
  memcpy(buf, g_data, to_copy);
47
48
23.8k
  g_data += to_copy;
49
23.8k
  g_size -= to_copy;
50
51
23.8k
  return to_copy;
52
23.8k
}
gs_device_faxg3_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
141k
{
43
141k
  size_t to_copy = min(len, g_size);
44
141k
  to_copy = min(INT_MAX, to_copy);
45
46
141k
  memcpy(buf, g_data, to_copy);
47
48
141k
  g_data += to_copy;
49
141k
  g_size -= to_copy;
50
51
141k
  return to_copy;
52
141k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
77.2k
{
43
77.2k
  size_t to_copy = min(len, g_size);
44
77.2k
  to_copy = min(INT_MAX, to_copy);
45
46
77.2k
  memcpy(buf, g_data, to_copy);
47
48
77.2k
  g_data += to_copy;
49
77.2k
  g_size -= to_copy;
50
51
77.2k
  return to_copy;
52
77.2k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
52.8k
{
43
52.8k
  size_t to_copy = min(len, g_size);
44
52.8k
  to_copy = min(INT_MAX, to_copy);
45
46
52.8k
  memcpy(buf, g_data, to_copy);
47
48
52.8k
  g_data += to_copy;
49
52.8k
  g_size -= to_copy;
50
51
52.8k
  return to_copy;
52
52.8k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
127k
{
43
127k
  size_t to_copy = min(len, g_size);
44
127k
  to_copy = min(INT_MAX, to_copy);
45
46
127k
  memcpy(buf, g_data, to_copy);
47
48
127k
  g_data += to_copy;
49
127k
  g_size -= to_copy;
50
51
127k
  return to_copy;
52
127k
}
gs_device_eps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
101k
{
43
101k
  size_t to_copy = min(len, g_size);
44
101k
  to_copy = min(INT_MAX, to_copy);
45
46
101k
  memcpy(buf, g_data, to_copy);
47
48
101k
  g_data += to_copy;
49
101k
  g_size -= to_copy;
50
51
101k
  return to_copy;
52
101k
}
gstoraster_pdf_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
31.5k
{
43
31.5k
  size_t to_copy = min(len, g_size);
44
31.5k
  to_copy = min(INT_MAX, to_copy);
45
46
31.5k
  memcpy(buf, g_data, to_copy);
47
48
31.5k
  g_data += to_copy;
49
31.5k
  g_size -= to_copy;
50
51
31.5k
  return to_copy;
52
31.5k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
79.8k
{
43
79.8k
  size_t to_copy = min(len, g_size);
44
79.8k
  to_copy = min(INT_MAX, to_copy);
45
46
79.8k
  memcpy(buf, g_data, to_copy);
47
48
79.8k
  g_data += to_copy;
49
79.8k
  g_size -= to_copy;
50
51
79.8k
  return to_copy;
52
79.8k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
35.6k
{
43
35.6k
  size_t to_copy = min(len, g_size);
44
35.6k
  to_copy = min(INT_MAX, to_copy);
45
46
35.6k
  memcpy(buf, g_data, to_copy);
47
48
35.6k
  g_data += to_copy;
49
35.6k
  g_size -= to_copy;
50
51
35.6k
  return to_copy;
52
35.6k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
490k
{
56
  /* Just discard everything. */
57
490k
  return len;
58
490k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
1.66k
{
56
  /* Just discard everything. */
57
1.66k
  return len;
58
1.66k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
23.5k
{
56
  /* Just discard everything. */
57
23.5k
  return len;
58
23.5k
}
gs_device_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
42.6k
{
56
  /* Just discard everything. */
57
42.6k
  return len;
58
42.6k
}
gs_device_png16m_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
16.6k
{
56
  /* Just discard everything. */
57
16.6k
  return len;
58
16.6k
}
gs_device_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
7.26k
{
56
  /* Just discard everything. */
57
7.26k
  return len;
58
7.26k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
25.7k
{
56
  /* Just discard everything. */
57
25.7k
  return len;
58
25.7k
}
gs_device_ljet4_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
9.16k
{
56
  /* Just discard everything. */
57
9.16k
  return len;
58
9.16k
}
gstoraster_fuzzer_all_colors.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
27.1k
{
56
  /* Just discard everything. */
57
27.1k
  return len;
58
27.1k
}
gs_device_xpswrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
20.8k
{
56
  /* Just discard everything. */
57
20.8k
  return len;
58
20.8k
}
gstoraster_ps_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
42.2k
{
56
  /* Just discard everything. */
57
42.2k
  return len;
58
42.2k
}
gs_device_faxg3_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
16.9k
{
56
  /* Just discard everything. */
57
16.9k
  return len;
58
16.9k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
3.56k
{
56
  /* Just discard everything. */
57
3.56k
  return len;
58
3.56k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
64.9k
{
56
  /* Just discard everything. */
57
64.9k
  return len;
58
64.9k
}
gs_device_pxlmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
22.4k
{
56
  /* Just discard everything. */
57
22.4k
  return len;
58
22.4k
}
gs_device_eps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
65.5k
{
56
  /* Just discard everything. */
57
65.5k
  return len;
58
65.5k
}
gstoraster_pdf_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
52.8k
{
56
  /* Just discard everything. */
57
52.8k
  return len;
58
52.8k
}
gs_device_bmpmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
20.8k
{
56
  /* Just discard everything. */
57
20.8k
  return len;
58
20.8k
}
gstoraster_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
26.1k
{
56
  /* Just discard everything. */
57
26.1k
  return len;
58
26.1k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
26.3k
{
66
26.3k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
26.3k
}
68
69
int fuzz_gs_device(
70
  const unsigned char *buf,
71
  size_t size,
72
  int color_scheme,
73
  const char *device_target,
74
  const char *output_file,
75
  int do_interpolation
76
)
77
140k
{
78
140k
  int ret;
79
140k
  void *gs = NULL;
80
140k
  char color_space[50];
81
140k
  char gs_device[50];
82
140k
  char gs_o[100];
83
140k
  char opt_interpolation[50];
84
  /*
85
   * We are expecting color_scheme to be in the [0:62] interval.
86
   * This corresponds to the color schemes defined here:
87
   * https://github.com/ArtifexSoftware/ghostpdl/blob/8c97d5adce0040ac38a1fb4d7954499c65f582ff/cups/libs/cups/raster.h#L102
88
   */
89
140k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
140k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
140k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
140k
  if (do_interpolation) {
93
5.29k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
5.29k
  }
95
135k
  else {
96
135k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
135k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
140k
  char *args[] = {
100
140k
    "gs",
101
140k
    "-K1048576",
102
140k
    "-r200x200",
103
140k
    "-sBandListStorage=memory",
104
140k
    "-dMaxBitmap=0",
105
140k
    "-dBufferSpace=450k",
106
140k
    "-dMediaPosition=1",
107
140k
    color_space,
108
140k
    "-dQUIET",
109
140k
    "-dSAFER",
110
140k
    "-dNOPAUSE",
111
140k
    "-dBATCH",
112
140k
    opt_interpolation,
113
140k
    "-dNOMEDIAATTRS",
114
140k
    "-sstdout=%%stderr",
115
140k
    gs_o,
116
140k
    gs_device,
117
140k
    "-_",
118
140k
  };
119
140k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
140k
  g_data = buf;
123
140k
  g_size = size;
124
125
140k
  ret = gsapi_new_instance(&gs, NULL);
126
140k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
140k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
140k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
140k
  if (ret < 0) {
134
0
    fprintf(stderr, "gsapi_set_arg_encoding: error %d\n", ret);
135
0
    gsapi_delete_instance(gs);
136
0
    return ret;
137
0
  }
138
139
140k
  ret = gsapi_init_with_args(gs, argc, args);
140
140k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
36.2k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
140k
  ret = gsapi_exit(gs);
145
140k
  if (ret < 0 && ret != gs_error_Quit) {
146
173
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
173
    gsapi_delete_instance(gs);
148
173
    return ret;
149
173
  }
150
151
140k
  gsapi_delete_instance(gs);
152
153
140k
  return 0;
154
140k
}