/src/libdwarf/fuzz/fuzz_globals.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2021 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | #include <fcntl.h> /* open() O_RDONLY O_BINARY */ |
13 | | #include <stdint.h> |
14 | | #include <stdio.h> |
15 | | #include <stdlib.h> |
16 | | #include <string.h> |
17 | | #include <sys/types.h> |
18 | | #include <unistd.h> |
19 | | |
20 | | #ifndef O_BINARY |
21 | 10.9k | #define O_BINARY 0 /* So it does nothing in Linux/Unix */ |
22 | | #endif |
23 | | |
24 | | /* |
25 | | * Libdwarf library callers can only use these headers. |
26 | | */ |
27 | | #include "dwarf.h" |
28 | | #include "libdwarf.h" |
29 | | |
30 | | int get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error); |
31 | | int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error); |
32 | | int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error); |
33 | | |
34 | | /* |
35 | | * A fuzzer that simulates a small part of the simplereader.c example. |
36 | | */ |
37 | 10.9k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
38 | 10.9k | char filename[256]; |
39 | | |
40 | | #ifdef DWREGRESSIONTEMP |
41 | | /* Under msys2, the /tmp/ results in an open fail, |
42 | | so we discard the /tmp/ here */ |
43 | | sprintf(filename, "junklibfuzzer.%d", getpid()); |
44 | | #else |
45 | 10.9k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
46 | 10.9k | #endif |
47 | 10.9k | FILE *fp = fopen(filename, "wb"); |
48 | 10.9k | if (!fp) { |
49 | 0 | printf("FAIL libfuzzer cannot open temp as writeable %s\n", |
50 | 0 | filename); |
51 | 0 | return 0; |
52 | 0 | } |
53 | 10.9k | fwrite(data, size, 1, fp); |
54 | 10.9k | fclose(fp); |
55 | | |
56 | 10.9k | Dwarf_Debug dbg = 0; |
57 | 10.9k | int res = DW_DLV_ERROR; |
58 | 10.9k | Dwarf_Error error = 0; |
59 | 10.9k | Dwarf_Handler errhand = 0; |
60 | 10.9k | Dwarf_Ptr errarg = 0; |
61 | | |
62 | 10.9k | int fd = open(filename, O_RDONLY | O_BINARY); |
63 | 10.9k | if (fd < 0) { |
64 | 0 | exit(EXIT_FAILURE); |
65 | 0 | } |
66 | | |
67 | 10.9k | res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
68 | | |
69 | 10.9k | if (res != DW_DLV_OK) { |
70 | 7.37k | dwarf_dealloc_error(dbg, error); |
71 | 7.37k | } else { |
72 | 3.58k | dwarf_return_empty_pubnames(dbg, 1); |
73 | 3.58k | dwarf_return_empty_pubnames(dbg, 0); |
74 | 3.58k | get_globals_example(dbg, &error); |
75 | 3.58k | get_globals_by_type_example(dbg, &error); |
76 | 3.58k | } |
77 | | |
78 | 10.9k | dwarf_finish(dbg); |
79 | 10.9k | close(fd); |
80 | 10.9k | unlink(filename); |
81 | 10.9k | return 0; |
82 | 10.9k | } |
83 | | |
84 | 3.58k | int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
85 | 3.58k | Dwarf_Signed count = 0; |
86 | 3.58k | Dwarf_Global *globs = 0; |
87 | 3.58k | Dwarf_Signed i = 0; |
88 | 3.58k | int res = 0; |
89 | | |
90 | 3.58k | res = dwarf_get_globals(dbg, &globs, &count, error); |
91 | 3.58k | if (res != DW_DLV_OK) { |
92 | 2.62k | return res; |
93 | 2.62k | } |
94 | 10.4k | for (i = 0; i < count; ++i) { |
95 | 9.46k | int tag_idx = dwarf_global_tag_number(globs[i]); // DWARF5 only |
96 | | |
97 | 9.46k | char *name = 0; |
98 | 9.46k | res = dwarf_globname(globs[i], &name, error); |
99 | 9.46k | if (res != DW_DLV_OK) { |
100 | 0 | continue; |
101 | 0 | } |
102 | | |
103 | 9.46k | Dwarf_Off dw_die_offset; |
104 | 9.46k | res = dwarf_global_die_offset(globs[i], &dw_die_offset, error); |
105 | 9.46k | if (res != DW_DLV_OK) { |
106 | 0 | continue; |
107 | 0 | } |
108 | 9.46k | Dwarf_Off dw_cu_offset; |
109 | 9.46k | res = dwarf_global_cu_offset(globs[i], &dw_cu_offset, error); |
110 | 9.46k | if (res != DW_DLV_OK) { |
111 | 0 | continue; |
112 | 0 | } |
113 | | |
114 | 9.46k | char *name_2; |
115 | 9.46k | Dwarf_Off dw_die_offset_2, dw_cu_offset_2; |
116 | 9.46k | dwarf_global_name_offsets(globs[i], &name_2, &dw_die_offset_2, |
117 | 9.46k | &dw_cu_offset_2, error); |
118 | 9.46k | if (res != DW_DLV_OK) { |
119 | 0 | continue; |
120 | 0 | } |
121 | | |
122 | 9.46k | int dw_category; |
123 | 9.46k | Dwarf_Off dw_offset_pub_header; |
124 | 9.46k | Dwarf_Unsigned dw_length_size; |
125 | 9.46k | Dwarf_Unsigned dw_length_pub; |
126 | 9.46k | Dwarf_Unsigned dw_version; |
127 | 9.46k | Dwarf_Unsigned dw_header_info_offset; |
128 | 9.46k | Dwarf_Unsigned dw_info_length; |
129 | 9.46k | res = dwarf_get_globals_header( |
130 | 9.46k | globs[i], &dw_category, &dw_offset_pub_header, &dw_length_size, |
131 | 9.46k | &dw_length_pub, &dw_version, &dw_header_info_offset, &dw_info_length, |
132 | 9.46k | error); |
133 | 9.46k | } |
134 | 957 | dwarf_globals_dealloc(dbg, globs, count); |
135 | 957 | return DW_DLV_OK; |
136 | 3.58k | } |
137 | | |
138 | | /* DWARF4 */ |
139 | 3.58k | int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
140 | 3.58k | int res = DW_DLV_OK; |
141 | 25.0k | for (int i = 0; i < 6; i++) { |
142 | 21.4k | Dwarf_Signed count = 0; |
143 | 21.4k | Dwarf_Global *contents = 0; |
144 | 21.4k | Dwarf_Signed i = 0; |
145 | | |
146 | 21.4k | res = dwarf_globals_by_type(dbg, i, &contents, &count, error); |
147 | | |
148 | 21.4k | dwarf_globals_dealloc(dbg, contents, count); |
149 | 21.4k | } |
150 | | |
151 | 3.58k | return res; |
152 | 3.58k | } |
153 | | |
154 | | /* DWARF4 */ |
155 | 0 | int get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
156 | 0 | Dwarf_Signed count = 0; |
157 | 0 | Dwarf_Global *contents = 0; |
158 | |
|
159 | 0 | int res = dwarf_get_pubtypes(dbg, &contents, &count, error); |
160 | |
|
161 | 0 | dwarf_globals_dealloc(dbg, contents, count); |
162 | |
|
163 | 0 | return res; |
164 | 0 | } |