/src/libdwarf/fuzz/fuzz_simplereader_tu.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 | 24.4k | #define O_BINARY 0 |
25 | | #endif |
26 | | |
27 | 24.4k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
28 | 24.4k | 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 | 24.4k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
36 | 24.4k | #endif |
37 | 24.4k | FILE *fp = fopen(filename, "wb"); |
38 | 24.4k | if (!fp) { |
39 | 0 | printf("FAIL libfuzzer cannot open temp as writeable %s\n", |
40 | 0 | filename); |
41 | 0 | return 0; |
42 | 0 | } |
43 | 24.4k | fwrite(data, size, 1, fp); |
44 | 24.4k | fclose(fp); |
45 | | |
46 | 24.4k | Dwarf_Debug dbg = 0; |
47 | 24.4k | int fuzz_fd = 0; |
48 | 24.4k | int res = DW_DLV_ERROR; |
49 | 24.4k | Dwarf_Error error = 0; |
50 | 24.4k | Dwarf_Handler errhand = 0; |
51 | 24.4k | Dwarf_Ptr errarg = 0; |
52 | 24.4k | Dwarf_Sig8 hash8; |
53 | 24.4k | Dwarf_Sig8 hash8_2; |
54 | 24.4k | Dwarf_Error *errp = 0; |
55 | 24.4k | Dwarf_Die die = 0; |
56 | 24.4k | Dwarf_Attribute attr = 0; |
57 | | |
58 | 24.4k | fuzz_fd = open(filename, O_RDONLY|O_BINARY); |
59 | 24.4k | if (fuzz_fd != -1) { |
60 | 24.4k | Dwarf_Debug_Fission_Per_CU fisdata; |
61 | 24.4k | memset(&fisdata, 0, sizeof(fisdata)); |
62 | 24.4k | res = dwarf_die_from_hash_signature(dbg, &hash8, "tu", &die, errp); |
63 | 24.4k | res = dwarf_get_debugfission_for_key(dbg, &hash8_2, "tu", &fisdata, errp); |
64 | 24.4k | Dwarf_Addr uval = 0; |
65 | 24.4k | res = dwarf_formaddr(attr, &uval, errp); |
66 | 24.4k | dwarf_dealloc(dbg, die, DW_DLA_DIE); |
67 | 24.4k | dwarf_finish(dbg); |
68 | 24.4k | } |
69 | 24.4k | close(fuzz_fd); |
70 | 24.4k | unlink(filename); |
71 | 24.4k | return 0; |
72 | 24.4k | } |