Repository navigation
feat(ffi): generate C++ bindings over the C ABI - #163
Conversation
Quest-Claim: d41b916a419f47a5
Writes include/<prefix>.hpp beside the C header: header-only C++17/20/23, with exceptions and RTTI on or off, and futures that poll, block, call back on an executor, or co_await. C's keyword escape now covers C++ keywords, so the C header compiles as C++. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A closure lent for the call is a template parameter in C++, called through a noexcept trampoline that lends each object as its class and gives the handle back unfreed, aborting if the call moved it out. The header checks the library's contract at load. moq.cpp follows moq.c onto owned paths and catalog::Producer::mutate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9f80f92c6d
ℹ️ 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".
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9f80f92c6d7a96c79886709c0b0e55c91f728c6a.
Two actionable buffer-conversion issues remain, already reported independently; referencing them rather than duplicating inline comments:
ffi/src/cpp.rs:167: an engaged optional empty C++buflowers its null storage pointer asNone. Normalize throughviewbefore lowering, preservingSome(empty). Existing finding.ffi/runtime/rt.hpp:119-122:view(nullptr, nonzero)substitutes a one-byte sentinel without changing the length, bypassing Rust's null-buffer validation and allowing out-of-bounds reads. Reject that malformed input; reserve the sentinel for empty buffers. Existing finding.
Overall direction is sound: header-only wrappers reuse the single C ABI, and explicit executor ownership avoids adding a background runtime. The wake latch and cancellation synchronization justify their complexity. No additional actionable bugs found in the inspected generator, runtime, ABI changes and tests.
Verification: static review only; I did not run builds, differential tests, sanitizers or platform compiler checks.
(Written by OpenAI)
…h a length An engaged optional buffer lowers through a view, so an empty buffer C++ made crosses as Some, not None. A view of null bytes with a nonzero length aborts instead of standing in a one-byte sentinel for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9761307a3305e665b7dbb9fcdb3b050bd1361338, compared with 9f80f92c6d7a96c79886709c0b0e55c91f728c6a.
Both findings referenced in my previous review are fixed: optional buffers now lower through view, preserving Some(empty), and null data with nonzero length aborts before sentinel substitution. The native/C/C++ transcript additions distinguish empty text from absent text, and the C++ abort case covers malformed views.
No new actionable bugs found in this update. These are focused fixes at the shared conversion and validation boundaries, with no additional machinery needed; the overall direction remains sound.
Verification: inspected the complete one-commit delta and relevant surrounding code. Static review only; I did not run builds or tests.
(Written by OpenAI)
|
Landing at 9761307, reviewed clean by Codex.
(Written by Claude Opus 5.5) |
Problem
The exporter only emits C. moq ships C++ (its OBS plugin builds C++17 and C17), and the epic needs a C++ generator over the one C ABI, with native futures, that builds at C++17, 20, and 23 with exceptions and RTTI on or off.
Approach
src/cpp.rswritesinclude/<prefix>.hppbeside the C header on every export. It's header-only and has no config. The runtime it shares,runtime/rt.hpp, holdsview,buf,result,waker,future, andtask. It throws nothing and reads no type information._, as does a member named as its class or one of its fields. Two members that still clash are an export error.Clone, where a copy clones.&selfmethods areconst, andselfmethods are&&.enum class, or for an enum with data astd::variantof a struct per variant, with methods and traits as free functions. Traits are abstract classes passed as aunique_ptr. Bytes go in as aviewand come back as abufthat owns Rust's buffer with no copy.poll(waker)polls once.get()blocks the thread.then(exec, cb)polls on a caller-given executor and returns ataskthat cancels when destroyed.co_await f.via(exec)resumes the coroutine on the executor.noexcepttrampoline lends each object as a reference to its class for that call, then gives the handle back unfreed. If the call moved the object out, it aborts.<prefix>_contract()against<PREFIX>_CONTRACTat load, before any other call, in an inline variable's initializer (feat(ffi): a contract symbol each header and library check #146). A mismatch aborts.namespace). C's keyword escape now covers C++ keywords too.tests/api.cppruns the transcript script through the C++ header at C++17, 20, and 23, with no exceptions or RTTI at 17 and 23. Its output must match native Rust line for line.api::yielded, wakes itself before returning pending), a cancel between wake and poll losing nothing, cancel from the callback, and a coroutine resumed or destroyed. It also checks the abort paths.into_labelor keeps it past the call. A program whose header names another contract aborts at load.moq/moq.cppportsmoq/moq.c, including owned paths andcatalog::Producer::mutate, against the real moq library at C++17 without exceptions or RTTI, injust ffi moq.moq/bench/export.cppjoinsjust ffi bench, and both benches gained a read path.Numbers
just ffi bench's export lines, 3 runs, local and loaded host (ns per 100 B frame):Generated size for moq's export:
moq.hppis 1,312,535 B raw and 228,494 B gzipped, on top ofmoq.h(708,988 B raw, 173,586 B gzipped). The fork's C++ for moq-ffi is 809,789 B raw and 98,503 B gzipped. That's not like for like: the export covers the full pub API of 7 crates, while moq-ffi is a hand-picked surface.Impact
include/<prefix>.hppon every export._in C (moq'snamespacebecomes_namespace). Seeffi/MIGRATE.md.view,result,task,future,poll_t,detail) and every variant's payload name<enum>_<variant>are now claimed, so an item named one of them clashes. Seeffi/MIGRATE.md.Choices made without asking
No exceptions anywhere.
result<T, E>is its own type, andvalue()on an error aborts. It isn'tstd::expectedat C++23, because the type would differ by standard, and that's an ODR violation when translation units mix standards (OBS's C++17 plugin inside a C++20 app). For the same reason,viais declared at every standard and only defined from C++20.The C++ header is always written beside the C header, with no flag. A C++-only member clash therefore fails a C-only export too.
Payload structs are named
<enum>_<variant>even for variants without fields, and that name is claimed in C.The waker/future latch: each poll registers the same waker node, so Rust reuses it. A wake that lands mid-poll sets a flag and re-posts the poll to the executor instead of looping, so a self-waking future can't starve the executor.
TSan on a cancel/wake stress test reports races only where the happens-before edge goes through Rust's
Arccount, which isn't instrumented. ASan/UBSan/LSan stay clean over 60k iterations, and the counts match, so I'm treating them as false positives.A lent closure is a template parameter, not a type-erased
function_ref. It needs no runtime type and no second indirect call, and its trampoline isnoexcept, so a throwing callable terminates instead of unwinding through Rust.Decisions
Settled by the maintainer, keeping each as built:
newmaps to: ✅new_, by the one keyword rule. Rejected: a C++ constructor (a falliblenewcan't be one without exceptions);init, as moq's fork-based C++ has it.Clone. Rejected:std::shared_ptr<T>everywhere, as the fork has.Alternatives
.cppbeside the header, as the fork does: a header is simpler to ship and to consume.std::chronoforDuration: Rust's range doesn't fitstd::chrono::nanoseconds, sodurationstays the exact C struct.Checks
just ffi test: pass (14 tests), including the C++ matrix with GCC 16, after merging main.just ffi moq: C and C++ both pass, after merging main.nix shell nixpkgs#clang): all 6 standard × exceptions/RTTI combinations pass the transcript and the futures checks.api.cppand on a cancel/wake stress test: clean, before the merge.just check: passFollow-ups
quest/m1/ffi-export/cpp.mdis rewritten to what remains: MSVC, Apple Clang, and Clang in CI.bench/ffiC++ runner through this header once moq's C++ package moves off the fork.🤖 Generated with Claude Code
(Written by Claude Opus 5.5)