Coverage Report

Created: 2025-09-27 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_globals.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
24.5k
#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 get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error);
31
int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error);
32
int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error);
33
34
/*
35
 * A fuzzer that simulates a small part of the simplereader.c example.
36
 */
37
24.5k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
38
24.5k
  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
24.5k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
46
24.5k
#endif
47
24.5k
  FILE *fp = fopen(filename, "wb");
48
24.5k
  if (!fp) {
49
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
50
0
        filename);
51
0
    return 0;
52
0
  }
53
24.5k
  fwrite(data, size, 1, fp);
54
24.5k
  fclose(fp);
55
56
24.5k
  Dwarf_Debug dbg = 0;
57
24.5k
  int res = DW_DLV_ERROR;
58
24.5k
  Dwarf_Error error = 0;
59
24.5k
  Dwarf_Handler errhand = 0;
60
24.5k
  Dwarf_Ptr errarg = 0;
61
62
24.5k
  int fd = open(filename, O_RDONLY | O_BINARY);
63
24.5k
  if (fd < 0) {
64
0
    exit(EXIT_FAILURE);
65
0
  }
66
67
24.5k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
68
69
24.5k
  if (res != DW_DLV_OK) {
70
20.0k
    dwarf_dealloc_error(dbg, error);
71
20.0k
  } else {
72
4.52k
    dwarf_return_empty_pubnames(dbg, 1);
73
4.52k
    dwarf_return_empty_pubnames(dbg, 0);
74
4.52k
    get_globals_example(dbg, &error);
75
4.52k
    get_globals_by_type_example(dbg, &error);
76
4.52k
  }
77
78
24.5k
  dwarf_finish(dbg);
79
24.5k
  close(fd);
80
24.5k
  unlink(filename);
81
24.5k
  return 0;
82
24.5k
}
83
84
2.45k
int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error) {
85
2.45k
  Dwarf_Signed count = 0;
86
2.45k
  Dwarf_Global *globs = 0;
87
2.45k
  Dwarf_Signed i = 0;
88
2.45k
  int res = 0;
89
90
2.45k
  res = dwarf_get_globals(dbg, &globs, &count, error);
91
2.45k
  if (res != DW_DLV_OK) {
92
1.86k
    return res;
93
1.86k
  }
94
14.2k
  for (i = 0; i < count; ++i) {
95
13.7k
    int tag_idx = dwarf_global_tag_number(globs[i]); // DWARF5 only
96
97
13.7k
    char *name = 0;
98
13.7k
    res = dwarf_globname(globs[i], &name, error);
99
13.7k
    if (res != DW_DLV_OK) {
100
0
      continue;
101
0
    }
102
103
13.7k
    Dwarf_Off dw_die_offset;
104
13.7k
    res = dwarf_global_die_offset(globs[i], &dw_die_offset, error);
105
13.7k
    if (res != DW_DLV_OK) {
106
0
      continue;
107
0
    }
108
13.7k
    Dwarf_Off dw_cu_offset;
109
13.7k
    res = dwarf_global_cu_offset(globs[i], &dw_cu_offset, error);
110
13.7k
    if (res != DW_DLV_OK) {
111
0
      continue;
112
0
    }
113
114
13.7k
    char *name_2;
115
13.7k
    Dwarf_Off dw_die_offset_2, dw_cu_offset_2;
116
13.7k
    dwarf_global_name_offsets(globs[i], &name_2, &dw_die_offset_2,
117
13.7k
                              &dw_cu_offset_2, error);
118
13.7k
    if (res != DW_DLV_OK) {
119
0
      continue;
120
0
    }
121
122
13.7k
    int dw_category;
123
13.7k
    Dwarf_Off dw_offset_pub_header;
124
13.7k
    Dwarf_Unsigned dw_length_size;
125
13.7k
    Dwarf_Unsigned dw_length_pub;
126
13.7k
    Dwarf_Unsigned dw_version;
127
13.7k
    Dwarf_Unsigned dw_header_info_offset;
128
13.7k
    Dwarf_Unsigned dw_info_length;
129
13.7k
    res = dwarf_get_globals_header(
130
13.7k
        globs[i], &dw_category, &dw_offset_pub_header, &dw_length_size,
131
13.7k
        &dw_length_pub, &dw_version, &dw_header_info_offset, &dw_info_length,
132
13.7k
        error);
133
13.7k
  }
134
582
  dwarf_globals_dealloc(dbg, globs, count);
135
582
  return DW_DLV_OK;
136
2.45k
}
137
138
/* DWARF4 */
139
2.45k
int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error) {
140
2.45k
  int res = DW_DLV_OK;
141
17.1k
  for (int i = 0; i < 6; i++) {
142
14.7k
    Dwarf_Signed count = 0;
143
14.7k
    Dwarf_Global *contents = 0;
144
14.7k
    Dwarf_Signed i = 0;
145
146
14.7k
    res = dwarf_globals_by_type(dbg, i, &contents, &count, error);
147
148
14.7k
    dwarf_globals_dealloc(dbg, contents, count);
149
14.7k
  }
150
151
2.45k
  return res;
152
2.45k
}
153
154
/* DWARF4 */
155
0
int get_pubtypes_example(Dwarf_Debug dbg, Dwarf_Error *error) {
156
0
  Dwarf_Signed count = 0;
157
0
  Dwarf_Global *contents = 0;
158
159
0
  int res = dwarf_get_pubtypes(dbg, &contents, &count, error);
160
161
0
  dwarf_globals_dealloc(dbg, contents, count);
162
163
0
  return res;
164
0
}