Coverage Report

Created: 2026-04-01 07:17

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.95M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.47M
{
43
1.47M
  size_t to_copy = min(len, g_size);
44
1.47M
  to_copy = min(INT_MAX, to_copy);
45
46
1.47M
  memcpy(buf, g_data, to_copy);
47
48
1.47M
  g_data += to_copy;
49
1.47M
  g_size -= to_copy;
50
51
1.47M
  return to_copy;
52
1.47M
}
gs_device_pdfwrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
136k
{
43
136k
  size_t to_copy = min(len, g_size);
44
136k
  to_copy = min(INT_MAX, to_copy);
45
46
136k
  memcpy(buf, g_data, to_copy);
47
48
136k
  g_data += to_copy;
49
136k
  g_size -= to_copy;
50
51
136k
  return to_copy;
52
136k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
83.0k
{
43
83.0k
  size_t to_copy = min(len, g_size);
44
83.0k
  to_copy = min(INT_MAX, to_copy);
45
46
83.0k
  memcpy(buf, g_data, to_copy);
47
48
83.0k
  g_data += to_copy;
49
83.0k
  g_size -= to_copy;
50
51
83.0k
  return to_copy;
52
83.0k
}
gs_device_ps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
104k
{
43
104k
  size_t to_copy = min(len, g_size);
44
104k
  to_copy = min(INT_MAX, to_copy);
45
46
104k
  memcpy(buf, g_data, to_copy);
47
48
104k
  g_data += to_copy;
49
104k
  g_size -= to_copy;
50
51
104k
  return to_copy;
52
104k
}
gs_device_png16m_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
65.6k
{
43
65.6k
  size_t to_copy = min(len, g_size);
44
65.6k
  to_copy = min(INT_MAX, to_copy);
45
46
65.6k
  memcpy(buf, g_data, to_copy);
47
48
65.6k
  g_data += to_copy;
49
65.6k
  g_size -= to_copy;
50
51
65.6k
  return to_copy;
52
65.6k
}
gs_device_pgmraw_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
69.7k
{
43
69.7k
  size_t to_copy = min(len, g_size);
44
69.7k
  to_copy = min(INT_MAX, to_copy);
45
46
69.7k
  memcpy(buf, g_data, to_copy);
47
48
69.7k
  g_data += to_copy;
49
69.7k
  g_size -= to_copy;
50
51
69.7k
  return to_copy;
52
69.7k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
176k
{
43
176k
  size_t to_copy = min(len, g_size);
44
176k
  to_copy = min(INT_MAX, to_copy);
45
46
176k
  memcpy(buf, g_data, to_copy);
47
48
176k
  g_data += to_copy;
49
176k
  g_size -= to_copy;
50
51
176k
  return to_copy;
52
176k
}
Unexecuted instantiation: gs_device_ljet4_fuzzer.cc:gs_stdin(void*, char*, int)
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
23.3k
{
43
23.3k
  size_t to_copy = min(len, g_size);
44
23.3k
  to_copy = min(INT_MAX, to_copy);
45
46
23.3k
  memcpy(buf, g_data, to_copy);
47
48
23.3k
  g_data += to_copy;
49
23.3k
  g_size -= to_copy;
50
51
23.3k
  return to_copy;
52
23.3k
}
gs_device_xpswrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
55.6k
{
43
55.6k
  size_t to_copy = min(len, g_size);
44
55.6k
  to_copy = min(INT_MAX, to_copy);
45
46
55.6k
  memcpy(buf, g_data, to_copy);
47
48
55.6k
  g_data += to_copy;
49
55.6k
  g_size -= to_copy;
50
51
55.6k
  return to_copy;
52
55.6k
}
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
164k
{
43
164k
  size_t to_copy = min(len, g_size);
44
164k
  to_copy = min(INT_MAX, to_copy);
45
46
164k
  memcpy(buf, g_data, to_copy);
47
48
164k
  g_data += to_copy;
49
164k
  g_size -= to_copy;
50
51
164k
  return to_copy;
52
164k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
54.2k
{
43
54.2k
  size_t to_copy = min(len, g_size);
44
54.2k
  to_copy = min(INT_MAX, to_copy);
45
46
54.2k
  memcpy(buf, g_data, to_copy);
47
48
54.2k
  g_data += to_copy;
49
54.2k
  g_size -= to_copy;
50
51
54.2k
  return to_copy;
52
54.2k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
119k
{
43
119k
  size_t to_copy = min(len, g_size);
44
119k
  to_copy = min(INT_MAX, to_copy);
45
46
119k
  memcpy(buf, g_data, to_copy);
47
48
119k
  g_data += to_copy;
49
119k
  g_size -= to_copy;
50
51
119k
  return to_copy;
52
119k
}
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.6k
{
43
31.6k
  size_t to_copy = min(len, g_size);
44
31.6k
  to_copy = min(INT_MAX, to_copy);
45
46
31.6k
  memcpy(buf, g_data, to_copy);
47
48
31.6k
  g_data += to_copy;
49
31.6k
  g_size -= to_copy;
50
51
31.6k
  return to_copy;
52
31.6k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
73.7k
{
43
73.7k
  size_t to_copy = min(len, g_size);
44
73.7k
  to_copy = min(INT_MAX, to_copy);
45
46
73.7k
  memcpy(buf, g_data, to_copy);
47
48
73.7k
  g_data += to_copy;
49
73.7k
  g_size -= to_copy;
50
51
73.7k
  return to_copy;
52
73.7k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
53.1k
{
43
53.1k
  size_t to_copy = min(len, g_size);
44
53.1k
  to_copy = min(INT_MAX, to_copy);
45
46
53.1k
  memcpy(buf, g_data, to_copy);
47
48
53.1k
  g_data += to_copy;
49
53.1k
  g_size -= to_copy;
50
51
53.1k
  return to_copy;
52
53.1k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
617k
{
56
  /* Just discard everything. */
57
617k
  return len;
58
617k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
47.9k
{
56
  /* Just discard everything. */
57
47.9k
  return len;
58
47.9k
}
gs_device_pxlcolor_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_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
43.5k
{
56
  /* Just discard everything. */
57
43.5k
  return len;
58
43.5k
}
gs_device_png16m_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
17.5k
{
56
  /* Just discard everything. */
57
17.5k
  return len;
58
17.5k
}
gs_device_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
18.5k
{
56
  /* Just discard everything. */
57
18.5k
  return len;
58
18.5k
}
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
}
Unexecuted instantiation: gs_device_ljet4_fuzzer.cc:gs_stdnull(void*, char const*, int)
gstoraster_fuzzer_all_colors.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
48.1k
{
56
  /* Just discard everything. */
57
48.1k
  return len;
58
48.1k
}
gs_device_xpswrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
21.1k
{
56
  /* Just discard everything. */
57
21.1k
  return len;
58
21.1k
}
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
17.0k
{
56
  /* Just discard everything. */
57
17.0k
  return len;
58
17.0k
}
gs_device_psdcmyk_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_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
66.4k
{
56
  /* Just discard everything. */
57
66.4k
  return len;
58
66.4k
}
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
21.3k
{
56
  /* Just discard everything. */
57
21.3k
  return len;
58
21.3k
}
gstoraster_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
64.8k
{
56
  /* Just discard everything. */
57
64.8k
  return len;
58
64.8k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
37.3k
{
66
37.3k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
37.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
174k
{
78
174k
  int ret;
79
174k
  void *gs = NULL;
80
174k
  char color_space[50];
81
174k
  char gs_device[50];
82
174k
  char gs_o[100];
83
174k
  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
174k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
174k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
174k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
174k
  if (do_interpolation) {
93
5.55k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
5.55k
  }
95
169k
  else {
96
169k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
169k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
174k
  char *args[] = {
100
174k
    "gs",
101
174k
    "-K1048576",
102
174k
    "-r200x200",
103
174k
    "-sBandListStorage=memory",
104
174k
    "-dMaxBitmap=0",
105
174k
    "-dBufferSpace=450k",
106
174k
    "-dMediaPosition=1",
107
174k
    color_space,
108
174k
    "-dQUIET",
109
174k
    "-dSAFER",
110
174k
    "-dNOPAUSE",
111
174k
    "-dBATCH",
112
174k
    opt_interpolation,
113
174k
    "-dNOMEDIAATTRS",
114
174k
    "-sstdout=%%stderr",
115
174k
    gs_o,
116
174k
    gs_device,
117
174k
    "-_",
118
174k
  };
119
174k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
174k
  g_data = buf;
123
174k
  g_size = size;
124
125
174k
  ret = gsapi_new_instance(&gs, NULL);
126
174k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
174k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
174k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
174k
  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
174k
  ret = gsapi_init_with_args(gs, argc, args);
140
174k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
48.2k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
174k
  ret = gsapi_exit(gs);
145
174k
  if (ret < 0 && ret != gs_error_Quit) {
146
178
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
178
    gsapi_delete_instance(gs);
148
178
    return ret;
149
178
  }
150
151
174k
  gsapi_delete_instance(gs);
152
153
174k
  return 0;
154
174k
}