-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.rs
More file actions
129 lines (108 loc) · 4.16 KB
/
Copy pathmain.rs
File metadata and controls
129 lines (108 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::any::Any;
use std::mem::ManuallyDrop;
use std::panic::{AssertUnwindSafe, catch_unwind, panic_any, resume_unwind};
use std::sync::atomic::{AtomicBool, Ordering};
static PANICKING_DROP_RAN: AtomicBool = AtomicBool::new(false);
static FIELD_AFTER_PANIC_DROPPED: AtomicBool = AtomicBool::new(false);
struct PanicsOnDrop;
impl Drop for PanicsOnDrop {
fn drop(&mut self) {
PANICKING_DROP_RAN.store(true, Ordering::SeqCst);
panic!("single panic from Drop");
}
}
struct RecordsDrop;
impl Drop for RecordsDrop {
fn drop(&mut self) {
FIELD_AFTER_PANIC_DROPPED.store(true, Ordering::SeqCst);
}
}
struct PanickingAggregate(RecordsDrop);
impl Drop for PanickingAggregate {
fn drop(&mut self) {
PANICKING_DROP_RAN.store(true, Ordering::SeqCst);
panic!("single panic from Drop");
}
}
#[repr(C)]
struct FormattingFields {
prefix: [u64; 11],
value: usize,
suffix: [u64; 12],
}
unsafe extern "C" {
#[link_name = "jvm:static:java/lang/Math:addExact"]
fn java_add_exact(left: i32, right: i32) -> i32;
}
fn move_plain_panic_payload(payload: Box<dyn Any>) -> Box<dyn Any> {
ManuallyDrop::into_inner(ManuallyDrop::new(payload))
}
fn main() {
let plain_payload = move_plain_panic_payload(Box::new(73_u32));
assert_eq!(plain_payload.downcast_ref::<u32>(), Some(&73));
let fields = FormattingFields {
prefix: [0; 11],
value: 2809,
suffix: [0; 12],
};
assert_eq!(format!("{}", fields.value), "2809");
let literal = catch_unwind(|| panic!("literal payload"))
.expect_err("literal panic should unwind");
assert_eq!(
literal.downcast_ref::<&'static str>().copied(),
Some("literal payload")
);
let formatted = catch_unwind(|| {
let value = 999_u64;
panic!("This is a formatted panic message: {}", value);
})
.expect_err("panic! must unwind into catch_unwind");
assert_eq!(
formatted.downcast_ref::<String>().map(String::as_str),
Some("This is a formatted panic message: 999")
);
let typed = catch_unwind(|| panic_any(1234_u32))
.expect_err("panic_any must preserve its typed payload");
assert_eq!(typed.downcast_ref::<u32>(), Some(&1234));
let resumed = catch_unwind(AssertUnwindSafe(|| resume_unwind(typed)))
.expect_err("resume_unwind must be caught by the next unwind boundary");
assert_eq!(resumed.downcast_ref::<u32>(), Some(&1234));
let unit = catch_unwind(AssertUnwindSafe(|| resume_unwind(Box::new(()))))
.expect_err("a zero-sized panic payload must unwind");
assert_eq!(unit.downcast_ref::<()>(), Some(&()));
let foreign = catch_unwind(|| unsafe { java_add_exact(i32::MAX, 1) })
.expect_err("a foreign JVM exception must unwind into catch_unwind");
let message = foreign
.downcast_ref::<String>()
.expect("foreign JVM failures should carry a diagnostic String");
assert!(message.contains("java.lang.ArithmeticException"));
assert!(message.contains("integer overflow"));
assert_eq!(unsafe { java_add_exact(20, 22) }, 42);
let add_exact: unsafe extern "C" fn(i32, i32) -> i32 = java_add_exact;
assert_eq!(unsafe { add_exact(19, 23) }, 42);
let drop_panic = catch_unwind(|| {
let _value = PanicsOnDrop;
})
.expect_err("a single panic during normal Drop must remain catchable");
assert!(PANICKING_DROP_RAN.load(Ordering::SeqCst));
assert_eq!(
drop_panic.downcast_ref::<&'static str>().copied(),
Some("single panic from Drop")
);
PANICKING_DROP_RAN.store(false, Ordering::SeqCst);
FIELD_AFTER_PANIC_DROPPED.store(false, Ordering::SeqCst);
let aggregate_drop_panic = catch_unwind(|| {
drop(PanickingAggregate(RecordsDrop));
})
.expect_err("an aggregate field panic must unwind after later fields are dropped");
assert_eq!(
aggregate_drop_panic
.downcast_ref::<&'static str>()
.copied(),
Some("single panic from Drop")
);
assert!(PANICKING_DROP_RAN.load(Ordering::SeqCst));
assert!(FIELD_AFTER_PANIC_DROPPED.load(Ordering::SeqCst));
let hook = std::panic::take_hook();
std::panic::set_hook(hook);
}