Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_gdbindex.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 <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
8.17k
#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
int examplew(Dwarf_Debug dbg, Dwarf_Error *error);
31
int examplewgdbindex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error);
32
int examplex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error);
33
34
/*
35
 * A fuzzer that simulates a small part of the simplereader.c example.
36
 */
37
8.17k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
38
8.17k
  char filename[256];
39
40
#ifdef DWREGRESSIONTEMP
41
  /*  Under msys2, the /tmp/ results in an open fail,
42
      so we discard the /tmp/ here */
43
  sprintf(filename, "junklibfuzzer.%d", getpid());
44
#else
45
8.17k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
46
8.17k
#endif
47
8.17k
  FILE *fp = fopen(filename, "wb");
48
8.17k
  if (!fp) {
49
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
50
0
        filename);
51
0
    return 0;
52
0
  }
53
8.17k
  fwrite(data, size, 1, fp);
54
8.17k
  fclose(fp);
55
56
8.17k
  Dwarf_Debug dbg = 0;
57
8.17k
  int res = DW_DLV_ERROR;
58
8.17k
  Dwarf_Error error = 0;
59
8.17k
  Dwarf_Handler errhand = 0;
60
8.17k
  Dwarf_Ptr errarg = 0;
61
8.17k
  int regtabrulecount = 0;
62
8.17k
  int curopt = 0;
63
64
8.17k
  int fd = open(filename, O_RDONLY | O_BINARY);
65
8.17k
  if (fd < 0) {
66
0
    exit(EXIT_FAILURE);
67
0
  }
68
69
8.17k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
70
71
8.17k
  if (res != DW_DLV_OK) {
72
6.91k
    dwarf_dealloc_error(dbg, error);
73
6.91k
  } else {
74
1.25k
    examplew(dbg, &error);
75
1.25k
  }
76
77
8.17k
  dwarf_finish(dbg);
78
8.17k
  close(fd);
79
8.17k
  unlink(filename);
80
8.17k
  return 0;
81
8.17k
}
82
83
1.25k
int examplew(Dwarf_Debug dbg, Dwarf_Error *error) {
84
1.25k
  Dwarf_Gdbindex gindexptr = 0;
85
1.25k
  Dwarf_Unsigned version = 0;
86
1.25k
  Dwarf_Unsigned cu_list_offset = 0;
87
1.25k
  Dwarf_Unsigned types_cu_list_offset = 0;
88
1.25k
  Dwarf_Unsigned address_area_offset = 0;
89
1.25k
  Dwarf_Unsigned symbol_table_offset = 0;
90
1.25k
  Dwarf_Unsigned constant_pool_offset = 0;
91
1.25k
  Dwarf_Unsigned section_size = 0;
92
1.25k
  const char *section_name = 0;
93
1.25k
  int res = 0;
94
95
1.25k
  res = dwarf_gdbindex_header(dbg, &gindexptr, &version, &cu_list_offset,
96
1.25k
                              &types_cu_list_offset, &address_area_offset,
97
1.25k
                              &symbol_table_offset, &constant_pool_offset,
98
1.25k
                              &section_size, &section_name, error);
99
1.25k
  if (res != DW_DLV_OK) {
100
1.05k
    return res;
101
1.05k
  }
102
204
  {
103
204
    Dwarf_Unsigned length = 0;
104
204
    Dwarf_Unsigned typeslength = 0;
105
204
    Dwarf_Unsigned i = 0;
106
204
    res = dwarf_gdbindex_culist_array(gindexptr, &length, error);
107
204
    if (res != DW_DLV_OK) {
108
0
      dwarf_dealloc_gdbindex(gindexptr);
109
0
      return res;
110
0
    }
111
45.8k
    for (i = 0; i < length; ++i) {
112
45.6k
      Dwarf_Unsigned cuoffset = 0;
113
45.6k
      Dwarf_Unsigned culength = 0;
114
45.6k
      res = dwarf_gdbindex_culist_entry(gindexptr, i, &cuoffset, &culength,
115
45.6k
                                        error);
116
45.6k
      if (res != DW_DLV_OK) {
117
0
        return res;
118
0
      }
119
45.6k
    }
120
204
    res = dwarf_gdbindex_types_culist_array(gindexptr, &typeslength, error);
121
204
    if (res != DW_DLV_OK) {
122
0
      dwarf_dealloc_gdbindex(gindexptr);
123
0
      return res;
124
0
    }
125
28.4k
    for (i = 0; i < typeslength; ++i) {
126
28.1k
      Dwarf_Unsigned cuoffset = 0;
127
28.1k
      Dwarf_Unsigned tuoffset = 0;
128
28.1k
      Dwarf_Unsigned type_signature = 0;
129
28.1k
      res = dwarf_gdbindex_types_culist_entry(
130
28.1k
          gindexptr, i, &cuoffset, &tuoffset, &type_signature, error);
131
28.1k
      if (res != DW_DLV_OK) {
132
0
        dwarf_dealloc_gdbindex(gindexptr);
133
0
        return res;
134
0
      }
135
28.1k
    }
136
137
204
    res = examplewgdbindex(gindexptr, error);
138
204
    if (res != DW_DLV_OK) {
139
0
      dwarf_dealloc_gdbindex(gindexptr);
140
0
      return res;
141
0
    }
142
143
204
    res = examplex(gindexptr, error);
144
204
    if (res != DW_DLV_OK) {
145
174
      dwarf_dealloc_gdbindex(gindexptr);
146
174
      return res;
147
174
    }
148
149
30
    dwarf_dealloc_gdbindex(gindexptr);
150
30
  }
151
30
  return DW_DLV_OK;
152
204
}
153
154
204
int examplewgdbindex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error) {
155
204
  Dwarf_Unsigned list_len = 0;
156
204
  Dwarf_Unsigned i = 0;
157
204
  int res = 0;
158
159
204
  res = dwarf_gdbindex_addressarea(gdbindex, &list_len, error);
160
204
  if (res != DW_DLV_OK) {
161
0
    return res;
162
0
  }
163
75.7k
  for (i = 0; i < list_len; i++) {
164
75.5k
    Dwarf_Unsigned lowpc = 0;
165
75.5k
    Dwarf_Unsigned highpc = 0;
166
75.5k
    Dwarf_Unsigned cu_index = 0;
167
75.5k
    res = dwarf_gdbindex_addressarea_entry(gdbindex, i, &lowpc, &highpc,
168
75.5k
                                           &cu_index, error);
169
75.5k
    if (res != DW_DLV_OK) {
170
0
      return res;
171
0
    }
172
75.5k
  }
173
204
  return DW_DLV_OK;
174
204
}
175
176
204
int examplex(Dwarf_Gdbindex gdbindex, Dwarf_Error *error) {
177
204
  Dwarf_Unsigned symtab_list_length = 0;
178
204
  Dwarf_Unsigned i = 0;
179
204
  int res = 0;
180
181
204
  res = dwarf_gdbindex_symboltable_array(gdbindex, &symtab_list_length, error);
182
204
  if (res != DW_DLV_OK) {
183
0
    return res;
184
0
  }
185
1.10k
  for (i = 0; i < symtab_list_length; i++) {
186
1.07k
    Dwarf_Unsigned symnameoffset = 0;
187
1.07k
    Dwarf_Unsigned cuvecoffset = 0;
188
1.07k
    Dwarf_Unsigned cuvec_len = 0;
189
1.07k
    Dwarf_Unsigned ii = 0;
190
1.07k
    const char *name = 0;
191
1.07k
    int resl = 0;
192
193
1.07k
    resl = dwarf_gdbindex_symboltable_entry(gdbindex, i, &symnameoffset,
194
1.07k
                                            &cuvecoffset, error);
195
1.07k
    if (resl != DW_DLV_OK) {
196
0
      return resl;
197
0
    }
198
1.07k
    resl =
199
1.07k
        dwarf_gdbindex_string_by_offset(gdbindex, symnameoffset, &name, error);
200
1.07k
    if (resl != DW_DLV_OK) {
201
73
      return resl;
202
73
    }
203
1.00k
    resl = dwarf_gdbindex_cuvector_length(gdbindex, cuvecoffset, &cuvec_len,
204
1.00k
                                          error);
205
1.00k
    if (resl != DW_DLV_OK) {
206
56
      return resl;
207
56
    }
208
160k
    for (ii = 0; ii < cuvec_len; ++ii) {
209
159k
      Dwarf_Unsigned attributes = 0;
210
159k
      Dwarf_Unsigned cu_index = 0;
211
159k
      Dwarf_Unsigned symbol_kind = 0;
212
159k
      Dwarf_Unsigned is_static = 0;
213
159k
      int res2 = 0;
214
215
159k
      res2 = dwarf_gdbindex_cuvector_inner_attributes(gdbindex, cuvecoffset, ii,
216
159k
                                                      &attributes, error);
217
159k
      if (res2 != DW_DLV_OK) {
218
45
        return res2;
219
45
      }
220
159k
      res2 = dwarf_gdbindex_cuvector_instance_expand_value(
221
159k
          gdbindex, attributes, &cu_index, &symbol_kind, &is_static, error);
222
159k
      if (res2 != DW_DLV_OK) {
223
0
        return res2;
224
0
      }
225
159k
    }
226
944
  }
227
30
  return DW_DLV_OK;
228
204
}