From 7a2a6b0927d660667fc4146541d550b076e80bd0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 22:06:09 -0800 Subject: [PATCH 1/2] chore(quest): claim quest/m1/ffi-export/kotlin-moved-snapshot.md Quest-Claim: 85f4f619dcf7bc25 From 1c4f56a011cf6dc607f9b739dd214d2c88e488e7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 22:21:04 -0800 Subject: [PATCH 2/2] fix(ffi): a Kotlin call closes the handles it moved, not those its argument holds after The writer marks each handle it encodes whose value the call takes, and abandons those once the call returns or unwinds, instead of walking the argument again: a callback that changed a list argument meanwhile no longer closes the wrong handles. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.kt | 23 ++++++++ ffi/src/kotlin.rs | 60 +++++++------------- ffi/tests/api.kt | 25 ++++++++ quest/m1/ffi-export/kotlin-moved-snapshot.md | 27 --------- 4 files changed, 70 insertions(+), 65 deletions(-) delete mode 100644 quest/m1/ffi-export/kotlin-moved-snapshot.md diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 765a72f2b..df62165c1 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -238,6 +238,29 @@ class __Writer { held.add(v) } + // Whether each of [held] is one [moved] encoded, up to the last it did. + private var moves: BooleanArray? = null + + /** An object's handle whose value the call takes, as [handle] encodes it, which [abandon] closes. */ + fun moved(v: __Handle?) { + handle(v) + if (v == null) return + val at = held!!.size - 1 + var moves = moves ?: BooleanArray(slots.size) + if (moves.size <= at) moves = moves.copyOf(slots.size) + moves[at] = true + this.moves = moves + } + + /** + * Abandon each handle [moved] encoded, once the call took their values: those encoded, + * not those the value holds now, as a callback may have changed it. + */ + fun abandon() { + val moves = moves ?: return + held?.forEachIndexed { i, h -> if (i < moves.size && moves[i]) h.abandon() } + } + /** * The encoding, then where [refs] names any, the place in it of each text, in order, and * how many, which Rust reads first. diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index daf6aa745..903fb52a0 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -899,6 +899,12 @@ impl<'a> Gen<'a> { /// Kotlin statements writing the Kotlin value `e` of type `ty` into the writer `w`. fn write_value(&mut self, ty: &Type, w: &str, e: &str) -> String { + self.encode(ty, w, e, false) + } + + /// [`Self::write_value`], marking in `w` each handle whose value the call takes if `moves`, + /// for `{w}.abandon()` to close those once it returns, not whatever `e` holds by then. + fn encode(&mut self, ty: &Type, w: &str, e: &str, moves: bool) -> String { let abi = self.abi; match ty { Type::Bool => format!("{w}.bool({e})"), @@ -924,36 +930,40 @@ impl<'a> Gen<'a> { Type::List(..) if let Some(inner) = ty.block() => format!("{w}.block({})", block(inner).signed(e)), Type::List(_, inner) => { let (l, v) = (self.fresh("l"), self.fresh("v")); - let item = self.write_value(inner, w, &v); + let item = self.encode(inner, w, &v, moves); format!("{e}.let {{ {l} -> {w}.i32({l}.size); for ({v} in {l}) {{ {item} }} }}") } Type::Option(inner) => match inner.base() { + Type::Object(_) if moves && moved(abi, inner) => format!("{w}.moved({e}?.handle)"), Type::Object(_) => format!("{w}.handle({e}?.handle)"), - base if Abi::is_buf(base) => self.write_value(base, w, e), + base if Abi::is_buf(base) => self.encode(base, w, e, moves), // None is a length of -1. Type::List(..) => { let v = self.fresh("v"); - let some = self.write_value(inner, w, &v); + let some = self.encode(inner, w, &v, moves); format!("{e}.let {{ {v} -> if ({v} == null) {w}.i32(-1) else {some} }}") } _ => { let v = self.fresh("v"); - let some = self.write_value(inner, w, &v); + let some = self.encode(inner, w, &v, moves); format!("{e}.let {{ {v} -> if ({v} == null) {w}.bool(false) else {{ {w}.bool(true); {some} }} }}") } }, - Type::Box(inner) | Type::Ref(_, inner) => self.write_value(inner, w, e), + Type::Box(inner) => self.encode(inner, w, e, moves), + // Borrowed, nothing in it moves. + Type::Ref(_, inner) => self.write_value(inner, w, e), Type::Record(i) => format!("w_{}({w}, {e})", abi.record(*i)), Type::Enum(i) => match abi.api.enums[*i].plain() { true => format!("{w}.i32({e}.value)"), false => format!("w_{}({w}, {e})", abi.enumeration(*i)), }, + Type::Object(_) if moves && moved(abi, ty) => format!("{w}.moved({e}.handle)"), Type::Object(_) => format!("{w}.handle({e}.handle)"), Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("w_{}({w}, {e})", abi.range(ty)), Type::Tuple(elements) => { let t = self.fresh("t"); let writes: Vec = (elements.iter().enumerate()) - .map(|(k, ty)| self.write_value(ty, w, &format!("{t}.{}", ordinal(k)))) + .map(|(k, ty)| self.encode(ty, w, &format!("{t}.{}", ordinal(k)), moves)) .collect(); format!("{e}.let {{ {t} -> {} }}", writes.join("; ")) } @@ -1317,13 +1327,14 @@ impl<'a> Gen<'a> { } Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) | Ty::In(_) => { let w = self.fresh("w"); - // Taken by value, each object in a list moves out, as one passed alone does. - if moved(abi, ty) { - let close = self.close(ty, e); - arg.after.push(close); + // Taken by value, each object in a list moves out, as one passed alone does: the + // writer closes those it encoded, as a callback may change the list meanwhile. + let moves = moved(abi, ty); + if moves { + arg.after.push(format!("{w}.abandon()")); } arg.open = Some(format!("_Native.__Writer().use {{ {w} ->")); - let value = self.write_value(ty, &w, e); + let value = self.encode(ty, &w, e, moves); arg.inside.push(value); // Then the arrays and text it names, which Rust copies from once. arg.jni.push("jobjectArray"); @@ -3548,33 +3559,6 @@ impl<'a> Gen<'a> { Some(indent_by(&code, indent_level)) } - /// The Kotlin statement closing each handle the value `e` of type `ty` holds, once a - /// call moved their values out. - fn close(&mut self, ty: &Type, e: &str) -> String { - match ty { - Type::Option(inner) => { - let v = self.fresh("v"); - let close = self.close(inner, &v); - format!("{e}?.let {{ {v} -> {close} }}") - } - Type::Box(inner) => self.close(inner, e), - Type::Tuple(elements) => { - let t = self.fresh("t"); - let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) - .map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k)))) - .collect(); - format!("{e}.let {{ {t} -> {} }}", closes.join("; ")) - } - Type::List(_, inner) => { - let v = self.fresh("v"); - let close = self.close(inner, &v); - format!("{e}.forEach {{ {v} -> {close} }}") - } - _ => format!("{e}.handle.abandon()"), - } - } - /// The glue's out values for `outs`, of a function returning `ok` or `err`, the /// pointers the C call takes after its parameters, and the statements writing them, /// `ok` saying whether it returned ok. diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index d5839fc83..04eb8cb13 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -1014,6 +1014,31 @@ fun unwinds() { Counter.new("zero", 0u).use { counter -> check(rejects { logCounts(failing, 0u, counter, more) }, "a callback's failed check unwinds a call that moved its arguments") } val gone = runCatching { more[0].get() }.exceptionOrNull() check(gone is IllegalStateException && gone.message == "Counter is closed", "what it moved is closed in Kotlin: $gone") + // A callback replacing what the call moved out of its list: the call closes what it took. + val taken = Counter.new("taken", 2u) + val placed = Counter.new("placed", 3u) + val changed = mutableListOf(taken) + val swapping = object : Log { + override fun log(level: UByte, line: String): Boolean { + changed[0] = placed + return true + } + } + Counter.new("zero", 0u).use { counter -> check(logCounts(swapping, 0u, counter, changed) == 2u, "a callback may change a list the call moved out of") } + check(runCatching { taken.get() }.exceptionOrNull()?.message == "Counter is closed", "what the call moved is closed") + check(placed.get().toUInt() == 3u, "what the callback put in its place stays open") + changed[0] = Counter.new("failing", 4u) + val replaced = changed[0] + val throwing = object : Log { + override fun log(level: UByte, line: String): Boolean { + changed[0] = placed + return lettered { 0xD800 } == 0 + } + } + Counter.new("zero", 0u).use { counter -> check(rejects { logCounts(throwing, 0u, counter, changed) }, "a failing callback unwinds a call that moved its list") } + check(runCatching { replaced.get() }.exceptionOrNull()?.message == "Counter is closed", "what an unwound call moved is closed") + check(placed.get().toUInt() == 3u, "what the callback put in its place stays open after it unwinds") + placed.close() val tag = Tag.new() check(tag.tagged(bumps) { 'a'.code } == "a" && rejects { tag.tagged(bumps) { 0xD800 } } && bumps.value == 4u, "a borrowed result's call writes back too: ${bumps.value}") check(tag.tagged(bumps) { 'b'.code } == "ab", "the receiver is whole after") diff --git a/quest/m1/ffi-export/kotlin-moved-snapshot.md b/quest/m1/ffi-export/kotlin-moved-snapshot.md deleted file mode 100644 index 84b6cf9a8..000000000 --- a/quest/m1/ffi-export/kotlin-moved-snapshot.md +++ /dev/null @@ -1,27 +0,0 @@ -# [S] Kotlin: a call closes the handles it moved, not those its argument holds after - -## Goal - -A Kotlin call that moves objects out of a collection argument, such as `logCounts`'s -`more: List`, closes exactly the handles it encoded, however it ends, even when a -callback it reaches changes that collection before it returns: the handles Rust took are -abandoned, and any other stays open. - -## Plan - -Found by CodeRabbit on [kixel-ai/rust2#922](https://github.com/kixel-ai/rust2/pull/922). -`Gen::moving` in `ffi/src/kotlin.rs` runs `after`, `Gen::close`'s statements, which walk -the argument again (`more.forEach { v -> v.handle.abandon() }`). A `MutableList` passed in -and changed by a reentrant callback, A replaced by B, abandons B, which Rust never took, and -leaves A's wrapper open. It was so on main's success path before #922, which runs the same -`after` on the unwind path too. - -Recommendation: collect the moved handles once, as the encoding walks the argument, and -abandon that snapshot: either an array the encoder fills beside `__Writer`'s `held`, or -the writer marking which held handles move. Avoid copying the collection per call. A -regression test in `tests/api.kt` replaces an element of `more` from `Log.log`. - -The maintainer kept this on #922's merge pick -([decision](https://github.com/kixel-ai/rust2/pull/922#issuecomment-7054292931)): -✅ keep (rejected: drop it, treating a mid-call change to a read-only `List` argument as caller -misuse), since abandoning a live handle leaks its value and leaves the object unusable.