Coverage Report

Created: 2023-06-07 07:08

/src/libdwarf/fuzz/fuzz_crc_32.c
Line
Count
Source (jump to first uncovered line)
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>
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
/*
28
 * Fuzzer function targeting dwarf_crc32
29
 */
30
15.5k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
31
15.5k
  char filename[256];
32
15.5k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
33
34
15.5k
  FILE *fp = fopen(filename, "wb");
35
15.5k
  if (!fp) {
36
0
    return 0;
37
0
  }
38
15.5k
  fwrite(data, size, 1, fp);
39
15.5k
  fclose(fp);
40
41
15.5k
  int fuzz_fd = 0;
42
15.5k
  Dwarf_Ptr errarg = 0;
43
15.5k
  Dwarf_Handler errhand = 0;
44
15.5k
  Dwarf_Error error = 0;
45
15.5k
  Dwarf_Debug dbg = 0;
46
15.5k
  off_t size_left = 0;
47
15.5k
  off_t fsize = 0;
48
15.5k
  unsigned char *crcbuf = 0;
49
15.5k
  int res = 0;
50
51
15.5k
  fuzz_fd = open(filename, O_RDONLY);
52
15.5k
  fsize = size_left = lseek(fuzz_fd, 0L, SEEK_END);
53
15.5k
  if (fuzz_fd != -1) {
54
15.5k
    dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
55
    /*  By not checking the return code, on a failed in we
56
        cannot dealloc the error field, so
57
        there is a leak from 
58
        _dwarf_special_no_dbg_error_malloc()  */
59
15.5k
    res = dwarf_crc32(dbg, crcbuf, &error);
60
     
61
15.5k
    dwarf_finish(dbg);
62
15.5k
  }
63
15.5k
  close(fuzz_fd);
64
15.5k
  unlink(filename);
65
15.5k
  return 0;
66
15.5k
}