Coverage Report

Created: 2025-08-03 06:26

/src/libdwarf/fuzz/fuzz_debug_addr_access.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/stat.h>
18
#include <sys/types.h>
19
#include <unistd.h>
20
21
/*
22
 * Libdwarf library callers can only use these headers.
23
 */
24
#include "dwarf.h"
25
#include "libdwarf.h"
26
27
#ifndef O_BINARY
28
8.33k
#define O_BINARY 0
29
#endif
30
31
/*
32
 * Fuzzer function
33
 */
34
8.33k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
8.33k
  char filename[256];
36
8.33k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
37
38
8.33k
  FILE *fp = fopen(filename, "wb");
39
8.33k
  if (!fp) {
40
0
    return 0;
41
0
  }
42
8.33k
  fwrite(data, size, 1, fp);
43
8.33k
  fclose(fp);
44
45
8.33k
  int fuzz_fd = 0;
46
8.33k
  Dwarf_Ptr errarg = 0;
47
8.33k
  Dwarf_Handler errhand = 0;
48
8.33k
  Dwarf_Error *errp = 0;
49
8.33k
  Dwarf_Debug dbg = 0;
50
51
8.33k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
52
8.33k
  if (fuzz_fd != -1) {
53
8.33k
    int res =
54
8.33k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
55
56
8.33k
    if (res != DW_DLV_OK) {
57
7.06k
      dwarf_finish(dbg);
58
7.06k
      close(fuzz_fd);
59
7.06k
      unlink(filename);
60
7.06k
      return 0;
61
7.06k
    }
62
1.27k
    Dwarf_Debug_Addr_Table debug_address_table;
63
1.27k
    Dwarf_Unsigned dw_section_offset = 0;
64
1.27k
    Dwarf_Unsigned dw_debug_address_table_length = 0;
65
1.27k
    Dwarf_Half dw_version;
66
1.27k
    Dwarf_Small dw_address_size;
67
1.27k
    Dwarf_Unsigned dw_dw_at_addr_base;
68
1.27k
    Dwarf_Unsigned dw_entry_count;
69
1.27k
    Dwarf_Unsigned dw_next_table_offset;
70
1.27k
    res = dwarf_debug_addr_table(dbg, dw_section_offset, &debug_address_table,
71
1.27k
                                 &dw_debug_address_table_length, &dw_version,
72
1.27k
                                 &dw_address_size, &dw_dw_at_addr_base,
73
1.27k
                                 &dw_entry_count, &dw_next_table_offset, errp);
74
75
1.27k
    if (res != DW_DLV_OK) {
76
1.25k
      if (res == DW_DLV_ERROR) {
77
996
        if (errp) {
78
0
          dwarf_dealloc_error(dbg, *errp);
79
0
        }
80
996
      }
81
1.25k
      dwarf_finish(dbg);
82
1.25k
      close(fuzz_fd);
83
1.25k
      unlink(filename);
84
1.25k
      return 0;
85
1.25k
    }
86
145k
    for (Dwarf_Unsigned curindex = 0; curindex < dw_entry_count; ++curindex) {
87
145k
      Dwarf_Unsigned addr = 0;
88
145k
      res =
89
145k
          dwarf_debug_addr_by_index(debug_address_table, curindex, &addr, errp);
90
91
145k
      if (res != DW_DLV_OK) {
92
0
        break;
93
0
      }
94
145k
    }
95
18
    if (errp) {
96
0
        dwarf_dealloc_error(dbg, *errp);
97
0
    }
98
18
    dwarf_dealloc_debug_addr_table(debug_address_table);
99
18
    dwarf_finish(dbg);
100
18
    close(fuzz_fd);
101
18
  }
102
103
18
  unlink(filename);
104
18
  return 0;
105
8.33k
}