Coverage Report

Created: 2025-08-28 07:06

/src/gs_fuzzlib.h
Line
Count
Source (jump to first uncovered line)
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.23M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.61M
{
43
1.61M
  size_t to_copy = min(len, g_size);
44
1.61M
  to_copy = min(INT_MAX, to_copy);
45
46
1.61M
  memcpy(buf, g_data, to_copy);
47
48
1.61M
  g_data += to_copy;
49
1.61M
  g_size -= to_copy;
50
51
1.61M
  return to_copy;
52
1.61M
}
gs_device_pdfwrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
163k
{
43
163k
  size_t to_copy = min(len, g_size);
44
163k
  to_copy = min(INT_MAX, to_copy);
45
46
163k
  memcpy(buf, g_data, to_copy);
47
48
163k
  g_data += to_copy;
49
163k
  g_size -= to_copy;
50
51
163k
  return to_copy;
52
163k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
84.0k
{
43
84.0k
  size_t to_copy = min(len, g_size);
44
84.0k
  to_copy = min(INT_MAX, to_copy);
45
46
84.0k
  memcpy(buf, g_data, to_copy);
47
48
84.0k
  g_data += to_copy;
49
84.0k
  g_size -= to_copy;
50
51
84.0k
  return to_copy;
52
84.0k
}
gs_device_ps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
118k
{
43
118k
  size_t to_copy = min(len, g_size);
44
118k
  to_copy = min(INT_MAX, to_copy);
45
46
118k
  memcpy(buf, g_data, to_copy);
47
48
118k
  g_data += to_copy;
49
118k
  g_size -= to_copy;
50
51
118k
  return to_copy;
52
118k
}
gs_device_png16m_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_pgmraw_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_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
182k
{
43
182k
  size_t to_copy = min(len, g_size);
44
182k
  to_copy = min(INT_MAX, to_copy);
45
46
182k
  memcpy(buf, g_data, to_copy);
47
48
182k
  g_data += to_copy;
49
182k
  g_size -= to_copy;
50
51
182k
  return to_copy;
52
182k
}
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
24.8k
{
43
24.8k
  size_t to_copy = min(len, g_size);
44
24.8k
  to_copy = min(INT_MAX, to_copy);
45
46
24.8k
  memcpy(buf, g_data, to_copy);
47
48
24.8k
  g_data += to_copy;
49
24.8k
  g_size -= to_copy;
50
51
24.8k
  return to_copy;
52
24.8k
}
gs_device_xpswrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
73.0k
{
43
73.0k
  size_t to_copy = min(len, g_size);
44
73.0k
  to_copy = min(INT_MAX, to_copy);
45
46
73.0k
  memcpy(buf, g_data, to_copy);
47
48
73.0k
  g_data += to_copy;
49
73.0k
  g_size -= to_copy;
50
51
73.0k
  return to_copy;
52
73.0k
}
gstoraster_ps_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
25.0k
{
43
25.0k
  size_t to_copy = min(len, g_size);
44
25.0k
  to_copy = min(INT_MAX, to_copy);
45
46
25.0k
  memcpy(buf, g_data, to_copy);
47
48
25.0k
  g_data += to_copy;
49
25.0k
  g_size -= to_copy;
50
51
25.0k
  return to_copy;
52
25.0k
}
gs_device_faxg3_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
137k
{
43
137k
  size_t to_copy = min(len, g_size);
44
137k
  to_copy = min(INT_MAX, to_copy);
45
46
137k
  memcpy(buf, g_data, to_copy);
47
48
137k
  g_data += to_copy;
49
137k
  g_size -= to_copy;
50
51
137k
  return to_copy;
52
137k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
184k
{
43
184k
  size_t to_copy = min(len, g_size);
44
184k
  to_copy = min(INT_MAX, to_copy);
45
46
184k
  memcpy(buf, g_data, to_copy);
47
48
184k
  g_data += to_copy;
49
184k
  g_size -= to_copy;
50
51
184k
  return to_copy;
52
184k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
63.1k
{
43
63.1k
  size_t to_copy = min(len, g_size);
44
63.1k
  to_copy = min(INT_MAX, to_copy);
45
46
63.1k
  memcpy(buf, g_data, to_copy);
47
48
63.1k
  g_data += to_copy;
49
63.1k
  g_size -= to_copy;
50
51
63.1k
  return to_copy;
52
63.1k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
56.4k
{
43
56.4k
  size_t to_copy = min(len, g_size);
44
56.4k
  to_copy = min(INT_MAX, to_copy);
45
46
56.4k
  memcpy(buf, g_data, to_copy);
47
48
56.4k
  g_data += to_copy;
49
56.4k
  g_size -= to_copy;
50
51
56.4k
  return to_copy;
52
56.4k
}
gs_device_eps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
114k
{
43
114k
  size_t to_copy = min(len, g_size);
44
114k
  to_copy = min(INT_MAX, to_copy);
45
46
114k
  memcpy(buf, g_data, to_copy);
47
48
114k
  g_data += to_copy;
49
114k
  g_size -= to_copy;
50
51
114k
  return to_copy;
52
114k
}
gstoraster_pdf_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
36.8k
{
43
36.8k
  size_t to_copy = min(len, g_size);
44
36.8k
  to_copy = min(INT_MAX, to_copy);
45
46
36.8k
  memcpy(buf, g_data, to_copy);
47
48
36.8k
  g_data += to_copy;
49
36.8k
  g_size -= to_copy;
50
51
36.8k
  return to_copy;
52
36.8k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
80.6k
{
43
80.6k
  size_t to_copy = min(len, g_size);
44
80.6k
  to_copy = min(INT_MAX, to_copy);
45
46
80.6k
  memcpy(buf, g_data, to_copy);
47
48
80.6k
  g_data += to_copy;
49
80.6k
  g_size -= to_copy;
50
51
80.6k
  return to_copy;
52
80.6k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
59.5k
{
43
59.5k
  size_t to_copy = min(len, g_size);
44
59.5k
  to_copy = min(INT_MAX, to_copy);
45
46
59.5k
  memcpy(buf, g_data, to_copy);
47
48
59.5k
  g_data += to_copy;
49
59.5k
  g_size -= to_copy;
50
51
59.5k
  return to_copy;
52
59.5k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
736k
{
56
  /* Just discard everything. */
57
736k
  return len;
58
736k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
66.9k
{
56
  /* Just discard everything. */
57
66.9k
  return len;
58
66.9k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
27.7k
{
56
  /* Just discard everything. */
57
27.7k
  return len;
58
27.7k
}
gs_device_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
58.9k
{
56
  /* Just discard everything. */
57
58.9k
  return len;
58
58.9k
}
gs_device_png16m_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
23.9k
{
56
  /* Just discard everything. */
57
23.9k
  return len;
58
23.9k
}
gs_device_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
22.7k
{
56
  /* Just discard everything. */
57
22.7k
  return len;
58
22.7k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
55.7k
{
56
  /* Just discard everything. */
57
55.7k
  return len;
58
55.7k
}
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
24.7k
{
56
  /* Just discard everything. */
57
24.7k
  return len;
58
24.7k
}
gstoraster_ps_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
44.3k
{
56
  /* Just discard everything. */
57
44.3k
  return len;
58
44.3k
}
gs_device_faxg3_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
19.0k
{
56
  /* Just discard everything. */
57
19.0k
  return len;
58
19.0k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
19.1k
{
56
  /* Just discard everything. */
57
19.1k
  return len;
58
19.1k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
68.9k
{
56
  /* Just discard everything. */
57
68.9k
  return len;
58
68.9k
}
gs_device_pxlmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
21.8k
{
56
  /* Just discard everything. */
57
21.8k
  return len;
58
21.8k
}
gs_device_eps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
76.0k
{
56
  /* Just discard everything. */
57
76.0k
  return len;
58
76.0k
}
gstoraster_pdf_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
61.1k
{
56
  /* Just discard everything. */
57
61.1k
  return len;
58
61.1k
}
gs_device_bmpmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
24.4k
{
56
  /* Just discard everything. */
57
24.4k
  return len;
58
24.4k
}
gstoraster_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
68.9k
{
56
  /* Just discard everything. */
57
68.9k
  return len;
58
68.9k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
39.3k
{
66
39.3k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
39.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
189k
{
78
189k
  int ret;
79
189k
  void *gs = NULL;
80
189k
  char color_space[50];
81
189k
  char gs_device[50];
82
189k
  char gs_o[100];
83
189k
  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
189k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
189k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
189k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
189k
  if (do_interpolation) {
93
6.07k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
6.07k
  }
95
183k
  else {
96
183k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
183k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
189k
  char *args[] = {
100
189k
    "gs",
101
189k
    "-K1048576",
102
189k
    "-r200x200",
103
189k
    "-sBandListStorage=memory",
104
189k
    "-dMaxBitmap=0",
105
189k
    "-dBufferSpace=450k",
106
189k
    "-dMediaPosition=1",
107
189k
    color_space,
108
189k
    "-dQUIET",
109
189k
    "-dSAFER",
110
189k
    "-dNOPAUSE",
111
189k
    "-dBATCH",
112
189k
    opt_interpolation,
113
189k
    "-dNOMEDIAATTRS",
114
189k
    "-sstdout=%%stderr",
115
189k
    gs_o,
116
189k
    gs_device,
117
189k
    "-_",
118
189k
  };
119
189k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
189k
  g_data = buf;
123
189k
  g_size = size;
124
125
189k
  ret = gsapi_new_instance(&gs, NULL);
126
189k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
189k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
189k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
189k
  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
189k
  ret = gsapi_init_with_args(gs, argc, args);
140
189k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
54.0k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
189k
  ret = gsapi_exit(gs);
145
189k
  if (ret < 0 && ret != gs_error_Quit) {
146
1
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
1
    return ret;
148
1
  }
149
150
189k
  gsapi_delete_instance(gs);
151
152
189k
  return 0;
153
189k
}