Skip to content

fix(ffi): give async test callbacks an owned completion sender - #542

Merged
eric8810 merged 3 commits into
masterfrom
hui/abi-primitives-sigsegv
Nov 24, 2026
Merged

eric8810 merged 3 commits into
masterfrom
hui/abi-primitives-sigsegv

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

aimux_call_async hands an opaque user_data pointer back to the completion callback on a runtime thread. In aimux-ffi/tests/abi_primitives.rs, async_calls_fan_out_from_another_runtime_and_own_the_inputs and accepted_async_calls_report_dispatch_errors_and_abort_once passed the address of a local Sender there and relied on the test thread outliving every callback. Those tests only wait for the completions they expect, so after the last one they drop both channel endpoints, and the runtime thread can still be inside the send that delivered it.

std::sync::mpsc publishes a message before it notifies the receiver. The channel write stores the message, marks the slot ready, and only then wakes a receiver. A receiver woken between those two steps can return from recv_timeout, let the test run to the end, and free the channel, which releases the heap-allocated counter. The runtime thread then enters the notification path with a pointer to freed memory and follows stale wake-list links.

A fault there has no Rust panic line: it is a plain SIGSEGV on x86_64 Linux, which matches the intermittent CI failure this fixes.

Fix

Every submission now hands over an owned sender. The test builds Box::into_raw(Box::new(send.clone())).cast(), and the callback's first statement takes it back with Box::from_raw, holding it until send returns. The channel therefore cannot be freed while a callback is running.

A rejected submission returns an owned error and never invokes the callback. That is the contract in aimux-ffi/include/aimux.h, and it holds in aimux-ffi/src/abi.rs because every failure path returns before the task is spawned. The test therefore reclaims the sender itself when submission returns a non-null error, so each context is freed exactly once.

Scope

Two files change, and no production code:

The expected export count stays a literal. The list it is compared with already comes from header_exports(ROOT), so deriving the expectation from the same call would compare the list with itself and could not catch a missing or extra export. The other numbers in that file are macOS deployment targets and are untouched.

Why this is not reproduced locally

The window sits between two adjacent statements in the standard library, so it needs preemption at exactly that point; it has only been observed on a loaded x86_64 Linux CI runner. The machine available here is aarch64 macOS with stable toolchains only, which rules out AddressSanitizer, Miri and a Linux run, and CI is not rerun for diagnosis. The change removes the precondition instead of narrowing the timing: after it, no path can drop the channel while a callback holds it.

Fixes #541

Verification

  • cargo test -p aimux-ffi --test abi_primitives: 12 passed
  • cargo test -p aimux-ffi: 24 passed across the lib and integration targets
  • cargo fmt --all -- --check
  • cargo clippy -p aimux-ffi --all-targets -- -D warnings
  • RUSTDOCFLAGS='-D warnings' cargo doc -p aimux-ffi --no-deps

The async ABI tests borrowed a local `Sender` into the runtime callback. std's channel publishes a message before it notifies the receiver, so the test thread could observe the last completion and free the channel while the runtime thread was still inside `Sender::send`, leaving it to touch a released heap counter.

Pass an owned sender per submission and take it back at the top of the callback, so the channel stays alive until `send` returns. Rejected submissions never invoke the callback, so the test reclaims the sender itself when submission returns an error.
@eric8810

Copy link
Copy Markdown
Contributor

补充一个会让这个 PR 一直红的点:aimux-ffi (aarch64-apple-darwin) 现在挂在 scripts/tests/test_check_native_artifact.py:522 的 assertEqual(len(exports), 14),实际是 16 —— 不是本 PR 的问题,是 #538 给 C ABI 加了 aimux_call_async / aimux_stream_async 之后这个断言在 master 上就红了(707c591b、b3017666 的 master run 同样失败)。#537 rebase 后也会撞到同一个断言。建议在这个 PR 里顺手把 14 改成 16(或从 header_exports 推导期望值),这样 #542 合入后 master 恢复绿,#537/#532/#533 的 CI 也能过。本 PR 对 UAF 的修法本身没问题。

The C ABI gained aimux_call_async and aimux_stream_async, so the header now declares 16 exports and the end-to-end dylib check reports 16. This test still expected 14 and failed on both the count and the report line.

Keep the expected count a literal: exports is already header_exports(ROOT), so deriving the expectation from it would compare the list with itself.
@yyhhyyyyyy

Copy link
Copy Markdown
Contributor Author

Fixed in 9e18ce2. test_macho_dylib_exports_end_to_end now expects 16 exports in both places: the count assertion at line 522 and the 16 required C ABI exports present message at line 548.

I kept the number literal instead of deriving it from header_exports. The exports list on the line above is already sorted(check.header_exports(ROOT)), so deriving the expectation from the same call would compare the list with itself and could not catch a missing or extra export.

Before the change, python3 -I scripts/tests/test_check_native_artifact.py failed with AssertionError: 16 != 14 at line 522. After it, the same command reports 15 tests, 1 skipped (the llvm-objcopy case, which needs the llvm-tools component), OK. cargo test -p aimux-ffi, cargo fmt --all -- --check, cargo clippy -p aimux-ffi --all-targets -- -D warnings and RUSTDOCFLAGS='-D warnings' cargo doc -p aimux-ffi --no-deps are green on the same head.

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

UAF 修法正确:回调第一句 Box::from_raw 接管 sender,submission 被拒时由调用方回收,每个 context 恰好释放一次;无生产代码改动。9e18ce2f 把导出断言改成 16 后 aimux-ffi (aarch64-apple-darwin) 已绿,C ABI 也绿。Rust core 现在只红 named_minimax_presets_route_only_h3_ids_to_the_native_media_models,是 #534×#536 的语义冲突,与 #544 同一个问题,#544 修。Approve。

@eric8810

Copy link
Copy Markdown
Contributor

@yyhhyyyyyy 已 approve。这个 PR 的 CI 现在只剩 Rust core 红,原因是 #544(修 minimax 测试语义冲突)已经合入 master,但本分支还没带上那个修复。麻烦 rebase/merge master 一下,Rust core 就会绿,然后我直接合。

Brings the MiniMax preset routing fix from #544 so the Rust core job no longer fails on the #534/#536 semantic conflict.
@yyhhyyyyyy

Copy link
Copy Markdown
Contributor Author

Merged master (ec4c872) into the branch with merge commit 8605bb3, bringing in the #544 fix for the MiniMax preset routing failure. A merge commit was used instead of a rebase because the branch had already been pushed; CI is running on the new head.

@eric8810
eric8810 merged commit 8a5df14 into master Nov 24, 2026
20 checks passed
yyhhyyyyyy added a commit that referenced this pull request Nov 24, 2026
Master brought the C ABI primitives race fix (#542) and the MiniMax media preset routing fix (#544). They touch only test code on this branch: aimux-ffi/tests/abi_primitives.rs, aimux-providers/tests/minimax_media_test.rs and scripts/tests/test_check_native_artifact.py. There were no conflicts.
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.

[bug] abi_primitives crashes with SIGSEGV in the Rust core job

2 participants