Debian Codegraph @

fuzz/fuzz_die_cu_attrs_loclist.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
21 /*
22 * Libdwarf library callers can only use these headers.
23 */
24 #include "dwarf.h"
25 #include "libdwarf.h"
26
27 #ifndef O_BINARY
28 #define O_BINARY 0
29 #endif
30
31 /*
32 * Fuzzer function
33 */
34 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35 char filename[256];
36 sprintf(filename, "/tmp/libfuzzer.%d", getpid());
37
38 FILE *fp = fopen(filename, "wb");
39 if (!fp) {
40 return 0;
41 }
42 fwrite(data, size, 1, fp);
43 fclose(fp);
44
45 Dwarf_Debug dbg = 0;
46 int fuzz_fd = 0;
47 int res = DW_DLV_ERROR;
48 Dwarf_Error error = 0;
49 Dwarf_Handler errhand = 0;
50 Dwarf_Ptr errarg = 0;
51 Dwarf_Error *errp = 0;
52 int i = 0;
53 Dwarf_Die die = 0;
54
55 fuzz_fd = open(filename, O_RDONLY | O_BINARY);
56 if (fuzz_fd != -1) {
57 res =
58 dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
59 if (res == DW_DLV_OK) {
60 Dwarf_Bool is_info = 0;
61 Dwarf_Unsigned cu_header_length = 0;
62 Dwarf_Half version_stamp = 0;
63 Dwarf_Off abbrev_offset = 0;
64 Dwarf_Half address_size = 0;
65 Dwarf_Half length_size = 0;
66 Dwarf_Half extension_size = 0;
67 Dwarf_Sig8 type_signature;
68 Dwarf_Unsigned typeoffset = 0;
69 Dwarf_Unsigned next_cu_header_offset = 0;
70 Dwarf_Half header_cu_type = 0;
71 Dwarf_Die cu_die = 0;
72 static const Dwarf_Sig8 zerosignature;
73
74 type_signature = zerosignature;
75 res = dwarf_next_cu_header_d(
76 dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
77 &address_size, &length_size, &extension_size, &type_signature,
78 &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
79 if (res == DW_DLV_OK) {
80 res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
81 if (res == DW_DLV_OK) {
82 Dwarf_Attribute *atlist = 0;
83 Dwarf_Signed atcount = 0;
84 Dwarf_Attribute attr = 0;
85
86 res = dwarf_attr(cu_die, DW_AT_name, &attr, errp);
87 res = dwarf_attrlist(cu_die, &atlist, &atcount, &error);
88
89 Dwarf_Loc_Head_c loclisthead = 0;
90 Dwarf_Unsigned loc_count = 0;
91 Dwarf_Unsigned i = 0;
92 Dwarf_Unsigned meaninglesstotal = 0;
93
94 res = dwarf_get_loclist_c(attr, &loclisthead, &loc_count, errp);
95 if (res == DW_DLV_OK) {
96
97 Dwarf_Addr localhighpc = 0;
98 Dwarf_Half form = 0;
99 enum Dwarf_Form_Class formclass = DW_FORM_CLASS_UNKNOWN;
100
101 res = dwarf_highpc_b(die, &localhighpc, &form, &formclass, errp);
102
103 if (form != DW_FORM_addr && !dwarf_addr_form_is_indexed(form)) {
104 Dwarf_Addr low_pc = 0;
105 res = dwarf_lowpc(die, &low_pc, errp);
106 if (res != DW_DLV_OK) {
107 dwarf_finish(dbg);
108 close(fuzz_fd);
109 unlink(filename);
110 return res;
111 } else {
112 localhighpc += low_pc;
113 }
114 }
115 for (i = 0; i < loc_count; ++i) {
116 Dwarf_Small loclist_lkind = 0;
117 Dwarf_Small lle_value = 0;
118 Dwarf_Unsigned rawval1 = 0;
119 Dwarf_Unsigned rawval2 = 0;
120 Dwarf_Bool debug_addr_unavailable = 0;
121 Dwarf_Addr lopc = 0;
122 Dwarf_Addr hipc = 0;
123 Dwarf_Unsigned loclist_expr_op_count = 0;
124 Dwarf_Locdesc_c locdesc_entry = 0;
125 Dwarf_Unsigned expression_offset = 0;
126 Dwarf_Unsigned locdesc_offset = 0;
127
128 res = dwarf_get_locdesc_entry_d(
129 loclisthead, i, &lle_value, &rawval1, &rawval2,
130 &debug_addr_unavailable, &lopc, &hipc, &loclist_expr_op_count,
131 &locdesc_entry, &loclist_lkind, &expression_offset,
132 &locdesc_offset, errp);
133 if (res == DW_DLV_OK) {
134 Dwarf_Unsigned j = 0;
135 int opres = 0;
136 Dwarf_Small op = 0;
137
138 for (j = 0; j < loclist_expr_op_count; ++j) {
139 Dwarf_Unsigned opd1 = 0;
140 Dwarf_Unsigned opd2 = 0;
141 Dwarf_Unsigned opd3 = 0;
142 Dwarf_Unsigned offsetforbranch = 0;
143
144 opres = dwarf_get_location_op_value_c(locdesc_entry, j, &op,
145 &opd1, &opd2, &opd3,
146 &offsetforbranch, errp);
147 }
148 }
149 }
150 dwarf_dealloc_loc_head_c(loclisthead);
151 }
152 }
153 dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
154 }
155 }
156 }
157 dwarf_finish(dbg);
158 close(fuzz_fd);
159 unlink(filename);
160 return 0;
161 }