Coverage Report

Created: 2025-11-11 06:41

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.3k
#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.3k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
60
13.3k
  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.3k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
66
13.3k
#endif
67
13.3k
  FILE *fp = fopen(filename, "wb");
68
13.3k
  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.3k
  fwrite(data, size, 1, fp);
74
13.3k
  fclose(fp);
75
76
13.3k
  Dwarf_Debug dbg = 0;
77
13.3k
  int fuzz_fd = 0;
78
13.3k
  int res = DW_DLV_ERROR;
79
13.3k
  Dwarf_Error error = 0;
80
13.3k
  Dwarf_Handler errhand = 0;
81
13.3k
  Dwarf_Ptr errarg = 0;
82
13.3k
  Dwarf_Error *errp = 0;
83
13.3k
  int i = 0;
84
13.3k
  Dwarf_Die die = 0;
85
86
13.3k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
87
13.3k
  if (fuzz_fd != -1) {
88
13.3k
    res =
89
13.3k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
90
13.3k
    if (res == DW_DLV_OK) {
91
6.25k
      Dwarf_Bool is_info = 0;
92
6.25k
      Dwarf_Unsigned cu_header_length = 0;
93
6.25k
      Dwarf_Half version_stamp = 0;
94
6.25k
      Dwarf_Off abbrev_offset = 0;
95
6.25k
      Dwarf_Half address_size = 0;
96
6.25k
      Dwarf_Half length_size = 0;
97
6.25k
      Dwarf_Half extension_size = 0;
98
6.25k
      Dwarf_Sig8 type_signature;
99
6.25k
      Dwarf_Unsigned typeoffset = 0;
100
6.25k
      Dwarf_Unsigned next_cu_header_offset = 0;
101
6.25k
      Dwarf_Half header_cu_type = 0;
102
6.25k
      Dwarf_Die cu_die = 0;
103
6.25k
      static const Dwarf_Sig8 zerosignature;
104
105
6.25k
      type_signature = zerosignature;
106
6.25k
      res = dwarf_next_cu_header_d(
107
6.25k
          dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
108
6.25k
          &address_size, &length_size, &extension_size, &type_signature,
109
6.25k
          &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
110
6.25k
      if (res == DW_DLV_OK) {
111
3.28k
        res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
112
3.28k
        if (res == DW_DLV_OK) {
113
2.88k
          Dwarf_Unsigned unsign = 0;
114
2.88k
          res = dwarf_bytesize(cu_die, &unsign, errp);
115
2.88k
          res = dwarf_bitsize(cu_die, &unsign, errp);
116
2.88k
          res = dwarf_arrayorder(cu_die, &unsign, errp);
117
118
2.88k
          Dwarf_Off section_offset = 0;
119
2.88k
          Dwarf_Off local_offset = 0;
120
2.88k
          res = dwarf_die_offsets(cu_die, &section_offset, &local_offset, errp);
121
122
2.88k
          Dwarf_Off off = 0;
123
2.88k
          res = dwarf_dietype_offset(die, &off, &is_info, &error);
124
125
2.88k
          Dwarf_Off agoff = 0;
126
2.88k
          Dwarf_Unsigned acount = 0;
127
2.88k
          res = dwarf_die_abbrev_global_offset(cu_die, &agoff, &acount, errp);
128
129
2.88k
          Dwarf_Bool someinfo = dwarf_get_die_infotypes_flag(cu_die);
130
131
2.88k
          Dwarf_Off globaloff = 0;
132
2.88k
          Dwarf_Unsigned length = 0;
133
2.88k
          res = dwarf_die_CU_offset_range(cu_die, &globaloff, &length, errp);
134
135
2.88k
          Dwarf_Half address_size = 0;
136
2.88k
          res = dwarf_get_address_size(dbg, &address_size, errp);
137
138
2.88k
          Dwarf_Unsigned offcnt = 0;
139
2.88k
          Dwarf_Off *offbuf = 0;
140
2.88k
          res = dwarf_offset_list(dbg, section_offset, is_info, &offbuf,
141
2.88k
                                  &offcnt, errp);
142
143
2.88k
          Dwarf_Off die_goff = 0;
144
2.88k
          res = dwarf_dieoffset(cu_die, &die_goff, errp);
145
146
2.88k
          int abbrev_code = dwarf_die_abbrev_code(cu_die);
147
148
2.88k
          Dwarf_Bool has_attr = 0;
149
2.88k
          res = dwarf_hasattr(cu_die, DW_AT_external, &has_attr, errp);
150
151
2.88k
          Dwarf_Bool is_dwo = 0;
152
2.88k
          Dwarf_Half offset_size = 0;
153
2.88k
          Dwarf_Off offset_of_length = 0;
154
2.88k
          Dwarf_Unsigned total_byte_length = 0;
155
2.88k
          Dwarf_Sig8 *typesign = 0;
156
157
2.88k
          res = dwarf_cu_header_basics(
158
2.88k
              cu_die, &version_stamp, &is_info, &is_dwo, &offset_size,
159
2.88k
              &address_size, &extension_size, &typesign, &offset_of_length,
160
2.88k
              &total_byte_length, errp);
161
162
2.88k
          Dwarf_Debug_Fission_Per_CU percu;
163
2.88k
          memset(&percu, 0, sizeof(percu));
164
2.88k
          res = dwarf_get_debugfission_for_die(cu_die, &percu, errp);
165
2.88k
          char *name = 0;
166
2.88k
          Dwarf_Half tag = 0;
167
2.88k
          const char *tagname = 0;
168
2.88k
          int res = 0;
169
2.88k
          Dwarf_Attribute *atlist = 0;
170
2.88k
          Dwarf_Signed atcount = 0;
171
2.88k
          Dwarf_Attribute attr = 0;
172
2.88k
          Dwarf_Half formnum = 0;
173
2.88k
          const char *formname = "form-name-unavailable";
174
175
2.88k
          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
2.88k
          res = dwarf_diename(cu_die, &name, errp);
183
184
2.88k
          res = dwarf_get_TAG_name(tag, &tagname);
185
186
2.88k
          res = dwarf_attr(cu_die, DW_AT_name, &attr, errp);
187
2.88k
          if (res != DW_DLV_OK) {
188
655
            printf("Forcing close, "
189
655
              "Null attr due to error means all will fail "
190
655
              "and the list of errors is unreasonably long \n"); 
191
655
            fflush(stdout);
192
655
            dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
193
655
            dwarf_finish(dbg);
194
655
            close(fuzz_fd);
195
655
            return 0;
196
655
          }
197
2.23k
#if 1
198
2.23k
            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.23k
#endif
208
2.23k
          res = dwarf_attrlist(cu_die, &atlist, &atcount, errp);
209
22.1k
          for (i = 0; i < atcount; ++i) {
210
19.9k
            Dwarf_Half attrnum = 0;
211
19.9k
            const char *attrname = 0;
212
19.9k
            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
19.9k
            Dwarf_Bool *dw_returned_bool = 0;
225
19.9k
            res = dwarf_hasform(attr, formnum, dw_returned_bool, errp);
226
19.9k
            res = dwarf_get_FORM_name(formnum, &formname);
227
19.9k
            Dwarf_Block *tempb = 0;
228
19.9k
            res = dwarf_formblock(attr, &tempb, errp);
229
19.9k
            if (res == DW_DLV_OK) {
230
3.91k
              Dwarf_Dsc_Head h = 0;
231
              /* Dwarf_Unsigned u = 0; unused */
232
3.91k
              Dwarf_Unsigned arraycount = 0;
233
3.91k
              int sres = 0;
234
235
3.91k
              sres = dwarf_discr_list(dbg, (Dwarf_Small *)tempb->bl_data,
236
3.91k
                                      tempb->bl_len, &h, &arraycount, errp);
237
3.91k
            }
238
19.9k
            res = dwarf_whatattr(atlist[i], &attrnum, errp);
239
19.9k
            dwarf_get_AT_name(attrnum, &attrname);
240
19.9k
            dwarf_dealloc_attribute(atlist[i]);
241
19.9k
            atlist[i] = 0;
242
19.9k
            char *stringval = 0;
243
19.9k
            res = dwarf_bitoffset(cu_die, &attrnum, &unsign, errp);
244
19.9k
            res = dwarf_die_text(cu_die, attrnum, &stringval, errp);
245
19.9k
            res = dwarf_get_form_class(version_stamp, attrnum,
246
19.9k
                                       next_cu_header_offset, formnum);
247
19.9k
          }
248
2.23k
          res = dwarf_set_tied_dbg(dbg, NULL, errp);
249
2.23k
        }
250
2.62k
        dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
251
2.62k
      }
252
6.25k
    }
253
13.3k
  }
254
12.7k
  dwarf_finish(dbg);
255
12.7k
  close(fuzz_fd);
256
12.7k
  unlink(filename);
257
12.7k
  return 0;
258
13.3k
}