Debian Codegraph @

fuzz/fuzz_debug_str.c

raw
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 #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 * Fuzzer function
32 */
33 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34 char filename[256];
35 sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
37 FILE *fp = fopen(filename, "wb");
38 if (!fp) {
39 return 0;
40 }
41 fwrite(data, size, 1, fp);
42 fclose(fp);
43
44 Dwarf_Debug dbg = 0;
45 int res = DW_DLV_ERROR;
46 Dwarf_Error error = 0;
47 Dwarf_Handler errhand = 0;
48 Dwarf_Ptr errarg = 0;
49
50 int fd = open(filename, O_RDONLY | O_BINARY);
51 if (fd < 0) {
52 exit(EXIT_FAILURE);
53 }
54
55 res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
56
57 if (res != DW_DLV_OK) {
58 dwarf_dealloc_error(dbg, error);
59 } else {
60 /* libdwarf does not require offset to be anything in
61 particular, and will work fine regardless
62 (possibly returning DW_DLV_ERROR or DW_DLV_OK). But
63 valgrind generates a warning passing in the uninitialized
64 value so let us initialize it to ... something. */
65 Dwarf_Off dw_offset = 11;
66 char *dw_string;
67 Dwarf_Signed dw_strlen_of_string;
68
69 while ((res = dwarf_get_str(dbg, dw_offset, &dw_string,
70 &dw_strlen_of_string, &error)) == DW_DLV_OK) {
71 dw_offset += dw_strlen_of_string + 1;
72 }
73 }
74
75 dwarf_finish(dbg);
76 close(fd);
77 unlink(filename);
78 return 0;
79 }