initial commit

Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-05-22 02:40:32 +03:00
commit bba5c8a39a
109 files changed
+14085

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
# Synthesizer unit tests — verify ELF/PE/Mach-O output structure without
# executing the synthesized blobs. These link the synthesizer static libraries
# directly; they don't need the full loader runtime.
# ── ELF synth tests ──────────────────────────────────────────────────────────
add_executable(test_elf_synth elf_synth_test.cpp)
target_link_libraries(test_elf_synth PRIVATE voidlibc_loader_common)
target_include_directories(test_elf_synth PRIVATE ${CMAKE_SOURCE_DIR})
add_test(NAME synth_elf COMMAND test_elf_synth)
set_tests_properties(synth_elf PROPERTIES LABELS "synth;unit")
# ── PE synth tests ───────────────────────────────────────────────────────────
add_executable(test_pe_synth pe_synth_test.cpp)
target_link_libraries(test_pe_synth PRIVATE voidlibc_loader_windows)
target_include_directories(test_pe_synth PRIVATE ${CMAKE_SOURCE_DIR})
add_test(NAME synth_pe COMMAND test_pe_synth)
set_tests_properties(synth_pe PROPERTIES LABELS "synth;unit")
# ── Mach-O synth tests ───────────────────────────────────────────────────────
add_executable(test_macho_synth macho_synth_test.cpp)
target_link_libraries(test_macho_synth PRIVATE voidlibc_loader_macos)
target_include_directories(test_macho_synth PRIVATE ${CMAKE_SOURCE_DIR})
add_test(NAME synth_macho COMMAND test_macho_synth)
set_tests_properties(synth_macho PROPERTIES LABELS "synth;unit")
+173
View File
@@ -0,0 +1,173 @@
// Unit tests for synthesize_elf() — verifies the structural correctness of
// the ELF64 DSO bytes without executing them.
#include "loader/common/elf_synth.hpp"
#include "loader/common/elf64.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace voidlibc::loader;
// ── Helpers ─────────────────────────────────────────────────────────────────
template<typename T>
static T read_at(const std::vector<std::uint8_t>& b, std::size_t off) {
T val{};
if (off + sizeof(T) <= b.size()) std::memcpy(&val, b.data() + off, sizeof(T));
return val;
}
static int count_dyn_tag(const std::vector<std::uint8_t>& b, std::int64_t tag) {
const Ehdr hdr = read_at<Ehdr>(b, 0);
int count = 0;
for (int pi = 0; pi < hdr.e_phnum; ++pi) {
const Phdr ph = read_at<Phdr>(b, hdr.e_phoff + static_cast<std::size_t>(pi) * sizeof(Phdr));
if (ph.p_type != kPtDynamic) continue;
for (std::size_t off = ph.p_offset; off + sizeof(Dyn) <= ph.p_offset + ph.p_filesz; off += sizeof(Dyn)) {
const Dyn dyn = read_at<Dyn>(b, off);
if (dyn.d_tag == kDtNull) break;
if (dyn.d_tag == tag) ++count;
}
break;
}
return count;
}
#define REQUIRE(cond) do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL [%s]: %s (line %d)\n", \
test_name, #cond, __LINE__); \
++failures; \
} \
} while (0)
// ── Tests ────────────────────────────────────────────────────────────────────
static int test_basic_magic_and_type(const char* test_name) {
int failures = 0;
ElfSynthInput in{};
in.code = {0x90}; // nop
ExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
const auto out = synthesize_elf(in);
REQUIRE(out.ok);
REQUIRE(out.bytes.size() >= sizeof(Ehdr));
const Ehdr hdr = read_at<Ehdr>(out.bytes, 0);
REQUIRE(hdr.e_ident[0] == kElfMag0);
REQUIRE(hdr.e_ident[1] == kElfMag1);
REQUIRE(hdr.e_ident[2] == kElfMag2);
REQUIRE(hdr.e_ident[3] == kElfMag3);
REQUIRE(hdr.e_type == kEtDyn);
REQUIRE(hdr.e_machine == kEmX86_64);
return failures;
}
static int test_multi_needed_two_libs(const char* test_name) {
int failures = 0;
ElfSynthInput in{};
in.code = {0x90};
ExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
ImportedSymbol im1{}; im1.sym_id = 0; im1.name = "printf"; im1.library = "libc.so.6";
ImportedSymbol im2{}; im2.sym_id = 1; im2.name = "sqrt"; im2.library = "libm.so.6";
in.imports.push_back(im1);
in.imports.push_back(im2);
const auto out = synthesize_elf(in);
REQUIRE(out.ok);
REQUIRE(count_dyn_tag(out.bytes, kDtNeeded) == 2);
return failures;
}
static int test_no_needed_when_no_imports(const char* test_name) {
int failures = 0;
ElfSynthInput in{};
in.code = {0x90};
ExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
// No imports → no DT_NEEDED
const auto out = synthesize_elf(in);
REQUIRE(out.ok);
REQUIRE(count_dyn_tag(out.bytes, kDtNeeded) == 0);
return failures;
}
static int test_versioned_import_emits_verneed(const char* test_name) {
int failures = 0;
ElfSynthInput in{};
in.code = {0x90};
ExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
ImportedSymbol im{};
im.sym_id = 0;
im.name = "memcpy";
im.library = "libc.so.6";
im.version_name = "GLIBC_2.14";
im.version_hash = elf_hash("GLIBC_2.14");
im.version_flags = 0;
in.imports.push_back(im);
const auto out = synthesize_elf(in);
REQUIRE(out.ok);
REQUIRE(count_dyn_tag(out.bytes, kDtVerneed) == 1);
REQUIRE(count_dyn_tag(out.bytes, kDtVerneedNum) == 1);
REQUIRE(count_dyn_tag(out.bytes, kDtVersym) == 1);
return failures;
}
static int test_unversioned_import_no_verneed(const char* test_name) {
int failures = 0;
ElfSynthInput in{};
in.code = {0x90};
ExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
ImportedSymbol im{};
im.sym_id = 0;
im.name = "strlen";
im.library = "libc.so.6";
// version_name intentionally empty
in.imports.push_back(im);
const auto out = synthesize_elf(in);
REQUIRE(out.ok);
REQUIRE(count_dyn_tag(out.bytes, kDtVerneed) == 0);
REQUIRE(count_dyn_tag(out.bytes, kDtVersym) == 0);
return failures;
}
// ── main ─────────────────────────────────────────────────────────────────────
struct Test { const char* name; int (*fn)(const char*); };
static const Test kTests[] = {
{"elf_basic_magic_and_type", test_basic_magic_and_type},
{"elf_multi_needed_two_libs", test_multi_needed_two_libs},
{"elf_no_needed_when_no_imports", test_no_needed_when_no_imports},
{"elf_versioned_import_emits_verneed",test_versioned_import_emits_verneed},
{"elf_unversioned_import_no_verneed", test_unversioned_import_no_verneed},
};
int main() {
int total_failures = 0;
for (const auto& t : kTests) {
const int f = t.fn(t.name);
total_failures += f;
std::printf("%s: %s\n", f == 0 ? "PASS" : "FAIL", t.name);
}
if (total_failures > 0) {
std::fprintf(stderr, "%d assertion(s) failed\n", total_failures);
return 1;
}
std::printf("All %zu ELF synth tests passed\n",
sizeof(kTests) / sizeof(kTests[0]));
return 0;
}
+157
View File
@@ -0,0 +1,157 @@
// Unit tests for synthesize_macho() — verifies Mach-O64 dylib output structure
// without executing it. Parses mach_header_64 and load commands directly.
#include "loader/macos/macho_synth.hpp"
#include "format/reloc_ir.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace voidlibc::loader::macos;
using voidlibc::format::RelocEntry;
using voidlibc::format::RelocOp;
template<typename T>
static T read_at(const std::vector<std::uint8_t>& b, std::size_t off) {
T val{};
if (off + sizeof(T) <= b.size()) std::memcpy(&val, b.data() + off, sizeof(T));
return val;
}
// Mach-O constants (matching macho_synth.cpp internals)
static constexpr std::uint32_t kMhMagic64 = 0xFEEDFACFu;
static constexpr std::uint32_t kMhDylib = 6u;
static constexpr std::uint32_t kLcLoadDylib = 0x0Cu;
// mach_header_64 layout:
// [0] magic uint32 [4] cputype int32
// [8] cpusubtype int32 [12] filetype uint32
// [16] ncmds uint32 [20] sizeofcmds uint32
// [24] flags uint32 [28] reserved uint32
// Load commands start at byte 32.
static int count_load_commands(const std::vector<std::uint8_t>& b, std::uint32_t cmd_type) {
const std::uint32_t ncmds = read_at<std::uint32_t>(b, 16);
std::size_t lc_off = 32;
int count = 0;
for (std::uint32_t i = 0; i < ncmds && lc_off + 8 <= b.size(); ++i) {
const std::uint32_t cmd = read_at<std::uint32_t>(b, lc_off);
const std::uint32_t cmdsize = read_at<std::uint32_t>(b, lc_off + 4);
if (cmdsize == 0) break;
if (cmd == cmd_type) ++count;
lc_off += cmdsize;
}
return count;
}
#define REQUIRE(cond) do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL [%s]: %s (line %d)\n", \
test_name, #cond, __LINE__); \
++failures; \
} \
} while (0)
static int test_macho_magic_and_filetype(const char* test_name) {
int failures = 0;
MachoSynthInput in{};
in.code = {0x90}; // nop
MachoExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
const auto out = synthesize_macho(in);
REQUIRE(out.ok);
REQUIRE(out.bytes.size() >= 32);
REQUIRE(read_at<std::uint32_t>(out.bytes, 0) == kMhMagic64); // magic
REQUIRE(read_at<std::uint32_t>(out.bytes, 12) == kMhDylib); // filetype
return failures;
}
// Verify that N entries in needed_libs → N LC_LOAD_DYLIB commands.
static int test_macho_multi_dylib_via_needed_libs(const char* test_name) {
int failures = 0;
MachoExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
// Two explicit dylib requirements → two LC_LOAD_DYLIB
{
MachoSynthInput in{};
in.code = {0x90};
in.exports.push_back(e);
in.needed_libs = {"/usr/lib/libSystem.B.dylib", "/usr/lib/libm.dylib"};
const auto out = synthesize_macho(in);
REQUIRE(out.ok);
REQUIRE(count_load_commands(out.bytes, kLcLoadDylib) == 2);
}
// One explicit dylib → one LC_LOAD_DYLIB
{
MachoSynthInput in{};
in.code = {0x90};
in.exports.push_back(e);
in.needed_libs = {"/usr/lib/libSystem.B.dylib"};
const auto out = synthesize_macho(in);
REQUIRE(out.ok);
REQUIRE(count_load_commands(out.bytes, kLcLoadDylib) == 1);
}
return failures;
}
// Verify per-import library attribution via PLT32 relocs drives LC_LOAD_DYLIB
// selection (the reloc-driven path inside synthesize_macho).
static int test_macho_import_library_attribution(const char* test_name) {
int failures = 0;
MachoExportedSymbol e{}; e.name = "fn"; e.code_offset = 5; e.is_function = true;
// Two CALL instructions in the code blob, each referencing a different import.
// offset 0: e8 00 00 00 00 (CALL rel32 placeholder, import sym_id=10)
// offset 5: 90 (nop — exported fn entry)
// offset 6: e8 00 00 00 00 (CALL rel32 placeholder, import sym_id=20)
MachoSynthInput in{};
in.code = {0xe8,0x00,0x00,0x00,0x00, 0x90,
0xe8,0x00,0x00,0x00,0x00, 0x90};
in.exports.push_back(e);
MachoImportedSymbol im1{}; im1.sym_id = 10; im1.name = "strlen";
im1.library = "/usr/lib/libSystem.B.dylib";
MachoImportedSymbol im2{}; im2.sym_id = 20; im2.name = "sqrt";
im2.library = "/usr/lib/libm.dylib";
in.imports.push_back(im1);
in.imports.push_back(im2);
// PLT32 relocs so the synthesizer sees nstubs=2 and picks up im.library.
RelocEntry r1{}; r1.op = RelocOp::PLT32; r1.dst_offset = 1; r1.sym_id = 10; r1.addend = -4;
RelocEntry r2{}; r2.op = RelocOp::PLT32; r2.dst_offset = 7; r2.sym_id = 20; r2.addend = -4;
in.relocs.push_back(r1);
in.relocs.push_back(r2);
const auto out = synthesize_macho(in);
REQUIRE(out.ok);
// Two distinct libraries → two LC_LOAD_DYLIB
REQUIRE(count_load_commands(out.bytes, kLcLoadDylib) == 2);
return failures;
}
struct Test { const char* name; int (*fn)(const char*); };
static const Test kTests[] = {
{"macho_magic_and_filetype", test_macho_magic_and_filetype},
{"macho_multi_dylib_via_needed_libs", test_macho_multi_dylib_via_needed_libs},
{"macho_import_library_attribution", test_macho_import_library_attribution},
};
int main() {
int total_failures = 0;
for (const auto& t : kTests) {
const int f = t.fn(t.name);
total_failures += f;
std::printf("%s: %s\n", f == 0 ? "PASS" : "FAIL", t.name);
}
if (total_failures > 0) {
std::fprintf(stderr, "%d assertion(s) failed\n", total_failures);
return 1;
}
std::printf("All %zu Mach-O synth tests passed\n",
sizeof(kTests) / sizeof(kTests[0]));
return 0;
}
+129
View File
@@ -0,0 +1,129 @@
// Unit tests for synthesize_pe() — verifies PE64 DLL output structure
// without executing it. Parses the raw bytes using known PE offsets.
#include "loader/windows/pe_synth.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace voidlibc::loader::windows;
template<typename T>
static T read_at(const std::vector<std::uint8_t>& b, std::size_t off) {
T val{};
if (off + sizeof(T) <= b.size()) std::memcpy(&val, b.data() + off, sizeof(T));
return val;
}
// From pe_synth.cpp layout (stable, documented in the file header):
// 0x000: DOS stub
// 0x040: "PE\0\0" PE signature
// 0x044: IMAGE_FILE_HEADER (20 bytes)
// 0x058: IMAGE_OPTIONAL_HEADER64
// +112 (= 0xC8): DataDirectory[0] export RVA/Size
// +120 (= 0xD0): DataDirectory[1] import RVA
// +124 (= 0xD4): DataDirectory[1] import Size = (ndlls+1)*20
static constexpr std::uint32_t kPeSigOff = 0x040;
static constexpr std::uint32_t kFileHdrOff = 0x044;
static constexpr std::uint32_t kOptHdrOff = 0x058;
static constexpr std::uint32_t kImportSizeOff= kOptHdrOff + 112 + 12; // 0xD4
static constexpr std::uint32_t kCharacteristicsOff = kFileHdrOff + 18; // 0x056
static constexpr std::uint16_t kCharDll = 0x2000;
static constexpr std::uint16_t kMachineAmd64 = 0x8664;
#define REQUIRE(cond) do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL [%s]: %s (line %d)\n", \
test_name, #cond, __LINE__); \
++failures; \
} \
} while (0)
static int test_pe_magic_and_dll_flag(const char* test_name) {
int failures = 0;
PeSynthInput in{};
in.code = {0x90}; // nop
in.dll_name = "test.dll";
WinExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
const auto out = synthesize_pe(in);
REQUIRE(out.ok);
REQUIRE(out.bytes.size() > kImportSizeOff + 4);
// MZ magic
REQUIRE(out.bytes[0] == 'M' && out.bytes[1] == 'Z');
// PE signature
REQUIRE(out.bytes[kPeSigOff + 0] == 'P');
REQUIRE(out.bytes[kPeSigOff + 1] == 'E');
REQUIRE(out.bytes[kPeSigOff + 2] == '\0');
REQUIRE(out.bytes[kPeSigOff + 3] == '\0');
// Machine = AMD64
REQUIRE(read_at<std::uint16_t>(out.bytes, kFileHdrOff) == kMachineAmd64);
// IMAGE_FILE_DLL set
const auto chars = read_at<std::uint16_t>(out.bytes, kCharacteristicsOff);
REQUIRE((chars & kCharDll) != 0);
return failures;
}
static int test_pe_multi_dll_import_dir_size(const char* test_name) {
int failures = 0;
PeSynthInput in{};
in.code = {0x90};
in.dll_name = "test.dll";
WinExportedSymbol e{}; e.name = "fn"; e.code_offset = 0; e.is_function = true;
in.exports.push_back(e);
WinImportedSymbol im1{}; im1.sym_id = 0; im1.name = "printf"; im1.library = "ucrtbase.dll";
WinImportedSymbol im2{}; im2.sym_id = 1; im2.name = "ExitProcess"; im2.library = "KERNEL32.DLL";
in.imports.push_back(im1);
in.imports.push_back(im2);
const auto out = synthesize_pe(in);
REQUIRE(out.ok);
// import dir size = (ndlls + 1) * 20; ndlls=2 → 60
const auto imp_sz = read_at<std::uint32_t>(out.bytes, kImportSizeOff);
REQUIRE(imp_sz == 60u);
// Sanity: single-DLL import should give size 40
PeSynthInput in1{};
in1.code = {0x90};
in1.dll_name = "test.dll";
in1.exports.push_back(e);
WinImportedSymbol im_single{}; im_single.sym_id = 0; im_single.name = "puts"; im_single.library = "ucrtbase.dll";
in1.imports.push_back(im_single);
const auto out1 = synthesize_pe(in1);
REQUIRE(out1.ok);
REQUIRE(read_at<std::uint32_t>(out1.bytes, kImportSizeOff) == 40u);
return failures;
}
struct Test { const char* name; int (*fn)(const char*); };
static const Test kTests[] = {
{"pe_magic_and_dll_flag", test_pe_magic_and_dll_flag},
{"pe_multi_dll_import_dir_size", test_pe_multi_dll_import_dir_size},
};
int main() {
int total_failures = 0;
for (const auto& t : kTests) {
const int f = t.fn(t.name);
total_failures += f;
std::printf("%s: %s\n", f == 0 ? "PASS" : "FAIL", t.name);
}
if (total_failures > 0) {
std::fprintf(stderr, "%d assertion(s) failed\n", total_failures);
return 1;
}
std::printf("All %zu PE synth tests passed\n",
sizeof(kTests) / sizeof(kTests[0]));
return 0;
}