THE WHITE DRAGON BOOK · CHAPTER 05

Chapter 5: W^X (RW → RX) In-Memory Linking & Lazy Trampolines

CH. 5 · W^X RW → RX IN-MEMORY LINKER MAP_RW Emit & Patch mprotect MAP_RX + TRAMPOLINE Lazy On-Demand Bind Zero Disk I/O · Strict W^X Enforcement
Vector Architecture Diagram · asset-ch05

Chapter 5: W^X (RW → RX) In-Memory Linking & Lazy Trampolines

Lineage: Green/Red/Purple Dragon (Static ld / Eager Dynamic Loader PLT/GOT) → White Dragon (Per-Origin W^X In-Memory Trampolines)
Live Proof: sections/chromium-build/inmem-linker/b8g_origin_linker.h

5.1 The Eager Linking Fallacy

Classical linkers (ld, lld-link, and OS dynamic loaders ntdll/ld.so) assume every binary a process might ever touch should be resolved up front into a global process address space. In a browser, that means an idle about:blank tab inherits mapped pages, relocations, and static initializers for ffmpeg, pdfium, dav1d, webrtc, skia, and sqlite — even when the origin never decodes a single video frame or PDF.

This eager global linking creates two fatal flaws:

5.2 Strict W^X (RW → RX) x86-64 Trampoline Synthesis

b8g::inmem_linker::B8gOriginArena replaces eager global linking with a single 4 KB per-origin trampoline page and a 32-bit loaded_mask bitset over B8gDepId (B8G_DEP_IPCZ through B8G_DEP_BROTLI).

When an origin starts, loaded_count == 0 and 0 B of optional dependency RAM is materialized. The first time an origin invokes a KEEP-ALGO subsystem, LoadDepOnDemand() resolves its zero-header B8G-ABI getter symbol and stamps a 16-byte aligned x86-64 direct-jump trampoline under strict Write XOR Execute (W^X) page protection:

/* Exact 16-byte x86-64 trampoline slot emitted by InstallWxTrampoline()
 * in sections/chromium-build/inmem-linker/b8g_origin_linker.h:173-188 */
DWORD old_prot = 0;
VirtualProtect(wx_trampoline_page, 4096, PAGE_READWRITE, &old_prot);

uint8_t* code = wx_trampoline_page + (slot * 16);
code[0] = 0x48;                          /* REX.W prefix                 */
code[1] = 0xB8;                          /* movabs rax, imm64            */
uint64_t addr = (uint64_t)target_fn;
memcpy(code + 2, &addr, 8);              /* 64-bit absolute target       */
code[10] = 0xFF;                         /* jmp rax                      */
code[11] = 0xE0;
code[12] = code[13] = code[14] = code[15] = 0xCC; /* int3 padding guard  */

VirtualProtect(wx_trampoline_page, 4096, PAGE_EXECUTE_READ, &old_prot);
FlushInstructionCache(GetCurrentProcess(), code, 16);

Security & Performance Invariants of the Trampoline Page

5.3 Lazy On-Demand Binding to B8gOriginAllocator

Every B8gDepModuleV3 loaded through the in-memory linker receives the origin's own B8gOriginAllocator (alloc, realloc, free no-op) during init_origin(). No third-party parser touches the global process heap, and every module carries its cryptographic cas_sha256 identity:

typedef struct B8gOriginAllocator {
  void* (*alloc)(void* origin_arena, size_t bytes, size_t align);
  void* (*realloc)(void* origin_arena, void* ptr, size_t bytes);
  void  (*free)(void* origin_arena, void* ptr); /* O(1) no-op; reclaimed on Dispose() */
} B8gOriginAllocator;

typedef struct B8gDepModuleV3 {
  uint32_t abi_version;   /* B8G_DEP_ABI_VERSION == 3u */
  uint32_t dep_id;        /* B8gDepId: IPCZ..BROTLI (0..18) */
  const char* name;
  const char* cas_sha256; /* .b8p content-addressed verification digest */
  void* (*init_origin)(void* origin_arena, const B8gOriginAllocator* alloc);
  void  (*dispose_origin)(void* module_state);
  const void* dispatch_table;
} B8gDepModuleV3;

On LoadDepOnDemand(dep_id, module_path, getter_symbol), if (loaded_mask & (1u << dep_id)) is already set, the cached dep_tables[dep_id] pointer returns in a single branch. Otherwise, the linker maps the module, stamps the W^X trampoline slot, binds dep_tables[dep_id], and flips the bit in loaded_mask.

5.4 Calling-Convention Canonicalization (The 2-Instruction Cross-OS Bridge)

A second, deeper consequence emerges when B8G_ARENA_V1 memory sections meet W^X trampolines and V8/WASM execution: OS calling-convention mismatch collapses to a single register move.

In classical native builds, Windows x64 (RCX, RDX, R8, R9 + 32-byte shadow space, LLP64 long) and Linux/macOS System V AMD64 (RDI, RSI, RDX, RCX, R8, R9 + 128-byte red zone, LP64 long) cannot share compiled .obj/.o code because every function packs multi-argument C++ structs across incompatible registers and stack frames.

Inside V8 and WASM, the engine already enforces one uniform internal calling convention across all operating systems, crossing the host ABI only at thin CEntry / WasmToC stubs. B8G-ABI generalizes that exact principle to native modules: instead of passing scattered stack arguments across boundaries, every subsystem receives one single pointer to an offset-indexed, 64-byte-aligned B8G_ARENA_V1 section (analogous to io_uring submission entries or WASM linear memory). Bridging Win64 ↔ Linux SysV x64 then reduces to a 2-instruction W^X trampoline (< 0.5 ns, zero stack copy):

mov rdi, rcx   ; Win64 arg0 (B8G_ARENA_V1*) -> Linux SysV arg0
jmp target     ; Zero-copy handoff into canonical section-indexed code

The memory abstraction therefore does three jobs at once: it aligns state for SIMD/cache-lines, makes every field reference-indexable and linkable in RAM, and canonicalizes the calling convention across operating systems.

5.5 Historical Contrast: Disk Linkers vs. W^X In-Memory Linking

Dimension Classical Disk / Dynamic Linker (lld-link / ld.so) White Dragon W^X In-Memory Linker
Idle Dependency RAM Full eager mapping (551 DLLs / 1.30 GB footprint) 0 B (loaded_mask == 0 until first call)
Warm Materialization (3 DLLs) Process-wide eager loader stall 0.893 ms (lazy per-origin bind + W^X stamp)
Warm Materialization (18 DLLs) Global symbol graph + TLS init 1.968 ms (all 18 B8G-ABI modules bound)
Code Page Protection Static .text or risky JIT RWX Strict W^X (RW → RX) with 0xCC slot guards
Unload / Isolation Process-lifetime global state; UAF persists Atomic VirtualFree / munmap on origin close

We have now eliminated textual headers (Ch. 3), redundant compilation (Ch. 4), and eager disk linking (Ch. 5). In Chapter 6, we examine the memory substrate beneath B8gOriginArena: B8G_ARENA_V1 & O(1) Origin Disposal.

← Back to White Dragon Book index