/src/libdwarf/fuzz/fuzz_showsectgrp.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 | | #define O_BINARY 0 |
25 | | #endif |
26 | | |
27 | 8.37k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | 8.37k | char filename[256]; |
29 | 8.37k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
30 | | |
31 | 8.37k | FILE *fp = fopen(filename, "wb"); |
32 | 8.37k | if (!fp) { |
33 | 0 | return 0; |
34 | 0 | } |
35 | 8.37k | fwrite(data, size, 1, fp); |
36 | 8.37k | fclose(fp); |
37 | | |
38 | 8.37k | Dwarf_Error *errp = 0; |
39 | 8.37k | Dwarf_Debug dbg = 0; |
40 | 8.37k | int res = 0; |
41 | 8.37k | int chosengroup = DW_GROUPNUMBER_ANY; |
42 | 8.37k | Dwarf_Error error = 0; |
43 | 8.37k | Dwarf_Unsigned section_count = 0; |
44 | 8.37k | Dwarf_Unsigned group_count = 0; |
45 | 8.37k | Dwarf_Unsigned selected_group = 0; |
46 | 8.37k | Dwarf_Unsigned map_entry_count = 0; |
47 | 8.37k | Dwarf_Unsigned *group_numbers_array = 0; |
48 | 8.37k | Dwarf_Unsigned *sec_numbers_array = 0; |
49 | 8.37k | const char **sec_names_array = 0; |
50 | | |
51 | 8.37k | int run = dwarf_init_path(filename, 0, 0, chosengroup, 0, 0, &dbg, errp); |
52 | 8.37k | if (run != -1) { |
53 | 5.78k | if (run == DW_DLV_ERROR) { |
54 | 5.48k | dwarf_finish(dbg); |
55 | 5.48k | unlink(filename); |
56 | 5.48k | return 0; |
57 | 5.48k | } |
58 | 306 | if (run == DW_DLV_NO_ENTRY) { |
59 | 0 | dwarf_finish(dbg); |
60 | 0 | unlink(filename); |
61 | 0 | return 0; |
62 | 0 | } |
63 | 306 | res = dwarf_sec_group_sizes(dbg, §ion_count, &group_count, |
64 | 306 | &selected_group, &map_entry_count, errp); |
65 | 306 | if (res != DW_DLV_OK) { |
66 | 0 | dwarf_dealloc_error(dbg, error); |
67 | 0 | error = 0; |
68 | 0 | dwarf_finish(dbg); |
69 | 0 | unlink(filename); |
70 | 0 | close(run); |
71 | 0 | return 0; |
72 | 0 | } |
73 | 306 | group_numbers_array = |
74 | 306 | (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned)); |
75 | 306 | sec_numbers_array = |
76 | 306 | (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned)); |
77 | 306 | sec_names_array = |
78 | 306 | (const char **)calloc(map_entry_count, sizeof(const char *)); |
79 | 306 | res = dwarf_sec_group_map(dbg, map_entry_count, group_numbers_array, |
80 | 306 | sec_numbers_array, sec_names_array, &error); |
81 | 306 | free(sec_names_array); |
82 | 306 | free(sec_numbers_array); |
83 | 306 | free(group_numbers_array); |
84 | 306 | dwarf_dealloc_error(dbg, error); |
85 | 306 | } |
86 | 2.89k | dwarf_finish(dbg); |
87 | 2.89k | unlink(filename); |
88 | 2.89k | close(run); |
89 | 2.89k | return 0; |
90 | 8.37k | } |