Coverage Report

Created: 2025-07-18 06:06

/src/fuzz-dwfl-core.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
# Copyright 2021 Google Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
################################################################################
17
*/
18
#include <assert.h>
19
#include <config.h>
20
#include <stdlib.h>
21
#include <unistd.h>
22
#include ELFUTILS_HEADER(dwfl)
23
24
static const Dwfl_Callbacks core_callbacks = {
25
  .find_elf = dwfl_build_id_find_elf,
26
  .find_debuginfo = dwfl_standard_find_debuginfo,
27
};
28
29
17.2k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
30
17.2k
  char name[] = "/tmp/fuzz-dwfl-core.XXXXXX";
31
17.2k
  int fd = -1;
32
17.2k
  ssize_t n;
33
17.2k
  off_t offset;
34
17.2k
  Elf *core = NULL;
35
17.2k
  Dwfl *dwfl = NULL;
36
37
17.2k
  fd = mkstemp(name);
38
17.2k
  assert(fd >= 0);
39
40
17.2k
  n = write(fd, data, size);
41
17.2k
  assert(n == (ssize_t) size);
42
43
17.2k
  offset = lseek(fd, 0, SEEK_SET);
44
17.2k
  assert(offset == 0);
45
46
17.2k
  elf_version(EV_CURRENT);
47
17.2k
  core = elf_begin(fd, ELF_C_READ_MMAP, NULL);
48
17.2k
  if (core == NULL)
49
86
    goto cleanup;
50
17.1k
  dwfl = dwfl_begin(&core_callbacks);
51
17.1k
  assert(dwfl != NULL);
52
17.1k
  if (dwfl_core_file_report(dwfl, core, NULL) < 0)
53
1.18k
    goto cleanup;
54
15.9k
  if (dwfl_report_end(dwfl, NULL, NULL) != 0)
55
0
    goto cleanup;
56
15.9k
  if (dwfl_core_file_attach(dwfl, core) < 0)
57
15.9k
    goto cleanup;
58
59
17.2k
cleanup:
60
17.2k
  dwfl_end(dwfl);
61
17.2k
  elf_end(core);
62
17.2k
  close(fd);
63
17.2k
  unlink(name);
64
17.2k
  return 0;
65
15.9k
}