Skip to content

Use libc's exit as the sandbox's SIGTERM handler - #28

Merged
anoek merged 1 commit into
coverage-testsfrom
fix-sigterm-handler
Oct 31, 2026
Merged

anoek merged 1 commit into
coverage-testsfrom
fix-sigterm-handler

Conversation

@anoek

@anoek anoek commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #26. This doesn't depend on the rest of the stack; it's here so it can land alongside it.

Fixes this warning:

warning: direct cast of function item into an integer
    --> src/sandbox/get_or_create.rs
     |
     |                 std::process::exit as libc::sighandler_t,
     = note: `#[warn(function_casts_as_integer)]` on by default

The compiler's suggested fix (as *const () as libc::sighandler_t) would only silence it. The underlying problem is that std::process::exit uses the Rust calling convention, while the kernel calls signal handlers with the C one. It only worked because the two agree for a single integer argument on x86_64 and aarch64.

The sandbox's PID 1 now registers libc::exit instead, through a typed function pointer:

let exit: unsafe extern "C" fn(libc::c_int) -> ! = libc::exit;
unsafe {
    libc::signal(libc::SIGTERM, exit as libc::sighandler_t);
}

The behavior doesn't change: SIGTERM exits PID 1 with status 15, and exit() still runs exit handlers. std::process::exit only adds flushing stdout, which is already closed at this point.

Why not a separate handler function

A Rust extern "C" fn handler that calls std::process::exit also works, but its body would always show as uncovered. In coverage builds, PID 1 dumps its profile early in its sleep loop, and LLVM ignores any later writes. #[coverage(off)] is still unstable.

Testing

  • New tests/ai_test_sigterm.rs checks that SIGTERM stops a sandbox's PID 1 and that the sandbox starts again afterwards. No existing test sent SIGTERM.
  • With this change alone on main, the full make test run passes: 285 passed, 0 failed. Every changed line is covered.
  • On this branch: ai_test_sigterm, ai_test_resource_limits, test_sandbox_inception and test_mount_unmount_kill pass, and the build has no warnings.

@anoek
anoek added this pull request to stack #27 October 31, 2026 13:22
@anoek
anoek removed this pull request from stack #27 October 31, 2026 13:30
@anoek
anoek added this pull request to stack #30 October 31, 2026 13:31
@anoek
anoek force-pushed the fix-sigterm-handler branch from b4cc02d to 2c7c994 Compare October 31, 2026 13:31
The sandbox's PID 1 registered std::process::exit as its SIGTERM handler
by casting it to sighandler_t, which rustc now warns about
(function_casts_as_integer). Beyond the warning, std::process::exit uses
the Rust calling convention while signal handlers are called with the C
one; it only worked because the two agree for a single integer argument
on the architectures we build for.

libc::exit has the C calling convention and does the same thing here
(stdout and stderr are already closed), so register it instead, through
a typed function pointer.

Also add a test that SIGTERM stops a sandbox, which nothing tested.
@anoek
anoek force-pushed the fix-sigterm-handler branch from 2c7c994 to f48ceda Compare October 31, 2026 14:14
@anoek
anoek merged commit bfad2f8 into main Oct 31, 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