Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_srcfiles.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 "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/stat.h>
20
#include <sys/types.h>
21
#include <unistd.h>
22
23
#ifndef O_BINARY
24
36.2k
#define O_BINARY 0
25
#endif
26
27
int examplee(Dwarf_Debug dbg, Dwarf_Die somedie, Dwarf_Error *error);
28
int exampled(Dwarf_Die somedie, Dwarf_Error *error);
29
36.2k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
30
36.2k
  char filename[256];
31
#ifdef DWREGRESSIONTEMP
32
  /* Under msys2, the /tmp/ results in an open fail */
33
  sprintf(filename, "junklibfuzzer.%d", getpid());
34
#else
35
36.2k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
36.2k
#endif
37
38
36.2k
  FILE *fp = fopen(filename, "wb");
39
36.2k
  if (!fp) {
40
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
41
0
        filename);
42
0
    return EXIT_FAILURE;
43
0
  }
44
36.2k
  fwrite(data, size, 1, fp);
45
36.2k
  fclose(fp);
46
47
36.2k
  Dwarf_Debug dbg = 0;
48
36.2k
  int fuzz_fd = 0;
49
36.2k
  int res = DW_DLV_ERROR;
50
36.2k
  Dwarf_Error error = 0;
51
36.2k
  Dwarf_Handler errhand = 0;
52
36.2k
  Dwarf_Ptr errarg = 0;
53
36.2k
  Dwarf_Sig8 hash8;
54
36.2k
  Dwarf_Error *errp = 0;
55
36.2k
  int simpleerrhand = 0;
56
36.2k
  int i = 0;
57
36.2k
  Dwarf_Die die;
58
59
36.2k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
60
36.2k
  if (fuzz_fd != -1) {
61
36.2k
    res =
62
36.2k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
63
36.2k
    if (res == DW_DLV_OK) {
64
17.8k
      Dwarf_Bool is_info = 0;
65
17.8k
      Dwarf_Unsigned cu_header_length = 0;
66
17.8k
      Dwarf_Half version_stamp = 0;
67
17.8k
      Dwarf_Off abbrev_offset = 0;
68
17.8k
      Dwarf_Half address_size = 0;
69
17.8k
      Dwarf_Half length_size = 0;
70
17.8k
      Dwarf_Half extension_size = 0;
71
17.8k
      Dwarf_Sig8 type_signature;
72
17.8k
      Dwarf_Unsigned typeoffset = 0;
73
17.8k
      Dwarf_Unsigned next_cu_header_offset = 0;
74
17.8k
      Dwarf_Half header_cu_type = 0;
75
17.8k
      int res = 0;
76
17.8k
      Dwarf_Die cu_die = 0;
77
17.8k
      int level = 0;
78
17.8k
      static const Dwarf_Sig8 zerosignature;
79
80
17.8k
      type_signature = zerosignature;
81
17.8k
      res = dwarf_next_cu_header_d(
82
17.8k
          dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
83
17.8k
          &address_size, &length_size, &extension_size, &type_signature,
84
17.8k
          &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
85
17.8k
      if (res == DW_DLV_OK) {
86
7.29k
        res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
87
7.29k
        if (res == DW_DLV_OK) {
88
5.98k
          examplee(dbg, cu_die, errp);
89
5.98k
          exampled(cu_die, errp);
90
5.98k
        } else {
91
1.30k
        }
92
93
7.29k
        dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
94
7.29k
      }
95
17.8k
    }
96
36.2k
  }
97
36.2k
  dwarf_finish(dbg);
98
36.2k
  close(fuzz_fd);
99
36.2k
  unlink(filename);
100
36.2k
  return 0;
101
36.2k
}
102
103
4.45k
int examplee(Dwarf_Debug dbg, Dwarf_Die somedie, Dwarf_Error *error) {
104
4.45k
  Dwarf_Signed count = 0;
105
4.45k
  char **srcfiles = 0;
106
4.45k
  Dwarf_Signed i = 0;
107
4.45k
  int res = 0;
108
109
4.45k
  res = dwarf_srcfiles(somedie, &srcfiles, &count, error);
110
4.45k
  if (res != DW_DLV_OK) {
111
4.22k
    return res;
112
4.22k
  }
113
5.15k
  for (i = 0; i < count; ++i) {
114
4.92k
    dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING);
115
4.92k
  }
116
231
  dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
117
231
  return DW_DLV_OK;
118
4.45k
}
119
120
4.45k
int exampled(Dwarf_Die somedie, Dwarf_Error *error) {
121
4.45k
  Dwarf_Signed count = 0;
122
4.45k
  Dwarf_Line_Context context = 0;
123
4.45k
  Dwarf_Line *linebuf = 0;
124
4.45k
  Dwarf_Signed i = 0;
125
4.45k
  Dwarf_Line line;
126
4.45k
  Dwarf_Small table_count = 0;
127
4.45k
  Dwarf_Unsigned version = 0;
128
4.45k
  int sres = 0;
129
130
4.45k
  int lineheader_errcount = 0;
131
4.45k
  dwarf_check_lineheader_b(somedie, &lineheader_errcount, error);
132
4.45k
  dwarf_print_lines(somedie, error, &lineheader_errcount);
133
134
4.45k
  sres = dwarf_srclines_b(somedie, &version, &table_count, &context, error);
135
4.45k
  if (sres != DW_DLV_OK) {
136
3.86k
    return sres;
137
3.86k
  }
138
594
  sres = dwarf_srclines_from_linecontext(context, &linebuf, &count, error);
139
594
  if (sres != DW_DLV_OK) {
140
0
    dwarf_srclines_dealloc_b(context);
141
0
    return sres;
142
0
  }
143
144
594
  Dwarf_Line *dw_linebuf_actuals = 0;  /* init by davea*/
145
594
  Dwarf_Signed dw_linecount_actuals = 0;  /* init by davea*/
146
147
594
  sres = dwarf_srclines_two_level_from_linecontext(
148
594
      context, &linebuf, &count, &dw_linebuf_actuals, &dw_linecount_actuals,
149
594
      error);
150
594
  if (sres != DW_DLV_OK) {
151
0
    dwarf_srclines_dealloc_b(context);
152
0
    return sres;
153
0
  }
154
155
594
  Dwarf_Unsigned dw_context_section_offset = 0;  /* init by davea*/
156
594
  sres =
157
594
      dwarf_srclines_table_offset(context, &dw_context_section_offset, error);
158
594
  if (sres != DW_DLV_OK) {
159
0
    dwarf_srclines_dealloc_b(context);
160
0
    return sres;
161
0
  }
162
163
594
  const char *dw_compilation_directory = 0;  /* init by davea*/
164
594
  sres = dwarf_srclines_comp_dir(context, &dw_compilation_directory, error);
165
594
  if (sres != DW_DLV_OK) {
166
0
    dwarf_srclines_dealloc_b(context);
167
0
    return sres;
168
0
  }
169
170
594
  Dwarf_Signed subprogram_count = 0;  /* init by davea*/
171
594
  sres = dwarf_srclines_subprog_count(context,
172
594
      &subprogram_count, error);
173
594
  if (sres != DW_DLV_OK) {
174
0
    dwarf_srclines_dealloc_b(context);
175
0
    return sres;
176
0
  }
177
178
594
  Dwarf_Unsigned version_2 = 0;  /* init by davea*/
179
594
  Dwarf_Small table_count_2 = 0;  /* init by davea*/
180
594
  dwarf_srclines_version(context, &version_2, &table_count_2, error);
181
182
594
  Dwarf_Signed dw_baseindex = 0;  /* init by davea*/
183
594
  Dwarf_Signed dw_count = 0;  /* init by davea*/
184
594
  Dwarf_Signed dw_endindex = 0; /* init by davea*/
185
594
  sres = dwarf_srclines_files_indexes(context,
186
594
      &dw_baseindex, &dw_count,
187
594
      &dw_endindex, error);
188
594
  if (sres != DW_DLV_OK) {
189
0
    dwarf_srclines_dealloc_b(context);
190
0
    return sres;
191
0
  }
192
193
5.03k
  for (i = 0; i < subprogram_count; i++) {
194
4.43k
    const char *dw_name = 0;  /* init by davea*/
195
4.43k
    Dwarf_Unsigned dw_decl_file = 0;  /* init by davea*/
196
4.43k
    Dwarf_Unsigned dw_decl_line = 0;  /* init by davea*/
197
4.43k
    sres = dwarf_srclines_subprog_data(context, i + 1,
198
4.43k
        &dw_name, &dw_decl_file,
199
4.43k
        &dw_decl_line, error);
200
4.43k
    if (sres != DW_DLV_OK) {
201
0
      continue;
202
0
    }
203
4.43k
  }
204
205
231k
  for (i = 0; i < count; ++i) {
206
230k
    line = linebuf[i];
207
208
230k
    Dwarf_Bool ans = 0; /* init by davea */
209
230k
    Dwarf_Unsigned linenum = 0; /* init by davea */
210
230k
    dwarf_linebeginstatement(line, &ans, error);
211
230k
    dwarf_lineendsequence(line, &ans, error);
212
230k
    dwarf_line_is_addr_set(line, &ans, error);
213
214
230k
    dwarf_lineno(line, &linenum, error);
215
230k
    dwarf_line_srcfileno(line, &linenum, error);
216
230k
    dwarf_lineoff_b(line, &linenum, error);
217
218
230k
    char *linesrc = 0; /* INIT by davea */
219
220
230k
    dwarf_linesrc(line, &linesrc, error);
221
222
230k
    Dwarf_Bool prologue_end = 0;  /* init by davea*/
223
230k
    Dwarf_Bool epilogue_begin = 0;  /* init by davea*/
224
230k
    Dwarf_Unsigned isa = 0; /* init by davea*/
225
230k
    Dwarf_Unsigned discriminator = 0; /* init by davea*/
226
230k
    dwarf_prologue_end_etc(line, &prologue_end, &epilogue_begin, &isa,
227
230k
                           &discriminator, error);
228
229
230k
#if 1 /* this is problematic and does not work */
230
230k
    Dwarf_Unsigned l_logical = 0;  /* init by davea*/
231
230k
    dwarf_linelogical(line, &l_logical, error);
232
233
230k
    Dwarf_Unsigned subprog_no = 0;  /* init by davea*/
234
230k
    dwarf_line_subprogno(line, &subprog_no, error);
235
230k
#endif
236
230k
  }
237
594
  dwarf_srclines_dealloc_b(context);
238
594
  return DW_DLV_OK;
239
594
}