commit
bba5c8a39a
109 files changed
+14085
No files matched your search
@@ -0,0 +1,212 @@
|
||||
# =============================================================================
|
||||
# VLC-producing library targets.
|
||||
# Each is a SHARED library that links 'void'; the auto-VLC hook (registered
|
||||
# at the bottom with voidlibc_enable_auto_vlc()) sees these, cross-compiles
|
||||
# their sources with zig cc, and produces fat .vls / .vle artifacts via
|
||||
# `voidlink fuse`. EXCLUDE_FROM_ALL keeps CMake from building the native .so.
|
||||
# =============================================================================
|
||||
|
||||
# --- add_real: add(int, int) — used by multiple tests ---
|
||||
add_library(add_real SHARED add_real.c)
|
||||
target_link_libraries(add_real PRIVATE void)
|
||||
set_target_properties(add_real PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# --- sum_squares: internal PC32/PLT32 reloc ---
|
||||
add_library(sum_squares SHARED sum_squares.c)
|
||||
target_link_libraries(sum_squares PRIVATE void)
|
||||
set_target_properties(sum_squares PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# --- hello_printf: external call to printf via PLT stub ---
|
||||
add_library(hello_printf SHARED hello_printf.c)
|
||||
target_link_libraries(hello_printf PRIVATE void)
|
||||
set_target_properties(hello_printf PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# --- static_counter: BSS + .data globals ---
|
||||
add_library(static_counter SHARED static_counter.c)
|
||||
target_link_libraries(static_counter PRIVATE void)
|
||||
set_target_properties(static_counter PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# --- extern_read: extern int resolved via GLOB_DAT ---
|
||||
# Windows PE synthesis can't handle GLOB_DAT for external globals; Linux only.
|
||||
add_library(extern_read SHARED extern_read.c)
|
||||
target_link_libraries(extern_read PRIVATE void)
|
||||
set_target_properties(extern_read PROPERTIES
|
||||
EXCLUDE_FROM_ALL TRUE
|
||||
VOIDLIBC_VLC_PLATFORMS "linux"
|
||||
)
|
||||
|
||||
# --- math_combined: multi-object merge (cross-object PLT32 → PC32) ---
|
||||
add_library(math_combined SHARED math_a.c math_b.c)
|
||||
target_link_libraries(math_combined PRIVATE void)
|
||||
set_target_properties(math_combined PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
|
||||
# --- hello_exec: executable VLC (.vle) with entry point ---
|
||||
add_library(hello_exec SHARED hello_exec.c)
|
||||
target_link_libraries(hello_exec PRIVATE void)
|
||||
set_target_properties(hello_exec PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
voidlibc_vlc_executable(hello_exec main)
|
||||
|
||||
# --- host_libc_pthread: pthread_create/join via host libpthread/libc ---
|
||||
add_library(host_libc_pthread SHARED host_libc_pthread.c)
|
||||
target_link_libraries(host_libc_pthread PRIVATE void)
|
||||
set_target_properties(host_libc_pthread PROPERTIES
|
||||
EXCLUDE_FROM_ALL TRUE
|
||||
VOIDLIBC_VLC_PLATFORMS "linux"
|
||||
)
|
||||
|
||||
# --- host_libc_multi: strlen (libc.so.6) + sqrt (libm.so.6) ---
|
||||
add_library(host_libc_multi SHARED host_libc_multi.c)
|
||||
target_link_libraries(host_libc_multi PRIVATE void)
|
||||
set_target_properties(host_libc_multi PROPERTIES
|
||||
EXCLUDE_FROM_ALL TRUE
|
||||
VOIDLIBC_VLC_PLATFORMS "linux"
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test executables — load .vls/.vle artifacts via dlopen (interposed).
|
||||
# All VLC paths use the deterministic naming: ${bindir}/<target-name>.vls|vle
|
||||
# =============================================================================
|
||||
|
||||
# --- e2e_load_add: hand-crafted VLC built in-process via builder API ---
|
||||
# (Not auto-hook driven; builds the VLC itself at runtime.)
|
||||
add_executable(e2e_load_add load_add.cpp)
|
||||
target_include_directories(e2e_load_add PRIVATE
|
||||
${CMAKE_SOURCE_DIR}
|
||||
${CMAKE_SOURCE_DIR}/voidlink
|
||||
)
|
||||
target_sources(e2e_load_add PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/voidlink/builder.cpp
|
||||
${CMAKE_SOURCE_DIR}/voidlink/writer.cpp
|
||||
)
|
||||
target_link_libraries(e2e_load_add PRIVATE void)
|
||||
add_test(NAME e2e_load_add COMMAND e2e_load_add)
|
||||
set_tests_properties(e2e_load_add PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_real_add ---
|
||||
add_executable(e2e_load_real_add load_real_add.cpp)
|
||||
target_link_libraries(e2e_load_real_add PRIVATE void)
|
||||
target_compile_definitions(e2e_load_real_add PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/add_real.vls"
|
||||
)
|
||||
add_test(NAME e2e_load_real_add COMMAND e2e_load_real_add)
|
||||
set_tests_properties(e2e_load_real_add PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_sum_squares ---
|
||||
add_executable(e2e_load_sum_squares load_sum_squares.cpp)
|
||||
target_link_libraries(e2e_load_sum_squares PRIVATE void)
|
||||
target_compile_definitions(e2e_load_sum_squares PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/sum_squares.vls"
|
||||
)
|
||||
add_test(NAME e2e_load_sum_squares COMMAND e2e_load_sum_squares)
|
||||
set_tests_properties(e2e_load_sum_squares PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_hello_printf ---
|
||||
add_executable(e2e_load_hello_printf load_hello_printf.cpp)
|
||||
target_link_libraries(e2e_load_hello_printf PRIVATE void)
|
||||
target_compile_definitions(e2e_load_hello_printf PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/hello_printf.vls"
|
||||
)
|
||||
add_test(NAME e2e_load_hello_printf COMMAND e2e_load_hello_printf)
|
||||
set_tests_properties(e2e_load_hello_printf PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_static_counter ---
|
||||
add_executable(e2e_load_static_counter load_static_counter.cpp)
|
||||
target_link_libraries(e2e_load_static_counter PRIVATE void)
|
||||
target_compile_definitions(e2e_load_static_counter PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/static_counter.vls"
|
||||
)
|
||||
add_test(NAME e2e_load_static_counter COMMAND e2e_load_static_counter)
|
||||
set_tests_properties(e2e_load_static_counter PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_extern_read ---
|
||||
add_executable(e2e_load_extern_read load_extern_read.cpp)
|
||||
target_link_libraries(e2e_load_extern_read PRIVATE void)
|
||||
target_compile_definitions(e2e_load_extern_read PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/extern_read.vls"
|
||||
)
|
||||
# The dlopen'd DSO resolves `the_answer` from this executable's symbol table.
|
||||
target_link_options(e2e_load_extern_read PRIVATE -Wl,--export-dynamic)
|
||||
add_test(NAME e2e_load_extern_read COMMAND e2e_load_extern_read)
|
||||
set_tests_properties(e2e_load_extern_read PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_load_multi_obj ---
|
||||
add_executable(e2e_load_multi_obj load_multi_obj.cpp)
|
||||
target_link_libraries(e2e_load_multi_obj PRIVATE void)
|
||||
target_compile_definitions(e2e_load_multi_obj PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/math_combined.vls"
|
||||
)
|
||||
add_test(NAME e2e_load_multi_obj COMMAND e2e_load_multi_obj)
|
||||
set_tests_properties(e2e_load_multi_obj PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_exec_mode: voidloader runs hello_exec.vle ---
|
||||
add_test(NAME e2e_exec_mode
|
||||
COMMAND $<TARGET_FILE:voidloader>
|
||||
${CMAKE_CURRENT_BINARY_DIR}/hello_exec.vle
|
||||
hello_arg
|
||||
)
|
||||
set_tests_properties(e2e_exec_mode PROPERTIES LABELS "e2e")
|
||||
|
||||
# --- e2e_host_libc_pthread ---
|
||||
add_executable(e2e_load_host_libc_pthread load_host_libc_pthread.cpp)
|
||||
target_link_libraries(e2e_load_host_libc_pthread PRIVATE void)
|
||||
target_compile_definitions(e2e_load_host_libc_pthread PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/host_libc_pthread.vls"
|
||||
)
|
||||
add_test(NAME e2e_host_libc_pthread COMMAND e2e_load_host_libc_pthread)
|
||||
set_tests_properties(e2e_host_libc_pthread PROPERTIES LABELS "e2e;host-libc")
|
||||
|
||||
# --- e2e_host_libc_multi ---
|
||||
add_executable(e2e_load_host_libc_multi load_host_libc_multi.cpp)
|
||||
target_link_libraries(e2e_load_host_libc_multi PRIVATE void)
|
||||
target_compile_definitions(e2e_load_host_libc_multi PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/host_libc_multi.vls"
|
||||
)
|
||||
add_test(NAME e2e_host_libc_multi COMMAND e2e_load_host_libc_multi)
|
||||
set_tests_properties(e2e_host_libc_multi PROPERTIES LABELS "e2e;host-libc")
|
||||
|
||||
# =============================================================================
|
||||
# fuse smoke test — verifies the fuse-produced fat VLC loads correctly.
|
||||
# The auto-hook already uses `voidlink fuse` to build add_real.vls with all 3
|
||||
# platform sections, so this test exercises that output directly.
|
||||
# =============================================================================
|
||||
add_executable(e2e_load_fuse load_real_add.cpp)
|
||||
target_link_libraries(e2e_load_fuse PRIVATE void)
|
||||
target_compile_definitions(e2e_load_fuse PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/add_real.vls"
|
||||
)
|
||||
add_test(NAME e2e_fuse COMMAND e2e_load_fuse)
|
||||
set_tests_properties(e2e_fuse PROPERTIES LABELS "e2e;fuse")
|
||||
|
||||
# =============================================================================
|
||||
# Fat VLC test — same add_real.vls artifact, labeled for CI filtering.
|
||||
# Confirms the Linux section of the multi-OS container loads and executes.
|
||||
# =============================================================================
|
||||
add_executable(e2e_load_fat_vlc load_real_add.cpp)
|
||||
target_link_libraries(e2e_load_fat_vlc PRIVATE void)
|
||||
target_compile_definitions(e2e_load_fat_vlc PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/add_real.vls"
|
||||
)
|
||||
add_test(NAME e2e_fat_vlc COMMAND e2e_load_fat_vlc)
|
||||
set_tests_properties(e2e_fat_vlc PROPERTIES LABELS "e2e;fat-vlc;cross-platform")
|
||||
|
||||
# =============================================================================
|
||||
# blob validation test — checks ELF/PE/Mach-O magic bytes in a fat .vls
|
||||
# without executing cross-platform code; runs on any Linux host.
|
||||
# =============================================================================
|
||||
add_executable(e2e_blob_validate validate_blobs.cpp)
|
||||
target_link_libraries(e2e_blob_validate PRIVATE voidlibc_format)
|
||||
target_include_directories(e2e_blob_validate PRIVATE ${CMAKE_SOURCE_DIR})
|
||||
target_compile_definitions(e2e_blob_validate PRIVATE
|
||||
VOIDLIBC_TEST_VLC_PATH="${CMAKE_CURRENT_BINARY_DIR}/add_real.vls"
|
||||
)
|
||||
add_test(NAME e2e_blob_validate COMMAND e2e_blob_validate)
|
||||
set_tests_properties(e2e_blob_validate PROPERTIES
|
||||
LABELS "e2e;cross-platform"
|
||||
DEPENDS add_real_vlc
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Register the auto-VLC DEFER hook for this directory.
|
||||
# All SHARED library targets above that link 'void' will get .vls/.vle built.
|
||||
# =============================================================================
|
||||
voidlibc_enable_auto_vlc()
|
||||
@@ -0,0 +1 @@
|
||||
int add(int a, int b) { return a + b; }
|
||||
@@ -0,0 +1,15 @@
|
||||
// Accesses an extern global variable via R_X86_64_REX_GOTPCRELX.
|
||||
// Tests that the GOT data slot + R_X86_64_GLOB_DAT flow works:
|
||||
// the synthesized ELF declares the symbol as SHN_UNDEF in .dynsym,
|
||||
// emits a GLOB_DAT reloc in .rela.dyn, and RTLD resolves it from
|
||||
// the caller's global symbol table.
|
||||
|
||||
extern int the_answer;
|
||||
|
||||
int get_the_answer(void) {
|
||||
return the_answer;
|
||||
}
|
||||
|
||||
int double_the_answer(void) {
|
||||
return the_answer * 2;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Exec-mode test: a complete program with main() and printf.
|
||||
// Compiled to a .vlc and run via voidloader; tests that:
|
||||
// - main() is exported and callable
|
||||
// - printf works via PLT → host libc
|
||||
// - argc/argv are forwarded correctly (argv[0] = .vlc path)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// argv[0] is the .vlc path; expect at least one extra arg.
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "FAIL: expected at least one argument\n");
|
||||
return 1;
|
||||
}
|
||||
// Check argv[1] is what we passed from the test.
|
||||
if (strcmp(argv[1], "hello_arg") != 0) {
|
||||
fprintf(stderr, "FAIL: argv[1] = '%s', expected 'hello_arg'\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
printf("PASS: exec mode — argc=%d argv[0]=%s argv[1]=%s\n",
|
||||
argc, argv[0], argv[1]);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int hello(void) {
|
||||
printf("hello from vlc: %d\n", 42);
|
||||
return 42;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
hello();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
// Combines a libc.so.6 symbol (strlen) and a libm.so.6 symbol (sqrt).
|
||||
// Returns 0 on success (verifies both imported symbols resolve correctly).
|
||||
int multi_libc_test(void) {
|
||||
const char* msg = "hello";
|
||||
size_t n = strlen(msg); // libc.so.6
|
||||
double s = sqrt((double)n); // libm.so.6
|
||||
// sqrt(5) ≈ 2.236; check it's in a reasonable range.
|
||||
return (s > 2.0 && s < 3.0) ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static void* worker(void* arg) {
|
||||
return (void*)((intptr_t)arg * 2);
|
||||
}
|
||||
|
||||
// Returns (input * 2) computed inside a spawned pthread.
|
||||
int pthread_double(int x) {
|
||||
pthread_t tid;
|
||||
if (pthread_create(&tid, NULL, worker, (void*)(intptr_t)x) != 0)
|
||||
return -1;
|
||||
void* ret = NULL;
|
||||
if (pthread_join(tid, &ret) != 0)
|
||||
return -2;
|
||||
return (int)(intptr_t)ret;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// 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(); }
|
||||
@@ -0,0 +1,54 @@
|
||||
// E2E: extern global variable access via GOTPCREL / GLOB_DAT.
|
||||
// extern_read.c references `the_answer` (an extern int defined here in the
|
||||
// test executable). The synthesized ELF must emit a .rela.dyn GLOB_DAT entry
|
||||
// so that RTLD resolves `the_answer` from the executable's global symbol table.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
// Defined here; exported to the process's global symbol table so the
|
||||
// dlopen'd synthesized ELF can resolve `the_answer` via GLOB_DAT.
|
||||
extern "C" int the_answer = 42;
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using int_fn = int (*)();
|
||||
auto* get = reinterpret_cast<int_fn>(::dlsym(h, "get_the_answer"));
|
||||
auto* dbl = reinterpret_cast<int_fn>(::dlsym(h, "double_the_answer"));
|
||||
REQUIRE(get != nullptr);
|
||||
REQUIRE(dbl != nullptr);
|
||||
|
||||
REQUIRE(get() == 42);
|
||||
REQUIRE(dbl() == 84);
|
||||
|
||||
the_answer = 7;
|
||||
REQUIRE(get() == 7);
|
||||
REQUIRE(dbl() == 14);
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_extern_read — GOTPCREL / GLOB_DAT extern global works\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,46 @@
|
||||
// E2E: external-call demo. hello_printf.c calls printf (libc) —
|
||||
// an external symbol resolved via a synthesized PLT stub at load time.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using fn_t = int (*)();
|
||||
auto* hello = reinterpret_cast<fn_t>(::dlsym(h, "hello"));
|
||||
REQUIRE(hello != nullptr);
|
||||
|
||||
const int result = hello();
|
||||
if (result != 42) {
|
||||
std::fprintf(stderr, "FAIL: hello() = %d (expected 42)\n", result);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_hello_printf — PLT stub resolved, "
|
||||
"hello() = %d\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,39 @@
|
||||
// E2E: host-libc delegation — symbols from both libc.so.6 (strlen) and libm.so.6 (sqrt).
|
||||
// Verifies that multi-DT_NEEDED ELF synthesis resolves symbols across two libraries.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
#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 main() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using fn_t = int (*)();
|
||||
auto* fn = reinterpret_cast<fn_t>(::dlsym(h, "multi_libc_test"));
|
||||
REQUIRE(fn != nullptr);
|
||||
|
||||
const int result = fn();
|
||||
if (result != 0) {
|
||||
std::fprintf(stderr, "FAIL: multi_libc_test() = %d (expected 0)\n", result);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_host_libc_multi — strlen(libc) + sqrt(libm) resolved\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// E2E: host-libc delegation — pthread_create/join resolved from libpthread.so.0
|
||||
// (or libc.so.6 on modern glibc where pthread is merged).
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
#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 main() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using fn_t = int (*)(int);
|
||||
auto* fn = reinterpret_cast<fn_t>(::dlsym(h, "pthread_double"));
|
||||
REQUIRE(fn != nullptr);
|
||||
|
||||
const int result = fn(21);
|
||||
if (result != 42) {
|
||||
std::fprintf(stderr, "FAIL: pthread_double(21) = %d (expected 42)\n", result);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_host_libc_pthread — pthread_create/join resolved, "
|
||||
"pthread_double(21) = %d\n", result);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// E2E: multi-object merge. math_a.o (add, square) and math_b.o
|
||||
// (add_and_double, sum_of_squares — calling into math_a) are merged into a
|
||||
// single .vlc. Tests that cross-object PLT32 relocs are resolved as direct
|
||||
// PC32 calls (no PLT stub for internally-defined symbols).
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using int2_fn = int (*)(int, int);
|
||||
using int1_fn = int (*)(int);
|
||||
|
||||
auto* add = reinterpret_cast<int2_fn>(::dlsym(h, "add"));
|
||||
auto* square = reinterpret_cast<int1_fn>(::dlsym(h, "square"));
|
||||
auto* add_and_double = reinterpret_cast<int2_fn>(::dlsym(h, "add_and_double"));
|
||||
auto* sum_of_squares = reinterpret_cast<int2_fn>(::dlsym(h, "sum_of_squares"));
|
||||
|
||||
REQUIRE(add != nullptr);
|
||||
REQUIRE(square != nullptr);
|
||||
REQUIRE(add_and_double != nullptr);
|
||||
REQUIRE(sum_of_squares != nullptr);
|
||||
|
||||
REQUIRE(add(3, 4) == 7);
|
||||
REQUIRE(square(5) == 25);
|
||||
REQUIRE(add_and_double(3, 4) == 14); // (3+4)*2
|
||||
REQUIRE(sum_of_squares(3, 4) == 25); // 9+16
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_multi_obj — multi-object merge + cross-object call\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,49 @@
|
||||
// E2E: stock-toolchain pipeline.
|
||||
//
|
||||
// CMake compiles add_real.c with -fPIC -c, invokes voidlink pack-elf to
|
||||
// produce add_real.vls, then this test loads it via dlopen (which hits the
|
||||
// libvoid interposer) and confirms add(7, 35) == 42.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using add_fn_t = int (*)(int, int);
|
||||
auto* add = reinterpret_cast<add_fn_t>(::dlsym(h, "add"));
|
||||
REQUIRE(add != nullptr);
|
||||
|
||||
const int result = add(7, 35);
|
||||
if (result != 42) {
|
||||
std::fprintf(stderr, "FAIL: add(7, 35) = %d (expected 42)\n", result);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_real_add — stock toolchain → .vls → libvoid → %d\n",
|
||||
result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,56 @@
|
||||
// E2E: global-data demo. static_counter.c uses a BSS global (call_count)
|
||||
// and an initialised .data global (step). PC32 relocs from .text to both
|
||||
// sections are patched at synthesis time using vaddr arithmetic.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using void_fn = void (*)();
|
||||
using int_fn = int (*)();
|
||||
|
||||
auto* reset = reinterpret_cast<void_fn>(::dlsym(h, "counter_reset"));
|
||||
auto* step_up = reinterpret_cast<int_fn> (::dlsym(h, "counter_step_up"));
|
||||
auto* get = reinterpret_cast<int_fn> (::dlsym(h, "counter_get"));
|
||||
REQUIRE(reset != nullptr);
|
||||
REQUIRE(step_up != nullptr);
|
||||
REQUIRE(get != nullptr);
|
||||
|
||||
REQUIRE(get() == 0);
|
||||
REQUIRE(step_up() == 1);
|
||||
REQUIRE(step_up() == 2);
|
||||
REQUIRE(step_up() == 3);
|
||||
REQUIRE(get() == 3);
|
||||
|
||||
reset();
|
||||
REQUIRE(get() == 0);
|
||||
REQUIRE(step_up() == 1);
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_static_counter — BSS/data globals work\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,49 @@
|
||||
// E2E: internal-call demo. sum_squares.c has two functions where
|
||||
// sum_squares calls square (marked noinline). gcc emits a PC32/PLT32
|
||||
// reloc on the call; voidlink lowers it to portable RelocOp::PC32 and
|
||||
// the synthesizer patches it in-place at load time.
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
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() {
|
||||
void* h = ::dlopen(VOIDLIBC_TEST_VLC_PATH, RTLD_NOW);
|
||||
REQUIRE(h != nullptr);
|
||||
|
||||
using fn_t = int (*)(int, int);
|
||||
auto* sum_squares = reinterpret_cast<fn_t>(::dlsym(h, "sum_squares"));
|
||||
REQUIRE(sum_squares != nullptr);
|
||||
|
||||
const int result = sum_squares(3, 4); // 9 + 16 = 25
|
||||
if (result != 25) {
|
||||
std::fprintf(stderr, "FAIL: sum_squares(3, 4) = %d (expected 25)\n",
|
||||
result);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
REQUIRE(::dlclose(h) == 0);
|
||||
std::printf("PASS: e2e_load_sum_squares — internal reloc patched, "
|
||||
"sum_squares(3, 4) = %d\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() { return run(); }
|
||||
@@ -0,0 +1,10 @@
|
||||
// Object A: provides add() and square().
|
||||
// Compiled separately to test multi-object merge.
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int square(int x) {
|
||||
return x * x;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Object B: calls add() and square() defined in math_a.c.
|
||||
// Tests cross-object PLT32 → PC32 resolution in the merger.
|
||||
|
||||
extern int add(int, int);
|
||||
extern int square(int);
|
||||
|
||||
int add_and_double(int a, int b) {
|
||||
return add(a, b) * 2;
|
||||
}
|
||||
|
||||
int sum_of_squares(int a, int b) {
|
||||
return add(square(a), square(b));
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Stateful library using BSS (zero-init global) and .data (init global).
|
||||
// Exercises PC32 relocs from .text to .bss/.data across segments.
|
||||
|
||||
static int call_count = 0; // BSS: zero-initialized
|
||||
static int step = 1; // .data: initialized to 1
|
||||
|
||||
void counter_reset(void) {
|
||||
call_count = 0;
|
||||
}
|
||||
|
||||
int counter_step_up(void) {
|
||||
call_count += step;
|
||||
return call_count;
|
||||
}
|
||||
|
||||
int counter_get(void) {
|
||||
return call_count;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
__attribute__((noinline))
|
||||
int square(int x) { return x * x; }
|
||||
|
||||
int sum_squares(int a, int b) {
|
||||
return square(a) + square(b);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// E2E: structural blob validation — confirms the Windows (MZ) and macOS
|
||||
// (Mach-O 64-bit) sections inside a fat .vls have correct magic bytes.
|
||||
// This runs on Linux and validates cross-platform blobs without executing them.
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "format/vlc_format.hpp"
|
||||
|
||||
#ifndef VOIDLIBC_TEST_VLC_PATH
|
||||
#error "VOIDLIBC_TEST_VLC_PATH must be set by CMake"
|
||||
#endif
|
||||
|
||||
using namespace voidlibc::format;
|
||||
|
||||
#define REQUIRE(cond) do { \
|
||||
if (!(cond)) { \
|
||||
std::fprintf(stderr, "FAIL: %s (line %d)\n", #cond, \
|
||||
__LINE__); \
|
||||
std::exit(1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static std::vector<std::uint8_t> read_section_bytes(
|
||||
std::ifstream& f, const SectionEntry& sec) {
|
||||
f.seekg(static_cast<std::streamoff>(sec.code_off));
|
||||
std::vector<std::uint8_t> buf(static_cast<std::size_t>(sec.code_size));
|
||||
f.read(reinterpret_cast<char*>(buf.data()),
|
||||
static_cast<std::streamsize>(buf.size()));
|
||||
return buf;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ifstream f(VOIDLIBC_TEST_VLC_PATH, std::ios::binary);
|
||||
REQUIRE(f.is_open());
|
||||
|
||||
FileHeader hdr{};
|
||||
f.read(reinterpret_cast<char*>(&hdr), sizeof(hdr));
|
||||
REQUIRE(f.good());
|
||||
REQUIRE(hdr.magic == kMagic);
|
||||
REQUIRE(hdr.section_count >= 3); // must have linux + windows + macos
|
||||
|
||||
f.seekg(static_cast<std::streamoff>(hdr.section_dir_off));
|
||||
std::vector<SectionEntry> sects(hdr.section_count);
|
||||
f.read(reinterpret_cast<char*>(sects.data()),
|
||||
static_cast<std::streamsize>(hdr.section_count * sizeof(SectionEntry)));
|
||||
REQUIRE(f.good());
|
||||
|
||||
bool saw_linux = false, saw_windows = false, saw_macos = false;
|
||||
|
||||
for (const auto& sec : sects) {
|
||||
REQUIRE(sec.kind == SectionKind::NativeBlob);
|
||||
REQUIRE(sec.code_size >= 4);
|
||||
auto bytes = read_section_bytes(f, sec);
|
||||
REQUIRE(bytes.size() >= 4);
|
||||
|
||||
if (sec.target_os == TargetOS::Linux) {
|
||||
// ELF magic: \x7F E L F
|
||||
REQUIRE(bytes[0] == 0x7F && bytes[1] == 'E' &&
|
||||
bytes[2] == 'L' && bytes[3] == 'F');
|
||||
saw_linux = true;
|
||||
} else if (sec.target_os == TargetOS::Windows) {
|
||||
// PE: MZ header
|
||||
REQUIRE(bytes[0] == 'M' && bytes[1] == 'Z');
|
||||
saw_windows = true;
|
||||
} else if (sec.target_os == TargetOS::MacOS) {
|
||||
// Mach-O 64-bit LE: 0xCFFAEDFE
|
||||
REQUIRE(bytes[0] == 0xCF && bytes[1] == 0xFA &&
|
||||
bytes[2] == 0xED && bytes[3] == 0xFE);
|
||||
saw_macos = true;
|
||||
}
|
||||
}
|
||||
|
||||
REQUIRE(saw_linux);
|
||||
REQUIRE(saw_windows);
|
||||
REQUIRE(saw_macos);
|
||||
|
||||
std::printf("PASS: e2e_blob_validate — ELF/PE/Mach-O magic bytes all correct\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user