From 549a6f8b67b9cd7c6dd217abef2ad9631a44adbe Mon Sep 17 00:00:00 2001 From: nandithebull Date: Sat, 21 Nov 2026 18:12:30 -0800 Subject: [PATCH] Pin upstream wasm2elf with the sys intrinsics instead of carrying a patch Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 +- elf/wasm2elf.patch | 147 --------------------------------------------- elf/wasm2elf.rev | 2 +- justfile | 1 - 4 files changed, 3 insertions(+), 151 deletions(-) delete mode 100644 elf/wasm2elf.patch diff --git a/README.md b/README.md index 00b413b..2bb8997 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,8 @@ If WABT's `wasm2c` runtime sources (`wasm-rt.h` and `wasm-rt-impl.c`) are not fo runtime and no OpenSSL, using [wasm2elf-mb](https://git.site.latha.org/nandi/wasm2elf-mb). `elf/flatten.py` merges the component's allocator module and core module into one core module; `elf/shim.wat` replaces the canon-lowered `wasi:*` imports with hand-written WAT -that calls Linux syscalls through a few `sys::*` intrinsics (`elf/wasm2elf.patch` adds -those plus bulk-memory `memory.copy`/`memory.fill` to wasm2elf, pinned in `elf/wasm2elf.rev`). +that calls Linux syscalls through a few `sys::*` intrinsics (wasm2elf gained those plus bulk-memory +`memory.copy`/`memory.fill` upstream; the revision is pinned in `elf/wasm2elf.rev`). The shim speaks HTTP/1.0 over IPv4 TCP with a minimal UDP DNS resolver (nameserver from `/etc/resolv.conf`). `just test-elf` passes 26 of 27 tests; the failure is diff --git a/elf/wasm2elf.patch b/elf/wasm2elf.patch deleted file mode 100644 index 9c9c319..0000000 --- a/elf/wasm2elf.patch +++ /dev/null @@ -1,147 +0,0 @@ -diff --git a/compile.mbt b/compile.mbt -index 5b90389..727ec3f 100644 ---- a/compile.mbt -+++ b/compile.mbt -@@ -355,6 +355,15 @@ fn compile_body( - 0x43 => ignore(r.raw32()) - 0x44 => ignore(r.raw64()) - 0x3F | 0x40 => ignore(r.byte()) -+ 0xFC => -+ match r.u32() { -+ 0x0A => { -+ ignore(r.byte()) -+ ignore(r.byte()) -+ } -+ 0x0B => ignore(r.byte()) -+ _ => () -+ } - n if n >= 0x28 && n <= 0x3E => { - ignore(r.u32()) - ignore(r.u32()) -@@ -487,6 +496,7 @@ fn compile_body( - f.code.bs([0x6A, 0xFF]) - f.height += 1 - } -+ 0xFC => f.bulk_mem(r.u32(), r) - 0x41 => { - f.code.b(0x68) - f.code.i32(r.s32()) -@@ -831,3 +841,59 @@ fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit - } - self.push_rax() - } -+ -+///| -+/// Bulk memory ops: memory.copy (0xFC 0x0A, overlap-safe) and memory.fill (0xFC 0x0B), -+/// with bounds checks, via rep movsb / rep stosb. -+fn Fn::bulk_mem(self : Fn, sub : Int32, r : Reader) -> Unit raise CompileError { -+ if self.mem_bytes == 0 { -+ raise CompileError("memory access but the module defines no memory") -+ } -+ match sub { -+ 0x0A => { -+ ignore(r.byte()) -+ ignore(r.byte()) -+ self.pop(1) // rcx = n -+ self.pop(6) // rsi = src -+ self.pop(7) // rdi = dst -+ self.code.bs([0x89, 0xC9, 0x89, 0xF6, 0x89, 0xFF]) // zero-extend ecx,esi,edi -+ self.code.bs([0x48, 0x8D, 0x04, 0x0E]) // lea rax,[rsi+rcx] -+ self.code.bs([0x48, 0x3D]) -+ self.code.i32(self.mem_bytes) -+ self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) -+ self.code.bs([0x48, 0x8D, 0x04, 0x0F]) // lea rax,[rdi+rcx] -+ self.code.bs([0x48, 0x3D]) -+ self.code.i32(self.mem_bytes) -+ self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) -+ self.code.bs([0x48, 0x81, 0xC6]) -+ self.code.i32(MEM_BASE) -+ self.code.bs([0x48, 0x81, 0xC7]) -+ self.code.i32(MEM_BASE) -+ self.code.bs([0x48, 0x39, 0xF7]) // cmp rdi,rsi -+ let fwd = self.code.rel8([0x76]) // jbe forward -+ self.code.bs([0xFD]) // std -+ self.code.bs([0x48, 0x8D, 0x74, 0x0E, 0xFF]) // lea rsi,[rsi+rcx-1] -+ self.code.bs([0x48, 0x8D, 0x7C, 0x0F, 0xFF]) // lea rdi,[rdi+rcx-1] -+ self.code.bs([0xF3, 0xA4, 0xFC]) // rep movsb; cld -+ let done = self.code.rel8([0xEB]) -+ self.code.patch_here8(fwd) -+ self.code.bs([0xF3, 0xA4]) -+ self.code.patch_here8(done) -+ } -+ 0x0B => { -+ ignore(r.byte()) -+ self.pop(1) // rcx = n -+ self.pop(0) // rax = value -+ self.pop(7) // rdi = dst -+ self.code.bs([0x89, 0xC9, 0x89, 0xFF]) -+ self.code.bs([0x48, 0x8D, 0x14, 0x0F]) // lea rdx,[rdi+rcx] -+ self.code.bs([0x48, 0x81, 0xFA]) -+ self.code.i32(self.mem_bytes) -+ self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) -+ self.code.bs([0x48, 0x81, 0xC7]) -+ self.code.i32(MEM_BASE) -+ self.code.bs([0xF3, 0xAA]) -+ } -+ _ => raise CompileError("unsupported 0xFC opcode \{sub}") -+ } -+} -diff --git a/module.mbt b/module.mbt -index 30d21d7..6079965 100644 ---- a/module.mbt -+++ b/module.mbt -@@ -72,6 +72,45 @@ fn import_stub(m : String, name : String) -> Asm? { - s.i32(MEM_BASE) - s.b(0) - } -+ // --- raw host intrinsics for hand-written wasm shims (module "sys") --- -+ "sys::syscall" => { -+ // (nr, a1..a6 : i64) -> i64 -+ s.bs([0x48, 0x8B, 0x45, 0x40]) // mov rax,[rbp+64] -+ s.bs([0x48, 0x8B, 0x7D, 0x38]) // mov rdi,[rbp+56] -+ s.bs([0x48, 0x8B, 0x75, 0x30]) // mov rsi,[rbp+48] -+ s.bs([0x48, 0x8B, 0x55, 0x28]) // mov rdx,[rbp+40] -+ s.bs([0x4C, 0x8B, 0x55, 0x20]) // mov r10,[rbp+32] -+ s.bs([0x4C, 0x8B, 0x45, 0x18]) // mov r8,[rbp+24] -+ s.bs([0x4C, 0x8B, 0x4D, 0x10]) // mov r9,[rbp+16] -+ s.bs([0x0F, 0x05]) -+ } -+ "sys::membase" => { -+ s.b(0xB8) -+ s.i32(MEM_BASE) -+ } -+ "sys::argv_base" => { -+ // initial rsp saved by _start (points at argc) -+ s.bs([0x48, 0x8B, 0x04, 0x25]) -+ s.i32(GLOB_BASE + 0xFF8) -+ } -+ "sys::peek64" => { -+ // (host addr : i64) -> i64 -+ s.bs([0x48, 0x8B, 0x45, 0x10, 0x48, 0x8B, 0x00]) -+ } -+ "sys::strlen" => { -+ // (host addr : i64) -> i64 -+ s.bs([0x48, 0x8B, 0x7D, 0x10, 0x31, 0xC0, 0x48, 0xC7, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF]) -+ s.bs([0xF2, 0xAE, 0x48, 0xF7, 0xD1, 0x48, 0xFF, 0xC9, 0x48, 0x89, 0xC8]) -+ } -+ "sys::copy_in" => { -+ // (dst guest i32, src host i64, n i32) -> () -+ s.bs([0x8B, 0x7D, 0x20]) // mov edi,[rbp+32] -+ s.bs([0x48, 0x81, 0xC7]) -+ s.i32(MEM_BASE) -+ s.bs([0x48, 0x8B, 0x75, 0x18]) // mov rsi,[rbp+24] -+ s.bs([0x8B, 0x4D, 0x10]) // mov ecx,[rbp+16] -+ s.bs([0xF3, 0xA4]) -+ } - "wasi:cli/exit::exit" => - // (status: result) -> () ; 0 = ok, 1 = err - s.bs([0x8B, 0x7D, 0x10, 0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05]) -@@ -335,6 +374,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError { - let phnum = if has_data { 2 } else { 1 } - let headers = 64 + 56 * phnum - let a = Asm::new() -+ a.bs([0x48, 0x89, 0x24, 0x25]) // mov [GLOB_BASE+0xFF8], rsp (argc/argv) -+ a.i32(GLOB_BASE + 0xFF8) - let start_call = a.rel([0xE8]) - a.bs([0x89, 0xC7]) // mov edi,eax - if types[func_types[entry_idx]].nresults == 0 { diff --git a/elf/wasm2elf.rev b/elf/wasm2elf.rev index 24d3448..2822b0c 100644 --- a/elf/wasm2elf.rev +++ b/elf/wasm2elf.rev @@ -1 +1 @@ -987cdf256bc7e298072c51f5078eec28500b7fae +fdc1cd4b3c6fffc985e37ecb7fba7930197d5a8a diff --git a/justfile b/justfile index 1f4d027..e71a662 100644 --- a/justfile +++ b/justfile @@ -24,7 +24,6 @@ elf: if [ ! -d $W ]; then git clone https://git.site.latha.org/nandi/wasm2elf-mb $W git -C $W checkout "$(cat elf/wasm2elf.rev)" - git -C $W apply ../../elf/wasm2elf.patch fi (cd $W && moon build --target native) python3 elf/flatten.py curl.wat elf/shim.wat build/curl-flat.wat