wasmtime/runtime/vm/traphandlers.rs
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
//! WebAssembly trap handling, which is built on top of the lower-level
//! signalhandling mechanisms.
mod backtrace;
#[cfg(feature = "coredump")]
#[path = "traphandlers/coredump_enabled.rs"]
mod coredump;
#[cfg(not(feature = "coredump"))]
#[path = "traphandlers/coredump_disabled.rs"]
mod coredump;
#[cfg(all(has_native_signals))]
mod signals;
#[cfg(all(has_native_signals))]
pub use self::signals::*;
use crate::prelude::*;
use crate::runtime::module::lookup_code;
use crate::runtime::store::{ExecutorRef, StoreOpaque};
use crate::runtime::vm::sys::traphandlers;
use crate::runtime::vm::{Instance, InterpreterRef, VMContext, VMStoreContext};
use crate::{StoreContextMut, WasmBacktrace};
use core::cell::Cell;
use core::ops::Range;
use core::ptr::{self, NonNull};
pub use self::backtrace::Backtrace;
pub use self::coredump::CoreDumpStack;
pub use self::tls::tls_eager_initialize;
#[cfg(feature = "async")]
pub use self::tls::{AsyncWasmCallState, PreviousAsyncWasmCallState};
pub use traphandlers::SignalHandler;
pub(crate) struct TrapRegisters {
pub pc: usize,
pub fp: usize,
}
/// Return value from `test_if_trap`.
pub(crate) enum TrapTest {
/// Not a wasm trap, need to delegate to whatever process handler is next.
NotWasm,
/// This trap was handled by the embedder via custom embedding APIs.
#[cfg(has_host_compiler_backend)]
#[cfg_attr(miri, expect(dead_code, reason = "using #[cfg] too unergonomic"))]
HandledByEmbedder,
/// This is a wasm trap, it needs to be handled.
#[cfg_attr(miri, expect(dead_code, reason = "using #[cfg] too unergonomic"))]
Trap {
/// How to longjmp back to the original wasm frame.
#[cfg(has_host_compiler_backend)]
jmp_buf: *const u8,
},
}
fn lazy_per_thread_init() {
traphandlers::lazy_per_thread_init();
}
/// Raises a preexisting trap and unwinds.
///
/// This function will execute the `longjmp` to make its way back to the
/// original `setjmp` performed when wasm was entered. This is currently
/// only called from the `raise` builtin of Wasmtime. This builtin is only used
/// when the host returns back to wasm and indicates that a trap should be
/// raised. In this situation the host has already stored trap information
/// within the `CallThreadState` and this is the low-level operation to actually
/// perform an unwind.
///
/// This function won't be use with Pulley, for example, as the interpreter
/// halts differently than native code. Additionally one day this will ideally
/// be implemented by Cranelift itself without need of a libcall when Cranelift
/// implements the exception handling proposal for example.
///
/// # Safety
///
/// Only safe to call when wasm code is on the stack, aka `catch_traps` must
/// have been previously called. Additionally no Rust destructors can be on the
/// stack. They will be skipped and not executed.
#[cfg(has_host_compiler_backend)]
pub(super) unsafe fn raise_preexisting_trap() -> ! {
tls::with(|info| info.unwrap().unwind())
}
/// Invokes the closure `f` and returns a `bool` if it succeeded.
///
/// This will invoke the closure `f` which returns a value that implements
/// `HostResult`. This trait abstracts over how host values are translated to
/// ABI values when going back into wasm. Some examples are:
///
/// * `T` - bare return types (not results) are simply returned as-is. No
/// `catch_unwind` happens as if a trap can't happen then the host shouldn't
/// be panicking or invoking user code.
///
/// * `Result<(), E>` - this represents an ABI return value of `bool` which
/// indicates whether the call succeeded. This return value will catch panics
/// and record trap information as `E`.
///
/// * `Result<u32, E>` - the ABI return value here is `u64` where on success
/// the 32-bit result is zero-extended and `u64::MAX` as a return value
/// indicates that a trap or panic happened.
///
/// This is primarily used in conjunction with the Cranelift-and-host boundary.
/// This function acts as a bridge between the two to appropriately handle
/// encoding host values to Cranelift-understood ABIs via the `HostResult`
/// trait.
pub fn catch_unwind_and_record_trap<R>(f: impl FnOnce() -> R) -> R::Abi
where
R: HostResult,
{
// Invoke the closure `f`, optionally catching unwinds depending on `R`. The
// return value is always provided and if unwind information is provided
// (e.g. `ret` is a "false"-y value) then it's recorded in TLS for the
// unwind operation that's about to happen from Cranelift-generated code.
let (ret, unwind) = R::maybe_catch_unwind(f);
if let Some(unwind) = unwind {
tls::with(|info| info.unwrap().record_unwind(unwind));
}
ret
}
/// A trait used in conjunction with `catch_unwind_and_record_trap` to convert a
/// Rust-based type to a specific ABI while handling traps/unwinds.
///
/// This type is implemented for return values from host function calls and
/// libcalls. The `Abi` value of this trait represents either a successful
/// execution with some payload state or that a failed execution happened. In
/// the event of a failed execution the state of the failure itself is stored
/// within `CallThreadState::unwind`. Cranelift-compiled code is expected to
/// test for this failure sentinel and process it accordingly.
///
/// See `catch_unwind_and_record_trap` for some more information as well.
pub trait HostResult {
/// The type of the value that's returned to Cranelift-compiled code. Needs
/// to be ABI-safe to pass through an `extern "C"` return value.
type Abi: Copy;
/// Executes `f` and returns the ABI/unwind information as a result.
///
/// This may optionally catch unwinds during execution depending on this
/// implementation. The ABI return value is unconditionally provided. If an
/// unwind was detected (e.g. a host panic or a wasm trap) then that's
/// additionally returned as well.
///
/// If an unwind is returned then it's expected that when the host returns
/// back to wasm (which should be soon after calling this through
/// `catch_unwind_and_record_trap`) then wasm will very quickly turn around
/// and initiate an unwind (currently through `raise_preexisting_trap`).
fn maybe_catch_unwind(f: impl FnOnce() -> Self) -> (Self::Abi, Option<UnwindReason>);
}
// Base case implementations that do not catch unwinds. These are for libcalls
// that neither trap nor execute user code. The raw value is the ABI itself.
//
// Panics in these libcalls will result in a process abort as unwinding is not
// allowed via Rust through `extern "C"` function boundaries.
macro_rules! host_result_no_catch {
($($t:ty,)*) => {
$(
impl HostResult for $t {
type Abi = $t;
fn maybe_catch_unwind(f: impl FnOnce() -> $t) -> ($t, Option<UnwindReason>) {
(f(), None)
}
}
)*
}
}
host_result_no_catch! {
(),
bool,
u32,
*mut u8,
u64,
}
impl HostResult for NonNull<u8> {
type Abi = *mut u8;
fn maybe_catch_unwind(f: impl FnOnce() -> Self) -> (*mut u8, Option<UnwindReason>) {
(f().as_ptr(), None)
}
}
/// Implementation of `HostResult` for `Result<T, E>`.
///
/// This is where things get interesting for `HostResult`. This is generically
/// defined to allow many shapes of the `Result` type to be returned from host
/// calls or libcalls. To do this an extra trait requirement is placed on the
/// successful result `T`: `HostResultHasUnwindSentinel`.
///
/// The general requirement is that `T` says what ABI it has, and the ABI must
/// have a sentinel value which indicates that an unwind in wasm should happen.
/// For example if `T = ()` then `true` means that the call succeeded and
/// `false` means that an unwind happened. Here the sentinel is `false` and the
/// ABI is `bool`.
///
/// This is the only implementation of `HostResult` which actually catches
/// unwinds as there's a sentinel to encode.
impl<T, E> HostResult for Result<T, E>
where
T: HostResultHasUnwindSentinel,
E: Into<TrapReason>,
{
type Abi = T::Abi;
fn maybe_catch_unwind(f: impl FnOnce() -> Result<T, E>) -> (T::Abi, Option<UnwindReason>) {
// First prepare the closure `f` as something that'll be invoked to
// generate the return value of this function. This is the
// conditionally, below, passed to `catch_unwind`.
let f = move || match f() {
Ok(ret) => (ret.into_abi(), None),
Err(reason) => (T::SENTINEL, Some(UnwindReason::Trap(reason.into()))),
};
// With `panic=unwind` use `std::panic::catch_unwind` to catch possible
// panics to rethrow.
#[cfg(all(feature = "std", panic = "unwind"))]
{
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(result) => result,
Err(err) => (T::SENTINEL, Some(UnwindReason::Panic(err))),
}
}
// With `panic=abort` there's no use in using `std::panic::catch_unwind`
// since it won't actually catch anything. Note that
// `std::panic::catch_unwind` will technically optimize to this but having
// this branch avoids using the `std::panic` module entirely.
#[cfg(not(all(feature = "std", panic = "unwind")))]
{
f()
}
}
}
/// Trait used in conjunction with `HostResult for Result<T, E>` where this is
/// the trait bound on `T`.
///
/// This is for values in the "ok" position of a `Result` return value. Each
/// value can have a separate ABI from itself (e.g. `type Abi`) and must be
/// convertible to the ABI. Additionally all implementations of this trait have
/// a "sentinel value" which indicates that an unwind happened. This means that
/// no valid instance of `Self` should generate the `SENTINEL` via the
/// `into_abi` function.
pub unsafe trait HostResultHasUnwindSentinel {
/// The Cranelift-understood ABI of this value (should not be `Self`).
type Abi: Copy;
/// A value that indicates that an unwind should happen and is tested for in
/// Cranelift-generated code.
const SENTINEL: Self::Abi;
/// Converts this value into the ABI representation. Should never returned
/// the `SENTINEL` value.
fn into_abi(self) -> Self::Abi;
}
/// No return value from the host is represented as a `bool` in the ABI. Here
/// `true` means that execution succeeded while `false` is the sentinel used to
/// indicate an unwind.
unsafe impl HostResultHasUnwindSentinel for () {
type Abi = bool;
const SENTINEL: bool = false;
fn into_abi(self) -> bool {
true
}
}
/// A 32-bit return value can be inflated to a 64-bit return value in the ABI.
/// In this manner a successful result is a zero-extended 32-bit value and the
/// failure sentinel is `u64::MAX` or -1 as a signed integer.
unsafe impl HostResultHasUnwindSentinel for u32 {
type Abi = u64;
const SENTINEL: u64 = u64::MAX;
fn into_abi(self) -> u64 {
self.into()
}
}
/// If there is not actual successful result (e.g. an empty enum) then the ABI
/// can be `()`, or nothing, because there's no successful result and it's
/// always a failure.
unsafe impl HostResultHasUnwindSentinel for core::convert::Infallible {
type Abi = ();
const SENTINEL: () = ();
fn into_abi(self) {
match self {}
}
}
/// Stores trace message with backtrace.
#[derive(Debug)]
pub struct Trap {
/// Original reason from where this trap originated.
pub reason: TrapReason,
/// Wasm backtrace of the trap, if any.
pub backtrace: Option<Backtrace>,
/// The Wasm Coredump, if any.
pub coredumpstack: Option<CoreDumpStack>,
}
/// Enumeration of different methods of raising a trap.
#[derive(Debug)]
pub enum TrapReason {
/// A user-raised trap through `raise_user_trap`.
User(Error),
/// A trap raised from Cranelift-generated code.
Jit {
/// The program counter where this trap originated.
///
/// This is later used with side tables from compilation to translate
/// the trapping address to a trap code.
pc: usize,
/// If the trap was a memory-related trap such as SIGSEGV then this
/// field will contain the address of the inaccessible data.
///
/// Note that wasm loads/stores are not guaranteed to fill in this
/// information. Dynamically-bounds-checked memories, for example, will
/// not access an invalid address but may instead load from NULL or may
/// explicitly jump to a `ud2` instruction. This is only available for
/// fault-based traps which are one of the main ways, but not the only
/// way, to run wasm.
faulting_addr: Option<usize>,
/// The trap code associated with this trap.
trap: wasmtime_environ::Trap,
},
/// A trap raised from a wasm libcall
Wasm(wasmtime_environ::Trap),
}
impl From<Error> for TrapReason {
fn from(err: Error) -> Self {
TrapReason::User(err)
}
}
impl From<wasmtime_environ::Trap> for TrapReason {
fn from(code: wasmtime_environ::Trap) -> Self {
TrapReason::Wasm(code)
}
}
/// Catches any wasm traps that happen within the execution of `closure`,
/// returning them as a `Result`.
///
/// # Unsafety
///
/// This function is unsafe because during the execution of `closure` it may be
/// longjmp'd over and none of its destructors on the stack may be run.
pub unsafe fn catch_traps<T, F>(
store: &mut StoreContextMut<'_, T>,
mut closure: F,
) -> Result<(), Box<Trap>>
where
F: FnMut(NonNull<VMContext>, Option<InterpreterRef<'_>>) -> bool,
{
let caller = store.0.default_caller();
let result = CallThreadState::new(store.0, caller).with(|cx| match store.0.executor() {
// In interpreted mode directly invoke the host closure since we won't
// be using host-based `setjmp`/`longjmp` as that's not going to save
// the context we want.
ExecutorRef::Interpreter(r) => {
cx.jmp_buf
.set(CallThreadState::JMP_BUF_INTERPRETER_SENTINEL);
closure(caller, Some(r))
}
// In native mode, however, defer to C to do the `setjmp` since Rust
// doesn't understand `setjmp`.
//
// Note that here we pass a function pointer to C to catch longjmp
// within, here it's `call_closure`, and that passes `None` for the
// interpreter since this branch is only ever taken if the interpreter
// isn't present.
#[cfg(has_host_compiler_backend)]
ExecutorRef::Native => traphandlers::wasmtime_setjmp(
cx.jmp_buf.as_ptr(),
{
extern "C" fn call_closure<F>(payload: *mut u8, caller: NonNull<VMContext>) -> bool
where
F: FnMut(NonNull<VMContext>, Option<InterpreterRef<'_>>) -> bool,
{
unsafe { (*(payload as *mut F))(caller, None) }
}
call_closure::<F>
},
&mut closure as *mut F as *mut u8,
caller,
),
});
return match result {
Ok(x) => Ok(x),
Err((UnwindReason::Trap(reason), backtrace, coredumpstack)) => Err(Box::new(Trap {
reason,
backtrace,
coredumpstack,
})),
#[cfg(all(feature = "std", panic = "unwind"))]
Err((UnwindReason::Panic(panic), _, _)) => std::panic::resume_unwind(panic),
};
}
// Module to hide visibility of the `CallThreadState::prev` field and force
// usage of its accessor methods.
mod call_thread_state {
use super::*;
use crate::runtime::vm::Unwind;
/// Temporary state stored on the stack which is registered in the `tls` module
/// below for calls into wasm.
pub struct CallThreadState {
pub(super) unwind: Cell<Option<(UnwindReason, Option<Backtrace>, Option<CoreDumpStack>)>>,
pub(super) jmp_buf: Cell<*const u8>,
#[cfg(all(has_native_signals))]
pub(super) signal_handler: Option<*const SignalHandler>,
pub(super) capture_backtrace: bool,
#[cfg(feature = "coredump")]
pub(super) capture_coredump: bool,
pub(crate) vm_store_context: NonNull<VMStoreContext>,
pub(crate) unwinder: &'static dyn Unwind,
pub(super) prev: Cell<tls::Ptr>,
#[cfg(all(has_native_signals, unix))]
pub(crate) async_guard_range: Range<*mut u8>,
// The values of `VMStoreContext::last_wasm_{exit_{pc,fp},entry_sp}` for
// the *previous* `CallThreadState` for this same store/limits. Our
// *current* last wasm PC/FP/SP are saved in `self.vm_store_context`. We
// save a copy of the old registers here because the `VMStoreContext`
// typically doesn't change across nested calls into Wasm (i.e. they are
// typically calls back into the same store and `self.vm_store_context
// == self.prev.vm_store_context`) and we must to maintain the list of
// contiguous-Wasm-frames stack regions for backtracing purposes.
old_last_wasm_exit_fp: Cell<usize>,
old_last_wasm_exit_pc: Cell<usize>,
old_last_wasm_entry_fp: Cell<usize>,
}
impl Drop for CallThreadState {
fn drop(&mut self) {
// Unwind information should not be present as it should have
// already been processed.
debug_assert!(self.unwind.replace(None).is_none());
unsafe {
let cx = self.vm_store_context.as_ref();
*cx.last_wasm_exit_fp.get() = self.old_last_wasm_exit_fp.get();
*cx.last_wasm_exit_pc.get() = self.old_last_wasm_exit_pc.get();
*cx.last_wasm_entry_fp.get() = self.old_last_wasm_entry_fp.get();
}
}
}
impl CallThreadState {
pub const JMP_BUF_INTERPRETER_SENTINEL: *mut u8 = 1 as *mut u8;
#[inline]
pub(super) fn new(store: &mut StoreOpaque, caller: NonNull<VMContext>) -> CallThreadState {
let vm_store_context = unsafe {
Instance::from_vmctx(caller, |i| i.vm_store_context())
.read()
.unwrap()
.as_non_null()
};
// Don't try to plumb #[cfg] everywhere for this field, just pretend
// we're using it on miri/windows to silence compiler warnings.
let _: Range<_> = store.async_guard_range();
CallThreadState {
unwind: Cell::new(None),
unwinder: store.unwinder(),
jmp_buf: Cell::new(ptr::null()),
#[cfg(all(has_native_signals))]
signal_handler: store.signal_handler(),
capture_backtrace: store.engine().config().wasm_backtrace,
#[cfg(feature = "coredump")]
capture_coredump: store.engine().config().coredump_on_trap,
vm_store_context,
#[cfg(all(has_native_signals, unix))]
async_guard_range: store.async_guard_range(),
prev: Cell::new(ptr::null()),
old_last_wasm_exit_fp: Cell::new(unsafe {
*vm_store_context.as_ref().last_wasm_exit_fp.get()
}),
old_last_wasm_exit_pc: Cell::new(unsafe {
*vm_store_context.as_ref().last_wasm_exit_pc.get()
}),
old_last_wasm_entry_fp: Cell::new(unsafe {
*vm_store_context.as_ref().last_wasm_entry_fp.get()
}),
}
}
/// Get the saved FP upon exit from Wasm for the previous `CallThreadState`.
pub fn old_last_wasm_exit_fp(&self) -> usize {
self.old_last_wasm_exit_fp.get()
}
/// Get the saved PC upon exit from Wasm for the previous `CallThreadState`.
pub fn old_last_wasm_exit_pc(&self) -> usize {
self.old_last_wasm_exit_pc.get()
}
/// Get the saved FP upon entry into Wasm for the previous `CallThreadState`.
pub fn old_last_wasm_entry_fp(&self) -> usize {
self.old_last_wasm_entry_fp.get()
}
/// Get the previous `CallThreadState`.
pub fn prev(&self) -> tls::Ptr {
self.prev.get()
}
#[inline]
pub(crate) unsafe fn push(&self) {
assert!(self.prev.get().is_null());
self.prev.set(tls::raw::replace(self));
}
#[inline]
pub(crate) unsafe fn pop(&self) {
let prev = self.prev.replace(ptr::null());
let head = tls::raw::replace(prev);
assert!(core::ptr::eq(head, self));
}
}
}
pub use call_thread_state::*;
pub enum UnwindReason {
#[cfg(all(feature = "std", panic = "unwind"))]
Panic(Box<dyn std::any::Any + Send>),
Trap(TrapReason),
}
impl CallThreadState {
#[inline]
fn with(
mut self,
closure: impl FnOnce(&CallThreadState) -> bool,
) -> Result<(), (UnwindReason, Option<Backtrace>, Option<CoreDumpStack>)> {
let succeeded = tls::set(&mut self, |me| closure(me));
if succeeded {
Ok(())
} else {
Err(self.read_unwind())
}
}
#[cold]
fn read_unwind(&self) -> (UnwindReason, Option<Backtrace>, Option<CoreDumpStack>) {
self.unwind.replace(None).unwrap()
}
/// Records the unwind information provided within this `CallThreadState`,
/// optionally capturing a backtrace at this time.
///
/// This function is used to stash metadata for why an unwind is about to
/// happen. The actual unwind is expected to happen after this function is
/// called using, for example, the `unwind` function below.
///
/// Note that this is a relatively low-level function and will panic if
/// mis-used.
///
/// # Panics
///
/// Panics if unwind information has already been recorded as that should
/// have been processed first.
fn record_unwind(&self, reason: UnwindReason) {
if cfg!(debug_assertions) {
let prev = self.unwind.replace(None);
assert!(prev.is_none());
}
let (backtrace, coredump) = match &reason {
// Panics don't need backtraces. There is nowhere to attach the
// hypothetical backtrace to and it doesn't really make sense to try
// in the first place since this is a Rust problem rather than a
// Wasm problem.
#[cfg(all(feature = "std", panic = "unwind"))]
UnwindReason::Panic(_) => (None, None),
// And if we are just propagating an existing trap that already has
// a backtrace attached to it, then there is no need to capture a
// new backtrace either.
UnwindReason::Trap(TrapReason::User(err))
if err.downcast_ref::<WasmBacktrace>().is_some() =>
{
(None, None)
}
UnwindReason::Trap(_) => (
self.capture_backtrace(self.vm_store_context.as_ptr(), None),
self.capture_coredump(self.vm_store_context.as_ptr(), None),
),
};
self.unwind.set(Some((reason, backtrace, coredump)));
}
/// Helper function to perform an actual unwinding operation.
///
/// This must be preceded by a `record_unwind` operation above to be
/// processed correctly on the other side.
///
/// # Unsafety
///
/// This function is not safe if the corresponding setjmp wasn't already
/// called. Additionally this isn't safe as it will skip all Rust
/// destructors on the stack, if there are any.
#[cfg(has_host_compiler_backend)]
unsafe fn unwind(&self) -> ! {
debug_assert!(!self.jmp_buf.get().is_null());
debug_assert!(self.jmp_buf.get() != CallThreadState::JMP_BUF_INTERPRETER_SENTINEL);
traphandlers::wasmtime_longjmp(self.jmp_buf.get());
}
fn capture_backtrace(
&self,
limits: *const VMStoreContext,
trap_pc_and_fp: Option<(usize, usize)>,
) -> Option<Backtrace> {
if !self.capture_backtrace {
return None;
}
Some(unsafe { Backtrace::new_with_trap_state(limits, self.unwinder, self, trap_pc_and_fp) })
}
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = &'a Self> + 'a {
let mut state = Some(self);
core::iter::from_fn(move || {
let this = state?;
state = unsafe { this.prev().as_ref() };
Some(this)
})
}
/// Trap handler using our thread-local state.
///
/// * `regs` - some special program registers at the time that the trap
/// happened, for example `pc`.
/// * `faulting_addr` - the system-provided address that the a fault, if
/// any, happened at. This is used when debug-asserting that all segfaults
/// are known to live within a `Store<T>` in a valid range.
/// * `call_handler` - a closure used to invoke the platform-specific
/// signal handler for each instance, if available.
///
/// Attempts to handle the trap if it's a wasm trap. Returns a `TrapTest`
/// which indicates what this could be, such as:
///
/// * `TrapTest::NotWasm` - not a wasm fault, this should get forwarded to
/// the next platform-specific fault handler.
/// * `TrapTest::HandledByEmbedder` - the embedder `call_handler` handled
/// this signal, nothing else to do.
/// * `TrapTest::Trap` - this is a wasm trap an the stack needs to be
/// unwound now.
pub(crate) fn test_if_trap(
&self,
regs: TrapRegisters,
faulting_addr: Option<usize>,
call_handler: impl Fn(&SignalHandler) -> bool,
) -> TrapTest {
// If we haven't even started to handle traps yet, bail out.
if self.jmp_buf.get().is_null() {
return TrapTest::NotWasm;
}
// First up see if any instance registered has a custom trap handler,
// in which case run them all. If anything handles the trap then we
// return that the trap was handled.
let _ = &call_handler;
#[cfg(all(has_native_signals, not(miri)))]
if let Some(handler) = self.signal_handler {
if unsafe { call_handler(&*handler) } {
return TrapTest::HandledByEmbedder;
}
}
// If this fault wasn't in wasm code, then it's not our problem
let Some((code, text_offset)) = lookup_code(regs.pc) else {
return TrapTest::NotWasm;
};
// If the fault was at a location that was not marked as potentially
// trapping, then that's a bug in Cranelift/Winch/etc. Don't try to
// catch the trap and pretend this isn't wasm so the program likely
// aborts.
let Some(trap) = code.lookup_trap_code(text_offset) else {
return TrapTest::NotWasm;
};
// If all that passed then this is indeed a wasm trap, so return the
// `jmp_buf` passed to `wasmtime_longjmp` to resume.
self.set_jit_trap(regs, faulting_addr, trap);
TrapTest::Trap {
#[cfg(has_host_compiler_backend)]
jmp_buf: self.take_jmp_buf(),
}
}
#[cfg(has_host_compiler_backend)]
pub(crate) fn take_jmp_buf(&self) -> *const u8 {
self.jmp_buf.replace(ptr::null())
}
pub(crate) fn set_jit_trap(
&self,
TrapRegisters { pc, fp, .. }: TrapRegisters,
faulting_addr: Option<usize>,
trap: wasmtime_environ::Trap,
) {
let backtrace = self.capture_backtrace(self.vm_store_context.as_ptr(), Some((pc, fp)));
let coredump = self.capture_coredump(self.vm_store_context.as_ptr(), Some((pc, fp)));
self.unwind.set(Some((
UnwindReason::Trap(TrapReason::Jit {
pc,
faulting_addr,
trap,
}),
backtrace,
coredump,
)))
}
}
// A private inner module for managing the TLS state that we require across
// calls in wasm. The WebAssembly code is called from C++ and then a trap may
// happen which requires us to read some contextual state to figure out what to
// do with the trap. This `tls` module is used to persist that information from
// the caller to the trap site.
pub(crate) mod tls {
use super::CallThreadState;
pub use raw::Ptr;
// An even *more* inner module for dealing with TLS. This actually has the
// thread local variable and has functions to access the variable.
//
// Note that this is specially done to fully encapsulate that the accessors
// for tls may or may not be inlined. Wasmtime's async support employs stack
// switching which can resume execution on different OS threads. This means
// that borrows of our TLS pointer must never live across accesses because
// otherwise the access may be split across two threads and cause unsafety.
//
// This also means that extra care is taken by the runtime to save/restore
// these TLS values when the runtime may have crossed threads.
//
// Note, though, that if async support is disabled at compile time then
// these functions are free to be inlined.
pub(super) mod raw {
use super::CallThreadState;
use sptr::Strict;
pub type Ptr = *const CallThreadState;
const _: () = {
assert!(core::mem::align_of::<CallThreadState>() > 1);
};
fn tls_get() -> (Ptr, bool) {
let mut initialized = false;
let p = Strict::map_addr(crate::runtime::vm::sys::tls_get(), |a| {
initialized = (a & 1) != 0;
a & !1
});
(p.cast(), initialized)
}
fn tls_set(ptr: Ptr, initialized: bool) {
let encoded = Strict::map_addr(ptr, |a| a | usize::from(initialized));
crate::runtime::vm::sys::tls_set(encoded.cast_mut().cast::<u8>());
}
#[cfg_attr(feature = "async", inline(never))] // see module docs
#[cfg_attr(not(feature = "async"), inline)]
pub fn replace(val: Ptr) -> Ptr {
// When a new value is configured that means that we may be
// entering WebAssembly so check to see if this thread has
// performed per-thread initialization for traps.
let (prev, initialized) = tls_get();
if !initialized {
super::super::lazy_per_thread_init();
}
tls_set(val, true);
prev
}
/// Eagerly initialize thread-local runtime functionality. This will be performed
/// lazily by the runtime if users do not perform it eagerly.
#[cfg_attr(feature = "async", inline(never))] // see module docs
#[cfg_attr(not(feature = "async"), inline)]
pub fn initialize() {
let (state, initialized) = tls_get();
if initialized {
return;
}
super::super::lazy_per_thread_init();
tls_set(state, true);
}
#[cfg_attr(feature = "async", inline(never))] // see module docs
#[cfg_attr(not(feature = "async"), inline)]
pub fn get() -> Ptr {
tls_get().0
}
}
pub use raw::initialize as tls_eager_initialize;
/// Opaque state used to persist the state of the `CallThreadState`
/// activations associated with a fiber stack that's used as part of an
/// async wasm call.
#[cfg(feature = "async")]
pub struct AsyncWasmCallState {
// The head of a linked list of activations that are currently present
// on an async call's fiber stack. This pointer points to the oldest
// activation frame where the `prev` links internally link to younger
// activation frames.
//
// When pushed onto a thread this linked list is traversed to get pushed
// onto the current thread at the time.
state: raw::Ptr,
}
#[cfg(feature = "async")]
impl AsyncWasmCallState {
/// Creates new state that initially starts as null.
pub fn new() -> AsyncWasmCallState {
AsyncWasmCallState {
state: core::ptr::null_mut(),
}
}
/// Pushes the saved state of this wasm's call onto the current thread's
/// state.
///
/// This will iterate over the linked list of states stored within
/// `self` and push them sequentially onto the current thread's
/// activation list.
///
/// The returned `PreviousAsyncWasmCallState` captures the state of this
/// thread just before this operation, and it must have its `restore`
/// method called to restore the state when the async wasm is suspended
/// from.
///
/// # Unsafety
///
/// Must be carefully coordinated with
/// `PreviousAsyncWasmCallState::restore` and fiber switches to ensure
/// that this doesn't push stale data and the data is popped
/// appropriately.
pub unsafe fn push(self) -> PreviousAsyncWasmCallState {
// Our `state` pointer is a linked list of oldest-to-youngest so by
// pushing in order of the list we restore the youngest-to-oldest
// list as stored in the state of this current thread.
let ret = PreviousAsyncWasmCallState { state: raw::get() };
let mut ptr = self.state;
while let Some(state) = ptr.as_ref() {
ptr = state.prev.replace(core::ptr::null_mut());
state.push();
}
ret
}
/// Performs a runtime check that this state is indeed null.
pub fn assert_null(&self) {
assert!(self.state.is_null());
}
/// Asserts that the current CallThreadState pointer, if present, is not
/// in the `range` specified.
///
/// This is used when exiting a future in Wasmtime to assert that the
/// current CallThreadState pointer does not point within the stack
/// we're leaving (e.g. allocated for a fiber).
pub fn assert_current_state_not_in_range(range: core::ops::Range<usize>) {
let p = raw::get() as usize;
assert!(p < range.start || range.end < p);
}
}
/// Opaque state used to help control TLS state across stack switches for
/// async support.
#[cfg(feature = "async")]
pub struct PreviousAsyncWasmCallState {
// The head of a linked list, similar to the TLS state. Note though that
// this list is stored in reverse order to assist with `push` and `pop`
// below.
//
// After a `push` call this stores the previous head for the current
// thread so we know when to stop popping during a `pop`.
state: raw::Ptr,
}
#[cfg(feature = "async")]
impl PreviousAsyncWasmCallState {
/// Pops a fiber's linked list of activations and stores them in
/// `AsyncWasmCallState`.
///
/// This will pop the top activation of this current thread continuously
/// until it reaches whatever the current activation was when `push` was
/// originally called.
///
/// # Unsafety
///
/// Must be paired with a `push` and only performed at a time when a
/// fiber is being suspended.
pub unsafe fn restore(self) -> AsyncWasmCallState {
let thread_head = self.state;
core::mem::forget(self);
let mut ret = AsyncWasmCallState::new();
loop {
// If the current TLS state is as we originally found it, then
// this loop is finished.
let ptr = raw::get();
if ptr == thread_head {
break ret;
}
// Pop this activation from the current thread's TLS state, and
// then afterwards push it onto our own linked list within this
// `AsyncWasmCallState`. Note that the linked list in `AsyncWasmCallState` is stored
// in reverse order so a subsequent `push` later on pushes
// everything in the right order.
(*ptr).pop();
if let Some(state) = ret.state.as_ref() {
(*ptr).prev.set(state);
}
ret.state = ptr;
}
}
}
#[cfg(feature = "async")]
impl Drop for PreviousAsyncWasmCallState {
fn drop(&mut self) {
panic!("must be consumed with `restore`");
}
}
/// Configures thread local state such that for the duration of the
/// execution of `closure` any call to `with` will yield `state`, unless
/// this is recursively called again.
#[inline]
pub fn set<R>(state: &mut CallThreadState, closure: impl FnOnce(&CallThreadState) -> R) -> R {
struct Reset<'a> {
state: &'a CallThreadState,
}
impl Drop for Reset<'_> {
#[inline]
fn drop(&mut self) {
unsafe {
self.state.pop();
}
}
}
unsafe {
state.push();
let reset = Reset { state };
closure(reset.state)
}
}
/// Returns the last pointer configured with `set` above, if any.
pub fn with<R>(closure: impl FnOnce(Option<&CallThreadState>) -> R) -> R {
let p = raw::get();
unsafe { closure(if p.is_null() { None } else { Some(&*p) }) }
}
}