Coverage Report

Created: 2026-09-01 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_gnu_index.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
9.19k
#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
/*
31
 * A fuzzer that simulates a small part of the simplereader.c example.
32
 */
33
9.19k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34
9.19k
  char filename[256];
35
36
#ifdef DWREGRESSIONTEMP
37
  /*  Under msys2, the /tmp/ results in an open fail,
38
      so we discard the /tmp/ here */
39
  sprintf(filename, "junklibfuzzer.%d", getpid());
40
#else
41
9.19k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
42
9.19k
#endif
43
9.19k
  FILE *fp = fopen(filename, "wb");
44
9.19k
  if (!fp) {
45
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
46
0
        filename);
47
0
    return 0;
48
0
  }
49
9.19k
  fwrite(data, size, 1, fp);
50
9.19k
  fclose(fp);
51
52
9.19k
  Dwarf_Debug dbg = 0;
53
9.19k
  int res = DW_DLV_ERROR;
54
9.19k
  Dwarf_Error error = 0;
55
9.19k
  Dwarf_Handler errhand = 0;
56
9.19k
  Dwarf_Ptr errarg = 0;
57
58
9.19k
  int fd = open(filename, O_RDONLY | O_BINARY);
59
9.19k
  if (fd < 0) {
60
0
    exit(EXIT_FAILURE);
61
0
  }
62
63
9.19k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
64
65
9.19k
  if (res != DW_DLV_OK) {
66
7.63k
    if (res == DW_DLV_ERROR) {
67
5.64k
    }
68
7.63k
    dwarf_dealloc_error(dbg, error);
69
7.63k
  } else {
70
1.55k
    Dwarf_Bool dw_which_section = 0;
71
1.55k
    Dwarf_Gnu_Index_Head dw_head;
72
1.55k
    Dwarf_Unsigned dw_index_block_count;
73
74
1.55k
    res = dwarf_get_gnu_index_head(dbg, dw_which_section, &dw_head,
75
1.55k
                                   &dw_index_block_count, &error);
76
77
1.55k
    if (res == DW_DLV_NO_ENTRY) {
78
1.34k
    } else if (res == DW_DLV_ERROR) {
79
1.31k
      dwarf_dealloc_error(dbg, error);
80
1.31k
    } else {
81
34
      Dwarf_Unsigned dw_block_length;
82
34
      Dwarf_Half dw_version;
83
34
      Dwarf_Unsigned dw_offset_into_debug_info;
84
34
      Dwarf_Unsigned dw_size_of_debug_info_area;
85
34
      Dwarf_Unsigned dw_count_of_index_entries;
86
83
      for (Dwarf_Unsigned block_number = 0; block_number < dw_index_block_count;
87
49
           block_number++) {
88
49
        res = dwarf_get_gnu_index_block(dw_head, block_number, &dw_block_length,
89
49
                                        &dw_version, &dw_offset_into_debug_info,
90
49
                                        &dw_size_of_debug_info_area,
91
49
                                        &dw_count_of_index_entries, &error);
92
93
49
        if (res == DW_DLV_NO_ENTRY) {
94
0
          continue;
95
49
        } else if (res == DW_DLV_ERROR) {
96
0
          break;
97
0
        }
98
49
        for (Dwarf_Unsigned entry_number = 0;
99
24.5k
             entry_number < dw_count_of_index_entries; entry_number++) {
100
24.4k
          Dwarf_Unsigned dw_offset_in_debug_info;
101
24.4k
          const char *dw_name_string;
102
24.4k
          unsigned char dw_flagbyte;
103
24.4k
          unsigned char dw_staticorglobal;
104
24.4k
          unsigned char dw_typeofentry;
105
24.4k
          res = dwarf_get_gnu_index_block_entry(
106
24.4k
              dw_head, block_number, entry_number, &dw_offset_in_debug_info,
107
24.4k
              &dw_name_string, &dw_flagbyte, &dw_staticorglobal,
108
24.4k
              &dw_typeofentry, &error);
109
24.4k
        }
110
49
      }
111
34
      dwarf_gnu_index_dealloc(dw_head);
112
34
    }
113
1.55k
  }
114
115
9.19k
  dwarf_finish(dbg);
116
9.19k
  close(fd);
117
9.19k
  unlink(filename);
118
9.19k
  return 0;
119
9.19k
}