/src/libdwarf/fuzz/fuzz_aranges.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 <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/types.h> |
18 | | #include <unistd.h> |
19 | | |
20 | | #ifndef O_BINARY |
21 | 27.7k | #define O_BINARY 0 /* So it does nothing in Linux/Unix */ |
22 | | #endif |
23 | | |
24 | | /* |
25 | | * Libdwarf library callers can only use these headers. |
26 | | */ |
27 | | #include "dwarf.h" |
28 | | #include "libdwarf.h" |
29 | | |
30 | | /* |
31 | | * Helper function definitions. |
32 | | */ |
33 | | static void cleanup_bad_arange(Dwarf_Debug dbg, Dwarf_Arange *arange, |
34 | | Dwarf_Signed i, Dwarf_Signed count); |
35 | | int arange_processing_example(Dwarf_Debug dbg, Dwarf_Error *error); |
36 | | |
37 | | /* |
38 | | * Fuzzer function |
39 | | */ |
40 | 27.7k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
41 | 27.7k | char filename[256]; |
42 | 27.7k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
43 | | |
44 | 27.7k | FILE *fp = fopen(filename, "wb"); |
45 | 27.7k | if (!fp) { |
46 | 0 | return 0; |
47 | 0 | } |
48 | 27.7k | fwrite(data, size, 1, fp); |
49 | 27.7k | fclose(fp); |
50 | | |
51 | 27.7k | Dwarf_Debug dbg = 0; |
52 | 27.7k | int res = DW_DLV_ERROR; |
53 | 27.7k | Dwarf_Error error = 0; |
54 | 27.7k | Dwarf_Handler errhand = 0; |
55 | 27.7k | Dwarf_Ptr errarg = 0; |
56 | | |
57 | 27.7k | int fd = open(filename, O_RDONLY | O_BINARY); |
58 | 27.7k | if (fd < 0) { |
59 | 0 | exit(EXIT_FAILURE); |
60 | 0 | } |
61 | | |
62 | 27.7k | res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
63 | | |
64 | 27.7k | if (res != DW_DLV_OK) { |
65 | 22.1k | dwarf_dealloc_error(dbg, error); |
66 | 22.1k | } else { |
67 | | /* Adding newline avoids a warning from diff |
68 | | in regressiontesting */ |
69 | 5.57k | printf("Processing\n"); |
70 | 5.57k | arange_processing_example(dbg, &error); |
71 | 5.57k | } |
72 | | |
73 | 27.7k | dwarf_finish(dbg); |
74 | 27.7k | close(fd); |
75 | 27.7k | unlink(filename); |
76 | 27.7k | return 0; |
77 | 27.7k | } |
78 | | |
79 | | static void cleanup_bad_arange(Dwarf_Debug dbg, Dwarf_Arange *arange, |
80 | 228 | Dwarf_Signed i, Dwarf_Signed count) { |
81 | 228 | Dwarf_Signed k = i; |
82 | 125k | for (; k < count; ++k) { |
83 | 125k | dwarf_dealloc(dbg, arange[k], DW_DLA_ARANGE); |
84 | 125k | arange[k] = 0; |
85 | 125k | } |
86 | 228 | } |
87 | | |
88 | | // Source: |
89 | | // https://www.prevanders.net/libdwarfdoc/group__aranges.html#ga9b628e21a71f4280f93788815796ef92 |
90 | 1.83k | int arange_processing_example(Dwarf_Debug dbg, Dwarf_Error *error) { |
91 | 1.83k | Dwarf_Signed count = 0; |
92 | 1.83k | Dwarf_Arange *arange = 0; |
93 | 1.83k | int res = 0; |
94 | | |
95 | 1.83k | res = dwarf_get_aranges(dbg, &arange, &count, error); |
96 | 1.83k | if (res == DW_DLV_OK) { |
97 | 341 | Dwarf_Signed i = 0; |
98 | | |
99 | 211k | for (i = 0; i < count; ++i) { |
100 | 211k | Dwarf_Arange ara = arange[i]; |
101 | 211k | Dwarf_Unsigned segment = 0; |
102 | 211k | Dwarf_Unsigned segment_entry_size = 0; |
103 | 211k | Dwarf_Addr start = 0; |
104 | 211k | Dwarf_Unsigned length = 0; |
105 | 211k | Dwarf_Off cu_die_offset = 0; |
106 | | |
107 | 211k | res = dwarf_get_arange_info_b(ara, &segment, &segment_entry_size, &start, |
108 | 211k | &length, &cu_die_offset, error); |
109 | 211k | if (res != DW_DLV_OK) { |
110 | 228 | cleanup_bad_arange(dbg, arange, i, count); |
111 | 228 | dwarf_dealloc(dbg, arange, DW_DLA_LIST); |
112 | 228 | return res; |
113 | 228 | } |
114 | 211k | dwarf_dealloc(dbg, ara, DW_DLA_ARANGE); |
115 | 211k | arange[i] = 0; |
116 | 211k | } |
117 | 113 | dwarf_dealloc(dbg, arange, DW_DLA_LIST); |
118 | 113 | } |
119 | 1.61k | return res; |
120 | 1.83k | } |