Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
015568b
chore(quest): claim quest/m1/ffi-proofs/checkers.md
kixelated Nov 19, 2026
5386f6a
Run UBSan, Valgrind, cgocheck2, and Go's leak profile on the api fixture
kixelated Nov 19, 2026
6f8ad94
Run CheckJNI on HotSpot and ART, and ART on 32-bit x86; check a closu…
kixelated Nov 19, 2026
93beab5
Run the Swift test under ThreadSanitizer
kixelated Nov 19, 2026
9bdc2eb
Run the web binding's test on Bun
kixelated Nov 19, 2026
c82dd4d
Loop the Python test on a debug CPython 3.15 with references and bloc…
kixelated Nov 19, 2026
2a79f35
Document the checkers, and finish the checkers quest
kixelated Nov 19, 2026
8ea1bd0
Format
kixelated Nov 19, 2026
e8fa88c
Match Android's Kotlin to the script run on the device, and size usiz…
kixelated Nov 19, 2026
c54da68
Name a failed run's exit status, and plan the arm sanitizers
kixelated Nov 19, 2026
325c04b
Narrow the kotlin-narrow follow-up to a closure's return, which alrea…
kixelated Nov 19, 2026
ffc6d00
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-proofs/c…
kixelated Nov 19, 2026
cca84c0
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-proofs/c…
kixelated Nov 19, 2026
05e1a95
Merge origin/main into quest/m1/ffi-proofs/checkers; record the follo…
kixelated Nov 20, 2026
d52a0ab
Merge origin/main; check CheckJNI's warnings in Kotlin's failing case…
kixelated Nov 20, 2026
7304bab
Merge origin/main; the generated Kotlin script reads usize and isize …
kixelated Nov 20, 2026
562b8cf
Format
kixelated Nov 20, 2026
336930f
Give the debug CPython and Swift TSan tests main's generated scripts
kixelated Nov 20, 2026
e5df715
Merge origin/main; the debug CPython's leak loop runs main's idioms c…
kixelated Nov 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ 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, and the library's abort messages
# and 16 KB alignment. x86_64, as moq's Android build:
# The Kotlin on ART under CheckJNI, shrunk by R8 as an app shrinks it, and the library's
# abort messages and 16 KB alignment. 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
Expand Down Expand Up @@ -402,6 +402,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

# An image whose kernel pages by 16 KB, as Google Play requires of 64-bit libraries from
# 2027-02-01: the library must load there, and its segments must align to that.
- name: API 35, 16 KB pages
Expand Down
35 changes: 21 additions & 14 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ packs the api fixture's with npm, installs it into a project of its own, type-ch
## 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
Expand All @@ -1529,10 +1529,10 @@ just ffi corpus # census, export, and bind each crate of corpus/crates.txt thr
just ffi corpus-update # take corpus/crates.txt afresh from crates.io's top crates, and resolve corpus/Cargo.lock
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, Firefox, and on Linux WebKit, lowering `using` three ways for WebKit, 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
Expand All @@ -1551,19 +1551,20 @@ 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, that calls racing `close()` or the GC
read live values, that the GC frees an object never closed left in an idle nursery or
pushed out of one, and that each object of many threads' returns, closes, and drops is
freed once, 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. It also reads the library's
segment alignment with `readelf`, which must be 16 KB, and checks that a failed check and a
panic reach logcat and bionic's abort message, which the tombstone shows. Nightly runs it on an
x86_64 emulator at API 24, the oldest moq's Android library supports, at API 35, and at
API 35 on an image whose kernel pages by 16 KB.
device's ABI, which ANDROID_HOME and ANDROID_NDK_HOME locate, under ART's CheckJNI. It also
reads the library's segment alignment with `readelf`, which must be 16 KB, and checks that a
failed check and a panic reach logcat and bionic's abort message, which the tombstone shows.
Nightly runs it on an x86_64 emulator at API 24, the oldest moq's Android library supports, at
API 35, and at API 35 on an image whose kernel pages by 16 KB, 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
Expand All @@ -1576,7 +1577,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
Expand Down Expand Up @@ -1647,10 +1649,15 @@ 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` and the protocol scripts from
`tests/miri/protocol.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` and the
protocol scripts from `tests/miri/protocol.rs` under Miri with strict provenance. The
protocol scripts' C driver runs under each sanitizer too.
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
Expand Down
6 changes: 3 additions & 3 deletions ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ protocol:
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

# 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 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_go_race 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:
Expand Down
13 changes: 10 additions & 3 deletions ffi/runtime/jni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,21 @@ pub fn upcall<R>(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<usize>, name: &CStr, sig: &CStr) -> jmethodID {
*cache.get_or_init(|| {
Expand Down
2 changes: 1 addition & 1 deletion ffi/src/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,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 {
Expand Down
15 changes: 15 additions & 0 deletions ffi/tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/netip"
"os"
"runtime"
"runtime/pprof"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -1435,4 +1436,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)
}
}
10 changes: 8 additions & 2 deletions ffi/tests/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ fun check(ok: Boolean, what: String) {
/** 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()

fun script() = transcript()

// A channel's futures, on a dispatcher the script drains, so it sees the receive pend and
Expand Down Expand Up @@ -789,7 +795,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")
Expand Down Expand Up @@ -840,7 +846,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<Pair<String, () -> Unit>>(
"api_wasm_only" to { wasmOnly() },
Expand Down
36 changes: 35 additions & 1 deletion ffi/tests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#
# With an argument, it instead checks the futures' semantics, what frees what nobody
# closed, the values only Python's types bound, how Python calls into the extension, or
# what Python code expects of a package, or breaks a contract, which must fail.
# what Python code expects of a package, under a debug CPython that all of those leak
# nothing, or breaks a contract, which must fail.

import array
import asyncio
import contextlib
import copy
import dataclasses
import datetime
Expand Down Expand Up @@ -1378,9 +1380,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, checks, futures, owned, values, calls, idioms):
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"]:
Expand Down
13 changes: 9 additions & 4 deletions ffi/tests/api.ts
Original file line number Diff line number Diff line change
@@ -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 TypeScript tests/fixtures/transcript
// writes from it, transcript.ts, which calls the functions exported here that
// transcript.rs defines. It runs on Node or Deno, from beside the binding's `web`
// transcript.rs defines. It runs 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; without it, the script loads the
// module its package holds, as `load` does by default. tests/fixtures.rs compares the
Expand Down Expand Up @@ -601,14 +601,19 @@ async function futures(): Promise<void> {
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<void> {
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()) {
Expand Down
Loading