Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug relating to type conversion in function calls #1602

Merged
merged 3 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## **[Unreleased]**
- [#1602](https://github.com/wasmerio/wasmer/pull/1602) Fix panic when calling host functions with negative numbers in certain situations
MarkMcCaskey marked this conversation as resolved.
Show resolved Hide resolved
- [#1590](https://github.com/wasmerio/wasmer/pull/1590) Fix soundness issue in API of vm::Global
- [#1566](https://github.com/wasmerio/wasmer/pull/1566) Add support for opening special Unix files to the WASI FS

Expand Down
37 changes: 26 additions & 11 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,35 @@ mod inner {
};
}

macro_rules! from_to_native_wasm_type_same_size {
( $( $type:ty => $native_type:ty ),* ) => {
$(
#[allow(clippy::use_self)]
unsafe impl FromToNativeWasmType for $type {
type Native = $native_type;

#[inline]
fn from_native(native: Self::Native) -> Self {
Self::from_ne_bytes(Self::Native::to_ne_bytes(native))
}

#[inline]
fn to_native(self) -> Self::Native {
Self::Native::from_ne_bytes(Self::to_ne_bytes(self))
}
}
)*
};
}

from_to_native_wasm_type!(
i8 => i32,
u8 => i32,
i16 => i32,
u16 => i32,
u16 => i32
);

from_to_native_wasm_type_same_size!(
MarkMcCaskey marked this conversation as resolved.
Show resolved Hide resolved
i32 => i32,
u32 => i32,
i64 => i64,
Expand All @@ -641,16 +665,7 @@ mod inner {
#[test]
fn test_to_native() {
assert_eq!(7i8.to_native(), 7i32);
}

#[test]
#[should_panic(
expected = "out of range type conversion attempt (tried to convert `u32` to `i32`)"
)]
fn test_to_native_panics() {
use std::{i32, u32};

assert_eq!(u32::MAX.to_native(), i32::MAX);
assert_eq!(u32::MAX.to_native(), -1);
}
}

Expand Down
54 changes: 54 additions & 0 deletions lib/api/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,57 @@ fn exports() -> Result<()> {
);
Ok(())
}

#[test]
fn calling_host_functions_with_negative_values_works() -> Result<()> {
let store = Store::default();
let wat = r#"(module
(import "host" "host_func1" (func (param i64)))
(import "host" "host_func2" (func (param i32)))
(import "host" "host_func3" (func (param i64)))
(import "host" "host_func4" (func (param i32)))

(func (export "call_host_func1")
(call 0 (i64.const -1)))
(func (export "call_host_func2")
(call 1 (i32.const -1)))
(func (export "call_host_func3")
(call 2 (i64.const -1)))
(func (export "call_host_func4")
(call 3 (i32.const -1)))
)"#;
let module = Module::new(&store, wat)?;
let imports = imports! {
"host" => {
"host_func1" => Function::new_native(&store, |p: u64| {
println!("host_func1: Found number {}", p);
assert_eq!(p, u64::max_value());
}),
"host_func2" => Function::new_native(&store, |p: u32| {
println!("host_func2: Found number {}", p);
assert_eq!(p, u32::max_value());
}),
"host_func3" => Function::new_native(&store, |p: i64| {
println!("host_func3: Found number {}", p);
assert_eq!(p, -1);
}),
"host_func4" => Function::new_native(&store, |p: i32| {
println!("host_func4: Found number {}", p);
assert_eq!(p, -1);
}),
}
};
let instance = Instance::new(&module, &imports)?;

let f1: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func1")?;
let f2: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func2")?;
let f3: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func3")?;
let f4: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func4")?;

f1.call()?;
f2.call()?;
f3.call()?;
f4.call()?;

Ok(())
}