Debian Codegraph @

fuzz/fuzz_str_offsets.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 "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/types.h>
20 #include <unistd.h>
21
22 #ifndef O_BINARY
23 #define O_BINARY 0 /* So it does nothing in Linux/Unix */
24 #endif
25
26 int string_offsets_example(Dwarf_Debug dbg, Dwarf_Error *error);
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 Dwarf_Debug dbg = 0;
39 int res = DW_DLV_ERROR;
40 Dwarf_Error error = 0;
41 Dwarf_Handler errhand = 0;
42 Dwarf_Ptr errarg = 0;
43 int regtabrulecount = 0;
44 int curopt = 0;
45
46 int fd = open(filename, O_RDONLY | O_BINARY);
47 if (fd < 0) {
48 exit(EXIT_FAILURE);
49 }
50
51 res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
52
53 if (res != DW_DLV_OK) {
54 dwarf_dealloc_error(dbg, error);
55 } else {
56 res = string_offsets_example(dbg, &error);
57 if (res != DW_DLV_OK) {
58 }
59 }
60
61 dwarf_finish(dbg);
62 close(fd);
63 unlink(filename);
64 return 0;
65 }
66
67 int string_offsets_example(Dwarf_Debug dbg, Dwarf_Error *error) {
68 int res = 0;
69 Dwarf_Str_Offsets_Table sot = 0;
70 Dwarf_Unsigned wasted_byte_count = 0;
71 Dwarf_Unsigned table_count = 0;
72 Dwarf_Error closeerror = 0;
73
74 res = dwarf_open_str_offsets_table_access(dbg, &sot, error);
75 if (res == DW_DLV_NO_ENTRY) {
76 return res;
77 }
78 if (res == DW_DLV_ERROR) {
79 return res;
80 }
81 for (;;) {
82 Dwarf_Unsigned unit_length = 0;
83 Dwarf_Unsigned unit_length_offset = 0;
84 Dwarf_Unsigned table_start_offset = 0;
85 Dwarf_Half entry_size = 0;
86 Dwarf_Half version = 0;
87 Dwarf_Half padding = 0;
88 Dwarf_Unsigned table_value_count = 0;
89 Dwarf_Unsigned i = 0;
90 Dwarf_Unsigned table_entry_value = 0;
91
92 res = dwarf_next_str_offsets_table(
93 sot, &unit_length, &unit_length_offset, &table_start_offset,
94 &entry_size, &version, &padding, &table_value_count, error);
95 if (res == DW_DLV_NO_ENTRY) {
96 break;
97 }
98 if (res == DW_DLV_ERROR) {
99 dwarf_close_str_offsets_table_access(sot, &closeerror);
100 dwarf_dealloc_error(dbg, closeerror);
101 return res;
102 }
103 for (i = 0; i < table_value_count; ++i) {
104 res = dwarf_str_offsets_value_by_index(sot, i, &table_entry_value, error);
105 if (res != DW_DLV_OK) {
106 dwarf_close_str_offsets_table_access(sot, &closeerror);
107 dwarf_dealloc_error(dbg, closeerror);
108 return res;
109 }
110 }
111 }
112 res = dwarf_str_offsets_statistics(sot, &wasted_byte_count, &table_count,
113 error);
114 if (res != DW_DLV_OK) {
115 dwarf_close_str_offsets_table_access(sot, &closeerror);
116 dwarf_dealloc_error(dbg, closeerror);
117 return res;
118 }
119 res = dwarf_close_str_offsets_table_access(sot, error);
120 sot = 0;
121 return res;
122 }