Coverage Report

Created: 2026-07-16 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libdwarf/fuzz/fuzz_crc_32.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
25.0k
#define O_BINARY 0
28
#endif
29
30
31
/*
32
 * Fuzzer function targeting dwarf_crc32
33
 */
34
25.0k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
25.0k
  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
25.0k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
41
25.0k
#endif
42
43
25.0k
  FILE *fp = fopen(filename, "wb");
44
25.0k
  if (!fp) {
45
0
    printf("FAIL libfuzzer cannot open temp as writeable %s\n",
46
0
        filename);
47
0
    return 0;
48
0
  }
49
25.0k
  fwrite(data, size, 1, fp);
50
25.0k
  fclose(fp);
51
52
25.0k
  int fuzz_fd = 0;
53
25.0k
  Dwarf_Ptr errarg = 0;
54
25.0k
  Dwarf_Handler errhand = 0;
55
25.0k
  Dwarf_Error error = 0;
56
25.0k
  Dwarf_Debug dbg = 0;
57
25.0k
  off_t size_left = 0;
58
25.0k
  off_t fsize = 0;
59
25.0k
  unsigned char *crcbuf = 0;
60
25.0k
  int res = 0;
61
62
25.0k
  fuzz_fd = open(filename, O_RDONLY|O_BINARY);
63
25.0k
  fsize = size_left = lseek(fuzz_fd, 0L, SEEK_END);
64
  /*  The read below will fail, so to avoid
65
      reading uninitialized data we ensure
66
      the data is initialized. */
67
68
25.0k
  if (fuzz_fd != -1) {
69
25.0k
    dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
70
    /*  By not checking the return code, on a failed init
71
        we cannot dealloc the error field, so
72
        there is a leak from
73
        _dwarf_special_no_dbg_error_malloc()  */
74
    /*  The library has no way to verify a non-null
75
        crcbuf points to a valid 4 byte block of memory.
76
        Passing in NULL results in returning DW_DLV_NO_ENTRY. */
77
25.0k
    res = dwarf_crc32(dbg, crcbuf, &error);
78
    /*  Ignoring res! */
79
80
25.0k
    dwarf_finish(dbg);
81
25.0k
  }
82
25.0k
  close(fuzz_fd);
83
25.0k
  unlink(filename);
84
25.0k
  return 0;
85
25.0k
}