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
//! Interface to low-level signal-handling mechanisms.

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use crate::vmcontext::VMContext;
use crate::RwLock;
use core::borrow::{Borrow, BorrowMut};
use core::cell::Cell;
use lazy_static::lazy_static;

#[derive(Default)]
struct TrapContext {
    tried_to_install_signal_handlers: Cell<bool>,
    have_signal_handlers: Cell<bool>,
}

extern "C" {
    fn EnsureEagerSignalHandlers() -> libc::c_int;
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    fn EnsureDarwinMachPorts() -> libc::c_int;
}

struct InstallState {
    tried: bool,
    success: bool,
}

impl InstallState {
    fn new() -> Self {
        Self {
            tried: false,
            success: false,
        }
    }
}

lazy_static! {
    static ref EAGER_INSTALL_STATE: RwLock<InstallState> = RwLock::new(InstallState::new());
    static ref LAZY_INSTALL_STATE: RwLock<InstallState> = RwLock::new(InstallState::new());
}

/// This function performs the low-overhead signal handler initialization that we
/// want to do eagerly to ensure a more-deterministic global process state. This
/// is especially relevant for signal handlers since handler ordering depends on
/// installation order: the wasm signal handler must run *before* the other crash
/// handlers and since POSIX signal handlers work LIFO, this function needs to be
/// called at the end of the startup process, after other handlers have been
/// installed. This function can thus be called multiple times, having no effect
/// after the first call.
#[no_mangle]
pub extern "C" fn wasmtime_init_eager() {
    let mut locked = EAGER_INSTALL_STATE.write().unwrap();
    let state = locked.borrow_mut();

    if state.tried {
        return;
    }

    state.tried = true;
    assert!(!state.success);

    if unsafe { EnsureEagerSignalHandlers() == 0 } {
        return;
    }

    state.success = true;
}

thread_local! {
    static TRAP_CONTEXT: TrapContext = TrapContext::default();
}

/// Assuming `EnsureEagerProcessSignalHandlers` has already been called,
/// this function performs the full installation of signal handlers which must
/// be performed per-thread. This operation may incur some overhead and
/// so should be done only when needed to use wasm.
#[no_mangle]
pub extern "C" fn wasmtime_init_finish(vmctx: &mut VMContext) {
    if !TRAP_CONTEXT.with(|cx| cx.tried_to_install_signal_handlers.get()) {
        TRAP_CONTEXT.with(|cx| {
            cx.tried_to_install_signal_handlers.set(true);
            assert!(!cx.have_signal_handlers.get());
        });

        {
            let locked = EAGER_INSTALL_STATE.read().unwrap();
            let state = locked.borrow();
            assert!(
                state.tried,
                "call wasmtime_init_eager before calling wasmtime_init_finish"
            );
            if !state.success {
                return;
            }
        }

        #[cfg(any(target_os = "macos", target_os = "ios"))]
        ensure_darwin_mach_ports();

        TRAP_CONTEXT.with(|cx| {
            cx.have_signal_handlers.set(true);
        })
    }

    let instance = unsafe { vmctx.instance() };
    let have_signal_handlers = TRAP_CONTEXT.with(|cx| cx.have_signal_handlers.get());
    if !have_signal_handlers && instance.needs_signal_handlers() {
        panic!("failed to install signal handlers");
    }
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
fn ensure_darwin_mach_ports() {
    let mut locked = LAZY_INSTALL_STATE.write().unwrap();
    let state = locked.borrow_mut();

    if state.tried {
        return;
    }

    state.tried = true;
    assert!(!state.success);

    if unsafe { EnsureDarwinMachPorts() != 0 } {
        return;
    }

    state.success = true;
}