33 lines
953 B
C++
33 lines
953 B
C++
// libFuzzer harness for the ELF object file reader.
|
|
// Build with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
|
// Run: ./fuzz_elf_reader [-max_total_time=60] [corpus_dir]
|
|
|
|
#include "voidlink/elf_reader.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
|
|
// Use memfd so no real file I/O hits disk.
|
|
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "fuzz_elf", 0));
|
|
if (fd < 0) return 0;
|
|
|
|
const std::uint8_t* p = data;
|
|
std::size_t rem = size;
|
|
while (rem > 0) {
|
|
const ssize_t n = ::write(fd, p, rem);
|
|
if (n <= 0) break;
|
|
p += static_cast<std::size_t>(n);
|
|
rem -= static_cast<std::size_t>(n);
|
|
}
|
|
|
|
char path[64];
|
|
std::snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
|
(void)voidlibc::voidlink::elf::parse_elf64_object(path);
|
|
|
|
::close(fd);
|
|
return 0;
|
|
}
|