Use a StoreOpaque during backtraces for metadata (#4325)

Previous to this commit Wasmtime would use the `GlobalModuleRegistry`
when learning information about a trap such as its trap code, the
symbols for each frame, etc. This has a downside though of holding a
global read-write lock for the duration of this operation which hinders
registration of new modules in parallel. In addition there was a fair
amount of internal duplication between this "global module registry" and
the store-local module registry. Finally relying on global state for
information like this gets a bit more brittle over time as it seems best
to scope global queries to precisely what's necessary rather than
holding extra information.

With the refactoring in wasm backtraces done in #4183 it's now possible
to always have a `StoreOpaque` reference when a backtrace is collected
for symbolication and otherwise Trap-identification purposes. This
commit adds a `StoreOpaque` parameter to the `Trap::from_runtime`
constructor and then plumbs that everywhere. Note that while doing this
I changed the internal `traphandlers::lazy_per_thread_init` function to
no longer return a `Result` and instead just `panic!` on Unix if memory
couldn't be allocated for a stack. This removed quite a lot of
error-handling code for a case that's expected to quite rarely happen.
If necessary in the future we can add a fallible initialization point
but this feels like a better default balance for the code here.

With a `StoreOpaque` in use when a trap is being symbolicated that means
we have a `ModuleRegistry` which can be used for queries and such. This
meant that the `GlobalModuleRegistry` state could largely be dismantled
and moved to per-`Store` state (within the `ModuleRegistry`, mostly just
moving methods around).

The final state is that the global rwlock is not exclusively scoped
around insertions/deletions/`is_wasm_trap_pc` which is just a lookup and
atomic add. Otherwise symbolication for a backtrace exclusively uses
store-local state now (as intended).

The original motivation for this commit was that frame information
lookup and pieces were looking to get somewhat complicated with the
addition of components which are a new vector of traps coming out of
Cranelift-generated code. My hope is that by having a `Store` around for
more operations it's easier to plumb all this through.
This commit is contained in:
Alex Crichton
2022-06-27 15:24:59 -05:00
committed by GitHub
parent 5c2c285dd7
commit 82a31680d6
12 changed files with 397 additions and 445 deletions

View File

@@ -1,4 +1,4 @@
use crate::traphandlers::{tls, wasmtime_longjmp, Trap};
use crate::traphandlers::{tls, wasmtime_longjmp};
use std::cell::RefCell;
use std::io;
use std::mem::{self, MaybeUninit};
@@ -252,7 +252,7 @@ unsafe fn set_pc(cx: *mut libc::c_void, pc: usize, arg1: usize) {
/// and registering our own alternate stack that is large enough and has a guard
/// page.
#[cold]
pub fn lazy_per_thread_init() -> Result<(), Box<Trap>> {
pub fn lazy_per_thread_init() {
// This thread local is purely used to register a `Stack` to get deallocated
// when the thread exists. Otherwise this function is only ever called at
// most once per-thread.
@@ -270,11 +270,10 @@ pub fn lazy_per_thread_init() -> Result<(), Box<Trap>> {
}
return STACK.with(|s| {
*s.borrow_mut() = unsafe { allocate_sigaltstack()? };
Ok(())
*s.borrow_mut() = unsafe { allocate_sigaltstack() };
});
unsafe fn allocate_sigaltstack() -> Result<Option<Stack>, Box<Trap>> {
unsafe fn allocate_sigaltstack() -> Option<Stack> {
// Check to see if the existing sigaltstack, if it exists, is big
// enough. If so we don't need to allocate our own.
let mut old_stack = mem::zeroed();
@@ -286,7 +285,7 @@ pub fn lazy_per_thread_init() -> Result<(), Box<Trap>> {
io::Error::last_os_error()
);
if old_stack.ss_flags & libc::SS_DISABLE == 0 && old_stack.ss_size >= MIN_STACK_SIZE {
return Ok(None);
return None;
}
// ... but failing that we need to allocate our own, so do all that
@@ -301,7 +300,7 @@ pub fn lazy_per_thread_init() -> Result<(), Box<Trap>> {
rustix::mm::ProtFlags::empty(),
rustix::mm::MapFlags::PRIVATE,
)
.map_err(|_| Box::new(Trap::oom()))?;
.expect("failed to allocate memory for sigaltstack");
// Prepare the stack with readable/writable memory and then register it
// with `sigaltstack`.
@@ -325,10 +324,10 @@ pub fn lazy_per_thread_init() -> Result<(), Box<Trap>> {
io::Error::last_os_error()
);
Ok(Some(Stack {
Some(Stack {
mmap_ptr: ptr,
mmap_size: alloc_size,
}))
})
}
impl Drop for Stack {