Debian Codegraph @

fuzz/fuzz_tie.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/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include "dwarf.h"
21 #include "libdwarf.h"
22
23 #ifndef O_BINARY
24 #define O_BINARY 0
25 #endif
26
27 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28 char filename[256];
29 sprintf(filename, "/tmp/libfuzzer.%d", getpid());
30
31 FILE *fp = fopen(filename, "wb");
32 if (!fp) {
33 return 0;
34 }
35 fwrite(data, size, 1, fp);
36 fclose(fp);
37
38 int fuzz_fd = 0;
39 Dwarf_Ptr errarg = 0;
40 Dwarf_Handler errhand = 0;
41 Dwarf_Error *errp = NULL;
42 Dwarf_Debug dbg = 0;
43
44 fuzz_fd = open(filename, O_RDONLY|O_BINARY);
45 if (fuzz_fd != -1) {
46 int res =
47 dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
48 if (res == DW_DLV_OK) {
49 res = dwarf_set_tied_dbg(dbg, NULL, errp);
50 }
51 dwarf_finish(dbg);
52 close(fuzz_fd);
53 }
54
55 unlink(filename);
56 return 0;
57 }