1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/ffi): don't panic on invalid enum values (#11815)

Co-authored-by: Feng Yu <f3n67u@gmail.com>
This commit is contained in:
Luca Casonato 2021-08-23 18:31:19 +02:00 committed by GitHub
parent 2187c11e5d
commit df084b9f14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 33 deletions

4
Cargo.lock generated
View file

@ -3280,9 +3280,9 @@ dependencies = [
[[package]]
name = "serde_v8"
version = "0.9.0"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88bf7bf03d60f6c5098d2d1867404ff50695435f638c34e4a95b8b3631cb4900"
checksum = "f5ce7662cda194ff443bddf146c952e83075889590838cd41df768fba7d152d0"
dependencies = [
"rusty_v8",
"serde",

View file

@ -0,0 +1,25 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertThrows, unitTest } from "./test_util.ts";
unitTest(function dlopenInvalidArguments() {
const filename = "/usr/lib/libc.so.6";
assertThrows(() => {
// @ts-expect-error: ForeignFunction cannot be null
Deno.dlopen(filename, { malloc: null });
}, TypeError);
assertThrows(() => {
Deno.dlopen(filename, {
// @ts-expect-error: invalid NativeType
malloc: { parameters: ["a"], result: "b" },
});
}, TypeError);
assertThrows(() => {
// @ts-expect-error: DynamicLibrary symbols cannot be null
Deno.dlopen(filename, null);
}, TypeError);
assertThrows(() => {
// @ts-expect-error: require 2 arguments
Deno.dlopen(filename);
}, TypeError);
});

View file

@ -25,7 +25,7 @@ pin-project = "1.0.7"
rusty_v8 = "0.26.0"
serde = { version = "1.0.126", features = ["derive"] }
serde_json = { version = "1.0.64", features = ["preserve_order"] }
serde_v8 = { version = "0.9.0" }
serde_v8 = { version = "0.9.3" }
url = { version = "2.2.2", features = ["serde"] }
[[example]]

View file

@ -75,12 +75,13 @@ impl DynamicLibraryResource {
) -> Result<(), AnyError> {
let fn_ptr = unsafe { self.lib.symbol::<*const c_void>(&symbol) }?;
let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _);
let parameter_types =
foreign_fn.parameters.into_iter().map(NativeType::from);
let result_type = NativeType::from(foreign_fn.result);
let cif = libffi::middle::Cif::new(
parameter_types.clone().map(libffi::middle::Type::from),
result_type.into(),
foreign_fn
.parameters
.clone()
.into_iter()
.map(libffi::middle::Type::from),
foreign_fn.result.into(),
);
self.symbols.insert(
@ -88,8 +89,8 @@ impl DynamicLibraryResource {
Symbol {
cif,
ptr,
parameter_types: parameter_types.collect(),
result_type,
parameter_types: foreign_fn.parameters,
result_type: foreign_fn.result,
},
);
@ -153,27 +154,6 @@ impl From<NativeType> for libffi::middle::Type {
}
}
impl From<String> for NativeType {
fn from(string: String) -> Self {
match string.as_str() {
"void" => NativeType::Void,
"u8" => NativeType::U8,
"i8" => NativeType::I8,
"u16" => NativeType::U16,
"i16" => NativeType::I16,
"u32" => NativeType::U32,
"i32" => NativeType::I32,
"u64" => NativeType::U64,
"i64" => NativeType::I64,
"usize" => NativeType::USize,
"isize" => NativeType::ISize,
"f32" => NativeType::F32,
"f64" => NativeType::F64,
_ => unimplemented!(),
}
}
}
#[repr(C)]
union NativeValue {
void_value: (),
@ -279,8 +259,8 @@ fn value_as_f64(value: Value) -> f64 {
#[derive(Deserialize, Debug)]
struct ForeignFunction {
parameters: Vec<String>,
result: String,
parameters: Vec<NativeType>,
result: NativeType,
}
#[derive(Deserialize, Debug)]