diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f28208923..8004e1b83 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -193,6 +193,11 @@ jobs: - name: Shapes run: nix develop --command just ffi shapes + # Longer random protocol scripts through the C ABI and each binding, against the + # model of what each step returns; the gate runs fewer. + - name: Protocol + run: nix develop --command just ffi protocol + # What exports, and every refusal, ranked. - name: Census run: nix develop --command just ffi census diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml index 6ee07cb45..064c0d35e 100644 --- a/ffi/Cargo.toml +++ b/ffi/Cargo.toml @@ -11,6 +11,11 @@ publish = false [package.metadata.rust-analyzer] rustc_private = true +# Mounted by `#[path]` into a copy of an export, or by more than one test, which cargo-shear +# does not follow. +[package.metadata.cargo-shear] +ignored-paths = ["tests/miri/*.rs", "tests/protocol/*.rs"] + [workspace] # Its own workspace, apart from the repo's: the driver builds on ts's pinned nightly, # which rust-toolchain.toml links. diff --git a/ffi/README.md b/ffi/README.md index fa002c4d9..439a502d0 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -880,7 +880,9 @@ value of a Rust type crosses as one Swift type that holds it exactly, and back u any executor awaits it. - **Checks**: a borrow or move Rust's rules refuse at run time, such as a borrow of a value that is not `Sync` on a second thread, throws a `CheckError`, holding the function and - the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a + the rule, before any Rust code runs, so the call changed nothing, but that a call + consuming an object took it, as its parameter consumes it whether or not the value moves, + and frees it as a scope's end would. So every function whose objects can fail a check `throws`, and a field read stays a property whose getter throws, except one of a noncopyable value, whose getter Swift cannot let throw, which is a method, `firstGet()`. Of a type whose checks can fail, or a record or enum holding one, `description` and `debugDescription` @@ -1425,6 +1427,7 @@ just ffi moq-swift # the same export, running only moq/moq.swift just ffi moq-api # stage moq's API on another target into target/moq-api, which the exports above merge just ffi census # census each into target/census just ffi shapes # round-trip every sampled type shape in every language but KNOWN's, as the nightly does +just ffi protocol # run longer protocol scripts from more seeds through the C ABI and every binding, as the nightly does nix develop .#bench --command just ffi browsers # api's TypeScript in headless Chromium and Firefox, as the nightly runs it nix develop .#android --command just ffi android # the Kotlin test, shrunk by R8, on the Android device adb reaches nix develop .#bench --command just ffi bench # the binding benchmark: bench/ffi/README.md's cases from Rust, and through the export from C, C++, Kotlin, Python, Swift, Go, and Dart @@ -1483,6 +1486,34 @@ the nightly does. `KNOWN` lists what a binding still gets wrong, each with the q fixes it; the driver skips those shapes, so the fix deletes its entry, and the nightly fails on an entry that matches no exported shape. A new IR `Type` variant fails `ir_covered` until the sampler covers it. +The protocol tests run random sequences of borrows, exclusive lends, moves, drops, frees, +polls, and cancels over the api fixture's checked types, on the thread that made a value +and another, against `tests/protocol/model.rs`: an executable model of what this README +says each step returns, the status a check fails with included. A seed makes a script of +steps and the model's transcript of their outcomes, shaped by what a driver's surface can +run, and each driver runs the script through its surface and prints the outcomes back, which +must match line for line, with the counts of the script's waker where it polls with one. +`tests/protocol.c` runs them through the C ABI, `tests/miri/protocol.rs` under Miri, and +`just ffi sanitize` runs the C driver under ASan and TSan. Each binding runs them through +its own wrapper too, from `tests/protocol.cpp`, `.swift`, `.py`, `.go`, `.kt`, `.dart`, and +`.ts`: its `Profile` in the model leaves out what the binding cannot write, such as a free +where its collector frees or a failed check where it aborts, and adds what the binding does +its own way, such as a close that refuses before Rust's check, a future made only as it is +awaited, or one made on a home. `protocol_profiles` checks that each flag keeps its +scripts to it. The gate runs 500 scripts of 100 steps through the C ABI, failing unless +they meet every status and make every kind of handle, and 40 to 500 through each binding; +`just ffi protocol`, run nightly, runs 20,000 of 400 steps through the C ABI and 1,000 to +5,000 through each binding. +`tests/borrow.rs` and `tests/hold.rs` model the borrow check and the bindings' hold counts +under loom, and `tests/future.rs` and `tests/thread.rs` model the runtime's futures and +threads, each mounting the runtime's own source over loom's primitives. The futures' model +races a wake from another thread against a poll, a change of waker, a start, and a free: +each C waker drops once and is never woken after, a pending poll is always woken, and a +future that is not `Send` refuses a poll elsewhere and drops on its own thread. The threads' +model races a free elsewhere against its thread's check, wake, drain, and exit, and lends +calls to homes that call each other: each value drops once on its own thread, or leaks once +that thread exited, and each home goes idle once no value or call holds it. Each explores +every interleaving, or for homes those within a preemption bound, in the gate. The `reject` fixture's expected errors, each with its notes, are `fixtures/reject/expected.txt`. The `lock` test exports the `lock` fixture for wasm32, staged, and the host, then again against that export's `api.json`: its `added` feature only adds and must pass, and its `next` @@ -1491,7 +1522,8 @@ error matching `fixtures/lock/expected.txt`. `just ffi sanitize`, run nightly, rebuilds the `api` export with std under AddressSanitizer and then ThreadSanitizer, runs `tests/api.c` and `tests/api.cpp` against it, and matches their output to the native transcript; it runs `tests/api.go` under Go's race detector; -and it drives the C ABI from `tests/miri/api.rs` under Miri with strict provenance. +and it drives the C ABI from `tests/miri/api.rs` and the protocol scripts from +`tests/miri/protocol.rs` under Miri with strict provenance. moq's crates export through `target/moq-ffi`, an empty library on each that `just ffi moq` writes in place of moq-ffi's remainder. The export serves the host and each target `just ffi moq-api` staged in `target/moq-api` from the same moq sources, and fails on a diff --git a/ffi/justfile b/ffi/justfile index 4d1712aca..82536ec9b 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -41,6 +41,10 @@ test *args: shapes: cargo test --locked --test fixtures shapes::nightly -- --ignored +# Run longer protocol scripts from more seeds through the C ABI and every binding, as the nightly does; the gate runs fewer. +protocol: + cargo test --locked --test fixtures protocol_nightly -- --ignored + # Run the api fixture's web test in headless Chromium and Firefox, in the bench shell, which carries them: nix develop .#bench. browsers: cargo test --locked --test fixtures -- --ignored --exact api_browsers diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 74d2c9a57..41649852d 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -14,13 +14,21 @@ use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::sync::Arc; use std::sync::atomic::{self, AtomicBool, AtomicUsize, Ordering}; -use std::task::{Context, Wake}; +use std::task::Context; mod borrow; use borrow::{Borrow, EXCLUSIVE, RESERVED, TAKEN, Table}; mod thread; pub use thread::Thread; use thread::Dropped; +mod future; +#[allow(unused_imports, reason = "the runtime serves every API, and an API may poll no future")] +pub use future::{ERROR, Future, PENDING, READY, Returned, Waker}; +// What thread.rs builds on beside the imports above, which its loom model swaps for +// loom's: a lock in a static apart, since loom makes one anew each run. +use std::sync::Mutex as StaticMutex; +use std::sync::{Mutex, MutexGuard, OnceLock}; +use std::thread as os; /// `value`'s `Hash`, through std's hasher with its fixed keys, so a value hashes alike on /// every call. @@ -1141,9 +1149,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); @@ -2115,90 +2129,6 @@ impl Drop for ExclusiveArc { } } -/// A foreign waker: `wake` may be called from any thread, any number of times, until -/// `drop` releases `data`. Either function may be null. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct Waker { - pub wake: Option, - pub drop: Option, - pub data: *mut c_void, -} - -impl Waker { - /// Whether `other` is this waker again, which a poll reuses rather than wrapping anew. - fn same(&self, other: &Self) -> bool { - self.wake.map(|f| f as usize) == other.wake.map(|f| f as usize) && self.data == other.data - } - - /// The Rust waker around it, which calls `drop` once its last clone drops. One Rust - /// returned stays wrapped too, so its `data` names it for as long as a future may - /// compare it. - pub fn lift(self) -> std::task::Waker { - match (self.wake, self.drop) { - // Nothing to wake or release: a ready future's first poll takes no allocation. - (None, None) => std::task::Waker::noop().clone(), - _ => std::task::Waker::from(Arc::new(Foreign(self))), - } - } - - /// Wake it, as C does. - /// - /// # Safety - /// It is not yet dropped. - pub unsafe fn wake(&self) { - if let Some(wake) = self.wake { - // SAFETY: the waker's contract. - unsafe { wake(self.data) }; - } - } - - /// Drop it, as C does once. - /// - /// # Safety - /// It is not yet dropped, and is never used again. - pub unsafe fn release(self) { - if let Some(drop) = self.drop { - // SAFETY: the waker's contract. - unsafe { drop(self.data) }; - } - } - - /// Rust's waker, or a context's, as one C owns: `wake` wakes it, and `drop` releases it. - pub fn lower(waker: impl Into) -> Self { - Self { - wake: Some(rust_wake), - drop: Some(rust_drop), - data: Box::into_raw(Box::new(waker.into().0)).cast(), - } - } -} - -/// A waker a function returns: its own, or a context's. -pub struct Returned(std::task::Waker); - -impl From for Returned { - fn from(waker: std::task::Waker) -> Self { - Self(waker) - } -} - -impl From> for Returned { - fn from(cx: Context<'_>) -> Self { - Self(cx.waker().clone()) - } -} - -unsafe extern "C" fn rust_wake(data: *mut c_void) { - // SAFETY: `Waker::lower` made `data`, which C has not dropped. - unsafe { &*data.cast::() }.wake_by_ref(); -} - -unsafe extern "C" fn rust_drop(data: *mut c_void) { - // SAFETY: `Waker::lower` made `data`, which C drops once. - drop(unsafe { Box::from_raw(data.cast::()) }); -} - /// A future C implements, of `()`: Rust polls it with a waker of its own each time, from /// any thread, until `poll` returns true, and calls `free` once it drops the future, done /// or not. `free` may be null. @@ -2255,35 +2185,6 @@ impl Drop for Polled { } } -struct Foreign(Waker); - -// SAFETY: the waker's contract: `wake` and `drop` may be called from any thread. -unsafe impl Send for Foreign {} -// SAFETY: as above. -unsafe impl Sync for Foreign {} - -impl Wake for Foreign { - fn wake(self: Arc) { - self.wake_by_ref(); - } - - fn wake_by_ref(self: &Arc) { - if let Some(wake) = self.0.wake { - // SAFETY: the waker's contract. - unsafe { wake(self.0.data) }; - } - } -} - -impl Drop for Foreign { - fn drop(&mut self) { - if let Some(drop) = self.0.drop { - // SAFETY: the waker's contract; this is the last reference. - unsafe { drop(self.0.data) }; - } - } -} - /// The data of a function C lends for a call, which Rust only passes back to it. #[derive(Clone, Copy)] pub struct Data(*mut c_void); @@ -2412,239 +2313,6 @@ impl std::io::Seek for Reader { } } -pub const PENDING: i32 = 0; -pub const READY: i32 = 1; -pub const ERROR: i32 = 2; - -/// A future behind a handle, polled from C: the head of the one allocation holding the -/// future and the function that lowers its result, which [`Future::new`] makes. -#[repr(C)] -pub struct Future { - /// Poll the allocation, lowering the result through the poll's out pointers once ready. - poll: unsafe fn(*mut Future, Waker, &[*mut c_void], *mut c_void, &str) -> i32, - /// Drop the allocation. - drop: unsafe fn(*mut Future), - /// The last foreign waker, and the Rust waker around it. - waker: Option<(Waker, std::task::Waker)>, - polling: AtomicBool, - /// For a future that is not `Send`, the thread it stays on, which alone polls or drops it. - owner: Option>, -} - -/// The allocation behind a handle: its head first, so the handle points at both. -#[repr(C)] -struct Typed { - head: Future, - /// The future, pinned here until it finishes and drops in place. - future: Option, - /// Writes the finished future's result through the poll's out pointers, one per value - /// it returns, and its error pointer, returning the poll. The poll that finishes the - /// future calls it, so it never crosses threads. - lower: L, -} - -impl Future { - pub fn new(future: F, lower: L) -> *mut Self - where - F: std::future::Future + Send + 'static, - // Send, as any poll may finish the future on another thread. - L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + Send + 'static, - { - Self::make(future, lower, None) - } - - /// A future that is not `Send`, which stays on this thread: a poll on another fails, - /// unless this is a home, which the poll then runs on, and a free there drops it here. - pub fn unsent(future: F, lower: L) -> *mut Self - where - F: std::future::Future + 'static, - L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, - { - Self::make(future, lower, Some(Thread::here())) - } - - fn make(future: F, lower: L, owner: Option>) -> *mut Self - where - F: std::future::Future + 'static, - L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, - { - let typed = Box::new(Typed { - head: Self { - poll: poll_typed::, - drop: drop_typed::, - waker: None, - polling: AtomicBool::new(false), - owner, - }, - future: Some(future), - lower, - }); - Box::into_raw(typed).cast() - } - - /// Poll it once with `waker`, writing the result through `outs` and `err` when ready. A - /// future that is not `Send` polled on another thread than its own fails, writing the - /// status through `status`, or aborting if that is null; one on a home polls there. - /// - /// # Safety - /// `future` is a live future handle, each of `outs` and `err` is null or points where - /// the function's result types go, and `status` is null or points where an `int32_t` goes. - pub unsafe fn poll( - future: *mut Self, - waker: Waker, - outs: &[*mut c_void], - err: *mut c_void, - status: *mut i32, - what: &str, - ) -> i32 { - if future.is_null() { - fail(what, "a future is null"); - } - // SAFETY: the caller's contract; only the flag and the owner are touched before - // it is held. - let head = unsafe { &*future }; - if let Some(owner) = &head.owner { - if owner.away() { - // SAFETY: a home, and this thread waits while it polls. - return unsafe { owner.run(|| Self::poll(future, waker, outs, err, status, what)) }; - } - if let Err(refused) = owner.own() { - if head.polling.swap(true, Ordering::Acquire) { - fail(what, "a future is polled concurrently"); - } - // A waker it never adopted is Rust's to drop, as a poll drops the one it - // replaces; the flag keeps the one it holds from changing meanwhile. - // SAFETY: the flag makes this the only access. - if !unsafe { &(*future).waker }.as_ref().is_some_and(|(last, _)| last.same(&waker)) { - // SAFETY: the caller gave it, and nothing else holds it. - unsafe { waker.release() }; - } - head.polling.store(false, Ordering::Release); - // SAFETY: the caller's contract. - return unsafe { failed(status, refused, what) }; - } - } - if head.polling.swap(true, Ordering::Acquire) { - fail(what, "a future is polled concurrently"); - } - // SAFETY: the flag makes this the only access, and `new` made the allocation. - let result = unsafe { (head.poll)(future, waker, outs, err, what) }; - // SAFETY: as above; the allocation outlives the poll. - unsafe { &*future }.polling.store(false, Ordering::Release); - result - } - - /// Make the future's first poll, with no waker: on `READY` or `ERROR` the result is - /// written, the future freed, and null written through `handle`; on `PENDING` its - /// handle goes there, for C to poll with a waker and free. - /// - /// # Safety - /// `future` is a handle `new` just made, `handle` is null, which aborts, or points - /// where a handle goes, and each of `outs` and `err` is null or points where the function's result - /// types go. - pub unsafe fn start( - future: *mut Self, - outs: &[*mut c_void], - err: *mut c_void, - handle: *mut *mut Self, - what: &str, - ) -> i32 { - if handle.is_null() { - fail(what, "a future's handle pointer is null"); - } - let none = Waker { - wake: None, - drop: None, - data: std::ptr::null_mut(), - }; - // SAFETY: the caller's contract; the future was made on this thread, or one this - // call runs on. - let result = unsafe { Self::poll(future, none, outs, err, std::ptr::null_mut(), what) }; - match result { - // SAFETY: the caller's contract. - PENDING => unsafe { handle.write(future) }, - // SAFETY: it finished; null tells C there is no handle, and `free` takes it. - _ => unsafe { - handle.write(std::ptr::null_mut()); - Self::free(future) - }, - } - result - } - - /// Drop the future, cancelling it if it has not finished. - /// - /// # Safety - /// `future` is null or a live future handle, freed once and not being polled. - pub unsafe fn free(future: *mut Self) { - if future.is_null() { - return; - } - // SAFETY: the caller's contract. - let owner = unsafe { &(*future).owner }; - // A future that is not `Send` drops on its own thread. - if let Some(owner) = owner.as_ref().filter(|owner| !owner.is_here()) { - // SAFETY: the future is the owner's. - unsafe { Dropped::new(Freed(future), owner.clone(), "a future") }.send(); - return; - } - // SAFETY: the caller's contract. - unsafe { ((*future).drop)(future) }; - } -} - -/// A future freed on another thread than its own, which drops it. -struct Freed(*mut Future); - -impl Drop for Freed { - fn drop(&mut self) { - // SAFETY: `Future::free`'s contract, on the future's own thread. - unsafe { ((*self.0).drop)(self.0) }; - } -} - -/// [`Future`]'s poll of a `Typed`. -/// -/// # Safety -/// `future` is a live `Typed` that only this call accesses, and each of `outs` and -/// `err` is null or points where the function's result types go. -unsafe fn poll_typed i32>( - future: *mut Future, - waker: Waker, - outs: &[*mut c_void], - err: *mut c_void, - what: &str, -) -> i32 { - // SAFETY: the caller's contract. - let Typed { head, future, lower } = unsafe { &mut *future.cast::>() }; - let Some(inner) = future.as_mut() else { - fail(what, "a future is polled after it finished"); - }; - if !head.waker.as_ref().is_some_and(|(last, _)| last.same(&waker)) { - head.waker = Some((waker, waker.lift())); - } - let (_, rust) = head.waker.as_ref().expect("set above"); - let mut cx = Context::from_waker(rust); - // SAFETY: the allocation never moves, and the future drops in place. - match unsafe { Pin::new_unchecked(inner) }.poll(&mut cx) { - std::task::Poll::Pending => PENDING, - std::task::Poll::Ready(value) => { - *future = None; - head.waker = None; - lower(value, outs, err) - } - } -} - -/// [`Future`]'s drop of a `Typed`. -/// -/// # Safety -/// `future` is a live `Typed`, dropped once. -unsafe fn drop_typed(future: *mut Future) { - // SAFETY: the caller's contract; `new` boxed it. - drop(unsafe { Box::from_raw(future.cast::>()) }); -} - /// Write `value` through `out` if the caller wants it, or drop it. /// /// # Safety diff --git a/ffi/runtime/rt/future.rs b/ffi/runtime/rt/future.rs new file mode 100644 index 000000000..8f915f9ed --- /dev/null +++ b/ffi/runtime/rt/future.rs @@ -0,0 +1,366 @@ +//! Futures C polls and the wakers it polls them with: a future is one allocation behind a +//! handle, polled one call at a time from any thread, and a waker C gives is wrapped once +//! and dropped once its last clone drops, on whichever thread that is. +//! +//! The runtime compiles this beside `rt.rs`, and `ffi/tests/future.rs` under loom, each +//! with its own `Arc`, `AtomicBool`, `Ordering`, and threads. + +use std::ffi::c_void; +use std::pin::Pin; +use std::task::{Context, Wake}; + +use super::thread::{Dropped, Thread}; +use super::{Arc, AtomicBool, Ordering, fail, failed}; + +/// A foreign waker: `wake` may be called from any thread, any number of times, until +/// `drop` releases `data`. Either function may be null. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct Waker { + pub wake: Option, + pub drop: Option, + pub data: *mut c_void, +} + +impl Waker { + /// Whether `other` is this waker again, which a poll reuses rather than wrapping anew: + /// one that differs in any function is another, whose `drop` must run. + fn same(&self, other: &Self) -> bool { + self.wake.map(|f| f as usize) == other.wake.map(|f| f as usize) + && self.drop.map(|f| f as usize) == other.drop.map(|f| f as usize) + && self.data == other.data + } + + /// The Rust waker around it, which calls `drop` once its last clone drops. One Rust + /// returned stays wrapped too, so its `data` names it for as long as a future may + /// compare it. + pub fn lift(self) -> std::task::Waker { + match (self.wake, self.drop) { + // Nothing to wake or release: a ready future's first poll takes no allocation. + (None, None) => std::task::Waker::noop().clone(), + _ => std::task::Waker::from(std::sync::Arc::new(Foreign(self))), + } + } + + /// Wake it, as C does. + /// + /// # Safety + /// It is not yet dropped. + pub unsafe fn wake(&self) { + if let Some(wake) = self.wake { + // SAFETY: the waker's contract. + unsafe { wake(self.data) }; + } + } + + /// Drop it, as C does once. + /// + /// # Safety + /// It is not yet dropped, and is never used again. + pub unsafe fn release(self) { + if let Some(drop) = self.drop { + // SAFETY: the waker's contract. + unsafe { drop(self.data) }; + } + } + + /// Rust's waker, or a context's, as one C owns: `wake` wakes it, and `drop` releases it. + pub fn lower(waker: impl Into) -> Self { + Self { + wake: Some(rust_wake), + drop: Some(rust_drop), + data: Box::into_raw(Box::new(waker.into().0)).cast(), + } + } +} + +/// A waker a function returns: its own, or a context's. +pub struct Returned(std::task::Waker); + +impl From for Returned { + fn from(waker: std::task::Waker) -> Self { + Self(waker) + } +} + +impl From> for Returned { + fn from(cx: Context<'_>) -> Self { + Self(cx.waker().clone()) + } +} + +unsafe extern "C" fn rust_wake(data: *mut c_void) { + // SAFETY: `Waker::lower` made `data`, which C has not dropped. + unsafe { &*data.cast::() }.wake_by_ref(); +} + +unsafe extern "C" fn rust_drop(data: *mut c_void) { + // SAFETY: `Waker::lower` made `data`, which C drops once. + drop(unsafe { Box::from_raw(data.cast::()) }); +} + +struct Foreign(Waker); + +// SAFETY: the waker's contract: `wake` and `drop` may be called from any thread. +unsafe impl Send for Foreign {} +// SAFETY: as above. +unsafe impl Sync for Foreign {} + +impl Wake for Foreign { + fn wake(self: std::sync::Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &std::sync::Arc) { + if let Some(wake) = self.0.wake { + // SAFETY: the waker's contract. + unsafe { wake(self.0.data) }; + } + } +} + +impl Drop for Foreign { + fn drop(&mut self) { + if let Some(drop) = self.0.drop { + // SAFETY: the waker's contract; this is the last reference. + unsafe { drop(self.0.data) }; + } + } +} + +pub const PENDING: i32 = 0; +pub const READY: i32 = 1; +pub const ERROR: i32 = 2; + +/// A future behind a handle, polled from C: the head of the one allocation holding the +/// future and the function that lowers its result, which [`Future::new`] makes. +#[repr(C)] +pub struct Future { + /// Poll the allocation, lowering the result through the poll's out pointers once ready. + poll: unsafe fn(*mut Future, Waker, &[*mut c_void], *mut c_void, &str) -> i32, + /// Drop the allocation. + drop: unsafe fn(*mut Future), + /// The last foreign waker, and the Rust waker around it. + waker: Option<(Waker, std::task::Waker)>, + polling: AtomicBool, + /// For a future that is not `Send`, the thread it stays on, which alone polls or drops it. + owner: Option>, +} + +/// The allocation behind a handle: its head first, so the handle points at both. +#[repr(C)] +struct Typed { + head: Future, + /// The future, pinned here until it finishes and drops in place. + future: Option, + /// Writes the finished future's result through the poll's out pointers, one per value + /// it returns, and its error pointer, returning the poll. The poll that finishes the + /// future calls it, so it never crosses threads. + lower: L, +} + +impl Future { + pub fn new(future: F, lower: L) -> *mut Self + where + F: std::future::Future + Send + 'static, + // Send, as any poll may finish the future on another thread. + L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + Send + 'static, + { + Self::make(future, lower, None) + } + + /// A future that is not `Send`, which stays on this thread: a poll on another fails, + /// unless this is a home, which the poll then runs on, and a free there drops it here. + pub fn unsent(future: F, lower: L) -> *mut Self + where + F: std::future::Future + 'static, + L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, + { + Self::make(future, lower, Some(Thread::here())) + } + + fn make(future: F, lower: L, owner: Option>) -> *mut Self + where + F: std::future::Future + 'static, + L: Fn(F::Output, &[*mut c_void], *mut c_void) -> i32 + 'static, + { + let typed = Box::new(Typed { + head: Self { + poll: poll_typed::, + drop: drop_typed::, + waker: None, + polling: AtomicBool::new(false), + owner, + }, + future: Some(future), + lower, + }); + Box::into_raw(typed).cast() + } + + /// Poll it once with `waker`, writing the result through `outs` and `err` when ready. A + /// future that is not `Send` polled on another thread than its own fails, writing the + /// status through `status`, or aborting if that is null; one on a home polls there. + /// + /// # Safety + /// `future` is a live future handle, each of `outs` and `err` is null or points where + /// the function's result types go, and `status` is null or points where an `int32_t` goes. + pub unsafe fn poll( + future: *mut Self, + waker: Waker, + outs: &[*mut c_void], + err: *mut c_void, + status: *mut i32, + what: &str, + ) -> i32 { + if future.is_null() { + fail(what, "a future is null"); + } + // SAFETY: the caller's contract. Only the flag and the owner are borrowed, each + // through its own field, as another poll may hold the rest while this one is refused. + let (polling, owner) = unsafe { (&(*future).polling, &(*future).owner) }; + if let Some(owner) = owner { + if owner.away() { + // SAFETY: a home, and this thread waits while it polls. + return unsafe { owner.run(|| Self::poll(future, waker, outs, err, status, what)) }; + } + if let Err(refused) = owner.own() { + if polling.swap(true, Ordering::Acquire) { + fail(what, "a future is polled concurrently"); + } + // A waker it never adopted is Rust's to drop, as a poll drops the one it + // replaces; the flag keeps the one it holds from changing meanwhile. + // SAFETY: the flag makes this the only access. + if !unsafe { &(*future).waker } + .as_ref() + .is_some_and(|(last, _)| last.same(&waker)) + { + // SAFETY: the caller gave it, and nothing else holds it. + unsafe { waker.release() }; + } + polling.store(false, Ordering::Release); + // SAFETY: the caller's contract. + return unsafe { failed(status, refused, what) }; + } + } + if polling.swap(true, Ordering::Acquire) { + fail(what, "a future is polled concurrently"); + } + // SAFETY: the flag makes this the only access, and `new` made the allocation. + let result = unsafe { ((*future).poll)(future, waker, outs, err, what) }; + polling.store(false, Ordering::Release); + result + } + + /// Make the future's first poll, with no waker: on `READY` or `ERROR` the result is + /// written, the future freed, and null written through `handle`; on `PENDING` its + /// handle goes there, for C to poll with a waker and free. + /// + /// # Safety + /// `future` is a handle `new` just made, `handle` is null, which aborts, or points + /// where a handle goes, and each of `outs` and `err` is null or points where the function's result + /// types go. + pub unsafe fn start( + future: *mut Self, + outs: &[*mut c_void], + err: *mut c_void, + handle: *mut *mut Self, + what: &str, + ) -> i32 { + if handle.is_null() { + fail(what, "a future's handle pointer is null"); + } + let none = Waker { + wake: None, + drop: None, + data: std::ptr::null_mut(), + }; + // SAFETY: the caller's contract; the future was made on this thread, or one this + // call runs on. + let result = unsafe { Self::poll(future, none, outs, err, std::ptr::null_mut(), what) }; + match result { + // SAFETY: the caller's contract. + PENDING => unsafe { handle.write(future) }, + // SAFETY: it finished; null tells C there is no handle, and `free` takes it. + _ => unsafe { + handle.write(std::ptr::null_mut()); + Self::free(future) + }, + } + result + } + + /// Drop the future, cancelling it if it has not finished. + /// + /// # Safety + /// `future` is null or a live future handle, freed once and not being polled. + pub unsafe fn free(future: *mut Self) { + if future.is_null() { + return; + } + // SAFETY: the caller's contract. + let owner = unsafe { &(*future).owner }; + // A future that is not `Send` drops on its own thread. + if let Some(owner) = owner.as_ref().filter(|owner| !owner.is_here()) { + // SAFETY: the future is the owner's. + unsafe { Dropped::new(Freed(future), owner.clone(), "a future") }.send(); + return; + } + // SAFETY: the caller's contract. + unsafe { ((*future).drop)(future) }; + } +} + +/// A future freed on another thread than its own, which drops it. +struct Freed(*mut Future); + +impl Drop for Freed { + fn drop(&mut self) { + // SAFETY: `Future::free`'s contract, on the future's own thread. + unsafe { ((*self.0).drop)(self.0) }; + } +} + +/// [`Future`]'s poll of a `Typed`. +/// +/// # Safety +/// `future` is a live `Typed` that only this call accesses, and each of `outs` and +/// `err` is null or points where the function's result types go. +unsafe fn poll_typed i32>( + future: *mut Future, + waker: Waker, + outs: &[*mut c_void], + err: *mut c_void, + what: &str, +) -> i32 { + let typed = future.cast::>(); + // SAFETY: the caller's contract. Only the fields the polling flag guards are borrowed, + // not the flag or the owner, which a concurrent poll reads before it is refused. + let (last, future, lower) = unsafe { (&mut (*typed).head.waker, &mut (*typed).future, &(*typed).lower) }; + let Some(inner) = future.as_mut() else { + fail(what, "a future is polled after it finished"); + }; + if !last.as_ref().is_some_and(|(last, _)| last.same(&waker)) { + *last = Some((waker, waker.lift())); + } + let (_, rust) = last.as_ref().expect("set above"); + let mut cx = Context::from_waker(rust); + // SAFETY: the allocation never moves, and the future drops in place. + match unsafe { Pin::new_unchecked(inner) }.poll(&mut cx) { + std::task::Poll::Pending => PENDING, + std::task::Poll::Ready(value) => { + *future = None; + *last = None; + lower(value, outs, err) + } + } +} + +/// [`Future`]'s drop of a `Typed`. +/// +/// # Safety +/// `future` is a live `Typed`, dropped once. +unsafe fn drop_typed(future: *mut Future) { + // SAFETY: the caller's contract; `new` boxed it. + drop(unsafe { Box::from_raw(future.cast::>()) }); +} diff --git a/ffi/runtime/rt/thread.rs b/ffi/runtime/rt/thread.rs index 20eba4fba..59c5c42c7 100644 --- a/ffi/runtime/rt/thread.rs +++ b/ffi/runtime/rt/thread.rs @@ -9,13 +9,15 @@ //! A home is a thread Rust owns, for a binding with no thread of its own that stays put, //! as a goroutine or a Dart isolate moves between threads: a call it makes on a value a //! home owns runs there while the caller waits, so the value never leaves its thread. +//! +//! The runtime compiles this beside `rt.rs`, and `ffi/tests/thread.rs` under loom, each +//! with its own locks, atomics, `Arc`, and threads. use std::cell::{Cell, OnceCell}; -use std::sync::OnceLock; use std::mem::{ManuallyDrop, MaybeUninit}; use std::panic::AssertUnwindSafe; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex, MutexGuard}; + +use super::{Arc, AtomicBool, Mutex, MutexGuard, OnceLock, Ordering, StaticMutex, Status, fail, os}; /// A thread values that are not `Send` stay on. pub struct Thread { @@ -23,7 +25,7 @@ pub struct Thread { pending: AtomicBool, jobs: Mutex, /// A home's thread, which parks for a job, unparked as one is queued. - parked: OnceLock, + parked: OnceLock, /// Whether it is a home, which runs a call from any thread. home: bool, } @@ -77,18 +79,24 @@ impl Dropped { } } - /// Queue it to its thread, or leak it, saying so, if that exited. - pub fn send(self) { + /// Queue it to its thread, or leak it, saying so, if that exited: whether it queued. + pub fn send(self) -> bool { // Kept until the push returns, so its thread may run the drop first: then this is // the last record, which lets the home go idle. let owner = self.owner.clone(); - if let Err(Job::Drop(dropped)) = owner.push(Job::Drop(self)) { - eprintln!( - "rust2-ffi: {} leaks: it is not Send, and its thread exited before it was freed", - dropped.what - ); - } + let queued = match owner.push(Job::Drop(self)) { + Ok(()) => true, + Err(Job::Drop(dropped)) => { + eprintln!( + "rust2-ffi: {} leaks: it is not Send, and its thread exited before it was freed", + dropped.what + ); + false + } + Err(_) => unreachable!("a push returns the job it was given"), + }; Thread::let_go(owner); + queued } } @@ -102,7 +110,7 @@ struct Run { /// What a thread waiting on a call it lent a home waits for. struct Done { ran: AtomicBool, - waiter: std::thread::Thread, + waiter: os::Thread, } impl Job { @@ -115,7 +123,7 @@ impl Job { // one through the C ABI does. // SAFETY: the waiting thread keeps the call alive until `ran`. if std::panic::catch_unwind(AssertUnwindSafe(|| unsafe { (run.call)(run.data) })).is_err() { - crate::rt::fail("a home", "a call panicked"); + fail("a home", "a call panicked"); } // SAFETY: as above; once `ran`, the waiter may return, so it is read first. let done = unsafe { &*run.done }; @@ -142,7 +150,7 @@ impl Drop for Here { thread_local!(static HERE: Here = const { Here(OnceCell::new(), Cell::new(false)) }); /// The homes no value lives on, idle until a call takes one. -static IDLE: Mutex>> = Mutex::new(Vec::new()); +static IDLE: StaticMutex>> = StaticMutex::new(Vec::new()); impl Thread { fn new(home: bool, gone: bool) -> Self { @@ -215,9 +223,9 @@ impl Thread { /// Whether this is its thread, so a value of it may be used, after dropping what other /// threads freed of it meanwhile. #[inline] - pub fn own(&self) -> Result<(), crate::rt::Status> { + pub fn own(&self) -> Result<(), Status> { if !self.is_here() { - return Err(crate::rt::Status::NotSend); + return Err(Status::NotSend); } if self.pending.load(Ordering::Acquire) { self.drain(); @@ -292,11 +300,17 @@ impl Thread { } let home = Arc::new(Self::new(true, false)); let serving = home.clone(); - let spawned = std::thread::Builder::new() + let spawned = os::Builder::new() .name("rust2-ffi home".to_string()) - .spawn(move || serving.serve()); - if spawned.is_err() { - crate::rt::fail("a home", "its thread could not be spawned"); + .spawn(move || Self::serve(serving)); + match spawned { + // Set before the home is returned, so every push to it finds its thread to + // unpark: one set by the home's own thread could land after a push that missed + // it, and the home, missing the push's pending flag in turn, would park for good. + Ok(spawned) => { + let _ = home.parked.set(spawned.thread().clone()); + } + Err(_) => fail("a home", "its thread could not be spawned"), } home } @@ -325,11 +339,10 @@ impl Thread { /// A home's thread: run each job queued for it, forever. Idle with no value living on /// it, which a value's record would count. - fn serve(self: Arc) { - let me: *const Self = &*self; - let _ = self.parked.set(std::thread::current()); + fn serve(this: Arc) { + let me: *const Self = &*this; HERE.with(|here| { - let _ = here.0.set(self); + let _ = here.0.set(this); }); // SAFETY: this thread's record holds it for as long as the thread runs, which is forever. let me = unsafe { &*me }; @@ -346,14 +359,16 @@ impl Thread { } }); if !me.pending.load(Ordering::Acquire) { - std::thread::park(); + os::park(); } } } /// This thread's record, if it is a home. fn serving() -> Option> { - HERE.try_with(|here| here.0.get().filter(|thread| thread.home).cloned()).ok().flatten() + HERE.try_with(|here| here.0.get().filter(|thread| thread.home).cloned()) + .ok() + .flatten() } /// Wait until `done`, running each job queued for `home`, this thread, meanwhile: @@ -362,7 +377,7 @@ impl Thread { while !done() { match home.pending.load(Ordering::Acquire) { true => home.drain(), - false => std::thread::park(), + false => os::park(), } } } @@ -400,7 +415,7 @@ impl Thread { } let done = Done { ran: AtomicBool::new(false), - waiter: std::thread::current(), + waiter: os::current(), }; let run = Run { call: invoker(&call), @@ -408,7 +423,7 @@ impl Thread { done: &done, }; if self.push(Job::Run(run)).is_err() { - crate::rt::fail("a home", "its thread exited"); + fail("a home", "its thread exited"); } let ran = || done.ran.load(Ordering::Acquire); // A home waiting on another runs each call queued for it meanwhile, as one the call @@ -417,7 +432,7 @@ impl Thread { Some(home) => Self::wait_serving(&home, ran), None => { while !ran() { - std::thread::park(); + os::park(); } } } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 11896a261..25163bc0d 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -23,6 +23,7 @@ use crate::ir::{ const RUNTIME: &str = include_str!("../runtime/rt.rs"); const BORROW: &str = include_str!("../runtime/rt/borrow.rs"); const THREAD: &str = include_str!("../runtime/rt/thread.rs"); +const FUTURE: &str = include_str!("../runtime/rt/future.rs"); /// The crate's build script, which refuses to build for a target but `targets`, on which an /// item a target compiles would be missing, or one it leaves out left over, and tells the @@ -415,6 +416,7 @@ impl Crate<'_> { output.file(&out.join("src/rt.rs"), RUNTIME); output.file(&out.join("src/rt/borrow.rs"), BORROW); output.file(&out.join("src/rt/thread.rs"), THREAD); + output.file(&out.join("src/rt/future.rs"), FUTURE); output.file(&out.join("include").join(format!("{}.h", self.abi.prefix)), &header); for (path, header) in &cpp.files { output.file(&out.join("include").join(path), header); diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index bbb48737d..a6bf9b864 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, protocol::Profile::C); + 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, protocol::Profile::C), + |_| 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, protocol::Profile::C), + |_| { + 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"]) @@ -686,23 +779,48 @@ fn cxx_command(link: &Link, standard: u8, plain: bool, src: &Path, exe: &Path) - cmd } +/// Random sequences of the ownership protocol's steps through the C++ header, by +/// `tests/protocol.cpp`, each matching what the model says each step returns: never a +/// failed check, which aborts in C++. +#[test] +fn api_cpp_protocol() { + cpp_protocol(&protocol::scripts( + 0, + PROTOCOL_SEEDS, + PROTOCOL_STEPS, + protocol::Profile::CPP, + )); +} + +/// Longer protocol scripts through the C++ header, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_cpp_protocol_nightly() { + cpp_protocol(&protocol::scripts(PROTOCOL_SEEDS, 5000, 400, protocol::Profile::CPP)); +} + +fn cpp_protocol(scripts: &[protocol::Script]) { + let api = api(); + let dir = scratch().join("api").join("protocol-cpp"); + std::fs::create_dir_all(&dir).unwrap(); + let exe = dir.join(format!("protocol{EXE_SUFFIX}")); + cxx( + &api.link(), + 17, + true, + &manifest_dir().join("tests").join("protocol.cpp"), + &exe, + ); + protocol::check(&dir.join("scripts"), scripts, |_| Command::new(&exe)); +} + /// The generated Kotlin, compiled with `tests/api.kt` against the library's JNI /// functions, runs its script on the JVM, then refuses a library of another contract. #[test] fn api_kotlin() { let api = api(); - let classes = scratch().join("api").join("kotlin"); + let classes = kotlin_classes(api); let jar = PathBuf::from(coroutines()); - let step = Step { - stamp: &step::stamp(&classes), - reads: &[ - &api.out.join("kotlin").join("api.kt"), - &manifest_dir().join("tests").join("api.kt"), - &jar, - ], - writes: &[&classes], - }; - step.run(&mut kotlinc(&api.out, &classes)); let java = |classpath: &[&Path], args: &[&str]| { let mut cmd = Command::new("kotlin"); cmd.arg(format!("-Djava.library.path={}", api.lib.display())) @@ -713,6 +831,12 @@ fn api_kotlin() { cmd }; kotlin(&api.transcript, &|args| java(&[&classes], args)); + // The protocol scripts, through `tests/protocol.kt`, compiled with the rest. + protocol::check_all( + &scratch().join("api").join("protocol").join("kotlin"), + &protocol::scripts(0, KOTLIN_PROTOCOL_SEEDS, PROTOCOL_STEPS, protocol::Profile::KOTLIN), + || protocol_kotlin(api, &classes), + ); let native_returns = run(cargo() .arg("run") @@ -866,13 +990,59 @@ fn api_android() { kotlin(&api.transcript, &art); } -/// kotlinc compiling the generated Kotlin with `tests/api.kt` into `dest`. +/// The gate's Kotlin protocol scripts, fewer than C's, as each costs the JVM more. +const KOTLIN_PROTOCOL_SEEDS: u64 = 100; + +/// `tests/protocol.kt`, compiled into `classes` with the api fixture's Kotlin, on the JVM. +fn protocol_kotlin(api: &Api, classes: &Path) -> Command { + let mut cmd = Command::new("kotlin"); + cmd.arg(format!("-Djava.library.path={}", api.lib.display())) + .arg("-cp") + .arg(std::env::join_paths([classes.as_os_str(), &coroutines()]).unwrap()) + .arg("ProtocolKt"); + cmd +} + +/// Longer Kotlin protocol scripts from other seeds, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_kotlin_protocol_nightly() { + let api = api(); + let classes = kotlin_classes(api); + protocol::check_all( + &scratch().join("api").join("protocol").join("kotlin-nightly"), + &protocol::scripts(PROTOCOL_SEEDS, 2000, 400, protocol::Profile::KOTLIN), + || protocol_kotlin(api, &classes), + ); +} + +/// The api fixture's Kotlin, `tests/api.kt`, and `tests/protocol.kt`, compiled. +fn kotlin_classes(api: &Api) -> PathBuf { + let classes = scratch().join("api").join("kotlin"); + let jar = PathBuf::from(coroutines()); + Step { + stamp: &step::stamp(&classes), + reads: &[ + &api.out.join("kotlin").join("api.kt"), + &manifest_dir().join("tests").join("api.kt"), + &manifest_dir().join("tests").join("protocol.kt"), + &jar, + ], + writes: &[&classes], + } + .run(&mut kotlinc(&api.out, &classes)); + classes +} + +/// kotlinc compiling the generated Kotlin with `tests/api.kt` and `tests/protocol.kt` into +/// `dest`. fn kotlinc(out: &Path, dest: &Path) -> Command { let mut cmd = Command::new("kotlinc"); cmd.args(["-Werror", "-cp"]) .arg(coroutines()) .arg(out.join("kotlin").join("api.kt")) .arg(manifest_dir().join("tests").join("api.kt")) + .arg(manifest_dir().join("tests").join("protocol.kt")) .arg("-d") .arg(dest); cmd @@ -1072,6 +1242,37 @@ fn api_python() { ); } +/// Random sequences of the ownership protocol's steps through the Python, by +/// `tests/protocol.py`, each matching what the model says each step returns. +#[test] +fn api_python_protocol() { + python_protocol(&protocol::scripts(0, 40, 100, protocol::Profile::PYTHON), "scripts"); +} + +/// Longer Python protocol scripts from other seeds, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_python_protocol_nightly() { + python_protocol(&protocol::scripts(40, 1000, 400, protocol::Profile::PYTHON), "nightly"); +} + +fn python_protocol(scripts: &[protocol::Script], dir: &str) { + let api = api(); + let python = std::env::var_os("PYTHON").unwrap_or_else(|| "python3".into()); + protocol::check( + &scratch().join("api").join("python-protocol").join(dir), + scripts, + |_| { + let mut cmd = Command::new(&python); + // As `api_python` runs its script: off the path, in dev mode, warnings as errors. + cmd.args(["-P", "-X", "dev", "-W", "error"]) + .arg(manifest_dir().join("tests").join("protocol.py")) + .env("PYTHONPATH", api.out.join("python")); + cmd + }, + ); +} + /// The api fixture's Python, and `tests/api_types.py` calling it as a typed caller does, /// pass mypy and pyright, both strict, at the oldest CPython its extension loads in, and /// `tests/bare_builtins.py` finds it reading no builtin by its bare name. Apart from @@ -1126,12 +1327,16 @@ fn kotlin_contract(kotlin: &Path) -> u64 { #[test] fn api_swift() { let api = api(); - let dir = scratch().join("api").join("swift"); - let module = api.out.join("swift"); - let include = api.out.join("include"); - let source = module.join("api.swift"); - let collections = swift_collections(); - let built = collections_dir(); + let SwiftApi { + dir, + module, + include, + source, + collections, + built, + swiftmodule, + lib, + } = swift_api(api); let step = Step { stamp: &dir.join("typecheck.stamp"), reads: &[&module, &include, &built], @@ -1142,21 +1347,6 @@ fn api_swift() { .args(["-swift-version", "5", "-typecheck"]) .arg(&source), ); - let (swiftmodule, lib) = (dir.join("Api.swiftmodule"), dir.join("libApi.a")); - let step = Step { - stamp: &dir.join("Api.stamp"), - reads: &[&module, &include, &built], - writes: &[&swiftmodule, &lib], - }; - step.run( - swiftc(&module, 13) - .args(["-swift-version", "6", "-parse-as-library", "-module-name", "Api"]) - .args(["-emit-module", "-emit-module-path"]) - .arg(&swiftmodule) - .args(["-emit-library", "-static", "-o"]) - .arg(&lib) - .arg(&source), - ); // The test's executor preference is macOS 15's. let exe = dir.join("api-test"); let test = manifest_dir().join("tests").join("api.swift"); @@ -1281,6 +1471,116 @@ fn api_swift() { ); } +/// The api fixture's Swift, built as the module `Api` in Swift 6's language mode. +struct SwiftApi { + dir: PathBuf, + /// The generated Swift's directory, with the C header's module map. + module: PathBuf, + include: PathBuf, + source: PathBuf, + /// swift-collections' libraries, and where they were built. + collections: Vec, + built: PathBuf, + swiftmodule: PathBuf, + lib: PathBuf, +} + +fn swift_api(api: &Api) -> SwiftApi { + let dir = scratch().join("api").join("swift"); + let module = api.out.join("swift"); + let include = api.out.join("include"); + let source = module.join("api.swift"); + let collections = swift_collections(); + let built = collections_dir(); + let (swiftmodule, lib) = (dir.join("Api.swiftmodule"), dir.join("libApi.a")); + let step = Step { + stamp: &dir.join("Api.stamp"), + reads: &[&module, &include, &built], + writes: &[&swiftmodule, &lib], + }; + // Once per process: the Swift tests share its outputs, which a second build running + // beside the first would remove. + static BUILT: OnceLock<()> = OnceLock::new(); + BUILT.get_or_init(|| { + std::fs::create_dir_all(&dir).unwrap(); + step.run( + swiftc(&module, 13) + .args(["-swift-version", "6", "-parse-as-library", "-module-name", "Api"]) + .args(["-emit-module", "-emit-module-path"]) + .arg(&swiftmodule) + .args(["-emit-library", "-static", "-o"]) + .arg(&lib) + .arg(&source), + ); + }); + SwiftApi { + dir, + module, + include, + source, + collections, + built, + swiftmodule, + lib, + } +} + +/// Random sequences of the ownership protocol's steps through the Swift, by +/// `tests/protocol.swift`, each matching what the model says each step returns: none Swift's +/// compiler would refuse, and each future an async call awaited as it is made. +#[test] +fn api_swift_protocol() { + swift_protocol(&protocol::scripts( + 0, + PROTOCOL_SEEDS, + PROTOCOL_STEPS, + protocol::Profile::SWIFT, + )); +} + +/// Longer protocol scripts through the Swift, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_swift_protocol_nightly() { + swift_protocol(&protocol::scripts(PROTOCOL_SEEDS, 5000, 400, protocol::Profile::SWIFT)); +} + +fn swift_protocol(scripts: &[protocol::Script]) { + let api = api(); + let swift = swift_api(api); + let dir = scratch().join("api").join("protocol-swift"); + let exe = dir.join(format!("protocol{EXE_SUFFIX}")); + let src = manifest_dir().join("tests").join("protocol.swift"); + let step = Step { + stamp: &step::stamp(&exe), + reads: &[ + &src, + &swift.module, + &swift.include, + &swift.built, + &swift.swiftmodule, + &swift.lib, + &api.staticlib(), + ], + writes: &[&exe], + }; + std::fs::create_dir_all(&dir).unwrap(); + // Its executor preference is macOS 15's. + step.run( + swiftc(&swift.module, 15) + .args(["-parse-as-library", "-I"]) + .arg(&swift.dir) + .arg(&src) + .arg(&swift.lib) + .args(&swift.collections) + .arg(api.staticlib()) + .args(&api.native) + .arg("-o") + .arg(&exe), + ); + protocol::check(&dir.join("scripts"), scripts, |_| Command::new(&exe)); +} + /// The fixture's script compiled for wasm32-wasip1 and run under Node's WASI: native Rust on /// the target the web binding serves, whose `usize` is 32 bits. fn wasm_transcript() -> String { @@ -1320,8 +1620,8 @@ fn wasm_transcript() -> String { String::from_utf8_lossy(&transcript.stdout).into_owned() } -/// The api fixture's web binding, beside a copy of `tests/api.ts` as a caller's project -/// holds it, type-checked with no issue, and the module the crate's `web` feature builds +/// The api fixture's web binding, beside copies of `tests/api.ts` and `tests/protocol.ts` +/// as a caller's project holds them, type-checked with no issue, and the module the crate's `web` feature builds /// for wasm32-wasip1, built once for each runtime's test. struct Web { dir: PathBuf, @@ -1345,6 +1645,10 @@ fn web() -> &'static Web { .env("RUSTFLAGS", "-D warnings")); step::mirror(&api.out.join("web"), &dir.join("web")); step::copy(&manifest_dir().join("tests").join("api.ts"), &dir.join("api.ts")); + step::copy( + &manifest_dir().join("tests").join("protocol.ts"), + &dir.join("protocol.ts"), + ); step::write(&dir.join("tsconfig.json"), TSCONFIG); run(Command::new("tsc").arg("-p").arg(&dir)); Web { @@ -1450,6 +1754,47 @@ fn web_script(runtime: &str, case: Option<&str>) -> Command { cmd } +/// Random sequences of the ownership protocol's steps through the web binding on Node and +/// on Deno, by `tests/protocol.ts`, each matching what the model says each step returns. +#[test] +fn api_web_protocol() { + for runtime in ["node", "deno"] { + web_protocol(runtime, "scripts", 0, PROTOCOL_SEEDS, PROTOCOL_STEPS); + } +} + +/// Longer protocol scripts from other seeds through the web binding on Node, as the +/// nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_web_protocol_nightly() { + web_protocol("node", "nightly", PROTOCOL_SEEDS, 5000, 400); +} + +/// `count` scripts from `first` of `steps` each, through `tests/protocol.ts` in one process +/// of `runtime`, Node or Deno. +fn web_protocol(runtime: &str, name: &str, first: u64, count: u64, steps: usize) { + let web = web(); + let scripts = protocol::scripts(first, count, steps, protocol::Profile::TS); + let driver = || { + let mut cmd = Command::new(runtime); + match runtime { + "deno" => cmd + .args(["run", "--allow-read", "--allow-write", "--allow-env"]) + .env("DENO_DIR", scratch().join("deno")) + .env("DENO_NO_UPDATE_CHECK", "1") + .env("NO_COLOR", "1"), + _ => cmd.arg("--no-warnings"), + }; + cmd.arg("protocol.ts") + .current_dir(&web.dir) + .env("RUST2_WASM", &web.wasm); + cmd + }; + let dir = scratch().join("api").join("web-protocol").join(runtime).join(name); + protocol::check_all(&dir, &scripts, driver); +} + /// The generated TypeScript runs `tests/api.ts`'s script on Node and on Deno, over the module /// the crate's `web` feature builds for wasm32-wasip1: it prints what native Rust prints on /// that target, then checks its futures, what disposing and the garbage collector free, what @@ -1559,7 +1904,7 @@ const TSCONFIG: &str = r#"{ "skipLibCheck": true, "types": [] }, - "include": ["api.ts", "web/*.ts"] + "include": ["api.ts", "protocol.ts", "web/*.ts"] } "#; @@ -1594,6 +1939,13 @@ fn api_dart() { for case in ["futures", "owned", "closed", "bytes", "bounds"] { run(&mut dart_run(&exe, &[case])); } + // The protocol scripts, through `tests/protocol.dart`. + let protocol = protocol_dart(api, &dir); + protocol::check_all( + &scratch().join("api").join("protocol").join("dart"), + &protocol::scripts(0, DART_PROTOCOL_SEEDS, PROTOCOL_STEPS, protocol::Profile::DART), + || dart_run(&protocol, &[]), + ); for (case, message) in [ ("moved", "Bad state: api_Counter_get: a handle's value was moved out"), ("boxed moved", "Bad state: api_Node_sum: a handle's value was moved out"), @@ -1648,6 +2000,33 @@ fn api_dart() { ); } +/// The gate's Dart protocol scripts, fewer than C's, as each step costs Dart more. +const DART_PROTOCOL_SEEDS: u64 = 100; + +/// `tests/protocol.dart`, compiled in `dir` against the api fixture's Dart. +fn protocol_dart(api: &Api, dir: &Path) -> PathBuf { + dart_compile( + "api", + dir, + &api.out.join("dart"), + &api.lib.join(PYTHON_LIB), + &manifest_dir().join("tests").join("protocol.dart"), + ) +} + +/// Longer Dart protocol scripts from other seeds, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_dart_protocol_nightly() { + let api = api(); + let protocol = protocol_dart(api, &scratch().join("api").join("dart")); + protocol::check_all( + &scratch().join("api").join("protocol").join("dart-nightly"), + &protocol::scripts(PROTOCOL_SEEDS, 2000, 400, protocol::Profile::DART), + || dart_run(&protocol, &[]), + ); +} + /// `main` compiled into a kernel snapshot in `dir`, importing the generated Dart in /// `package` as `package:/.dart`, whose native asset is the shared library /// `lib`: as a package's build hook would bundle it, which `dart compile` has no flag for. @@ -1872,6 +2251,34 @@ fn go_layout(api: &Api, module: &Path, main: &str) { ); } +/// Random sequences of the ownership protocol's steps through the Go, by +/// `tests/protocol.go`, each matching what the model says each step returns. +#[test] +fn api_go_protocol() { + go_protocol(&protocol::scripts(0, 100, 100, protocol::Profile::GO), "scripts"); +} + +/// Longer Go protocol scripts from other seeds, as the nightly runs them. +#[test] +#[ignore = "slow: just ffi protocol"] +fn api_go_protocol_nightly() { + go_protocol(&protocol::scripts(100, 5000, 400, protocol::Profile::GO), "nightly"); +} + +fn go_protocol(scripts: &[protocol::Script], dir: &str) { + let api = api(); + let built = scratch().join("api").join("go-protocol"); + let module = built.join("module"); + go_layout( + api, + &module, + &std::fs::read_to_string(manifest_dir().join("tests").join("protocol.go")).unwrap(), + ); + let exe = built.join(format!("protocol{EXE_SUFFIX}")); + go_build(api, &module, &exe); + protocol::check(&built.join(dir), scripts, |_| Command::new(&exe)); +} + /// Build the Go `module` into `exe` against the api fixture's static library. fn go_build(api: &Api, module: &Path, exe: &Path) { let step = Step { diff --git a/ffi/tests/fixtures/protocol.rs b/ffi/tests/fixtures/protocol.rs new file mode 100644 index 000000000..651c7f8d9 --- /dev/null +++ b/ffi/tests/fixtures/protocol.rs @@ -0,0 +1,311 @@ +//! 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::io::Read; +use std::path::Path; +use std::process::{Command, Output, Stdio}; +use std::time::{Duration, Instant}; + +pub use model::{Profile, Script}; + +/// The scripts of `count` seeds from `first`, each `len` random steps long, for a surface +/// of `profile`. +pub fn scripts(first: u64, count: u64, len: usize, profile: Profile) -> Vec