From 015568b5aaf3cbe520003f99afe4e81c59441254 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 02:42:51 -0800 Subject: [PATCH 01/13] chore(quest): claim quest/m1/ffi-proofs/checkers.md Quest-Claim: 4c77667a63611f41 From 5386f6a1c880e0b927c307e617b08ce5af689a01 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 03:14:13 -0800 Subject: [PATCH 02/13] Run UBSan, Valgrind, cgocheck2, and Go's leak profile on the api fixture --- ffi/justfile | 4 +-- ffi/tests/api.go | 15 +++++++++ ffi/tests/fixtures.rs | 76 ++++++++++++++++++++++++++++++++++++------- flake.nix | 5 ++- 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/ffi/justfile b/ffi/justfile index 4d1712aca..57930849d 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -49,9 +49,9 @@ browsers: android: cargo test --locked --test fixtures -- --ignored --exact api_android -# Run the api fixture's C and C++ scripts under AddressSanitizer and ThreadSanitizer, its Go script under Go's race detector, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. +# Run the api fixture's C and C++ scripts under AddressSanitizer with UndefinedBehaviorSanitizer, ThreadSanitizer, and Valgrind, its Go script under Go's race detector and cgocheck2, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. sanitize: - cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_go_race api_miri + cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_valgrind api_go_checked api_miri # Apply formatters without changing program behavior. fix: diff --git a/ffi/tests/api.go b/ffi/tests/api.go index d944aa4b6..eb829b5a1 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -17,6 +17,7 @@ import ( "net/netip" "os" "runtime" + "runtime/pprof" "slices" "strings" "sync" @@ -2097,4 +2098,18 @@ func main() { default: broken(os.Args[1]) } + unleaked() +} + +// unleaked fails if a goroutine is blocked forever on what nothing else can reach, such +// as a future's wake channel a cancelled call dropped. +func unleaked() { + leaks := pprof.Lookup("goroutineleak") + var stacks strings.Builder + // Writing it runs the collection that finds them. + ok(leaks.WriteTo(&stacks, 1)) + if leaks.Count() != 0 { + fmt.Fprint(os.Stderr, stacks.String()) + os.Exit(1) + } } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index bbb48737d..3ebfc483c 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -219,9 +219,8 @@ fn api() -> &'static Api { /// exclusively. const OUTLIVED: &str = "api_Meter_count: a handle lent from another outlived it"; -#[test] -fn api_c() { - let api = api(); +/// `tests/api.c` built by `CC` against the api fixture's static library. +fn api_c_exe(api: &Api) -> PathBuf { let exe = scratch().join("api").join("api-test"); let cc = std::env::var_os("CC").unwrap_or_else(|| "cc".into()); let src = manifest_dir().join("tests").join("api.c"); @@ -233,7 +232,7 @@ fn api_c() { } .run( Command::new(cc) - .args(["-std=c11", "-pedantic", "-Wall", "-Wextra", "-Werror", "-I"]) + .args(["-std=c11", "-pedantic", "-Wall", "-Wextra", "-Werror", "-g", "-I"]) .arg(&include) .arg(&src) .arg(api.staticlib()) @@ -241,6 +240,13 @@ fn api_c() { .arg("-o") .arg(&exe), ); + exe +} + +#[test] +fn api_c() { + let api = api(); + let exe = api_c_exe(api); let c = run(&mut Command::new(&exe)); assert_eq!( String::from_utf8_lossy(&c.stdout), @@ -320,12 +326,13 @@ fn api_tsan() { } /// The Go script under Go's race detector, which catches a race between an object's -/// `Close` and the nursery that pushes it out on another goroutine. +/// `Close` and the nursery that pushes it out on another goroutine, and with cgocheck2, +/// which catches a Go pointer stored in memory Rust owns. #[test] #[ignore = "slow: just ffi sanitize"] -fn api_go_race() { +fn api_go_checked() { let api = api(); - let dir = scratch().join("api").join("go-race"); + let dir = scratch().join("api").join("go-checked"); let module = dir.join("module"); go_layout( api, @@ -333,20 +340,57 @@ fn api_go_race() { &std::fs::read_to_string(manifest_dir().join("tests").join("api.go")).unwrap(), ); let exe = dir.join(format!("api-test{EXE_SUFFIX}")); - run(go(&module, api, &["build", "-race", "-o"]).arg(&exe)); + run(go(&module, api, &["build", "-race", "-o"]) + .arg(&exe) + .env("GOEXPERIMENT", "cgocheck2")); // Every report fails the run. let env = [("GORACE", "halt_on_error=1")]; let output = run(Command::new(&exe).envs(env)); assert_eq!( String::from_utf8_lossy(&output.stdout), api.transcript, - "the Go transcript under the race detector differs from the native one" + "the Go transcript under the race detector and cgocheck2 differs from the native one" ); for case in ["futures", "owned", "closed", "bytes", "closures"] { run(Command::new(&exe).arg(case).envs(env)); } } +/// The C and C++ scripts under Valgrind's memcheck, against the library as the gate builds +/// it: uninstrumented, so it also sees what a sanitizer's own build of std would hide, such +/// as a read of memory nothing wrote. A definite or indirect leak fails the run too. +#[test] +#[ignore = "needs Valgrind: just ffi sanitize"] +fn api_valgrind() { + let api = api(); + let c = api_c_exe(api); + let cpp = scratch().join("api").join(format!("api-c++17{EXE_SUFFIX}")); + cxx(&api.link(), 17, true, &manifest_dir().join("tests").join("api.cpp"), &cpp); + for (exe, cases) in [(&c, &["statuses", "threads"][..]), (&cpp, &["futures", "lifetimes"])] { + for case in std::iter::once(None).chain(cases.iter().copied().map(Some)) { + let output = run(Command::new("valgrind") + .args([ + "--quiet", + "--error-exitcode=99", + "--exit-on-first-error=yes", + "--track-origins=yes", + "--leak-check=full", + "--errors-for-leak-kinds=definite,indirect", + ]) + .arg(exe) + .args(case)); + if case.is_none() { + assert_eq!( + String::from_utf8_lossy(&output.stdout), + api.transcript, + "{} under Valgrind differs from the native transcript", + exe.display() + ); + } + } + } +} + /// 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. fn sanitized(sanitizer: &str) { @@ -371,11 +415,19 @@ fn sanitized(sanitizer: &str) { .collect(); let lib = dir.join("glue").join(host()).join("debug").join("libapi.a"); let tests = manifest_dir().join("tests"); - let sanitize = format!("-fsanitize={sanitizer}"); + // UndefinedBehaviorSanitizer rides with AddressSanitizer on the C and C++ side, whose + // undefined behavior Rust's checks cannot see. `-fno-wrapv` overrides a hardened + // compiler's, such as Nix's, which would define signed overflow and so hide it. + let sanitize: &[&str] = match sanitizer { + "address" => &["-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-wrapv"], + "thread" => &["-fsanitize=thread"], + other => panic!("no C flags for the {other} sanitizer"), + }; // Every report fails the run, rather than only those the sanitizer deems fatal. let env = [ ("ASAN_OPTIONS", "detect_leaks=1:abort_on_error=1"), ("TSAN_OPTIONS", "halt_on_error=1"), + ("UBSAN_OPTIONS", "halt_on_error=1:print_stacktrace=1"), ]; for (compiler, std, src, args) in [ ("clang", "-std=c11", "api.c", &["threads"][..]), @@ -383,7 +435,9 @@ fn sanitized(sanitizer: &str) { ] { let exe = dir.join(src.replace('.', "-")); run(Command::new(compiler) - .args([std, "-g", "-Wall", "-Wextra", "-Werror", &sanitize, "-I"]) + .args([std, "-g", "-Wall", "-Wextra", "-Werror"]) + .args(sanitize) + .arg("-I") .arg(api.out.join("include")) .arg(tests.join(src)) .arg(&lib) diff --git a/flake.nix b/flake.nix index 041d46d98..deb2872ac 100644 --- a/flake.nix +++ b/flake.nix @@ -319,7 +319,10 @@ # `just ffi sanitize` builds the glue with the nightly's sanitizers, linked by clang, and # runs it under the nightly's Miri. llvm-symbolizer names the frames of a report. devShells.sanitize = self.devShells.${system}.clang.overrideAttrs (old: { - nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.llvm ]; + nativeBuildInputs = old.nativeBuildInputs ++ [ + pkgs.llvm + pkgs.valgrind + ]; RUST2_TS_TOOLCHAIN = tsToolchain.override { extensions = (builtins.fromTOML (builtins.readFile ./ts/rust-toolchain.toml)).toolchain.components From 6f8ad94b9e714af4a57c896ad7a7936510a99b91 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 03:24:45 -0800 Subject: [PATCH 03/13] Run CheckJNI on HotSpot and ART, and ART on 32-bit x86; check a closure's throw before reading its return --- .github/workflows/nightly.yml | 14 ++++++++++++-- ffi/justfile | 2 +- ffi/runtime/jni.rs | 13 ++++++++++--- ffi/src/kotlin.rs | 2 +- ffi/tests/fixtures.rs | 32 ++++++++++++++++++++++++-------- flake.nix | 5 +++-- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f28208923..7921eb22f 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -315,8 +315,9 @@ jobs: if: runner.os == 'macOS' run: just ffi test swift && just ffi moq-swift - # The Kotlin on ART, shrunk by R8 as an app shrinks it. x86_64, as moq's Android build: - # this image ships the Android SDK and NDK, and its KVM runs an x86_64 emulator at speed. + # The Kotlin on ART under CheckJNI, shrunk by R8 as an app shrinks it. x86_64, as moq's + # Android build: this image ships the Android SDK and NDK, and its KVM runs an x86_64 + # emulator at speed. ffi-android: name: Export to Kotlin on Android runs-on: ubuntu-24.04 @@ -352,6 +353,15 @@ jobs: arch: x86_64 script: nix develop .#android --command just ffi android + # A 32-bit ABI, as moq's armv7 build is, whose pointers and usize are half a long: + # x86, which this runner's KVM runs at speed. + - name: API 24, 32-bit + uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 + with: + api-level: 24 + arch: x86 + script: nix develop .#android --command just ffi android + ffi-bench: name: Bench the bindings runs-on: ubuntu-24.04-arm diff --git a/ffi/justfile b/ffi/justfile index 57930849d..563ab8580 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -45,7 +45,7 @@ shapes: browsers: cargo test --locked --test fixtures -- --ignored --exact api_browsers -# Shrink the api fixture's Kotlin with R8 and its generated rules, and run its test on the Android device adb reaches, in the Android shell (nix develop .#android) with ANDROID_HOME and ANDROID_NDK_HOME set. +# Shrink the api fixture's Kotlin with R8 and its generated rules, and run its test under CheckJNI on the Android device adb reaches, in the Android shell (nix develop .#android) with ANDROID_HOME and ANDROID_NDK_HOME set. android: cargo test --locked --test fixtures -- --ignored --exact api_android diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index 9c05fd0ed..d7cb95087 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -180,14 +180,21 @@ pub fn upcall(what: &str, f: impl FnOnce(*mut JNIEnv) -> R) -> R { fail(env, what, "a call into Kotlin is out of memory"); } let result = f(env); - if ((**env).v1_2.ExceptionCheck)(env) { - fail(env, what, "a call into Kotlin threw"); - } + threw(env, what); ((**env).v1_2.PopLocalFrame)(env, std::ptr::null_mut()); result } } +/// Abort if the call into Kotlin `f` just made threw: JNI allows no other call while an +/// exception is pending, so a call whose result is read checks this first. +pub fn threw(env: *mut JNIEnv, what: &str) { + // SAFETY: `env` is this thread's. + if unsafe { ((**env).v1_2.ExceptionCheck)(env) } { + fail(env, what, "a call into Kotlin threw"); + } +} + /// The static method `name` of the native class, whose type is `sig`, cached in `cache`. pub fn method(env: *mut JNIEnv, cache: &OnceLock, name: &CStr, sig: &CStr) -> jmethodID { *cache.get_or_init(|| { diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index dd0aab07d..d2622d4a1 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -2161,7 +2161,7 @@ impl<'a> Gen<'a> { _ => unreachable!("a callback or closure returns no {c_ok:?}"), }; let back = match thrown { - None => back, + None => format!("rt::threw(env, \"{what}\");\n\t\t{back}"), Some(_) => { let err = outs.len() - 1; let ok = match c_ok { diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 3ebfc483c..9f03ed824 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -759,7 +759,8 @@ fn api_kotlin() { 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())) + cmd.arg("-J-Xcheck:jni") + .arg(format!("-Djava.library.path={}", api.lib.display())) .arg("-cp") .arg(std::env::join_paths(classpath.iter().copied().chain([jar.as_path()])).unwrap()) .arg("ApiKt") @@ -815,9 +816,9 @@ fn api_kotlin() { } /// The same script on Android: the classes, shrunk by R8 into an app's dex with only the -/// generated rules keeping what JNI looks up, run on ART on the device adb reaches against -/// the library the NDK builds for its ABI, both for API 24, the oldest moq's Android library -/// supports. +/// generated rules keeping what JNI looks up, run on ART under CheckJNI on the device adb +/// reaches against the library the NDK builds for its ABI, both for API 24, the oldest moq's +/// Android library supports. #[test] #[ignore = "needs the Android SDK and NDK, cargo-ndk, and a device: just ffi android"] fn api_android() { @@ -835,6 +836,7 @@ fn api_android() { let target = match abi.as_str() { "arm64-v8a" => "aarch64-linux-android", "x86_64" => "x86_64-linux-android", + "x86" => "i686-linux-android", abi => panic!("the android shell's toolchain has no target for the device's ABI {abi}"), }; let out = dir.join("out"); @@ -913,7 +915,7 @@ fn api_android() { let args: String = args.iter().map(|arg| format!(" '{arg}'")).collect(); let mut cmd = Command::new(&adb); cmd.arg("shell").arg(format!( - "ANDROID_DATA={DEVICE} dalvikvm -Djava.library.path={DEVICE} -cp {DEVICE}/classes.dex ApiKt{args}" + "ANDROID_DATA={DEVICE} dalvikvm -Xcheck:jni -Djava.library.path={DEVICE} -cp {DEVICE}/classes.dex ApiKt{args}" )); cmd }; @@ -957,14 +959,15 @@ fn newest(dir: &Path) -> PathBuf { /// transcript; then its futures, what the GC frees, and each contract it fails loud on. fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { let kt = run(&mut vm(&[])); + jni_checked(&kt, "the Kotlin transcript"); assert_eq!( String::from_utf8_lossy(&kt.stdout), native, "the Kotlin transcript differs from the native one" ); - run(&mut vm(&["futures"])); - run(&mut vm(&["owned"])); - run(&mut vm(&["clash"])); + for case in ["futures", "owned", "clash"] { + jni_checked(&run(&mut vm(&[case])), case); + } for (case, message) in [ ("moved", "IllegalStateException: Counter is closed"), ("boxed moved", "IllegalStateException: Node is closed"), @@ -991,6 +994,19 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { } } +/// Asserts the run of `what` drew no warning from HotSpot's CheckJNI, which only warns of +/// a JNI call while an exception may be pending or of a local reference past its frame's +/// capacity, and on stdout. ART's CheckJNI aborts instead. +fn jni_checked(output: &Output, what: &str) { + for printed in [&output.stdout, &output.stderr] { + let printed = String::from_utf8_lossy(printed); + assert!( + !printed.contains("WARNING in native method") && !printed.contains("WARNING: JNI"), + "CheckJNI warned in {what}:\n{printed}" + ); + } +} + /// Asserts the run of `what` failed, as a broken contract must, and on Linux, where the test /// programs make themselves non-dumpable, without dumping core: each core costs the host a /// write and a run of its crash handler. Elsewhere the host's own core limit decides. diff --git a/flake.nix b/flake.nix index deb2872ac..6b8855f25 100644 --- a/flake.nix +++ b/flake.nix @@ -333,14 +333,15 @@ }); # `just ffi android` builds the Kotlin's library for an Android device, with the NDK # cargo-ndk finds through ANDROID_NDK_HOME, and `just ffi moq-api` stages moq's API on - # each Android ABI moq ships: the SDK and NDK are the caller's, as moq's Android build - # takes the GitHub runner image's. + # each Android ABI moq ships, and the x86 emulator runs the 32-bit test: the SDK and + # NDK are the caller's, as moq's Android build takes the GitHub runner image's. devShells.android = self.devShells.${system}.default.overrideAttrs (old: { nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.cargo-ndk ]; RUST2_TS_TOOLCHAIN = tsToolchain.override { targets = (builtins.fromTOML (builtins.readFile ./ts/rust-toolchain.toml)).toolchain.targets ++ [ "aarch64-linux-android" "armv7-linux-androideabi" + "i686-linux-android" "x86_64-linux-android" ]; }; From 93beab5423e1c4b8cd0ca092c6647e4d8160a6a1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 04:06:00 -0800 Subject: [PATCH 04/13] Run the Swift test under ThreadSanitizer --- ffi/justfile | 4 +-- ffi/tests/fixtures.rs | 79 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/ffi/justfile b/ffi/justfile index 563ab8580..419091e64 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -49,9 +49,9 @@ browsers: android: cargo test --locked --test fixtures -- --ignored --exact api_android -# Run the api fixture's C and C++ scripts under AddressSanitizer with UndefinedBehaviorSanitizer, ThreadSanitizer, and Valgrind, its Go script under Go's race detector and cgocheck2, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. +# Run the api fixture's C and C++ scripts under AddressSanitizer with UndefinedBehaviorSanitizer, ThreadSanitizer, and Valgrind, its Swift script under ThreadSanitizer, its Go script under Go's race detector and cgocheck2, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. sanitize: - cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_valgrind api_go_checked api_miri + cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_valgrind api_swift_tsan api_go_checked api_miri # Apply formatters without changing program behavior. fix: diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 9f03ed824..ed4339ff3 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -391,9 +391,9 @@ fn api_valgrind() { } } -/// 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. -fn sanitized(sanitizer: &str) { +/// The api fixture's glue built, std included, under the `sanitizer`: its static library, +/// and what a program linking it links besides. +fn sanitized_glue(sanitizer: &str) -> (PathBuf, Vec) { let api = api(); let dir = scratch().join("api").join(sanitizer); let built = run(cargo() @@ -413,7 +413,22 @@ fn sanitized(sanitizer: &str) { .split_whitespace() .map(str::to_string) .collect(); - let lib = dir.join("glue").join(host()).join("debug").join("libapi.a"); + (dir.join("glue").join(host()).join("debug").join("libapi.a"), native) +} + +/// Every report fails the run, rather than only those the sanitizer deems fatal. +const SANITIZER_ENV: [(&str, &str); 3] = [ + ("ASAN_OPTIONS", "detect_leaks=1:abort_on_error=1"), + ("TSAN_OPTIONS", "halt_on_error=1"), + ("UBSAN_OPTIONS", "halt_on_error=1:print_stacktrace=1"), +]; + +/// Build the api fixture's glue under the `sanitizer`, and run the C and C++ scripts +/// against it, built by clang under the same one. +fn sanitized(sanitizer: &str) { + let api = api(); + let dir = scratch().join("api").join(sanitizer); + let (lib, native) = sanitized_glue(sanitizer); let tests = manifest_dir().join("tests"); // UndefinedBehaviorSanitizer rides with AddressSanitizer on the C and C++ side, whose // undefined behavior Rust's checks cannot see. `-fno-wrapv` overrides a hardened @@ -423,12 +438,6 @@ fn sanitized(sanitizer: &str) { "thread" => &["-fsanitize=thread"], other => panic!("no C flags for the {other} sanitizer"), }; - // Every report fails the run, rather than only those the sanitizer deems fatal. - let env = [ - ("ASAN_OPTIONS", "detect_leaks=1:abort_on_error=1"), - ("TSAN_OPTIONS", "halt_on_error=1"), - ("UBSAN_OPTIONS", "halt_on_error=1:print_stacktrace=1"), - ]; for (compiler, std, src, args) in [ ("clang", "-std=c11", "api.c", &["threads"][..]), ("clang++", "-std=c++17", "api.cpp", &["futures", "lifetimes"][..]), @@ -444,14 +453,14 @@ fn sanitized(sanitizer: &str) { .args(&native) .arg("-o") .arg(&exe)); - let output = run(Command::new(&exe).envs(env)); + let output = run(Command::new(&exe).envs(SANITIZER_ENV)); assert_eq!( String::from_utf8_lossy(&output.stdout), api.transcript, "{src} under {sanitizer} differs from the native transcript" ); for arg in args { - run(Command::new(&exe).arg(arg).envs(env)); + run(Command::new(&exe).arg(arg).envs(SANITIZER_ENV)); } } } @@ -1351,6 +1360,52 @@ fn api_swift() { ); } +/// `tests/api.swift`'s script, its futures, and what releasing frees, under +/// ThreadSanitizer, against the glue built under it: it catches a race between a wake on +/// Rust's thread and the task it resumes, or an executor's job and a release. +#[test] +#[ignore = "needs the nightly's rust-src and Swift's ThreadSanitizer: just ffi sanitize"] +fn api_swift_tsan() { + let api = api(); + let (glue, native) = sanitized_glue("thread"); + let dir = scratch().join("api").join("swift-thread"); + let module = api.out.join("swift"); + let collections = swift_collections(); + let (swiftmodule, lib) = (dir.join("Api.swiftmodule"), dir.join("libApi.a")); + std::fs::create_dir_all(&dir).unwrap(); + run(swiftc(&module, 13) + .args(["-sanitize=thread", "-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(module.join("api.swift"))); + let exe = dir.join("api-test"); + run(swiftc(&module, 15) + .args(["-sanitize=thread", "-parse-as-library", "-I"]) + .arg(&dir) + .arg(manifest_dir().join("tests").join("api.swift")) + .arg(&lib) + .args(&collections) + .arg(&glue) + .args(&native) + .arg("-o") + .arg(&exe)); + // What Swift's own runtime and Dispatch do goes unchecked, as Darwin's default has it: + // they are uninstrumented, so TSan would miss the atomics that order a task's frame + // before its runtime frees it, and report that free as a race. + let env = [("TSAN_OPTIONS", "halt_on_error=1:ignore_noninstrumented_modules=1")]; + let output = run(Command::new(&exe).envs(env)); + assert_eq!( + String::from_utf8_lossy(&output.stdout), + api.transcript, + "the Swift transcript under ThreadSanitizer differs from the native one" + ); + for case in ["futures", "owned", "shared"] { + run(Command::new(&exe).arg(case).envs(env)); + } +} + /// 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 { From 9bdc2eb5f7cd3ad3cef1291c568ff9a6843eec80 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 04:12:08 -0800 Subject: [PATCH 05/13] Run the web binding's test on Bun --- ffi/README.md | 2 +- ffi/tests/api.ts | 17 +++++++++++------ ffi/tests/fixtures.rs | 17 +++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index fa002c4d9..cda401de2 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1417,7 +1417,7 @@ target there. ## Tests ```bash -just ffi test # export the fixtures, build them, and drive api from C, C++, Kotlin, Python, Swift, Go, Dart, and TypeScript on Node and Deno +just ffi test # export the fixtures, build them, and drive api from C, C++, Kotlin, Python, Swift, Go, Dart, and TypeScript on Node, Deno, and Bun just ffi moq # export moq's crates at the pinned revision, build them, and run moq/moq.c, moq/moq.cpp, moq/moq.kt, moq/moq.py, moq/moq.swift, moq/moq.go, and moq/moq.dart just ffi moq-c # the same export, built in debug, running only moq/moq.c: `just ci`'s check on moq just ffi moq-cpp # the same export, running only moq/moq.cpp diff --git a/ffi/tests/api.ts b/ffi/tests/api.ts index 9d0b4d8ec..d3da7e50f 100644 --- a/ffi/tests/api.ts +++ b/ffi/tests/api.ts @@ -1,7 +1,7 @@ // The api fixture's web test: the script fixtures/api/examples/transcript.rs runs as -// native Rust does on wasm32-wasip1, through the generated TypeScript on Node or Deno, from -// beside the binding's `web` directory, or in a browser, through tests/browser.ts's -// `process`. RUST2_WASM is the path of the module, built with the crate's `web` feature. +// native Rust does on wasm32-wasip1, through the generated TypeScript on Node, Deno, or +// Bun, from beside the binding's `web` directory, or in a browser, through +// tests/browser.ts's `process`. RUST2_WASM is the path of the module, built with the crate's `web` feature. // tests/fixtures.rs compares the transcripts. // // With an argument, it instead checks the futures' semantics, what disposing and the @@ -1304,14 +1304,19 @@ async function futures(): Promise { print("futures ok"); } -/** Node's garbage collector, which a flag exposes. */ +/** The garbage collector: Bun's own, else V8's, which a flag exposes. */ let gc: (() => void) | undefined; /** Run the garbage collector until `done()`, failing past a deadline. */ async function collect(done: () => boolean, what: string): Promise { if (gc === undefined) { - process.getBuiltinModule("node:v8").setFlagsFromString("--expose-gc"); - gc = process.getBuiltinModule("node:vm").runInNewContext("gc"); + const bun = (globalThis as { Bun?: { gc(force: boolean): void } }).Bun; + if (bun !== undefined) { + gc = () => bun.gc(true); + } else { + process.getBuiltinModule("node:v8").setFlagsFromString("--expose-gc"); + gc = process.getBuiltinModule("node:vm").runInNewContext("gc"); + } } const deadline = Date.now() + 10000; while (!done()) { diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index ed4339ff3..8d521f116 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1557,7 +1557,8 @@ fn web_checks(runtime: &str, cases: &[&str], ran: &mut dyn FnMut(Option<&str>) - } } -/// `tests/api.ts`'s script on `runtime`, Node or Deno, with `case`, from beside the binding. +/// `tests/api.ts`'s script on `runtime`, Node, Deno, or Bun, with `case`, from beside the +/// binding. fn web_script(runtime: &str, case: Option<&str>) -> Command { let web = web(); let mut cmd = Command::new(runtime); @@ -1575,15 +1576,15 @@ fn web_script(runtime: &str, case: Option<&str>) -> Command { cmd } -/// 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 -/// a closed object refuses, how bytes cross, and what a call lent bytes reads once memory -/// grows, and fails loud on each contract it breaks; a binding of another contract refuses -/// the module. +/// The generated TypeScript runs `tests/api.ts`'s script on Node, Deno, and Bun, 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 a closed object refuses, how bytes cross, and what a call lent bytes reads once +/// memory grows, and fails loud on each contract it breaks; a binding of another contract +/// refuses the module. #[test] fn api_web() { - for runtime in ["node", "deno"] { + for runtime in ["node", "deno", "bun"] { web_checks(runtime, &WEB_CASES, &mut |case| { output(&mut web_script(runtime, case)).into() }); From c82dd4d80f2d0502781e31bb09c5ee4331d8f0e7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 04:28:07 -0800 Subject: [PATCH 06/13] Loop the Python test on a debug CPython 3.15 with references and blocks flat --- ffi/justfile | 4 ++-- ffi/tests/api.py | 38 ++++++++++++++++++++++++++++++++++++-- ffi/tests/fixtures.rs | 24 +++++++++++++++++++++++- flake.nix | 6 ++++-- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/ffi/justfile b/ffi/justfile index 419091e64..1c675d76c 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -49,9 +49,9 @@ browsers: android: cargo test --locked --test fixtures -- --ignored --exact api_android -# Run the api fixture's C and C++ scripts under AddressSanitizer with UndefinedBehaviorSanitizer, ThreadSanitizer, and Valgrind, its Swift script under ThreadSanitizer, its Go script under Go's race detector and cgocheck2, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. +# Run the api fixture's C and C++ scripts under AddressSanitizer with UndefinedBehaviorSanitizer, ThreadSanitizer, and Valgrind, its Swift script under ThreadSanitizer, its Go script under Go's race detector and cgocheck2, its Python script looped on a debug CPython, and its glue's C ABI under Miri, in the sanitize shell: nix develop .#sanitize. sanitize: - cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_valgrind api_swift_tsan api_go_checked api_miri + cargo test --locked --test fixtures -- --ignored --exact api_asan api_tsan api_valgrind api_swift_tsan api_go_checked api_python_debug api_miri # Apply formatters without changing program behavior. fix: diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 43cebc3f9..b0f7d7d65 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -2,11 +2,13 @@ # natively, through the generated Python. tests/fixtures.rs compares the transcripts. # # With an argument, it instead checks the futures' semantics, what frees what nobody -# closed, the values only Python's types bound, or how Python calls into the extension, -# or breaks a contract, which must fail. +# closed, the values only Python's types bound, how Python calls into the extension, or +# under a debug CPython that all of those leak nothing, or breaks a contract, which must +# fail. import array import asyncio +import contextlib import datetime import gc import io @@ -1770,9 +1772,41 @@ def readinto(self, buffer, /): raise ValueError(f"no case {which}") +def leaks(): + """The other runs' checks, looped under a debug CPython: once two rounds warm the + interpreter, five more leave its references and memory blocks exactly as they were.""" + check(hasattr(sys, "gettotalrefcount"), "leaks runs under a debug CPython") + + def counts(): + # Printed lines wait in the stream's buffer, and attribute lookups in the + # interpreter's caches, until they fill. + sys.stdout.flush() + sys._clear_internal_caches() + gc.collect() + return sys.gettotalrefcount(), sys.getallocatedblocks() + + def rounds(n): + for _ in range(n): + for run in (script, futures, owned, values, calls): + run() + + with open(os.devnull, "w") as null, contextlib.redirect_stdout(null): + rounds(2) + first = counts() + # Each measurement held is counted by the next one. + before = counts() + rounds(5) + after = counts() + held = [b - f for b, f in zip(before, first)] + grown = [a - b for a, b in zip(after, before)] + check(grown == held, f"five rounds left the references and blocks {before} as {after}") + + match sys.argv[1:]: case []: script() + case ["leaks"]: + leaks() case ["futures"]: futures() case ["owned"]: diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 8d521f116..2c2af4936 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1072,9 +1072,31 @@ const PYTHON_LIB: &str = match (cfg!(windows), cfg!(target_os = "macos")) { /// contract it fails loud on. #[test] fn api_python() { + python_checks(&std::env::var_os("PYTHON").unwrap_or_else(|| "python3".into())); +} + +/// `api_python`'s checks on a debug build of the newest CPython, which asserts what a +/// release build trusts of the runtime's reference counting, then each of them looped, +/// leaving the interpreter's references and memory blocks as they were. +#[test] +#[ignore = "needs a debug CPython, which builds from source: just ffi sanitize"] +fn api_python_debug() { + let interpreter = std::env::var_os("RUST2_PYTHON_DEBUG") + .expect("RUST2_PYTHON_DEBUG names a debug CPython: run this in the sanitize shell"); + python_checks(&interpreter); + let output = run(Command::new(&interpreter) + .args(["-P", "-X", "dev", "-W", "error"]) + .arg(manifest_dir().join("tests").join("api.py")) + .arg("leaks") + .env("PYTHONPATH", api().out.join("python"))); + assert!(output.stdout.is_empty(), "leaks printed {}", String::from_utf8_lossy(&output.stdout)); +} + +/// `api_python`'s checks, run by `interpreter`. +fn python_checks(interpreter: &std::ffi::OsStr) { let api = api(); let python = |dir: &Path, args: &[&str]| { - let mut cmd = Command::new(std::env::var_os("PYTHON").unwrap_or_else(|| "python3".into())); + let mut cmd = Command::new(interpreter); // `-P` keeps `tests/` off the path, where `api.py` is this script; dev mode and // warnings as errors fail on what the runtime only warns of. cmd.args(["-P", "-X", "dev", "-W", "error"]) diff --git a/flake.nix b/flake.nix index 6b8855f25..3182f0bdf 100644 --- a/flake.nix +++ b/flake.nix @@ -316,13 +316,15 @@ devShells.clang = self.devShells.${system}.default.overrideAttrs (old: { nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.clang ]; }); - # `just ffi sanitize` builds the glue with the nightly's sanitizers, linked by clang, and - # runs it under the nightly's Miri. llvm-symbolizer names the frames of a report. + # `just ffi sanitize` builds the glue with the nightly's sanitizers, linked by clang, + # runs it under the nightly's Miri and Valgrind, and runs the Python on a debug build of + # the newest CPython, which nothing caches. llvm-symbolizer names a report's frames. devShells.sanitize = self.devShells.${system}.clang.overrideAttrs (old: { nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.llvm pkgs.valgrind ]; + RUST2_PYTHON_DEBUG = "${pkgs.python315.override { enableDebug = true; }}/bin/python3"; RUST2_TS_TOOLCHAIN = tsToolchain.override { extensions = (builtins.fromTOML (builtins.readFile ./ts/rust-toolchain.toml)).toolchain.components From 2a79f3553fa8e1122b084f123b827114e76fd8aa Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 04:31:32 -0800 Subject: [PATCH 07/13] Document the checkers, and finish the checkers quest --- ffi/README.md | 25 ++++++++++++++++--------- quest/m1/ffi-proofs/checkers.md | 28 ---------------------------- 2 files changed, 16 insertions(+), 37 deletions(-) delete mode 100644 quest/m1/ffi-proofs/checkers.md diff --git a/ffi/README.md b/ffi/README.md index cda401de2..54a1d5836 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1426,10 +1426,10 @@ just ffi moq-api # stage moq's API on another target into target/moq-a 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 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 .#android --command just ffi android # the Kotlin test, shrunk by R8, under CheckJNI 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 nix develop .#bench --command just ffi codspeed # after bench-build, each single-threaded case once under CodSpeed's CPU simulation, as the CodSpeed workflow runs it -nix develop .#sanitize --command just ffi sanitize # api's C and C++ scripts under ASan and TSan, its Go under the race detector, and its C ABI under Miri +nix develop .#sanitize --command just ffi sanitize # api's C and C++ scripts under ASan with UBSan, TSan, and Valgrind, its Swift under TSan, its Go under the race detector and cgocheck2, its Python looped on a debug CPython, and its C ABI under Miri ``` The `api` fixture covers each rule once, and re-exports the `dep` fixture's types, which @@ -1442,14 +1442,15 @@ runs the script through the C++ header at C++17, 20, and 23, with exceptions and at 17 and 23, builds it under Clang's `-Wconsumed -Wdangling -Werror`, each case of `tests/api_strict.cpp` failing there, then checks its futures: a wake that lands during a poll, a cancel between a wake and its poll that loses nothing, and a coroutine resumed or destroyed. `tests/api.kt` -runs the script through the Kotlin on the JVM, checks its futures the same way, that a call +runs the script through the Kotlin on the JVM under CheckJNI, where a warning fails it, checks its futures the same way, that a call after `close()` throws, that `Bytes` and their slices keep Rust's buffer, past the object or record they came from, until the GC frees it, and that calls racing `close()` or the GC read live values, and breaks each runtime check, which must throw or abort with its message. `just ffi android` runs the same on ART: the Kotlin and `tests/api.kt`, shrunk by R8 into an app's dex with only the generated rules keeping what JNI looks up, against the library built by the NDK for the -device's ABI, which ANDROID_HOME and ANDROID_NDK_HOME locate. Nightly runs it on an -x86_64 emulator at API 24, the oldest moq's Android library supports, and at API 35. +device's ABI, which ANDROID_HOME and ANDROID_NDK_HOME locate, under ART's CheckJNI. Nightly +runs it on an x86_64 emulator at API 24, the oldest moq's Android library supports, and at +API 35, and on a 32-bit x86 one at API 24, as moq's armv7 build is 32-bit. `tests/api.py` runs the script through the Python, in CPython's development mode with warnings as errors, checks its futures the same way, what frees an object or bytes nobody closed, and the values Python's types bound, and breaks each runtime check, which must raise or @@ -1461,7 +1462,8 @@ language mode and type-checked in Swift 5's, checks its futures the same way and releasing frees, and breaks each runtime check, which must trap or abort with its message, and each move or borrow the compiler checks, which must fail to compile. It runs with nixpkgs' Swift on Linux, and nightly with Apple's on macOS. -`tests/api.go` runs the script through the Go after `go vet`, checks its futures the same +`tests/api.go` runs the script through the Go after `go vet`, failing any run that leaves a +goroutine Go's `goroutineleak` profile finds, checks its futures the same way, plus a context done before the call, a deadline, and a handle closed during a call, checks that bytes cross both ways, that a `Bytes` keeps Rust's buffer while only a sub-view or a reader reaches it and that the GC frees it once none does, that the GC frees an object @@ -1489,9 +1491,14 @@ against that export's `api.json`: its `added` feature only adds and must pass, a feature breaks the API twice for each kind of break, once in an item only wasm32 has, each 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 then ThreadSanitizer, runs `tests/api.c` and `tests/api.cpp` against it, the first time +with UndefinedBehaviorSanitizer too, and `tests/api.swift` against the second, and matches +their output to the native transcript; it runs `tests/api.c` and `tests/api.cpp` against the +uninstrumented library under Valgrind's memcheck, `tests/api.go` under Go's race detector +and cgocheck2, and `tests/api.py`'s checks on a debug build of the newest CPython, then its +`leaks` case, which loops them and fails unless the interpreter's references and memory +blocks end where they began; and it drives the C ABI from `tests/miri/api.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/quest/m1/ffi-proofs/checkers.md b/quest/m1/ffi-proofs/checkers.md deleted file mode 100644 index 6045c29bd..000000000 --- a/quest/m1/ffi-proofs/checkers.md +++ /dev/null @@ -1,28 +0,0 @@ -# [M] Run every language's own checkers - -## Goal - -The nightly sanitize job, or the gate where a check is fast, adds: - -- UBSan with ASan on the C and C++ tests, and Valgrind memcheck on both against the - uninstrumented library; -- Go's `GOEXPERIMENT=cgocheck2`, and an empty `goroutineleak` profile after the futures - and cancel cases; -- CheckJNI on HotSpot (`-Xcheck:jni`) and on ART; -- the Swift test under ThreadSanitizer; -- Bun as a runtime of the web binding's test, beside Node and Deno; -- a 32-bit ART run (x86 emulator at API 24), since moq stages armv7 and only 64-bit ABIs - run today; -- the Python test looped under a debug CPython, with `sys.gettotalrefcount()` and - `sys.getallocatedblocks()` flat across iterations, and a run on CPython 3.15. - -## Plan - -Found by the 2026-11-18 language audit. `just ffi sanitize` covers C, C++, Go's race -detector, and Miri; nothing runs the others. The Python runtime does about 3,000 lines of -manual reference counting. - -Each check that finds a bug gets the fix in this PR, or a quest. Say how much each added -gate check costs. - -Public API: none. From 8ea1bd0212b7914ace8f2d9b1c95e6ce36ef6734 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 04:33:22 -0800 Subject: [PATCH 08/13] Format --- ffi/tests/fixtures.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 2c2af4936..7c1a21c94 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -365,7 +365,13 @@ fn api_valgrind() { let api = api(); let c = api_c_exe(api); let cpp = scratch().join("api").join(format!("api-c++17{EXE_SUFFIX}")); - cxx(&api.link(), 17, true, &manifest_dir().join("tests").join("api.cpp"), &cpp); + cxx( + &api.link(), + 17, + true, + &manifest_dir().join("tests").join("api.cpp"), + &cpp, + ); for (exe, cases) in [(&c, &["statuses", "threads"][..]), (&cpp, &["futures", "lifetimes"])] { for case in std::iter::once(None).chain(cases.iter().copied().map(Some)) { let output = run(Command::new("valgrind") @@ -434,7 +440,11 @@ fn sanitized(sanitizer: &str) { // undefined behavior Rust's checks cannot see. `-fno-wrapv` overrides a hardened // compiler's, such as Nix's, which would define signed overflow and so hide it. let sanitize: &[&str] = match sanitizer { - "address" => &["-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-wrapv"], + "address" => &[ + "-fsanitize=address,undefined", + "-fno-sanitize-recover=all", + "-fno-wrapv", + ], "thread" => &["-fsanitize=thread"], other => panic!("no C flags for the {other} sanitizer"), }; @@ -1089,7 +1099,11 @@ fn api_python_debug() { .arg(manifest_dir().join("tests").join("api.py")) .arg("leaks") .env("PYTHONPATH", api().out.join("python"))); - assert!(output.stdout.is_empty(), "leaks printed {}", String::from_utf8_lossy(&output.stdout)); + assert!( + output.stdout.is_empty(), + "leaks printed {}", + String::from_utf8_lossy(&output.stdout) + ); } /// `api_python`'s checks, run by `interpreter`. @@ -1396,7 +1410,14 @@ fn api_swift_tsan() { let (swiftmodule, lib) = (dir.join("Api.swiftmodule"), dir.join("libApi.a")); std::fs::create_dir_all(&dir).unwrap(); run(swiftc(&module, 13) - .args(["-sanitize=thread", "-swift-version", "6", "-parse-as-library", "-module-name", "Api"]) + .args([ + "-sanitize=thread", + "-swift-version", + "6", + "-parse-as-library", + "-module-name", + "Api", + ]) .args(["-emit-module", "-emit-module-path"]) .arg(&swiftmodule) .args(["-emit-library", "-static", "-o"]) From e8fa88c337a98b26d07c2cd7f4d0d02891244536 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 05:21:45 -0800 Subject: [PATCH 09/13] Match Android's Kotlin to the script run on the device, and size usize by the target --- ffi/tests/api.kt | 14 +++++++--- ffi/tests/fixtures.rs | 40 +++++++++++++++++++++++++--- quest/m1/ffi-export/kotlin-narrow.md | 24 +++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 quest/m1/ffi-export/kotlin-narrow.md diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 107ab47fa..1186149a9 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -919,6 +919,12 @@ fun socketHex(addr: SocketAddr): String = when (addr) { /** Whether [f] throws `IllegalArgumentException`, as a value its Rust type cannot hold does. */ fun rejects(f: () -> Any?): Boolean = runCatching(f).exceptionOrNull() is IllegalArgumentException +/** The library's `usize::MAX`, which its target's pointer width sets: 32 bits on armv7 and x86 Android. */ +fun usizeMax(): ULong = if (pointerBits(platform()) == 64u) ULong.MAX_VALUE else UInt.MAX_VALUE.toULong() + +/** The library's `isize::MIN` and `isize::MAX`. */ +fun isizeRange(): LongRange = if (pointerBits(platform()) == 64u) Long.MIN_VALUE..Long.MAX_VALUE else Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong() + /** * Each type's edge values, passed through Rust and back, as transcript.rs prints them; then * each value the Kotlin type holds and Rust's cannot, which throws, and each conversion to @@ -929,8 +935,8 @@ fun lossless() { .map { sameDuration(it) } println("lossless duration ${durations.joinToString(" ") { "${it.secs}.${it.nanos.toString().padStart(9, '0')}" }}") println("lossless u64 ${sameU64(0u)} ${sameU64(1uL shl 63)} ${sameU64(ULong.MAX_VALUE)}") - println("lossless usize ${sameUsize(0u)} ${sameUsize(ULong.MAX_VALUE)}") - println("lossless isize ${sameIsize(Long.MIN_VALUE)} ${sameIsize(Long.MAX_VALUE)}") + println("lossless usize ${sameUsize(0u)} ${sameUsize(usizeMax())}") + println("lossless isize ${sameIsize(isizeRange().first)} ${sameIsize(isizeRange().last)}") val two = java.math.BigInteger.valueOf(2) val wides = listOf(java.math.BigInteger.ZERO, two.pow(64), two.pow(128) - java.math.BigInteger.ONE) println("lossless u128 ${wides.joinToString(" ") { sameU128(it).toString(16).padStart(32, '0') }}") @@ -1356,7 +1362,7 @@ fun readings() { for (n in listOf(1u, 2u, 3u, 127u)) { val r = reading(n)!! check(r.debug() == readingDebug(n), "reading $n crosses as Rust holds it: ${r.debug()}") - check(r.count == ULong.MAX_VALUE - n && r.len == ULong.MAX_VALUE - n && r.tag == (UShort.MAX_VALUE - n.toUShort()).toUShort(), "reading $n's unsigned scalars") + check(r.count == ULong.MAX_VALUE - n && r.len == usizeMax() - n && r.tag == (UShort.MAX_VALUE - n.toUShort()).toUShort(), "reading $n's unsigned scalars") check(r.at.x == Int.MIN_VALUE + n.toInt() && r.offset == (Byte.MIN_VALUE + n.toInt()).toByte(), "reading $n's signed scalars") check(r.level == -n.toFloat() / 2 && r.ratio == Double.MAX_VALUE / n.toDouble() && r.mark == 0x10FFFF - n.toInt(), "reading $n's floats and char") check(r.ok == (n % 2u == 1u) && r.shape == (if (n % 2u == 1u) Shape.CIRCLE else Shape.SQUARE) && r.note == "reading $n", "reading $n's bool, enum, and text") @@ -1403,7 +1409,7 @@ fun owned() { check(runCatching { token.idGet }.exceptionOrNull()?.message == "Token is closed", "a call after close throws") // The file serves wasm32 too: what only wasm32 has throws on the host's library. - check(pointerBits(platform()) == 64u && pointerBits(Platform.Native(64u)) == 64u, "the host is native") + check(platform() is Platform.Native && pointerBits(Platform.Native(64u)) == 64u, "the host is native") check(isFamily(Family.NATIVE), "and of the native family") for ((what, call) in listOf Unit>>( "api_wasm_only" to { wasmOnly() }, diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 7c1a21c94..75b02f76f 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -837,12 +837,11 @@ fn api_kotlin() { /// The same script on Android: the classes, shrunk by R8 into an app's dex with only the /// generated rules keeping what JNI looks up, run on ART under CheckJNI on the device adb /// reaches against the library the NDK builds for its ABI, both for API 24, the oldest moq's -/// Android library supports. +/// Android library supports, and matched to the script run natively there. #[test] #[ignore = "needs the Android SDK and NDK, cargo-ndk, and a device: just ffi android"] fn api_android() { const DEVICE: &str = "/data/local/tmp/rust2-ffi"; - let api = api(); let sdk = PathBuf::from(std::env::var_os("ANDROID_HOME").expect("ANDROID_HOME names the Android SDK")); let adb = sdk.join("platform-tools").join("adb"); let dir = scratch().join("api").join("android"); @@ -876,6 +875,32 @@ fn api_android() { .args(["--features", "kotlin"]) .env("RUSTFLAGS", "-D warnings")); + // The fixture's script, built for the device as the library is, whose run there is what + // the Kotlin must print: a 32-bit ABI's `usize` differs from the host's. + let transcript = dir.join("transcript"); + run(cargo() + .args([ + "ndk", + "--target", + &abi, + "--platform", + "24", + "build", + "--quiet", + "--locked", + ]) + .args([ + "--package", + "api-fixture", + "--example", + "transcript", + "--features", + "doubled", + ]) + .arg("--target-dir") + .arg(&transcript) + .current_dir(manifest_dir())); + // The app: the script's entry point, kept as an app keeps its own, and everything it // reaches, which R8 shrinks and renames, keeping only what the rules keep: the // generated ones, and those kotlinx-coroutines' jar holds, which an app's build reads @@ -929,7 +954,16 @@ fn api_android() { .arg("push") .arg(dex.join("classes.dex")) .arg(dir.join("lib").join(&abi).join("libapi.so")) + .arg( + transcript + .join(target) + .join("debug") + .join("examples") + .join("transcript"), + ) .arg(DEVICE)); + let native = run(Command::new(&adb).args(["shell", &format!("{DEVICE}/transcript")])); + let native = String::from_utf8_lossy(&native.stdout); let art = |args: &[&str]| { let args: String = args.iter().map(|arg| format!(" '{arg}'")).collect(); let mut cmd = Command::new(&adb); @@ -938,7 +972,7 @@ fn api_android() { )); cmd }; - kotlin(&api.transcript, &art); + kotlin(&native, &art); } /// kotlinc compiling the generated Kotlin with `tests/api.kt` into `dest`. diff --git a/quest/m1/ffi-export/kotlin-narrow.md b/quest/m1/ffi-export/kotlin-narrow.md new file mode 100644 index 000000000..5096431ed --- /dev/null +++ b/quest/m1/ffi-export/kotlin-narrow.md @@ -0,0 +1,24 @@ +# [S] Kotlin: a usize too wide for a 32-bit target throws + +## Goal + +On a 32-bit Android ABI (armv7, x86), Kotlin passing a `ULong` past `usize::MAX`, or a +`Long` outside `isize`'s range, throws `IllegalArgumentException`, as every other value +Rust's type cannot hold does, and leaves the call unmade. Today the glue aborts the +process. This covers a parameter, a field of a record crossing in, and a closure's return. +`tests/api.kt`'s `lossless()` checks each one on the 32-bit emulator that the nightly runs. + +## Plan + +Found by the checkers quest's review +([kixel-ai/rust2#891](https://github.com/kixel-ai/rust2/pull/891)). Kotlin crosses +`usize` as a 64-bit `Long` on every target, since one binding serves them all. The glue +narrows it with `rt::narrow` (`ffi/runtime/jni.rs:1241`), which calls `rt::fail`. The +repository's rules allow an abort only where the language has no error of its own. + +Recommended: Kotlin checks the width before the call, against the library's pointer width +read once at load, as it already checks a `Duration`'s nanoseconds. Narrowing inside the +glue cannot unwind out of a nested `Wire::read`. The other bindings use a native type the +width of the target's `usize`, so they need nothing. + +Public API: none. A behavior change: an abort becomes an exception. From c54da68a586810116887da14b06e6aea6d942767 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 05:26:11 -0800 Subject: [PATCH 10/13] Name a failed run's exit status, and plan the arm sanitizers --- ffi/tests/fixtures.rs | 3 ++- quest/m1/ffi-proofs/arm-sanitizers.md | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 quest/m1/ffi-proofs/arm-sanitizers.md diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 75b02f76f..f0c4e61c3 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -68,7 +68,8 @@ fn output(cmd: &mut Command) -> Output { fn check(cmd: &Command, output: &Output) { assert!( output.status.success(), - "{cmd:?} failed:\n{}{}", + "{cmd:?} failed with {}:\n{}{}", + output.status, String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); diff --git a/quest/m1/ffi-proofs/arm-sanitizers.md b/quest/m1/ffi-proofs/arm-sanitizers.md new file mode 100644 index 000000000..4ed9f5c31 --- /dev/null +++ b/quest/m1/ffi-proofs/arm-sanitizers.md @@ -0,0 +1,26 @@ +# [S] The nightly sanitizers run on the arm runner + +## Goal + +The nightly's `ffi-sanitize` job passes on `ubuntu-24.04-arm`. Today `api_asan`, +`api_tsan`, and `api_swift_tsan` fail there on their first program run, which prints +nothing. Each one passes on x86_64 Linux. + +## Plan + +Found by the checkers quest +([kixel-ai/rust2#891](https://github.com/kixel-ai/rust2/pull/891)). The job has failed on +every nightly since it was added on 2026-11-14, for example +[run 44339014570](https://github.com/kixel-ai/rust2/actions/runs/44339014570). Valgrind, +Miri, Go's race detector, and the debug CPython in the same job pass. + +A failed run now names its exit status (`check` in `ffi/tests/fixtures.rs`). Read that +first. The leading suspect is the runner kernel's ASLR entropy (`vm.mmap_rnd_bits`): it +breaks the sanitizer runtimes' shadow layouts, and the common fix lowers it with `sysctl` +in the job. The other suspect is a mismatch between nixpkgs clang's compiler-rt and the +nightly rustc's instrumentation on aarch64. Fix the cause, not the symptom. Running on +x86_64 instead would hide it. + +## Hosts + +- linux - the failure is on aarch64 Linux; reproduce on an arm64 Linux host or the runner From 325c04b8f8dcb730272579abe5a64264faa13d06 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 11:15:58 -0800 Subject: [PATCH 11/13] Narrow the kotlin-narrow follow-up to a closure's return, which already-checked arguments leave Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-export/kotlin-narrow.md | 47 ++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/quest/m1/ffi-export/kotlin-narrow.md b/quest/m1/ffi-export/kotlin-narrow.md index 5096431ed..2bb86e163 100644 --- a/quest/m1/ffi-export/kotlin-narrow.md +++ b/quest/m1/ffi-export/kotlin-narrow.md @@ -1,24 +1,45 @@ -# [S] Kotlin: a usize too wide for a 32-bit target throws +# [S] Kotlin: a closure's usize too wide for a 32-bit target throws ## Goal -On a 32-bit Android ABI (armv7, x86), Kotlin passing a `ULong` past `usize::MAX`, or a -`Long` outside `isize`'s range, throws `IllegalArgumentException`, as every other value -Rust's type cannot hold does, and leaves the call unmade. Today the glue aborts the -process. This covers a parameter, a field of a record crossing in, and a closure's return. -`tests/api.kt`'s `lossless()` checks each one on the 32-bit emulator that the nightly runs. +On a 32-bit Android ABI (armv7, x86), a Kotlin closure or callback that returns a `ULong` +past `usize::MAX`, or a `Long` outside `isize`'s range, raises `IllegalArgumentException` +at the Kotlin call that reached the closure, as an out-of-range argument does, instead of +aborting the process. `tests/api.kt`'s `lossless()` checks it on the 32-bit emulator that +the nightly runs. ## Plan Found by the checkers quest's review ([kixel-ai/rust2#891](https://github.com/kixel-ai/rust2/pull/891)). Kotlin crosses -`usize` as a 64-bit `Long` on every target, since one binding serves them all. The glue -narrows it with `rt::narrow` (`ffi/runtime/jni.rs:1241`), which calls `rt::fail`. The -repository's rules allow an abort only where the language has no error of its own. +`usize` as a 64-bit `Long` on every target, since one binding serves them all. -Recommended: Kotlin checks the width before the call, against the library's pointer width -read once at load, as it already checks a `Duration`'s nanoseconds. Narrowing inside the -glue cannot unwind out of a nested `Wire::read`. The other bindings use a native type the -width of the target's `usize`, so they need nothing. +Already done: `to_signed` (`ffi/src/kotlin.rs`) wraps every `usize` and `isize` that +Kotlin hands over, whether a parameter, a record field, an encoded value, or a closure's +return, in `_Native.__usize` or `__isize` (`ffi/runtime/rt.kt`). Those helpers check the +value against the library's `usize::MAX`, which is read once at load through +`__usize_max`, and throw `__Invalid`, an `IllegalArgumentException`, before the call +crosses. So an argument or field out of range already throws and leaves the call unmade, +and `rt::narrow` (`ffi/runtime/jni.rs`) never fails for a value Kotlin passes. + +Left: a closure's return. There `__usize` throws inside the upcall, and `rt::upcall`'s +`rt::threw` aborts on the pending exception, as it does for anything a closure with no +`Result` return throws. Another width check cannot fix this. The exception has to cross +the Rust frames between the upcall and the outer Kotlin call. One way: the upcall leaves +the exception pending, returns a zeroed value, and marks the thread so that each Rust +frame between it and the outermost JNI entry returns at once. That entry then lets JNI +rethrow. Without unwinding, the Rust code in between would keep running on a value the +closure never returned. That breaks the source's semantics, which is why abort is today's +behavior. Public API: none. A behavior change: an abort becomes an exception. + +## Waiting + +- Maintainer: keep or drop this quest. Every closure that throws aborts today, because a + Rust caller of a closure with no `Result` return has no error path, and the repository's + rules allow an abort where the language has no error of its own. An out-of-range + return is one more such throw. Keeping it means unwinding a Kotlin exception through + Rust frames, a far larger change than `[S]`, and it should then cover every closure + throw. Recommended: drop it, since the abort follows the rules and only a closure + returning a usize too wide for a 32-bit target reaches it. From 562b8cfa81bf1b0bdeca61f62f381f77ba461495 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 19:41:39 -0800 Subject: [PATCH 12/13] Format Co-Authored-By: Claude Opus 5.5 --- ffi/tests/fixtures.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index cffa0b749..215a52b35 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -496,14 +496,11 @@ fn api_valgrind() { generated: Some(cpp_transcript()), ..api.link() }; - cxx( - &link, - 17, - true, - &manifest_dir().join("tests").join("api.cpp"), - &cpp, - ); - for (exe, cases) in [(&c, &["statuses", "threads"][..]), (&cpp, &["futures", "lifetimes", "checks"])] { + cxx(&link, 17, true, &manifest_dir().join("tests").join("api.cpp"), &cpp); + for (exe, cases) in [ + (&c, &["statuses", "threads"][..]), + (&cpp, &["futures", "lifetimes", "checks"]), + ] { for case in std::iter::once(None).chain(cases.iter().copied().map(Some)) { let output = run(Command::new("valgrind") .args([ From 336930f7748ec31990a72ae319d4fad2a77d85c2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 20:12:52 -0800 Subject: [PATCH 13/13] Give the debug CPython and Swift TSan tests main's generated scripts Co-Authored-By: Claude Opus 5.5 --- ffi/tests/fixtures.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 215a52b35..e79904cc5 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -1485,7 +1485,10 @@ fn api_python_debug() { .args(["-P", "-X", "dev", "-W", "error"]) .arg(manifest_dir().join("tests").join("api.py")) .arg("leaks") - .env("PYTHONPATH", api().out.join("python"))); + .env( + "PYTHONPATH", + std::env::join_paths([api().out.join("python"), python_transcript()]).unwrap(), + )); assert!( output.stdout.is_empty(), "leaks printed {}", @@ -1493,10 +1496,16 @@ fn api_python_debug() { ); } +/// The directory of the Python script generated from the transcript, which `tests/api.py` +/// imports. +fn python_transcript() -> PathBuf { + generated("python", "transcript.py", &transcript::python::emit(transcript())) +} + /// `api_python`'s checks, run by `interpreter`. fn python_checks(interpreter: &std::ffi::OsStr) { let api = api(); - let scripts = generated("python", "transcript.py", &transcript::python::emit(transcript())); + let scripts = python_transcript(); let python = |dir: &Path, args: &[&str]| { let mut cmd = Command::new(interpreter); // `-P` keeps `tests/` off the path, where `api.py` is this script; dev mode and @@ -1756,8 +1765,7 @@ fn api_swift() { // The test's executor preference is macOS 15's. let exe = dir.join("api-test"); let test = manifest_dir().join("tests").join("api.swift"); - let script = - generated("swift", "transcript.swift", &transcript::swift::emit(transcript())).join("transcript.swift"); + let script = swift_transcript(); // The Clang module the test and the contract program import on Linux. let nodump = manifest_dir().join("tests").join("nodump"); let step = Step { @@ -1915,6 +1923,7 @@ fn api_swift_tsan() { .args(["-sanitize=thread", "-parse-as-library", "-I"]) .arg(&dir) .arg(manifest_dir().join("tests").join("api.swift")) + .arg(swift_transcript()) .arg(&lib) .args(&collections) .arg(&glue) @@ -1931,11 +1940,16 @@ fn api_swift_tsan() { api.transcript, "the Swift transcript under ThreadSanitizer differs from the native one" ); - for case in ["futures", "owned", "shared"] { + for case in ["checks", "futures", "owned", "shared"] { run(Command::new(&exe).arg(case).envs(env)); } } +/// The Swift script generated from the transcript, which `tests/api.swift` calls. +fn swift_transcript() -> PathBuf { + generated("swift", "transcript.swift", &transcript::swift::emit(transcript())).join("transcript.swift") +} + /// The api fixture's Swift, built as the module `Api` in Swift 6's language mode. struct SwiftApi { dir: PathBuf,