Coverage Report

Created: 2025-10-10 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_xuindex.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/stat.h>
20
#include <sys/types.h>
21
#include <unistd.h>
22
23
#ifndef O_BINARY
24
15.2k
#define O_BINARY 0
25
#endif
26
27
15.2k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28
15.2k
  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
15.2k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
15.2k
#endif
37
15.2k
  FILE *fp = fopen(filename, "wb");
38
15.2k
  if (!fp) {
39
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
40
0
        filename);
41
0
    return 0;
42
0
  }
43
15.2k
  fwrite(data, size, 1, fp);
44
15.2k
  fclose(fp);
45
46
15.2k
  Dwarf_Debug dbg = 0;
47
15.2k
  int fuzz_fd = 0;
48
15.2k
  int res = DW_DLV_ERROR;
49
15.2k
  Dwarf_Error error = 0;
50
15.2k
  Dwarf_Handler errhand = 0;
51
15.2k
  Dwarf_Ptr errarg = 0;
52
15.2k
  Dwarf_Error *errp = 0;
53
15.2k
  int i = 0;
54
15.2k
  Dwarf_Die die;
55
56
15.2k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
57
15.2k
  if (fuzz_fd != -1) {
58
15.2k
    res =
59
15.2k
        dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
60
15.2k
    if (res == DW_DLV_OK) {
61
461
      Dwarf_Xu_Index_Header xuhdr = 0;
62
461
      Dwarf_Unsigned version_number = 0;
63
461
      Dwarf_Unsigned offsets_count = 0;
64
461
      Dwarf_Unsigned units_count = 0;
65
461
      Dwarf_Unsigned hash_slots_count = 0;
66
461
      const char *section_name = 0;
67
461
      res = dwarf_get_xu_index_header(dbg, "cu", &xuhdr, &version_number,
68
461
                                      &offsets_count, &units_count,
69
461
                                      &hash_slots_count, &section_name, errp);
70
461
    }
71
15.2k
  }
72
15.2k
  dwarf_finish(dbg);
73
15.2k
  close(fuzz_fd);
74
15.2k
  unlink(filename);
75
15.2k
  return 0;
76
15.2k
}