Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

3.4 KB
Raw Blame
//! ISA-specific stack-switching routines.

// The bodies are defined in inline assembly in the conditionally
// included modules below; their symbols are visible in the binary and
// accessed via the `extern "C"` declarations below that.

/// Raw fibers on top of the routines here.
pub(crate) mod raw;

cfg_select! {
    // Miri uses another fiber implementation, but raw fibers still refer to
    // the routines here.
    all(feature = "std", miri) => {
        pub(crate) use unsupported::*;
    }
    target_arch = "aarch64" => {
        mod aarch64;
        pub(crate) use supported::*;
        pub(crate) use aarch64::*;
    }
    target_arch = "x86_64" => {
        mod x86_64;
        pub(crate) use supported::*;
        pub(crate) use x86_64::*;
    }
    target_arch = "x86" => {
        mod x86;
        pub(crate) use supported::*;
        pub(crate) use x86::*;
    }
    target_arch = "arm" => {
        mod arm;
        pub(crate) use supported::*;
        pub(crate) use arm::*;
    }
    target_arch = "s390x" => {
        mod s390x;
        pub(crate) use supported::*;
        pub(crate) use s390x::*;
    }
    target_arch = "riscv64" => {
        mod riscv64;
        pub(crate) use supported::*;
        pub(crate) use riscv64::*;
    }
    all(target_arch = "riscv32", not(target_feature = "f"), not(target_feature = "v")) => {
        mod riscv32imac;
        pub(crate) use supported::*;
        pub(crate) use riscv32imac::*;
    }
    target_arch = "loongarch64" => {
        mod loongarch64;
        pub(crate) use supported::*;
        pub(crate) use loongarch64::*;
    }
    feature = "custom" => {
        mod custom;
        pub(crate) use custom::*;
    }
    _ => {
        // No support for this platform. Don't fail compilation though and
        // instead defer the error to happen at runtime when a fiber is created.
        // Should help keep compiles working and narrows the failure to only
        // situations that need fibers on unsupported platforms.
        pub(crate) use unsupported::*;
    }
}

/// A helper module to get reexported above in each case that we actually have
/// stack-switching routines available in inline asm. The fall-through case
/// though reexports the `unsupported` module instead.
#[allow(
    dead_code,
    reason = "expected to have dead code in some configurations"
)]
mod supported {
    pub const SUPPORTED_ARCH: bool = true;

    /// Whether raw fibers can use the routines here. They require the
    /// reserved-slot stack layout documented in `unix.rs`.
    pub const RAW_FIBERS: bool = true;
}

/// Helper module reexported in the fallback case above when the current host
/// architecture is not supported for stack switching. The `SUPPORTED_ARCH`
/// boolean here is set to `false` which causes `Fiber::new` to return `false`.
#[allow(
    dead_code,
    reason = "expected to have dead code in some configurations"
)]
mod unsupported {
    pub const SUPPORTED_ARCH: bool = false;
    pub const RAW_FIBERS: bool = false;
    pub const SUSPENDED_FRAME: Option<(usize, usize)> = None;

    pub(crate) unsafe fn wasmtime_fiber_init(
        _top_of_stack: *mut u8,
        _entry: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
        _entry_arg0: *mut u8,
    ) {
        unreachable!();
    }

    pub(crate) unsafe fn wasmtime_fiber_switch(_top_of_stack: *mut u8) {
        unreachable!();
    }

    pub(crate) unsafe extern "C" fn wasmtime_fiber_switch_(_top_of_stack: *mut u8) {
        unreachable!();
    }
}