Add wasm2c native compilation target and host runtime
Add native/extract_core.py to extract embedded core Wasm modules from curl.wasm and native/wasi_host.c providing a POSIX/OpenSSL WASI 0.2 host shim for wasm2c. Add native, test-native, and run-native recipes to justfile (with positional-arguments enabled), support CURL_WASM_NATIVE in tests/test_curl.py, ignore build artifacts in .gitignore, and document native compilation in README.md.
This commit is contained in:
1 parent
e04081daa0
commit
2ff249ff89
6 files changed
+1918
-3
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
curl.wasm
|
curl.wasm
|
||||||
|
curl-native
|
||||||
|
build/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
@@ -27,6 +27,16 @@ Run the test suite:
|
|||||||
make test
|
make test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Compile to Native (`wasm2c`)
|
||||||
|
|
||||||
|
You can also compile `curl.wasm` into a standalone native binary (`./curl-native`, backed by POSIX sockets and OpenSSL) using `wasm2c` via `just`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
just native
|
||||||
|
just run-native -fsSI https://example.com
|
||||||
|
just test-native
|
||||||
|
```
|
||||||
|
|
||||||
## Supported Options
|
## Supported Options
|
||||||
|
|
||||||
| Option | Description |
|
| Option | Description |
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
set positional-arguments
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@just --list
|
@just --list
|
||||||
|
|
||||||
@@ -5,11 +7,27 @@ build:
|
|||||||
wasm-tools parse curl.wat -o curl.wasm
|
wasm-tools parse curl.wat -o curl.wasm
|
||||||
wasm-tools validate curl.wasm --features component-model
|
wasm-tools validate curl.wasm --features component-model
|
||||||
|
|
||||||
|
native: build
|
||||||
|
mkdir -p build
|
||||||
|
python3 native/extract_core.py curl.wasm build
|
||||||
|
wasm2c build/mem_mod.wasm -n mem_mod -o build/mem_mod.c
|
||||||
|
wasm2c build/curl_core.wasm -n curl_core -o build/curl_core.c
|
||||||
|
cc -O2 -w -Ibuild -c build/mem_mod.c -o build/mem_mod.o
|
||||||
|
cc -O2 -w -Ibuild -c build/curl_core.c -o build/curl_core.o
|
||||||
|
cc -O2 -Wall -Wextra -Werror -Wno-unused-parameter -Ibuild -c native/wasi_host.c -o build/wasi_host.o
|
||||||
|
cc -O2 build/mem_mod.o build/curl_core.o build/wasi_host.o -lwasm-rt-impl -lssl -lcrypto -lm -o curl-native
|
||||||
|
|
||||||
test: build
|
test: build
|
||||||
python3 tests/test_curl.py
|
python3 tests/test_curl.py
|
||||||
|
|
||||||
|
test-native: native
|
||||||
|
CURL_WASM_NATIVE=./curl-native python3 tests/test_curl.py
|
||||||
|
|
||||||
run *args: build
|
run *args: build
|
||||||
wasmtime run -S http --dir=. -- ./curl.wasm {{args}}
|
wasmtime run -S http --dir=. -- ./curl.wasm "$@"
|
||||||
|
|
||||||
|
run-native *args: native
|
||||||
|
./curl-native "$@"
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f curl.wasm
|
rm -rf curl.wasm curl-native build
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Extract the two core WebAssembly modules from the curl.wasm WASI 0.2 component."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def read_uleb128(buf: bytes, pos: int) -> tuple[int, int]:
|
||||||
|
val = 0
|
||||||
|
shift = 0
|
||||||
|
while True:
|
||||||
|
b = buf[pos]
|
||||||
|
pos += 1
|
||||||
|
val |= (b & 0x7F) << shift
|
||||||
|
if not (b & 0x80):
|
||||||
|
return val, pos
|
||||||
|
shift += 7
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
if len(sys.argv) != 3:
|
||||||
|
sys.stderr.write("Usage: extract_core.py <component.wasm> <out_dir>\n")
|
||||||
|
return 2
|
||||||
|
|
||||||
|
comp_path, out_dir = sys.argv[1], sys.argv[2]
|
||||||
|
data = open(comp_path, "rb").read()
|
||||||
|
if len(data) < 8 or data[:8] != b"\x00asm\x0d\x00\x01\x00":
|
||||||
|
sys.stderr.write(f"Error: {comp_path} is not a WASI 0.2 component binary\n")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
modules: list[bytes] = []
|
||||||
|
pos = 8
|
||||||
|
while pos < len(data):
|
||||||
|
sec_id = data[pos]
|
||||||
|
pos += 1
|
||||||
|
sec_size, pos = read_uleb128(data, pos)
|
||||||
|
payload = data[pos : pos + sec_size]
|
||||||
|
pos += sec_size
|
||||||
|
if sec_id == 1: # CoreModuleSection
|
||||||
|
if len(payload) < 8 or payload[:8] != b"\x00asm\x01\x00\x00\x00":
|
||||||
|
sys.stderr.write("Error: invalid core module payload in component\n")
|
||||||
|
return 1
|
||||||
|
modules.append(payload)
|
||||||
|
|
||||||
|
if len(modules) != 2:
|
||||||
|
sys.stderr.write(f"Error: expected 2 core modules in {comp_path}, found {len(modules)}\n")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
os.makedirs(out_dir, exist_ok=True)
|
||||||
|
with open(os.path.join(out_dir, "mem_mod.wasm"), "wb") as f:
|
||||||
|
f.write(modules[0])
|
||||||
|
with open(os.path.join(out_dir, "curl_core.wasm"), "wb") as f:
|
||||||
|
f.write(modules[1])
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
+1822
File diff suppressed because it is too large.
Load diff
+6
-1
@@ -229,8 +229,13 @@ class TestServerHandler(http.server.BaseHTTPRequestHandler):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
NATIVE_BIN = os.path.abspath(os.environ["CURL_WASM_NATIVE"]) if os.environ.get("CURL_WASM_NATIVE") else None
|
||||||
|
|
||||||
|
|
||||||
def run_curl(args, cwd=ROOT, stdin_bytes=None, use_dir=True, use_wrapper=False):
|
def run_curl(args, cwd=ROOT, stdin_bytes=None, use_dir=True, use_wrapper=False):
|
||||||
if use_wrapper:
|
if NATIVE_BIN:
|
||||||
|
cmd = [NATIVE_BIN] + args
|
||||||
|
elif use_wrapper:
|
||||||
cmd = [WRAPPER] + args
|
cmd = [WRAPPER] + args
|
||||||
else:
|
else:
|
||||||
cmd = ["wasmtime", "run", "-S", "http"]
|
cmd = ["wasmtime", "run", "-S", "http"]
|
||||||
|
|||||||
Reference in new issue
Block a user