From: Bennet Bleßmann Date: Tue, 21 Jan 2025 21:23:50 +0000 (+0100) Subject: use libffi::middle instead of libffi::low were possible X-Git-Tag: v0.10.0~29^2~10 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=3d2439c92b038f8ecc01ce24f408170d61dd0bdb;p=scryer-prolog.git use libffi::middle instead of libffi::low were possible --- diff --git a/src/ffi.rs b/src/ffi.rs index 2f949bf8..1cc762dd 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -25,17 +25,17 @@ use crate::forms::Number; use crate::parser::ast::Fixnum; use dashu::Integer; +use libffi::middle::{Arg, Cif, CodePtr, Type}; +use libloading::{Library, Symbol}; use ordered_float::OrderedFloat; use std::alloc::{self, Layout}; use std::any::Any; use std::collections::HashMap; use std::error::Error; use std::ffi::{c_void, CString}; -use std::ptr::addr_of_mut; - -use libffi::low::type_tag::STRUCT; -use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, ffi_cif, ffi_type, prep_cif, types, CodePtr}; -use libloading::{Library, Symbol}; +use std::fmt::Debug; +use std::marker::PhantomData; +use std::ops::Deref; pub struct FunctionDefinition { pub name: String, @@ -45,8 +45,8 @@ pub struct FunctionDefinition { #[derive(Debug)] pub struct FunctionImpl { - cif: ffi_cif, - args: Vec<*mut ffi_type>, + cif: Cif, + args: Vec, code_ptr: CodePtr, return_struct_name: Option, } @@ -57,26 +57,65 @@ pub struct ForeignFunctionTable { structs: HashMap, } -#[derive(Clone)] +#[derive(Clone, Debug)] struct StructImpl { - ffi_type: ffi_type, - fields: Vec<*mut ffi_type>, + ffi_type: Type, + fields: Vec, atom_fields: Vec, } -impl std::fmt::Debug for StructImpl { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("StructImpl") - .field("ffi_type", &&"") - .field("fields", &self.fields) - .field("atom_fields", &self.atom_fields) - .finish() +struct PointerArgs<'a, 'val> { + memory: Vec, + phantom: PhantomData<&'a mut ArgValues<'val>>, +} + +impl Deref for PointerArgs<'_, '_> { + type Target = [Arg]; + + fn deref(&self) -> &Self::Target { + &self.memory } } -struct PointerArgs { - pointers: Vec<*mut c_void>, - _memory: Vec>, +enum ArgValues<'a> { + U8(u8), + I8(i8), + U16(u16), + I16(i16), + U32(u32), + I32(i32), + U64(u64), + I64(i64), + F32(f32), + F64(f64), + Ptr(*mut c_void, PhantomData<&'a CString>), + Struct(Box), +} + +impl<'val> ArgValues<'val> { + fn new( + val: &'val mut Value, + arg_type: &Type, + structs_table: &mut HashMap, + ) -> Result { + match (unsafe { *arg_type.as_raw_ptr() }).type_ as u32 { + libffi::raw::FFI_TYPE_UINT8 => Ok(Self::U8(val.as_int()?)), + libffi::raw::FFI_TYPE_SINT8 => Ok(Self::I8(val.as_int()?)), + libffi::raw::FFI_TYPE_UINT16 => Ok(Self::U16(val.as_int()?)), + libffi::raw::FFI_TYPE_SINT16 => Ok(Self::I16(val.as_int()?)), + libffi::raw::FFI_TYPE_UINT32 => Ok(Self::U32(val.as_int()?)), + libffi::raw::FFI_TYPE_SINT32 => Ok(Self::I32(val.as_int()?)), + libffi::raw::FFI_TYPE_UINT64 => Ok(Self::U64(val.as_int()?)), + libffi::raw::FFI_TYPE_SINT64 => Ok(Self::I64(val.as_int()?)), + libffi::raw::FFI_TYPE_FLOAT => Ok(Self::F32(val.as_float()? as f32)), + libffi::raw::FFI_TYPE_DOUBLE => Ok(Self::F64(val.as_float()?)), + libffi::raw::FFI_TYPE_POINTER => Ok(Self::Ptr(val.as_ptr()?, PhantomData)), + libffi::raw::FFI_TYPE_STRUCT => Ok(Self::Struct( + ForeignFunctionTable::build_struct(val, structs_table)?.0, + )), + _ => Err(FFIError::InvalidFFIType), + } + } } impl ForeignFunctionTable { @@ -85,16 +124,25 @@ impl ForeignFunctionTable { } pub fn define_struct(&mut self, name: &str, atom_fields: Vec) -> Result<(), FFIError> { - let mut fields: Vec<_> = atom_fields + let fields: Vec<_> = atom_fields .iter() .map(|x| self.map_type_ffi(x)) - .collect::>()?; - fields.push(std::ptr::null_mut::()); - let struct_type = ffi_type { - type_: STRUCT, - elements: fields.as_mut_ptr(), - ..Default::default() + .collect::>()?; + let struct_type = libffi::middle::Type::structure(fields.iter().cloned()); + + unsafe { + // ensure that size and alignment of struct_type are set properly + use libffi::low::{ffi_abi_FFI_DEFAULT_ABI, prep_cif}; + prep_cif( + &mut Default::default(), + ffi_abi_FFI_DEFAULT_ABI, + 1, + struct_type.as_raw_ptr(), + [struct_type.as_raw_ptr()].as_mut_ptr(), + ) + .unwrap() }; + self.structs.insert( name.to_string(), StructImpl { @@ -106,24 +154,24 @@ impl ForeignFunctionTable { Ok(()) } - fn map_type_ffi(&mut self, source: &Atom) -> Result<*mut ffi_type, FFIError> { + fn map_type_ffi(&mut self, source: &Atom) -> Result { Ok(match source { - atom!("sint64") => addr_of_mut!(types::sint64), - atom!("sint32") => addr_of_mut!(types::sint32), - atom!("sint16") => addr_of_mut!(types::sint16), - atom!("sint8") => addr_of_mut!(types::sint8), - atom!("uint64") => addr_of_mut!(types::uint64), - atom!("uint32") => addr_of_mut!(types::uint32), - atom!("uint16") => addr_of_mut!(types::uint16), - atom!("uint8") => addr_of_mut!(types::uint8), - atom!("bool") => addr_of_mut!(types::sint8), - atom!("void") => addr_of_mut!(types::void), - atom!("cstr") => addr_of_mut!(types::pointer), - atom!("ptr") => addr_of_mut!(types::pointer), - atom!("f32") => addr_of_mut!(types::float), - atom!("f64") => addr_of_mut!(types::double), + atom!("sint64") => libffi::middle::Type::i64(), + atom!("sint32") => libffi::middle::Type::i32(), + atom!("sint16") => libffi::middle::Type::i16(), + atom!("sint8") => libffi::middle::Type::i8(), + atom!("uint64") => libffi::middle::Type::u64(), + atom!("uint32") => libffi::middle::Type::u32(), + atom!("uint16") => libffi::middle::Type::u16(), + atom!("uint8") => libffi::middle::Type::u8(), + atom!("bool") => libffi::middle::Type::i8(), + atom!("void") => libffi::middle::Type::void(), + atom!("cstr") => libffi::middle::Type::pointer(), + atom!("ptr") => libffi::middle::Type::pointer(), + atom!("f32") => libffi::middle::Type::f32(), + atom!("f64") => libffi::middle::Type::f64(), struct_name => match self.structs.get_mut(&*struct_name.as_str()) { - Some(ref mut struct_type) => &mut struct_type.ffi_type, + Some(ref mut struct_type) => struct_type.ffi_type.clone(), None => return Err(FFIError::InvalidFFIType), }, }) @@ -141,29 +189,21 @@ impl ForeignFunctionTable { let symbol_name: CString = CString::new(function.name.clone())?; let code_ptr: Symbol<*mut c_void> = library.get(&symbol_name.into_bytes_with_nul())?; - let mut args: Vec<_> = function + let args: Vec<_> = function .args .iter() .map(|x| self.map_type_ffi(x)) - .collect::>()?; - let mut cif: ffi_cif = Default::default(); - prep_cif( - &mut cif, - ffi_abi_FFI_DEFAULT_ABI, - args.len(), - self.map_type_ffi(&function.return_value)?, - args.as_mut_ptr(), - ) - .unwrap(); - - let return_struct_name = if (*self.map_type_ffi(&function.return_value)?).type_ - as u32 - == libffi::raw::FFI_TYPE_STRUCT - { - Some(function.return_value.as_str().to_string()) - } else { - None - }; + .collect::>()?; + let result = self.map_type_ffi(&function.return_value)?; + + let cif = libffi::middle::Cif::new(args.clone(), result.clone()); + + let return_struct_name = + if (*result.as_raw_ptr()).type_ as u32 == libffi::raw::FFI_TYPE_STRUCT { + Some(function.return_value.as_str().to_string()) + } else { + None + }; ff_table.table.insert( function.name.clone(), @@ -181,61 +221,33 @@ impl ForeignFunctionTable { Ok(()) } - fn build_pointer_args( - args: &mut [Value], - type_args: &[*mut ffi_type], - structs_table: &mut HashMap, - ) -> Result { - let mut pointers = Vec::with_capacity(args.len()); - let mut _memory = Vec::new(); - for i in 0..args.len() { - let field_type = type_args[i]; - unsafe { - macro_rules! push_int { - ($type:ty) => {{ - let n: $type = args[i].as_int()?; - let mut box_value = Box::new(n) as Box; - pointers.push(&mut *box_value as *mut _ as *mut c_void); - _memory.push(box_value); - }}; - } + fn build_pointer_args<'args, 'val>(args: &[ArgValues<'val>]) -> PointerArgs<'args, 'val> { + let args = args + .iter() + .map(|arg| match arg { + ArgValues::U8(a) => libffi::middle::arg(a), + ArgValues::I8(a) => libffi::middle::arg(a), + ArgValues::U16(a) => libffi::middle::arg(a), + ArgValues::I16(a) => libffi::middle::arg(a), + ArgValues::U32(a) => libffi::middle::arg(a), + ArgValues::I32(a) => libffi::middle::arg(a), + ArgValues::U64(a) => libffi::middle::arg(a), + ArgValues::I64(a) => libffi::middle::arg(a), + ArgValues::F32(a) => libffi::middle::arg(a), + ArgValues::F64(a) => libffi::middle::arg(a), + ArgValues::Ptr(ptr, _) => unsafe { std::mem::transmute::<*mut c_void, Arg>(*ptr) }, + ArgValues::Struct(s) => unsafe { + std::mem::transmute::<*const c_void, Arg>( + s.as_ref() as *const _ as *const c_void + ) + }, + }) + .collect(); - match (*field_type).type_ as u32 { - libffi::raw::FFI_TYPE_UINT8 => push_int!(u8), - libffi::raw::FFI_TYPE_SINT8 => push_int!(i8), - libffi::raw::FFI_TYPE_UINT16 => push_int!(u16), - libffi::raw::FFI_TYPE_SINT16 => push_int!(i16), - libffi::raw::FFI_TYPE_UINT32 => push_int!(u32), - libffi::raw::FFI_TYPE_SINT32 => push_int!(i32), - libffi::raw::FFI_TYPE_UINT64 => push_int!(u64), - libffi::raw::FFI_TYPE_SINT64 => push_int!(i64), - libffi::raw::FFI_TYPE_FLOAT => { - let n: f32 = args[i].as_float()? as f32; - let mut box_value = Box::new(n) as Box; - pointers.push(&mut *box_value as *mut _ as *mut c_void); - _memory.push(box_value); - } - libffi::raw::FFI_TYPE_DOUBLE => { - let n: f64 = args[i].as_float()?; - let mut box_value = Box::new(n) as Box; - pointers.push(&mut *box_value as *mut _ as *mut c_void); - _memory.push(box_value); - } - libffi::raw::FFI_TYPE_POINTER => { - let ptr: *mut c_void = args[i].as_ptr()?; - pointers.push(ptr); - } - libffi::raw::FFI_TYPE_STRUCT => { - let (mut ptr, _size, _align) = - Self::build_struct(&mut args[i], structs_table)?; - pointers.push(&mut *ptr as *mut _ as *mut c_void); - _memory.push(ptr); - } - _ => return Err(FFIError::InvalidFFIType), - } - } + PointerArgs { + memory: args, + phantom: PhantomData, } - Ok(PointerArgs { pointers, _memory }) } fn build_struct( @@ -245,13 +257,11 @@ impl ForeignFunctionTable { match arg { Value::Struct(ref name, ref mut struct_args) => { if let Some(ref mut struct_type) = structs_table.clone().get_mut(name) { - let layout = Layout::from_size_align( - struct_type.ffi_type.size, - struct_type.ffi_type.alignment.into(), - ) - .unwrap(); - let align = struct_type.ffi_type.alignment as usize; - let size = struct_type.ffi_type.size; + let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() }; + let layout = + Layout::from_size_align(ffi_type.size, ffi_type.alignment.into()).unwrap(); + let align = ffi_type.alignment as usize; + let size = ffi_type.size; let ptr = unsafe { alloc::alloc(layout) as *mut c_void }; if ptr.is_null() { @@ -280,9 +290,9 @@ impl ForeignFunctionTable { }}; } - let field = struct_type.fields[i]; + let field = &struct_type.fields[i]; unsafe { - match (*field).type_ as u32 { + match (*field.as_raw_ptr()).type_ as u32 { libffi::raw::FFI_TYPE_UINT8 => try_write_int!(u8), libffi::raw::FFI_TYPE_SINT8 => try_write_int!(i8), libffi::raw::FFI_TYPE_UINT16 => try_write_int!(u16), @@ -336,93 +346,96 @@ impl ForeignFunctionTable { arena: &mut Arena, ) -> Result { let function_impl = self.table.get_mut(name).ok_or(FFIError::FunctionNotFound)?; - let mut pointer_args = - Self::build_pointer_args(&mut args, &function_impl.args, &mut self.structs)?; + if function_impl.args.len() != args.len() { + return Err(FFIError::ArgCountMismatch); + } - unsafe { - macro_rules! call_and_return { - ($type:ty) => {{ - let mut n: $type = 0; - libffi::raw::ffi_call( - &mut function_impl.cif, - Some(*function_impl.code_ptr.as_safe_fun()), - &mut n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr() as *mut *mut c_void, - ); - Ok(Value::Number(fixnum!(Number, n, arena))) - }}; - } + let args = args + .iter_mut() + .zip(function_impl.args.iter()) + .map(|(arg, arg_type)| ArgValues::new(arg, arg_type, &mut self.structs)) + .collect::, _>>()?; + + let args = Self::build_pointer_args(&args); + + macro_rules! call_and_return_int { + ($type:ty) => {{ + let n = function_impl + .cif + .call::<$type>(function_impl.code_ptr, &args); + Ok(Value::Number(fixnum!(Number, n, arena))) + }}; + } - match (*function_impl.cif.rtype).type_ as u32 { - libffi::raw::FFI_TYPE_VOID => call_and_return!(i32), - libffi::raw::FFI_TYPE_UINT8 => call_and_return!(u8), - libffi::raw::FFI_TYPE_SINT8 => call_and_return!(i8), - libffi::raw::FFI_TYPE_UINT16 => call_and_return!(u16), - libffi::raw::FFI_TYPE_SINT16 => call_and_return!(i16), - libffi::raw::FFI_TYPE_UINT32 => call_and_return!(u32), - libffi::raw::FFI_TYPE_SINT32 => call_and_return!(i32), - libffi::raw::FFI_TYPE_UINT64 => call_and_return!(u64), - libffi::raw::FFI_TYPE_SINT64 => call_and_return!(i64), - libffi::raw::FFI_TYPE_POINTER => { - let mut n: *mut c_void = std::ptr::null_mut(); - libffi::raw::ffi_call( - &mut function_impl.cif, - Some(*function_impl.code_ptr.as_safe_fun()), - &mut n as *mut *mut c_void as *mut c_void, - pointer_args.pointers.as_mut_ptr(), - ); - Ok(Value::Number(fixnum!(Number, n as isize, arena))) - } - libffi::raw::FFI_TYPE_FLOAT => { - let mut n: f32 = 0.0; - libffi::raw::ffi_call( - &mut function_impl.cif, - Some(*function_impl.code_ptr.as_safe_fun()), - &mut n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr(), - ); - Ok(Value::Number(Number::Float(OrderedFloat(n.into())))) - } - libffi::raw::FFI_TYPE_DOUBLE => { - let mut n: f64 = 0.0; - libffi::raw::ffi_call( - &mut function_impl.cif, - Some(*function_impl.code_ptr.as_safe_fun()), - &mut n as *mut _ as *mut c_void, - pointer_args.pointers.as_mut_ptr(), - ); - Ok(Value::Number(Number::Float(OrderedFloat(n)))) + macro_rules! call_and_return_float { + ($type:ty) => {{ + let n = function_impl + .cif + .call::<$type>(function_impl.code_ptr, &args); + Ok(Value::Number(Number::Float(OrderedFloat(f64::from(n))))) + }}; + } + + let ffi_rtype = unsafe { *(*function_impl.cif.as_raw_ptr()).rtype }; + + match ffi_rtype.type_ as u32 { + libffi::raw::FFI_TYPE_VOID => { + unsafe { + function_impl + .cif + .call::(function_impl.code_ptr, &args) + }; + Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0)))) + } + libffi::raw::FFI_TYPE_UINT8 => unsafe { call_and_return_int!(u8) }, + libffi::raw::FFI_TYPE_SINT8 => unsafe { call_and_return_int!(i8) }, + libffi::raw::FFI_TYPE_UINT16 => unsafe { call_and_return_int!(u16) }, + libffi::raw::FFI_TYPE_SINT16 => unsafe { call_and_return_int!(i16) }, + libffi::raw::FFI_TYPE_UINT32 => unsafe { call_and_return_int!(u32) }, + libffi::raw::FFI_TYPE_SINT32 => unsafe { call_and_return_int!(i32) }, + libffi::raw::FFI_TYPE_UINT64 => unsafe { call_and_return_int!(u64) }, + libffi::raw::FFI_TYPE_SINT64 => unsafe { call_and_return_int!(i64) }, + libffi::raw::FFI_TYPE_POINTER => { + let ptr = unsafe { + function_impl + .cif + .call::<*mut c_void>(function_impl.code_ptr, &args) + }; + Ok(Value::Number(fixnum!(Number, ptr as isize, arena))) + } + libffi::raw::FFI_TYPE_FLOAT => unsafe { call_and_return_float!(f32) }, + libffi::raw::FFI_TYPE_DOUBLE => unsafe { call_and_return_float!(f64) }, + libffi::raw::FFI_TYPE_STRUCT => { + let name = &function_impl + .return_struct_name + .clone() + .ok_or(FFIError::StructNotFound)?; + let struct_type = self.structs.get(name).ok_or(FFIError::StructNotFound)?; + let ffi_type = unsafe { *struct_type.ffi_type.as_raw_ptr() }; + let layout = + Layout::from_size_align(ffi_type.size, ffi_type.alignment.into()).unwrap(); + let ptr = unsafe { alloc::alloc(layout) }; + + if ptr.is_null() { + return Err(FFIError::AllocationFailed); } - libffi::raw::FFI_TYPE_STRUCT => { - let name = &function_impl - .return_struct_name - .clone() - .ok_or(FFIError::StructNotFound)?; - let struct_type = self.structs.get(name).ok_or(FFIError::StructNotFound)?; - let layout = Layout::from_size_align( - struct_type.ffi_type.size, - struct_type.ffi_type.alignment.into(), - ) - .unwrap(); - let ptr = alloc::alloc(layout) as *mut c_void; - if ptr.is_null() { - panic!("allocation failed") - } + let ptr_args: &[Arg] = &args; + unsafe { libffi::raw::ffi_call( - &mut function_impl.cif, + function_impl.cif.as_raw_ptr(), Some(*function_impl.code_ptr.as_safe_fun()), - &mut *ptr as *mut _, - pointer_args.pointers.as_mut_ptr(), - ); - let struct_val = self.read_struct(ptr, name, struct_type, arena); - #[allow(clippy::from_raw_with_void_ptr)] - drop(Box::from_raw(ptr)); - struct_val - } - _ => unreachable!(), + ptr as *mut c_void, + ptr_args.as_ptr() as *mut *mut c_void, + ) + }; + let struct_val = self.read_struct(ptr as *mut c_void, name, struct_type, arena); + + unsafe { alloc::dealloc(ptr, layout) }; + struct_val } + _ => unreachable!(), } } @@ -437,9 +450,7 @@ impl ForeignFunctionTable { let mut returns = Vec::new(); let mut field_ptr = ptr; - for i in 0..(struct_type.fields.len() - 1) { - let field = struct_type.fields[i]; - + for (field, type_name) in struct_type.fields.iter().zip(&struct_type.atom_fields) { macro_rules! read_and_push_int { ($type:ty) => {{ field_ptr = @@ -450,7 +461,7 @@ impl ForeignFunctionTable { }}; } - match (*field).type_ as u32 { + match (*field.as_raw_ptr()).type_ as u32 { libffi::raw::FFI_TYPE_UINT8 => read_and_push_int!(u8), libffi::raw::FFI_TYPE_SINT8 => read_and_push_int!(i8), libffi::raw::FFI_TYPE_UINT16 => read_and_push_int!(u16), @@ -475,17 +486,18 @@ impl ForeignFunctionTable { field_ptr = field_ptr.add(std::mem::size_of::()); } libffi::raw::FFI_TYPE_STRUCT => { - let substruct = struct_type.atom_fields[i].as_str(); + let substruct = type_name.as_str(); let struct_type = self .structs .get(&*substruct) .ok_or(FFIError::StructNotFound)?; - field_ptr = field_ptr - .add(field_ptr.align_offset(struct_type.ffi_type.alignment as usize)); + let ffi_type = *struct_type.ffi_type.as_raw_ptr(); + field_ptr = + field_ptr.add(field_ptr.align_offset(ffi_type.alignment as usize)); let struct_val = self.read_struct(field_ptr, &substruct, struct_type, arena); returns.push(struct_val?); - field_ptr = field_ptr.add(struct_type.ffi_type.size); + field_ptr = field_ptr.add(ffi_type.size); } _ => { unreachable!() @@ -549,6 +561,8 @@ pub enum FFIError { InvalidStructName, FunctionNotFound, StructNotFound, + ArgCountMismatch, + AllocationFailed, } impl std::fmt::Display for FFIError { diff --git a/src/machine/machine_errors.rs b/src/machine/machine_errors.rs index 4551991e..cbeb617e 100644 --- a/src/machine/machine_errors.rs +++ b/src/machine/machine_errors.rs @@ -599,6 +599,8 @@ impl MachineState { FFIError::InvalidStructName => atom!("invalid_struct_name"), FFIError::FunctionNotFound => atom!("function_not_found"), FFIError::StructNotFound => atom!("struct_not_found"), + FFIError::ArgCountMismatch => atom!("mismatched_argument_count"), + FFIError::AllocationFailed => atom!("allocation_failed"), }; let stub = functor!(atom!("ffi_error"), [atom_as_cell(error_atom)]);