| 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 get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error); |
| 31 |
int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error); |
| 32 |
int get_globals_example(Dwarf_Debug dbg, 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 |
|
| 54 |
int fd = open(filename, O_RDONLY | O_BINARY); |
| 55 |
if (fd < 0) { |
| 56 |
exit(EXIT_FAILURE); |
| 57 |
} |
| 58 |
|
| 59 |
res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
| 60 |
|
| 61 |
if (res != DW_DLV_OK) { |
| 62 |
dwarf_dealloc_error(dbg, error); |
| 63 |
} else { |
| 64 |
dwarf_return_empty_pubnames(dbg, 1); |
| 65 |
dwarf_return_empty_pubnames(dbg, 0); |
| 66 |
get_globals_example(dbg, &error); |
| 67 |
get_globals_by_type_example(dbg, &error); |
| 68 |
} |
| 69 |
|
| 70 |
dwarf_finish(dbg); |
| 71 |
close(fd); |
| 72 |
unlink(filename); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
| 77 |
Dwarf_Signed count = 0; |
| 78 |
Dwarf_Global *globs = 0; |
| 79 |
Dwarf_Signed i = 0; |
| 80 |
int res = 0; |
| 81 |
|
| 82 |
res = dwarf_get_globals(dbg, &globs, &count, error); |
| 83 |
if (res != DW_DLV_OK) { |
| 84 |
return res; |
| 85 |
} |
| 86 |
for (i = 0; i < count; ++i) { |
| 87 |
int tag_idx = dwarf_global_tag_number(globs[i]); // DWARF5 only |
| 88 |
|
| 89 |
char *name = 0; |
| 90 |
res = dwarf_globname(globs[i], &name, error); |
| 91 |
if (res != DW_DLV_OK) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
Dwarf_Off dw_die_offset; |
| 96 |
res = dwarf_global_die_offset(globs[i], &dw_die_offset, error); |
| 97 |
if (res != DW_DLV_OK) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
Dwarf_Off dw_cu_offset; |
| 101 |
res = dwarf_global_cu_offset(globs[i], &dw_cu_offset, error); |
| 102 |
if (res != DW_DLV_OK) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
char *name_2; |
| 107 |
Dwarf_Off dw_die_offset_2, dw_cu_offset_2; |
| 108 |
dwarf_global_name_offsets(globs[i], &name_2, &dw_die_offset_2, |
| 109 |
&dw_cu_offset_2, error); |
| 110 |
if (res != DW_DLV_OK) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
int dw_category; |
| 115 |
Dwarf_Off dw_offset_pub_header; |
| 116 |
Dwarf_Unsigned dw_length_size; |
| 117 |
Dwarf_Unsigned dw_length_pub; |
| 118 |
Dwarf_Unsigned dw_version; |
| 119 |
Dwarf_Unsigned dw_header_info_offset; |
| 120 |
Dwarf_Unsigned dw_info_length; |
| 121 |
res = dwarf_get_globals_header( |
| 122 |
globs[i], &dw_category, &dw_offset_pub_header, &dw_length_size, |
| 123 |
&dw_length_pub, &dw_version, &dw_header_info_offset, &dw_info_length, |
| 124 |
error); |
| 125 |
} |
| 126 |
dwarf_globals_dealloc(dbg, globs, count); |
| 127 |
return DW_DLV_OK; |
| 128 |
} |
| 129 |
|
| 130 |
/* DWARF4 */ |
| 131 |
int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
| 132 |
int res = DW_DLV_OK; |
| 133 |
for (int i = 0; i < 6; i++) { |
| 134 |
Dwarf_Signed count = 0; |
| 135 |
Dwarf_Global *contents = 0; |
| 136 |
Dwarf_Signed i = 0; |
| 137 |
|
| 138 |
res = dwarf_globals_by_type(dbg, i, &contents, &count, error); |
| 139 |
|
| 140 |
dwarf_globals_dealloc(dbg, contents, count); |
| 141 |
} |
| 142 |
|
| 143 |
return res; |
| 144 |
} |
| 145 |
|
| 146 |
/* DWARF4 */ |
| 147 |
int get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
| 148 |
Dwarf_Signed count = 0; |
| 149 |
Dwarf_Global *contents = 0; |
| 150 |
|
| 151 |
int res = dwarf_get_pubtypes(dbg, &contents, &count, error); |
| 152 |
|
| 153 |
dwarf_globals_dealloc(dbg, contents, count); |
| 154 |
|
| 155 |
return res; |
| 156 |
} |