Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bench/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,26 @@ profile), Dart 3.13.0. C and Dart are from the same run, for scale; wall ns per
An object return pays the handle's `FinalizationRegistry` entry, as Go's and Dart's pay
their collectors'. Firefox runs every row at about 2 to 3 times Node's.

### The export's build cost

The web export [refuses what its host lacks](../../ffi/README.md#typescript-web) by
walking MIR, so it builds every crate with `-Zalways-encode-mir`, takes std's from the
dev shell's MIR sysroot, and checks the glue crate a second time to walk it. Measured
2026-11-17 on the same host, the stand-in's export from an emptied target through
[`measure.py`](measure.py), at rust2 `3b2e79f0` (signatures alone) and `0dc269dc` (with
the refusal), 3 rounds each, alternating, at a load average of 24 to 31; medians:

| export | wall s | CPU s | peak RSS |
|---|---|---|---|
| signatures alone | 27.1 | 76.4 | 856 MiB |
| with the refusal | 45.7 | 87.8 | 1,773 MiB |

The refusal adds about 11 CPU seconds, and its walk of the stand-in's 299,455 reachable
instances takes 6.5 s of them. Wall time ranged 24 to 60 s and 39 to 69 s between rounds
at this load, so its gap is rough; the walk's check runs after moq's crates, with nothing
beside it. Peak memory doubles, steady across rounds. A native export reads no MIR and
pays none of it.

### Lending returned bytes

Bytes a call returns are an [`rt.Bytes`](../../ffi/README.md#typescript-web): up to 64 B a copy, and
Expand Down
13 changes: 11 additions & 2 deletions driver/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,24 @@ pub struct Check<'a> {
pub args: &'a [String],
/// The environment cargo runs in, which the wrapper reads its job from.
pub env: &'a [(&'a str, &'a OsStr)],
/// Where cargo builds, absolute, if not [`target_dir`]'s for the manifest: another
/// package's, whose crates this check then shares.
pub target_dir: Option<&'a Path>,
/// Where cargo runs, which it reads its config and a `--config` path from, absolute, if
/// not the package's directory: another package's, whose config this check then shares.
pub dir: Option<&'a Path>,
}

impl Check<'_> {
/// Run it, after cleaning the package: cargo can't tell the driver's output is
/// stale, so the package always recompiles.
pub fn run(&self) -> Result<ExitCode, String> {
let exe = std::env::current_exe().map_err(|err| format!("finding this executable: {err}"))?;
let target_dir = target_dir(self.tool, Some(self.manifest))?;
let package_dir = self.manifest.parent().expect("a manifest path has a parent");
let target_dir = match self.target_dir {
Some(dir) => dir.to_path_buf(),
None => target_dir(self.tool, Some(self.manifest))?,
};
let package_dir = (self.dir).unwrap_or_else(|| self.manifest.parent().expect("a manifest path has a parent"));
let cargo = |subcommand: &str| {
let mut cmd = cargo();
// Cargo reads its config from where it runs, so run where the package is.
Expand Down
125 changes: 125 additions & 0 deletions ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rust2-driver = { path = "../driver" }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.154"
toml = "1.1.8"
wasmparser = { version = "0.263.0", default-features = false, features = ["std"] }

[dev-dependencies]
loom = "0.7.3"
Expand Down
25 changes: 21 additions & 4 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1319,10 +1319,27 @@ target there.
API for it, so nothing of WASI ships. Its clocks are `performance.now`, from
`performance.timeOrigin` for the real-time one; its randomness `crypto.getRandomValues`;
its stdout and stderr `console.log` and `console.error`, a line at a time; its
environment and arguments empty; and its exit an abort. `load` refuses a module importing
anything else. Measured on the api fixture, stripped, with LTO at `opt-level = "s"`, that
std costs 24 KB over `wasm32-unknown-unknown`'s (3%), 9.5 KB gzipped, and the glue 2 KB
more; `wasm32-unknown-unknown` has no clock, so `Instant::now` panics there.
environment and arguments empty; and its exit an abort. What else the module would
import, such as the file system, sockets, or another module's function, is refused at
export, at the span of each call in the crate's own code that reaches it, naming the
imports it reaches and the path down to them. The export walks MIR from each exported
function, and each one a crate's `.init_array` static runs when the module loads,
through every call, `dyn` implementation, function pointer, static, and drop it can
reach, std's included, and into C and whatever Rust the C calls back, so a new std needs
no change here: every crate builds with `-Zalways-encode-mir`, std's from the dev
shell's MIR sysroot or else `-Zbuild-std`, which doubles the export's peak memory, as
[measured](../bench/ffi/README.md#the-exports-build-cost); a native export reads no MIR.
A C function or static no linked library defines, which would fail the module's link, is
refused the same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1`
compile to stubs that panic. `thread::Builder::spawn` and a socket's `connect` and `bind`
import nothing there and return an error, as Rust's own build for the target does, so
they are not refused; `thread::spawn` panics on that error, which aborts the module. The
walk checks the generated crate, so unlike the export's other refusals it runs once
`out/` is written, and a refused export leaves that `out/` behind, failed, as a refused
`Cargo.lock` does. `load` still refuses a module importing anything else. Measured on
the api fixture, stripped, with LTO at `opt-level = "s"`, that std costs 24 KB over
`wasm32-unknown-unknown`'s (3%), 9.5 KB gzipped, and the glue 2 KB more;
`wasm32-unknown-unknown` has no clock, so `Instant::now` panics there.
- **The API** is rust2-ts's shape: each type is a module of its own, `web/<Type>.ts`, which
exports the type by its name and its functions, each taking `self` first, so a caller
writes `import * as Counter from "./web/Counter.ts"` and `Counter.get(c)`, and a bundler
Expand Down
25 changes: 25 additions & 0 deletions ffi/fixtures/lacks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "lacks-fixture"
description = "Exports whose wasm32-wasip1 module would import what the web target's host lacks, each refused at the call that reaches it"
version = "0.0.0"
edition = "2024"
license = "MIT OR Apache-2.0"
publish = false

# The export's prefix.
[lib]
name = "lacks"

[features]
# A call of a C function no library defines, which fails the module's link, so a build
# without it can compare the module's imports with what the export refused.
undefined = []
# A function calling itself with an ever larger type, which the walk refuses at the
# recursion limit, as rustc's collector does, rather than walk without end.
recursion = []

[dependencies]
web-sys = { version = "0.3.107", features = ["console"] }

[lints]
workspace = true
43 changes: 43 additions & 0 deletions ffi/fixtures/lacks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Compile `src/needs.ll` and `src/ctor.ll` into `libneeds.a` for a wasm32 target, with the
//! toolchain's own llc and llvm-ar, which its `llvm-tools` component ships.

use std::path::PathBuf;
use std::process::Command;

fn main() {
println!("cargo::rerun-if-changed=src/needs.ll");
println!("cargo::rerun-if-changed=src/ctor.ll");
if std::env::var("CARGO_CFG_TARGET_ARCH").unwrap() != "wasm32" {
return;
}
let rustc = std::env::var("RUSTC").unwrap();
let sysroot = Command::new(rustc).args(["--print", "sysroot"]).output().unwrap();
assert!(sysroot.status.success(), "rustc --print sysroot failed");
let sysroot = String::from_utf8(sysroot.stdout).unwrap();
let bin = PathBuf::from(sysroot.trim())
.join("lib/rustlib")
.join(std::env::var("HOST").unwrap())
.join("bin");
let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let run = |cmd: &mut Command| {
let status = cmd.status().unwrap();
assert!(status.success(), "{cmd:?} failed");
};
let archive = out.join("libneeds.a");
let _ = std::fs::remove_file(&archive);
let mut ar = Command::new(bin.join("llvm-ar"));
ar.arg("rcs").arg(&archive);
// Each its own object, so a call pulls only its own object, and its constructor, into
// the link.
for name in ["needs", "ctor"] {
let object = out.join(format!("{name}.o"));
run(Command::new(bin.join("llc"))
.args(["-mtriple=wasm32-wasip1", "-filetype=obj"])
.arg(format!("src/{name}.ll"))
.arg("-o")
.arg(&object));
ar.arg(object);
}
run(&mut ar);
println!("cargo::rustc-link-search=native={}", out.display());
}
25 changes: 25 additions & 0 deletions ffi/fixtures/lacks/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fixtures/lacks/src/lib.rs:104:11: rust2-ffi: this calls `undefined`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:130:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:137:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:14:2: rust2-ffi: `std::thread::sleep` reaches `wasi_snapshot_preview1.poll_oneoff`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:151:11: rust2-ffi: the C function `needs_missing` reaches `missing`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:158:11: rust2-ffi: the C function `needs_data` reaches `missing_data`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:174:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:186:11: rust2-ffi: the module imports `rust2.rust2_spoof`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:18:1: rust2-ffi: the drop of `std::net::TcpListener` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:198:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:203:27: rust2-ffi: `<T as std::convert::Into<U>>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:203:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:203:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:225:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:257:27: rust2-ffi: `<T as std::convert::Into<U>>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:257:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:257:48: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:41:3: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:52:2: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:62:2: rust2-ffi: `std::fs::exists` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:85:11: rust2-ffi: `std::fs::remove_file` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_unlink_file`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:91:2: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
fixtures/lacks/src/lib.rs:9:2: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config
1 change: 1 addition & 0 deletions ffi/fixtures/lacks/ffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude = []
Loading