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
@@ -3,3 +3,4 @@ curl-native
|
||||
build/
|
||||
__pycache__/
|
||||
*.pyc
|
||||
curl-elf
|
||||
@@ -77,3 +77,16 @@ If WABT's `wasm2c` runtime sources (`wasm-rt.h` and `wasm-rt-impl.c`) are not fo
|
||||
### `--write-out` Variables
|
||||
|
||||
`%{http_code}`, `%{response_code}`, `%{size_download}`, `%{size_upload}`, `%{time_total}`, `%{url_effective}`, `%{content_type}`, `%{redirect_url}`, `%{num_redirects}`, `%{scheme}`, `%{method}`, `\n`, `\r`, `\t`, `\\`, `%%`.
|
||||
|
||||
## Compile to a static ELF (`wasm2elf`, spike)
|
||||
|
||||
`just elf` builds `./curl-elf`, a ~1 MB static x86-64 Linux executable with no libc, no
|
||||
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`).
|
||||
|
||||
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
|
||||
`live_https_example_com`: **`https://` is not supported** (no TLS stack).
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Flatten curl.wat's component (mem module + curl core module + canon-lowered wasi
|
||||
imports) into ONE plain core module for wasm2elf. The lowered `wasi` imports are replaced
|
||||
by the WAT shims in shim.wat, which talk to Linux through the `sys` host intrinsics
|
||||
that elf/wasm2elf.patch adds to wasm2elf.
|
||||
|
||||
usage: flatten.py curl.wat shim.wat out.wat
|
||||
"""
|
||||
import re, sys
|
||||
|
||||
src = open(sys.argv[1]).read().split("\n")
|
||||
shim = open(sys.argv[2]).read()
|
||||
|
||||
def find(pred, start=0):
|
||||
for i in range(start, len(src)):
|
||||
if pred(src[i]):
|
||||
return i
|
||||
raise SystemExit("marker not found")
|
||||
|
||||
# mem module body: from the realloc func to the line before the closing paren of $mem_mod
|
||||
m0 = find(lambda l: '(func $realloc (export "realloc")' in l)
|
||||
m1 = find(lambda l: l.startswith(" (core instance $mem_inst"), m0)
|
||||
mem = src[m0:m1]
|
||||
while mem and mem[-1].strip() == "":
|
||||
mem.pop()
|
||||
assert mem.pop() == " )" # closing paren of (core module $mem_mod
|
||||
mem = [l.replace('(func (export "alloc")', "(func $alloc")
|
||||
.replace('(func (export "heap_mark")', "(func $heap_mark")
|
||||
.replace('(func (export "heap_reset")', "(func $heap_reset")
|
||||
.replace('(export "realloc")', "") for l in mem]
|
||||
|
||||
# curl core module body: from its header to the line before the section-5 banner
|
||||
c0 = find(lambda l: l.startswith(" (core module $curl"))
|
||||
c1 = find(lambda l: "5. Instantiate Core $curl" in l, c0) - 1 # banner starts with a ;; ==== line
|
||||
while src[c1].strip().startswith(";;") or src[c1].strip() == "":
|
||||
c1 -= 1
|
||||
body = src[c0:c1 + 1]
|
||||
body.pop() # closing paren of (core module $curl
|
||||
|
||||
imports, rest = [], []
|
||||
for l in body:
|
||||
m = re.match(r'\s*\(import "wasi" "(\w+)" \(func (\$\w+) (.*)\)\)\s*$', l)
|
||||
if m:
|
||||
imports.append((m.group(1), m.group(2), m.group(3)))
|
||||
elif re.match(r'\s*\(import "mem"', l) or l.startswith(" (core module"):
|
||||
pass
|
||||
else:
|
||||
rest.append(l)
|
||||
|
||||
defined = set(re.findall(r"\(func (\$\w+)", shim))
|
||||
out = [
|
||||
"(module",
|
||||
'(import "sys" "syscall" (func $syscall (param i64 i64 i64 i64 i64 i64 i64) (result i64)))',
|
||||
'(import "sys" "membase" (func $membase (result i64)))',
|
||||
'(import "sys" "argv_base" (func $argv_base (result i64)))',
|
||||
'(import "sys" "peek64" (func $peek64 (param i64) (result i64)))',
|
||||
'(import "sys" "strlen" (func $strlen (param i64) (result i64)))',
|
||||
'(import "sys" "copy_in" (func $copy_in (param i32 i64 i32)))',
|
||||
'(memory (export "memory") 1024)',
|
||||
"(global $heap (mut i32) (i32.const 65536))",
|
||||
]
|
||||
out += mem
|
||||
for name, fid, sig in imports:
|
||||
if fid not in defined:
|
||||
out.append(f"(func {fid} {sig} unreachable)")
|
||||
out.append(shim)
|
||||
out += rest
|
||||
out.append(")")
|
||||
open(sys.argv[3], "w").write("\n".join(out) + "\n")
|
||||
+519
@@ -0,0 +1,519 @@
|
||||
;; ---- spike shims: lowered wasi:* -> linux syscalls (plain HTTP/1.0, IPv4) ----
|
||||
;; shim heap lives at 0x02000000.. ; scratch syscall buffer at 0x01FF0000
|
||||
(global $sh (mut i32) (i32.const 0x02000000))
|
||||
(global $g_resp (mut i32) (i32.const 0)) ;; last response obj
|
||||
(func $sys (param i64 i64 i64 i64) (result i64)
|
||||
(call $syscall (local.get 0) (local.get 1) (local.get 2) (local.get 3) (i64.const 0) (i64.const 0) (i64.const 0)))
|
||||
(func $hp (param i32) (result i64) ;; guest ptr -> host ptr
|
||||
(i64.add (i64.extend_i32_u (local.get 0)) (call $membase)))
|
||||
(func $sbrk (param $n i32) (result i32) (local $p i32)
|
||||
(local.set $p (global.get $sh))
|
||||
(global.set $sh (i32.and (i32.add (i32.add (local.get $p) (local.get $n)) (i32.const 7)) (i32.const -8)))
|
||||
(local.get $p))
|
||||
(func $dup (param $p i32) (param $n i32) (result i32) (local $d i32)
|
||||
(local.set $d (call $sbrk (i32.add (local.get $n) (i32.const 1))))
|
||||
(memory.copy (local.get $d) (local.get $p) (local.get $n))
|
||||
(local.get $d))
|
||||
(func $ieq (param $a i32) (param $al i32) (param $b i32) (param $bl i32) (result i32) (local $i i32) (local $x i32) (local $y i32)
|
||||
(if (i32.ne (local.get $al) (local.get $bl)) (then (return (i32.const 0))))
|
||||
(loop $l
|
||||
(if (i32.lt_u (local.get $i) (local.get $al)) (then
|
||||
(local.set $x (i32.load8_u (i32.add (local.get $a) (local.get $i))))
|
||||
(local.set $y (i32.load8_u (i32.add (local.get $b) (local.get $i))))
|
||||
(if (i32.and (i32.ge_u (local.get $x) (i32.const 65)) (i32.le_u (local.get $x) (i32.const 90))) (then (local.set $x (i32.add (local.get $x) (i32.const 32)))))
|
||||
(if (i32.and (i32.ge_u (local.get $y) (i32.const 65)) (i32.le_u (local.get $y) (i32.const 90))) (then (local.set $y (i32.add (local.get $y) (i32.const 32)))))
|
||||
(if (i32.ne (local.get $x) (local.get $y)) (then (return (i32.const 0))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $l))))
|
||||
(i32.const 1))
|
||||
|
||||
;; ---- trivial streams / io ----
|
||||
(func $get_stdin (result i32) (i32.const 0))
|
||||
(func $get_stdout (result i32) (i32.const 1))
|
||||
(func $get_stderr (result i32) (i32.const 2))
|
||||
(func $poll_block (param i32))
|
||||
(func $out_subscribe (param i32) (result i32) (i32.const 1))
|
||||
(func $out_check_write (param $s i32) (param $rp i32)
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i64.store (i32.add (local.get $rp) (i32.const 8)) (i64.const 65536)))
|
||||
(func $out_blocking_flush (param $s i32) (param $rp i32) (i32.store8 (local.get $rp) (i32.const 0)))
|
||||
(func $out_write (param $s i32) (param $p i32) (param $n i32) (param $rp i32) (local $b i32) (local $bl i32) (local $w i64) (local $o i32)
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(if (i32.and (i32.ge_u (local.get $s) (i32.const 8192)) (i32.lt_u (local.get $s) (i32.const 12288))) (then
|
||||
(local.set $o (i32.const 0)) (local.set $s (i32.sub (local.get $s) (i32.const 8192)))
|
||||
(loop $fl
|
||||
(if (i32.lt_u (local.get $o) (local.get $n)) (then
|
||||
(local.set $w (call $sys (i64.const 1) (i64.extend_i32_u (local.get $s))
|
||||
(call $hp (i32.add (local.get $p) (local.get $o))) (i64.extend_i32_u (i32.sub (local.get $n) (local.get $o)))))
|
||||
(if (i64.le_s (local.get $w) (i64.const 0)) (then (i32.store8 (local.get $rp) (i32.const 1)) (i32.store8 offset=4 (local.get $rp) (i32.const 1)) (return)))
|
||||
(local.set $o (i32.add (local.get $o) (i32.wrap_i64 (local.get $w))))
|
||||
(br $fl))))
|
||||
(return)))
|
||||
(if (i32.lt_u (local.get $s) (i32.const 16)) (then
|
||||
(local.set $o (i32.const 0))
|
||||
(loop $l
|
||||
(if (i32.lt_u (local.get $o) (local.get $n)) (then
|
||||
(local.set $w (call $sys (i64.const 1) (i64.extend_i32_u (local.get $s))
|
||||
(call $hp (i32.add (local.get $p) (local.get $o))) (i64.extend_i32_u (i32.sub (local.get $n) (local.get $o)))))
|
||||
(if (i64.le_s (local.get $w) (i64.const 0)) (then (return)))
|
||||
(local.set $o (i32.add (local.get $o) (i32.wrap_i64 (local.get $w))))
|
||||
(br $l))))
|
||||
(return)))
|
||||
;; request body stream: handle = outgoing-body obj {req@0}; req body buf: ptr@64 len@68 cap@72
|
||||
(local.set $b (i32.load (local.get $s)))
|
||||
(if (i32.eqz (i32.load offset=72 (local.get $b))) (then
|
||||
(i32.store offset=64 (local.get $b) (i32.const 0x02800000))
|
||||
(i32.store offset=72 (local.get $b) (i32.const 0x00600000))))
|
||||
(if (i32.gt_u (i32.add (i32.load offset=68 (local.get $b)) (local.get $n)) (i32.load offset=72 (local.get $b))) (then
|
||||
(i32.store8 (local.get $rp) (i32.const 1)) (i32.store8 offset=4 (local.get $rp) (i32.const 1)) (return)))
|
||||
(memory.copy (i32.add (i32.load offset=64 (local.get $b)) (i32.load offset=68 (local.get $b))) (local.get $p) (local.get $n))
|
||||
(i32.store offset=68 (local.get $b) (i32.add (i32.load offset=68 (local.get $b)) (local.get $n))))
|
||||
(func $clock_now (result i64)
|
||||
(drop (call $sys (i64.const 228) (i64.const 1) (call $hp (i32.const 0x01FF0000)) (i64.const 0)))
|
||||
(i64.add (i64.mul (i64.load (i32.const 0x01FF0000)) (i64.const 1000000000)) (i64.load (i32.const 0x01FF0008))))
|
||||
(func $err_to_debug_string (param i32 i32) (i32.store (local.get 1) (i32.const 0)) (i32.store offset=4 (local.get 1) (i32.const 0)))
|
||||
;; ---- args: argv from the initial process stack ----
|
||||
(func $get_arguments (param $rp i32) (local $base i64) (local $argc i32) (local $i i32) (local $arr i32) (local $sp i64) (local $n i32) (local $d i32)
|
||||
(local.set $base (call $argv_base))
|
||||
(local.set $argc (i32.wrap_i64 (call $peek64 (local.get $base))))
|
||||
(local.set $arr (call $sbrk (i32.mul (local.get $argc) (i32.const 8))))
|
||||
(loop $l
|
||||
(if (i32.lt_u (local.get $i) (local.get $argc)) (then
|
||||
(local.set $sp (call $peek64 (i64.add (local.get $base) (i64.extend_i32_u (i32.mul (i32.add (local.get $i) (i32.const 1)) (i32.const 8))))))
|
||||
(local.set $n (i32.wrap_i64 (call $strlen (local.get $sp))))
|
||||
(local.set $d (call $sbrk (i32.add (local.get $n) (i32.const 1))))
|
||||
(call $copy_in (local.get $d) (local.get $sp) (i32.add (local.get $n) (i32.const 1)))
|
||||
(i32.store (i32.add (local.get $arr) (i32.mul (local.get $i) (i32.const 8))) (local.get $d))
|
||||
(i32.store offset=4 (i32.add (local.get $arr) (i32.mul (local.get $i) (i32.const 8))) (local.get $n))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $l))))
|
||||
(i32.store (local.get $rp) (local.get $arr))
|
||||
(i32.store offset=4 (local.get $rp) (local.get $argc)))
|
||||
|
||||
;; ---- fields: {n@0, entries@8.. 16 bytes each, cap 128} ----
|
||||
(func $new_fields (result i32) (call $sbrk (i32.const 2056)))
|
||||
(func $fields_append (param $f i32) (param $kp i32) (param $kl i32) (param $vp i32) (param $vl i32) (param $rp i32) (local $e i32)
|
||||
(local.set $e (i32.add (i32.add (local.get $f) (i32.const 8)) (i32.mul (i32.load (local.get $f)) (i32.const 16))))
|
||||
(i32.store (local.get $e) (call $dup (local.get $kp) (local.get $kl)))
|
||||
(i32.store offset=4 (local.get $e) (local.get $kl))
|
||||
(i32.store offset=8 (local.get $e) (call $dup (local.get $vp) (local.get $vl)))
|
||||
(i32.store offset=12 (local.get $e) (local.get $vl))
|
||||
(i32.store (local.get $f) (i32.add (i32.load (local.get $f)) (i32.const 1)))
|
||||
(i32.store8 (local.get $rp) (i32.const 0)))
|
||||
(func $fields_delete (param $f i32) (param $kp i32) (param $kl i32) (param $rp i32) (local $i i32) (local $j i32) (local $n i32) (local $e i32)
|
||||
(local.set $n (i32.load (local.get $f)))
|
||||
(loop $l
|
||||
(if (i32.lt_u (local.get $i) (local.get $n)) (then
|
||||
(local.set $e (i32.add (i32.add (local.get $f) (i32.const 8)) (i32.mul (local.get $i) (i32.const 16))))
|
||||
(if (i32.eqz (call $ieq (i32.load (local.get $e)) (i32.load offset=4 (local.get $e)) (local.get $kp) (local.get $kl))) (then
|
||||
(memory.copy (i32.add (i32.add (local.get $f) (i32.const 8)) (i32.mul (local.get $j) (i32.const 16))) (local.get $e) (i32.const 16))
|
||||
(local.set $j (i32.add (local.get $j) (i32.const 1)))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $l))))
|
||||
(i32.store (local.get $f) (local.get $j))
|
||||
(i32.store8 (local.get $rp) (i32.const 0)))
|
||||
(func $fields_set (param $f i32) (param $kp i32) (param $kl i32) (param $vp i32) (param $vl i32) (param $rp i32)
|
||||
(call $fields_delete (local.get $f) (local.get $kp) (local.get $kl) (local.get $rp))
|
||||
(call $fields_append (local.get $f) (local.get $kp) (local.get $kl) (local.get $vp) (local.get $vl) (local.get $rp)))
|
||||
(func $fields_entries (param $f i32) (param $rp i32)
|
||||
(i32.store (local.get $rp) (i32.add (local.get $f) (i32.const 8)))
|
||||
(i32.store offset=4 (local.get $rp) (i32.load (local.get $f))))
|
||||
|
||||
;; ---- request object: hdrs@0 method@4 mp@8 ml@12 scheme@16 authp@20 authl@24 pathp@28 pathl@32
|
||||
;; body: bufptr@64 len@68 cap@72 ; resp@76 ; state@80 ----
|
||||
(func $new_outgoing_request (param $h i32) (result i32) (local $r i32)
|
||||
(local.set $r (call $sbrk (i32.const 128)))
|
||||
(i32.store (local.get $r) (local.get $h))
|
||||
(local.get $r))
|
||||
(func $req_set_method (param $r i32) (param $tag i32) (param $p i32) (param $l i32) (result i32)
|
||||
(i32.store offset=4 (local.get $r) (local.get $tag))
|
||||
(i32.store offset=8 (local.get $r) (call $dup (local.get $p) (local.get $l)))
|
||||
(i32.store offset=12 (local.get $r) (local.get $l))
|
||||
(i32.const 0))
|
||||
(func $req_set_scheme (param $r i32) (param $some i32) (param $v i32) (param $p i32) (param $l i32) (result i32)
|
||||
(i32.store offset=16 (local.get $r) (local.get $v)) (i32.const 0))
|
||||
(func $req_set_authority (param $r i32) (param $some i32) (param $p i32) (param $l i32) (result i32)
|
||||
(i32.store offset=20 (local.get $r) (call $dup (local.get $p) (local.get $l)))
|
||||
(i32.store offset=24 (local.get $r) (local.get $l)) (i32.const 0))
|
||||
(func $req_set_path (param $r i32) (param $some i32) (param $p i32) (param $l i32) (result i32)
|
||||
(i32.store offset=28 (local.get $r) (call $dup (local.get $p) (local.get $l)))
|
||||
(i32.store offset=32 (local.get $r) (local.get $l)) (i32.const 0))
|
||||
(func $outgoing_request_body (param $r i32) (param $rp i32) (local $b i32)
|
||||
(local.set $b (call $sbrk (i32.const 16)))
|
||||
(i32.store (local.get $b) (local.get $r))
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (local.get $b)))
|
||||
(func $outgoing_body_write (param $b i32) (param $rp i32)
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (local.get $b)))
|
||||
(func $outgoing_body_finish (param $b i32) (param $a i32) (param $c i32) (param $rp i32) (i32.store8 (local.get $rp) (i32.const 0)))
|
||||
(func $new_request_options (result i32) (call $sbrk (i32.const 16)))
|
||||
(func $ro_set_connect_timeout (param i32 i32 i64) (result i32) (i32.const 0))
|
||||
(func $ro_set_first_byte_timeout (param i32 i32 i64) (result i32) (i32.const 0))
|
||||
(func $ro_set_between_bytes_timeout (param i32 i32 i64) (result i32) (i32.const 0))
|
||||
|
||||
;; error codes (error-code variant tags): DNS-error=1, connection-refused=6, internal-error=38? use indices below
|
||||
(func $http_fail (param $rp i32) (param $code i32)
|
||||
(i32.store8 (local.get $rp) (i32.const 1))
|
||||
(i32.store8 offset=8 (local.get $rp) (local.get $code)))
|
||||
|
||||
;; http_handle: executes immediately for bodyless; body is sent lazily in fir_get
|
||||
(func $http_handle (param $r i32) (param $ho i32) (param $o i32) (param $rp i32)
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=8 (local.get $rp) (local.get $r))) ;; future = request obj
|
||||
(func $fir_subscribe (param i32) (result i32) (i32.const 1))
|
||||
(func $fir_get (param $r i32) (param $rp i32) (local $rc i32)
|
||||
(if (i32.eqz (i32.load offset=80 (local.get $r))) (then
|
||||
(i32.store offset=80 (local.get $r) (i32.const 1))
|
||||
(i32.store offset=84 (local.get $r) (call $do_request (local.get $r)))))
|
||||
(local.set $rc (i32.load offset=84 (local.get $r)))
|
||||
(i32.store8 (local.get $rp) (i32.const 1))
|
||||
(i32.store8 offset=8 (local.get $rp) (i32.const 0))
|
||||
(if (i32.lt_s (local.get $rc) (i32.const 0))
|
||||
(then (i32.store8 offset=16 (local.get $rp) (i32.const 1)) (i32.store8 offset=24 (local.get $rp) (i32.sub (i32.const 0) (local.get $rc))))
|
||||
(else (i32.store8 offset=16 (local.get $rp) (i32.const 0)) (i32.store offset=24 (local.get $rp) (i32.load offset=76 (local.get $r))))))
|
||||
|
||||
;; resp object: status@0 hdrs@4 bodyptr@8 bodylen@12 pos@16
|
||||
(func $resp_status (param $r i32) (result i32) (i32.load (local.get $r)))
|
||||
(func $resp_headers (param $r i32) (result i32) (i32.load offset=4 (local.get $r)))
|
||||
(func $resp_consume (param $r i32) (param $rp i32) (i32.store8 (local.get $rp) (i32.const 0)) (i32.store offset=4 (local.get $rp) (local.get $r)))
|
||||
(func $body_stream (param $r i32) (param $rp i32) (i32.store8 (local.get $rp) (i32.const 0)) (i32.store offset=4 (local.get $rp) (local.get $r)))
|
||||
(func $in_blocking_read (param $r i32) (param $max i64) (param $rp i32) (local $left i32) (local $n i32) (local $w i64)
|
||||
(if (i32.lt_u (local.get $r) (i32.const 12288)) (then
|
||||
(if (i32.ge_u (local.get $r) (i32.const 4096)) (then (local.set $r (i32.sub (local.get $r) (i32.const 4096)))))
|
||||
(if (i64.gt_u (local.get $max) (i64.const 65536)) (then (local.set $max (i64.const 65536))))
|
||||
(local.set $w (call $sys (i64.const 0) (i64.extend_i32_u (local.get $r)) (call $hp (i32.const 0x02FC0000)) (local.get $max)))
|
||||
(if (i64.le_s (local.get $w) (i64.const 0)) (then
|
||||
(i32.store8 (local.get $rp) (i32.const 1)) (i32.store offset=4 (local.get $rp) (i32.const 1)) (return)))
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (i32.const 0x02FC0000))
|
||||
(i32.store offset=8 (local.get $rp) (i32.wrap_i64 (local.get $w)))
|
||||
(return)))
|
||||
(local.set $left (i32.sub (i32.load offset=12 (local.get $r)) (i32.load offset=16 (local.get $r))))
|
||||
(if (i32.eqz (local.get $left)) (then
|
||||
(i32.store8 (local.get $rp) (i32.const 1))
|
||||
(i32.store offset=4 (local.get $rp) (i32.const 1)) ;; stream-error::closed
|
||||
(return)))
|
||||
(local.set $n (local.get $left))
|
||||
(if (i64.lt_u (local.get $max) (i64.extend_i32_u (local.get $n))) (then (local.set $n (i32.wrap_i64 (local.get $max)))))
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (i32.add (i32.load offset=8 (local.get $r)) (i32.load offset=16 (local.get $r))))
|
||||
(i32.store offset=8 (local.get $rp) (local.get $n))
|
||||
(i32.store offset=16 (local.get $r) (i32.add (i32.load offset=16 (local.get $r)) (local.get $n))))
|
||||
|
||||
;; ---- file system: unsupported (returns err) ----
|
||||
(func $in_dummy)
|
||||
|
||||
;; drops: no-ops
|
||||
(func $drop_io_error (param i32)) (func $drop_pollable (param i32)) (func $drop_fields (param i32))
|
||||
(func $drop_outgoing_body (param i32)) (func $drop_outgoing_request (param i32)) (func $drop_request_options (param i32))
|
||||
(func $drop_incoming_body (param i32)) (func $drop_incoming_response (param i32)) (func $drop_fir (param i32))
|
||||
|
||||
;; ---- literals @0x0000E0000 ----
|
||||
(data (i32.const 0x0000E0000) "GET\00\00\00\00\00HEAD\00\00\00\00POST\00\00\00\00PUT\00\00\00\00\00DELETE\00\00CONNECT\00OPTIONS\00TRACE\00\00\00PATCH\00\00\00")
|
||||
(data (i32.const 0x0000E0100) " HTTP/1.0\0d\0aHost: ")
|
||||
(data (i32.const 0x0000E0120) "Connection: close\0d\0a\0d\0a")
|
||||
(data (i32.const 0x0000E0140) "Content-Length: ")
|
||||
(data (i32.const 0x0000E0160) "/etc/resolv.conf\00")
|
||||
(data (i32.const 0x0000E0180) "nameserver ")
|
||||
(data (i32.const 0x0000E0190) "content-length")
|
||||
(data (i32.const 0x0000E01A0) "localhost")
|
||||
|
||||
(func $app (param $d i32) (param $p i32) (param $l i32) (result i32)
|
||||
(memory.copy (local.get $d) (local.get $p) (local.get $l)) (i32.add (local.get $d) (local.get $l)))
|
||||
(func $appnum (param $d i32) (param $n i32) (result i32) (local $t i32) (local $k i32) (local $div i32)
|
||||
(local.set $div (i32.const 1))
|
||||
(loop $l (if (i32.ge_u (i32.div_u (local.get $n) (local.get $div)) (i32.const 10)) (then (local.set $div (i32.mul (local.get $div) (i32.const 10))) (br $l))))
|
||||
(loop $m
|
||||
(i32.store8 (local.get $d) (i32.add (i32.const 48) (i32.rem_u (i32.div_u (local.get $n) (local.get $div)) (i32.const 10))))
|
||||
(local.set $d (i32.add (local.get $d) (i32.const 1)))
|
||||
(local.set $div (i32.div_u (local.get $div) (i32.const 10)))
|
||||
(br_if $m (local.get $div)))
|
||||
(local.get $d))
|
||||
|
||||
;; parse dotted quad p,l -> ip (bytes a.b.c.d little-endian in memory order) or 0
|
||||
(func $parse_ip (param $p i32) (param $l i32) (result i32) (local $i i32) (local $c i32) (local $v i32) (local $oct i32) (local $ip i32) (local $dig i32)
|
||||
(loop $l
|
||||
(if (i32.lt_u (local.get $i) (local.get $l)) (then
|
||||
(local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $i))))
|
||||
(if (i32.and (i32.ge_u (local.get $c) (i32.const 48)) (i32.le_u (local.get $c) (i32.const 57)))
|
||||
(then (local.set $v (i32.add (i32.mul (local.get $v) (i32.const 10)) (i32.sub (local.get $c) (i32.const 48))))
|
||||
(local.set $dig (i32.const 1))
|
||||
(if (i32.gt_u (local.get $v) (i32.const 255)) (then (return (i32.const 0)))))
|
||||
(else
|
||||
(if (i32.or (i32.ne (local.get $c) (i32.const 46)) (i32.eqz (local.get $dig))) (then (return (i32.const 0))))
|
||||
(local.set $ip (i32.or (local.get $ip) (i32.shl (local.get $v) (i32.mul (local.get $oct) (i32.const 8)))))
|
||||
(local.set $oct (i32.add (local.get $oct) (i32.const 1))) (local.set $v (i32.const 0)) (local.set $dig (i32.const 0))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $l))))
|
||||
(if (i32.or (i32.ne (local.get $oct) (i32.const 3)) (i32.eqz (local.get $dig))) (then (return (i32.const 0))))
|
||||
(i32.or (local.get $ip) (i32.shl (local.get $v) (i32.const 24))))
|
||||
|
||||
;; sockaddr_in at scratch 0x01FF0000: family(2) port(BE) ip
|
||||
(func $mk_sa (param $ip i32) (param $port i32)
|
||||
(i64.store (i32.const 0x01FF0000) (i64.const 0)) (i64.store (i32.const 0x01FF0008) (i64.const 0))
|
||||
(i32.store16 (i32.const 0x01FF0000) (i32.const 2))
|
||||
(i32.store8 (i32.const 0x01FF0002) (i32.shr_u (local.get $port) (i32.const 8)))
|
||||
(i32.store8 (i32.const 0x01FF0003) (local.get $port))
|
||||
(i32.store (i32.const 0x01FF0004) (local.get $ip)))
|
||||
|
||||
;; resolve via /etc/resolv.conf nameserver (default 127.0.0.53). returns ip or 0
|
||||
(func $dns (param $hp i32) (param $hl i32) (result i32)
|
||||
(local $fd i64) (local $ns i32) (local $n i32) (local $q i32) (local $i i32) (local $c i32) (local $lab i32) (local $len i32)
|
||||
(local $an i32) (local $p i32) (local $t i32) (local $rd i32) (local $r i64)
|
||||
(local.set $ns (i32.const 0x3500007F))
|
||||
;; read resolv.conf
|
||||
(local.set $fd (call $sys (i64.const 2) (call $hp (i32.const 0x0000E0160)) (i64.const 0) (i64.const 0)))
|
||||
(if (i64.ge_s (local.get $fd) (i64.const 0)) (then
|
||||
(local.set $n (i32.wrap_i64 (call $sys (i64.const 0) (local.get $fd) (call $hp (i32.const 0x01FF1400)) (i64.const 4000))))
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0)))
|
||||
(if (i32.gt_s (local.get $n) (i32.const 0)) (then
|
||||
(i32.store8 (i32.add (i32.const 0x01FF1400) (local.get $n)) (i32.const 0))
|
||||
(local.set $i (i32.const 0))
|
||||
(loop $scan
|
||||
(if (i32.lt_u (local.get $i) (local.get $n)) (then
|
||||
(if (call $ieq (i32.add (i32.const 0x01FF1400) (local.get $i)) (i32.const 11) (i32.const 0x0000E0180) (i32.const 11)) (then
|
||||
(local.set $p (i32.add (i32.add (i32.const 0x01FF1400) (local.get $i)) (i32.const 11)))
|
||||
(local.set $len (i32.const 0))
|
||||
(loop $e (if (i32.gt_u (i32.load8_u (i32.add (local.get $p) (local.get $len))) (i32.const 32)) (then (local.set $len (i32.add (local.get $len) (i32.const 1))) (br $e))))
|
||||
(local.set $q (call $parse_ip (local.get $p) (local.get $len)))
|
||||
(if (local.get $q) (then (local.set $ns (local.get $q)) (local.set $i (local.get $n))))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $scan))))))))
|
||||
;; build query at 0x01FF0100
|
||||
(local.set $q (i32.const 0x01FF0100))
|
||||
(i32.store8 (local.get $q) (i32.const 0x12)) (i32.store8 offset=1 (local.get $q) (i32.const 0x34))
|
||||
(i32.store8 offset=2 (local.get $q) (i32.const 1)) (i32.store8 offset=3 (local.get $q) (i32.const 0))
|
||||
(i32.store8 offset=4 (local.get $q) (i32.const 0)) (i32.store8 offset=5 (local.get $q) (i32.const 1))
|
||||
(i64.store offset=6 (local.get $q) (i64.const 0)) ;; an,ns,ar = 0 (bytes 6..11 + 2 extra)
|
||||
(i32.store16 offset=12 (local.get $q) (i32.const 0))
|
||||
(local.set $p (i32.add (local.get $q) (i32.const 12)))
|
||||
;; labels
|
||||
(local.set $i (i32.const 0))
|
||||
(loop $lbl
|
||||
(local.set $lab (local.get $p))
|
||||
(local.set $p (i32.add (local.get $p) (i32.const 1)))
|
||||
(local.set $len (i32.const 0))
|
||||
(loop $ch
|
||||
(if (i32.lt_u (local.get $i) (local.get $hl)) (then
|
||||
(local.set $c (i32.load8_u (i32.add (local.get $hp) (local.get $i))))
|
||||
(if (i32.ne (local.get $c) (i32.const 46)) (then
|
||||
(i32.store8 (local.get $p) (local.get $c))
|
||||
(local.set $p (i32.add (local.get $p) (i32.const 1)))
|
||||
(local.set $len (i32.add (local.get $len) (i32.const 1)))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $ch))))))
|
||||
(i32.store8 (local.get $lab) (local.get $len))
|
||||
(if (i32.lt_u (local.get $i) (local.get $hl)) (then (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $lbl))))
|
||||
(i32.store8 (local.get $p) (i32.const 0))
|
||||
(i32.store8 offset=1 (local.get $p) (i32.const 0)) (i32.store8 offset=2 (local.get $p) (i32.const 1))
|
||||
(i32.store8 offset=3 (local.get $p) (i32.const 0)) (i32.store8 offset=4 (local.get $p) (i32.const 1))
|
||||
(local.set $len (i32.sub (i32.add (local.get $p) (i32.const 5)) (local.get $q)))
|
||||
;; udp socket, connect, send
|
||||
(local.set $fd (call $sys (i64.const 41) (i64.const 2) (i64.const 2) (i64.const 0)))
|
||||
(if (i64.lt_s (local.get $fd) (i64.const 0)) (then (return (i32.const 0))))
|
||||
(call $mk_sa (local.get $ns) (i32.const 53))
|
||||
(if (i64.lt_s (call $sys (i64.const 42) (local.get $fd) (call $hp (i32.const 0x01FF0000)) (i64.const 16)) (i64.const 0)) (then
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0))) (return (i32.const 0))))
|
||||
(drop (call $sys (i64.const 1) (local.get $fd) (call $hp (local.get $q)) (i64.extend_i32_u (local.get $len))))
|
||||
;; poll 3s
|
||||
(i64.store32 (i32.const 0x01FF00F0) (local.get $fd))
|
||||
(i32.store16 (i32.const 0x01FF00F4) (i32.const 1))
|
||||
(i32.store16 (i32.const 0x01FF00F6) (i32.const 0))
|
||||
(local.set $r (call $sys (i64.const 7) (call $hp (i32.const 0x01FF00F0)) (i64.const 1) (i64.const 3000)))
|
||||
(if (i64.le_s (local.get $r) (i64.const 0)) (then
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0))) (return (i32.const 0))))
|
||||
(local.set $n (i32.wrap_i64 (call $sys (i64.const 0) (local.get $fd) (call $hp (i32.const 0x01FF0400)) (i64.const 4000))))
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0)))
|
||||
(if (i32.lt_s (local.get $n) (i32.const 12)) (then (return (i32.const 0))))
|
||||
(local.set $an (i32.or (i32.shl (i32.load8_u (i32.const 0x01FF0406)) (i32.const 8)) (i32.load8_u (i32.const 0x01FF0407))))
|
||||
;; skip question
|
||||
(local.set $p (i32.const 0x01FF040C))
|
||||
(loop $sk
|
||||
(local.set $c (i32.load8_u (local.get $p)))
|
||||
(local.set $p (i32.add (local.get $p) (i32.const 1)))
|
||||
(if (local.get $c) (then (local.set $p (i32.add (local.get $p) (local.get $c))) (br $sk))))
|
||||
(local.set $p (i32.add (local.get $p) (i32.const 4)))
|
||||
(loop $ans
|
||||
(if (i32.and (i32.ne (local.get $an) (i32.const 0)) (i32.lt_u (local.get $p) (i32.add (i32.const 0x01FF0400) (local.get $n)))) (then
|
||||
;; name
|
||||
(loop $nm
|
||||
(local.set $c (i32.load8_u (local.get $p)))
|
||||
(if (i32.eq (i32.and (local.get $c) (i32.const 0xC0)) (i32.const 0xC0))
|
||||
(then (local.set $p (i32.add (local.get $p) (i32.const 2))))
|
||||
(else (local.set $p (i32.add (local.get $p) (i32.const 1)))
|
||||
(if (local.get $c) (then (local.set $p (i32.add (local.get $p) (local.get $c))) (br $nm))))))
|
||||
(local.set $t (i32.or (i32.shl (i32.load8_u (local.get $p)) (i32.const 8)) (i32.load8_u offset=1 (local.get $p))))
|
||||
(local.set $rd (i32.or (i32.shl (i32.load8_u offset=8 (local.get $p)) (i32.const 8)) (i32.load8_u offset=9 (local.get $p))))
|
||||
(if (i32.and (i32.eq (local.get $t) (i32.const 1)) (i32.eq (local.get $rd) (i32.const 4))) (then
|
||||
(return (i32.load offset=10 (local.get $p)))))
|
||||
(local.set $p (i32.add (local.get $p) (i32.add (i32.const 10) (local.get $rd))))
|
||||
(local.set $an (i32.sub (local.get $an) (i32.const 1)))
|
||||
(br $ans))))
|
||||
(i32.const 0))
|
||||
|
||||
;; ---- do_request: returns 0 (resp stored @76) or negative error-code tag ----
|
||||
(func $do_request (param $r i32) (result i32)
|
||||
(local $ap i32) (local $al i32) (local $i i32) (local $hl i32) (local $port i32) (local $ip i32) (local $fd i64)
|
||||
(local $buf i32) (local $d i32) (local $h i32) (local $n i32) (local $e i32) (local $off i32) (local $w i64)
|
||||
(local $rb i32) (local $rl i32) (local $hs i32) (local $st i32) (local $resp i32) (local $ls i32) (local $le i32)
|
||||
(local $colon i32) (local $f i32) (local $cl i32) (local $c i32) (local $tag i32)
|
||||
(if (i32.ne (i32.load offset=16 (local.get $r)) (i32.const 0)) (then (return (i32.const -12)))) ;; https: TLS unsupported in spike
|
||||
(local.set $ap (i32.load offset=20 (local.get $r))) (local.set $al (i32.load offset=24 (local.get $r)))
|
||||
(local.set $hl (local.get $al)) (local.set $port (i32.const 80))
|
||||
(local.set $i (i32.const 0))
|
||||
(loop $f (if (i32.lt_u (local.get $i) (local.get $al)) (then
|
||||
(if (i32.eq (i32.load8_u (i32.add (local.get $ap) (local.get $i))) (i32.const 58)) (then
|
||||
(local.set $hl (local.get $i)) (local.set $port (i32.const 0))
|
||||
(local.set $c (i32.add (local.get $i) (i32.const 1)))
|
||||
(loop $pp (if (i32.lt_u (local.get $c) (local.get $al)) (then
|
||||
(local.set $port (i32.add (i32.mul (local.get $port) (i32.const 10)) (i32.sub (i32.load8_u (i32.add (local.get $ap) (local.get $c))) (i32.const 48))))
|
||||
(local.set $c (i32.add (local.get $c) (i32.const 1))) (br $pp))))
|
||||
(local.set $i (local.get $al))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $f))))
|
||||
(local.set $ip (call $parse_ip (local.get $ap) (local.get $hl)))
|
||||
(if (i32.eqz (local.get $ip)) (then
|
||||
(if (call $ieq (local.get $ap) (local.get $hl) (i32.const 0x0000E01A0) (i32.const 9)) (then (local.set $ip (i32.const 0x0100007F)))
|
||||
(else (local.set $ip (call $dns (local.get $ap) (local.get $hl)))))))
|
||||
(if (i32.eqz (local.get $ip)) (then (return (i32.const -1))))
|
||||
;; build request
|
||||
(local.set $buf (i32.const 0x02F00000))
|
||||
(local.set $d (local.get $buf))
|
||||
(local.set $tag (i32.load offset=4 (local.get $r)))
|
||||
(if (i32.lt_u (local.get $tag) (i32.const 9))
|
||||
(then (local.set $d (call $app (local.get $d) (i32.add (i32.const 0x0000E0000) (i32.mul (local.get $tag) (i32.const 8))) (i32.const 0)))
|
||||
(local.set $n (i32.const 0))
|
||||
(loop $sl (if (i32.load8_u (i32.add (i32.add (i32.const 0x0000E0000) (i32.mul (local.get $tag) (i32.const 8))) (local.get $n))) (then (local.set $n (i32.add (local.get $n) (i32.const 1))) (br $sl))))
|
||||
(local.set $d (call $app (local.get $d) (i32.add (i32.const 0x0000E0000) (i32.mul (local.get $tag) (i32.const 8))) (local.get $n))))
|
||||
(else (local.set $d (call $app (local.get $d) (i32.load offset=8 (local.get $r)) (i32.load offset=12 (local.get $r))))))
|
||||
(i32.store8 (local.get $d) (i32.const 32)) (local.set $d (i32.add (local.get $d) (i32.const 1)))
|
||||
(local.set $d (call $app (local.get $d) (i32.load offset=28 (local.get $r)) (i32.load offset=32 (local.get $r))))
|
||||
(local.set $d (call $app (local.get $d) (i32.const 0x0000E0100) (i32.const 17)))
|
||||
(local.set $d (call $app (local.get $d) (local.get $ap) (local.get $al)))
|
||||
(i32.store8 (local.get $d) (i32.const 13)) (i32.store8 offset=1 (local.get $d) (i32.const 10)) (local.set $d (i32.add (local.get $d) (i32.const 2)))
|
||||
(local.set $h (i32.load (local.get $r)))
|
||||
(local.set $cl (i32.const 0))
|
||||
(local.set $i (i32.const 0))
|
||||
(loop $hh (if (i32.lt_u (local.get $i) (i32.load (local.get $h))) (then
|
||||
(local.set $e (i32.add (i32.add (local.get $h) (i32.const 8)) (i32.mul (local.get $i) (i32.const 16))))
|
||||
(if (call $ieq (i32.load (local.get $e)) (i32.load offset=4 (local.get $e)) (i32.const 0x0000E0190) (i32.const 14)) (then (local.set $cl (i32.const 1))))
|
||||
(local.set $d (call $app (local.get $d) (i32.load (local.get $e)) (i32.load offset=4 (local.get $e))))
|
||||
(i32.store8 (local.get $d) (i32.const 58)) (i32.store8 offset=1 (local.get $d) (i32.const 32)) (local.set $d (i32.add (local.get $d) (i32.const 2)))
|
||||
(local.set $d (call $app (local.get $d) (i32.load offset=8 (local.get $e)) (i32.load offset=12 (local.get $e))))
|
||||
(i32.store8 (local.get $d) (i32.const 13)) (i32.store8 offset=1 (local.get $d) (i32.const 10)) (local.set $d (i32.add (local.get $d) (i32.const 2)))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $hh))))
|
||||
(if (i32.and (i32.eqz (local.get $cl)) (i32.ne (i32.load offset=68 (local.get $r)) (i32.const 0))) (then
|
||||
(local.set $d (call $app (local.get $d) (i32.const 0x0000E0140) (i32.const 16)))
|
||||
(local.set $d (call $appnum (local.get $d) (i32.load offset=68 (local.get $r))))
|
||||
(i32.store8 (local.get $d) (i32.const 13)) (i32.store8 offset=1 (local.get $d) (i32.const 10)) (local.set $d (i32.add (local.get $d) (i32.const 2)))))
|
||||
(local.set $d (call $app (local.get $d) (i32.const 0x0000E0120) (i32.const 21)))
|
||||
;; connect
|
||||
(local.set $fd (call $sys (i64.const 41) (i64.const 2) (i64.const 1) (i64.const 0)))
|
||||
(if (i64.lt_s (local.get $fd) (i64.const 0)) (then (return (i32.const -3))))
|
||||
(call $mk_sa (local.get $ip) (local.get $port))
|
||||
(if (i64.lt_s (call $sys (i64.const 42) (local.get $fd) (call $hp (i32.const 0x01FF0000)) (i64.const 16)) (i64.const 0)) (then
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0))) (return (i32.const -6))))
|
||||
;; send all
|
||||
(local.set $off (local.get $buf))
|
||||
(loop $snd (if (i32.lt_u (local.get $off) (local.get $d)) (then
|
||||
(local.set $w (call $sys (i64.const 1) (local.get $fd) (call $hp (local.get $off)) (i64.extend_i32_u (i32.sub (local.get $d) (local.get $off)))))
|
||||
(if (i64.le_s (local.get $w) (i64.const 0)) (then (drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0))) (return (i32.const -7))))
|
||||
(local.set $off (i32.add (local.get $off) (i32.wrap_i64 (local.get $w)))) (br $snd))))
|
||||
(local.set $off (i32.load offset=64 (local.get $r)))
|
||||
(local.set $d (i32.add (local.get $off) (i32.load offset=68 (local.get $r))))
|
||||
(loop $snd2 (if (i32.lt_u (local.get $off) (local.get $d)) (then
|
||||
(local.set $w (call $sys (i64.const 1) (local.get $fd) (call $hp (local.get $off)) (i64.extend_i32_u (i32.sub (local.get $d) (local.get $off)))))
|
||||
(if (i64.le_s (local.get $w) (i64.const 0)) (then (drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0))) (return (i32.const -7))))
|
||||
(local.set $off (i32.add (local.get $off) (i32.wrap_i64 (local.get $w)))) (br $snd2))))
|
||||
;; receive until EOF
|
||||
(local.set $rb (i32.const 0x03000000))
|
||||
(local.set $rl (i32.const 0))
|
||||
(loop $rcv
|
||||
(local.set $w (call $sys (i64.const 0) (local.get $fd) (call $hp (i32.add (local.get $rb) (local.get $rl))) (i64.extend_i32_u (i32.sub (i32.const 0xFFFFFF) (local.get $rl)))))
|
||||
(if (i64.gt_s (local.get $w) (i64.const 0)) (then
|
||||
(local.set $rl (i32.add (local.get $rl) (i32.wrap_i64 (local.get $w)))) (br $rcv))))
|
||||
(drop (call $sys (i64.const 3) (local.get $fd) (i64.const 0) (i64.const 0)))
|
||||
;; parse head
|
||||
(local.set $hs (i32.const -1)) (local.set $i (i32.const 0))
|
||||
(loop $fh (if (i32.lt_u (i32.add (local.get $i) (i32.const 3)) (local.get $rl)) (then
|
||||
(if (i32.eq (i32.load (i32.add (local.get $rb) (local.get $i))) (i32.const 0x0A0D0A0D)) (then (local.set $hs (local.get $i)) (local.set $i (local.get $rl))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $fh))))
|
||||
(if (i32.lt_s (local.get $hs) (i32.const 0)) (then (return (i32.const -24)))) ;; HTTP-response-incomplete
|
||||
(local.set $resp (call $sbrk (i32.const 32)))
|
||||
;; status: "HTTP/1.x NNN"
|
||||
(local.set $st (i32.const 0)) (local.set $i (i32.const 9))
|
||||
(loop $sd (if (i32.lt_u (local.get $i) (i32.const 12)) (then
|
||||
(local.set $st (i32.add (i32.mul (local.get $st) (i32.const 10)) (i32.sub (i32.load8_u (i32.add (local.get $rb) (local.get $i))) (i32.const 48))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1))) (br $sd))))
|
||||
(i32.store (local.get $resp) (local.get $st))
|
||||
(local.set $f (call $new_fields))
|
||||
(i32.store offset=4 (local.get $resp) (local.get $f))
|
||||
;; header lines from first CRLF
|
||||
(local.set $ls (i32.const 0))
|
||||
(loop $ln1 (if (i32.ne (i32.load16_u (i32.add (local.get $rb) (local.get $ls))) (i32.const 0x0A0D)) (then (local.set $ls (i32.add (local.get $ls) (i32.const 1))) (br $ln1))))
|
||||
(local.set $ls (i32.add (local.get $ls) (i32.const 2)))
|
||||
(loop $lines (if (i32.lt_u (local.get $ls) (i32.add (local.get $hs) (i32.const 2))) (then
|
||||
(local.set $le (local.get $ls))
|
||||
(loop $eol (if (i32.ne (i32.load16_u (i32.add (local.get $rb) (local.get $le))) (i32.const 0x0A0D)) (then (local.set $le (i32.add (local.get $le) (i32.const 1))) (br $eol))))
|
||||
(local.set $colon (local.get $ls))
|
||||
(loop $cc (if (i32.and (i32.lt_u (local.get $colon) (local.get $le)) (i32.ne (i32.load8_u (i32.add (local.get $rb) (local.get $colon))) (i32.const 58))) (then (local.set $colon (i32.add (local.get $colon) (i32.const 1))) (br $cc))))
|
||||
(if (i32.lt_u (local.get $colon) (local.get $le)) (then
|
||||
(local.set $c (i32.add (local.get $colon) (i32.const 1)))
|
||||
(loop $ws (if (i32.and (i32.lt_u (local.get $c) (local.get $le)) (i32.eq (i32.load8_u (i32.add (local.get $rb) (local.get $c))) (i32.const 32))) (then (local.set $c (i32.add (local.get $c) (i32.const 1))) (br $ws))))
|
||||
(call $fields_append (local.get $f) (i32.add (local.get $rb) (local.get $ls)) (i32.sub (local.get $colon) (local.get $ls))
|
||||
(i32.add (local.get $rb) (local.get $c)) (i32.sub (local.get $le) (local.get $c)) (i32.const 0x01FF0010))))
|
||||
(local.set $ls (i32.add (local.get $le) (i32.const 2)))
|
||||
(br $lines))))
|
||||
(i32.store offset=8 (local.get $resp) (i32.add (local.get $rb) (i32.add (local.get $hs) (i32.const 4))))
|
||||
(i32.store offset=12 (local.get $resp) (i32.sub (local.get $rl) (i32.add (local.get $hs) (i32.const 4))))
|
||||
(i32.store offset=76 (local.get $r) (local.get $resp))
|
||||
(i32.const 0))
|
||||
|
||||
;; ---- filesystem: one preopen "." (handle 1000 = AT_FDCWD); other descriptors are real fds ----
|
||||
;; input stream handles: fd+4096 ; output stream handles: fd+8192
|
||||
(data (i32.const 0x0000E1B0) ".\00")
|
||||
(func $get_directories (param $rp i32) (local $a i32)
|
||||
(local.set $a (call $sbrk (i32.const 16)))
|
||||
(i32.store (local.get $a) (i32.const 1000))
|
||||
(i32.store offset=4 (local.get $a) (i32.const 0x0000E1B0))
|
||||
(i32.store offset=8 (local.get $a) (i32.const 1))
|
||||
(i32.store (local.get $rp) (local.get $a))
|
||||
(i32.store offset=4 (local.get $rp) (i32.const 1)))
|
||||
(func $errno_code (param $e i64) (result i32)
|
||||
(if (i64.eq (local.get $e) (i64.const -2)) (then (return (i32.const 20))))
|
||||
(if (i64.eq (local.get $e) (i64.const -13)) (then (return (i32.const 0))))
|
||||
(if (i64.eq (local.get $e) (i64.const -17)) (then (return (i32.const 7))))
|
||||
(if (i64.eq (local.get $e) (i64.const -21)) (then (return (i32.const 14))))
|
||||
(i32.const 13))
|
||||
(func $desc_open_at (param $self i32) (param $pf i32) (param $pp i32) (param $pl i32) (param $of i32) (param $df i32) (param $rp i32)
|
||||
(local $fl i64) (local $path i32) (local $r i64) (local $dirfd i64)
|
||||
(local.set $path (call $dup (local.get $pp) (local.get $pl)))
|
||||
(if (i32.and (local.get $df) (i32.const 2)) (then (local.set $fl (i64.const 1))))
|
||||
(if (i32.and (local.get $df) (i32.const 1)) (then (local.set $fl (select (i64.const 2) (i64.const 0) (i64.eqz (local.get $fl))))))
|
||||
(if (i32.and (local.get $df) (i32.const 1)) (then (if (i32.and (local.get $df) (i32.const 2)) (then (local.set $fl (i64.const 2))) (else (local.set $fl (i64.const 0))))))
|
||||
(if (i32.and (local.get $of) (i32.const 1)) (then (local.set $fl (i64.or (local.get $fl) (i64.const 0x40)))))
|
||||
(if (i32.and (local.get $of) (i32.const 4)) (then (local.set $fl (i64.or (local.get $fl) (i64.const 0x80)))))
|
||||
(if (i32.and (local.get $of) (i32.const 8)) (then (local.set $fl (i64.or (local.get $fl) (i64.const 0x200)))))
|
||||
(if (i32.and (local.get $of) (i32.const 2)) (then (local.set $fl (i64.or (local.get $fl) (i64.const 0x10000)))))
|
||||
(local.set $dirfd (select (i64.const -100) (i64.extend_i32_u (local.get $self)) (i32.eq (local.get $self) (i32.const 1000))))
|
||||
(local.set $r (call $syscall (i64.const 257) (local.get $dirfd) (call $hp (local.get $path)) (local.get $fl) (i64.const 420) (i64.const 0) (i64.const 0)))
|
||||
(if (i64.lt_s (local.get $r) (i64.const 0))
|
||||
(then (i32.store8 (local.get $rp) (i32.const 1)) (i32.store8 offset=4 (local.get $rp) (call $errno_code (local.get $r))))
|
||||
(else (i32.store8 (local.get $rp) (i32.const 0)) (i32.store offset=4 (local.get $rp) (i32.wrap_i64 (local.get $r))))))
|
||||
(func $desc_read_via_stream (param $d i32) (param $off i64) (param $rp i32) (local $fd i64)
|
||||
(local.set $fd (call $sys (i64.const 32) (i64.extend_i32_u (local.get $d)) (i64.const 0) (i64.const 0)))
|
||||
(if (i64.gt_u (local.get $off) (i64.const 0)) (then (drop (call $sys (i64.const 8) (local.get $fd) (local.get $off) (i64.const 0)))))
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (i32.add (i32.wrap_i64 (local.get $fd)) (i32.const 4096))))
|
||||
(func $desc_write_via_stream (param $d i32) (param $off i64) (param $rp i32) (local $fd i64)
|
||||
(local.set $fd (call $sys (i64.const 32) (i64.extend_i32_u (local.get $d)) (i64.const 0) (i64.const 0)))
|
||||
(i32.store8 (local.get $rp) (i32.const 0))
|
||||
(i32.store offset=4 (local.get $rp) (i32.add (i32.wrap_i64 (local.get $fd)) (i32.const 8192))))
|
||||
(func $drop_descriptor (param $h i32)
|
||||
(if (i32.lt_u (local.get $h) (i32.const 1000)) (then (drop (call $sys (i64.const 3) (i64.extend_i32_u (local.get $h)) (i64.const 0) (i64.const 0))))))
|
||||
(func $drop_input_stream (param $h i32)
|
||||
(if (i32.and (i32.ge_u (local.get $h) (i32.const 4096)) (i32.lt_u (local.get $h) (i32.const 8192)))
|
||||
(then (drop (call $sys (i64.const 3) (i64.extend_i32_u (i32.sub (local.get $h) (i32.const 4096))) (i64.const 0) (i64.const 0))))))
|
||||
(func $drop_output_stream (param $h i32)
|
||||
(if (i32.and (i32.ge_u (local.get $h) (i32.const 8192)) (i32.lt_u (local.get $h) (i32.const 12288)))
|
||||
(then (drop (call $sys (i64.const 3) (i64.extend_i32_u (i32.sub (local.get $h) (i32.const 8192))) (i64.const 0) (i64.const 0))))))
|
||||
@@ -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 {
|
||||
@@ -0,0 +1 @@
|
||||
987cdf256bc7e298072c51f5078eec28500b7fae
|
||||
@@ -16,6 +16,25 @@ test: build
|
||||
test-native: native
|
||||
CURL_WASM_NATIVE=./curl-native python3 tests/test_curl.py
|
||||
|
||||
# Spike: static x86-64 ELF via wasm2elf (no libc/runtime; plain HTTP only, no TLS yet)
|
||||
elf:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
W=build/wasm2elf
|
||||
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
|
||||
wasm-tools parse build/curl-flat.wat -o build/curl-flat.wasm
|
||||
wasm-tools validate build/curl-flat.wasm
|
||||
$W/_build/native/debug/build/cmd/main/main.exe build/curl-flat.wasm curl-elf run
|
||||
|
||||
test-elf: elf
|
||||
CURL_WASM_NATIVE=./curl-elf python3 tests/test_curl.py
|
||||
|
||||
run *args: build
|
||||
wasmtime run -S http --dir=. -- ./curl.wasm "$@"
|
||||
|
||||
@@ -23,4 +42,4 @@ run-native *args: native
|
||||
./curl-native "$@"
|
||||
|
||||
clean:
|
||||
rm -rf curl.wasm curl-native build
|
||||
rm -rf curl.wasm curl-native curl-elf build
|
||||
Reference in new issue
Block a user