summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2021-08-23 18:31:19 +0200
committerGitHub <noreply@github.com>2021-08-23 18:31:19 +0200
commitdf084b9f14e06f866f18206e8051c4721702fb44 (patch)
tree2b544f7106f8970b995fda53ba558bd20d2be414
parent2187c11e5d026733b4df1d675cfe5922302c8f4b (diff)
fix(ext/ffi): don't panic on invalid enum values (#11815)
Co-authored-by: Feng Yu <f3n67u@gmail.com>
-rw-r--r--Cargo.lock4
-rw-r--r--cli/tests/unit/ffi_test.ts25
-rw-r--r--core/Cargo.toml2
-rw-r--r--ext/ffi/lib.rs40
4 files changed, 38 insertions, 33 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d775bb0bf..40c405d80 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
diff --git a/cli/tests/unit/ffi_test.ts b/cli/tests/unit/ffi_test.ts
new file mode 100644
index 000000000..ab25df699
--- /dev/null
+++ b/cli/tests/unit/ffi_test.ts
@@ -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);
+});
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 7ddb7d814..78fbf1836 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -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]]
diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs
index cfff59953..673f83472 100644
--- a/ext/ffi/lib.rs
+++ b/ext/ffi/lib.rs
@@ -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)]