Coverage Report

Created: 2026-08-08 08:00

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.45M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.22M
{
43
1.22M
  size_t to_copy = min(len, g_size);
44
1.22M
  to_copy = min(INT_MAX, to_copy);
45
46
1.22M
  memcpy(buf, g_data, to_copy);
47
48
1.22M
  g_data += to_copy;
49
1.22M
  g_size -= to_copy;
50
51
1.22M
  return to_copy;
52
1.22M
}
gs_device_pdfwrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
12.4k
{
43
12.4k
  size_t to_copy = min(len, g_size);
44
12.4k
  to_copy = min(INT_MAX, to_copy);
45
46
12.4k
  memcpy(buf, g_data, to_copy);
47
48
12.4k
  g_data += to_copy;
49
12.4k
  g_size -= to_copy;
50
51
12.4k
  return to_copy;
52
12.4k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
74.8k
{
43
74.8k
  size_t to_copy = min(len, g_size);
44
74.8k
  to_copy = min(INT_MAX, to_copy);
45
46
74.8k
  memcpy(buf, g_data, to_copy);
47
48
74.8k
  g_data += to_copy;
49
74.8k
  g_size -= to_copy;
50
51
74.8k
  return to_copy;
52
74.8k
}
gs_device_ps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
103k
{
43
103k
  size_t to_copy = min(len, g_size);
44
103k
  to_copy = min(INT_MAX, to_copy);
45
46
103k
  memcpy(buf, g_data, to_copy);
47
48
103k
  g_data += to_copy;
49
103k
  g_size -= to_copy;
50
51
103k
  return to_copy;
52
103k
}
gs_device_png16m_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
55.3k
{
43
55.3k
  size_t to_copy = min(len, g_size);
44
55.3k
  to_copy = min(INT_MAX, to_copy);
45
46
55.3k
  memcpy(buf, g_data, to_copy);
47
48
55.3k
  g_data += to_copy;
49
55.3k
  g_size -= to_copy;
50
51
55.3k
  return to_copy;
52
55.3k
}
gs_device_pgmraw_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
59.1k
{
43
59.1k
  size_t to_copy = min(len, g_size);
44
59.1k
  to_copy = min(INT_MAX, to_copy);
45
46
59.1k
  memcpy(buf, g_data, to_copy);
47
48
59.1k
  g_data += to_copy;
49
59.1k
  g_size -= to_copy;
50
51
59.1k
  return to_copy;
52
59.1k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
168k
{
43
168k
  size_t to_copy = min(len, g_size);
44
168k
  to_copy = min(INT_MAX, to_copy);
45
46
168k
  memcpy(buf, g_data, to_copy);
47
48
168k
  g_data += to_copy;
49
168k
  g_size -= to_copy;
50
51
168k
  return to_copy;
52
168k
}
gs_device_ljet4_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
25.4k
{
43
25.4k
  size_t to_copy = min(len, g_size);
44
25.4k
  to_copy = min(INT_MAX, to_copy);
45
46
25.4k
  memcpy(buf, g_data, to_copy);
47
48
25.4k
  g_data += to_copy;
49
25.4k
  g_size -= to_copy;
50
51
25.4k
  return to_copy;
52
25.4k
}
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
15.1k
{
43
15.1k
  size_t to_copy = min(len, g_size);
44
15.1k
  to_copy = min(INT_MAX, to_copy);
45
46
15.1k
  memcpy(buf, g_data, to_copy);
47
48
15.1k
  g_data += to_copy;
49
15.1k
  g_size -= to_copy;
50
51
15.1k
  return to_copy;
52
15.1k
}
gs_device_xpswrite_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
50.0k
{
43
50.0k
  size_t to_copy = min(len, g_size);
44
50.0k
  to_copy = min(INT_MAX, to_copy);
45
46
50.0k
  memcpy(buf, g_data, to_copy);
47
48
50.0k
  g_data += to_copy;
49
50.0k
  g_size -= to_copy;
50
51
50.0k
  return to_copy;
52
50.0k
}
gstoraster_ps_fuzzer.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_faxg3_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
154k
{
43
154k
  size_t to_copy = min(len, g_size);
44
154k
  to_copy = min(INT_MAX, to_copy);
45
46
154k
  memcpy(buf, g_data, to_copy);
47
48
154k
  g_data += to_copy;
49
154k
  g_size -= to_copy;
50
51
154k
  return to_copy;
52
154k
}
gs_device_psdcmyk_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_pdfwrite_opts_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
56.3k
{
43
56.3k
  size_t to_copy = min(len, g_size);
44
56.3k
  to_copy = min(INT_MAX, to_copy);
45
46
56.3k
  memcpy(buf, g_data, to_copy);
47
48
56.3k
  g_data += to_copy;
49
56.3k
  g_size -= to_copy;
50
51
56.3k
  return to_copy;
52
56.3k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
76.1k
{
43
76.1k
  size_t to_copy = min(len, g_size);
44
76.1k
  to_copy = min(INT_MAX, to_copy);
45
46
76.1k
  memcpy(buf, g_data, to_copy);
47
48
76.1k
  g_data += to_copy;
49
76.1k
  g_size -= to_copy;
50
51
76.1k
  return to_copy;
52
76.1k
}
gs_device_eps2write_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
94.3k
{
43
94.3k
  size_t to_copy = min(len, g_size);
44
94.3k
  to_copy = min(INT_MAX, to_copy);
45
46
94.3k
  memcpy(buf, g_data, to_copy);
47
48
94.3k
  g_data += to_copy;
49
94.3k
  g_size -= to_copy;
50
51
94.3k
  return to_copy;
52
94.3k
}
gstoraster_pdf_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
29.4k
{
43
29.4k
  size_t to_copy = min(len, g_size);
44
29.4k
  to_copy = min(INT_MAX, to_copy);
45
46
29.4k
  memcpy(buf, g_data, to_copy);
47
48
29.4k
  g_data += to_copy;
49
29.4k
  g_size -= to_copy;
50
51
29.4k
  return to_copy;
52
29.4k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
82.1k
{
43
82.1k
  size_t to_copy = min(len, g_size);
44
82.1k
  to_copy = min(INT_MAX, to_copy);
45
46
82.1k
  memcpy(buf, g_data, to_copy);
47
48
82.1k
  g_data += to_copy;
49
82.1k
  g_size -= to_copy;
50
51
82.1k
  return to_copy;
52
82.1k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
36.2k
{
43
36.2k
  size_t to_copy = min(len, g_size);
44
36.2k
  to_copy = min(INT_MAX, to_copy);
45
46
36.2k
  memcpy(buf, g_data, to_copy);
47
48
36.2k
  g_data += to_copy;
49
36.2k
  g_size -= to_copy;
50
51
36.2k
  return to_copy;
52
36.2k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
475k
{
56
  /* Just discard everything. */
57
475k
  return len;
58
475k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
2.30k
{
56
  /* Just discard everything. */
57
2.30k
  return len;
58
2.30k
}
gs_device_pxlcolor_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_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
41.9k
{
56
  /* Just discard everything. */
57
41.9k
  return len;
58
41.9k
}
gs_device_png16m_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
15.8k
{
56
  /* Just discard everything. */
57
15.8k
  return len;
58
15.8k
}
gs_device_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
7.33k
{
56
  /* Just discard everything. */
57
7.33k
  return len;
58
7.33k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
21.9k
{
56
  /* Just discard everything. */
57
21.9k
  return len;
58
21.9k
}
gs_device_ljet4_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
8.29k
{
56
  /* Just discard everything. */
57
8.29k
  return len;
58
8.29k
}
gstoraster_fuzzer_all_colors.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
26.7k
{
56
  /* Just discard everything. */
57
26.7k
  return len;
58
26.7k
}
gs_device_xpswrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
17.3k
{
56
  /* Just discard everything. */
57
17.3k
  return len;
58
17.3k
}
gstoraster_ps_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
41.8k
{
56
  /* Just discard everything. */
57
41.8k
  return len;
58
41.8k
}
gs_device_faxg3_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
17.3k
{
56
  /* Just discard everything. */
57
17.3k
  return len;
58
17.3k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
3.46k
{
56
  /* Just discard everything. */
57
3.46k
  return len;
58
3.46k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
65.9k
{
56
  /* Just discard everything. */
57
65.9k
  return len;
58
65.9k
}
gs_device_pxlmono_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
18.4k
{
56
  /* Just discard everything. */
57
18.4k
  return len;
58
18.4k
}
gs_device_eps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
62.6k
{
56
  /* Just discard everything. */
57
62.6k
  return len;
58
62.6k
}
gstoraster_pdf_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
49.3k
{
56
  /* Just discard everything. */
57
49.3k
  return len;
58
49.3k
}
gs_device_bmpmono_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
}
gstoraster_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
25.8k
{
56
  /* Just discard everything. */
57
25.8k
  return len;
58
25.8k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
25.4k
{
66
25.4k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
25.4k
}
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
135k
{
78
135k
  int ret;
79
135k
  void *gs = NULL;
80
135k
  char color_space[50];
81
135k
  char gs_device[50];
82
135k
  char gs_o[100];
83
135k
  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
135k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
135k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
135k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
135k
  if (do_interpolation) {
93
5.30k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
5.30k
  }
95
130k
  else {
96
130k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
130k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
135k
  char *args[] = {
100
135k
    "gs",
101
135k
    "-K1048576",
102
135k
    "-r200x200",
103
135k
    "-sBandListStorage=memory",
104
135k
    "-dMaxBitmap=0",
105
135k
    "-dBufferSpace=450k",
106
135k
    "-dMediaPosition=1",
107
135k
    color_space,
108
135k
    "-dQUIET",
109
135k
    "-dSAFER",
110
135k
    "-dNOPAUSE",
111
135k
    "-dBATCH",
112
135k
    opt_interpolation,
113
135k
    "-dNOMEDIAATTRS",
114
135k
    "-sstdout=%%stderr",
115
135k
    gs_o,
116
135k
    gs_device,
117
135k
    "-_",
118
135k
  };
119
135k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
135k
  g_data = buf;
123
135k
  g_size = size;
124
125
135k
  ret = gsapi_new_instance(&gs, NULL);
126
135k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
135k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
135k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
135k
  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
135k
  ret = gsapi_init_with_args(gs, argc, args);
140
135k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
34.2k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
135k
  ret = gsapi_exit(gs);
145
135k
  if (ret < 0 && ret != gs_error_Quit) {
146
0
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
0
    gsapi_delete_instance(gs);
148
0
    return ret;
149
0
  }
150
151
135k
  gsapi_delete_instance(gs);
152
153
135k
  return 0;
154
135k
}