;; ---- 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))))))