Coverage Report

Created: 2025-07-11 06:46

/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
16.8k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
30
16.8k
  char name[] = "/tmp/fuzz-dwfl-core.XXXXXX";
31
16.8k
  int fd = -1;
32
16.8k
  ssize_t n;
33
16.8k
  off_t offset;
34
16.8k
  Elf *core = NULL;
35
16.8k
  Dwfl *dwfl = NULL;
36
37
16.8k
  fd = mkstemp(name);
38
16.8k
  assert(fd >= 0);
39
40
16.8k
  n = write(fd, data, size);
41
16.8k
  assert(n == (ssize_t) size);
42
43
16.8k
  offset = lseek(fd, 0, SEEK_SET);
44
16.8k
  assert(offset == 0);
45
46
16.8k
  elf_version(EV_CURRENT);
47
16.8k
  core = elf_begin(fd, ELF_C_READ_MMAP, NULL);
48
16.8k
  if (core == NULL)
49
92
    goto cleanup;
50
16.8k
  dwfl = dwfl_begin(&core_callbacks);
51
16.8k
  assert(dwfl != NULL);
52
16.8k
  if (dwfl_core_file_report(dwfl, core, NULL) < 0)
53
1.09k
    goto cleanup;
54
15.7k
  if (dwfl_report_end(dwfl, NULL, NULL) != 0)
55
0
    goto cleanup;
56
15.7k
  if (dwfl_core_file_attach(dwfl, core) < 0)
57
15.6k
    goto cleanup;
58
59
16.8k
cleanup:
60
16.8k
  dwfl_end(dwfl);
61
16.8k
  elf_end(core);
62
16.8k
  close(fd);
63
16.8k
  unlink(name);
64
16.8k
  return 0;
65
15.7k
}