Skip to content

C: render Duration as int64_t nanoseconds (v0.11.0-kixelated.7+v0.32.2) - #6

Merged
kixelated merged 3 commits into
kixelated/uniffi-0.32from
kixelated/c-duration
Oct 24, 2026
Merged

kixelated merged 3 commits into
kixelated/uniffi-0.32from
kixelated/c-duration

Conversation

@kixelated

@kixelated kixelated commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

The C backend renders uniffi's builtin Duration instead of refusing it, so moq-ffi can export typed durations without losing the generated C. Pinned as a draft by moq-dev/moq#6683.

C

  • Duration is <namespace>_duration, a typedef int64_t count of nanoseconds, mirroring the C++ binding's std::chrono::duration<int64_t, std::nano>. The typedef puts the unit in every field, argument, and return; it is emitted only when the interface uses a duration.
  • It is a scalar to the rest of the backend: Option<Duration> is <namespace>_optional_duration { has_value, value }, and lists and maps work as for any scalar.

C++ (also reached by C)

The C++ converter, which the C backend lowers and lifts through, silently corrupted out-of-range values:

  • Writing a negative duration sent its negative seconds as a u64, so Rust saw a duration of about 584 billion years.
  • Reading a Rust duration past INT64_MAX nanoseconds (about 292 years) overflowed int64_t, which is undefined.

Both now fail through the existing macros::fail: a throw under the exceptions style, an abort with a message under the expected style the C backend builds on. Every value in between round-trips as before. The negative check sits in allocation_size, which every lower runs before it allocates anything. The first version checked in write, which threw after the buffer was allocated, and memcheck caught the leak in chronological-test.

Tests

  • c_api fixture: CapiTimer with a Duration field, an Option<Duration>, and a Vec<Duration>. capi_twice and capi_halve cross a duration and an optional one in both directions, and capi_make_duration builds one on the Rust side. The C test round-trips values across the seconds boundary and exactly INT64_MAX.
  • c-negative-duration-test and c-long-duration-test run c_api-test with an argument and match the abort message, the way expected-value-on-error-test does. Both fail on the old converter.
  • chronological-test expects a negative duration to throw under the exceptions style; it aborted on the old converter.
  • The generator's own collision checks refused the first fixture names, capi_double (it becomes double under strip_function_prefix) and capi_duration (it clashes with the new typedef), so those checks cover durations too.

Run locally: the full ctest suite (60 tests, including valgrind memcheck) on Linux with GCC 16, cargo fmt --check, and cargo clippy (no new warnings). CI passes on Linux (GCC, Clang), macOS (GCC, Clang), and Windows (MSVC).

Known gap, not new

Under the exceptions style, a throw while lowering a later argument still leaks the buffers of arguments already lowered. Every converter that can throw has this problem, so it is left for a separate change.

Release

As in #5, a commit bumps the version to v0.11.0-kixelated.7+v0.32.2 and moves the changelog entry under it, so the moq pin's hashes stay the same once the tag exists. The leak fix landed after it and changes no version. Merging and tagging are the maintainer's call; nothing is tagged.

(Written by Claude Opus 5.5)

kixelated and others added 2 commits October 24, 2026 12:19
The C backend maps uniffi's Duration to <namespace>_duration, a typedef of
int64_t nanoseconds mirroring the C++ binding's std::chrono::nanoseconds.

The C++ converter now fails on a negative duration, which reached Rust as
a huge one, and on one from Rust past INT64_MAX nanoseconds, which
overflowed. Both abort under the expected style the C backend builds on.

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

chatgpt-codex-connector Bot commented Oct 24, 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-24T19:28:01.035959Z ca955d8 New commits
🔒 Security Review ✅ Completed 2026-10-24T19:30:56.825789Z ca955d8 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.

Refusing it in write threw after lower had allocated the buffer, which
leaked under the exceptions style; memcheck caught it in chronological-test.

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

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

ℹ️ 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 on lines +30 to +32
if (secs > static_cast<uint64_t>(max_secs.count()) ||
(secs == static_cast<uint64_t>(max_secs.count()) && nanos > max_nanos)) {
{% call macros::fail("Duration does not fit in int64_t nanoseconds") %}

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 Free the owning RustBuffer when duration validation throws

Under the C++ exceptions style, receiving a Rust duration above INT64_MAX nanoseconds now throws from read(), bypassing the rustbuffer_free(buf) that follows it in lift(). RustStream does not own or release the buffer, so catching this exception leaks the returned Rust allocation on every call. The same problem affects enclosing records and collections whose readers encounter an oversized duration, potentially leaking a much larger buffer. Make the owning lift paths exception-safe so rejecting an out-of-range duration also releases its buffer.

Useful? React with 👍 / 👎.

kixelated added a commit to moq-dev/moq that referenced this pull request Oct 24, 2026
Pins the fork commit that renders UniFFI's Duration in the generated C,
kixelated/uniffi-bindgen-cpp#6, until it is tagged
v0.11.0-kixelated.7+v0.32.2. Finishes quest/m1/c/duration.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated merged commit a5a1ac4 into kixelated/uniffi-0.32 Oct 24, 2026
5 checks passed
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