Coverage Report

Created: 2025-10-13 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_aranges.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
8.73k
#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
 * Helper function definitions.
32
 */
33
static void cleanup_bad_arange(Dwarf_Debug dbg, Dwarf_Arange *arange,
34
                               Dwarf_Signed i, Dwarf_Signed count);
35
int arange_processing_example(Dwarf_Debug dbg, Dwarf_Error *error);
36
37
/*
38
 * Fuzzer function
39
 */
40
8.73k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
41
8.73k
  char filename[256];
42
#ifdef DWREGRESSIONTEMP
43
  /* Under msys2, the /tmp/ results in an open fail */
44
  sprintf(filename, "junklibfuzzer.%d", getpid());
45
#else
46
8.73k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
47
8.73k
#endif
48
49
8.73k
  FILE *fp = fopen(filename, "wb");
50
8.73k
  if (!fp) {
51
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
52
0
        filename);
53
0
    return 0;
54
0
  }
55
8.73k
  fwrite(data, size, 1, fp);
56
8.73k
  fclose(fp);
57
58
8.73k
  Dwarf_Debug dbg = 0;
59
8.73k
  int res = DW_DLV_ERROR;
60
8.73k
  Dwarf_Error error = 0;
61
8.73k
  Dwarf_Handler errhand = 0;
62
8.73k
  Dwarf_Ptr errarg = 0;
63
64
8.73k
  int fd = open(filename, O_RDONLY | O_BINARY);
65
8.73k
  if (fd < 0) {
66
0
    exit(EXIT_FAILURE);
67
0
  }
68
69
8.73k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
70
71
8.73k
  if (res != DW_DLV_OK) {
72
7.27k
    dwarf_dealloc_error(dbg, error);
73
7.27k
  } else {
74
    /*  Adding newline avoids a warning from diff
75
        in regressiontesting */
76
1.46k
    printf("Processing\n");
77
1.46k
    arange_processing_example(dbg, &error);
78
1.46k
  }
79
80
8.73k
  dwarf_finish(dbg);
81
8.73k
  close(fd);
82
8.73k
  unlink(filename);
83
8.73k
  return 0;
84
8.73k
}
85
86
static void cleanup_bad_arange(Dwarf_Debug dbg, Dwarf_Arange *arange,
87
46
                               Dwarf_Signed i, Dwarf_Signed count) {
88
46
  Dwarf_Signed k = i;
89
595k
  for (; k < count; ++k) {
90
595k
    dwarf_dealloc(dbg, arange[k], DW_DLA_ARANGE);
91
595k
    arange[k] = 0;
92
595k
  }
93
46
}
94
95
// Source:
96
// https://www.prevanders.net/libdwarfdoc/group__aranges.html#ga9b628e21a71f4280f93788815796ef92
97
1.46k
int arange_processing_example(Dwarf_Debug dbg, Dwarf_Error *error) {
98
1.46k
  Dwarf_Signed count = 0;
99
1.46k
  Dwarf_Arange *arange = 0;
100
1.46k
  int res = 0;
101
102
1.46k
  res = dwarf_get_aranges(dbg, &arange, &count, error);
103
1.46k
  if (res == DW_DLV_OK) {
104
138
    Dwarf_Signed i = 0;
105
106
700k
    for (i = 0; i < count; ++i) {
107
699k
      Dwarf_Arange ara = arange[i];
108
699k
      Dwarf_Unsigned segment = 0;
109
699k
      Dwarf_Unsigned segment_entry_size = 0;
110
699k
      Dwarf_Addr start = 0;
111
699k
      Dwarf_Unsigned length = 0;
112
699k
      Dwarf_Off cu_die_offset = 0;
113
114
699k
      res = dwarf_get_arange_info_b(ara, &segment, &segment_entry_size, &start,
115
699k
                                    &length, &cu_die_offset, error);
116
699k
      if (res != DW_DLV_OK) {
117
46
        cleanup_bad_arange(dbg, arange, i, count);
118
46
        dwarf_dealloc(dbg, arange, DW_DLA_LIST);
119
46
        return res;
120
46
      }
121
699k
      dwarf_dealloc(dbg, ara, DW_DLA_ARANGE);
122
699k
      arange[i] = 0;
123
699k
    }
124
92
    dwarf_dealloc(dbg, arange, DW_DLA_LIST);
125
92
  }
126
1.41k
  return res;
127
1.46k
}