Coverage Report

Created: 2025-07-11 06:09

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