32 lines
916 B
C++
32 lines
916 B
C++
// libFuzzer harness for the Mach-O object file reader.
|
|
// Build with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
|
// Run: ./fuzz_macho_reader [-max_total_time=60] [corpus_dir]
|
|
|
|
#include "voidlink/macho_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) {
|
|
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "fuzz_macho", 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::macho::parse_macho_object(path);
|
|
|
|
::close(fd);
|
|
return 0;
|
|
}
|