Skip to content

Commit

Permalink
fix(ext/ffi): disallow empty ffi structs (denoland#17487)
Browse files Browse the repository at this point in the history
This patch makes `NativeType` to `libffi::middle::Type` conversion
failliable and w.t disallows struct with empty fields. libffi does not
handle "empty" struct because they don't exist in C (or Rust).

Fixes denoland#17481
  • Loading branch information
littledivy authored Jan 21, 2023
1 parent 638b6ef commit 5928925
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
4 changes: 2 additions & 2 deletions ext/ffi/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ where
permissions.check(None)?;
};

let symbol = PtrSymbol::new(pointer, &def);
let symbol = PtrSymbol::new(pointer, &def)?;
let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;
let def_result = def.result.clone();

Expand Down Expand Up @@ -379,7 +379,7 @@ where
permissions.check(None)?;
};

let symbol = PtrSymbol::new(pointer, &def);
let symbol = PtrSymbol::new(pointer, &def)?;
let call_args = ffi_parse_args(scope, parameters, &def.parameters)?;

let out_buffer = out_buffer
Expand Down
17 changes: 11 additions & 6 deletions ext/ffi/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ pub struct PtrSymbol {
}

impl PtrSymbol {
pub fn new(fn_ptr: usize, def: &ForeignFunction) -> Self {
pub fn new(fn_ptr: usize, def: &ForeignFunction) -> Result<Self, AnyError> {
let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _);
let cif = libffi::middle::Cif::new(
def
.parameters
.clone()
.into_iter()
.map(libffi::middle::Type::from),
def.result.clone().into(),
.map(libffi::middle::Type::try_from)
.collect::<Result<Vec<_>, _>>()?,
def.result.clone().try_into()?,
);

Self { cif, ptr }
Ok(Self { cif, ptr })
}
}

Expand Down Expand Up @@ -578,8 +579,12 @@ where
waker: None,
}));
let cif = Cif::new(
args.parameters.into_iter().map(libffi::middle::Type::from),
libffi::middle::Type::from(args.result),
args
.parameters
.into_iter()
.map(libffi::middle::Type::try_from)
.collect::<Result<Vec<_>, _>>()?,
libffi::middle::Type::try_from(args.result)?,
);

// SAFETY: CallbackInfo is leaked, is not null and stays valid as long as the callback exists.
Expand Down
5 changes: 3 additions & 2 deletions ext/ffi/dlfcn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ where
.parameters
.clone()
.into_iter()
.map(libffi::middle::Type::from),
foreign_fn.result.clone().into(),
.map(libffi::middle::Type::try_from)
.collect::<Result<Vec<_>, _>>()?,
foreign_fn.result.clone().try_into()?,
);

let func_key = v8::String::new(scope, &symbol_key).unwrap();
Expand Down
27 changes: 20 additions & 7 deletions ext/ffi/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use deno_core::error::type_error;
use deno_core::error::AnyError;

/// Defines the accepted types that can be used as
/// parameters and return values in FFI.
#[derive(Clone, Debug, serde::Deserialize, Eq, PartialEq)]
Expand All @@ -25,9 +28,11 @@ pub enum NativeType {
Struct(Box<[NativeType]>),
}

impl From<NativeType> for libffi::middle::Type {
fn from(native_type: NativeType) -> Self {
match native_type {
impl TryFrom<NativeType> for libffi::middle::Type {
type Error = AnyError;

fn try_from(native_type: NativeType) -> Result<Self, Self::Error> {
Ok(match native_type {
NativeType::Void => libffi::middle::Type::void(),
NativeType::U8 | NativeType::Bool => libffi::middle::Type::u8(),
NativeType::I8 => libffi::middle::Type::i8(),
Expand All @@ -44,10 +49,18 @@ impl From<NativeType> for libffi::middle::Type {
NativeType::Pointer | NativeType::Buffer | NativeType::Function => {
libffi::middle::Type::pointer()
}
NativeType::Struct(fields) => libffi::middle::Type::structure(
fields.iter().map(|field| field.clone().into()),
),
}
NativeType::Struct(fields) => {
libffi::middle::Type::structure(match fields.len() > 0 {
true => fields
.iter()
.map(|field| field.clone().try_into())
.collect::<Result<Vec<_>, _>>()?,
false => {
return Err(type_error("Struct must have at least one field"))
}
})
}
})
}
}

Expand Down
36 changes: 35 additions & 1 deletion test_ffi/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ assertThrows(
"Failed to register symbol non_existent_symbol",
);

assertThrows(() => {
Deno.dlopen(libPath, {
print_something: {
parameters: [],
result: { struct: [] }
},
}),
TypeError,
"Struct must have at least one field"
});

assertThrows(() => {
Deno.dlopen(libPath, {
print_something: {
parameters: [ { struct: [] } ],
result: "void",
},
}),
TypeError,
"Struct must have at least one field"
});

const Empty = { struct: [] }
assertThrows(() => {
Deno.dlopen(libPath, {
print_something: {
parameters: [ { struct: [Empty] } ],
result: "void",
},
}),
TypeError,
"Struct must have at least one field"
});

const Point = ["f64", "f64"];
const Size = ["f64", "f64"];
const Rect = ["f64", "f64", "f64", "f64"];
Expand Down Expand Up @@ -720,4 +754,4 @@ function testOptimized(fn, callback) {
console.log(r2);
}
assertIsOptimized(fn);
}
}

0 comments on commit 5928925

Please sign in to comment.