-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose the unwinding crate's "personality" and "panic-handler" features.
The unwinding crate provides "personality" and "panic-handler" features which define real `#[lang = eh_personality]` and `#[panic_handler]` implementations that support unwinding. This follows the suggestion in sunfishcode/eyra#51.
- Loading branch information
1 parent
cb63742
commit d7ca156
Showing
10 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "c-scape-unwinding" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies.libc] | ||
path = "../../c-scape" | ||
# Disable the default features, enable "take-charge" mode, and enable the | ||
# "personality" and "panic-handler" features. | ||
default-features = false | ||
features = [ | ||
"take-charge", | ||
"thread", | ||
"call-main", | ||
"malloc-via-rust-global-alloc", | ||
"define-mem-functions", | ||
"threadsafe-setenv", | ||
"personality", | ||
"panic-handler", | ||
] | ||
package = "c-scape" | ||
|
||
[dependencies] | ||
errno = { version = "0.3.3", default-features = false } | ||
rustix-dlmalloc = { version = "0.1.0", features = ["global"] } | ||
# Depend on `unwinding` so that we can do `catch_unwind`. | ||
unwinding = { version = "0.2.2", default-features = false, features = ["panic"] } | ||
|
||
# This is just an example crate, and not part of the c-ward workspace. | ||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This crate demonstrates the use of c-scape in "take-charge" mode with `no_main` | ||
and `no_std`, and includes unwinding panic support. | ||
|
||
It's the same as [c-scape-example], but enables the "personality" and | ||
"panic-handler" features instead of defining stub `#[panic_handler]` and | ||
`#[lang = "eh_personality"]` functions, and performs an unwind. | ||
|
||
[c-scape-example]: https://github.com/sunfishcode/c-ward/tree/main/example-crates/c-scape-example#readme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main() { | ||
// Pass -nostartfiles to the linker. | ||
println!("cargo:rustc-link-arg=-nostartfiles"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! A simple example using `no_main`, `no_std`, and "take-charge" mode, using | ||
//! the "personaliity" and "panic_handler" to support unwinding. | ||
#![no_std] | ||
#![no_main] | ||
|
||
#[global_allocator] | ||
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc; | ||
|
||
#[no_mangle] | ||
unsafe extern "C" fn main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 { | ||
// Panic and catch it. | ||
unwinding::panic::catch_unwind(|| call_do_panic()).unwrap_err(); | ||
|
||
// Call functions declared in the `libc` crate, which will be resolved by | ||
// c-scape. c-scape doesn't have `printf`, so we do it by hand. | ||
let message = b"Hello, world!\n"; | ||
let mut remaining = &message[..]; | ||
while !remaining.is_empty() { | ||
match libc::write(1, message.as_ptr().cast(), message.len()) { | ||
-1 => match errno::errno().0 { | ||
libc::EINTR => continue, | ||
_ => panic!(), | ||
}, | ||
n => remaining = &remaining[n as usize..], | ||
} | ||
} | ||
libc::exit(0); | ||
} | ||
|
||
fn call_do_panic() { | ||
do_panic() | ||
} | ||
|
||
fn do_panic() { | ||
panic!("catch me!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters