Coverage Report

Created: 2025-11-24 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_die_cu_info1.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/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
#ifndef O_BINARY
28
22.9k
#define O_BINARY 0
29
#endif
30
31
/*
32
 * Fuzzer function targeting dwarf_next_cu_header_d and dwarf_siblingof_b
33
 */
34
22.9k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
22.9k
  char filename[256];
36
#ifdef DWREGRESSIONTEMP
37
  /* Under msys2, the /tmp/ results in an open fail */
38
  sprintf(filename, "junklibfuzzer.%d", getpid());
39
#else
40
22.9k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
41
22.9k
#endif
42
22.9k
  FILE *fp = fopen(filename, "wb");
43
22.9k
  if (!fp) {
44
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
45
0
        filename);
46
0
    return 0;
47
0
  }
48
22.9k
  fwrite(data, size, 1, fp);
49
22.9k
  fclose(fp);
50
51
22.9k
  Dwarf_Debug dbg = 0;
52
22.9k
  int fuzz_fd = 0;
53
22.9k
  int res = DW_DLV_ERROR;
54
22.9k
  Dwarf_Error error = 0;
55
22.9k
  Dwarf_Handler errhand = 0;
56
22.9k
  Dwarf_Ptr errarg = 0;
57
22.9k
  Dwarf_Error *errp = 0;
58
22.9k
  int i = 0;
59
22.9k
  Dwarf_Die die;
60
61
22.9k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
62
22.9k
  if (fuzz_fd != -1) {
63
22.9k
    res =
64
22.9k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
65
22.9k
    if (res == DW_DLV_OK) {
66
8.72k
      Dwarf_Bool is_info = 1;
67
8.72k
      Dwarf_Unsigned cu_header_length = 0;
68
8.72k
      Dwarf_Half version_stamp = 0;
69
8.72k
      Dwarf_Off abbrev_offset = 0;
70
8.72k
      Dwarf_Half address_size = 0;
71
8.72k
      Dwarf_Half length_size = 0;
72
8.72k
      Dwarf_Half extension_size = 0;
73
8.72k
      Dwarf_Sig8 type_signature;
74
8.72k
      Dwarf_Unsigned typeoffset = 0;
75
8.72k
      Dwarf_Unsigned next_cu_header_offset = 0;
76
8.72k
      Dwarf_Half header_cu_type = 0;
77
8.72k
      Dwarf_Die cu_die = 0;
78
8.72k
      static const Dwarf_Sig8 zerosignature;
79
80
8.72k
      type_signature = zerosignature;
81
8.72k
      res = dwarf_next_cu_header_d(
82
8.72k
          dbg, is_info, &cu_header_length, &version_stamp, &abbrev_offset,
83
8.72k
          &address_size, &length_size, &extension_size, &type_signature,
84
8.72k
          &typeoffset, &next_cu_header_offset, &header_cu_type, errp);
85
8.72k
      if (res == DW_DLV_OK) {
86
87
962
        res = dwarf_siblingof_b(dbg, NULL, is_info, &cu_die, errp);
88
962
        dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
89
962
      }
90
8.72k
    }
91
22.9k
  }
92
22.9k
  dwarf_finish(dbg);
93
22.9k
  close(fuzz_fd);
94
22.9k
  unlink(filename);
95
22.9k
  return 0;
96
22.9k
}