/src/libdwarf/fuzz/fuzz_gnu_index.c
Line | Count | Source (jump to first uncovered line) |
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 | 9.23k | #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 | | /* |
31 | | * A fuzzer that simulates a small part of the simplereader.c example. |
32 | | */ |
33 | 9.23k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
34 | 9.23k | char filename[256]; |
35 | 9.23k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
36 | | |
37 | 9.23k | FILE *fp = fopen(filename, "wb"); |
38 | 9.23k | if (!fp) { |
39 | 0 | return 0; |
40 | 0 | } |
41 | 9.23k | fwrite(data, size, 1, fp); |
42 | 9.23k | fclose(fp); |
43 | | |
44 | 9.23k | Dwarf_Debug dbg = 0; |
45 | 9.23k | int res = DW_DLV_ERROR; |
46 | 9.23k | Dwarf_Error error = 0; |
47 | 9.23k | Dwarf_Handler errhand = 0; |
48 | 9.23k | Dwarf_Ptr errarg = 0; |
49 | | |
50 | 9.23k | int fd = open(filename, O_RDONLY | O_BINARY); |
51 | 9.23k | if (fd < 0) { |
52 | 0 | exit(EXIT_FAILURE); |
53 | 0 | } |
54 | | |
55 | 9.23k | res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
56 | | |
57 | 9.23k | if (res != DW_DLV_OK) { |
58 | 7.65k | if (res == DW_DLV_ERROR) { |
59 | 5.24k | } |
60 | 7.65k | dwarf_dealloc_error(dbg, error); |
61 | 7.65k | } else { |
62 | 1.57k | Dwarf_Bool dw_which_section = 0; |
63 | 1.57k | Dwarf_Gnu_Index_Head dw_head; |
64 | 1.57k | Dwarf_Unsigned dw_index_block_count; |
65 | | |
66 | 1.57k | res = dwarf_get_gnu_index_head(dbg, dw_which_section, &dw_head, |
67 | 1.57k | &dw_index_block_count, &error); |
68 | | |
69 | 1.57k | if (res == DW_DLV_NO_ENTRY) { |
70 | 1.34k | } else if (res == DW_DLV_ERROR) { |
71 | 1.30k | dwarf_dealloc_error(dbg, error); |
72 | 1.30k | } else { |
73 | 41 | Dwarf_Unsigned dw_block_length; |
74 | 41 | Dwarf_Half dw_version; |
75 | 41 | Dwarf_Unsigned dw_offset_into_debug_info; |
76 | 41 | Dwarf_Unsigned dw_size_of_debug_info_area; |
77 | 41 | Dwarf_Unsigned dw_count_of_index_entries; |
78 | 112 | for (Dwarf_Unsigned block_number = 0; block_number < dw_index_block_count; |
79 | 71 | block_number++) { |
80 | 71 | res = dwarf_get_gnu_index_block(dw_head, block_number, &dw_block_length, |
81 | 71 | &dw_version, &dw_offset_into_debug_info, |
82 | 71 | &dw_size_of_debug_info_area, |
83 | 71 | &dw_count_of_index_entries, &error); |
84 | | |
85 | 71 | if (res == DW_DLV_NO_ENTRY) { |
86 | 0 | continue; |
87 | 71 | } else if (res == DW_DLV_ERROR) { |
88 | 0 | break; |
89 | 0 | } |
90 | 71 | for (Dwarf_Unsigned entry_number = 0; |
91 | 8.45k | entry_number < dw_count_of_index_entries; entry_number++) { |
92 | 8.38k | Dwarf_Unsigned dw_offset_in_debug_info; |
93 | 8.38k | const char *dw_name_string; |
94 | 8.38k | unsigned char dw_flagbyte; |
95 | 8.38k | unsigned char dw_staticorglobal; |
96 | 8.38k | unsigned char dw_typeofentry; |
97 | 8.38k | res = dwarf_get_gnu_index_block_entry( |
98 | 8.38k | dw_head, block_number, entry_number, &dw_offset_in_debug_info, |
99 | 8.38k | &dw_name_string, &dw_flagbyte, &dw_staticorglobal, |
100 | 8.38k | &dw_typeofentry, &error); |
101 | 8.38k | } |
102 | 71 | } |
103 | 41 | dwarf_gnu_index_dealloc(dw_head); |
104 | 41 | } |
105 | 1.57k | } |
106 | | |
107 | 9.23k | dwarf_finish(dbg); |
108 | 9.23k | close(fd); |
109 | 9.23k | unlink(filename); |
110 | 9.23k | return 0; |
111 | 9.23k | } |