Skip to content

feat(ffi): exporter spike, a crate's pub API to C - #41

Merged
kixelated merged 18 commits into
mainfrom
quest/m1/ffi-export-spike
Oct 25, 2026
Merged

kixelated merged 18 commits into
mainfrom
quest/m1/ffi-export-spike

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The exporter spike asks whether a rustc driver can turn a crate's pub API into bindings with no attributes, starting with a C ABI, and how much of moq's hand-written wrapper is really needed.

Approach

  • driver/: the front end rust2-ts had, shared: the CLI that runs cargo check with the binary as RUSTC_WRAPPER, and the wrapper side. rust2-ts now uses it, with no change in behavior; the flake builds ts with it.
  • ffi/ (rust2-ffi): a second driver on the same nightly. It walks every pub item reachable from the crate root into its own IR, then writes a crate of extern "C" functions plus a runtime, and a C header. Fixed rules: all-pub structs are records, other types Arc handles (borrows checked at run time, Copy handles copied), Result is bool plus out pointers, futures are polled from C with a waker and no runtime, pub traits are C callback tables. Anything else is a compile error at its span unless ffi.toml excludes it.
  • Naming: path segments in snake case joined by _. Every C name is registered; a clash is an error at both spans, fixed with [rename]. moq-net has one: session::Stats and Session::stats are both moq_session_stats.
  • Proof: ffi/fixtures/api covers each rule. tests/api.c runs the fixture's native script through the C ABI, and the transcripts must match line for line; each runtime check must abort with its message. fixtures/reject snapshots the span errors. just ffi moq exports moq-net at the pinned revision, builds it, and runs ffi/moq/moq.c: publish and subscribe to a track in one process, each read a polled future. It is clean under ASan, LSan, and UBSan.
  • just check and just test include ffi. Nightly adds an export plus census job and a bench job against moq-c.

Report

Coverage of moq-net

Item Exported Refused Share
Types 134 19 87%
Methods and fields of exported types 498 94 84%
Free functions 2 4 33%
Traits, as callbacks 1 14 6%
Constants 6 3 66%
Re-exports of other crates and macros 0 9 0%

The 134 types are 6 records, 15 enums, and 113 objects. 39 poll_* methods whose async twin exports as a future are left out by rule (decision 3), not counted as refused. 7 would-be values became objects because a field does not map, among them Error. Traits are mostly moq-net's transport abstraction (associated types, blanket impls), which C would not implement anyway.

Refusal census, ranked

Items Construct What it is
28 foreign type 7 poll_*(&kio::Waiter) methods without an async twin, moq_pattern, moq_clock::Instant, Range, Bound
24 borrowed object return mostly info() -> &broadcast::Info / &track::Info. A clone would be faithful for these, but not for handles like &broadcast::Consumer
19 generic type transport::poll::* futures, Timed<T>, frame::Buffer<N>, Path<'a>
14 generic parameter RangeBounds, Consume<T>, transport bounds
13 refused type Path<'a> cascading into AsPath, root(), absolute()
9 re-export of another crate moq_pattern, moq_clock, bytes, kio
8 Vec<T> of non-bytes stats snapshots, version lists
5 to 1 supertrait, u128, slice, associated type, const generic, blanket impl, tuple, &mut value, empty enum

How much of moq-ffi is needed: little of it for moq-net itself. Four rules cover most of what is left: tuples as out parameters, Path<'static>, clone-on-borrowed-Clone, and Range. The tuple rule matters most, since without it origin::Producer::new cannot be called. What moq-ffi really adds is in other crates: sessions over moq-native, media over hang, moq-mux and the codecs. It also adds a runtime to drive sessions and origins, whose Driver::poll(Instant, &kio::Waiter) is refused.

Against moq's hand-written rs/moq-c

export moq-c
C functions 1000 168
Header, with docs 264 KB, 68 KB gzip 146 KB, 33 KB gzip
Header, comments stripped 99 KB, 13 KB gzip 30 KB, 4.7 KB gzip
100 B stamped frame write, -O2, 1M frames, 8 runs 213-230 ns 200-219 ns
of which the timestamp's handle 7-11 ns none (u64 micros)
cheapest handle call (is_closed) 6.6 ns n/a
  • Coverage: moq-c's 168 functions split into 72 on moq-net's model (origin, broadcast, track, group, frame, bandwidth, requests), 74 on media, and 22 on sessions, servers, and logging. The export covers the model, except origin creation (a tuple return) and anything returning a Path (a request's or announcement's path). Media and sessions live in other crates; the export would need to run on those too.
  • Call overhead: the same. Both spend their time in moq-net. moq-c pays a global state mutex and slab lookup per call, and the export an atomic borrow per handle. The export's extra 5% is the Copy timestamp kept behind a handle.
  • Size: the export has 6x the functions, since it is the whole model rather than a curated slice, plus a poll and free per future. Per function the header is smaller: about 99 bytes against moq-c's 178, without comments.

The IR

ffi/src/ir.rs is plain data, with no language decided. It holds records, enums (plain or tagged), objects (with sync and copy), callbacks, functions, and constants. Each has its path from the crate root, span, and docs. A function has a receiver kind (&, &mut, owned), params, an ok and error type, whether it is a future, and what it reads (a call, a field, or a constant). Types keep Rust's exact form (&str vs String, Box<dyn T> vs impl T), so a generator can borrow where Rust borrows. abi.rs decides the C names and signatures once, so the header and glue agree.

What the next languages need

The IR already carries what a generator needs: paths for its own naming rule, docs, receiver kinds, and borrow forms. The C ABI already gives them handles with checked borrows (safe to share from GC finalizers and threads) and futures with wakers (one poll and one wake callback per language). It also gives callback tables. Each language then needs:

  • an idiomatic naming rule;
  • a wrapper class per object, with free on close or finalize;
  • a native future around poll;
  • a contract symbol so a header and library from different builds fail to load together.

The paused fork quests

All folded into the exporter (decision 1), and deleted here; what remains of each is in quest/m1/ffi-export.md:

  • Done at the C ABI: the C-first layering, the name clash, bulk and zero-copy bytes, cancel-safe sync poll with a reused waker, and three crossings per ready future instead of five.
  • Moot: checksums.
  • Still needed: one contract symbol per build.
  • Becomes a per-generator choice: Kotlin's JNI.

Impact

  • New tool rust2-ffi (ffi/), unpublished. Its CLI, ffi.toml (package, prefix, exclude, rename), and generated C ABI are new public API.
  • New crate rust2-driver (driver/), unpublished, a path dependency of both drivers.
  • rust2-ts: it now finds a library named apart from its package. Otherwise no behavior change. Its CLI parsing and wrapper setup moved into rust2-driver, which takes a driver's own options (ts's --lower) and lets its job add rustc arguments (the MIR lowering's -Zub-checks=no and -Cpanic=unwind).
  • flake.nix: the ts package builds from ts/ plus driver/. The bench shell sets LIBCLANG_PATH for moq-c.
  • just check and just test run ffi. Nightly gains ffi and ffi-bench jobs.
  • Quests: the spike, the nine paused fork quests, and moq-poll-migrate (which stood in for foreign poll's blocker) are deleted. ffi-export holds what remains of them.

Alternatives

  • rustdoc JSON or parsing source: rejected with the maintainer. Types, visibility, and trait impls come from the compiler.
  • Integer handles (moq-c's style) instead of Arc pointers: these need a global table and lock. A pointer with a borrow flag costs the same and gives each language a GC-friendly handle.
  • A blocking Mutex per object: deadlocks a pending &self future against any &mut self call. The run-time borrow check refuses that instead, as Rust would at compile time.
  • CamelCase type segments in C names (moq_track_Producer_append_group): this makes the type boundary unambiguous, but a module/function join can still clash, so registration is needed either way. Snake case reads as C, and rename fixes the rare clash.

Decisions

Settled by the maintainer, as recommended:

  1. Adopt the exporter, and fold the paused quests into it. Applied to the quest tree here.
  2. Enums whose fields do not map, and structs with private fields, become objects, each listed in the census. Values that hold themselves become objects by the same rule.
  3. poll_* methods whose async twin exports as a future are left out by rule, and listed in the census.
  4. rust2-ffi lives in ffi/, and the shared rust2-driver in driver/.

Review

Eleven Codex findings were fixed, each with a fixture case or test:

  • A future from a function that is not async is now called at the exported call, as Rust runs it; one that borrows its arguments is refused.
  • A recursive value is now an object, and a borrowed return must be Clone and hold no non-Copy handle.
  • An Option nested through a Box is refused.
  • A handle lent inside a value (&Box<T>) is refused rather than moved.
  • Both drivers find the configured library by Cargo's package name, so a library named apart from its package works. They also accept rlib, and fail rather than succeed silently when the wrapper never ran.
  • A type with its own Drop is an object, so the destructor runs when C frees it.
  • A value that is not Sync is shared on one thread at a time, so one handle passed twice works as in Rust. This replaces the _eq special case.
  • A relative CARGO_TARGET_DIR is the caller's, and the drivers' completion marker is per run.

Follow-ups

  • quest/m1/ffi-export.md: the rules above, several crates, driving sessions, a contract symbol, and generators, including what the folded quests left: per-standard C++, Kotlin's JNI, and each language's async glue.
  • The exporter's runtime borrow check and futures should join the binding benchmark once a second language exists.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 11 commits October 24, 2026 18:42
…d reject fixture

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Exporter spike: a crate's pub API to C feat(ffi): exporter spike, a crate's pub API to C Oct 25, 2026
@kixelated
kixelated marked this pull request as ready for review October 25, 2026 03:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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 🔄 Running since 2026-10-25T07:22:33.486809Z e679853 New commits
🔒 Security Review ✅ Completed 2026-10-25T07:28:48.096354Z e679853 New commits
ℹ️ 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: 6171efce01

ℹ️ 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/glue.rs Outdated
let body = stmts.join("\n\t");
if let Some((_, poll, free)) = &export.future {
self.line(format!(
"#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{body}\n\trt::Future::new(async move {{\n\t\tlet result = {call}.await;\n\t\t::std::boxed::Box::new(move |out: *mut c_void, err: *mut c_void| -> i32 {{\n\t\t\tlet _ = (out, err);\n\t\t\t{}\n\t\t}}) as rt::Done\n\t}})\n}}\n",

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 Preserve eager execution of functions returning futures

For an ordinary fn returning impl Future, placing {call} inside async move defers the function body until the first poll, although native Rust executes that body when constructing the future. Side effects disappear entirely if C creates and cancels the future without polling it. An isolated differential check using the generated glue confirmed that a counter increment performed before returning std::future::ready(...) occurs natively but not through the ABI. Invoke the factory during the exported call while retaining any argument storage its future borrows. This violates AGENTS.md's explicit requirement to preserve source laziness.

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 bc87005. A future from a function that is not async is now called during the exported call, with its arguments borrowed only for that call as in a sync call, and the export polls what it returns. One that borrows its arguments (impl Future + '_) is refused at its span, since the export cannot keep them alive past the call. The api fixture's Channel::send_later sends at the call and its future is freed unpolled, matching the native transcript; fixtures/reject covers the refusal.

(written by Opus 5.5)

Comment thread ffi/src/glue.rs
Comment on lines +236 to +237
Type::Option(inner) | Type::Box(inner) => self.owns(inner),
Type::Record(i) => self.abi.api.records[*i].fields.iter().any(|f| self.owns(&f.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 Detect recursive value types before generating their ABI

A valid public type such as pub struct Node { pub next: Option<Box<Node>> } passes field classification as a record, but this ownership traversal follows Record → Option → Box → Record indefinitely. Running the generator with that collected IR reproduces a stack-overflow abort. Moreover, the ABI erases Box into an inline value, so merely adding a visited set here would still leave an unrepresentable recursive C struct. Detect these cycles during classification and make the types opaque or refuse them with a source-span diagnostic, as required by AGENTS.md.

AGENTS.md reference: AGENTS.md:L14-L14

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 bc87005. Classification now detects a value that holds itself through other values, Option, or Box, and makes it an object, which the census lists, rather than an unrepresentable C struct. A borrowed return must also be Clone and hold no non-Copy handle, so Node::next is refused rather than cloned. The api fixture's Node list covers both.

(written by Opus 5.5)

…leave out poll twins; fold the paused quests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@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: bc87005787

ℹ️ 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/glue.rs
Comment on lines +316 to +318
"#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}) -> bool {{\n\tlet w = \"{name}_eq\";\n\tlet a = {};\n\tlet b = {};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)\n}}\n",
borrow("a"),
borrow("b")

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 Borrow aliased non-Sync equality operands only once

For a Send + !Sync object implementing PartialEq, such as a struct with a private Cell<u32> field, calling the generated _eq(h, h) aborts: both operands acquire an exclusive borrow, so the second acquisition fails. An isolated differential check using the generated glue produced true natively but aborted with a handle is already borrowed through C. When the handles alias, acquire one guard and pass its reference as both operands, preserving the actual PartialEq implementation rather than assuming self-equality.

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 da759a4: _eq with one handle twice takes one borrow and compares the value with itself, so a !Sync object no longer aborts. The api fixture's Tally (a private Cell) checks api_tally_eq(t, t) against the native transcript.

(written by Opus 5.5)

Comment thread ffi/src/collect.rs
Comment on lines +1093 to +1095
let inner = self.ty(args.type_at(0), pos.inner())?;
if matches!(inner, Type::Option(_)) {
return Err(why("nested Option", ty.to_string()));

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 nested options through erased Box wrappers

A signature such as pub fn nested(value: Option<Box<Option<u32>>>) bypasses this check because the outer option's immediate inner type is Type::Box, not Type::Option. ABI naming then erases the box and reaches Abi::key's unreachable! for Option(Int(U32)), which I reproduced with the corresponding IR. Both export and census therefore panic instead of reporting the unsupported signature at its source span. Check through the wrappers erased by the ABI before accepting the outer option.

AGENTS.md reference: AGENTS.md:L14-L14

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 da759a4: the nested-option check looks through Box and references, so Option<Box<Option<u32>>> is refused at its span. fixtures/reject covers it.

(written by Opus 5.5)

…n Option inside a Box inside an Option

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@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: da759a4

Both prior findings are addressed:

  • Aliased equality operands acquire one borrow and invoke the actual PartialEq implementation on that reference twice, rather than assuming self-equality. The non-Sync Tally fixture covers the original abort: #41 (comment)
  • Nested-option validation recursively strips Box/reference wrappers through Type::base(), rejecting the previously panicking signature at its span. The rejection fixture covers Option<Box<Option>>: #41 (comment)

No new actionable bugs found. The fixes are narrow and reuse existing borrowing/type-normalization mechanisms.

Verification: static changes/surrounding-code inspection only; tests were not run.

@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: da759a4700

ℹ️ 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/glue.rs
Mutability::Mut => ("let mut", format!("&mut {local}")),
Mutability::Not => ("let", format!("&{local}")),
};
(format!("{binding} {local} = {};", self.lift(inner, c)), pass)

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 Preserve object ownership behind borrowed Box arguments

For an accepted signature such as pub fn read(value: &Box<Thing>) -> u32, where Thing is a non-Copy object, this fallback lifts the box using Object::take and passes a reference to the newly owned box. Consequently, a read-only call consumes the caller's handle and drops its value when the call returns. An isolated differential check using the actual generated glue allowed two reads natively but aborted on the second ABI read with a handle's value was moved out. Borrow the underlying object without consuming it, or reject this signature at its source span if the erased ABI cannot represent that borrow.

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 a06d781: a reference parameter whose referent holds a non-Copy handle anywhere but directly (&Box<T>, &Option<T>) is refused at its span, since it would cross by value and move the caller's handle. Only &T of a handle is lent. fixtures/reject's peek(&Box<Opaque>) covers it.

(written by Opus 5.5)

Comment thread ffi/src/driver.rs Outdated
Comment on lines +46 to +47
if !rust2_driver::rustc::compiles_lib(args, &config.crate_name()) {
return Ok(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 Resolve the library target name independently of the package

For a package whose manifest specifies a different [lib] name, config.crate_name() still derives the name from the package, so this condition skips the configured library entirely. The CLI then returns Cargo's successful check status without creating the requested export or census, potentially leaving stale output in place. Resolve the actual library target name from the manifest or Cargo metadata and use it consistently for wrapper selection and generated Rust paths; otherwise report the unsupported configuration rather than silently succeeding.

AGENTS.md reference: AGENTS.md:L14-L14

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 a06d781, for both drivers since the check is in the shared rust2-driver: the wrapper matches on Cargo's CARGO_PKG_NAME (plus --crate-type lib and not --test) rather than a name derived from the package, and the exporter names the library by rustc's own crate name. The api fixture's library is now api_lib in package api-fixture.

(written by Opus 5.5)

kixelated and others added 2 commits October 24, 2026 22:44
…pike

# Conflicts:
#	ts/src/cli.rs
#	ts/src/driver.rs
…ide a value

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@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: a06d781

Both intervening independent findings are addressed:

  • Wrapped non-Copy handle reference parameters are rejected at their source span rather than consuming the caller's handle. Direct object references retain borrowing, with a rejection fixture for &Box: #41 (comment)
  • Library selection matches Cargo's package identity while generated Rust paths use rustc's actual library name. The renamed api_lib fixture exercises the distinction: #41 (comment)

No new actionable bugs found in the substantive delta. Earlier equality-aliasing and nested-option fixes remain intact. Reusing existing handle validation and shared driver selection keeps these fixes narrow.

Verification: static changes and surrounding-code inspection only; tests were not run.

@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: a06d781fc9

ℹ️ 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
Comment on lines +247 to +251
let record = !variant.fields.is_empty()
&& variant.fields.iter().all(|f| f.vis.is_public())
&& !variant.is_field_list_non_exhaustive();
match record {
true => Decl::Record(0),

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 Keep types with custom destructors behind handles

For an all-public struct with scalar fields and a custom Drop implementation, this classification exports the type as a record. The generated lower_<record> destroys the Rust value before the exported constructor returns, while the record's _free does nothing. A differential check with Token { pub id: u32 } whose destructor increments a counter produced zero drops before freeing natively but one through the generated ABI. Destructors that release resources therefore invalidate them while C still owns the returned value. Classify types with custom destructors as objects instead, including enums with the same problem.

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 4f1dc55: a struct or enum with its own Drop impl is an object, so its destructor runs when C frees the handle. The api fixture's Token checks that no drop happens before api_token_free and one after, against the native transcript.

(written by Opus 5.5)

Comment thread driver/src/rustc.rs Outdated
/// library target is called.
pub fn compiles_lib(args: &[String], package: &str) -> bool {
let ours = std::env::var("CARGO_PKG_NAME").is_ok_and(|name| name == package);
let lib = args.windows(2).any(|w| w[0] == "--crate-type" && w[1] == "lib");

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 Recognize explicit rlib library targets

For a package declaring [lib] crate-type = ["rlib"], Cargo passes --crate-type rlib, not lib, so this predicate skips the configured library. The FFI CLI then returns a successful check status without writing either the export or census, potentially leaving stale output in place. This invocation was confirmed with a local Cargo wrapper; rustc --help explicitly lists rlib among the supported --crate-type values. Recognize this library form or report an unsupported configuration instead of silently succeeding.

AGENTS.md reference: AGENTS.md:L14-L14

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 4f1dc55, in the shared driver: --crate-type rlib counts as the library. The CLI also no longer succeeds silently: the wrapper marks a file once it has run the job, and if cargo succeeds without that mark, the CLI fails with "cargo compiled no library of package ...".

(written by Opus 5.5)

…pt rlib and fail when no library ran

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@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: 72dcb0a

[P2] Use an absolute completion-marker path — driver/src/cargo.rs:143–154,162. With CARGO_TARGET_DIR=target, invoking the CLI from /repo with --config crates/api/ffi.toml starts Cargo in /repo/crates/api. The wrapper writes target/rust2-ffi.ran relative to that package, but the parent removes/checks it relative to /repo, incorrectly reporting “cargo compiled no library” after successful generation. Resolve target_dir against package_dir and use the resulting absolute path for Cargo, marker removal, RAN and the final check. Add a regression invoking outside the package with a relative target directory.

The intervening findings are addressed: custom Drop types remain behind handles (#41 (comment)), and explicit rlib targets are recognized (#41 (comment)). The direction is sound; the completion safeguard needs consistent cross-process path resolution.

Verification: static delta and surrounding-code inspection only; no tests run.

@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: 72dcb0ae9a

ℹ️ 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/glue.rs
Comment on lines +692 to +695
(
format!(
"{binding} {local} = unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }};",
self.object_path(*i)

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 Share one guard for aliased read-only object arguments

For a Send + !Sync object containing a private Cell<u32>, an ordinary exported function such as read_two(a: &Thing, b: &Thing) aborts when C passes the same handle twice: each argument independently acquires an exclusive guard, so the second acquisition fails. A differential check using the generated glue returned 6 natively but aborted through the ABI with a handle is already borrowed. The existing _eq alias handling does not cover ordinary functions or a receiver aliasing another shared argument. Reuse one guard for aliased shared arguments within a call while retaining exclusive access against other calls.

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 e679853, in the runtime rather than per call site: a value that is not Sync is now shared on one thread at a time. Its state holds the owning thread, and depth counts that thread's borrows, so one handle passed twice (or a receiver aliasing an argument) is two shared borrows on one thread, as Rust allows. A borrow from another thread still aborts. Futures, which may be polled on any thread, keep holding such a value exclusively. The _eq special case went away with it. The api fixture's bump_both(&tally, &tally) matches the native transcript.

(written by 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: 46c5fa2

No new actionable bugs found in this delta. PID-specific markers and checking existence before cleanup are a sensible, narrow improvement.

Still open, not new:

  • Previous relative-marker-path P2 #41 (review) remains at driver/src/cargo.rs:144–155,163. Relative CARGO_TARGET_DIR is resolved from different working directories by parent and wrapper. Normalize the shared marker path to an absolute path.
  • Independent aliased read-only object argument finding #41 (comment) is unchanged by this driver-only update.

Earlier custom-Drop and rlib fixes remain intact. Static delta and surrounding-code inspection only; no tests run.

…lative target directory as the caller's

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated

Copy link
Copy Markdown
Collaborator Author

Review of 72dcb0a, the relative completion marker: fixed in e679853. A relative CARGO_TARGET_DIR now resolves against the caller's directory, as they meant it, before cargo runs in the package's. The marker is also per process, since concurrent runs share a target directory (the cause of 72dcb0a's CI failure). The new relative_target test runs from outside the package with CARGO_TARGET_DIR=build.

Correction: my replies on the Drop and rlib threads name 4f1dc55; the commit is 72dcb0a.

(written by 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: e679853

Both previously open findings are addressed:

  • Relative completion-marker path issue #41 (review) is fixed by resolving CARGO_TARGET_DIR to an absolute path before Cargo changes directories. A regression fixture invokes outside the package.
  • Aliased shared-object argument issue #41 (comment) is fixed for synchronous calls through same-thread borrow depth. Other threads, mutable borrows and moves remain excluded; future-held non-Sync objects retain exclusive guards. The C/native transcript covers the same-handle case.

No new actionable bugs found. Centralizing alias handling in the runtime is more consistent than special-casing equality; added complexity serves required borrowing semantics.

Verification: static delta and surrounding-code inspection only; tests not run.

(Written by OpenAI)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Ready to merge at e679853:

  • Decisions: all four maintainer decisions are recorded under ## Decisions. Decision 1 is applied to the quest tree: the nine paused fork quests and moq-poll-migrate are folded into quest/m1/ffi-export.md and deleted.
  • Main: merged in after feat(ts): MIR lowering spike, which moves the translator to MIR #36, with the shared driver taking ts's --lower option and the MIR job's extra rustc arguments.
  • Checks: just check and just test pass locally in the dev shell, and quest check (also --base origin/main) passes from outside the checkout. CI's Check passes on e679853.
  • Review: all eleven Codex findings, through the review of 72dcb0a, are fixed with fixture coverage and replied to. On e679853, Codex's security review completed with no findings. Its code review has shown running for about three hours without posting, so this merges on the reviewed findings being resolved.

(written by Opus 5.5)

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