/src/gstoraster_fuzzer.cc
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 | 18.8k | #define min(x, y) ((x) < (y) ? (x) : (y)) |
31 | | |
32 | | static int gs_stdin(void *inst, char *buf, int len) |
33 | 9.44k | { |
34 | 9.44k | size_t to_copy = min(len, g_size); |
35 | 9.44k | to_copy = min(INT_MAX, to_copy); |
36 | | |
37 | 9.44k | memcpy(buf, g_data, to_copy); |
38 | | |
39 | 9.44k | g_data += to_copy; |
40 | 9.44k | g_size -= to_copy; |
41 | | |
42 | 9.44k | return to_copy; |
43 | 9.44k | } |
44 | | |
45 | | static int gs_stdnull(void *inst, const char *buf, int len) |
46 | 5.54k | { |
47 | | /* Just discard everything. */ |
48 | 5.54k | return len; |
49 | 5.54k | } |
50 | | |
51 | | static int gs_to_raster_fuzz(const unsigned char *buf, size_t size) |
52 | 683 | { |
53 | 683 | int ret; |
54 | 683 | void *gs = NULL; |
55 | | |
56 | | /* Mostly stolen from cups-filters gstoraster. */ |
57 | 683 | char *args[] = { |
58 | 683 | "gs", |
59 | 683 | "-K1048576", |
60 | 683 | "-r200x200", |
61 | 683 | "-sBandListStorage=memory", |
62 | 683 | "-dMaxBitmap=0", |
63 | 683 | "-dBufferSpace=450k", |
64 | 683 | "-dMediaPosition=1", |
65 | 683 | "-dcupsColorSpace=1", /* RGB */ |
66 | 683 | "-dQUIET", |
67 | 683 | "-dSAFER", |
68 | 683 | "-dNOPAUSE", |
69 | 683 | "-dBATCH", |
70 | 683 | "-dNOINTERPOLATE", |
71 | 683 | "-dNOMEDIAATTRS", |
72 | 683 | "-sstdout=%%stderr", |
73 | 683 | "-sOutputFile=/dev/null", |
74 | 683 | "-sDEVICE=cups", |
75 | 683 | "-_", |
76 | 683 | }; |
77 | 683 | int argc = sizeof(args) / sizeof(args[0]); |
78 | | |
79 | | /* Stash buffers globally, for gs_stdin(). */ |
80 | 683 | g_data = buf; |
81 | 683 | g_size = size; |
82 | | |
83 | 683 | ret = gsapi_new_instance(&gs, NULL); |
84 | 683 | if (ret < 0) { |
85 | 0 | fprintf(stderr, "gsapi_new_instance: error %d\n", ret); |
86 | 0 | return ret; |
87 | 0 | } |
88 | | |
89 | 683 | gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull); |
90 | 683 | ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8); |
91 | 683 | if (ret < 0) { |
92 | 0 | fprintf(stderr, "gsapi_set_arg_encoding: error %d\n", ret); |
93 | 0 | gsapi_delete_instance(gs); |
94 | 0 | return ret; |
95 | 0 | } |
96 | | |
97 | 683 | ret = gsapi_init_with_args(gs, argc, args); |
98 | 683 | if (ret && ret != gs_error_Quit) |
99 | | /* Just keep going, to cleanup. */ |
100 | 9 | fprintf(stderr, "gsapi_init_with_args: error %d\n", ret); |
101 | | |
102 | 683 | ret = gsapi_exit(gs); |
103 | 683 | if (ret < 0 && ret != gs_error_Quit) { |
104 | 0 | fprintf(stderr, "gsapi_exit: error %d\n", ret); |
105 | 0 | return ret; |
106 | 0 | } |
107 | | |
108 | 683 | gsapi_delete_instance(gs); |
109 | | |
110 | 683 | return 0; |
111 | 683 | } |
112 | | |
113 | 683 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
114 | 683 | gs_to_raster_fuzz(data, size); |
115 | 683 | return 0; |
116 | 683 | } |