Skip to content

Commit c0e00c8

Browse files
[21.0.x] Fix backtraces through empty sequences of Wasm frames (#9412)
* Fix backtraces through empty sequences of Wasm frames This fixes a bug where we would not properly handle contiguous sequences of Wasm frames that are empty. This was mistakenly believed to be an impossible scenario, and before the tail-calls proposal it was impossible, however it can now happen after the following series of events: * Host calls into Wasm, pushing the entry trampoline frame. * Entry trampoline calls the actual Wasm function, pushing a Wasm frame. * Wasm function tail calls to an imported host function, *replacing* the Wasm frame with the exit trampoline's frame. Now we have a stack like `[host, entry trampoline, exit trampoline]`, which has zero Wasm frames between the entry and exit trampolines. If the host function that the exit trampoline calls out to attempts to capture a backtrace, then -- before this commit -- we would fail an internal assertion and panic. That panic would then unwind to the first Rust frame that is called by Wasm. With Rust 1.81 and later, Rust automatically inserts a panic handler that prevents the unwind from continuing into external/foreign code, which is undefined behavior, and aborts the process. Rust versions before 1.81 would attempt to continue unwinding, hitting undefined behavior. This commit fixes the backtrace capturing machinery to handle empty sequences of Wasm frames, passes the assertion, and avoids unwinding into external/foreign code. Co-Authored-By: Alex Crichton <[email protected]> * Add release notes * Add more release notes * Remove usage of wasmtime_test macro * Enable more proposals * ignore tail calls tests on s390x, since it didn't implement them in this release --------- Co-authored-by: Nick Fitzgerald <[email protected]>
1 parent cc4917a commit c0e00c8

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

RELEASES.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
--------------------------------------------------------------------------------
22

3+
## 21.0.2
4+
5+
Released 2024-10-09.
6+
7+
### Fixed
8+
9+
* Fix a runtime crash when combining tail-calls with host imports that capture a
10+
stack trace or trap.
11+
[GHSA-q8hx-mm92-4wvg](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-q8hx-mm92-4wvg)
12+
13+
* Fix a race condition could lead to WebAssembly control-flow integrity and type
14+
safety violations.
15+
[GHSA-7qmx-3fpx-r45m](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-7qmx-3fpx-r45m)
16+
17+
--------------------------------------------------------------------------------
18+
319
## 21.0.1
420

521
Released 2024-05-22.

crates/wasmtime/src/runtime/vm/traphandlers/backtrace.rs

+20
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ impl Backtrace {
178178

179179
arch::assert_entry_sp_is_aligned(trampoline_sp);
180180

181+
// It is possible that the contiguous sequence of Wasm frames is
182+
// empty. This is rare, but can happen if:
183+
//
184+
// * Host calls into Wasm, pushing the entry trampoline frame
185+
//
186+
// * Entry trampoline calls the actual Wasm function, pushing a Wasm frame
187+
//
188+
// * Wasm function tail calls to an imported host function, *replacing*
189+
// the Wasm frame with the exit trampoline's frame
190+
//
191+
// Now we have a stack like `[host, entry trampoline, exit trampoline]`
192+
// which has a contiguous sequence of Wasm frames that are empty.
193+
//
194+
// Therefore, check if we've reached the entry trampoline's SP as the
195+
// first thing we do.
196+
if arch::reached_entry_sp(fp, trampoline_sp) {
197+
log::trace!("=== Empty contiguous sequence of Wasm frames ===");
198+
return ControlFlow::Continue(());
199+
}
200+
181201
loop {
182202
// At the start of each iteration of the loop, we know that `fp` is
183203
// a frame pointer from Wasm code. Therefore, we know it is not

tests/all/module.rs

-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ fn concurrent_type_modifications_and_checks() -> Result<()> {
459459

460460
let mut config = Config::new();
461461
config.wasm_function_references(true);
462-
463462
let engine = Engine::new(&config)?;
464463

465464
let mut threads = Vec::new();

tests/all/traps.rs

+124
Original file line numberDiff line numberDiff line change
@@ -1679,3 +1679,127 @@ fn async_stack_size_ignored_if_disabled() -> Result<()> {
16791679

16801680
Ok(())
16811681
}
1682+
1683+
#[test]
1684+
#[cfg(not(target_arch = "s390x"))]
1685+
fn tail_call_to_imported_function() -> Result<()> {
1686+
let mut config = Config::new();
1687+
config.wasm_tail_call(true);
1688+
let engine = Engine::new(&config)?;
1689+
1690+
let module = Module::new(
1691+
&engine,
1692+
r#"
1693+
(module
1694+
(import "" "" (func (result i32)))
1695+
1696+
(func (export "run") (result i32)
1697+
return_call 0
1698+
)
1699+
)
1700+
"#,
1701+
)?;
1702+
1703+
let mut store = Store::new(&engine, ());
1704+
let host_func = Func::wrap(&mut store, || -> Result<i32> { bail!("whoopsie") });
1705+
let instance = Instance::new(&mut store, &module, &[host_func.into()])?;
1706+
1707+
let run = instance.get_typed_func::<(), i32>(&mut store, "run")?;
1708+
let err = run.call(&mut store, ()).unwrap_err();
1709+
assert!(err.to_string().contains("whoopsie"));
1710+
1711+
Ok(())
1712+
}
1713+
1714+
#[test]
1715+
#[cfg(not(target_arch = "s390x"))]
1716+
fn tail_call_to_imported_function_in_start_function() -> Result<()> {
1717+
let mut config = Config::new();
1718+
config.wasm_tail_call(true);
1719+
let engine = Engine::new(&config)?;
1720+
1721+
let module = Module::new(
1722+
&engine,
1723+
r#"
1724+
(module
1725+
(import "" "" (func))
1726+
1727+
(func $f
1728+
return_call 0
1729+
)
1730+
1731+
(start $f)
1732+
)
1733+
"#,
1734+
)?;
1735+
1736+
let mut store = Store::new(&engine, ());
1737+
let host_func = Func::wrap(&mut store, || -> Result<()> { bail!("whoopsie") });
1738+
let err = Instance::new(&mut store, &module, &[host_func.into()]).unwrap_err();
1739+
assert!(err.to_string().contains("whoopsie"));
1740+
1741+
Ok(())
1742+
}
1743+
1744+
#[test]
1745+
#[cfg(not(target_arch = "s390x"))]
1746+
fn return_call_ref_to_imported_function() -> Result<()> {
1747+
let mut config = Config::new();
1748+
config.wasm_tail_call(true);
1749+
config.wasm_function_references(true);
1750+
let engine = Engine::new(&config)?;
1751+
1752+
let module = Module::new(
1753+
&engine,
1754+
r#"
1755+
(module
1756+
(type (func (result i32)))
1757+
(func (export "run") (param (ref 0)) (result i32)
1758+
(return_call_ref 0 (local.get 0))
1759+
)
1760+
)
1761+
"#,
1762+
)?;
1763+
1764+
let mut store = Store::new(&engine, ());
1765+
let host_func = Func::wrap(&mut store, || -> Result<i32> { bail!("whoopsie") });
1766+
let instance = Instance::new(&mut store, &module, &[])?;
1767+
1768+
let run = instance.get_typed_func::<Func, i32>(&mut store, "run")?;
1769+
let err = run.call(&mut store, host_func).unwrap_err();
1770+
assert!(err.to_string().contains("whoopsie"));
1771+
1772+
Ok(())
1773+
}
1774+
1775+
#[test]
1776+
#[cfg(not(target_arch = "s390x"))]
1777+
fn return_call_indirect_to_imported_function() -> Result<()> {
1778+
let mut config = Config::new();
1779+
config.wasm_tail_call(true);
1780+
config.wasm_function_references(true);
1781+
let engine = Engine::new(&config)?;
1782+
1783+
let module = Module::new(
1784+
&engine,
1785+
r#"
1786+
(module
1787+
(import "" "" (func (result i32)))
1788+
(table 1 funcref (ref.func 0))
1789+
(func (export "run") (result i32)
1790+
(return_call_indirect (result i32) (i32.const 0))
1791+
)
1792+
)
1793+
"#,
1794+
)?;
1795+
1796+
let mut store = Store::new(&engine, ());
1797+
let host_func = Func::wrap(&mut store, || -> Result<i32> { bail!("whoopsie") });
1798+
let instance = Instance::new(&mut store, &module, &[host_func.into()])?;
1799+
1800+
let run = instance.get_typed_func::<(), i32>(&mut store, "run")?;
1801+
let err = run.call(&mut store, ()).unwrap_err();
1802+
assert!(err.to_string().contains("whoopsie"));
1803+
1804+
Ok(())
1805+
}

0 commit comments

Comments
 (0)