Coverage Report

Created: 2026-07-13 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_die_cu_attrs.c
Line
Count
Source
1
/* Copyright 2024 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
21
/*
22
 * Libdwarf library callers can only use these headers.
23
 */
24
#include "dwarf.h"
25
#include "libdwarf.h"
26
#ifndef O_BINARY
27
13.8k
#define O_BINARY 0
28
#endif
29
30
/*  As originally written, reading a fuzzed object,
31
    this group of statements
32
    will loop  'forever', meaning until any running
33
    Sanitizer will run out of memory keeping track,
34
    or without a Sanitizer involved the application will
35
    run out of membore..
36
37
    There are memory leaks on every error and from
38
    any successful calls.
39
40
    That is not a reproducible regression test.
41
42
    The problem is that nothing here sets 'attr'
43
    or checks for failure.
44
    there is no sane stopping condition.
45
    If the object being read is trivial this might stop
46
    in an acceptable time.
47
    But we check for null 'attr' (it is NULL, see below)
48
    and stop.
49
    It's a a silly testcase as writen, and tests very
50
    little of a practical nature.
51
    David Anderson December 13, 2024. */
52
53
54
55
56
/*
57
 * Fuzzer function
58
 */
59
13.8k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
60
13.8k
  char filename[256];
61
#ifdef DWREGRESSIONTEMP
62
  /* Under msys2, the /tmp/ results in an open fail */
63
  sprintf(filename, "junklibfuzzer.%d", getpid());
64
#else
65
13.8k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
66
13.8k
#endif
67
13.8k
  FILE *fp = fopen(filename, "wb");
68
13.8k
  if (!fp) {
69
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
70
0
        filename);
71
0
    return 0;
72
0
  }
73
13.8k
  fwrite(data, size, 1, fp);
74
13.8k
  fclose(fp);
75
76
13.8k
  Dwarf_Debug dbg = 0;
77
13.8k
  int fuzz_fd = 0;
78
13.8k
  int res = DW_DLV_ERROR;
79
13.8k
  Dwarf_Error error = 0;
80
13.8k
  Dwarf_Handler errhand = 0;
81
13.8k
  Dwarf_Ptr errarg = 0;
82
13.8k
  Dwarf_Error *errp = 0;
83
13.8k
  int i = 0;
84
13.8k
  Dwarf_Die die = 0;
85
86
13.8k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
87
13.8k
  if (fuzz_fd != -1) {
88
13.8k
    res =
89
13.8k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
90
13.8k
    if (res == DW_DLV_OK) {
91
6.66k
      Dwarf_Bool is_info = 0;
92
6.66k
      Dwarf_Unsigned cu_header_length = 0;
93
6.66k
      Dwarf_Half version_stamp = 0;
94
6.66k
      Dwarf_Off abbrev_offset = 0;
95
6.66k
      Dwarf_Half address_size = 0;
96
6.66k
      Dwarf_Half length_size = 0;
97
6.66k
      Dwarf_Half extension_size = 0;
98
6.66k
      Dwarf_Sig8 type_signature;
99
6.66k
      Dwarf_Unsigned typeoffset = 0;
100
6.66k
      Dwarf_Unsigned next_cu_header_offset = 0;
101
6.66k
      Dwarf_Half header_cu_type = 0;
102
6.66k
      Dwarf_Die cu_die = 0;
103
6.66k
      static const Dwarf_Sig8 zerosignature;
104
105
6.66k
      type_signature = zerosignature;
106
6.66k
      res = dwarf_next_cu_header_d(
107
6.66k
          dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
108
6.66k
          &address_size, &length_size, &extension_size, &type_signature,
109
6.66k
          &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
110
6.66k
      if (res == DW_DLV_OK) {
111
3.41k
        res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
112
3.41k
        if (res == DW_DLV_OK) {
113
3.22k
          Dwarf_Unsigned unsign = 0;
114
3.22k
          res = dwarf_bytesize(cu_die, &unsign, errp);
115
3.22k
          res = dwarf_bitsize(cu_die, &unsign, errp);
116
3.22k
          res = dwarf_arrayorder(cu_die, &unsign, errp);
117
118
3.22k
          Dwarf_Off section_offset = 0;
119
3.22k
          Dwarf_Off local_offset = 0;
120
3.22k
          res = dwarf_die_offsets(cu_die, &section_offset, &local_offset, errp);
121
122
3.22k
          Dwarf_Off off = 0;
123
3.22k
          res = dwarf_dietype_offset(die, &off, &is_info, &error);
124
125
3.22k
          Dwarf_Off agoff = 0;
126
3.22k
          Dwarf_Unsigned acount = 0;
127
3.22k
          res = dwarf_die_abbrev_global_offset(cu_die, &agoff, &acount, errp);
128
129
3.22k
          Dwarf_Bool someinfo = dwarf_get_die_infotypes_flag(cu_die);
130
131
3.22k
          Dwarf_Off globaloff = 0;
132
3.22k
          Dwarf_Unsigned length = 0;
133
3.22k
          res = dwarf_die_CU_offset_range(cu_die, &globaloff, &length, errp);
134
135
3.22k
          Dwarf_Half address_size = 0;
136
3.22k
          res = dwarf_get_address_size(dbg, &address_size, errp);
137
138
3.22k
          Dwarf_Unsigned offcnt = 0;
139
3.22k
          Dwarf_Off *offbuf = 0;
140
3.22k
          res = dwarf_offset_list(dbg, section_offset, is_info, &offbuf,
141
3.22k
                                  &offcnt, errp);
142
143
3.22k
          Dwarf_Off die_goff = 0;
144
3.22k
          res = dwarf_dieoffset(cu_die, &die_goff, errp);
145
146
3.22k
          int abbrev_code = dwarf_die_abbrev_code(cu_die);
147
148
3.22k
          Dwarf_Bool has_attr = 0;
149
3.22k
          res = dwarf_hasattr(cu_die, DW_AT_external, &has_attr, errp);
150
151
3.22k
          Dwarf_Bool is_dwo = 0;
152
3.22k
          Dwarf_Half offset_size = 0;
153
3.22k
          Dwarf_Off offset_of_length = 0;
154
3.22k
          Dwarf_Unsigned total_byte_length = 0;
155
3.22k
          Dwarf_Sig8 *typesign = 0;
156
157
3.22k
          res = dwarf_cu_header_basics(
158
3.22k
              cu_die, &version_stamp, &is_info, &is_dwo, &offset_size,
159
3.22k
              &address_size, &extension_size, &typesign, &offset_of_length,
160
3.22k
              &total_byte_length, errp);
161
162
3.22k
          Dwarf_Debug_Fission_Per_CU percu;
163
3.22k
          memset(&percu, 0, sizeof(percu));
164
3.22k
          res = dwarf_get_debugfission_for_die(cu_die, &percu, errp);
165
3.22k
          char *name = 0;
166
3.22k
          Dwarf_Half tag = 0;
167
3.22k
          const char *tagname = 0;
168
3.22k
          int res = 0;
169
3.22k
          Dwarf_Attribute *atlist = 0;
170
3.22k
          Dwarf_Signed atcount = 0;
171
3.22k
          Dwarf_Attribute attr = 0;
172
3.22k
          Dwarf_Half formnum = 0;
173
3.22k
          const char *formname = "form-name-unavailable";
174
175
3.22k
          if (res == DW_DLV_ERROR || res == DW_DLV_NO_ENTRY) {
176
0
            dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
177
0
            dwarf_finish(dbg);
178
0
            close(fuzz_fd);
179
0
            return 0;
180
0
          }
181
182
3.22k
          res = dwarf_diename(cu_die, &name, errp);
183
184
3.22k
          res = dwarf_get_TAG_name(tag, &tagname);
185
186
3.22k
          res = dwarf_attr(cu_die, DW_AT_name, &attr, errp);
187
3.22k
          if (res != DW_DLV_OK) {
188
865
            printf("Forcing close, "
189
865
              "Null attr due to error means all will fail "
190
865
              "and the list of errors is unreasonably long \n"); 
191
865
            fflush(stdout);
192
865
            dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
193
865
            dwarf_finish(dbg);
194
865
            close(fuzz_fd);
195
865
            return 0;
196
865
          }
197
2.36k
#if 1
198
2.36k
            if (!attr) {
199
0
              printf("Forcing close, "
200
0
                "Null attr corrupt object or \n"); 
201
0
              fflush(stdout);
202
0
              dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
203
0
              dwarf_finish(dbg);
204
0
              close(fuzz_fd);
205
0
              return 0;
206
0
            }
207
2.36k
#endif
208
2.36k
          res = dwarf_attrlist(cu_die, &atlist, &atcount, errp);
209
25.2k
          for (i = 0; i < atcount; ++i) {
210
22.8k
            Dwarf_Half attrnum = 0;
211
22.8k
            const char *attrname = 0;
212
22.8k
            res = dwarf_whatform(attr, &formnum, errp);
213
#if 0
214
            if (!attr) {
215
              printf("Forcing close, "
216
                "Null attr corrupt object or \n"); 
217
              fflush(stdout);
218
              dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
219
              dwarf_finish(dbg);
220
              close(fuzz_fd);
221
              return 0;
222
            }
223
#endif
224
22.8k
            Dwarf_Bool *dw_returned_bool = 0;
225
22.8k
            res = dwarf_hasform(attr, formnum, dw_returned_bool, errp);
226
22.8k
            res = dwarf_get_FORM_name(formnum, &formname);
227
22.8k
            Dwarf_Block *tempb = 0;
228
22.8k
            res = dwarf_formblock(attr, &tempb, errp);
229
22.8k
            if (res == DW_DLV_OK) {
230
5.59k
              Dwarf_Dsc_Head h = 0;
231
              /* Dwarf_Unsigned u = 0; unused */
232
5.59k
              Dwarf_Unsigned arraycount = 0;
233
5.59k
              int sres = 0;
234
235
5.59k
              sres = dwarf_discr_list(dbg, (Dwarf_Small *)tempb->bl_data,
236
5.59k
                                      tempb->bl_len, &h, &arraycount, errp);
237
5.59k
            }
238
22.8k
            res = dwarf_whatattr(atlist[i], &attrnum, errp);
239
22.8k
            dwarf_get_AT_name(attrnum, &attrname);
240
22.8k
            dwarf_dealloc_attribute(atlist[i]);
241
22.8k
            atlist[i] = 0;
242
22.8k
            char *stringval = 0;
243
22.8k
            res = dwarf_bitoffset(cu_die, &attrnum, &unsign, errp);
244
22.8k
            res = dwarf_die_text(cu_die, attrnum, &stringval, errp);
245
22.8k
            res = dwarf_get_form_class(version_stamp, attrnum,
246
22.8k
                                       next_cu_header_offset, formnum);
247
22.8k
          }
248
2.36k
          res = dwarf_set_tied_dbg(dbg, NULL, errp);
249
2.36k
        }
250
2.55k
        dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
251
2.55k
      }
252
6.66k
    }
253
13.8k
  }
254
13.0k
  dwarf_finish(dbg);
255
13.0k
  close(fuzz_fd);
256
13.0k
  unlink(filename);
257
13.0k
  return 0;
258
13.8k
}