summaryrefslogtreecommitdiffstats
path: root/vendor/psm/examples/panics.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/psm/examples/panics.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/psm/examples/panics.rs')
-rw-r--r--vendor/psm/examples/panics.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/psm/examples/panics.rs b/vendor/psm/examples/panics.rs
new file mode 100644
index 000000000..ada658d80
--- /dev/null
+++ b/vendor/psm/examples/panics.rs
@@ -0,0 +1,52 @@
+extern crate psm;
+
+use std::panic;
+
+const CHAIN_DEPTH: usize = 16;
+
+psm::psm_stack_manipulation! {
+ yes {
+ use std::alloc;
+ const STACK_ALIGN: usize = 4096;
+ // Generating backraces (because of RUST_BACKTRACE) create a few quite large frames, so it is
+ // important, that all frames have sufficient amount of available memory to not run over the
+ // stack...
+ const FRAME_SIZE: usize = 4096 * 10;
+
+ fn panic_chain(depth: usize) {
+ if depth == 0 {
+ panic!("full chain!");
+ } else {
+ unsafe {
+ let layout = alloc::Layout::from_size_align(FRAME_SIZE, STACK_ALIGN).unwrap();
+ let new_stack = alloc::alloc(layout);
+ assert!(!new_stack.is_null(), "allocations must succeed!");
+ let p = psm::on_stack(new_stack, FRAME_SIZE, || {
+ panic::catch_unwind(|| {
+ panic_chain(depth - 1);
+ })
+ });
+ alloc::dealloc(new_stack, layout);
+ p.map_err(panic::resume_unwind).unwrap()
+ }
+ }
+ }
+
+ fn main() {
+ panic_chain(CHAIN_DEPTH);
+ }
+
+ #[test]
+ fn run_example() {
+ assert!(panic::catch_unwind(|| {
+ panic_chain(CHAIN_DEPTH);
+ }).is_err(), "Panic did not propagate!");
+ }
+ }
+
+ no {
+ fn main() {
+ eprintln!("Stack manipulation not supported by this target");
+ }
+ }
+}