/src/libdwarf/fuzz/fuzz_init_path.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 <stdint.h> |
13 | | #include <stdio.h> |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | #include <sys/types.h> |
17 | | #include <unistd.h> |
18 | | |
19 | | /* |
20 | | * Libdwarf library callers can only use these headers. |
21 | | */ |
22 | | #include "dwarf.h" |
23 | | #include "libdwarf.h" |
24 | | |
25 | | /* |
26 | | * A fuzzer that simulates a small part of the simplereader.c example. |
27 | | */ |
28 | 8.56k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
29 | 8.56k | char filename[256]; |
30 | | |
31 | | #ifdef DWREGRESSIONTEMP |
32 | | /* Under msys2, the /tmp/ results in an open fail, |
33 | | so we discard the /tmp/ here */ |
34 | | sprintf(filename, "junklibfuzzer.%d", getpid()); |
35 | | #else |
36 | 8.56k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
37 | 8.56k | #endif |
38 | 8.56k | FILE *fp = fopen(filename, "wb"); |
39 | 8.56k | if (!fp) { |
40 | 0 | printf("FAIL libfuzzer cannot open temp as writeable %s\n", |
41 | 0 | filename); |
42 | 0 | return 0; |
43 | 0 | } |
44 | 8.56k | fwrite(data, size, 1, fp); |
45 | 8.56k | fclose(fp); |
46 | 8.56k | Dwarf_Ptr errarg = 0; |
47 | 8.56k | Dwarf_Handler errhand = 0; |
48 | 8.56k | Dwarf_Debug dbg = 0; |
49 | 8.56k | Dwarf_Error *errp = NULL; |
50 | 8.56k | #define MACHO_PATH_LEN 2000 |
51 | 8.56k | char macho_real_path[2000]; |
52 | 8.56k | dwarf_init_path(filename, macho_real_path, MACHO_PATH_LEN, DW_GROUPNUMBER_ANY, |
53 | 8.56k | errhand, errarg, &dbg, errp); |
54 | | |
55 | 8.56k | dwarf_finish(dbg); |
56 | 8.56k | static const char *glpath[1] = {"/usr/include/c++/9/debug"}; |
57 | 8.56k | unsigned char path_source = 0; |
58 | 8.56k | dwarf_init_path_dl(filename, NULL, 256, DW_GROUPNUMBER_ANY, errhand, errarg, |
59 | 8.56k | &dbg, (char **)glpath, 1, &path_source, errp); |
60 | 8.56k | dwarf_finish(dbg); |
61 | 8.56k | unlink(filename); |
62 | 8.56k | return 0; |
63 | 8.56k | } |