Debian Codegraph @

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