From 3c4851e1de705af9c3228fe40a2e9935ffd81ab9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 01:26:48 -0800 Subject: [PATCH 01/21] chore(quest): claim quest/m1/ffi-proofs/protocol.md Quest-Claim: 7ab08adc5d48a51f From 1e2e9ba5c13865c8469d193b8f255e69c17e8611 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 02:03:37 -0800 Subject: [PATCH 02/21] Test the ownership protocol against a model through the C ABI A seeded model of the README's protocol writes scripts of borrows, lends, moves, drops, frees, polls, and cancels on two threads, each step with the outcome it must return; tests/protocol.c runs them natively in the gate and under ASan and TSan, and tests/miri/protocol.rs under Miri. A handle lent from another whose lender was dropped or moved out failed as MOVED, though its own value was never moved: it now fails as OUTLIVED, as the README says. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.rs | 12 +- ffi/tests/fixtures.rs | 107 +++- ffi/tests/fixtures/protocol.rs | 112 ++++ ffi/tests/miri/protocol.rs | 329 ++++++++++ ffi/tests/protocol.c | 261 ++++++++ ffi/tests/protocol/model.rs | 1027 ++++++++++++++++++++++++++++++++ 6 files changed, 1838 insertions(+), 10 deletions(-) create mode 100644 ffi/tests/fixtures/protocol.rs create mode 100644 ffi/tests/miri/protocol.rs create mode 100644 ffi/tests/protocol.c create mode 100644 ffi/tests/protocol/model.rs diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 74d2c9a57..28cb04846 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -1141,9 +1141,15 @@ trait Lends: Send + Sync { impl Lends for Object { fn hold(&self, epoch: usize, local: bool, column: &mut usize, level: u32) -> Result { let listed = match local { - true => self.acquire_local(column, level)?, - false => self.acquire(column, level)?, - }; + true => self.acquire_local(column, level), + false => self.acquire(column, level), + } + // A lender moved out or dropped since outlived this handle, whose own value is not + // the one gone. + .map_err(|status| match status { + Status::Moved => Status::Outlived, + status => status, + })?; if self.epoch.load(Ordering::Acquire) != epoch { self.unhold(local, *column, level, listed); return Err(Status::Outlived); diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index bbb48737d..73ef8c885 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -6,6 +6,8 @@ //! The reject fixture's export must fail at each item it cannot export, and generated //! type shapes round-trip in every language ([`shapes`]). +#[path = "fixtures/protocol.rs"] +mod protocol; #[path = "fixtures/shapes.rs"] mod shapes; #[path = "fixtures/step.rs"] @@ -303,6 +305,78 @@ fn api_c() { } } +/// Random sequences of the ownership protocol's steps through the C ABI, by +/// `tests/protocol.c`, each matching what the model says each step returns. +#[test] +fn api_protocol() { + let api = api(); + let exe = protocol_c( + api, + &scratch().join("api").join("protocol"), + &[], + &api.staticlib(), + &api.native, + ); + let scripts = protocol::scripts(0, PROTOCOL_SEEDS, PROTOCOL_STEPS, true); + protocol::covered(&scripts); + protocol::check( + &scratch().join("api").join("protocol").join("scripts"), + &scripts, + |_| Command::new(&exe), + ); +} + +/// The gate's protocol scripts: how many, and their random steps each. +const PROTOCOL_SEEDS: u64 = 500; +const PROTOCOL_STEPS: usize = 100; + +/// Longer protocol scripts from other seeds, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_protocol_nightly() { + let api = api(); + let exe = protocol_c( + api, + &scratch().join("api").join("protocol"), + &[], + &api.staticlib(), + &api.native, + ); + protocol::check( + &scratch().join("api").join("protocol").join("nightly"), + &protocol::scripts(PROTOCOL_SEEDS, 20_000, 400, true), + |_| Command::new(&exe), + ); +} + +/// `tests/protocol.c` built in `dir` against `lib`, with `flags` to the compiler. +fn protocol_c(api: &Api, dir: &Path, flags: &[&str], lib: &Path, native: &[String]) -> PathBuf { + std::fs::create_dir_all(dir).unwrap(); + let exe = dir.join(format!("protocol{EXE_SUFFIX}")); + let cc = std::env::var_os("CC").unwrap_or_else(|| "cc".into()); + let compiler = if flags.is_empty() { cc } else { "clang".into() }; + let src = manifest_dir().join("tests").join("protocol.c"); + let include = api.out.join("include"); + Step { + stamp: &step::stamp(&exe), + reads: &[&src, &include, lib], + writes: &[&exe], + } + .run( + Command::new(compiler) + .args(["-std=c11", "-pedantic", "-g", "-Wall", "-Wextra", "-Werror"]) + .args(flags) + .arg("-I") + .arg(&include) + .arg(&src) + .arg(lib) + .args(native) + .arg("-o") + .arg(&exe), + ); + exe +} + /// The C and C++ scripts against the glue built under AddressSanitizer, whose leak check /// catches what a cancelled future or a freed handle keeps. #[test] @@ -348,7 +422,8 @@ fn api_go_race() { } /// Build the api fixture's glue, std included, under the `sanitizer`, and run the C and -/// C++ scripts against it, built by clang under the same one. +/// C++ scripts and the protocol scripts' C driver against it, built by clang under the +/// same one. fn sanitized(sanitizer: &str) { let api = api(); let dir = scratch().join("api").join(sanitizer); @@ -400,10 +475,22 @@ fn sanitized(sanitizer: &str) { run(Command::new(&exe).arg(arg).envs(env)); } } + // The protocol scripts, whose steps on two threads free and drop what the other made. + let exe = protocol_c(api, &dir.join("protocol"), &[&sanitize], &lib, &native); + protocol::check( + &dir.join("protocol").join("scripts"), + &protocol::scripts(0, PROTOCOL_SEEDS, 200, true), + |_| { + let mut cmd = Command::new(&exe); + cmd.envs(env); + cmd + }, + ); } /// The glue's `extern "C"` functions driven from Rust under Miri, by `tests/miri/api.rs` -/// mounted as a test module of a copy of the export. +/// and the protocol scripts' driver `tests/miri/protocol.rs`, mounted as test modules of a +/// copy of the export. #[test] #[ignore = "needs the nightly's miri: just ffi sanitize"] fn api_miri() { @@ -415,14 +502,20 @@ fn api_miri() { std::fs::copy(api.out.join(file), copy.join(file)).unwrap(); } std::fs::create_dir_all(copy.join("src").join("rt")).unwrap(); - for file in ["rt.rs", "rt/borrow.rs", "rt/thread.rs", "lib.rs"] { + for file in ["rt.rs", "lib.rs"] { std::fs::copy(api.out.join("src").join(file), copy.join("src").join(file)).unwrap(); } + for entry in std::fs::read_dir(api.out.join("src").join("rt")).unwrap() { + let entry = entry.unwrap(); + std::fs::copy(entry.path(), copy.join("src").join("rt").join(entry.file_name())).unwrap(); + } let mut lib = std::fs::read_to_string(copy.join("src").join("lib.rs")).unwrap(); - lib.push_str(&format!( - "\n#[cfg(test)]\n#[path = {:?}]\nmod miri;\n", - manifest_dir().join("tests").join("miri").join("api.rs") - )); + for (module, file) in [("miri", "api.rs"), ("protocol", "protocol.rs")] { + lib.push_str(&format!( + "\n#[cfg(test)]\n#[path = {:?}]\nmod {module};\n", + manifest_dir().join("tests").join("miri").join(file) + )); + } std::fs::write(copy.join("src").join("lib.rs"), lib).unwrap(); run(cargo() .args(["miri", "test", "--locked", "--lib", "--manifest-path"]) diff --git a/ffi/tests/fixtures/protocol.rs b/ffi/tests/fixtures/protocol.rs new file mode 100644 index 000000000..62eac775f --- /dev/null +++ b/ffi/tests/fixtures/protocol.rs @@ -0,0 +1,112 @@ +//! Random sequences of the ownership protocol's steps, each run against the model in +//! `tests/protocol/model.rs` of what it must return: a driver runs a script through its +//! surface, printing each step's outcome, and the model's transcript must match it. + +#[path = "../protocol/model.rs"] +#[allow(dead_code, reason = "the Miri driver reads what writing scripts does not")] +pub mod model; + +use std::path::Path; +use std::process::Command; + +pub use model::Script; + +/// The scripts of `count` seeds from `first`, each `len` random steps long. +pub fn scripts(first: u64, count: u64, len: usize, homes: bool) -> Vec