Coverage Report

Created: 2026-02-26 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_str_offsets.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 "dwarf.h"
13
#include "libdwarf.h"
14
#include <fcntl.h> /* open() O_RDONLY O_BINARY */
15
#include <stdint.h>
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <sys/types.h>
20
#include <unistd.h>
21
22
#ifndef O_BINARY
23
7.70k
#define O_BINARY 0 /* So it does nothing in Linux/Unix */
24
#endif
25
26
int string_offsets_example(Dwarf_Debug dbg, Dwarf_Error *error);
27
7.70k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28
7.70k
  char filename[256];
29
30
#ifdef DWREGRESSIONTEMP
31
  /*  Under msys2, the /tmp/ results in an open fail,
32
      so we discard the /tmp/ here */
33
  sprintf(filename, "junklibfuzzer.%d", getpid());
34
#else
35
7.70k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
7.70k
#endif
37
7.70k
  FILE *fp = fopen(filename, "wb");
38
7.70k
  if (!fp) {
39
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
40
0
        filename);
41
0
    return 0;
42
0
  }
43
7.70k
  fwrite(data, size, 1, fp);
44
7.70k
  fclose(fp);
45
46
7.70k
  Dwarf_Debug dbg = 0;
47
7.70k
  int res = DW_DLV_ERROR;
48
7.70k
  Dwarf_Error error = 0;
49
7.70k
  Dwarf_Handler errhand = 0;
50
7.70k
  Dwarf_Ptr errarg = 0;
51
7.70k
  int regtabrulecount = 0;
52
7.70k
  int curopt = 0;
53
54
7.70k
  int fd = open(filename, O_RDONLY | O_BINARY);
55
7.70k
  if (fd < 0) {
56
0
    exit(EXIT_FAILURE);
57
0
  }
58
59
7.70k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
60
61
7.70k
  if (res != DW_DLV_OK) {
62
6.99k
    dwarf_dealloc_error(dbg, error);
63
6.99k
  } else {
64
706
    res = string_offsets_example(dbg, &error);
65
706
    if (res != DW_DLV_OK) {
66
508
    }
67
706
  }
68
69
7.70k
  dwarf_finish(dbg);
70
7.70k
  close(fd);
71
7.70k
  unlink(filename);
72
7.70k
  return 0;
73
7.70k
}
74
75
706
int string_offsets_example(Dwarf_Debug dbg, Dwarf_Error *error) {
76
706
  int res = 0;
77
706
  Dwarf_Str_Offsets_Table sot = 0;
78
706
  Dwarf_Unsigned wasted_byte_count = 0;
79
706
  Dwarf_Unsigned table_count = 0;
80
706
  Dwarf_Error closeerror = 0;
81
82
706
  res = dwarf_open_str_offsets_table_access(dbg, &sot, error);
83
706
  if (res == DW_DLV_NO_ENTRY) {
84
227
    return res;
85
227
  }
86
479
  if (res == DW_DLV_ERROR) {
87
96
    return res;
88
96
  }
89
709
  for (;;) {
90
709
    Dwarf_Unsigned unit_length = 0;
91
709
    Dwarf_Unsigned unit_length_offset = 0;
92
709
    Dwarf_Unsigned table_start_offset = 0;
93
709
    Dwarf_Half entry_size = 0;
94
709
    Dwarf_Half version = 0;
95
709
    Dwarf_Half padding = 0;
96
709
    Dwarf_Unsigned table_value_count = 0;
97
709
    Dwarf_Unsigned i = 0;
98
709
    Dwarf_Unsigned table_entry_value = 0;
99
100
709
    res = dwarf_next_str_offsets_table(
101
709
        sot, &unit_length, &unit_length_offset, &table_start_offset,
102
709
        &entry_size, &version, &padding, &table_value_count, error);
103
709
    if (res == DW_DLV_NO_ENTRY) {
104
198
      break;
105
198
    }
106
511
    if (res == DW_DLV_ERROR) {
107
185
      dwarf_close_str_offsets_table_access(sot, &closeerror);
108
185
      dwarf_dealloc_error(dbg, closeerror);
109
185
      return res;
110
185
    }
111
2.31M
    for (i = 0; i < table_value_count; ++i) {
112
2.31M
      res = dwarf_str_offsets_value_by_index(sot, i, &table_entry_value, error);
113
2.31M
      if (res != DW_DLV_OK) {
114
0
        dwarf_close_str_offsets_table_access(sot, &closeerror);
115
0
        dwarf_dealloc_error(dbg, closeerror);
116
0
        return res;
117
0
      }
118
2.31M
    }
119
326
  }
120
198
  res = dwarf_str_offsets_statistics(sot, &wasted_byte_count, &table_count,
121
198
                                     error);
122
198
  if (res != DW_DLV_OK) {
123
0
    dwarf_close_str_offsets_table_access(sot, &closeerror);
124
0
    dwarf_dealloc_error(dbg, closeerror);
125
0
    return res;
126
0
  }
127
198
  res = dwarf_close_str_offsets_table_access(sot, error);
128
198
  sot = 0;
129
198
  return res;
130
198
}