Coverage Report

Created: 2026-05-24 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_showsectgrp.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
#define O_BINARY 0
25
#endif
26
27
1.90k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
28
1.90k
  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
1.90k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
36
1.90k
#endif
37
1.90k
  FILE *fp = fopen(filename, "wb");
38
1.90k
  if (!fp) {
39
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
40
0
        filename);
41
0
    return 0;
42
0
  }
43
1.90k
  fwrite(data, size, 1, fp);
44
1.90k
  fclose(fp);
45
46
1.90k
  Dwarf_Error *errp = 0;
47
1.90k
  Dwarf_Debug dbg = 0;
48
1.90k
  int res = 0;
49
1.90k
  int chosengroup = DW_GROUPNUMBER_ANY;
50
1.90k
  Dwarf_Error error = 0;
51
1.90k
  Dwarf_Unsigned section_count = 0;
52
1.90k
  Dwarf_Unsigned group_count = 0;
53
1.90k
  Dwarf_Unsigned selected_group = 0;
54
1.90k
  Dwarf_Unsigned map_entry_count = 0;
55
1.90k
  Dwarf_Unsigned *group_numbers_array = 0;
56
1.90k
  Dwarf_Unsigned *sec_numbers_array = 0;
57
1.90k
  const char **sec_names_array = 0;
58
59
1.90k
  int run = dwarf_init_path(filename, 0, 0, chosengroup, 0, 0, &dbg, errp);
60
1.90k
  if (run != -1) {
61
1.56k
    if (run == DW_DLV_ERROR) {
62
1.45k
      dwarf_finish(dbg);
63
1.45k
      unlink(filename);
64
1.45k
      return 0;
65
1.45k
    }
66
106
    if (run == DW_DLV_NO_ENTRY) {
67
0
      dwarf_finish(dbg);
68
0
      unlink(filename);
69
0
      return 0;
70
0
    }
71
106
    res = dwarf_sec_group_sizes(dbg, &section_count, &group_count,
72
106
                                &selected_group, &map_entry_count, errp);
73
106
    if (res != DW_DLV_OK) {
74
0
      dwarf_dealloc_error(dbg, error);
75
0
      error = 0;
76
0
      dwarf_finish(dbg);
77
0
      unlink(filename);
78
0
      close(run);
79
0
      return 0;
80
0
    }
81
106
    group_numbers_array =
82
106
        (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned));
83
106
    sec_numbers_array =
84
106
        (Dwarf_Unsigned *)calloc(map_entry_count, sizeof(Dwarf_Unsigned));
85
106
    sec_names_array =
86
106
        (const char **)calloc(map_entry_count, sizeof(const char *));
87
106
    res = dwarf_sec_group_map(dbg, map_entry_count, group_numbers_array,
88
106
                              sec_numbers_array, sec_names_array, &error);
89
106
    free(sec_names_array);
90
106
    free(sec_numbers_array);
91
106
    free(group_numbers_array);
92
106
    dwarf_dealloc_error(dbg, error);
93
106
  }
94
451
  dwarf_finish(dbg);
95
451
  unlink(filename);
96
451
  close(run);
97
451
  return 0;
98
1.90k
}