Coverage Report

Created: 2023-06-07 07:08

/src/libdwarf/fuzz/fuzz_die_cu_print.c
Line
Count
Source (jump to first uncovered line)
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>
13
#include <stdint.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
#include <sys/stat.h>
18
#include <sys/types.h>
19
#include <unistd.h>
20
21
/*
22
 * Libdwarf library callers can only use these headers.
23
 */
24
#include "dwarf.h"
25
#include "libdwarf.h"
26
27
/*
28
 * A fuzzer that simulates a small part of the simplereader.c example.
29
 * This fuzzer targets dwarf_init_b.
30
 */
31
10.5k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
10.5k
  char filename[256];
33
10.5k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
34
35
10.5k
  FILE *fp = fopen(filename, "wb");
36
10.5k
  if (!fp) {
37
0
    return 0;
38
0
  }
39
10.5k
  fwrite(data, size, 1, fp);
40
10.5k
  fclose(fp);
41
42
10.5k
  Dwarf_Debug dbg = 0;
43
10.5k
  int fuzz_fd = 0;
44
10.5k
  int res = DW_DLV_ERROR;
45
10.5k
  Dwarf_Error error = 0;
46
10.5k
  Dwarf_Handler errhand = 0;
47
10.5k
  Dwarf_Ptr errarg = 0;
48
10.5k
  Dwarf_Error *errp = 0;
49
10.5k
  int i = 0;
50
10.5k
  Dwarf_Die die;
51
52
10.5k
  fuzz_fd = open(filename, O_RDONLY);
53
10.5k
  if (fuzz_fd != -1) {
54
10.5k
    res =
55
10.5k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
56
10.5k
    if (res == DW_DLV_OK) {
57
3.70k
      Dwarf_Bool is_info = 0;
58
3.70k
      Dwarf_Unsigned cu_header_length = 0;
59
3.70k
      Dwarf_Half version_stamp = 0;
60
3.70k
      Dwarf_Off abbrev_offset = 0;
61
3.70k
      Dwarf_Half address_size = 0;
62
3.70k
      Dwarf_Half length_size = 0;
63
3.70k
      Dwarf_Half extension_size = 0;
64
3.70k
      Dwarf_Sig8 type_signature;
65
3.70k
      Dwarf_Unsigned typeoffset = 0;
66
3.70k
      Dwarf_Unsigned next_cu_header_offset = 0;
67
3.70k
      Dwarf_Half header_cu_type = 0;
68
3.70k
      Dwarf_Die cu_die = 0;
69
3.70k
      static const Dwarf_Sig8 zerosignature;
70
71
3.70k
      res = dwarf_get_address_size(dbg, &address_size, errp);
72
73
3.70k
      const char *frame_section_name = 0;
74
3.70k
      res = dwarf_get_frame_section_name(dbg, &frame_section_name, errp);
75
76
3.70k
      type_signature = zerosignature;
77
3.70k
      res = dwarf_next_cu_header_d(
78
3.70k
          dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
79
3.70k
          &address_size, &length_size, &extension_size, &type_signature,
80
3.70k
          &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
81
3.70k
      if (res == DW_DLV_OK) {
82
1.20k
        res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
83
1.20k
        if (res == DW_DLV_OK) {
84
1.14k
          Dwarf_Die child = 0;
85
1.14k
          res = dwarf_child(cu_die, &child, errp);
86
1.14k
        }
87
88
2.50k
      } else {
89
2.50k
        dwarf_finish(dbg);
90
2.50k
        close(fuzz_fd);
91
2.50k
        unlink(filename);
92
2.50k
        return 0;
93
2.50k
      }
94
1.20k
      dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
95
1.20k
    }
96
10.5k
  }
97
8.07k
  dwarf_finish(dbg);
98
8.07k
  close(fuzz_fd);
99
8.07k
  unlink(filename);
100
8.07k
  return 0;
101
10.5k
}