/src/libdwarf/fuzz/fuzz_die_cu_e_print.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/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 | | #ifndef O_BINARY |
27 | 11.6k | #define O_BINARY 0 |
28 | | #endif |
29 | | |
30 | | |
31 | | /* |
32 | | * A fuzzer that simulates a small part of the simplereader.c example. |
33 | | * This fuzzer targets dwarf_init_b. |
34 | | */ |
35 | 11.6k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
36 | 11.6k | char filename[256]; |
37 | 11.6k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
38 | | |
39 | 11.6k | FILE *fp = fopen(filename, "wb"); |
40 | 11.6k | if (!fp) { |
41 | 0 | return 0; |
42 | 0 | } |
43 | 11.6k | fwrite(data, size, 1, fp); |
44 | 11.6k | fclose(fp); |
45 | | |
46 | 11.6k | Dwarf_Debug dbg = 0; |
47 | 11.6k | int fuzz_fd = 0; |
48 | 11.6k | int res = DW_DLV_ERROR; |
49 | 11.6k | Dwarf_Error error = 0; |
50 | 11.6k | Dwarf_Handler errhand = 0; |
51 | 11.6k | Dwarf_Ptr errarg = 0; |
52 | 11.6k | Dwarf_Error *errp = 0; |
53 | 11.6k | int i = 0; |
54 | 11.6k | Dwarf_Die die; |
55 | | |
56 | 11.6k | fuzz_fd = open(filename, O_RDONLY|O_BINARY); |
57 | 11.6k | if (fuzz_fd != -1) { |
58 | 11.6k | res = |
59 | 11.6k | dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp); |
60 | 11.6k | if (res == DW_DLV_OK) { |
61 | 4.46k | Dwarf_Bool is_info = 0; |
62 | 4.46k | Dwarf_Unsigned cu_header_length = 0; |
63 | 4.46k | Dwarf_Half version_stamp = 0; |
64 | 4.46k | Dwarf_Off abbrev_offset = 0; |
65 | 4.46k | Dwarf_Half address_size = 0; |
66 | 4.46k | Dwarf_Half length_size = 0; |
67 | 4.46k | Dwarf_Half extension_size = 0; |
68 | 4.46k | Dwarf_Sig8 type_signature; |
69 | 4.46k | Dwarf_Unsigned typeoffset = 0; |
70 | 4.46k | Dwarf_Unsigned next_cu_header_offset = 0; |
71 | 4.46k | Dwarf_Half header_cu_type = 0; |
72 | 4.46k | Dwarf_Die cu_die = 0; |
73 | 4.46k | static const Dwarf_Sig8 zerosignature; |
74 | | |
75 | 4.46k | res = dwarf_get_address_size(dbg, &address_size, errp); |
76 | | |
77 | 4.46k | const char *frame_section_name = 0; |
78 | 4.46k | res = dwarf_get_frame_section_name(dbg, &frame_section_name, errp); |
79 | | |
80 | 4.46k | type_signature = zerosignature; |
81 | 4.46k | res = dwarf_next_cu_header_e( |
82 | 4.46k | dbg, is_info,&cu_die, &cu_header_length, &version_stamp, &abbrev_offset, |
83 | 4.46k | &address_size, &length_size, &extension_size, &type_signature, |
84 | 4.46k | &typeoffset, &next_cu_header_offset, &header_cu_type, errp); |
85 | 4.46k | if (res == DW_DLV_OK) { |
86 | 869 | Dwarf_Die child = 0; |
87 | 869 | res = dwarf_child(cu_die, &child, errp); |
88 | 3.59k | } else { |
89 | 3.59k | dwarf_finish(dbg); |
90 | 3.59k | close(fuzz_fd); |
91 | 3.59k | unlink(filename); |
92 | 3.59k | return 0; |
93 | 3.59k | } |
94 | 869 | dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); |
95 | 869 | } |
96 | 11.6k | } |
97 | 8.06k | dwarf_finish(dbg); |
98 | 8.06k | close(fuzz_fd); |
99 | 8.06k | unlink(filename); |
100 | 8.06k | return 0; |
101 | 11.6k | } |