Coverage Report

Created: 2025-08-29 06:51

/src/libdwarf/fuzz/fuzz_globals.c
Line
Count
Source (jump to first uncovered line)
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
28.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
28.5k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
38
28.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
28.5k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
46
28.5k
#endif
47
28.5k
  FILE *fp = fopen(filename, "wb");
48
28.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
28.5k
  fwrite(data, size, 1, fp);
54
28.5k
  fclose(fp);
55
56
28.5k
  Dwarf_Debug dbg = 0;
57
28.5k
  int res = DW_DLV_ERROR;
58
28.5k
  Dwarf_Error error = 0;
59
28.5k
  Dwarf_Handler errhand = 0;
60
28.5k
  Dwarf_Ptr errarg = 0;
61
62
28.5k
  int fd = open(filename, O_RDONLY | O_BINARY);
63
28.5k
  if (fd < 0) {
64
0
    exit(EXIT_FAILURE);
65
0
  }
66
67
28.5k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
68
69
28.5k
  if (res != DW_DLV_OK) {
70
22.4k
    dwarf_dealloc_error(dbg, error);
71
22.4k
  } else {
72
6.06k
    dwarf_return_empty_pubnames(dbg, 1);
73
6.06k
    dwarf_return_empty_pubnames(dbg, 0);
74
6.06k
    get_globals_example(dbg, &error);
75
6.06k
    get_globals_by_type_example(dbg, &error);
76
6.06k
  }
77
78
28.5k
  dwarf_finish(dbg);
79
28.5k
  close(fd);
80
28.5k
  unlink(filename);
81
28.5k
  return 0;
82
28.5k
}
83
84
3.52k
int get_globals_example(Dwarf_Debug dbg, Dwarf_Error *error) {
85
3.52k
  Dwarf_Signed count = 0;
86
3.52k
  Dwarf_Global *globs = 0;
87
3.52k
  Dwarf_Signed i = 0;
88
3.52k
  int res = 0;
89
90
3.52k
  res = dwarf_get_globals(dbg, &globs, &count, error);
91
3.52k
  if (res != DW_DLV_OK) {
92
2.61k
    return res;
93
2.61k
  }
94
10.7k
  for (i = 0; i < count; ++i) {
95
9.79k
    int tag_idx = dwarf_global_tag_number(globs[i]); // DWARF5 only
96
97
9.79k
    char *name = 0;
98
9.79k
    res = dwarf_globname(globs[i], &name, error);
99
9.79k
    if (res != DW_DLV_OK) {
100
0
      continue;
101
0
    }
102
103
9.79k
    Dwarf_Off dw_die_offset;
104
9.79k
    res = dwarf_global_die_offset(globs[i], &dw_die_offset, error);
105
9.79k
    if (res != DW_DLV_OK) {
106
0
      continue;
107
0
    }
108
9.79k
    Dwarf_Off dw_cu_offset;
109
9.79k
    res = dwarf_global_cu_offset(globs[i], &dw_cu_offset, error);
110
9.79k
    if (res != DW_DLV_OK) {
111
0
      continue;
112
0
    }
113
114
9.79k
    char *name_2;
115
9.79k
    Dwarf_Off dw_die_offset_2, dw_cu_offset_2;
116
9.79k
    dwarf_global_name_offsets(globs[i], &name_2, &dw_die_offset_2,
117
9.79k
                              &dw_cu_offset_2, error);
118
9.79k
    if (res != DW_DLV_OK) {
119
0
      continue;
120
0
    }
121
122
9.79k
    int dw_category;
123
9.79k
    Dwarf_Off dw_offset_pub_header;
124
9.79k
    Dwarf_Unsigned dw_length_size;
125
9.79k
    Dwarf_Unsigned dw_length_pub;
126
9.79k
    Dwarf_Unsigned dw_version;
127
9.79k
    Dwarf_Unsigned dw_header_info_offset;
128
9.79k
    Dwarf_Unsigned dw_info_length;
129
9.79k
    res = dwarf_get_globals_header(
130
9.79k
        globs[i], &dw_category, &dw_offset_pub_header, &dw_length_size,
131
9.79k
        &dw_length_pub, &dw_version, &dw_header_info_offset, &dw_info_length,
132
9.79k
        error);
133
9.79k
  }
134
912
  dwarf_globals_dealloc(dbg, globs, count);
135
912
  return DW_DLV_OK;
136
3.52k
}
137
138
/* DWARF4 */
139
3.52k
int get_globals_by_type_example(Dwarf_Debug dbg, Dwarf_Error *error) {
140
3.52k
  int res = DW_DLV_OK;
141
24.6k
  for (int i = 0; i < 6; i++) {
142
21.1k
    Dwarf_Signed count = 0;
143
21.1k
    Dwarf_Global *contents = 0;
144
21.1k
    Dwarf_Signed i = 0;
145
146
21.1k
    res = dwarf_globals_by_type(dbg, i, &contents, &count, error);
147
148
21.1k
    dwarf_globals_dealloc(dbg, contents, count);
149
21.1k
  }
150
151
3.52k
  return res;
152
3.52k
}
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
}