Skip to content

Commit 52524d2

Browse files
authored
Expose TrapCode::Interrupt on epoch based interruption (#4105)
1 parent 71fc16b commit 52524d2

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

crates/cranelift/src/func_environ.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
593593
// The reason is that one can construct a "zip-bomb-like"
594594
// program with exponential-in-program-size runtime, with no
595595
// backedges (loops), by building a tree of function calls: f0
596-
// calls f1 ten tims, f1 calls f2 ten times, etc. E.g., nine
596+
// calls f1 ten times, f1 calls f2 ten times, etc. E.g., nine
597597
// levels of this yields a billion function calls with no
598598
// backedges. So we can't do checks only at backedges.
599599
//

crates/wasmtime/src/store.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,14 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
18561856

18571857
fn new_epoch(&mut self) -> Result<u64, anyhow::Error> {
18581858
return match &self.epoch_deadline_behavior {
1859-
&EpochDeadline::Trap => Err(anyhow::Error::new(EpochDeadlineError)),
1859+
&EpochDeadline::Trap => {
1860+
let trap = Trap::new_wasm(
1861+
None,
1862+
wasmtime_environ::TrapCode::Interrupt,
1863+
wasmtime_runtime::Backtrace::new(),
1864+
);
1865+
Err(anyhow::Error::from(trap))
1866+
}
18601867
#[cfg(feature = "async")]
18611868
&EpochDeadline::YieldAndExtendDeadline { delta } => {
18621869
// Do the async yield. May return a trap if future was
@@ -1870,17 +1877,6 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
18701877
Ok(self.get_epoch_deadline())
18711878
}
18721879
};
1873-
1874-
#[derive(Debug)]
1875-
struct EpochDeadlineError;
1876-
1877-
impl fmt::Display for EpochDeadlineError {
1878-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1879-
f.write_str("epoch deadline reached during execution")
1880-
}
1881-
}
1882-
1883-
impl std::error::Error for EpochDeadlineError {}
18841880
}
18851881
}
18861882

examples/interrupt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> Result<()> {
2929
let trap = run.call(&mut store, ()).unwrap_err();
3030

3131
println!("trap received...");
32-
assert!(trap.to_string().contains("epoch deadline reached"));
32+
assert!(trap.trap_code().unwrap() == TrapCode::Interrupt);
3333

3434
Ok(())
3535
}

tests/all/cli_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn timeout_in_start() -> Result<()> {
175175
assert_eq!(output.stdout, b"");
176176
let stderr = String::from_utf8_lossy(&output.stderr);
177177
assert!(
178-
stderr.contains("epoch deadline reached during execution"),
178+
stderr.contains("wasm trap: interrupt"),
179179
"bad stderr: {}",
180180
stderr
181181
);
@@ -196,7 +196,7 @@ fn timeout_in_invoke() -> Result<()> {
196196
assert_eq!(output.stdout, b"");
197197
let stderr = String::from_utf8_lossy(&output.stderr);
198198
assert!(
199-
stderr.contains("epoch deadline reached during execution"),
199+
stderr.contains("wasm trap: interrupt"),
200200
"bad stderr: {}",
201201
stderr
202202
);

tests/all/iloop.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn loops_interruptable() -> anyhow::Result<()> {
3333
store.engine().increment_epoch();
3434
let trap = iloop.call(&mut store, ()).unwrap_err();
3535
assert!(
36-
trap.to_string().contains("epoch deadline reached"),
36+
trap.trap_code().unwrap() == TrapCode::Interrupt,
3737
"bad message: {}",
3838
trap
3939
);
@@ -50,7 +50,7 @@ fn functions_interruptable() -> anyhow::Result<()> {
5050
store.engine().increment_epoch();
5151
let trap = iloop.call(&mut store, ()).unwrap_err();
5252
assert!(
53-
trap.to_string().contains("epoch deadline reached"),
53+
trap.trap_code().unwrap() == TrapCode::Interrupt,
5454
"{}",
5555
trap.to_string()
5656
);
@@ -103,7 +103,7 @@ fn loop_interrupt_from_afar() -> anyhow::Result<()> {
103103
thread.join().unwrap();
104104
assert!(HITS.load(SeqCst) > NUM_HITS);
105105
assert!(
106-
trap.to_string().contains("epoch deadline reached"),
106+
trap.trap_code().unwrap() == TrapCode::Interrupt,
107107
"bad message: {}",
108108
trap.to_string()
109109
);
@@ -143,7 +143,7 @@ fn function_interrupt_from_afar() -> anyhow::Result<()> {
143143
thread.join().unwrap();
144144
assert!(HITS.load(SeqCst) > NUM_HITS);
145145
assert!(
146-
trap.to_string().contains("epoch deadline reached"),
146+
trap.trap_code().unwrap() == TrapCode::Interrupt,
147147
"bad message: {}",
148148
trap.to_string()
149149
);

0 commit comments

Comments
 (0)