Debian Codegraph @

fuzz/fuzz_debug_addr_access.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 int fuzz_fd = 0;
46 Dwarf_Ptr errarg = 0;
47 Dwarf_Handler errhand = 0;
48 Dwarf_Error *errp = 0;
49 Dwarf_Debug dbg = 0;
50
51 fuzz_fd = open(filename, O_RDONLY|O_BINARY);
52 if (fuzz_fd != -1) {
53 int res =
54 dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
55
56 if (res != DW_DLV_OK) {
57 dwarf_finish(dbg);
58 close(fuzz_fd);
59 unlink(filename);
60 return 0;
61 }
62 Dwarf_Debug_Addr_Table debug_address_table;
63 Dwarf_Unsigned dw_section_offset = 0;
64 Dwarf_Unsigned dw_debug_address_table_length = 0;
65 Dwarf_Half dw_version;
66 Dwarf_Small dw_address_size;
67 Dwarf_Unsigned dw_dw_at_addr_base;
68 Dwarf_Unsigned dw_entry_count;
69 Dwarf_Unsigned dw_next_table_offset;
70 res = dwarf_debug_addr_table(dbg, dw_section_offset, &debug_address_table,
71 &dw_debug_address_table_length, &dw_version,
72 &dw_address_size, &dw_dw_at_addr_base,
73 &dw_entry_count, &dw_next_table_offset, errp);
74
75 if (res != DW_DLV_OK) {
76 if (res == DW_DLV_ERROR) {
77 if (errp) {
78 dwarf_dealloc_error(dbg, *errp);
79 }
80 }
81 dwarf_finish(dbg);
82 close(fuzz_fd);
83 unlink(filename);
84 return 0;
85 }
86 for (Dwarf_Unsigned curindex = 0; curindex < dw_entry_count; ++curindex) {
87 Dwarf_Unsigned addr = 0;
88 res =
89 dwarf_debug_addr_by_index(debug_address_table, curindex, &addr, errp);
90
91 if (res != DW_DLV_OK) {
92 break;
93 }
94 }
95 if (errp) {
96 dwarf_dealloc_error(dbg, *errp);
97 }
98 dwarf_dealloc_debug_addr_table(debug_address_table);
99 dwarf_finish(dbg);
100 close(fuzz_fd);
101 }
102
103 unlink(filename);
104 return 0;
105 }