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

91 lines
2.6 KiB
C++

// E2E: build a one-section .vlc containing the x86_64 bytes for
//
// int add(int a, int b) { return a + b; }
//
// then dlopen it (triggering the libvoid interposer), dlsym("add"), call, assert.
//
// Machine code (System V AMD64 ABI: arg1=EDI, arg2=ESI, return=EAX):
// mov eax, edi ; 89 f8
// add eax, esi ; 01 f0
// ret ; c3
#include "builder.hpp"
#include "format/vlc_format.hpp"
#include <cstdio>
#include <cstdlib>
#include <dlfcn.h>
#include <filesystem>
namespace fs = std::filesystem;
using namespace voidlibc::format;
using namespace voidlibc::voidlink;
namespace {
#define REQUIRE(cond) do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL: %s (line %d)\n", #cond, \
__LINE__); \
const char* le = ::dlerror(); \
if (le) std::fprintf(stderr, " dlerror: %s\n", le); \
std::exit(1); \
} \
} while (0)
int run() {
const fs::path tmp = fs::temp_directory_path() / "voidlibc_add.vls";
fs::remove(tmp);
// 1. Build the .vlc.
{
ContainerBuilder b;
SymbolSpec add_sym{};
add_sym.name = "add";
add_sym.kind = SymbolKind::Function;
add_sym.value = 0;
add_sym.size = 5;
add_sym.section_index = 0;
add_sym.exported = true;
b.add_symbol(std::move(add_sym));
SectionSpec sec{};
sec.target_os = TargetOS::Linux;
sec.code = {0x89, 0xf8, 0x01, 0xf0, 0xc3};
sec.entry_offset = 0;
b.add_section(std::move(sec));
const auto w = b.write(tmp);
REQUIRE(w.ok);
}
// 2. Load via dlopen — the libvoid interposer detects VLC magic and
// synthesizes an ELF DSO transparently.
void* h = ::dlopen(tmp.c_str(), RTLD_NOW);
REQUIRE(h != nullptr);
// 3. Resolve add.
using add_fn_t = int (*)(int, int);
auto* add = reinterpret_cast<add_fn_t>(::dlsym(h, "add"));
REQUIRE(add != nullptr);
// 4. Call it.
const int result = add(7, 35);
if (result != 42) {
std::fprintf(stderr, "FAIL: add(7, 35) = %d (expected 42)\n", result);
std::exit(1);
}
// 5. Close.
REQUIRE(::dlclose(h) == 0);
fs::remove(tmp);
std::printf("PASS: e2e_load_add — add(7, 35) = %d\n", result);
return 0;
}
} // namespace
int main() { return run(); }