use crate::Result;
#[cfg(feature = "stack-switching")]
use crate::prelude::*;
use core::ops::Range;
use crate::runtime::vm::VMPayloads;
#[cfg(feature = "stack-switching")]
use crate::runtime::vm::ValRaw;
#[cfg(all(feature = "stack-switching", has_virtual_memory))]
use crate::runtime::vm::host_page_size;
use crate::runtime::vm::{VMContext, VMFuncRef};
#[cfg(feature = "stack-switching")]
pub(crate) fn continuation_runtime_headroom(
parameter_count: u32,
return_value_count: u32,
) -> Result<usize> {
let fixed_headroom = if cfg!(has_virtual_memory) {
host_page_size()
} else {
0usize
};
let slot_size = core::mem::size_of::<ValRaw>();
let trampoline_values_size = usize::try_from(parameter_count.max(return_value_count))?
.checked_mul(slot_size)
.ok_or_else(|| format_err!("continuation trampoline stack size overflow"))?;
let outgoing_args_size = usize::try_from(parameter_count)?
.checked_mul(slot_size)
.ok_or_else(|| format_err!("continuation parameter stack size overflow"))?;
let headroom = fixed_headroom
.checked_add(trampoline_values_size)
.and_then(|n| n.checked_add(outgoing_args_size))
.ok_or_else(|| format_err!("continuation runtime headroom overflow"))?;
Ok(headroom)
}
cfg_select! {
all(feature = "stack-switching", unix, target_arch = "x86_64") => {
mod unix;
use unix as imp;
}
_ => {
mod dummy;
use dummy as imp;
}
}
#[derive(Debug)]
#[repr(C)]
pub struct VMContinuationStack(imp::VMContinuationStack);
impl VMContinuationStack {
pub fn new(size: usize) -> Result<Self> {
Ok(Self(imp::VMContinuationStack::new(size)?))
}
pub fn unallocated() -> Self {
Self(imp::VMContinuationStack::unallocated())
}
pub fn is_unallocated(&self) -> bool {
imp::VMContinuationStack::is_unallocated(&self.0)
}
pub unsafe fn from_raw_parts(bottom: *mut u8, guard_size: usize, len: usize) -> Result<Self> {
Ok(Self(unsafe {
imp::VMContinuationStack::from_raw_parts(bottom, guard_size, len)?
}))
}
pub fn is_from_raw_parts(&self) -> bool {
self.0.is_from_raw_parts()
}
pub fn top(&self) -> Option<*mut u8> {
self.0.top()
}
pub fn range(&self) -> Option<Range<usize>> {
self.0.range()
}
#[cfg(all(feature = "stack-switching", asan))]
pub(crate) fn asan_range(&self) -> Option<Range<usize>> {
self.0.asan_range()
}
pub fn control_context_instruction_pointer(&self) -> usize {
self.0.control_context_instruction_pointer()
}
pub fn control_context_frame_pointer(&self) -> usize {
self.0.control_context_frame_pointer()
}
pub fn control_context_stack_pointer(&self) -> usize {
self.0.control_context_stack_pointer()
}
pub fn initialize(
&self,
func_ref: *const VMFuncRef,
caller_vmctx: *mut VMContext,
args: *mut VMPayloads,
parameter_count: u32,
return_value_count: u32,
gc_refs: bool,
runtime_headroom: usize,
) -> Result<()> {
self.0.initialize(
func_ref,
caller_vmctx,
args,
parameter_count,
return_value_count,
gc_refs,
runtime_headroom,
)
}
}