158 lines
5.9 KiB
C++
158 lines
5.9 KiB
C++
// 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;
|
|
}
|