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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ffi/moq/moq.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ final class Queue: TaskExecutor, @unchecked Sendable {
}
}

/// Run `body` as a task that prefers this queue, counting an error it throws as a failure.
func launch(_ body: @escaping @Sendable () async throws -> Void) {
_ = Task(executorPreference: self, operation: body)
Task(executorPreference: self) {
do {
try await body()
} catch {
expect(false, "a launched task threw \(error)")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
}
}

Expand Down
53 changes: 38 additions & 15 deletions ffi/tests/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

// What a function marked so holds lives in its own frame: the Android test keeps it from
// R8's inlining, since ART's interpreter keeps every reference a frame's registers last
// held reachable until the frame returns, which would keep it from the GC checks after.
annotation class OwnFrame

// The Log callback: keeps even levels.
class PrintLog : Log, AutoCloseable {
override fun log(level: UByte, line: String): Boolean {
Expand Down Expand Up @@ -914,7 +919,7 @@ fun futures() {
run {
val (job, weak) = idle(scope)
q.drain()
check(weak.get() != null, "a closure a pending future keeps stays held")
check(held(weak), "a closure a pending future keeps stays held")
job.cancel()
q.drain()
released(weak, "a closure a cancelled future kept is released")
Expand All @@ -923,24 +928,30 @@ fun futures() {
}

// A closure Spawned keeps, which returns [n], once Spawned ran it.
@OwnFrame
fun spawned(n: UInt): java.lang.ref.WeakReference<Any> {
val task = { n }
check(Spawned.new(task).join() == n, "a kept closure runs on Rust's thread")
return java.lang.ref.WeakReference(task)
}

// A drive whose closure waits for a wake that never comes, launched on [scope].
@OwnFrame
fun idle(scope: CoroutineScope): Pair<kotlinx.coroutines.Job, java.lang.ref.WeakReference<Any>> {
val start = now()
val poll = { _: Instant, _: Waiter -> start.let { null } }
val job = scope.launch { drive(start, poll) }
return Pair(job, java.lang.ref.WeakReference(poll))
}

// Whether [weak] still reaches its value, which never reaches the caller's frame.
@OwnFrame
fun held(weak: java.lang.ref.WeakReference<*>): Boolean = weak.get() != null
Comment thread
kixelated marked this conversation as resolved.
Resolved

// Collect until [weak] is gone, as once Rust lets go of the closure it held.
fun released(weak: java.lang.ref.WeakReference<*>, what: String) {
val deadline = System.nanoTime() + 10.seconds.inWholeNanoseconds
while (weak.get() != null) {
while (held(weak)) {
check(System.nanoTime() < deadline, what)
Runtime.getRuntime().gc()
Thread.sleep(1)
Expand Down Expand Up @@ -1103,21 +1114,23 @@ fun reads(bytes: Bytes, value: Byte, what: String) {
}
}

// In its own frame, so nothing on the caller's keeps them reachable. The bytes are long
// enough to be lent.
// Never closed, and long enough to be lent.
@OwnFrame
fun unclosed() {
Token.new(3u)
tokenBytes(4u, 1025u)
}

// A slice of Rust's bytes, made in its own frame so nothing keeps the Bytes it came from,
// which keeps Rust's buffer while it reads.
// A slice of Rust's bytes, made in a frame of its own so nothing keeps the Bytes it came
// from, which keeps Rust's buffer while it reads.
@OwnFrame
fun keptBySlice(freed: UInt) {
val view = sliced(5u)
uncollected(freed, "a slice keeps Rust's bytes")
check(view.size == 10 && view.toByteArray().all { it == 5.toByte() }, "a slice reads Rust's bytes once the Bytes it came from are gone")
}

@OwnFrame
fun sliced(id: UInt): Bytes = tokenBytes(id, 1025u).slice(1015)

// Frees racing calls: a close racing calls on other threads, and the GC freeing what a
Expand Down Expand Up @@ -1162,31 +1175,42 @@ fun raced() {
}
var rounds = 0u
while (cycles.get() < 50) {
val id = rounds % 100u + 1u
check(tokenBytes(id, 2048u)[2047] == id.toByte(), "a read by index races the GC")
check(tokenBytes(id, 2048u).slice(1024).toByteArray().all { it == id.toByte() }, "a slice's copy races the GC")
check(Token.new(id).idGet == id, "a call on an object races the GC")
racedRound(rounds % 100u + 1u)
rounds++
}
collecting.set(false)
collector.join()
collected(before + 3u * rounds, "the GC frees what each call raced")
}

// Calls on values nothing else reaches once it returns, holding [id], racing the GC.
@OwnFrame
fun racedRound(id: UInt) {
check(tokenBytes(id, 2048u)[2047] == id.toByte(), "a read by index races the GC")
check(tokenBytes(id, 2048u).slice(1024).toByteArray().all { it == id.toByte() }, "a slice's copy races the GC")
check(Token.new(id).idGet == id, "a call on an object races the GC")
}

// At most 64 MiB of Rust's bytes are lent at once, freed or not, since the GC sees none of
// them: past that, bytes cross copied, freed at the call, until the GC frees what was lent.
// First, so no bytes an earlier step lent count against it.
fun budgeted() {
val before = dropped()
lentToBudget(before)
collected(before + 65u, "the GC frees bytes lent up to the budget")
check(tokenBytes(3u, 2048u)[2047] == 3.toByte() && dropped() == before + 65u, "bytes cross lent again once the GC frees them")
lentAgain(before + 65u)
collected(before + 66u, "the GC frees bytes lent once the budget is free")
}

// Bytes lent once the GC freed those that filled the budget, with Rust's drops still [freed].
@OwnFrame
fun lentAgain(freed: UInt) {
check(tokenBytes(3u, 2048u)[2047] == 3.toByte() && dropped() == freed, "bytes cross lent again once the GC frees them")
}

// Rust's bytes either side of the copy limit, returning the drops once the call has freed
// those it copied. In its own frame, which the Android test keeps from R8's inlining, as
// ART's interpreter keeps the last ones reachable from the caller's until it returns.
// those it copied.
@OwnFrame
fun lentAround(): UInt {
var freed = dropped()
for (n in 1023..1025) {
Expand All @@ -1198,8 +1222,7 @@ fun lentAround(): UInt {
return freed
}

// In its own frame, which the Android test keeps from R8's inlining, so nothing on the
// caller's keeps them reachable.
@OwnFrame
fun lentToBudget(before: UInt) {
val mib = 1 shl 20
val held = List(64) { tokenBytes(1u, mib.toUInt()) }
Expand Down
6 changes: 3 additions & 3 deletions ffi/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ fn api_android() {
// 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
// from it. Kotlin's annotations are on an app's classpath, not in its dex. The
// script's functions that hold lent bytes in their own frame are kept too, as R8 would
// inline them into their caller, whose frame ART's interpreter keeps reaching the bytes.
// script's functions marked `OwnFrame` are kept too, as R8 would inline them into their
// caller, whose frame ART's interpreter keeps reaching what they held.
let jar = dir.join("app.jar");
run(kotlinc(&out, &jar).arg("-include-runtime"));
run(Command::new("jar")
Expand All @@ -634,7 +634,7 @@ fn api_android() {
std::fs::write(
&app,
format!(
"-keep class ApiKt {{ public static void main(java.lang.String[]); static *** lentAround(); static *** lentToBudget*(...); }}\n-dontwarn org.jetbrains.annotations.**\n-printusage {}\n",
"-keep class ApiKt {{ public static void main(java.lang.String[]); @OwnFrame static <methods>; }}\n-dontwarn org.jetbrains.annotations.**\n-printusage {}\n",
dir.join("usage.txt").display()
),
)
Expand Down
8 changes: 2 additions & 6 deletions quest/m0/ffi-moq-gate.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ adds to the gate, warm and cold.
## Plan

Decided with the maintainer on 2026-11-12, in the quest audit: ✅ in m0. Today the
exporter's check on its first consumer runs only nightly, where it was red on 2026-11-12
([the nightly](/quest/m0/nightly-red.md)), while the experiment's runs on every merge.
exporter's check on its first consumer runs only nightly, where it was red on 2026-11-12,
while the experiment's runs on every merge.

- The nightly's full export step takes about 13m40s, too slow for the gate. Find the
cheapest step that still fails on a real break. Recommended: export the stand-in facade
Expand All @@ -22,7 +22,3 @@ exporter's check on its first consumer runs only nightly, where it was red on 20
without it.

Public API: none.

## Required

- [The nightly passes again](/quest/m0/nightly-red.md) - a gate added while the export is red would block every exporter merge
33 changes: 0 additions & 33 deletions quest/m0/nightly-red.md

This file was deleted.