Spike: build a static ELF via wasm2elf with WAT syscall shims
Add elf/ (flatten.py, shim.wat, wasm2elf.patch pinned to wasm2elf-mb rev) and just elf / test-elf recipes. The ELF handles plain HTTP, DNS, files and stdin; 26/27 repo tests pass, https:// is unsupported (no TLS). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed06fa994a
commit
27ce306c7c
7 files changed
+770
-1
No files matched your search
@@ -0,0 +1,147 @@
|
||||
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 {
|
||||
Reference in new issue
Block a user