/src/libdwarf/fuzz/fuzz_xuindex.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 "dwarf.h" |
13 | | #include "libdwarf.h" |
14 | | #include <fcntl.h> /* open() O_RDONLY O_BINARY */ |
15 | | #include <stdint.h> |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #include <sys/stat.h> |
20 | | #include <sys/types.h> |
21 | | #include <unistd.h> |
22 | | |
23 | | #ifndef O_BINARY |
24 | 16.5k | #define O_BINARY 0 |
25 | | #endif |
26 | | |
27 | 16.5k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | 16.5k | char filename[256]; |
29 | 16.5k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
30 | | |
31 | 16.5k | FILE *fp = fopen(filename, "wb"); |
32 | 16.5k | if (!fp) { |
33 | 0 | return 0; |
34 | 0 | } |
35 | 16.5k | fwrite(data, size, 1, fp); |
36 | 16.5k | fclose(fp); |
37 | | |
38 | 16.5k | Dwarf_Debug dbg = 0; |
39 | 16.5k | int fuzz_fd = 0; |
40 | 16.5k | int res = DW_DLV_ERROR; |
41 | 16.5k | Dwarf_Error error = 0; |
42 | 16.5k | Dwarf_Handler errhand = 0; |
43 | 16.5k | Dwarf_Ptr errarg = 0; |
44 | 16.5k | Dwarf_Error *errp = 0; |
45 | 16.5k | int i = 0; |
46 | 16.5k | Dwarf_Die die; |
47 | | |
48 | 16.5k | fuzz_fd = open(filename, O_RDONLY|O_BINARY); |
49 | 16.5k | if (fuzz_fd != -1) { |
50 | 16.5k | res = |
51 | 16.5k | dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp); |
52 | 16.5k | if (res == DW_DLV_OK) { |
53 | 384 | Dwarf_Xu_Index_Header xuhdr = 0; |
54 | 384 | Dwarf_Unsigned version_number = 0; |
55 | 384 | Dwarf_Unsigned offsets_count = 0; |
56 | 384 | Dwarf_Unsigned units_count = 0; |
57 | 384 | Dwarf_Unsigned hash_slots_count = 0; |
58 | 384 | const char *section_name = 0; |
59 | 384 | res = dwarf_get_xu_index_header(dbg, "cu", &xuhdr, &version_number, |
60 | 384 | &offsets_count, &units_count, |
61 | 384 | &hash_slots_count, §ion_name, errp); |
62 | 384 | } |
63 | 16.5k | } |
64 | 16.5k | dwarf_finish(dbg); |
65 | 16.5k | close(fuzz_fd); |
66 | 16.5k | unlink(filename); |
67 | 16.5k | return 0; |
68 | 16.5k | } |