Coverage Report

Created: 2025-07-18 06:39

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