Coverage Report

Created: 2025-09-27 06:39

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
7.77k
#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
7.77k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34
7.77k
  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
7.77k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
42
7.77k
#endif
43
7.77k
  FILE *fp = fopen(filename, "wb");
44
7.77k
  if (!fp) {
45
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
46
0
        filename);
47
0
    return 0;
48
0
  }
49
7.77k
  fwrite(data, size, 1, fp);
50
7.77k
  fclose(fp);
51
52
7.77k
  Dwarf_Debug dbg = 0;
53
7.77k
  int res = DW_DLV_ERROR;
54
7.77k
  Dwarf_Error error = 0;
55
7.77k
  Dwarf_Handler errhand = 0;
56
7.77k
  Dwarf_Ptr errarg = 0;
57
58
7.77k
  int fd = open(filename, O_RDONLY | O_BINARY);
59
7.77k
  if (fd < 0) {
60
0
    exit(EXIT_FAILURE);
61
0
  }
62
63
7.77k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
64
65
7.77k
  if (res != DW_DLV_OK) {
66
6.49k
    if (res == DW_DLV_ERROR) {
67
4.75k
    }
68
6.49k
    dwarf_dealloc_error(dbg, error);
69
6.49k
  } else {
70
1.27k
    Dwarf_Bool dw_which_section = 0;
71
1.27k
    Dwarf_Gnu_Index_Head dw_head;
72
1.27k
    Dwarf_Unsigned dw_index_block_count;
73
74
1.27k
    res = dwarf_get_gnu_index_head(dbg, dw_which_section, &dw_head,
75
1.27k
                                   &dw_index_block_count, &error);
76
77
1.27k
    if (res == DW_DLV_NO_ENTRY) {
78
1.03k
    } else if (res == DW_DLV_ERROR) {
79
988
      dwarf_dealloc_error(dbg, error);
80
988
    } else {
81
48
      Dwarf_Unsigned dw_block_length;
82
48
      Dwarf_Half dw_version;
83
48
      Dwarf_Unsigned dw_offset_into_debug_info;
84
48
      Dwarf_Unsigned dw_size_of_debug_info_area;
85
48
      Dwarf_Unsigned dw_count_of_index_entries;
86
110
      for (Dwarf_Unsigned block_number = 0; block_number < dw_index_block_count;
87
62
           block_number++) {
88
62
        res = dwarf_get_gnu_index_block(dw_head, block_number, &dw_block_length,
89
62
                                        &dw_version, &dw_offset_into_debug_info,
90
62
                                        &dw_size_of_debug_info_area,
91
62
                                        &dw_count_of_index_entries, &error);
92
93
62
        if (res == DW_DLV_NO_ENTRY) {
94
0
          continue;
95
62
        } else if (res == DW_DLV_ERROR) {
96
0
          break;
97
0
        }
98
62
        for (Dwarf_Unsigned entry_number = 0;
99
17.8k
             entry_number < dw_count_of_index_entries; entry_number++) {
100
17.8k
          Dwarf_Unsigned dw_offset_in_debug_info;
101
17.8k
          const char *dw_name_string;
102
17.8k
          unsigned char dw_flagbyte;
103
17.8k
          unsigned char dw_staticorglobal;
104
17.8k
          unsigned char dw_typeofentry;
105
17.8k
          res = dwarf_get_gnu_index_block_entry(
106
17.8k
              dw_head, block_number, entry_number, &dw_offset_in_debug_info,
107
17.8k
              &dw_name_string, &dw_flagbyte, &dw_staticorglobal,
108
17.8k
              &dw_typeofentry, &error);
109
17.8k
        }
110
62
      }
111
48
      dwarf_gnu_index_dealloc(dw_head);
112
48
    }
113
1.27k
  }
114
115
7.77k
  dwarf_finish(dbg);
116
7.77k
  close(fd);
117
7.77k
  unlink(filename);
118
7.77k
  return 0;
119
7.77k
}