Files
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

130 lines
4.6 KiB
C++

// 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;
}