| 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 |
#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 examplew(Dwarf_Debug dbg, Dwarf_Error *error); |
| 31 |
int examplewgdbindex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error); |
| 32 |
int examplex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error); |
| 33 |
|
| 34 |
/* |
| 35 |
* A fuzzer that simulates a small part of the simplereader.c example. |
| 36 |
*/ |
| 37 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 38 |
char filename[256]; |
| 39 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 40 |
|
| 41 |
FILE *fp = fopen(filename, "wb"); |
| 42 |
if (!fp) { |
| 43 |
return 0; |
| 44 |
} |
| 45 |
fwrite(data, size, 1, fp); |
| 46 |
fclose(fp); |
| 47 |
|
| 48 |
Dwarf_Debug dbg = 0; |
| 49 |
int res = DW_DLV_ERROR; |
| 50 |
Dwarf_Error error = 0; |
| 51 |
Dwarf_Handler errhand = 0; |
| 52 |
Dwarf_Ptr errarg = 0; |
| 53 |
int regtabrulecount = 0; |
| 54 |
int curopt = 0; |
| 55 |
|
| 56 |
int fd = open(filename, O_RDONLY | O_BINARY); |
| 57 |
if (fd < 0) { |
| 58 |
exit(EXIT_FAILURE); |
| 59 |
} |
| 60 |
|
| 61 |
res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
| 62 |
|
| 63 |
if (res != DW_DLV_OK) { |
| 64 |
dwarf_dealloc_error(dbg, error); |
| 65 |
} else { |
| 66 |
examplew(dbg, &error); |
| 67 |
} |
| 68 |
|
| 69 |
dwarf_finish(dbg); |
| 70 |
close(fd); |
| 71 |
unlink(filename); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int examplew(Dwarf_Debug dbg, Dwarf_Error *error) { |
| 76 |
Dwarf_Gdbindex gindexptr = 0; |
| 77 |
Dwarf_Unsigned version = 0; |
| 78 |
Dwarf_Unsigned cu_list_offset = 0; |
| 79 |
Dwarf_Unsigned types_cu_list_offset = 0; |
| 80 |
Dwarf_Unsigned address_area_offset = 0; |
| 81 |
Dwarf_Unsigned symbol_table_offset = 0; |
| 82 |
Dwarf_Unsigned constant_pool_offset = 0; |
| 83 |
Dwarf_Unsigned section_size = 0; |
| 84 |
const char *section_name = 0; |
| 85 |
int res = 0; |
| 86 |
|
| 87 |
res = dwarf_gdbindex_header(dbg, &gindexptr, &version, &cu_list_offset, |
| 88 |
&types_cu_list_offset, &address_area_offset, |
| 89 |
&symbol_table_offset, &constant_pool_offset, |
| 90 |
§ion_size, §ion_name, error); |
| 91 |
if (res != DW_DLV_OK) { |
| 92 |
return res; |
| 93 |
} |
| 94 |
{ |
| 95 |
Dwarf_Unsigned length = 0; |
| 96 |
Dwarf_Unsigned typeslength = 0; |
| 97 |
Dwarf_Unsigned i = 0; |
| 98 |
res = dwarf_gdbindex_culist_array(gindexptr, &length, error); |
| 99 |
if (res != DW_DLV_OK) { |
| 100 |
dwarf_dealloc_gdbindex(gindexptr); |
| 101 |
return res; |
| 102 |
} |
| 103 |
for (i = 0; i < length; ++i) { |
| 104 |
Dwarf_Unsigned cuoffset = 0; |
| 105 |
Dwarf_Unsigned culength = 0; |
| 106 |
res = dwarf_gdbindex_culist_entry(gindexptr, i, &cuoffset, &culength, |
| 107 |
error); |
| 108 |
if (res != DW_DLV_OK) { |
| 109 |
return res; |
| 110 |
} |
| 111 |
} |
| 112 |
res = dwarf_gdbindex_types_culist_array(gindexptr, &typeslength, error); |
| 113 |
if (res != DW_DLV_OK) { |
| 114 |
dwarf_dealloc_gdbindex(gindexptr); |
| 115 |
return res; |
| 116 |
} |
| 117 |
for (i = 0; i < typeslength; ++i) { |
| 118 |
Dwarf_Unsigned cuoffset = 0; |
| 119 |
Dwarf_Unsigned tuoffset = 0; |
| 120 |
Dwarf_Unsigned type_signature = 0; |
| 121 |
res = dwarf_gdbindex_types_culist_entry( |
| 122 |
gindexptr, i, &cuoffset, &tuoffset, &type_signature, error); |
| 123 |
if (res != DW_DLV_OK) { |
| 124 |
dwarf_dealloc_gdbindex(gindexptr); |
| 125 |
return res; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
res = examplewgdbindex(gindexptr, error); |
| 130 |
if (res != DW_DLV_OK) { |
| 131 |
dwarf_dealloc_gdbindex(gindexptr); |
| 132 |
return res; |
| 133 |
} |
| 134 |
|
| 135 |
res = examplex(gindexptr, error); |
| 136 |
if (res != DW_DLV_OK) { |
| 137 |
dwarf_dealloc_gdbindex(gindexptr); |
| 138 |
return res; |
| 139 |
} |
| 140 |
|
| 141 |
dwarf_dealloc_gdbindex(gindexptr); |
| 142 |
} |
| 143 |
return DW_DLV_OK; |
| 144 |
} |
| 145 |
|
| 146 |
int examplewgdbindex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error) { |
| 147 |
Dwarf_Unsigned list_len = 0; |
| 148 |
Dwarf_Unsigned i = 0; |
| 149 |
int res = 0; |
| 150 |
|
| 151 |
res = dwarf_gdbindex_addressarea(gdbindex, &list_len, error); |
| 152 |
if (res != DW_DLV_OK) { |
| 153 |
return res; |
| 154 |
} |
| 155 |
for (i = 0; i < list_len; i++) { |
| 156 |
Dwarf_Unsigned lowpc = 0; |
| 157 |
Dwarf_Unsigned highpc = 0; |
| 158 |
Dwarf_Unsigned cu_index = 0; |
| 159 |
res = dwarf_gdbindex_addressarea_entry(gdbindex, i, &lowpc, &highpc, |
| 160 |
&cu_index, error); |
| 161 |
if (res != DW_DLV_OK) { |
| 162 |
return res; |
| 163 |
} |
| 164 |
} |
| 165 |
return DW_DLV_OK; |
| 166 |
} |
| 167 |
|
| 168 |
int examplex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error) { |
| 169 |
Dwarf_Unsigned symtab_list_length = 0; |
| 170 |
Dwarf_Unsigned i = 0; |
| 171 |
int res = 0; |
| 172 |
|
| 173 |
res = dwarf_gdbindex_symboltable_array(gdbindex, &symtab_list_length, error); |
| 174 |
if (res != DW_DLV_OK) { |
| 175 |
return res; |
| 176 |
} |
| 177 |
for (i = 0; i < symtab_list_length; i++) { |
| 178 |
Dwarf_Unsigned symnameoffset = 0; |
| 179 |
Dwarf_Unsigned cuvecoffset = 0; |
| 180 |
Dwarf_Unsigned cuvec_len = 0; |
| 181 |
Dwarf_Unsigned ii = 0; |
| 182 |
const char *name = 0; |
| 183 |
int resl = 0; |
| 184 |
|
| 185 |
resl = dwarf_gdbindex_symboltable_entry(gdbindex, i, &symnameoffset, |
| 186 |
&cuvecoffset, error); |
| 187 |
if (resl != DW_DLV_OK) { |
| 188 |
return resl; |
| 189 |
} |
| 190 |
resl = |
| 191 |
dwarf_gdbindex_string_by_offset(gdbindex, symnameoffset, &name, error); |
| 192 |
if (resl != DW_DLV_OK) { |
| 193 |
return resl; |
| 194 |
} |
| 195 |
resl = dwarf_gdbindex_cuvector_length(gdbindex, cuvecoffset, &cuvec_len, |
| 196 |
error); |
| 197 |
if (resl != DW_DLV_OK) { |
| 198 |
return resl; |
| 199 |
} |
| 200 |
for (ii = 0; ii < cuvec_len; ++ii) { |
| 201 |
Dwarf_Unsigned attributes = 0; |
| 202 |
Dwarf_Unsigned cu_index = 0; |
| 203 |
Dwarf_Unsigned symbol_kind = 0; |
| 204 |
Dwarf_Unsigned is_static = 0; |
| 205 |
int res2 = 0; |
| 206 |
|
| 207 |
res2 = dwarf_gdbindex_cuvector_inner_attributes(gdbindex, cuvecoffset, ii, |
| 208 |
&attributes, error); |
| 209 |
if (res2 != DW_DLV_OK) { |
| 210 |
return res2; |
| 211 |
} |
| 212 |
res2 = dwarf_gdbindex_cuvector_instance_expand_value( |
| 213 |
gdbindex, attributes, &cu_index, &symbol_kind, &is_static, error); |
| 214 |
if (res2 != DW_DLV_OK) { |
| 215 |
return res2; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
return DW_DLV_OK; |
| 220 |
} |