/src/libdwarf/fuzz/fuzz_srcfiles.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 "dwarf.h" |
13 | | #include "libdwarf.h" |
14 | | #include <fcntl.h> /* open() O_RDONLY O_BINARY */ |
15 | | #include <stdint.h> |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #include <sys/stat.h> |
20 | | #include <sys/types.h> |
21 | | #include <unistd.h> |
22 | | |
23 | | #ifndef O_BINARY |
24 | 37.7k | #define O_BINARY 0 |
25 | | #endif |
26 | | |
27 | | int examplee(Dwarf_Debug dbg, Dwarf_Die somedie, Dwarf_Error *error); |
28 | | int exampled(Dwarf_Die somedie, Dwarf_Error *error); |
29 | 37.7k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
30 | 37.7k | char filename[256]; |
31 | 37.7k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
32 | | |
33 | 37.7k | FILE *fp = fopen(filename, "wb"); |
34 | 37.7k | if (!fp) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | 37.7k | fwrite(data, size, 1, fp); |
38 | 37.7k | fclose(fp); |
39 | | |
40 | 37.7k | Dwarf_Debug dbg = 0; |
41 | 37.7k | int fuzz_fd = 0; |
42 | 37.7k | int res = DW_DLV_ERROR; |
43 | 37.7k | Dwarf_Error error = 0; |
44 | 37.7k | Dwarf_Handler errhand = 0; |
45 | 37.7k | Dwarf_Ptr errarg = 0; |
46 | 37.7k | Dwarf_Sig8 hash8; |
47 | 37.7k | Dwarf_Error *errp = 0; |
48 | 37.7k | int simpleerrhand = 0; |
49 | 37.7k | int i = 0; |
50 | 37.7k | Dwarf_Die die; |
51 | | |
52 | 37.7k | fuzz_fd = open(filename, O_RDONLY|O_BINARY); |
53 | 37.7k | if (fuzz_fd != -1) { |
54 | 37.7k | res = |
55 | 37.7k | dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp); |
56 | 37.7k | if (res == DW_DLV_OK) { |
57 | 16.6k | Dwarf_Bool is_info = 0; |
58 | 16.6k | Dwarf_Unsigned cu_header_length = 0; |
59 | 16.6k | Dwarf_Half version_stamp = 0; |
60 | 16.6k | Dwarf_Off abbrev_offset = 0; |
61 | 16.6k | Dwarf_Half address_size = 0; |
62 | 16.6k | Dwarf_Half length_size = 0; |
63 | 16.6k | Dwarf_Half extension_size = 0; |
64 | 16.6k | Dwarf_Sig8 type_signature; |
65 | 16.6k | Dwarf_Unsigned typeoffset = 0; |
66 | 16.6k | Dwarf_Unsigned next_cu_header_offset = 0; |
67 | 16.6k | Dwarf_Half header_cu_type = 0; |
68 | 16.6k | int res = 0; |
69 | 16.6k | Dwarf_Die cu_die = 0; |
70 | 16.6k | int level = 0; |
71 | 16.6k | static const Dwarf_Sig8 zerosignature; |
72 | | |
73 | 16.6k | type_signature = zerosignature; |
74 | 16.6k | res = dwarf_next_cu_header_d( |
75 | 16.6k | dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset, |
76 | 16.6k | &address_size, &length_size, &extension_size, &type_signature, |
77 | 16.6k | &typeoffset, &next_cu_header_offset, &header_cu_type, errp); |
78 | 16.6k | if (res == DW_DLV_OK) { |
79 | 7.40k | res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp); |
80 | 7.40k | if (res == DW_DLV_OK) { |
81 | 6.03k | examplee(dbg, cu_die, errp); |
82 | 6.03k | exampled(cu_die, errp); |
83 | 6.03k | } else { |
84 | 1.36k | } |
85 | | |
86 | 7.40k | dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
87 | 7.40k | } |
88 | 16.6k | } |
89 | 37.7k | } |
90 | 37.7k | dwarf_finish(dbg); |
91 | 37.7k | close(fuzz_fd); |
92 | 37.7k | unlink(filename); |
93 | 37.7k | return 0; |
94 | 37.7k | } |
95 | | |
96 | 4.54k | int examplee(Dwarf_Debug dbg, Dwarf_Die somedie, Dwarf_Error *error) { |
97 | 4.54k | Dwarf_Signed count = 0; |
98 | 4.54k | char **srcfiles = 0; |
99 | 4.54k | Dwarf_Signed i = 0; |
100 | 4.54k | int res = 0; |
101 | | |
102 | 4.54k | res = dwarf_srcfiles(somedie, &srcfiles, &count, error); |
103 | 4.54k | if (res != DW_DLV_OK) { |
104 | 4.20k | return res; |
105 | 4.20k | } |
106 | 3.02k | for (i = 0; i < count; ++i) { |
107 | 2.68k | dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING); |
108 | 2.68k | } |
109 | 346 | dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST); |
110 | 346 | return DW_DLV_OK; |
111 | 4.54k | } |
112 | | |
113 | 4.54k | int exampled(Dwarf_Die somedie, Dwarf_Error *error) { |
114 | 4.54k | Dwarf_Signed count = 0; |
115 | 4.54k | Dwarf_Line_Context context = 0; |
116 | 4.54k | Dwarf_Line *linebuf = 0; |
117 | 4.54k | Dwarf_Signed i = 0; |
118 | 4.54k | Dwarf_Line line; |
119 | 4.54k | Dwarf_Small table_count = 0; |
120 | 4.54k | Dwarf_Unsigned version = 0; |
121 | 4.54k | int sres = 0; |
122 | | |
123 | 4.54k | int lineheader_errcount = 0; |
124 | 4.54k | dwarf_check_lineheader_b(somedie, &lineheader_errcount, error); |
125 | 4.54k | dwarf_print_lines(somedie, error, &lineheader_errcount); |
126 | | |
127 | 4.54k | sres = dwarf_srclines_b(somedie, &version, &table_count, &context, error); |
128 | 4.54k | if (sres != DW_DLV_OK) { |
129 | 3.91k | return sres; |
130 | 3.91k | } |
131 | 630 | sres = dwarf_srclines_from_linecontext(context, &linebuf, &count, error); |
132 | 630 | if (sres != DW_DLV_OK) { |
133 | 0 | dwarf_srclines_dealloc_b(context); |
134 | 0 | return sres; |
135 | 0 | } |
136 | | |
137 | 630 | Dwarf_Line *dw_linebuf_actuals = 0; /* init by davea*/ |
138 | 630 | Dwarf_Signed dw_linecount_actuals = 0; /* init by davea*/ |
139 | | |
140 | 630 | sres = dwarf_srclines_two_level_from_linecontext( |
141 | 630 | context, &linebuf, &count, &dw_linebuf_actuals, &dw_linecount_actuals, |
142 | 630 | error); |
143 | 630 | if (sres != DW_DLV_OK) { |
144 | 0 | dwarf_srclines_dealloc_b(context); |
145 | 0 | return sres; |
146 | 0 | } |
147 | | |
148 | 630 | Dwarf_Unsigned dw_context_section_offset = 0; /* init by davea*/ |
149 | 630 | sres = |
150 | 630 | dwarf_srclines_table_offset(context, &dw_context_section_offset, error); |
151 | 630 | if (sres != DW_DLV_OK) { |
152 | 0 | dwarf_srclines_dealloc_b(context); |
153 | 0 | return sres; |
154 | 0 | } |
155 | | |
156 | 630 | const char *dw_compilation_directory = 0; /* init by davea*/ |
157 | 630 | sres = dwarf_srclines_comp_dir(context, &dw_compilation_directory, error); |
158 | 630 | if (sres != DW_DLV_OK) { |
159 | 0 | dwarf_srclines_dealloc_b(context); |
160 | 0 | return sres; |
161 | 0 | } |
162 | | |
163 | 630 | Dwarf_Signed subprogram_count = 0; /* init by davea*/ |
164 | 630 | sres = dwarf_srclines_subprog_count(context, |
165 | 630 | &subprogram_count, error); |
166 | 630 | if (sres != DW_DLV_OK) { |
167 | 0 | dwarf_srclines_dealloc_b(context); |
168 | 0 | return sres; |
169 | 0 | } |
170 | | |
171 | 630 | Dwarf_Unsigned version_2 = 0; /* init by davea*/ |
172 | 630 | Dwarf_Small table_count_2 = 0; /* init by davea*/ |
173 | 630 | dwarf_srclines_version(context, &version_2, &table_count_2, error); |
174 | | |
175 | 630 | Dwarf_Signed dw_baseindex = 0; /* init by davea*/ |
176 | 630 | Dwarf_Signed dw_count = 0; /* init by davea*/ |
177 | 630 | Dwarf_Signed dw_endindex = 0; /* init by davea*/ |
178 | 630 | sres = dwarf_srclines_files_indexes(context, |
179 | 630 | &dw_baseindex, &dw_count, |
180 | 630 | &dw_endindex, error); |
181 | 630 | if (sres != DW_DLV_OK) { |
182 | 0 | dwarf_srclines_dealloc_b(context); |
183 | 0 | return sres; |
184 | 0 | } |
185 | | |
186 | 5.24k | for (i = 0; i < subprogram_count; i++) { |
187 | 4.61k | const char *dw_name = 0; /* init by davea*/ |
188 | 4.61k | Dwarf_Unsigned dw_decl_file = 0; /* init by davea*/ |
189 | 4.61k | Dwarf_Unsigned dw_decl_line = 0; /* init by davea*/ |
190 | 4.61k | sres = dwarf_srclines_subprog_data(context, i + 1, |
191 | 4.61k | &dw_name, &dw_decl_file, |
192 | 4.61k | &dw_decl_line, error); |
193 | 4.61k | if (sres != DW_DLV_OK) { |
194 | 0 | continue; |
195 | 0 | } |
196 | 4.61k | } |
197 | | |
198 | 174k | for (i = 0; i < count; ++i) { |
199 | 173k | line = linebuf[i]; |
200 | | |
201 | 173k | Dwarf_Bool ans = 0; /* init by davea */ |
202 | 173k | Dwarf_Unsigned linenum = 0; /* init by davea */ |
203 | 173k | dwarf_linebeginstatement(line, &ans, error); |
204 | 173k | dwarf_lineendsequence(line, &ans, error); |
205 | 173k | dwarf_line_is_addr_set(line, &ans, error); |
206 | | |
207 | 173k | dwarf_lineno(line, &linenum, error); |
208 | 173k | dwarf_line_srcfileno(line, &linenum, error); |
209 | 173k | dwarf_lineoff_b(line, &linenum, error); |
210 | | |
211 | 173k | char *linesrc = 0; /* INIT by davea */ |
212 | | |
213 | 173k | dwarf_linesrc(line, &linesrc, error); |
214 | | |
215 | 173k | Dwarf_Bool prologue_end = 0; /* init by davea*/ |
216 | 173k | Dwarf_Bool epilogue_begin = 0; /* init by davea*/ |
217 | 173k | Dwarf_Unsigned isa = 0; /* init by davea*/ |
218 | 173k | Dwarf_Unsigned discriminator = 0; /* init by davea*/ |
219 | 173k | dwarf_prologue_end_etc(line, &prologue_end, &epilogue_begin, &isa, |
220 | 173k | &discriminator, error); |
221 | | |
222 | 173k | #if 1 /* this is problematic and does not work */ |
223 | 173k | Dwarf_Unsigned l_logical = 0; /* init by davea*/ |
224 | 173k | dwarf_linelogical(line, &l_logical, error); |
225 | | |
226 | 173k | Dwarf_Unsigned subprog_no = 0; /* init by davea*/ |
227 | 173k | dwarf_line_subprogno(line, &subprog_no, error); |
228 | 173k | #endif |
229 | 173k | } |
230 | 630 | dwarf_srclines_dealloc_b(context); |
231 | 630 | return DW_DLV_OK; |
232 | 630 | } |