WEP: Native Executables
Context
A Wado program reaches its users today as a component that wado run or
another Wasm host runs. A command-line tool distributed that way asks every user
to install a runtime first, and few will. The tool must ship as one native
executable that runs with nothing else installed.
The intended workflow builds on CI, one job per platform. Each job fetches the
release wado for its platform and builds the executable for that same
platform. A build therefore only has to target the host it runs on.
Decision
wado build --exe links a native executable
wado build --exe builds the program and links it, with the host's C toolchain,
into one executable for the host. The executable holds:
- the Wado runtime:
wado-hostwithout itstoolchainfeature, on a wasmtime that has no Cranelift; - the program's component, compiled ahead of time to machine code (
.cwasm).
Nothing is fetched at build time or at run time.
The release wado carries the runtime as a static library
The release workflow builds the runtime as a static library
(libwado_runtime.a, or wado_runtime.lib on Windows) and embeds it in the
wado binary. It also records the native libraries the runtime needs, as
rustc --print native-static-libs reports them, so wado build --exe passes
the linker the same list.
The C library is linked statically wherever the platform allows it, so the executable needs no particular libc version on the user's machine:
- On Linux the runtime targets musl, and
wadocarries musl'slibc.aand startup objects, as Rust's musl targets do. A static glibc is no substitute: its name lookup loads NSS modules at run time, and the executable resolves names. - On Windows the runtime links the C runtime statically (
crt-static). - On macOS
libSystemis the one supported way to reach the kernel and cannot be linked statically. It is part of the OS, so this costs nothing.
The component goes in an object file of its own
wado build --exe takes the component wado build builds for the
wasi:cli/command world, and refuses a package that declares none: the
executable calls its run.
It compiles the component with Engine::precompile_component, on an Engine
whose Config::target names the host's triple. A target set that way infers no
CPU feature from the build machine, so the code runs on any CPU of the
architecture, not only on the CI runner's. Cranelift compiles at the level the
build's -O maps to, as wado run does. The runtime loads code of any level:
wasmtime does not hold a .cwasm to the loading engine's opt_level.
It wraps the result in an object file with the object crate, as one
page-aligned section that the linker maps read-only and executable: a section of
the __TEXT segment on Mach-O, and one flagged SHF_ALLOC | SHF_EXECINSTR on
ELF or executable on PE. The runtime finds the component by a symbol on that
section and never reads its own file to find it.
The runtime runs the code where it lies. It loads the component with
Component::deserialize_raw, which uses the memory it is given rather than a
copy. Its Engine takes a CustomCodeMemory whose publish_executable changes
nothing, since the linker already mapped that memory executable. Loading writes
nothing into that memory: CodeMemory::new rejects a .cwasm with relocations
outside .debug_*, and memory from MmapVec::from_raw reports no virtual
memory, so publish skips make_readonly. No page is remapped from writable to
executable, so a notarized macOS executable needs no JIT entitlement
(com.apple.security.cs.allow-jit or
com.apple.security.cs.allow-unsigned-executable-memory).
The runtime and the compiler that produced the .cwasm must configure the
Engine identically, or wasmtime refuses the code. wado-host's
default_config splits into the part both share (Wasm features, collector,
tunables) and the compiler's Cranelift settings, which a wasmtime without
Cranelift cannot set. The .cwasm records the Cranelift settings, and the
runtime library and the compiler come from the same release.
A C compiler driver links, and signs on macOS
On Linux and macOS, wado build --exe links through cc, which on macOS comes
with the Xcode Command Line Tools. On macOS the driver passes the linker the SDK
root and platform version. On Linux it runs with -static -nostdlib, and wado
passes musl's objects in place of the host's C library. On Windows it calls MSVC's link.exe. That is not on PATH in an
ordinary shell, so wado finds it and the LIB paths it needs from the Visual
Studio installation, as cc-rs does.
When it finds none, the error says so, names the driver it looked for, and tells the user to install that toolchain or a compatible one. It recommends none: any linker the host's C toolchain uses will do.
On macOS, Apple's linker ad-hoc signs its output, as it does every executable it links. A distributor then signs it with a Developer ID and notarizes it as any other executable. Wado ships no code that writes Mach-O or a code signature.
The executable grants what wado run grants
The executable reaches what wado run grants a program given no flags. Its
filesystem is the current directory and what lies under it. It inherits the
environment and the host's network, name lookup included. The filesystem limit
guards the user's machine against a bug in the program, not against a malicious
one.
Every argument belongs to the program, so the executable takes no flag of its own to widen the grant.
What was rejected
Append the component to a prebuilt executable
deno compile and bun build --compile work this way, and the libsui crate
offers it. Appending breaks the code signature on macOS, so the tool has to
rewrite the Mach-O load commands and sign the binary again. libsui does both,
but it belongs to Deno, whose own development ends after Cloudflare's
acquisition
(Linux Today).
Writing that part ourselves keeps hundreds of lines of Mach-O and
code-signing logic for what a linker already does.
Generate a Rust crate and build it with cargo
This embeds the component with include_bytes! and links wado-host from
source. It works, but every build needs a Rust toolchain and a full wasmtime
build.
Link with a linker written in Rust
libwild links ELF only, so macOS and Windows would still link through the
host's toolchain, and wado would keep two ways to link.
Compile the Wasm to C
wasm2c and similar tools translate no Wasm GC, stack switching or Component Model code, all of which Wado emits.
Roadmap
- [x] A
wado-runtimecrate buildswado-hostwithouttoolchain, on wasmtime without Cranelift, as a static library that runs an embedded.cwasm. - [ ] The release workflow embeds that library and its native library list in
wado. The C code of the runtime's dependencies (ring, ittapi) builds for musl with clang as the target's C compiler; no musl toolchain is needed. - [x]
wado build --exeprecompiles the component, writes its object file and runs the linker on Linux and macOS (x86_64 and aarch64) and on x86_64 Windows. On a change to what an executable is linked from, CI runstools/test-exe.sh, which links one and runs it, on x86_64 and aarch64 Linux, aarch64 macOS and x86_64 Windows; no runner tests x86_64 macOS. It stays a script, not an integration test, since it builds the runtime library first. - [ ] A notarized macOS executable runs with no entitlement.
