use std::{
alloc::{Layout, alloc, dealloc, handle_alloc_error},
error::Error,
fmt,
marker::PhantomData,
mem::ManuallyDrop,
ptr::NonNull,
sync::Arc,
};
use crate::{
runtime::{
ExecutionErrorCode, LeasedJitCompilationUnit, LehitValue, OwnedJitCompilationUnit,
TypeMetadataRegistry, TypeOrigin,
},
semantic::{CanonicalSymbolPath, SemanticNamedType, SemanticType},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LehitTypeDescriptor<const SIZE: usize> {
type_: SemanticNamedType,
compilation_identity: u64,
}
impl<const SIZE: usize> LehitTypeDescriptor<SIZE> {
fn new(
type_: SemanticNamedType,
metadata: &TypeMetadataRegistry,
compilation_identity: u64,
) -> Result<Self, AbiCallError> {
let semantic = SemanticType::Named(type_.clone());
let layout = metadata
.get_named(&type_)
.ok_or_else(|| AbiCallError::UnsupportedType(semantic.clone()))?;
if layout.origin != TypeOrigin::Lehit || layout.size != SIZE {
return Err(AbiCallError::LehitValueMismatch {
expected: type_.path.clone(),
semantic,
});
}
Ok(Self {
type_,
compilation_identity,
})
}
pub fn concrete_type(&self) -> &SemanticNamedType {
&self.type_
}
pub fn type_path(&self) -> &CanonicalSymbolPath {
&self.type_.path
}
fn type_for(&self, identity: u64) -> Result<&SemanticNamedType, AbiCallError> {
if self.compilation_identity == identity {
Ok(&self.type_)
} else {
Err(AbiCallError::DescriptorCompilationUnitMismatch)
}
}
}
pub unsafe trait NativeCallValue: Copy + 'static {
fn matches(type_: &SemanticType) -> bool;
fn is_void() -> bool {
false
}
}
macro_rules! scalar_call_value {
($($rust:ty => $name:literal),* $(,)?) => {$ (
unsafe impl NativeCallValue for $rust {
fn matches(type_: &SemanticType) -> bool {
matches!(type_, SemanticType::Named(named)
if named.path.module == crate::core_lib::PRIMITIVE_MODULE_FILE_PATH
&& named.path.local == $name
&& named.generic_arguments.is_empty())
}
}
)* };
}
scalar_call_value!(
bool => "bool",
i8 => "i8",
i16 => "i16",
i32 => "i32",
i64 => "i64",
u8 => "u8",
u16 => "u16",
u32 => "u32",
u64 => "u64",
isize => "isize",
usize => "usize",
f32 => "f32",
f64 => "f64",
);
unsafe impl NativeCallValue for () {
fn matches(type_: &SemanticType) -> bool {
matches!(type_, SemanticType::Void)
}
fn is_void() -> bool {
true
}
}
pub struct Function {
arguments: Vec<CallData>,
result: Option<CallData>,
arguments_size: usize,
arguments_align: usize,
platform_abi_function_ptr: Option<*const ()>,
lehit_abi_function: AbiFunction,
result_drop: Option<*const u8>,
unit: Arc<LeasedJitCompilationUnit>,
}
pub struct CallData {
type_: SemanticType,
kind: CallDataKind,
offset: usize,
size: usize,
align: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallDataKind {
Value,
IndirectValue,
Ref,
Mut,
FatRef,
FatMut,
}
impl CallDataKind {
fn indirect_size_and_align(&self) -> Option<(usize, usize)> {
match self {
Self::Value => None,
Self::IndirectValue | Self::Ref | Self::Mut => Some((
std::mem::size_of::<*const u8>(),
std::mem::align_of::<*const u8>(),
)),
Self::FatRef | Self::FatMut => Some((
std::mem::size_of::<*const u8>() + std::mem::size_of::<usize>(),
std::mem::align_of::<*const u8>(),
)),
}
}
}
pub type LehitAbiFunction =
unsafe extern "C" fn(result: *mut u8, arguments: *const u8) -> AbiCallStatus;
pub struct AbiFunction {
function: LehitAbiFunction,
}
impl AbiFunction {
pub fn pointer(&self) -> LehitAbiFunction {
self.function
}
}
struct AlignedCallBuffer {
pointer: NonNull<u8>,
layout: Layout,
}
impl AlignedCallBuffer {
fn new(size: usize, align: usize) -> Self {
let layout = Layout::from_size_align(size, align).expect("valid ABI call-buffer layout");
let pointer = if size == 0 {
NonNull::dangling()
} else {
NonNull::new(unsafe { alloc(layout) }).unwrap_or_else(|| handle_alloc_error(layout))
};
Self { pointer, layout }
}
#[cfg(test)]
fn size(&self) -> usize {
self.layout.size()
}
#[cfg(test)]
fn align(&self) -> usize {
self.layout.align()
}
fn as_ptr(&self) -> *const u8 {
self.pointer.as_ptr()
}
fn as_mut_ptr(&mut self) -> *mut u8 {
self.pointer.as_ptr()
}
}
impl Drop for AlignedCallBuffer {
fn drop(&mut self) {
if self.layout.size() != 0 {
unsafe { dealloc(self.pointer.as_ptr(), self.layout) };
}
}
}
pub struct AbiCallBuilder<'function> {
function: &'function Function,
current_argument_index: usize,
arguments: AlignedCallBuffer,
owned_arguments: Vec<AlignedCallBuffer>,
pending_owned_drops: Vec<(*mut u8, *const u8)>,
}
pub struct ScopedAbiCallBuilder<'function, 'arguments> {
inner: AbiCallBuilder<'function>,
_arguments: PhantomData<&'arguments mut ()>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AbiCallError {
UnknownFunction(CanonicalSymbolPath),
UnsupportedType(SemanticType),
TooManyArguments {
expected: usize,
},
MissingArguments {
expected: usize,
actual: usize,
},
ArgumentTypeMismatch {
index: usize,
rust: &'static str,
semantic: SemanticType,
},
ReturnTypeMismatch {
rust: &'static str,
semantic: SemanticType,
},
DescriptorCompilationUnitMismatch,
LehitValueMismatch {
expected: CanonicalSymbolPath,
semantic: SemanticType,
},
Execution(AbiCallStatus),
}
impl fmt::Display for AbiCallError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownFunction(path) => write!(
formatter,
"compiled unit has no Lehit ABI entry for `{}::{}`",
path.module, path.local
),
Self::UnsupportedType(type_) => write!(formatter, "unsupported ABI type `{type_:?}`"),
Self::TooManyArguments { expected } => {
write!(formatter, "call accepts only {expected} arguments")
}
Self::MissingArguments { expected, actual } => write!(
formatter,
"call requires {expected} arguments, but only {actual} were supplied"
),
Self::ArgumentTypeMismatch {
index,
rust,
semantic,
} => write!(
formatter,
"Rust argument {index} type `{rust}` is incompatible with `{semantic:?}`"
),
Self::ReturnTypeMismatch { rust, semantic } => write!(
formatter,
"Rust return type `{rust}` is incompatible with `{semantic:?}`"
),
Self::DescriptorCompilationUnitMismatch => {
formatter.write_str("Lehit type descriptor belongs to a different compilation unit")
}
Self::LehitValueMismatch { expected, semantic } => write!(
formatter,
"expected Lehit type `{}::{}`, got `{semantic:?}`",
expected.module, expected.local
),
Self::Execution(status) => {
write!(
formatter,
"Lehit execution failed with status {}",
status.into_raw()
)
}
}
}
}
impl Error for AbiCallError {}
impl AbiCallError {
pub const fn status(&self) -> Option<AbiCallStatus> {
match self {
Self::Execution(status) => Some(*status),
_ => None,
}
}
}
impl CallData {
pub fn semantic_type(&self) -> &SemanticType {
&self.type_
}
pub fn kind(&self) -> CallDataKind {
self.kind
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn size(&self) -> usize {
self.size
}
pub fn align(&self) -> usize {
self.align
}
}
impl Function {
pub(crate) fn new(
unit: Arc<LeasedJitCompilationUnit>,
path: &CanonicalSymbolPath,
) -> Result<Self, AbiCallError> {
let signature = unit
.signature(path)
.ok_or_else(|| AbiCallError::UnknownFunction(path.clone()))?;
if signature.receiver.is_some() {
return Err(AbiCallError::UnsupportedType(SemanticType::SelfType));
}
let mut arguments = Vec::with_capacity(signature.arguments.len());
let mut next_offset = 0usize;
let mut arguments_align = 1usize;
for argument in &signature.arguments {
let kind = match &argument.type_ {
SemanticType::Reference { mutable: true, .. } => CallDataKind::Mut,
SemanticType::Reference { mutable: false, .. } => CallDataKind::Ref,
SemanticType::Pointer { .. } => CallDataKind::Value,
SemanticType::Named(named)
if named.path.module == crate::core_lib::PRIMITIVE_MODULE_FILE_PATH =>
{
CallDataKind::Value
}
_ => match unit.type_metadata().metadata_of(&argument.type_) {
Some(metadata)
if matches!(
metadata.kind,
crate::runtime::TypeMetadataKind::Scalar { .. }
) =>
{
CallDataKind::Value
}
_ => CallDataKind::IndirectValue,
},
};
let (size, align) = kind
.indirect_size_and_align()
.or_else(|| unit.type_metadata().layout_of(&argument.type_))
.ok_or_else(|| AbiCallError::UnsupportedType(argument.type_.clone()))?;
next_offset = next_offset.next_multiple_of(align);
arguments.push(CallData {
type_: argument.type_.clone(),
kind,
offset: next_offset,
size,
align,
});
next_offset += size;
arguments_align = arguments_align.max(align);
}
let arguments_size = next_offset.next_multiple_of(arguments_align);
let result = if matches!(signature.return_type.as_ref(), SemanticType::Void) {
None
} else {
let (size, align) = unit
.type_metadata()
.layout_of(signature.return_type.as_ref())
.ok_or_else(|| AbiCallError::UnsupportedType(*signature.return_type.clone()))?;
Some(CallData {
type_: *signature.return_type.clone(),
kind: CallDataKind::Value,
offset: 0,
size,
align,
})
};
let pointer = unit
.lehit_abi_function_ptr(path)
.ok_or_else(|| AbiCallError::UnknownFunction(path.clone()))?;
let function = unsafe { std::mem::transmute::<*const u8, LehitAbiFunction>(pointer) };
Ok(Self {
arguments,
result,
arguments_size,
arguments_align,
platform_abi_function_ptr: unit.function_ptr(path).map(|pointer| pointer.cast()),
lehit_abi_function: AbiFunction { function },
result_drop: unit.return_drop_ptr(path),
unit,
})
}
pub fn lehit_type_descriptor<const SIZE: usize>(
&self,
type_: SemanticNamedType,
) -> Result<LehitTypeDescriptor<SIZE>, AbiCallError> {
LehitTypeDescriptor::new(type_, self.unit.type_metadata(), self.unit.identity())
}
pub fn arguments_size_and_align(&self) -> (usize, usize) {
(self.arguments_size, self.arguments_align)
}
pub fn result_size_and_align(&self) -> (usize, usize) {
self.result
.as_ref()
.map_or((0, 1), |result| (result.size, result.align))
}
pub fn get_arguments(&self) -> &[CallData] {
&self.arguments
}
pub fn get_result(&self) -> Option<&CallData> {
self.result.as_ref()
}
pub fn abi_call_builder(&self) -> AbiCallBuilder<'_> {
AbiCallBuilder {
function: self,
current_argument_index: 0,
arguments: AlignedCallBuffer::new(self.arguments_size, self.arguments_align),
owned_arguments: Vec::new(),
pending_owned_drops: Vec::new(),
}
}
pub fn call_scoped<'arguments>(
&'arguments self,
configure: impl FnOnce(&mut ScopedAbiCallBuilder<'_, 'arguments>) -> Result<(), AbiCallError>,
) -> Result<AbiCallResult<'arguments>, AbiCallError> {
let mut builder = ScopedAbiCallBuilder {
inner: self.abi_call_builder(),
_arguments: PhantomData,
};
configure(&mut builder)?;
builder.inner.call()
}
pub fn platform_abi_function_ptr(&self) -> Option<*const ()> {
self.platform_abi_function_ptr
}
pub fn lehit_abi_function(&self) -> &AbiFunction {
&self.lehit_abi_function
}
}
impl<'function> AbiCallBuilder<'function> {
pub fn push_rust_value_argument<T: NativeCallValue>(
&mut self,
value: T,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
if argument.kind != CallDataKind::Value
|| !T::matches(&argument.type_)
|| argument.size != std::mem::size_of::<T>()
|| argument.align != std::mem::align_of::<T>()
{
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<T>(),
semantic: argument.type_.clone(),
});
}
unsafe {
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<T>()
.write(value);
}
self.current_argument_index += 1;
Ok(self)
}
pub unsafe fn push_lehit_value_argument<const SIZE: usize>(
&mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
value: LehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let expected_type = descriptor
.type_for(self.function.unit.identity())
.map_err(|_| AbiCallError::DescriptorCompilationUnitMismatch)?;
let (value_size, value_align) = self
.function
.unit
.type_metadata()
.layout_of(&argument.type_)
.ok_or_else(|| AbiCallError::UnsupportedType(argument.type_.clone()))?;
if argument.kind != CallDataKind::IndirectValue
|| argument.type_ != SemanticType::Named(expected_type.clone())
|| value_size != SIZE
{
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<LehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
}
let mut storage = AlignedCallBuffer::new(value_size, value_align);
unsafe {
std::ptr::copy_nonoverlapping(
value.as_bytes().as_ptr(),
storage.as_mut_ptr(),
value_size,
);
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*const u8>()
.write(storage.as_ptr());
}
self.owned_arguments.push(storage);
self.current_argument_index += 1;
Ok(self)
}
pub fn push_leased_lehit_value_argument<const SIZE: usize>(
&mut self,
value: LeasedLehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let SemanticType::Named(expected) = &argument.type_ else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<LeasedLehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
};
if argument.kind != CallDataKind::IndirectValue {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<LeasedLehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
}
let (value, drop_pointer) = value.take_for_call(&self.function.unit, expected)?;
let (_, align) = self
.function
.unit
.type_metadata()
.layout_of(&argument.type_)
.ok_or_else(|| AbiCallError::UnsupportedType(argument.type_.clone()))?;
let mut storage = AlignedCallBuffer::new(SIZE, align);
unsafe {
std::ptr::copy_nonoverlapping(value.as_bytes().as_ptr(), storage.as_mut_ptr(), SIZE);
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*const u8>()
.write(storage.as_ptr());
}
if let Some(drop_pointer) = drop_pointer {
self.pending_owned_drops
.push((storage.as_mut_ptr(), drop_pointer));
}
self.owned_arguments.push(storage);
self.current_argument_index += 1;
Ok(self)
}
pub fn push_rust_ref_argument<T: NativeCallValue>(
&mut self,
value: &T,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let SemanticType::Reference {
mutable: false,
target,
..
} = &argument.type_
else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&T>(),
semantic: argument.type_.clone(),
});
};
if argument.kind != CallDataKind::Ref || !T::matches(target) {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&T>(),
semantic: argument.type_.clone(),
});
}
let mut storage =
AlignedCallBuffer::new(std::mem::size_of::<T>(), std::mem::align_of::<T>());
unsafe {
storage.as_mut_ptr().cast::<T>().write(*value);
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*const u8>()
.write(storage.as_ptr());
}
self.owned_arguments.push(storage);
self.current_argument_index += 1;
Ok(self)
}
pub fn push_lehit_ref_argument<const SIZE: usize>(
&mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
value: &LehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let expected_type = descriptor
.type_for(self.function.unit.identity())
.map_err(|_| AbiCallError::DescriptorCompilationUnitMismatch)?;
let SemanticType::Reference {
mutable: false,
target,
..
} = &argument.type_
else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&LehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
};
if target.as_ref() != &SemanticType::Named(expected_type.clone()) {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&LehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
}
let (_, align) = self
.function
.unit
.type_metadata()
.layout_of(target)
.ok_or_else(|| AbiCallError::UnsupportedType(*target.clone()))?;
let mut storage = AlignedCallBuffer::new(SIZE, align);
unsafe {
std::ptr::copy_nonoverlapping(value.as_bytes().as_ptr(), storage.as_mut_ptr(), SIZE);
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*const u8>()
.write(storage.as_ptr());
}
self.owned_arguments.push(storage);
self.current_argument_index += 1;
Ok(self)
}
pub fn push_leased_lehit_ref_argument<const SIZE: usize>(
&mut self,
value: &LeasedLehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let SemanticType::Reference {
mutable: false,
target,
..
} = &argument.type_
else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&LeasedLehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
};
let SemanticType::Named(expected) = target.as_ref() else {
return Err(AbiCallError::UnsupportedType(*target.clone()));
};
value.validate_for(&self.function.unit, expected)?;
let (size, align) = self
.function
.unit
.type_metadata()
.layout_of(target)
.ok_or_else(|| AbiCallError::UnsupportedType(*target.clone()))?;
if size != SIZE {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&LeasedLehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
}
let mut storage = AlignedCallBuffer::new(size, align);
unsafe {
std::ptr::copy_nonoverlapping(value.as_bytes().as_ptr(), storage.as_mut_ptr(), size);
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*const u8>()
.write(storage.as_ptr());
}
self.owned_arguments.push(storage);
self.current_argument_index += 1;
Ok(self)
}
pub unsafe fn push_rust_mut_argument<T: NativeCallValue>(
&mut self,
value: *mut T,
) -> Result<&mut Self, AbiCallError> {
unsafe { self.push_mut_pointer::<T>(value.cast(), T::matches) }
}
pub unsafe fn push_lehit_mut_argument<const SIZE: usize>(
&mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
value: *mut LehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let expected = descriptor
.type_for(self.function.unit.identity())
.map_err(|_| AbiCallError::DescriptorCompilationUnitMismatch)?
.clone();
unsafe {
self.push_mut_pointer::<LehitValue<SIZE>>(value.cast(), move |type_| {
type_ == &SemanticType::Named(expected.clone())
})
}
}
unsafe fn push_mut_pointer<T>(
&mut self,
value: *mut u8,
matches: impl Fn(&SemanticType) -> bool,
) -> Result<&mut Self, AbiCallError> {
let index = self.current_argument_index;
let argument =
self.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.function.arguments.len(),
})?;
let SemanticType::Reference {
mutable: true,
target,
..
} = &argument.type_
else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<T>(),
semantic: argument.type_.clone(),
});
};
if argument.kind != CallDataKind::Mut || !matches(target) {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<T>(),
semantic: argument.type_.clone(),
});
}
unsafe {
self.arguments
.as_mut_ptr()
.add(argument.offset)
.cast::<*mut u8>()
.write(value);
}
self.current_argument_index += 1;
Ok(self)
}
pub fn call(mut self) -> Result<AbiCallResult<'function>, AbiCallError> {
if self.current_argument_index != self.function.arguments.len() {
return Err(AbiCallError::MissingArguments {
expected: self.function.arguments.len(),
actual: self.current_argument_index,
});
}
let (result_size, result_align) = self.function.result_size_and_align();
let mut result = AlignedCallBuffer::new(result_size, result_align);
self.pending_owned_drops.clear();
let status = unsafe {
(self.function.lehit_abi_function.function)(
result.as_mut_ptr(),
self.arguments.as_ptr(),
)
};
if status.is_success() {
Ok(AbiCallResult {
data: result,
function: self.function,
drop_pending: self.function.result_drop.is_some(),
})
} else {
Err(AbiCallError::Execution(status))
}
}
}
pub struct AbiCallResult<'function> {
data: AlignedCallBuffer,
function: &'function Function,
drop_pending: bool,
}
impl Drop for AbiCallBuilder<'_> {
fn drop(&mut self) {
for (value, drop_pointer) in self.pending_owned_drops.drain(..).rev() {
let drop_value: unsafe extern "C" fn(*mut u8) =
unsafe { std::mem::transmute(drop_pointer) };
unsafe { drop_value(value) };
}
}
}
impl<'function, 'arguments> ScopedAbiCallBuilder<'function, 'arguments> {
pub fn push_rust_value_argument<T: NativeCallValue>(
&mut self,
value: T,
) -> Result<&mut Self, AbiCallError> {
self.inner.push_rust_value_argument(value)?;
Ok(self)
}
pub fn push_rust_ref_argument<T: NativeCallValue>(
&mut self,
value: &'arguments T,
) -> Result<&mut Self, AbiCallError> {
self.inner.push_rust_ref_argument(value)?;
Ok(self)
}
pub fn push_rust_mut_argument<T: NativeCallValue>(
&mut self,
value: &'arguments mut T,
) -> Result<&mut Self, AbiCallError> {
unsafe { self.inner.push_rust_mut_argument(value)? };
Ok(self)
}
pub fn push_lehit_ref_argument<const SIZE: usize>(
&mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
value: &'arguments LehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
self.inner.push_lehit_ref_argument(descriptor, value)?;
Ok(self)
}
pub fn push_leased_lehit_value_argument<const SIZE: usize>(
&mut self,
value: LeasedLehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
self.inner.push_leased_lehit_value_argument(value)?;
Ok(self)
}
pub fn push_leased_lehit_mut_argument<const SIZE: usize>(
&mut self,
value: &'arguments mut LeasedLehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
let index = self.inner.current_argument_index;
let argument =
self.inner
.function
.arguments
.get(index)
.ok_or(AbiCallError::TooManyArguments {
expected: self.inner.function.arguments.len(),
})?;
let SemanticType::Reference {
mutable: true,
target,
..
} = &argument.type_
else {
return Err(AbiCallError::ArgumentTypeMismatch {
index,
rust: std::any::type_name::<&mut LeasedLehitValue<SIZE>>(),
semantic: argument.type_.clone(),
});
};
let SemanticType::Named(expected) = target.as_ref() else {
return Err(AbiCallError::UnsupportedType(*target.clone()));
};
value.validate_for(&self.inner.function.unit, expected)?;
let pointer = value.value.as_bytes_mut().as_mut_ptr();
let expected = expected.clone();
unsafe {
self.inner
.push_mut_pointer::<LehitValue<SIZE>>(pointer, move |type_| {
type_ == &SemanticType::Named(expected.clone())
})?;
}
Ok(self)
}
pub fn push_lehit_mut_argument<const SIZE: usize>(
&mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
value: &'arguments mut LehitValue<SIZE>,
) -> Result<&mut Self, AbiCallError> {
unsafe { self.inner.push_lehit_mut_argument(descriptor, value)? };
Ok(self)
}
}
impl AbiCallResult<'_> {
pub fn into_rust_value<T: NativeCallValue>(mut self) -> Result<T, AbiCallError> {
let Some(result) = self.function.result.as_ref() else {
return Err(AbiCallError::ReturnTypeMismatch {
rust: std::any::type_name::<T>(),
semantic: SemanticType::Void,
});
};
if !T::matches(&result.type_)
|| result.size != std::mem::size_of::<T>()
|| result.align != std::mem::align_of::<T>()
{
return Err(AbiCallError::ReturnTypeMismatch {
rust: std::any::type_name::<T>(),
semantic: result.type_.clone(),
});
}
self.drop_pending = false;
Ok(unsafe { self.data.as_ptr().cast::<T>().read() })
}
pub fn into_leased_lehit_value<const SIZE: usize>(
mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
) -> Result<LeasedLehitValue<SIZE>, AbiCallError> {
let value = self.read_lehit_value(descriptor)?;
self.drop_pending = false;
Ok(LeasedLehitValue {
value: ManuallyDrop::new(value),
drop_pointer: self.function.result_drop,
type_: descriptor.concrete_type().clone(),
_unit: Arc::clone(&self.function.unit),
})
}
pub unsafe fn into_lehit_value<const SIZE: usize>(
mut self,
descriptor: &LehitTypeDescriptor<SIZE>,
) -> Result<LehitValue<SIZE>, AbiCallError> {
let value = self.read_lehit_value(descriptor)?;
self.drop_pending = false;
Ok(value)
}
fn read_lehit_value<const SIZE: usize>(
&self,
descriptor: &LehitTypeDescriptor<SIZE>,
) -> Result<LehitValue<SIZE>, AbiCallError> {
let Some(result) = self.function.result.as_ref() else {
return Err(AbiCallError::ReturnTypeMismatch {
rust: std::any::type_name::<LehitValue<SIZE>>(),
semantic: SemanticType::Void,
});
};
let expected = descriptor
.type_for(self.function.unit.identity())
.map_err(|_| AbiCallError::DescriptorCompilationUnitMismatch)?;
if result.type_ != SemanticType::Named(expected.clone()) || result.size != SIZE {
return Err(AbiCallError::ReturnTypeMismatch {
rust: std::any::type_name::<LehitValue<SIZE>>(),
semantic: result.type_.clone(),
});
}
let mut bytes = [0u8; SIZE];
unsafe {
std::ptr::copy_nonoverlapping(self.data.as_ptr(), bytes.as_mut_ptr(), SIZE);
}
Ok(LehitValue::from_bytes(bytes))
}
pub fn into_void(mut self) -> Result<(), AbiCallError> {
if self.function.result.is_none() {
self.drop_pending = false;
Ok(())
} else {
Err(AbiCallError::ReturnTypeMismatch {
rust: std::any::type_name::<()>(),
semantic: self.function.result.as_ref().unwrap().type_.clone(),
})
}
}
}
impl Drop for AbiCallResult<'_> {
fn drop(&mut self) {
if !self.drop_pending {
return;
}
if let Some(pointer) = self.function.result_drop {
let drop_value: unsafe extern "C" fn(*mut u8) = unsafe { std::mem::transmute(pointer) };
unsafe { drop_value(self.data.as_mut_ptr()) };
}
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct AbiCallStatus(u32);
impl AbiCallStatus {
pub const SUCCESS: Self = Self(ExecutionErrorCode::None as u32);
pub const BOUNDS_VIOLATION: Self = Self(ExecutionErrorCode::BoundsViolation as u32);
pub const DIVISION_BY_ZERO: Self = Self(ExecutionErrorCode::DivisionByZero as u32);
pub const INTEGER_OVERFLOW: Self = Self(ExecutionErrorCode::IntegerOverflow as u32);
pub const INVALID_SHIFT: Self = Self(ExecutionErrorCode::InvalidShift as u32);
pub const fn from_raw(value: u32) -> Self {
Self(value)
}
pub const fn into_raw(self) -> u32 {
self.0
}
pub const fn is_success(self) -> bool {
self.0 == Self::SUCCESS.0
}
}
pub struct LeasedLehitValue<const SIZE: usize> {
value: ManuallyDrop<LehitValue<SIZE>>,
drop_pointer: Option<*const u8>,
type_: SemanticNamedType,
_unit: Arc<LeasedJitCompilationUnit>,
}
impl OwnedJitCompilationUnit {
pub fn lehit_type_descriptor<const SIZE: usize>(
&self,
type_: SemanticNamedType,
) -> Result<LehitTypeDescriptor<SIZE>, AbiCallError> {
LehitTypeDescriptor::new(type_, self.type_metadata(), self.identity())
}
}
impl LeasedJitCompilationUnit {
pub fn lehit_type_descriptor<const SIZE: usize>(
&self,
type_: SemanticNamedType,
) -> Result<LehitTypeDescriptor<SIZE>, AbiCallError> {
LehitTypeDescriptor::new(type_, self.type_metadata(), self.identity())
}
}
impl<const SIZE: usize> LeasedLehitValue<SIZE> {
pub fn as_bytes(&self) -> &[u8; SIZE] {
self.value.as_bytes()
}
pub fn as_bytes_mut(&mut self) -> &mut [u8; SIZE] {
self.value.as_bytes_mut()
}
pub fn type_path(&self) -> &CanonicalSymbolPath {
&self.type_.path
}
pub fn concrete_type(&self) -> &SemanticNamedType {
&self.type_
}
pub unsafe fn into_inner(mut self) -> LehitValue<SIZE> {
self.drop_pointer = None;
unsafe { ManuallyDrop::take(&mut self.value) }
}
fn validate_for(
&self,
unit: &LeasedJitCompilationUnit,
expected: &SemanticNamedType,
) -> Result<(), AbiCallError> {
if self._unit.identity() != unit.identity() {
return Err(AbiCallError::DescriptorCompilationUnitMismatch);
}
if &self.type_ != expected {
return Err(AbiCallError::LehitValueMismatch {
expected: expected.path.clone(),
semantic: SemanticType::Named(self.type_.clone()),
});
}
Ok(())
}
fn take_for_call(
mut self,
unit: &LeasedJitCompilationUnit,
expected: &SemanticNamedType,
) -> Result<(LehitValue<SIZE>, Option<*const u8>), AbiCallError> {
self.validate_for(unit, expected)?;
let drop_pointer = self.drop_pointer.take();
let value = unsafe { ManuallyDrop::take(&mut self.value) };
Ok((value, drop_pointer))
}
}
impl<const SIZE: usize> fmt::Debug for LeasedLehitValue<SIZE> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("LeasedLehitValue")
.field("type", &self.type_)
.field("bytes", &self.as_bytes())
.finish()
}
}
impl<const SIZE: usize> Drop for LeasedLehitValue<SIZE> {
fn drop(&mut self) {
if let Some(pointer) = self.drop_pointer {
let function: unsafe extern "C" fn(*mut u8) =
unsafe { std::mem::transmute_copy(&pointer) };
unsafe { function(self.value.as_bytes_mut().as_mut_ptr()) };
}
}
}
#[cfg(test)]
mod tests {
use super::{AbiCallStatus, AlignedCallBuffer, CallDataKind};
use crate::{runtime::ExecutionErrorCode, semantic::SemanticType};
#[test]
fn aligned_call_buffer_handles_zero_sized_and_aligned_storage() {
let mut empty = AlignedCallBuffer::new(0, 1);
assert_eq!(empty.size(), 0);
assert_eq!(empty.align(), 1);
assert!(!empty.as_ptr().is_null());
assert!(!empty.as_mut_ptr().is_null());
let mut buffer = AlignedCallBuffer::new(32, 16);
assert_eq!(buffer.size(), 32);
assert_eq!(buffer.align(), 16);
assert_eq!((buffer.as_ptr() as usize) % 16, 0);
unsafe { buffer.as_mut_ptr().write_bytes(0xA5, buffer.size()) };
}
#[test]
fn abi_call_status_has_a_fixed_u32_representation() {
assert_eq!(
std::mem::size_of::<AbiCallStatus>(),
std::mem::size_of::<u32>()
);
assert!(AbiCallStatus::SUCCESS.is_success());
assert!(!AbiCallStatus::DIVISION_BY_ZERO.is_success());
assert_eq!(
AbiCallStatus::BOUNDS_VIOLATION.into_raw(),
ExecutionErrorCode::BoundsViolation as u32
);
assert_eq!(AbiCallStatus::from_raw(99).into_raw(), 99);
}
#[test]
fn indirect_argument_slots_use_machine_pointer_layout() {
let pointer = (
std::mem::size_of::<*const u8>(),
std::mem::align_of::<*const u8>(),
);
let fat_pointer = (
std::mem::size_of::<*const u8>() + std::mem::size_of::<usize>(),
std::mem::align_of::<*const u8>(),
);
assert_eq!(CallDataKind::Ref.indirect_size_and_align(), Some(pointer));
assert_eq!(CallDataKind::Mut.indirect_size_and_align(), Some(pointer));
assert_eq!(
CallDataKind::FatRef.indirect_size_and_align(),
Some(fat_pointer)
);
assert_eq!(
CallDataKind::FatMut.indirect_size_and_align(),
Some(fat_pointer)
);
assert_eq!(CallDataKind::Value.indirect_size_and_align(), None);
assert_eq!(
CallDataKind::IndirectValue.indirect_size_and_align(),
Some(pointer)
);
}
#[test]
fn scalar_call_values_require_the_canonical_primitive_module() {
let foreign_i32 = SemanticType::Named(crate::semantic::SemanticNamedType {
path: crate::semantic::CanonicalSymbolPath {
module: "app://foreign.lt".into(),
local: "i32".into(),
},
generic_arguments: Vec::new(),
});
let core_i32 = SemanticType::Named(crate::semantic::SemanticNamedType {
path: crate::semantic::CanonicalSymbolPath {
module: crate::core_lib::PRIMITIVE_MODULE_FILE_PATH.into(),
local: "i32".into(),
},
generic_arguments: Vec::new(),
});
assert!(!<i32 as crate::runtime::NativeCallValue>::matches(
&foreign_i32
));
assert!(<i32 as crate::runtime::NativeCallValue>::matches(&core_i32));
}
}