/src/libdwarf/fuzz/fuzz_showsectgrp.c
Line | Count | Source |
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.18k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | 8.18k | char filename[256]; |
29 | | |
30 | | #ifdef DWREGRESSIONTEMP |
31 | | /* Under msys2, the /tmp/ results in an open fail, |
32 | | so we discard the /tmp/ here */ |
33 | | sprintf(filename, "junklibfuzzer.%d", getpid()); |
34 | | #else |
35 | 8.18k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
36 | 8.18k | #endif |
37 | 8.18k | FILE *fp = fopen(filename, "wb"); |
38 | 8.18k | if (!fp) { |
39 | 0 | printf("FAIL libfuzzer cannot open temp as writeable %s\n", |
40 | 0 | filename); |
41 | 0 | return 0; |
42 | 0 | } |
43 | 8.18k | fwrite(data, size, 1, fp); |
44 | 8.18k | fclose(fp); |
45 | | |
46 | 8.18k | Dwarf_Error *errp = 0; |
47 | 8.18k | Dwarf_Debug dbg = 0; |
48 | 8.18k | int res = 0; |
49 | 8.18k | int chosengroup = DW_GROUPNUMBER_ANY; |
50 | 8.18k | Dwarf_Error error = 0; |
51 | 8.18k | Dwarf_Unsigned section_count = 0; |
52 | 8.18k | Dwarf_Unsigned group_count = 0; |
53 | 8.18k | Dwarf_Unsigned selected_group = 0; |
54 | 8.18k | Dwarf_Unsigned map_entry_count = 0; |
55 | 8.18k | Dwarf_Unsigned *group_numbers_array = 0; |
56 | 8.18k | Dwarf_Unsigned *sec_numbers_array = 0; |
57 | 8.18k | const char **sec_names_array = 0; |
58 | | |
59 | 8.18k | int run = dwarf_init_path(filename, 0, 0, chosengroup, 0, 0, &dbg, errp); |
60 | 8.18k | if (run != -1) { |
61 | 6.16k | if (run == DW_DLV_ERROR) { |
62 | 5.84k | dwarf_finish(dbg); |
63 | 5.84k | unlink(filename); |
64 | 5.84k | return 0; |
65 | 5.84k | } |
66 | 315 | if (run == DW_DLV_NO_ENTRY) { |
67 | 0 | dwarf_finish(dbg); |
68 | 0 | unlink(filename); |
69 | 0 | return 0; |
70 | 0 | } |
71 | 315 | res = dwarf_sec_group_sizes(dbg, §ion_count, &group_count, |
72 | 315 | &selected_group, &map_entry_count, errp); |
73 | 315 | if (res != DW_DLV_OK) { |
74 | 0 | dwarf_dealloc_error(dbg, error); |
75 | 0 | error = 0; |
76 | 0 | dwarf_finish(dbg); |
77 | 0 | unlink(filename); |
78 | 0 | close(run); |
79 | 0 | return 0; |
80 | 0 | } |
81 | 315 | group_numbers_array = |
82 | 315 | (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned)); |
83 | 315 | sec_numbers_array = |
84 | 315 | (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned)); |
85 | 315 | sec_names_array = |
86 | 315 | (const char **)calloc(map_entry_count, sizeof(const char *)); |
87 | 315 | res = dwarf_sec_group_map(dbg, map_entry_count, group_numbers_array, |
88 | 315 | sec_numbers_array, sec_names_array, &error); |
89 | 315 | free(sec_names_array); |
90 | 315 | free(sec_numbers_array); |
91 | 315 | free(group_numbers_array); |
92 | 315 | dwarf_dealloc_error(dbg, error); |
93 | 315 | } |
94 | 2.33k | dwarf_finish(dbg); |
95 | 2.33k | unlink(filename); |
96 | 2.33k | close(run); |
97 | 2.33k | return 0; |
98 | 8.18k | } |