Coverage Report

Created: 2025-12-31 07:31

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.24M
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
1.62M
{
43
1.62M
  size_t to_copy = min(len, g_size);
44
1.62M
  to_copy = min(INT_MAX, to_copy);
45
46
1.62M
  memcpy(buf, g_data, to_copy);
47
48
1.62M
  g_data += to_copy;
49
1.62M
  g_size -= to_copy;
50
51
1.62M
  return to_copy;
52
1.62M
}
gs_device_pdfwrite_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
}
gs_device_pxlcolor_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_ps2write_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
}
gs_device_png16m_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_pgmraw_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
87.5k
{
43
87.5k
  size_t to_copy = min(len, g_size);
44
87.5k
  to_copy = min(INT_MAX, to_copy);
45
46
87.5k
  memcpy(buf, g_data, to_copy);
47
48
87.5k
  g_data += to_copy;
49
87.5k
  g_size -= to_copy;
50
51
87.5k
  return to_copy;
52
87.5k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
185k
{
43
185k
  size_t to_copy = min(len, g_size);
44
185k
  to_copy = min(INT_MAX, to_copy);
45
46
185k
  memcpy(buf, g_data, to_copy);
47
48
185k
  g_data += to_copy;
49
185k
  g_size -= to_copy;
50
51
185k
  return to_copy;
52
185k
}
gstoraster_fuzzer_all_colors.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
24.4k
{
43
24.4k
  size_t to_copy = min(len, g_size);
44
24.4k
  to_copy = min(INT_MAX, to_copy);
45
46
24.4k
  memcpy(buf, g_data, to_copy);
47
48
24.4k
  g_data += to_copy;
49
24.4k
  g_size -= to_copy;
50
51
24.4k
  return to_copy;
52
24.4k
}
gs_device_xpswrite_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
}
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
151k
{
43
151k
  size_t to_copy = min(len, g_size);
44
151k
  to_copy = min(INT_MAX, to_copy);
45
46
151k
  memcpy(buf, g_data, to_copy);
47
48
151k
  g_data += to_copy;
49
151k
  g_size -= to_copy;
50
51
151k
  return to_copy;
52
151k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
93.2k
{
43
93.2k
  size_t to_copy = min(len, g_size);
44
93.2k
  to_copy = min(INT_MAX, to_copy);
45
46
93.2k
  memcpy(buf, g_data, to_copy);
47
48
93.2k
  g_data += to_copy;
49
93.2k
  g_size -= to_copy;
50
51
93.2k
  return to_copy;
52
93.2k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
67.8k
{
43
67.8k
  size_t to_copy = min(len, g_size);
44
67.8k
  to_copy = min(INT_MAX, to_copy);
45
46
67.8k
  memcpy(buf, g_data, to_copy);
47
48
67.8k
  g_data += to_copy;
49
67.8k
  g_size -= to_copy;
50
51
67.8k
  return to_copy;
52
67.8k
}
gs_device_pxlmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
70.2k
{
43
70.2k
  size_t to_copy = min(len, g_size);
44
70.2k
  to_copy = min(INT_MAX, to_copy);
45
46
70.2k
  memcpy(buf, g_data, to_copy);
47
48
70.2k
  g_data += to_copy;
49
70.2k
  g_size -= to_copy;
50
51
70.2k
  return to_copy;
52
70.2k
}
gs_device_eps2write_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
}
gstoraster_pdf_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
41.1k
{
43
41.1k
  size_t to_copy = min(len, g_size);
44
41.1k
  to_copy = min(INT_MAX, to_copy);
45
46
41.1k
  memcpy(buf, g_data, to_copy);
47
48
41.1k
  g_data += to_copy;
49
41.1k
  g_size -= to_copy;
50
51
41.1k
  return to_copy;
52
41.1k
}
gs_device_bmpmono_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
90.1k
{
43
90.1k
  size_t to_copy = min(len, g_size);
44
90.1k
  to_copy = min(INT_MAX, to_copy);
45
46
90.1k
  memcpy(buf, g_data, to_copy);
47
48
90.1k
  g_data += to_copy;
49
90.1k
  g_size -= to_copy;
50
51
90.1k
  return to_copy;
52
90.1k
}
gstoraster_fuzzer.cc:gs_stdin(void*, char*, int)
Line
Count
Source
42
67.4k
{
43
67.4k
  size_t to_copy = min(len, g_size);
44
67.4k
  to_copy = min(INT_MAX, to_copy);
45
46
67.4k
  memcpy(buf, g_data, to_copy);
47
48
67.4k
  g_data += to_copy;
49
67.4k
  g_size -= to_copy;
50
51
67.4k
  return to_copy;
52
67.4k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
694k
{
56
  /* Just discard everything. */
57
694k
  return len;
58
694k
}
gs_device_pdfwrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
71.8k
{
56
  /* Just discard everything. */
57
71.8k
  return len;
58
71.8k
}
gs_device_pxlcolor_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
34.8k
{
56
  /* Just discard everything. */
57
34.8k
  return len;
58
34.8k
}
gs_device_ps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
14.1k
{
56
  /* Just discard everything. */
57
14.1k
  return len;
58
14.1k
}
gs_device_png16m_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_pgmraw_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
22.9k
{
56
  /* Just discard everything. */
57
22.9k
  return len;
58
22.9k
}
gs_device_tiffsep1_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
35.8k
{
56
  /* Just discard everything. */
57
35.8k
  return len;
58
35.8k
}
gstoraster_fuzzer_all_colors.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
50.5k
{
56
  /* Just discard everything. */
57
50.5k
  return len;
58
50.5k
}
gs_device_xpswrite_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
32.3k
{
56
  /* Just discard everything. */
57
32.3k
  return len;
58
32.3k
}
gstoraster_ps_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
42.4k
{
56
  /* Just discard everything. */
57
42.4k
  return len;
58
42.4k
}
gs_device_faxg3_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
23.4k
{
56
  /* Just discard everything. */
57
23.4k
  return len;
58
23.4k
}
gs_device_psdcmyk_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
6.77k
{
56
  /* Just discard everything. */
57
6.77k
  return len;
58
6.77k
}
gs_device_pdfwrite_opts_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
69.1k
{
56
  /* Just discard everything. */
57
69.1k
  return len;
58
69.1k
}
gs_device_pxlmono_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_eps2write_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
76.3k
{
56
  /* Just discard everything. */
57
76.3k
  return len;
58
76.3k
}
gstoraster_pdf_fuzzer.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
68.5k
{
56
  /* Just discard everything. */
57
68.5k
  return len;
58
68.5k
}
gs_device_bmpmono_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.cc:gs_stdnull(void*, char const*, int)
Line
Count
Source
55
70.5k
{
56
  /* Just discard everything. */
57
70.5k
  return len;
58
70.5k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
40.2k
{
66
40.2k
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
40.2k
}
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
182k
{
78
182k
  int ret;
79
182k
  void *gs = NULL;
80
182k
  char color_space[50];
81
182k
  char gs_device[50];
82
182k
  char gs_o[100];
83
182k
  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
182k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
182k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
182k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
182k
  if (do_interpolation) {
93
6.07k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
6.07k
  }
95
176k
  else {
96
176k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
176k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
182k
  char *args[] = {
100
182k
    "gs",
101
182k
    "-K1048576",
102
182k
    "-r200x200",
103
182k
    "-sBandListStorage=memory",
104
182k
    "-dMaxBitmap=0",
105
182k
    "-dBufferSpace=450k",
106
182k
    "-dMediaPosition=1",
107
182k
    color_space,
108
182k
    "-dQUIET",
109
182k
    "-dSAFER",
110
182k
    "-dNOPAUSE",
111
182k
    "-dBATCH",
112
182k
    opt_interpolation,
113
182k
    "-dNOMEDIAATTRS",
114
182k
    "-sstdout=%%stderr",
115
182k
    gs_o,
116
182k
    gs_device,
117
182k
    "-_",
118
182k
  };
119
182k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
182k
  g_data = buf;
123
182k
  g_size = size;
124
125
182k
  ret = gsapi_new_instance(&gs, NULL);
126
182k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
182k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
182k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
182k
  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
182k
  ret = gsapi_init_with_args(gs, argc, args);
140
182k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
48.5k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
182k
  ret = gsapi_exit(gs);
145
182k
  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
182k
  gsapi_delete_instance(gs);
151
152
182k
  return 0;
153
182k
}