| 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 |
#ifndef O_BINARY |
| 27 |
#define O_BINARY 0 |
| 28 |
#endif |
| 29 |
|
| 30 |
/* |
| 31 |
* Fuzzer function |
| 32 |
*/ |
| 33 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 34 |
char filename[256]; |
| 35 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 36 |
|
| 37 |
FILE *fp = fopen(filename, "wb"); |
| 38 |
if (!fp) { |
| 39 |
return 0; |
| 40 |
} |
| 41 |
fwrite(data, size, 1, fp); |
| 42 |
fclose(fp); |
| 43 |
|
| 44 |
Dwarf_Debug dbg = 0; |
| 45 |
int fuzz_fd = 0; |
| 46 |
int res = DW_DLV_ERROR; |
| 47 |
Dwarf_Error error = 0; |
| 48 |
Dwarf_Handler errhand = 0; |
| 49 |
Dwarf_Ptr errarg = 0; |
| 50 |
Dwarf_Error *errp = 0; |
| 51 |
int i = 0; |
| 52 |
Dwarf_Die die = 0; |
| 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 |
Dwarf_Die cu_die = 0; |
| 71 |
static const Dwarf_Sig8 zerosignature; |
| 72 |
|
| 73 |
type_signature = zerosignature; |
| 74 |
res = dwarf_next_cu_header_d( |
| 75 |
dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset, |
| 76 |
&address_size, &length_size, &extension_size, &type_signature, |
| 77 |
&typeoffset, &next_cu_header_offset, &header_cu_type, errp); |
| 78 |
if (res == DW_DLV_OK) { |
| 79 |
res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp); |
| 80 |
if (res == DW_DLV_OK) { |
| 81 |
Dwarf_Unsigned unsign = 0; |
| 82 |
res = dwarf_bytesize(cu_die, &unsign, errp); |
| 83 |
res = dwarf_bitsize(cu_die, &unsign, errp); |
| 84 |
res = dwarf_arrayorder(cu_die, &unsign, errp); |
| 85 |
|
| 86 |
Dwarf_Off section_offset = 0; |
| 87 |
Dwarf_Off local_offset = 0; |
| 88 |
res = dwarf_die_offsets(cu_die, §ion_offset, &local_offset, errp); |
| 89 |
|
| 90 |
Dwarf_Off off = 0; |
| 91 |
res = dwarf_dietype_offset(die, &off, &is_info, &error); |
| 92 |
|
| 93 |
Dwarf_Off agoff = 0; |
| 94 |
Dwarf_Unsigned acount = 0; |
| 95 |
res = dwarf_die_abbrev_global_offset(cu_die, &agoff, &acount, errp); |
| 96 |
|
| 97 |
Dwarf_Bool someinfo = dwarf_get_die_infotypes_flag(cu_die); |
| 98 |
|
| 99 |
Dwarf_Off globaloff = 0; |
| 100 |
Dwarf_Unsigned length = 0; |
| 101 |
res = dwarf_die_CU_offset_range(cu_die, &globaloff, &length, errp); |
| 102 |
|
| 103 |
Dwarf_Half address_size = 0; |
| 104 |
res = dwarf_get_address_size(dbg, &address_size, errp); |
| 105 |
|
| 106 |
Dwarf_Unsigned offcnt = 0; |
| 107 |
Dwarf_Off *offbuf = 0; |
| 108 |
res = dwarf_offset_list(dbg, section_offset, is_info, &offbuf, |
| 109 |
&offcnt, errp); |
| 110 |
|
| 111 |
Dwarf_Off die_goff = 0; |
| 112 |
res = dwarf_dieoffset(cu_die, &die_goff, errp); |
| 113 |
|
| 114 |
int abbrev_code = dwarf_die_abbrev_code(cu_die); |
| 115 |
|
| 116 |
Dwarf_Bool has_attr = 0; |
| 117 |
res = dwarf_hasattr(cu_die, DW_AT_external, &has_attr, errp); |
| 118 |
|
| 119 |
Dwarf_Bool is_dwo = 0; |
| 120 |
Dwarf_Half offset_size = 0; |
| 121 |
Dwarf_Off offset_of_length = 0; |
| 122 |
Dwarf_Unsigned total_byte_length = 0; |
| 123 |
Dwarf_Sig8 *typesign = 0; |
| 124 |
|
| 125 |
res = dwarf_cu_header_basics( |
| 126 |
cu_die, &version_stamp, &is_info, &is_dwo, &offset_size, |
| 127 |
&address_size, &extension_size, &typesign, &offset_of_length, |
| 128 |
&total_byte_length, errp); |
| 129 |
|
| 130 |
Dwarf_Debug_Fission_Per_CU percu; |
| 131 |
memset(&percu, 0, sizeof(percu)); |
| 132 |
res = dwarf_get_debugfission_for_die(cu_die, &percu, errp); |
| 133 |
char *name = 0; |
| 134 |
Dwarf_Half tag = 0; |
| 135 |
const char *tagname = 0; |
| 136 |
int res = 0; |
| 137 |
Dwarf_Attribute *atlist = 0; |
| 138 |
Dwarf_Signed atcount = 0; |
| 139 |
Dwarf_Attribute attr = 0; |
| 140 |
Dwarf_Half formnum = 0; |
| 141 |
const char *formname = "form-name-unavailable"; |
| 142 |
|
| 143 |
if (res == DW_DLV_ERROR || res == DW_DLV_NO_ENTRY) { |
| 144 |
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
| 145 |
dwarf_finish(dbg); |
| 146 |
close(fuzz_fd); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
res = dwarf_diename(cu_die, &name, errp); |
| 151 |
|
| 152 |
res = dwarf_get_TAG_name(tag, &tagname); |
| 153 |
|
| 154 |
res = dwarf_attr(cu_die, DW_AT_name, &attr, errp); |
| 155 |
res = dwarf_attrlist(cu_die, &atlist, &atcount, errp); |
| 156 |
for (i = 0; i < atcount; ++i) { |
| 157 |
Dwarf_Half attrnum = 0; |
| 158 |
const char *attrname = 0; |
| 159 |
res = dwarf_whatform(attr, &formnum, errp); |
| 160 |
Dwarf_Bool *dw_returned_bool = 0; |
| 161 |
/* This next call is incorrect! libdwarf now returns |
| 162 |
DW_DLV_ERROR */ |
| 163 |
res = dwarf_hasform(attr, formnum, dw_returned_bool, errp); |
| 164 |
res = dwarf_get_FORM_name(formnum, &formname); |
| 165 |
Dwarf_Block *tempb = 0; |
| 166 |
res = dwarf_formblock(attr, &tempb, errp); |
| 167 |
if (res == DW_DLV_OK) { |
| 168 |
Dwarf_Dsc_Head h = 0; |
| 169 |
/* Dwarf_Unsigned u = 0; unused */ |
| 170 |
Dwarf_Unsigned arraycount = 0; |
| 171 |
int sres = 0; |
| 172 |
|
| 173 |
sres = dwarf_discr_list(dbg, (Dwarf_Small *)tempb->bl_data, |
| 174 |
tempb->bl_len, &h, &arraycount, errp); |
| 175 |
} |
| 176 |
res = dwarf_whatattr(atlist[i], &attrnum, errp); |
| 177 |
dwarf_get_AT_name(attrnum, &attrname); |
| 178 |
dwarf_dealloc_attribute(atlist[i]); |
| 179 |
atlist[i] = 0; |
| 180 |
char *stringval = 0; |
| 181 |
res = dwarf_bitoffset(cu_die, &attrnum, &unsign, errp); |
| 182 |
res = dwarf_die_text(cu_die, attrnum, &stringval, errp); |
| 183 |
res = dwarf_get_form_class(version_stamp, attrnum, |
| 184 |
next_cu_header_offset, formnum); |
| 185 |
} |
| 186 |
res = dwarf_set_tied_dbg(dbg, NULL, errp); |
| 187 |
} |
| 188 |
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
dwarf_finish(dbg); |
| 193 |
close(fuzz_fd); |
| 194 |
unlink(filename); |
| 195 |
return 0; |
| 196 |
} |