Debian Codegraph @

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