This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathstate.rs
More file actions
234 lines (210 loc) · 7.26 KB
/
state.rs
File metadata and controls
234 lines (210 loc) · 7.26 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::instance::siginfo_ext::SiginfoExt;
use crate::instance::{FaultDetails, TerminationDetails, YieldedVal};
use crate::sysdeps::UContext;
use libc::{SIGBUS, SIGSEGV};
use std::any::Any;
use std::ffi::{CStr, CString};
/// The representation of a Lucet instance's state machine.
pub enum State {
/// The instance is freshly-created, but needs to run the start section before running other entrypoints.
///
/// Transitions to `Running` when the start section is run, and then to `Ready` if it succeeds.
NotStarted,
/// The instance is ready to run.
///
/// Transitions to `Running` when the instance is run, or to `Ready` when it's reset.
Ready,
/// The instance is running.
///
/// Transitions to `Ready` when the guest function returns normally, or to `Faulted`,
/// `Terminating`, or `Yielding` if the instance faults, terminates, or yields, or to
/// `BoundExpired` if the instance is run with an instruction bound and reaches it.
Running {
/// Indicates whether the instance is running in an async context (`Instance::run_async`)
/// or not. Needed by `Vmctx::block_on`.
async_context: bool,
},
/// The instance has faulted, potentially fatally.
///
/// Transitions to `Faulted` when filling in additional fault details, to `Running` if
/// re-running a non-fatally faulted instance, or to `Ready` or `NotStarted` when the instance
/// is reset.
Faulted {
details: FaultDetails,
siginfo: libc::siginfo_t,
context: UContext,
},
/// The instance is in the process of terminating.
///
/// Transitions only to `Terminated`; the `TerminationDetails` are always extracted into a
/// `RunResult` before anything else happens to the instance.
Terminating { details: TerminationDetails },
/// The instance has terminated, and must be reset before running again.
///
/// Transitions to `Ready` or `NotStarted` if the instance is reset.
Terminated,
/// The instance is in the process of yielding.
///
/// Transitions only to `Yielded`; the `YieldedVal` is always extracted into a
/// `RunResult` before anything else happens to the instance.
Yielding {
val: YieldedVal,
/// A phantom value carrying the type of the expected resumption value.
///
/// Concretely, this should only ever be `Box<PhantomData<R>>` where `R` is the type
/// the guest expects upon resumption.
expecting: Box<dyn Any>,
},
/// The instance has yielded.
///
/// Transitions to `Running` if the instance is resumed, or to `Ready` or `NotStarted` if the
/// instance is reset.
Yielded {
/// A phantom value carrying the type of the expected resumption value.
///
/// Concretely, this should only ever be `Box<PhantomData<R>>` where `R` is the type
/// the guest expects upon resumption.
expecting: Box<dyn Any>,
},
/// The instance has reached an instruction-count bound.
///
/// Transitions to `Running` if the instance is resumed, or to `Ready` or `NotStarted` if the
/// instance is reset.
BoundExpired,
/// A placeholder state used with `std::mem::replace()` when a new state must be constructed by
/// moving values out of an old state.
///
/// This is used so that we do not need a `Clone` impl for this type, which would add
/// unnecessary constraints to the types of values instances could yield or terminate with.
///
/// It is an error for this state to appear outside of a transition between other states.
Transitioning,
}
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::NotStarted => write!(f, "not started"),
State::Ready => write!(f, "ready"),
State::Running {
async_context: false,
} => write!(f, "running"),
State::Running {
async_context: true,
} => write!(f, "running (in async context)"),
State::Faulted {
details, siginfo, ..
} => {
write!(f, "{}", details)?;
write!(
f,
" triggered by {}: ",
strsignal_wrapper(siginfo.si_signo)
.into_string()
.expect("strsignal returns valid UTF-8")
)?;
if siginfo.si_signo == SIGSEGV || siginfo.si_signo == SIGBUS {
// We know this is inside the heap guard, because by the time we get here,
// `lucet_error_verify_trap_safety` will have run and validated it.
write!(
f,
" accessed memory at {:p} (inside heap guard)",
siginfo.si_addr_ext()
)?;
}
Ok(())
}
State::Terminated { .. } => write!(f, "terminated"),
State::Terminating { .. } => write!(f, "terminating"),
State::Yielding { .. } => write!(f, "yielding"),
State::Yielded { .. } => write!(f, "yielded"),
State::BoundExpired => write!(f, "bound expired"),
State::Transitioning { .. } => {
write!(f, "transitioning (IF YOU SEE THIS, THERE'S PROBABLY A BUG)")
}
}
}
}
impl State {
pub fn is_not_started(&self) -> bool {
if let State::NotStarted { .. } = self {
true
} else {
false
}
}
pub fn is_ready(&self) -> bool {
if let State::Ready { .. } = self {
true
} else {
false
}
}
pub fn is_running(&self) -> bool {
if let State::Running { .. } = self {
true
} else {
false
}
}
pub fn is_running_async(&self) -> bool {
if let State::Running { async_context } = self {
*async_context
} else {
false
}
}
pub fn is_faulted(&self) -> bool {
if let State::Faulted { .. } = self {
true
} else {
false
}
}
pub fn is_fatal(&self) -> bool {
if let State::Faulted {
details: FaultDetails { fatal, .. },
..
} = self
{
*fatal
} else {
false
}
}
pub fn is_terminated(&self) -> bool {
if let State::Terminated { .. } = self {
true
} else {
false
}
}
pub fn is_yielded(&self) -> bool {
if let State::Yielded { .. } = self {
true
} else {
false
}
}
pub fn is_yielding(&self) -> bool {
if let State::Yielding { .. } = self {
true
} else {
false
}
}
pub fn is_bound_expired(&self) -> bool {
if let State::BoundExpired = self {
true
} else {
false
}
}
}
// TODO: PR into `libc`
extern "C" {
fn strsignal(sig: libc::c_int) -> *mut libc::c_char;
}
// TODO: PR into `nix`
fn strsignal_wrapper(sig: libc::c_int) -> CString {
unsafe { CStr::from_ptr(strsignal(sig)).to_owned() }
}