/src/libdwarf/fuzz/fuzz_macro_dwarf4.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 | 8.88k | #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.88k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
34 | 8.88k | char filename[256]; |
35 | 8.88k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
36 | | |
37 | 8.88k | FILE *fp = fopen(filename, "wb"); |
38 | 8.88k | if (!fp) { |
39 | 0 | return 0; |
40 | 0 | } |
41 | 8.88k | fwrite(data, size, 1, fp); |
42 | 8.88k | fclose(fp); |
43 | | |
44 | 8.88k | Dwarf_Debug dbg = 0; |
45 | 8.88k | int res = DW_DLV_ERROR; |
46 | 8.88k | Dwarf_Error error = 0; |
47 | 8.88k | Dwarf_Handler errhand = 0; |
48 | 8.88k | Dwarf_Ptr errarg = 0; |
49 | | |
50 | 8.88k | int fd = open(filename, O_RDONLY | O_BINARY); |
51 | 8.88k | if (fd < 0) { |
52 | 0 | exit(EXIT_FAILURE); |
53 | 0 | } |
54 | | |
55 | 8.88k | res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error); |
56 | | |
57 | 8.88k | if (res != DW_DLV_OK) { |
58 | 7.91k | dwarf_dealloc_error(dbg, error); |
59 | 7.91k | } else { |
60 | 978 | res = 0; |
61 | 978 | Dwarf_Off cur_off = 0; |
62 | 978 | Dwarf_Macro_Details *maclist = 0; |
63 | 978 | Dwarf_Signed i = 0; |
64 | 978 | Dwarf_Signed count = 0; |
65 | 978 | Dwarf_Unsigned max = 500000; /* sanity limit */ |
66 | | |
67 | 402k | while ((res = dwarf_get_macro_details(dbg, cur_off, max, &count, &maclist, |
68 | 402k | &error)) == DW_DLV_OK) { |
69 | 2.94M | for (i = 0; i < count; ++i) { |
70 | 2.54M | Dwarf_Macro_Details *mentry = maclist + i; |
71 | 2.54M | Dwarf_Signed lineno = mentry->dmd_lineno; |
72 | 2.54M | } |
73 | 401k | cur_off = maclist[count - 1].dmd_offset + 1; |
74 | 401k | dwarf_dealloc(dbg, maclist, DW_DLA_STRING); |
75 | 401k | } |
76 | 978 | } |
77 | | |
78 | 8.88k | dwarf_finish(dbg); |
79 | 8.88k | close(fd); |
80 | 8.88k | unlink(filename); |
81 | 8.88k | return 0; |
82 | 8.88k | } |