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