Coverage Report

Created: 2026-02-14 07:09

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
3.33M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.66M
{
43
1.66M
  size_t to_copy = min(len, g_size);
44
1.66M
  to_copy = min(INT_MAX, to_copy);
45
46
1.66M
  memcpy(buf, g_data, to_copy);
47
48
1.66M
  g_data += to_copy;
49
1.66M
  g_size -= to_copy;
50
51
1.66M
  return to_copy;
52
1.66M
}
gs_device_pdfwrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
139k
{
43
139k
  size_t to_copy = min(len, g_size);
44
139k
  to_copy = min(INT_MAX, to_copy);
45
46
139k
  memcpy(buf, g_data, to_copy);
47
48
139k
  g_data += to_copy;
49
139k
  g_size -= to_copy;
50
51
139k
  return to_copy;
52
139k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
86.0k
{
43
86.0k
  size_t to_copy = min(len, g_size);
44
86.0k
  to_copy = min(INT_MAX, to_copy);
45
46
86.0k
  memcpy(buf, g_data, to_copy);
47
48
86.0k
  g_data += to_copy;
49
86.0k
  g_size -= to_copy;
50
51
86.0k
  return to_copy;
52
86.0k
}
gs_device_ps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
115k
{
43
115k
  size_t to_copy = min(len, g_size);
44
115k
  to_copy = min(INT_MAX, to_copy);
45
46
115k
  memcpy(buf, g_data, to_copy);
47
48
115k
  g_data += to_copy;
49
115k
  g_size -= to_copy;
50
51
115k
  return to_copy;
52
115k
}
gs_device_png16m_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
175k
{
43
175k
  size_t to_copy = min(len, g_size);
44
175k
  to_copy = min(INT_MAX, to_copy);
45
46
175k
  memcpy(buf, g_data, to_copy);
47
48
175k
  g_data += to_copy;
49
175k
  g_size -= to_copy;
50
51
175k
  return to_copy;
52
175k
}
gs_device_pgmraw_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
70.9k
{
43
70.9k
  size_t to_copy = min(len, g_size);
44
70.9k
  to_copy = min(INT_MAX, to_copy);
45
46
70.9k
  memcpy(buf, g_data, to_copy);
47
48
70.9k
  g_data += to_copy;
49
70.9k
  g_size -= to_copy;
50
51
70.9k
  return to_copy;
52
70.9k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
170k
{
43
170k
  size_t to_copy = min(len, g_size);
44
170k
  to_copy = min(INT_MAX, to_copy);
45
46
170k
  memcpy(buf, g_data, to_copy);
47
48
170k
  g_data += to_copy;
49
170k
  g_size -= to_copy;
50
51
170k
  return to_copy;
52
170k
}
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
24.7k
{
43
24.7k
  size_t to_copy = min(len, g_size);
44
24.7k
  to_copy = min(INT_MAX, to_copy);
45
46
24.7k
  memcpy(buf, g_data, to_copy);
47
48
24.7k
  g_data += to_copy;
49
24.7k
  g_size -= to_copy;
50
51
24.7k
  return to_copy;
52
24.7k
}
gs_device_xpswrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
91.5k
{
43
91.5k
  size_t to_copy = min(len, g_size);
44
91.5k
  to_copy = min(INT_MAX, to_copy);
45
46
91.5k
  memcpy(buf, g_data, to_copy);
47
48
91.5k
  g_data += to_copy;
49
91.5k
  g_size -= to_copy;
50
51
91.5k
  return to_copy;
52
91.5k
}
gstoraster_ps_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
23.4k
{
43
23.4k
  size_t to_copy = min(len, g_size);
44
23.4k
  to_copy = min(INT_MAX, to_copy);
45
46
23.4k
  memcpy(buf, g_data, to_copy);
47
48
23.4k
  g_data += to_copy;
49
23.4k
  g_size -= to_copy;
50
51
23.4k
  return to_copy;
52
23.4k
}
gs_device_faxg3_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
142k
{
43
142k
  size_t to_copy = min(len, g_size);
44
142k
  to_copy = min(INT_MAX, to_copy);
45
46
142k
  memcpy(buf, g_data, to_copy);
47
48
142k
  g_data += to_copy;
49
142k
  g_size -= to_copy;
50
51
142k
  return to_copy;
52
142k
}
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
59.9k
{
43
59.9k
  size_t to_copy = min(len, g_size);
44
59.9k
  to_copy = min(INT_MAX, to_copy);
45
46
59.9k
  memcpy(buf, g_data, to_copy);
47
48
59.9k
  g_data += to_copy;
49
59.9k
  g_size -= to_copy;
50
51
59.9k
  return to_copy;
52
59.9k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
125k
{
43
125k
  size_t to_copy = min(len, g_size);
44
125k
  to_copy = min(INT_MAX, to_copy);
45
46
125k
  memcpy(buf, g_data, to_copy);
47
48
125k
  g_data += to_copy;
49
125k
  g_size -= to_copy;
50
51
125k
  return to_copy;
52
125k
}
gs_device_eps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
107k
{
43
107k
  size_t to_copy = min(len, g_size);
44
107k
  to_copy = min(INT_MAX, to_copy);
45
46
107k
  memcpy(buf, g_data, to_copy);
47
48
107k
  g_data += to_copy;
49
107k
  g_size -= to_copy;
50
51
107k
  return to_copy;
52
107k
}
gstoraster_pdf_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
33.2k
{
43
33.2k
  size_t to_copy = min(len, g_size);
44
33.2k
  to_copy = min(INT_MAX, to_copy);
45
46
33.2k
  memcpy(buf, g_data, to_copy);
47
48
33.2k
  g_data += to_copy;
49
33.2k
  g_size -= to_copy;
50
51
33.2k
  return to_copy;
52
33.2k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
81.5k
{
43
81.5k
  size_t to_copy = min(len, g_size);
44
81.5k
  to_copy = min(INT_MAX, to_copy);
45
46
81.5k
  memcpy(buf, g_data, to_copy);
47
48
81.5k
  g_data += to_copy;
49
81.5k
  g_size -= to_copy;
50
51
81.5k
  return to_copy;
52
81.5k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
53.5k
{
43
53.5k
  size_t to_copy = min(len, g_size);
44
53.5k
  to_copy = min(INT_MAX, to_copy);
45
46
53.5k
  memcpy(buf, g_data, to_copy);
47
48
53.5k
  g_data += to_copy;
49
53.5k
  g_size -= to_copy;
50
51
53.5k
  return to_copy;
52
53.5k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
639k
{
56
  /* Just discard everything. */
57
639k
  return len;
58
639k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
46.3k
{
56
  /* Just discard everything. */
57
46.3k
  return len;
58
46.3k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
26.6k
{
56
  /* Just discard everything. */
57
26.6k
  return len;
58
26.6k
}
gs_device_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
46.7k
{
56
  /* Just discard everything. */
57
46.7k
  return len;
58
46.7k
}
gs_device_png16m_fuzzer.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_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
19.3k
{
56
  /* Just discard everything. */
57
19.3k
  return len;
58
19.3k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
24.9k
{
56
  /* Just discard everything. */
57
24.9k
  return len;
58
24.9k
}
gstoraster_fuzzer_all_colors.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
51.1k
{
56
  /* Just discard everything. */
57
51.1k
  return len;
58
51.1k
}
gs_device_xpswrite_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
}
gstoraster_ps_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
41.3k
{
56
  /* Just discard everything. */
57
41.3k
  return len;
58
41.3k
}
gs_device_faxg3_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
19.8k
{
56
  /* Just discard everything. */
57
19.8k
  return len;
58
19.8k
}
gs_device_psdcmyk_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_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
68.6k
{
56
  /* Just discard everything. */
57
68.6k
  return len;
58
68.6k
}
gs_device_pxlmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
23.8k
{
56
  /* Just discard everything. */
57
23.8k
  return len;
58
23.8k
}
gs_device_eps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
67.6k
{
56
  /* Just discard everything. */
57
67.6k
  return len;
58
67.6k
}
gstoraster_pdf_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
55.4k
{
56
  /* Just discard everything. */
57
55.4k
  return len;
58
55.4k
}
gs_device_bmpmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
22.6k
{
56
  /* Just discard everything. */
57
22.6k
  return len;
58
22.6k
}
gstoraster_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
58.0k
{
56
  /* Just discard everything. */
57
58.0k
  return len;
58
58.0k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
36.8k
{
66
36.8k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
36.8k
}
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
184k
{
78
184k
  int ret;
79
184k
  void *gs = NULL;
80
184k
  char color_space[50];
81
184k
  char gs_device[50];
82
184k
  char gs_o[100];
83
184k
  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
184k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
184k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
184k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
184k
  if (do_interpolation) {
93
5.99k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
5.99k
  }
95
178k
  else {
96
178k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
178k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
184k
  char *args[] = {
100
184k
    "gs",
101
184k
    "-K1048576",
102
184k
    "-r200x200",
103
184k
    "-sBandListStorage=memory",
104
184k
    "-dMaxBitmap=0",
105
184k
    "-dBufferSpace=450k",
106
184k
    "-dMediaPosition=1",
107
184k
    color_space,
108
184k
    "-dQUIET",
109
184k
    "-dSAFER",
110
184k
    "-dNOPAUSE",
111
184k
    "-dBATCH",
112
184k
    opt_interpolation,
113
184k
    "-dNOMEDIAATTRS",
114
184k
    "-sstdout=%%stderr",
115
184k
    gs_o,
116
184k
    gs_device,
117
184k
    "-_",
118
184k
  };
119
184k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
184k
  g_data = buf;
123
184k
  g_size = size;
124
125
184k
  ret = gsapi_new_instance(&gs, NULL);
126
184k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
184k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
184k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
184k
  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
184k
  ret = gsapi_init_with_args(gs, argc, args);
140
184k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
51.9k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
184k
  ret = gsapi_exit(gs);
145
184k
  if (ret < 0 && ret != gs_error_Quit) {
146
187
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
187
    return ret;
148
187
  }
149
150
184k
  gsapi_delete_instance(gs);
151
152
184k
  return 0;
153
184k
}