/src/libdwarf/fuzz/fuzz_macro_dwarf4.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 <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 | 8.41k | #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 | | * A fuzzer that simulates a small part of the simplereader.c example. |
32 | | */ |
33 | 8.41k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
34 | 8.41k | char filename[256]; |
35 | | |
36 | | #ifdef DWREGRESSIONTEMP |
37 | | /* Under msys2, the /tmp/ results in an open fail, |
38 | | so we discard the /tmp/ here */ |
39 | | sprintf(filename, "junklibfuzzer.%d", getpid()); |
40 | | #else |
41 | 8.41k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
42 | 8.41k | #endif |
43 | 8.41k | FILE *fp = fopen(filename, "wb"); |
44 | 8.41k | if (!fp) { |
45 | 0 | printf("FAIL libfuzzer cannot open temp as writeable %s\n", |
46 | 0 | filename); |
47 | 0 | return 0; |
48 | 0 | } |
49 | 8.41k | fwrite(data, size, 1, fp); |
50 | 8.41k | fclose(fp); |
51 | | |
52 | 8.41k | Dwarf_Debug dbg = 0; |
53 | 8.41k | int res = DW_DLV_ERROR; |
54 | 8.41k | Dwarf_Error error = 0; |
55 | 8.41k | Dwarf_Handler errhand = 0; |
56 | 8.41k | Dwarf_Ptr errarg = 0; |
57 | | |
58 | 8.41k | int fd = open(filename, O_RDONLY | O_BINARY); |
59 | 8.41k | if (fd < 0) { |
60 | 0 | exit(EXIT_FAILURE); |
61 | 0 | } |
62 | | |
63 | 8.41k | res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
64 | | |
65 | 8.41k | if (res != DW_DLV_OK) { |
66 | 7.45k | dwarf_dealloc_error(dbg, error); |
67 | 7.45k | } else { |
68 | 957 | res = 0; |
69 | 957 | Dwarf_Off cur_off = 0; |
70 | 957 | Dwarf_Macro_Details *maclist = 0; |
71 | 957 | Dwarf_Signed i = 0; |
72 | 957 | Dwarf_Signed count = 0; |
73 | 957 | Dwarf_Unsigned max = 500000; /* sanity limit */ |
74 | | |
75 | 22.7k | while ((res = dwarf_get_macro_details(dbg, cur_off, max, &count, &maclist, |
76 | 22.7k | &error)) == DW_DLV_OK) { |
77 | 92.0k | for (i = 0; i < count; ++i) { |
78 | 70.2k | Dwarf_Macro_Details *mentry = maclist + i; |
79 | 70.2k | Dwarf_Signed lineno = mentry->dmd_lineno; |
80 | 70.2k | } |
81 | 21.8k | cur_off = maclist[count - 1].dmd_offset + 1; |
82 | 21.8k | dwarf_dealloc(dbg, maclist, DW_DLA_STRING); |
83 | 21.8k | } |
84 | 957 | } |
85 | | |
86 | 8.41k | dwarf_finish(dbg); |
87 | 8.41k | close(fd); |
88 | 8.41k | unlink(filename); |
89 | 8.41k | return 0; |
90 | 8.41k | } |