39
39
//! caller must ensure:
40
40
//! - The stack size provided is large enough for the closure to run with.
41
41
//! - The closure does not unwind or return control flow by any means other than
42
- //! directly returning. `std` users do not need to worry about this due to
42
+ //! directly returning. `std` users do not need to worry about this due to
43
43
//! the existence of `catch_unwind`.
44
44
//!
45
45
//! ## `nostd` Support
46
- //!
46
+ //!
47
47
//! This crate is compatible with `nostd` environments, but it is less safe
48
- //! in the event that your stack-switched stack panics. Panicking on a separate
49
- //! stack can cause undefined behavior (UB), but if it can be caught with
48
+ //! in the event that your stack-switched stack panics. Panicking on a separate
49
+ //! stack can cause undefined behavior (UB), but if it can be caught with
50
50
//! `std::panic::catch_unwind`, that aspect of the safety should be more safe.
51
- //!
51
+ //!
52
52
//! ## Use Cases
53
53
//!
54
54
//! - Cryptographic routines
@@ -66,10 +66,7 @@ extern crate std;
66
66
#[ cfg( feature = "std" ) ]
67
67
use core:: any:: Any ;
68
68
#[ cfg( feature = "std" ) ]
69
- use std:: {
70
- boxed:: Box ,
71
- panic:: catch_unwind,
72
- } ;
69
+ use std:: { boxed:: Box , panic:: catch_unwind} ;
73
70
#[ cfg( feature = "std" ) ]
74
71
type StackSwitchResult < T > = Result < T , Box < dyn Any + Send > > ;
75
72
#[ cfg( not( feature = "std" ) ) ]
@@ -79,7 +76,7 @@ use core::panic::{AssertUnwindSafe, UnwindSafe};
79
76
80
77
#[ derive( Debug ) ]
81
78
enum Error {
82
- StackPanicked
79
+ StackPanicked ,
83
80
}
84
81
85
82
psm:: psm_stack_manipulation! {
@@ -98,23 +95,23 @@ psm::psm_stack_manipulation! {
98
95
/// * `crypto_fn` - the code to run while on the separate stack.
99
96
///
100
97
/// ## Panicking
101
- ///
98
+ ///
102
99
/// This function panics when `psm` detects that `on_stack` is unavailable.
103
- ///
100
+ ///
104
101
/// ## Errors
105
- ///
106
- /// With the `std` feature enabled, this function will result in an error when
107
- /// the closure panics. You may want to log these errors securely, privately,
108
- /// as cryptography panics could be a little revealing if displayed to
102
+ ///
103
+ /// With the `std` feature enabled, this function will result in an error when
104
+ /// the closure panics. You may want to log these errors securely, privately,
105
+ /// as cryptography panics could be a little revealing if displayed to
109
106
/// the end user.
110
- ///
107
+ ///
111
108
/// ## Debugging
112
- ///
113
- /// Using `#[inline(never)]` on the closure's function definition could
109
+ ///
110
+ /// Using `#[inline(never)]` on the closure's function definition could
114
111
/// make it easier to debug as the function should show up.
115
- ///
112
+ ///
116
113
/// # Safety
117
- ///
114
+ ///
118
115
/// * The stack needs to be large enough for `crypto_fn()` to execute without
119
116
/// overflow.
120
117
/// * `nostd` only: `crypto_fn()` must not unwind or return control flow by any other means
@@ -128,7 +125,7 @@ psm::psm_stack_manipulation! {
128
125
"Stack size must be greater than 0 kb and `* 1024` must not overflow `isize`"
129
126
) ;
130
127
let mut stack = create_aligned_vec( stack_size_kb as usize , align_of:: <u128 >( ) ) ;
131
-
128
+
132
129
let res = unsafe {
133
130
psm:: on_stack( stack. as_mut_ptr( ) , stack. len( ) , || {
134
131
#[ cfg( not( feature = "std" ) ) ]
0 commit comments