Debian Codegraph @

fuzz/fuzz_macro_dwarf5.c

raw
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/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include "dwarf.h"
21 #include "libdwarf.h"
22
23 #define TRUE 1
24 #define FALSE 0
25 #ifndef O_BINARY
26 #define O_BINARY 0
27 #endif
28
29 int examplep5(Dwarf_Die cu_die, Dwarf_Error *error);
30
31 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32 char filename[256];
33 sprintf(filename, "/tmp/libfuzzer.%d", getpid());
34
35 FILE *fp = fopen(filename, "wb");
36 if (!fp) {
37 return 0;
38 }
39 fwrite(data, size, 1, fp);
40 fclose(fp);
41
42 Dwarf_Debug dbg = 0;
43 int fuzz_fd = 0;
44 int res = DW_DLV_ERROR;
45 Dwarf_Error error = 0;
46 Dwarf_Handler errhand = 0;
47 Dwarf_Ptr errarg = 0;
48 Dwarf_Sig8 hash8;
49 Dwarf_Error *errp = 0;
50 int simpleerrhand = 0;
51 int i = 0;
52 Dwarf_Die die;
53
54 fuzz_fd = open(filename, O_RDONLY|O_BINARY);
55 if (fuzz_fd != -1) {
56 res =
57 dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
58 if (res == DW_DLV_OK) {
59 Dwarf_Bool is_info = 0;
60 Dwarf_Unsigned cu_header_length = 0;
61 Dwarf_Half version_stamp = 0;
62 Dwarf_Off abbrev_offset = 0;
63 Dwarf_Half address_size = 0;
64 Dwarf_Half length_size = 0;
65 Dwarf_Half extension_size = 0;
66 Dwarf_Sig8 type_signature;
67 Dwarf_Unsigned typeoffset = 0;
68 Dwarf_Unsigned next_cu_header_offset = 0;
69 Dwarf_Half header_cu_type = 0;
70 int res = 0;
71 Dwarf_Die cu_die = 0;
72 int level = 0;
73 static const Dwarf_Sig8 zerosignature;
74
75 type_signature = zerosignature;
76 res = dwarf_next_cu_header_d(
77 dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
78 &address_size, &length_size, &extension_size, &type_signature,
79 &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
80 if (res == DW_DLV_OK) {
81 printf(">>CU header length..........0x%lx\n",
82 (unsigned long)cu_header_length);
83 printf(">>Version stamp.............%d\n", version_stamp);
84 printf(">>Address size .............%d\n", address_size);
85 printf(">>Offset size...............%d\n", length_size);
86 printf(">>Next cu header offset.....0x%lx\n",
87 (unsigned long)next_cu_header_offset);
88 res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
89 if (res == DW_DLV_OK) {
90 examplep5(cu_die, &error);
91 };
92 dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
93 }
94 }
95 }
96 dwarf_finish(dbg);
97 close(fuzz_fd);
98 unlink(filename);
99 return 0;
100 }
101
102 int examplep5(Dwarf_Die cu_die, Dwarf_Error *error) {
103 int lres = 0;
104 Dwarf_Unsigned version = 0;
105 Dwarf_Macro_Context macro_context = 0;
106 Dwarf_Unsigned macro_unit_offset = 0;
107 Dwarf_Unsigned number_of_ops = 0;
108 Dwarf_Unsigned ops_total_byte_len = 0;
109 Dwarf_Bool is_primary = TRUE;
110 unsigned k = 0;
111
112 /* Just call once each way to test both.
113 Really the second is just for imported units,
114 but this is a way to get testing of both calls.
115 */
116 for (; k < 2 ;++k) {
117 if (is_primary) {
118 lres = dwarf_get_macro_context(cu_die, &version, &macro_context,
119 &macro_unit_offset, &number_of_ops,
120 &ops_total_byte_len, error);
121 is_primary = FALSE;
122 } else {
123 lres = dwarf_get_macro_context_by_offset(
124 cu_die, macro_unit_offset, &version, &macro_context, &number_of_ops,
125 &ops_total_byte_len, error);
126 }
127
128 if (lres == DW_DLV_ERROR) {
129 return lres;
130 }
131 if (lres == DW_DLV_NO_ENTRY) {
132 break;
133 }
134 for (k = 0; k < number_of_ops; ++k) {
135 Dwarf_Unsigned section_offset = 0;
136 Dwarf_Half macro_operator = 0;
137 Dwarf_Half forms_count = 0;
138 const Dwarf_Small *formcode_array = 0;
139 Dwarf_Unsigned line_number = 0;
140 Dwarf_Unsigned index = 0;
141 Dwarf_Unsigned offset = 0;
142 const char *macro_string = 0;
143 int lres2 = 0;
144
145 lres2 =
146 dwarf_get_macro_op(macro_context, k, &section_offset, &macro_operator,
147 &forms_count, &formcode_array, error);
148 if (lres2 != DW_DLV_OK) {
149 dwarf_dealloc_macro_context(macro_context);
150 return lres2;
151 }
152 switch (macro_operator) {
153 case 0:
154 break;
155 case DW_MACRO_end_file:
156 break;
157 case DW_MACRO_define:
158 case DW_MACRO_undef:
159 case DW_MACRO_define_strp:
160 case DW_MACRO_undef_strp:
161 case DW_MACRO_define_strx:
162 case DW_MACRO_undef_strx:
163 case DW_MACRO_define_sup:
164 case DW_MACRO_undef_sup: {
165 lres2 = dwarf_get_macro_defundef(macro_context, k, &line_number, &index,
166 &offset, &forms_count, &macro_string,
167 error);
168 if (lres2 != DW_DLV_OK) {
169 dwarf_dealloc_macro_context(macro_context);
170 return lres2;
171 }
172 } break;
173 case DW_MACRO_start_file: {
174 lres2 = dwarf_get_macro_startend_file(macro_context, k, &line_number,
175 &index, &macro_string, error);
176 if (lres2 != DW_DLV_OK) {
177 dwarf_dealloc_macro_context(macro_context);
178 return lres2;
179 }
180 } break;
181 case DW_MACRO_import: {
182 lres2 = dwarf_get_macro_import(macro_context, k, &offset, error);
183 if (lres2 != DW_DLV_OK) {
184 dwarf_dealloc_macro_context(macro_context);
185 return lres2;
186 }
187 } break;
188 case DW_MACRO_import_sup: {
189 lres2 = dwarf_get_macro_import(macro_context, k, &offset, error);
190 if (lres2 != DW_DLV_OK) {
191 dwarf_dealloc_macro_context(macro_context);
192 return lres2;
193 }
194 } break;
195 default:
196 break;
197 }
198 }
199 dwarf_dealloc_macro_context(macro_context);
200 macro_context = 0;
201 }
202 return DW_DLV_OK;
203 }