bytecodealliance / rustix

Safe Rust bindings to POSIX-ish APIs
Other
1.46k stars 152 forks source link

Add `rustix::process::exit` to terminate processes #1133

Open danielschemmel opened 1 month ago

danielschemmel commented 1 month ago

This PR adds rustix::process::exit to exit the current process. This is useful for no_std code, which cannot call std::process::exit.

The kernel backend uses the exit_group syscall, which is equivalent to the _exit/_Exit POSIX/C functions. For consistency, I also used the _exit function for the C backend.

There are two main ways in which this could be changed:

  1. Naming the function rustix::process::_exit instead, which would differ from the usual Rust naming scheme.
  2. Calling exit in the C backend instead, which would call at_exit functions only when the C backend is enabled.

This PR is additive and should not require a semver bump.

danielschemmel commented 1 month ago

Generally speaking, any no_std Rust code that wants to exit in a similar fashion to std::process::exit (rather than returning through main), for example when having to write a panic handler.

For the specific use-case, we are writing a no_std rust library that needs to be as unintrusive as possible, when linked into the target process. Critically, calling libc is problematic (we are currently doing a very hacky dance of a statically linked and symbol-renamed musl, and would really like to get rid of it completely).

The point of this library is to serve as a runtime for testing via symbolic execution, which means we really want to run below the "user-facing libc". I am currently trying to move away from this multiple-but-different libc approach and make our runtime (which is mostly written in Rust) be pure Rust, ideally using the x86_64-unknown-linux-none target.

The OS APIs that we are currently using are mostly covered by Rustix, but sometimes we need to immediately exit without going through user code (the code under test is after all by definition untrustworthy).

danielschemmel commented 1 month ago

Turns out that I was not quite thorough enough, and that rustix::runtime::exit_group was exactly what I was looking for, I just could not find it without your comments.

I still think that a more discoverable function (probably in rustix::process) would be good for more general no_std libraries, what do you think?