Coverage Report

Created: 2025-09-27 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_init_binary.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
#ifndef O_BINARY
27
21.4k
#define O_BINARY 0
28
#endif
29
30
/*
31
 * A fuzzer that simulates a small part of the simplereader.c example.
32
 * This fuzzer targets dwarf_init_b.
33
 */
34
21.4k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
21.4k
  char filename[256];
36
37
#ifdef DWREGRESSIONTEMP
38
  /*  Under msys2, the /tmp/ results in an open fail,
39
      so we discard the /tmp/ here */
40
  sprintf(filename, "junklibfuzzer.%d", getpid());
41
#else
42
21.4k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
43
21.4k
#endif
44
21.4k
  FILE *fp = fopen(filename, "wb");
45
21.4k
  if (!fp) {
46
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
47
0
        filename);
48
0
    return 0;
49
0
  }
50
21.4k
  fwrite(data, size, 1, fp);
51
21.4k
  fclose(fp);
52
53
21.4k
  int fuzz_fd = 0;
54
21.4k
  Dwarf_Ptr errarg = 0;
55
21.4k
  Dwarf_Handler errhand = 0;
56
21.4k
  Dwarf_Error *errp = NULL;
57
21.4k
  Dwarf_Debug dbg = 0;
58
59
21.4k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
60
21.4k
  if (fuzz_fd != -1) {
61
21.4k
    dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp);
62
21.4k
    dwarf_finish(dbg);
63
21.4k
    close(fuzz_fd);
64
21.4k
  }
65
66
21.4k
  unlink(filename);
67
21.4k
  return 0;
68
21.4k
}