Coverage Report

Created: 2026-04-12 06:25

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
8.63k
#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
8.63k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28
8.63k
  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
8.63k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
8.63k
#endif
37
8.63k
  FILE *fp = fopen(filename, "wb");
38
8.63k
  if (!fp) {
39
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
40
0
        filename);
41
0
    return 0;
42
0
  }
43
8.63k
  fwrite(data, size, 1, fp);
44
8.63k
  fclose(fp);
45
46
8.63k
  Dwarf_Debug dbg = 0;
47
8.63k
  int res = DW_DLV_ERROR;
48
8.63k
  Dwarf_Error error = 0;
49
8.63k
  Dwarf_Handler errhand = 0;
50
8.63k
  Dwarf_Ptr errarg = 0;
51
8.63k
  int regtabrulecount = 0;
52
8.63k
  int curopt = 0;
53
54
8.63k
  int fd = open(filename, O_RDONLY | O_BINARY);
55
8.63k
  if (fd < 0) {
56
0
    exit(EXIT_FAILURE);
57
0
  }
58
59
8.63k
  res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
60
61
8.63k
  if (res != DW_DLV_OK) {
62
7.85k
    dwarf_dealloc_error(dbg, error);
63
7.85k
  } else {
64
774
    res = string_offsets_example(dbg, &error);
65
774
    if (res != DW_DLV_OK) {
66
577
    }
67
774
  }
68
69
8.63k
  dwarf_finish(dbg);
70
8.63k
  close(fd);
71
8.63k
  unlink(filename);
72
8.63k
  return 0;
73
8.63k
}
74
75
774
int string_offsets_example(Dwarf_Debug dbg, Dwarf_Error *error) {
76
774
  int res = 0;
77
774
  Dwarf_Str_Offsets_Table sot = 0;
78
774
  Dwarf_Unsigned wasted_byte_count = 0;
79
774
  Dwarf_Unsigned table_count = 0;
80
774
  Dwarf_Error closeerror = 0;
81
82
774
  res = dwarf_open_str_offsets_table_access(dbg, &sot, error);
83
774
  if (res == DW_DLV_NO_ENTRY) {
84
256
    return res;
85
256
  }
86
518
  if (res == DW_DLV_ERROR) {
87
129
    return res;
88
129
  }
89
738
  for (;;) {
90
738
    Dwarf_Unsigned unit_length = 0;
91
738
    Dwarf_Unsigned unit_length_offset = 0;
92
738
    Dwarf_Unsigned table_start_offset = 0;
93
738
    Dwarf_Half entry_size = 0;
94
738
    Dwarf_Half version = 0;
95
738
    Dwarf_Half padding = 0;
96
738
    Dwarf_Unsigned table_value_count = 0;
97
738
    Dwarf_Unsigned i = 0;
98
738
    Dwarf_Unsigned table_entry_value = 0;
99
100
738
    res = dwarf_next_str_offsets_table(
101
738
        sot, &unit_length, &unit_length_offset, &table_start_offset,
102
738
        &entry_size, &version, &padding, &table_value_count, error);
103
738
    if (res == DW_DLV_NO_ENTRY) {
104
197
      break;
105
197
    }
106
541
    if (res == DW_DLV_ERROR) {
107
192
      dwarf_close_str_offsets_table_access(sot, &closeerror);
108
192
      dwarf_dealloc_error(dbg, closeerror);
109
192
      return res;
110
192
    }
111
1.95M
    for (i = 0; i < table_value_count; ++i) {
112
1.95M
      res = dwarf_str_offsets_value_by_index(sot, i, &table_entry_value, error);
113
1.95M
      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
1.95M
    }
119
349
  }
120
197
  res = dwarf_str_offsets_statistics(sot, &wasted_byte_count, &table_count,
121
197
                                     error);
122
197
  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
197
  res = dwarf_close_str_offsets_table_access(sot, error);
128
197
  sot = 0;
129
197
  return res;
130
197
}