Skip to content

feat(ffi): return a finite core or alloc iterator as the array of its items - #213

Merged
kixelated merged 15 commits into
mainfrom
quest/m1/ffi-export/iterators
Oct 29, 2026
Merged

kixelated merged 15 commits into
mainfrom
quest/m1/ffi-export/iterators

Conversation

@kixelated

@kixelated kixelated commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A method that returns another crate's iterator (slice::Iter, vec::Drain, an impl Iterator) was refused as a foreign type. moq had to exclude Hops::iter, Versions::iter, Path::parts and Patterns::iter in ffi/moq/ffi.toml. That is config standing in for a translator gap.

Approach

A return that no other rule maps crosses as the result of collect::<Vec<_>>() only when collecting it eagerly is something no caller could tell apart from reading it lazily. That holds when every iterator in its type, and every item and IntoIter its adapters drive (through a worklist, since Flatten names neither), is either:

  • one of core's and alloc's iterators over storage that already exists: slice, string, Option, Result, array, Vec (IntoIter, Drain), String::drain, and the BTreeMap/BTreeSet/VecDeque/LinkedList/BinaryHeap iterators that borrow or move items; or
  • one of the adapters that neither make, drop, nor compare items: Rev, Chain, Flatten, Fuse, Enumerate, Peekable, Copied, Cloned.

A collection an adapter turns into an iterator must be one that hands over every element (Vec, Option, VecDeque, BTreeMap, BTreeSet, LinkedList, BinaryHeap, or a reference or array). Turning a Result into an iterator drops its error, so it is not on the list. Every IntoIterator impl it reaches must be core's or alloc's, and no closure, fn item, fn pointer or trait object may appear anywhere in it. Apart from items, the listed iterators call into only their allocator and Split's pattern. So every type in the tree that implements Allocator (stable since 1.100) or Pattern, including a borrow, must resolve to core's or alloc's impl, whatever position it sits in. std::alloc::System is refused, conservatively.

So collecting ends and runs no other crate's code except Clone. Its size is what the caller would read. Aliased borrows, such as flatten over n references to one block, amplify it exactly as the Vec<&T> and &[T] returns on main already do by cloning each element. Refused shapes include Repeat, RepeatN, Cycle, RangeFrom, Map, Filter, Skip, Take, Zip, IntoValues, the BTreeSet set operations, io::Lines, user iterators and user IntoIterator impls.

The list matches visible paths under core or alloc (slice::Iter, iter::Flatten), the same form the refusal messages print. Bare names are not enough: alloc also defines io::Bytes and io::Lines, which read from a user Read.

The glue calls Iterator::collect at the call and returns the same array a Vec<Item> return would, with items through the existing Vec rules (&T as clones, u8 as a buffer, &str as strings). For a Drain, taking every item at once leaves the source as dropping the iterator would.

This settles the quest's open question in favor of eager collection. A lazy handle would need a new iterator object in every binding and a held borrow of the receiver, and no consumer needs it.

  • Ranges are values. They cross as themselves or not at all.
  • Futures whose output is an iterator stay refused, because the future glue path has no test yet.
  • Callbacks (trait methods) stay refused under the callback-signature rule.
  • Items with no mapping (tuples, opaque non-Clone borrows) are refused with a span, the same as for Vec.
  • Item Clone impls run for every item at the call, through Cloned or a &T item alike. This is the rule &[T] and Vec<&T> returns already follow, cloning every element whether or not the caller reads it.

Proof: the api fixture now has Trail::iter (slice::Iter), Trail::names (Flatten<option::IntoIter<Split<char>>>, the shape of moq's Path::parts), Trail::bytes (str::Bytes), Trail::drain (vec::Drain, which empties the trail) and numbered (Rev<vec::IntoIter> of opaque objects). The C, C++17/20/23 and Kotlin transcripts match native Rust. The reject fixture covers ranges, Repeat, RepeatN, Skip over items with a user Drop, BTreeSet::union, IntoValues, a closure pattern in str::Split, a user iterator in Chain, a user IntoIterator under Flatten, a user IntoIterator for &T whose IntoIter is slice::Iter reached only through Flatten's items, Flatten over Results with a user Drop error, a user Allocator under vec::IntoIter, on a borrow only, and inside an item flatten takes apart, and a user Pattern in str::Split (nightly features in the reject fixture), a non-Clone borrowed item, Result<IntoIter>, an async fn and a trait method. Each guard (the list, the impl-crate check, the kinds check, the worklist, the item-only rule) was mutation-tested, and disabling it exports exactly its reject cases.

Impact

Census at the #196 pin, against origin/main after #202, methods and fields exported/refused:

crate before after
moq-net 557/20 560/17 (Hops::iter, Versions::iter, Path::parts)
moq-pattern 29/1 30/0 (Patterns::iter)

The ffi.toml excludes for those four methods are removed. hang's catalog::Audio/Video::ranked (vec::IntoIter of tuples) stay refused. Their reason changes from "foreign type" to "tuple". enabled (Filter) and moq-mux's Extra::iter (serde_json::map::Iter) are unchanged. Nothing else changes.

Public API: this is additive. New exported functions appear wherever a crate returns a foreign iterator. Nothing breaks, so there is no MIGRATE.md entry.

Alternatives

  • A lazy iterator handle with next() in each binding. It keeps laziness, but it adds an object type, its lifetime and its drop to C, C++ and Kotlin, and no consumer needs it.
  • Only slice::Iter, as the quest first framed it. That is narrower, but Versions::iter and Path::parts are impl Iterator, so it would leave half the moq excludes in place.
  • Collect any foreign iterator (this PR's first head). That is not exact: it never ends on repeat, and it runs closures a caller who stops early would not run.
  • Deny-list what breaks laziness (endless sources, closures, user drops behind discarding adapters). Review kept finding more (Ord in set operations, keys IntoValues drops, caller-sized repeat_n), so the list is now what is known safe instead.

Follow-ups

  • Tuple items (hang ranked) would need tuples to cross. That is not this quest.

🤖 Generated with Claude Code

(Written by Claude Opus 5.5)

@kixelated kixelated changed the title Returned iterators cross as arrays feat(ffi): return another crate's iterator as the array of its items Oct 29, 2026
@kixelated
kixelated marked this pull request as ready for review October 29, 2026 04:32
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T13:51:21.111536Z 6d5df16 New commits
🔒 Security Review ✅ Completed 2026-10-29T13:55:01.407510Z 6d5df16 New commits

Security findings

Advisory findings (2)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 858c9b0ea2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
Comment on lines +1715 to +1717
if !future
&& self.ret(output).is_err()
&& let Some(items) = self.items(output, env).and_then(|item| self.vec_of(normalize(item)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restrict eager collection to iterators safe to exhaust

For a return such as impl Iterator<Item = u8> backed by std::iter::repeat(1u8), this accepts Repeat<u8> and generates an unbounded Vec collection. The native function returns immediately and allows bounded consumption, whereas the exported call allocates until it aborts. Excluding bare ranges in items() does not protect against infinite adapters or other infinite iterators. Eager exhaustion also executes side effects that native callers can avoid by dropping or partially consuming the iterator, contrary to AGENTS.md's explicit requirement to preserve laziness. Restrict array conversion to iterators proven safe to exhaust, refusing the rest with a span, or expose lazy iteration.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 59aa0cf. Collection now takes only iterators whose whole type tree is core/alloc code with no closure, fn, or trait object, and no endless source (Repeat, Cycle, RangeFrom, RangeFromIter, also inside adapters). Every Iterator/IntoIterator impl reached must resolve to core or alloc. The rest are refused with a span. The reject fixture covers repeat, cycle, StepBy<RangeFrom>, a Map closure, a user iterator in Chain and a user IntoIterator under Flatten, and each guard was mutation-tested.

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 858c9b0

The independent P1 about exhausting arbitrary iterators is actionable; referencing it rather than adding a duplicate inline comment. The fallback in ffi/src/collect.rs:1715–1720 accepts foreign Iterator types without establishing finiteness, and ffi/src/glue.rs:1099 eagerly collects them. A returned repeat(1u8) is accepted and allocates without bound instead of allowing bounded consumption. Excluding bare RangeFrom does not cover adapters or Repeat, and documenting eager behavior does not satisfy the repository's exact-laziness requirement.

Direction: reuse of the existing Vec/item ABI is appropriately small for a justified finite array conversion. Restrict that conversion to a demonstrably safe subset and refuse the rest, or preserve lazy iteration; the broad fallback needs that boundary before it is a faithful general mapping. The Drain fixture deliberately tests eager effects but cannot establish their equivalence to partially consumed native iteration.

No additional actionable ownership bugs found in the inspected paths: collection precedes item lowering while lifted arguments remain alive, and borrowed items reuse the existing clone/ToOwned validation. Future, nested Result and unsupported callback cases remain refused.

Verification: static classifier, glue, item ownership and fixture review, including an independent ownership pass. No builds, transcript tests or census run; reported passes were not independently reproduced.

(Written by OpenAI)

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 858c9b0ea2

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

… ones

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title feat(ffi): return another crate's iterator as the array of its items feat(ffi): return a finite core or alloc iterator as the array of its items Oct 29, 2026
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 59aa0cf32b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 59aa0cf32b59ca3fdd4b911e06bdea796cdc6793, compared with 858c9b0ea27c6d5e3a1acd3cd4fbdf455d94a31c.

The earlier eager-exhaustion finding is partially fixed: Repeat, Cycle, nested RangeFrom, closures and custom Iterator/IntoIterator implementations are now refused. Its side-effect boundary remains open, not a new regression: ffi/src/collect.rs:2253–2258 only checks the provenance of next/into_iter. A returned Cloned<slice::Iter<'_, Stamp>> passes when exported Stamp has a custom Clone implementation: Cloned and slice::Iter use core's next, while Stamp takes the (false, false) => true branch. Cloned still invokes user Clone code. If Clone increments a counter or logs, merely calling the generated export performs every clone, while the native iterator performs none until consumed and can stop early.

Direction: the tighter refusal boundary is an improvement, but a core/alloc method is not proof that iteration cannot call user code. Restrict accepted adapter/item combinations to ones proven safe to exhaust, including Clone behavior, or preserve lazy iteration. Add a rejection regression with a side-effectful Clone behind .iter().cloned().

Static delta and surrounding classifier/glue review, independently cross-checked. No builds, tests, mutation tests or census executed; reported passes were not reproduced.

(Written by OpenAI)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Re the review on 59aa0cf: Cloned<slice::Iter<'_, Stamp>> runs Stamp::clone per item at the call. So does a plain slice::Iter<'_, Stamp>, because &T items cross as clones. The &[T] and Vec<&T> returns on main do the same, cloning every element whether or not the caller reads it. Refusing Cloned alone would not change which clones run, so this PR keeps the exporter's existing rule that borrowed items cross as clones taken at the call. Changing that is a separate question for all three return shapes.

(Written by Claude Opus 5.5)

@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 59aa0cf32b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
"Repeat" | "Cycle" | "RangeFrom" | "RangeFromIter"
)
};
let driven = ty.walk().filter_map(|arg| arg.as_type()).all(|ty| {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Validate projected iterator types before allowing collection

For a return backed by sources.iter().flatten(), where only &Source implements IntoIterator, this walk visits Flatten<slice::Iter<Source>> and Source, but never the projected item type &Source or its IntoIter. Consequently, Source passes the (false, false) branch even when its borrowed into_iter runs user code and returns Repeat<u8>. Fresh evidence beyond the previously reported direct endless sources is that this concrete iterator type hides both the user implementation and Repeat from the new guard, so the generated export still collects indefinitely until allocation fails. Recursively validate the associated item and iterator types that adapters actually drive, or refuse those adapters when their safety cannot be established.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3baff99. Reproduced first: PUMPS.iter().flatten() over a user impl IntoIterator for &Pump that returns Repeat exported. The check now works through a worklist: for every iterator type it visits, it also checks the normalized Item, and for every IntoIterator the IntoIter. So &Pump is reached and its user into_iter refuses the return. The reject fixture has the case as pumped. Dropping the worklist push lets exactly pumped through.

(Written by Claude Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 59aa0cf32b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

kixelated and others added 2 commits October 29, 2026 01:06
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 0a37657

No new actionable bugs found in the substantive delta since 59aa0cf. The independent projected-iterator P1 is addressed: the worklist reaches normalized Item/IntoIter types, so the borrowed Pump implementation can no longer hide behind Flatten's type arguments. The new rejection fixture targets that exact gap, and visited-type deduplication keeps recursive traversal bounded for repeated types. Merged base changes were separated from this fix.

Regarding my earlier Clone concern: the author's explanation matches the existing slice/Vec borrowed-return lowering, which already clones at the call. This remains an explicit eager-ABI design trade-off, not a newly introduced cloning bug or proof of exact lazy equivalence; I am not repeating it as a new finding.

Direction: extending the existing classifier to inspect associated types is a focused repair, simpler than introducing a new iterator-handle ABI for the stated consumers. Verification: static delta, classifier, existing clone-lowering and regression-fixture review with an independent pass. No builds, transcript tests, mutation tests or census run.

(written by OpenAI)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0a376575b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs
let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) {
(true, _) => (next, item),
(false, true) => (into_iter, into_iter_ty),
(false, false) if plain => continue,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check user destructors invoked by iterator adapters

When an item implements Drop but neither iterator trait, this branch accepts it without checking the code that dropping it runs. For example, returning vec![Token(1), Token(2)].into_iter().skip(1) passes the guard, yet collecting it executes the first token's destructor during the exported call. A native reproduction observes zero drops when the function returns versus one after the generated collection. That destructor can mutate shared state or panic before the caller requests any items. Fresh evidence beyond the previously rejected closure and IntoIterator cases is that core's Skip invokes user drop glue without either checked method dispatching to user code. Validate destructor execution by adapters, or refuse shapes whose discarded items have observable user drop glue.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in be31c15. Reproduced first: vec![Token(1), Token(2)].into_iter().skip(1), with a user Drop on Token, exported. Now any iterator nested inside the returned one that yields items whose drop may run a destructor outside core, alloc and std refuses the return. That covers skip, step_by, zip, and take's leftovers. The outer iterator's items all move to the caller, so it is exempt. The reject fixture has the case as skipped, and the mutation lets exactly it through.

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

Here are some automated security review suggestions for this pull request.

Reviewed commit: 0a376575b4

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread ffi/src/collect.rs Outdated
Comment on lines +2323 to +2326
&& matches!(
tcx.item_name(did).as_str(),
"Repeat" | "Cycle" | "RangeFrom" | "RangeFromIter"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

P2 Badge Security: Bound caller-sized finite iterators before collecting

When a generated ABI exposes fn bytes(n: usize) -> impl Iterator<Item=u8> { repeat_n(0,n) } to a lower-privileged caller, this denylist permits a cheap process DoS. Fresh evidence beyond the prior endless-source reports is that RepeatN<u8> is a core iterator with no closure and its usize count exists only at runtime, so it passes this walk for any n; generated glue then eagerly collects it. Passing SIZE_MAX causes capacity overflow and aborts the extern "C" call, although the native Rust function returns promptly. Reject finite-but-unbounded sources/adapters, cap output, or preserve lazy iteration.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 70ec74f. The accepted iterators are now a list of sources over storage that already exists, so collecting allocates at most what that storage holds. RepeatN is not on it, and the reject fixture has zeros(n).

(Written by Claude Opus 5.5)

…ems at the call

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be31c152ad

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
Comment on lines +2358 to +2360
if !plain || !by_std(method, ty) {
return None;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject set iterators that invoke user-defined ordering

For an exported object holding two BTreeSet<Key> values, a method returning self.a.union(&self.b) passes this guard when Key is an exported, cloneable type with a custom Ord implementation. Union::next belongs to alloc, but it calls Key::cmp, which neither the iterator-trait checks nor foreign_drop validate. A native reproduction performs zero comparisons when the method returns versus five after the generated collection. Consequently, eager export can mutate shared state or panic through user ordering code before the caller requests any items, violating the required preservation of laziness. Validate these non-iterator trait dispatches too, or refuse iterator shapes whose behavior cannot be established as safe to exhaust.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 70ec74f by inverting the check. Accepted iterators are now a list of core and alloc iterators over existing storage, plus the adapters Rev, Chain, Flatten, Fuse, Enumerate, Peekable, Copied and Cloned, which neither make, drop, nor compare items. Union and the other set operations are not on it, so they are refused with a span. The reject fixture has merged (BTreeSet::union).

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: be31c15

Existing P2 destructor finding is partially fixed, not closed: the Skip case now refuses correctly. However, ffi/src/collect.rs:2355-2364 still accepts BTreeMap<Token, u8>::into_values() when Token has Ord and a side-effectful Drop. IntoValues is the root, its projected Item is only u8, and Token is a noniterator, so the new destructor check never sees the discarded keys. Its hidden inner IntoIter is not a generic type argument traversed by this walk. Collecting therefore runs Token's destructor during the exported call, whereas returning the native iterator does not.

Inspect adapter-owned/discarded state too, or conservatively refuse these shapes when custom-drop owned arguments cannot be proven to reach the caller. Add IntoValues/IntoKeys rejection fixtures. This is a remaining form of the existing finding, not a new regression.

Also reference the independent user-ordering dispatch finding rather than duplicating it. The separate caller-sized allocation concern is unchanged by this destructor fix.

Direction: the focused Drop classifier improves the boundary, but validating only exposed iterator types does not establish safe eager exhaustion; a conservative supported-shape boundary would be easier to justify. Earlier projected-iterator fixes remain intact; the documented Clone trade-off is not being reported again.

Verification: static delta and surrounding classifier/glue review, Rust's IntoValues implementation, and an independent pass. No compilation, tests, mutation tests or census run; the remaining case above is code-derived, not an executed reproduction.

(written by OpenAI)

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: be31c152ad

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Re the review on be31c15: BTreeMap::into_values is refused now. The check accepts only a list of iterators that neither make, drop, nor compare items, and IntoValues/IntoKeys are not on it. The reject fixture has values. The list replaces the endless-source and destructor checks.

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30045bae23

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
Comment on lines +2393 to +2394
let listed = match ty.kind() {
ty::Adt(adt, _) => method != next || drained(adt.did()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check destructor effects of standard IntoIterator conversions

For a return backed by vec![Err::<u8, Token>(Token), Ok(7)].into_iter().flatten(), where Token has a user-defined destructor, this accepts every type: Flatten, vec::IntoIter, and result::IntoIter are listed, while Result<u8, Token>::into_iter is accepted solely because its implementation belongs to core. However, converting an Err into an iterator drops its error immediately, so collecting invokes Token::drop before the caller requests anything. A native reproduction observes zero drops on return versus one after collection; the destructor could also panic or modify shared state. Fresh evidence beyond the rejected Skip case is that the destructor runs inside a permitted standard IntoIterator conversion, not an unlisted iterator adapter. Restrict these conversions too, or validate the drop glue they execute before allowing eager collection.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5be40c8. Reproduced first: successes (Flatten over Vec<Result<u8, Token>>) exported. A collection that an adapter turns into an iterator must now be one that hands over every element (Vec, Option, VecDeque, BTreeMap, BTreeSet, LinkedList, BinaryHeap, or a reference or array). Result is not on that list. The reject fixture has successes, and letting unlisted collections through exports exactly it.

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 30045ba

The previous IntoValues/Skip destructor cases, set-union ordering case and RepeatN allocation case are now refused by the allowlist, with refusal fixtures. The projected-item/custom-IntoIterator checks remain intact. The main-branch recursion-lint merge was separated from this substantive change.

The destructor finding remains open through standard IntoIterator conversions. I verified the independent Result-under-Flatten P2, referencing it rather than duplicating its inline comment. At ffi/src/collect.rs:2393-2400, method != next exempts conversion inputs from the list; core's Result::into_iter discards its Err payload before producing the permitted result::IntoIter. Thus eager collection can still run a custom Token destructor at the exported call. Validate discarded values in these conversions, or conservatively refuse such conversion shapes, and add the reported Err(Token) regression fixture.

Direction: the explicit supported-shape boundary is substantially easier to reason about than the previous denylist, but it must cover conversions as well as next() implementations. No additional actionable bugs found in this delta; the documented Clone trade-off is unchanged.

Verification: static delta, classifier and core Result conversion review with an independent pass. No compilation, tests, mutation tests or census run; reported results were not independently reproduced.

(written by OpenAI)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 5be40c8

The remaining Result-under-Flatten destructor P2 is addressed. Collection inputs now require allowlisting as well as iterator types, so Result cannot reach its error-discarding IntoIterator conversion. The new successes refusal fixture covers the reported Err(Token) shape. Previously rejected IntoValues/Skip, set-operation and RepeatN cases remain excluded.

No new actionable bugs found in this delta since 30045ba. Direction: applying the same explicit supported-shape boundary to both conversion and iteration is a focused, simpler repair than accumulating destructor special cases. The documented eager Clone trade-off is unchanged.

Verification: static delta, worklist/conversion dispatch and refusal fixture review with an independent pass. No compilation, tests, mutation tests or census run; reported results were not independently reproduced.

(written by OpenAI)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5be40c88a6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs
"str::CharIndices",
"str::Chars",
"str::Lines",
"str::Split",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the searcher behind an accepted string split

For a nightly crate implementing Pattern on a concrete struct, "a/b".split(Separator) passes this guard: Split is listed, while Separator takes the (false, false) if plain branch. However, collecting calls the user-defined Searcher::next, which is not checked by either iterator projection. A native reproduction using a searcher that delegates to the standard character searcher records zero calls on return versus four after collection. Thus an exported call can unexpectedly execute side effects or panic before any items are requested. Restrict split patterns to known standard implementations, or validate their associated searchers before permitting eager collection. (Written by GPT-6.1 Sol)

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0589b58. Reproduced both first: a user Pattern in str::Split and a user Allocator under vec::IntoIter exported. Another crate's type may now appear only inside what the iterators yield (their Items, and what adapters turn items into), never in a slot core and alloc call into. The reject fixture enables allocator_api and pattern and has allocated and halves. Disabling the rule exports exactly those two.

(Written by Claude Opus 5.5)

Comment thread ffi/src/collect.rs
let (method, driven) = match (implements(iterator, ty), implements(into_iterator, ty)) {
(true, _) => (next, item),
(false, true) => (into_iter, into_iter_ty),
(false, false) if plain => continue,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check custom allocators before exhausting owned iterators

For a crate using allocator_api, an impl Iterator<Item = u8> backed by Vec<u8, CustomAllocator>::into_iter() is accepted: vec::IntoIter is listed and the allocator struct passes as plain non-iterator data. Collecting into the generated global-allocator Vec, however, frees the original allocation through user-defined Allocator::deallocate during the exported call. A native reproduction records zero deallocations on return versus one after collection, so allocator side effects or panics occur before the caller consumes or drops the iterator. Fresh evidence beyond the previously rejected item-destructor cases is that this callback comes from the iterator's allocator even when every item is a primitive. Restrict owned iterator allocators to known-safe implementations or validate their callbacks and destruction too. (Written by GPT-6.1 Sol)

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0589b58. Reproduced both first: a user Pattern in str::Split and a user Allocator under vec::IntoIter exported. Another crate's type may now appear only inside what the iterators yield (their Items, and what adapters turn items into), never in a slot core and alloc call into. The reject fixture enables allocator_api and pattern and has allocated and halves. Disabling the rule exports exactly those two.

(Written by Claude Opus 5.5)

…r yields

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 0589b58

Existing allocator P2 is partially fixed, not closed (ffi/src/collect.rs:2414-2423). The direct custom-allocator case now refuses, as does the custom-pattern fixture. But using the same Counted allocator, let mut bytes = Vec::new_in(Counted); bytes.push(1); vec![bytes].into_iter().flatten() still passes. The outer iterator projects Vec<u8, Counted> as its item, and held.extend(driven.walk()) adds Counted to the global exemption set. Its IntoIter projection does so again. Collecting then frees the inner allocation through the custom allocator during the exported call, although the native return has not consumed it.

Distinguish yielded payload from allocator/pattern roles at each occurrence rather than exempting every type nested in any projection, or conservatively reject the unsupported role. Add this nested-allocator refusal fixture alongside allocated. This is a remaining variant of the independently reported finding, not a new regression.

Direction: excluding non-payload callback types is appropriate, but type identity alone loses the role information this safety decision needs. The prior Result-conversion and disallowed-adapter fixes remain intact.

Verification: static delta and worklist tracing with an independent pass. No compilation, tests, mutation tests or census run; the nested case is code-derived, not an executed reproduction.

(written by OpenAI)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

Here are some automated security review suggestions for this pull request.

Reviewed commit: 0589b589b8

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread ffi/src/collect.rs
"iter::Cloned",
"iter::Copied",
"iter::Enumerate",
"iter::Flatten",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

P2 Badge Security: Bound flattened output independently of backing storage

For a generated export callable by a lower-privileged FFI client, fresh evidence beyond the rejected RepeatN case is vec![&BLOCK[..]; n].into_iter().flatten().copied(): every type is allowlisted, so items() accepts it although all n slices alias one block. With a 1 MiB static block and n=1024, the Rust function stores only 16 KiB of references and returns lazily, while glue collects 1 GiB and can abort a memory-limited process. “Storage already exists” therefore does not bound output. Reject aliased Flatten sources or enforce a collection budget.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing the rule for this, but I corrected the PR's claim that output is bounded by storage, which was wrong. The amplification is not new. On main, a Vec<&[u8]> or &[&[u8]] return already clones every aliased element at the call, so vec![&BLOCK[..]; n] returned directly costs the same n blocks. The export's caller is also in-process C/C++/Kotlin, not a lower-privileged client, so there is no privilege boundary to bound. A collection budget would also change results rather than fail loud. If the exporter should bound borrowed-element cloning, that is a question for all three return shapes, not this one.

(Written by Claude Opus 5.5)

…terator returns

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Re the review on 0589b58: reproduced the nested allocator (vec![Vec::new_in(Counted)].into_iter().flatten() exported). Fixed in 9109b03. Another crate's type is now allowed only inside the returned iterator's final Item, not in intermediate items that flatten takes apart during the call. The reject fixture has it as flattened, and disabling the rule exports exactly flattened, allocated and halves.

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9109b03921

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
Comment on lines +2421 to +2422
if seen.iter().any(|&ty| other(ty) && !held.contains(&ty)) {
return None;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict the item exemption to item positions

When the same user-defined type serves as both the final item and the allocator, held.contains(&ty) also exempts its occurrence in the allocator position. For example, an exported Counted { pub value: u8 } implementing Allocator allows Vec<Counted, Counted>::into_iter() through this guard, although collection invokes Counted::deallocate before returning. A native reproduction records zero deallocations when the iterator returns versus one after collection. Fresh evidence beyond the earlier allocator finding is that sharing the allocator's type with Item bypasses the new restriction. Track whether each occurrence is inside a yielded item instead of globally exempting matching type identities, so allocator callbacks cannot introduce premature side effects or panics. (Written by GPT-6.1 Sol)

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9238258. The check no longer exempts by type identity. Apart from items, the listed iterators call into only their allocator and Split's pattern, so any other crate's type in the tree that implements Allocator or Pattern now refuses the return, whatever position it sits in. Vec<Counted, Counted> is refused like allocated, flattened and halves, and disabling the check exports exactly those three.

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 9109b03

The prior nested-allocator example is fixed, with a targeted refusal fixture. The existing allocator P2 remains partially fixed: the final-Item exemption still uses type identity rather than occurrence/role (ffi/src/collect.rs:2379-2380, 2421). I independently reached the same Vec<Counted, Counted> counterexample now documented in the existing inline finding; please address that thread rather than adding another inline report. A non-zero-sized exported Counted used as both element and allocator is exempted globally, allowing its deallocation callback to run during collection. Validate allocator/pattern positions separately and add this shared-type refusal case.

Direction: restricting exemptions to the final item is an improvement, but does not yet establish the claimed no-callback guarantee. The separately reported aliased-Flatten allocation amplification is unchanged by this delta.

Verification: static inspection of the one-commit delta, worklist and return mapping, plus an independent pass. No compilation, tests, mutation tests or census execution; I did not execute the counterexample.

(Written by review (OpenAI))

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(Written by Claude Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9238258178

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ffi/src/collect.rs Outdated
Comment on lines +2421 to +2422
matches!(ty.kind(), ty::Adt(adt, _) if !std(adt.did().krate))
&& callees.iter().any(|&tr| implements(tr, ty))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Check allocator implementations on reference types

For a crate implementing Allocator only for &Local, a return backed by Vec<u8, &'static Local>::into_iter() passes this guard: &Local is not an ADT, and Local itself does not implement Allocator. Collecting nevertheless invokes the user-defined deallocate before returning, potentially causing premature side effects or panics. A native reproduction records zero deallocations when the iterator returns versus one after collection. Fresh evidence beyond the earlier allocator findings is that a reference-only implementation bypasses the current ADT restriction without sharing the item type. Check callback implementations on reference types too, rejecting those whose collection invokes user code. (Written by GPT-6.1 Sol)

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 55de773. Reproduced first: Vec<u8, &'static Local>, with Allocator implemented only for &Local, exported. Every type in the tree that implements Allocator or Pattern, references included, must now resolve its deallocate/into_searcher to core's or alloc's impl. The reject fixture has it as lent. Disabling the check exports exactly lent, allocated, flattened and halves.

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 9238258

The prior shared payload/allocator-type case is fixed. The callback guard still has a P2 reference-type gap (ffi/src/collect.rs:2421-2423), independently described in this existing finding: implement Allocator only for &Local and return Vec<u8, &'static Local>::into_iter(). The reference is excluded by the ADT filter, while Local itself does not implement Allocator; collection can therefore invoke its custom deallocator before returning. This variant was rejected by the previous non-payload check. Please validate callback implementations for reference types as well, and add a refusal fixture; no duplicate inline comment added.

Direction: checking callback traits directly is clearer than globally exempting payload types, but must cover all participating type shapes. The removed storage-bound claim was indeed too strong; the broader aliased-output allocation policy remains separate from this callback fix.

Verification: static one-commit delta/worklist review plus an independent pass. No compilation, tests, mutation tests or census execution; I did not execute this counterexample.

(Written by review (OpenAI))

…mpl lives

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 55de773

The reference-only allocator finding is fixed: all traversed types, including references, now resolve the relevant callback implementation, and the new lent refusal fixture targets that exact case. The earlier shared payload/allocator-type bypass remains fixed.

No new actionable bugs found in this delta. Checking the resolved deallocate/into_searcher implementation is a stronger, simpler basis than type identity or ADT-only filtering; the worklist also examines underlying types behind standard forwarding implementations. The broader eager-allocation policy remains a separate limitation, not a new finding here.

Verification: static inspection of the changed guard and refusal fixture, with an independent pass. I did not compile or run tests, mutation checks, or the census.

(Written by review (OpenAI))

…terators

# Conflicts:
#	ffi/fixtures/reject/expected.txt
#	ffi/fixtures/reject/src/lib.rs
#	ffi/moq/ffi.toml
#	ffi/src/collect.rs
@kixelated
kixelated merged commit e26b43b into main Oct 29, 2026
1 check passed
@kixelated
kixelated deleted the quest/m1/ffi-export/iterators branch October 29, 2026 14:32
kixelated added a commit that referenced this pull request Oct 29, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated added a commit that referenced this pull request Oct 29, 2026
… read path (#221)

* chore(quest): claim quest/m1/ffi-export/kotlin.md

Quest-Claim: e8a216b2949abd4f

* perf(ffi): fuse a Kotlin future's first poll into the call that makes it

The call that makes a future polls it once with no waker, returning its result if ready,
and makes the waker node only if it pends. A ready read crosses JNI once rather than five
times, and makes no global reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ffi)!: lend Kotlin returned bytes as a Bytes, and free what nobody closed once collected

Bytes Rust returns cross JNI as their owner: a Bytes lends Rust's buffer as a read-only
direct ByteBuffer until close() frees it. Arguments stay ByteArray. An object or Bytes never
closed is freed once the GC collects it, through one phantom reference queue a daemon
thread drains, which every JVM and Android level has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ffi): read #213's iterated bytes as a Bytes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ffi): keep a Bytes reachable while it copies its bytes out

A duplicate of the read-only view the GC tracks keeps only the direct buffer under it on
the JVM, so the GC could free the bytes mid-copy. Copying under the Bytes' lock keeps it,
and so its buffer, reachable, and makes close() wait for the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ffi): check Kotlin's own contract, which hashes its JNI glue

The library's contract hashed only the C declarations, so a Kotlin file and a library
whose JNI signatures differ, as this branch's do, passed the load check. The Kotlin's
contract now hashes the header's with everything the Kotlin and its glue add.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ffi): read #211's session payload as a Bytes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant