commit
bba5c8a39a
109 files changed
+14085
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user