Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the unwinding dependency conditional on panic = "unwind". #153

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c-scape/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-all
# TODO: Eventually, we should propose a `fde-phdr-rustix` backend option to
# upstream `unwinding` so that it doesn't need to go through `dl_iterate_phdr`,
# but `fde-phdr-dl` works for now.
[target.'cfg(not(target_arch = "arm"))'.dependencies.unwinding]
[target.'cfg(all(panic = "unwind", not(target_arch = "arm")))'.dependencies.unwinding]
version = "0.2.3"
default-features = false
features = [
Expand Down
104 changes: 0 additions & 104 deletions c-scape/src/unwind.rs

This file was deleted.

2 changes: 2 additions & 0 deletions example-crates/c-gull-example-panic-abort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
26 changes: 26 additions & 0 deletions example-crates/c-gull-example-panic-abort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "c-gull-example-panic-abort"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.libc]
path = "../../c-gull"
default-features = false
features = [
"take-charge",
"std",
"thread",
"call-main",
"malloc-via-crates",
"threadsafe-setenv"
]
package = "c-gull"

[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
3 changes: 3 additions & 0 deletions example-crates/c-gull-example-panic-abort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Like the [c-gull-example example], but uses panic=abort in Cargo.toml.

[c-gull-example example]: https://github.com/sunfishcode/c-ward/blob/main/example-crates/c-gull-example/README.md
4 changes: 4 additions & 0 deletions example-crates/c-gull-example-panic-abort/build.rs
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");
}
8 changes: 8 additions & 0 deletions example-crates/c-gull-example-panic-abort/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! A simple example using "take-charge" mode.

fn main() {
println!("Hello world using Rust `println!`!");
unsafe {
libc::printf("Hello world using libc `printf`!\n\0".as_ptr().cast());
}
}
2 changes: 2 additions & 0 deletions example-crates/c-gull-unwinding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
21 changes: 21 additions & 0 deletions example-crates/c-gull-unwinding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "c-gull-unwinding"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.libc]
path = "../../c-gull"
default-features = false
features = [
"take-charge",
"std",
"thread",
"call-main",
"malloc-via-crates",
"threadsafe-setenv"
]
package = "c-gull"

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
6 changes: 6 additions & 0 deletions example-crates/c-gull-unwinding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This crate demonstrates the use of c-gull in "take-charge" mode, and includes
unwinding panic support.

It's the same as [c-gull-example], but it performs an unwind.

[c-gull-example]: https://github.com/sunfishcode/c-ward/tree/main/example-crates/c-gull-example#readme
4 changes: 4 additions & 0 deletions example-crates/c-gull-unwinding/build.rs
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");
}
19 changes: 19 additions & 0 deletions example-crates/c-gull-unwinding/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! A simple example using `std::panic::catch_unwind`.

fn main() {
// Panic and catch it.
std::panic::catch_unwind(|| call_do_panic()).unwrap_err();

println!("Hello, world!");
unsafe {
libc::printf("Hello world using libc `printf`!\n\0".as_ptr().cast());
}
}

fn call_do_panic() {
do_panic()
}

fn do_panic() {
panic!("catch me!");
}
2 changes: 2 additions & 0 deletions example-crates/c-scape-example-panic-abort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
34 changes: 34 additions & 0 deletions example-crates/c-scape-example-panic-abort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "c-scape-example"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.libc]
path = "../../c-scape"
# Disable the default features, and enable "take-charge" mode.
default-features = false
features = [
"take-charge",
"thread",
"call-main",
"malloc-via-rust-global-alloc",
"threadsafe-setenv",
# This simple example will never unwind.
"eh-personality-continue",
# This simple example will never panic.
"panic-handler-trap",
"global-allocator",
]
package = "c-scape"

[dependencies]
errno = { version = "0.3.3", default-features = false }

[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
3 changes: 3 additions & 0 deletions example-crates/c-scape-example-panic-abort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Like the [c-scape-example example], but uses panic=abort in Cargo.toml.

[c-scape-example example]: https://github.com/sunfishcode/c-ward/blob/main/example-crates/c-scape-example/README.md
4 changes: 4 additions & 0 deletions example-crates/c-scape-example-panic-abort/build.rs
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");
}
22 changes: 22 additions & 0 deletions example-crates/c-scape-example-panic-abort/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! A simple example using `no_main`, `no_std`, and "take-charge" mode.

#![no_std]
#![no_main]

#[no_mangle]
unsafe extern "C" fn main(_argc: i32, _argv: *const *const u8, _envp: *const *const u8) -> i32 {
// 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(libc::STDOUT_FILENO, message.as_ptr().cast(), message.len()) {
-1 => match errno::errno().0 {
libc::EINTR => continue,
_ => panic!(),
},
n => remaining = &remaining[n as usize..],
}
}
libc::exit(0);
}
2 changes: 1 addition & 1 deletion example-crates/c-scape-unwinding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ and `no_std`, and includes unwinding panic support.

It's the same as [c-scape-example], but enables the "eh-personality" and
"panic-handler" features instead of defining stub `#[panic_handler]` and
`#[lang = "eh_personality"]` functions, and performs an unwind.
`#[lang = "eh_personality"]` functions, and it performs an unwind.

[c-scape-example]: https://github.com/sunfishcode/c-ward/tree/main/example-crates/c-scape-example#readme
36 changes: 36 additions & 0 deletions tests/example_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ fn example_crate_c_gull_example() {
);
}

#[test]
fn example_crate_c_gull_example_panic_abort() {
test_crate(
"c-gull-example-panic-abort",
&[],
&[],
"Hello world using Rust `println!`!\nHello world using libc `printf`!\n",
"",
None,
);
}

#[test]
fn example_crate_c_gull_lto() {
test_crate(
Expand Down Expand Up @@ -102,6 +114,18 @@ fn example_crate_c_scape_example() {
test_crate("c-scape-example", &[], &[], "Hello, world!\n", "", None);
}

#[test]
fn example_crate_c_scape_example_panic_abort() {
test_crate(
"c-scape-example-panic-abort",
&[],
&[],
"Hello, world!\n",
"",
None,
);
}

#[test]
fn example_crate_c_scape_unwinding() {
test_crate(
Expand All @@ -114,6 +138,18 @@ fn example_crate_c_scape_unwinding() {
);
}

#[test]
fn example_crate_c_gull_unwinding() {
test_crate(
"c-gull-unwinding",
&[],
&[("RUST_BACKTRACE", "0")],
"Hello, world!\nHello world using libc `printf`!\n",
"thread 'main' panicked at src/main.rs:18:5:\ncatch me!\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n",
None,
);
}

#[test]
fn example_crate_dns() {
test_crate(
Expand Down