Fix tests for the Cranelift backend

This commit is contained in:
Mark McCaskey 2020-04-24 14:30:14 -07:00
parent b9ec8f9845
commit 9723270f96
18 changed files with 69 additions and 46 deletions

View File

@ -50,8 +50,8 @@ typedef struct {
} callbacks_t; } callbacks_t;
typedef struct { typedef struct {
size_t data, vtable; size_t data;
} box_any_t; } runtime_error_t;
enum WasmTrapType { enum WasmTrapType {
Unreachable = 0, Unreachable = 0,
@ -121,7 +121,7 @@ private:
struct WasmErrorSink { struct WasmErrorSink {
WasmTrapType *trap_out; WasmTrapType *trap_out;
box_any_t *user_error; runtime_error_t *user_error;
}; };
struct WasmException : std::exception { struct WasmException : std::exception {
@ -149,14 +149,14 @@ public:
struct UserException : UncatchableException { struct UserException : UncatchableException {
public: public:
UserException(size_t data, size_t vtable) : error_data({data, vtable}) {} UserException(size_t data) : error_data({data}) {}
virtual std::string description() const noexcept override { virtual std::string description() const noexcept override {
return "user exception"; return "user exception";
} }
// The parts of a `Box<dyn Any>`. // The pointer to `Option<RuntimeError>`.
box_any_t error_data; runtime_error_t error_data;
virtual void write_error(WasmErrorSink &out) const noexcept override { virtual void write_error(WasmErrorSink &out) const noexcept override {
*out.user_error = error_data; *out.user_error = error_data;
@ -274,10 +274,10 @@ result_t module_load(const uint8_t *mem_ptr, size_t mem_size,
void module_delete(WasmModule *module) { delete module; } void module_delete(WasmModule *module) { delete module; }
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust // Throw a pointer that's assumed to be `*mut RuntimeError` on the rust
// side. // side.
[[noreturn]] void throw_any(size_t data, size_t vtable) { [[noreturn]] void throw_runtime_error(size_t data) {
unsafe_unwind(new UserException(data, vtable)); unsafe_unwind(new UserException(data));
} }
// Throw a pointer that's assumed to be codegen::BreakpointHandler on the // Throw a pointer that's assumed to be codegen::BreakpointHandler on the
@ -288,7 +288,7 @@ void module_delete(WasmModule *module) { delete module; }
bool cxx_invoke_trampoline(trampoline_t trampoline, void *ctx, void *func, bool cxx_invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
void *params, void *results, WasmTrapType *trap_out, void *params, void *results, WasmTrapType *trap_out,
box_any_t *user_error, void *invoke_env) noexcept { runtime_error_t *user_error, void *invoke_env) noexcept {
try { try {
catch_unwind([trampoline, ctx, func, params, results]() { catch_unwind([trampoline, ctx, func, params, results]() {
trampoline(ctx, func, params, results); trampoline(ctx, func, params, results);

View File

@ -56,7 +56,7 @@ extern "C" {
/// but this is cleaner, I think? /// but this is cleaner, I think?
#[cfg_attr(nightly, unwind(allowed))] #[cfg_attr(nightly, unwind(allowed))]
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
fn throw_runtime_error(data: RuntimeError) -> !; fn throw_runtime_error(data: *mut Option<RuntimeError>) -> !;
#[allow(improper_ctypes)] #[allow(improper_ctypes)]
fn cxx_invoke_trampoline( fn cxx_invoke_trampoline(
@ -475,8 +475,7 @@ impl RunnableModule for LLVMBackend {
} }
unsafe fn do_early_trap(&self, data: RuntimeError) -> ! { unsafe fn do_early_trap(&self, data: RuntimeError) -> ! {
// maybe need to box leak it? throw_runtime_error(Box::into_raw(Box::new(Some(data))))
throw_runtime_error(data)
} }
} }

View File

@ -35,6 +35,7 @@ use wasmer_runtime_core::{
backend::{CacheGen, CompilerConfig, Token}, backend::{CacheGen, CompilerConfig, Token},
cache::{Artifact, Error as CacheError}, cache::{Artifact, Error as CacheError},
codegen::*, codegen::*,
error::RuntimeError,
memory::MemoryType, memory::MemoryType,
module::{ModuleInfo, ModuleInner}, module::{ModuleInfo, ModuleInner},
parse::{wp_type_to_type, LoadError}, parse::{wp_type_to_type, LoadError},
@ -940,12 +941,11 @@ pub struct CodegenError {
// prevents unused function elimination. // prevents unused function elimination.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn callback_trampoline( pub unsafe extern "C" fn callback_trampoline(
b: *mut Option<Box<dyn std::any::Any>>, b: *mut Option<RuntimeError>,
callback: *mut BreakpointHandler, callback: *mut BreakpointHandler,
) { ) {
let callback = Box::from_raw(callback); let callback = Box::from_raw(callback);
let result: Result<(), Box<dyn std::any::Any + Send>> = let result: Result<(), RuntimeError> = callback(BreakpointInfo { fault: None });
callback(BreakpointInfo { fault: None });
match result { match result {
Ok(()) => *b = None, Ok(()) => *b = None,
Err(e) => *b = Some(e), Err(e) => *b = Some(e),

View File

@ -97,7 +97,9 @@ impl FunctionMiddleware for Metering {
ty: WpTypeOrFuncType::Type(WpType::EmptyBlockType), ty: WpTypeOrFuncType::Type(WpType::EmptyBlockType),
})); }));
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(|_| { sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(|_| {
Err(RuntimeError::Metering(Box::new(ExecutionLimitExceededError))) Err(RuntimeError::Metering(Box::new(
ExecutionLimitExceededError,
)))
})))); }))));
sink.push(Event::WasmOwned(Operator::End)); sink.push(Event::WasmOwned(Operator::End));
} }

View File

@ -21,6 +21,7 @@ use wasmer::import::{ImportObject, ImportObjectIterator};
use wasmer::vm::Ctx; use wasmer::vm::Ctx;
use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type}; use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type};
use wasmer_runtime_core::{ use wasmer_runtime_core::{
error::RuntimeError,
export::{Context, FuncPointer}, export::{Context, FuncPointer},
module::ImportName, module::ImportName,
}; };
@ -723,7 +724,7 @@ pub unsafe extern "C" fn wasmer_trap(
(&*ctx.module) (&*ctx.module)
.runnable_module .runnable_module
.do_early_trap(Box::new(error_message)); // never returns .do_early_trap(RuntimeError::User(Box::new(error_message))); // never returns
// cbindgen does not generate a binding for a function that // cbindgen does not generate a binding for a function that
// returns `!`. Since we also need to error in some cases, the // returns `!`. Since we also need to error in some cases, the

View File

@ -69,7 +69,7 @@ int main()
wasmer_last_error_message(error_str, error_len); wasmer_last_error_message(error_str, error_len);
printf("Error str: `%s`\n", error_str); printf("Error str: `%s`\n", error_str);
assert(0 == strcmp(error_str, "Call error: \"Hello\"")); assert(0 == strcmp(error_str, "Call error: User supplied error: \"Hello\""));
printf("Destroying func\n"); printf("Destroying func\n");
wasmer_import_func_destroy(func); wasmer_import_func_destroy(func);

View File

@ -924,7 +924,7 @@ void *wasmer_instance_context_data_get(const wasmer_instance_context_t *ctx);
* // Allocate them and set them on the given instance. * // Allocate them and set them on the given instance.
* my_data *data = malloc(sizeof(my_data)); * my_data *data = malloc(sizeof(my_data));
* data-> = ; * data-> = ;
* wasmer_instance_context_data_set(instance, (void*) my_data); * wasmer_instance_context_data_set(instance, (void*) data);
* *
* // You can read your data. * // You can read your data.
* { * {

View File

@ -736,7 +736,7 @@ void *wasmer_instance_context_data_get(const wasmer_instance_context_t *ctx);
/// // Allocate them and set them on the given instance. /// // Allocate them and set them on the given instance.
/// my_data *data = malloc(sizeof(my_data)); /// my_data *data = malloc(sizeof(my_data));
/// data->… = …; /// data->… = …;
/// wasmer_instance_context_data_set(instance, (void*) my_data); /// wasmer_instance_context_data_set(instance, (void*) data);
/// ///
/// // You can read your data. /// // You can read your data.
/// { /// {

View File

@ -265,10 +265,25 @@ pub enum InvokeError {
/// extra TODO: investigate if this can be a `Box<InvokeError>` instead (looks like probably no) /// extra TODO: investigate if this can be a `Box<InvokeError>` instead (looks like probably no)
/// TODO: /// TODO:
EarlyTrap(Box<RuntimeError>), EarlyTrap(Box<RuntimeError>),
/// Indicates an error that ocurred related to breakpoints. (currently Singlepass only) /// Indicates an error that ocurred related to breakpoints.
Breakpoint(Box<RuntimeError>), Breakpoint(Box<RuntimeError>),
} }
impl From<InvokeError> for RuntimeError {
fn from(other: InvokeError) -> RuntimeError {
match other {
InvokeError::EarlyTrap(re) | InvokeError::Breakpoint(re) => *re,
_ => RuntimeError::InvokeError(other),
}
}
}
impl PartialEq for RuntimeError {
fn eq(&self, _other: &RuntimeError) -> bool {
false
}
}
//impl std::error::Error for InvokeError {} //impl std::error::Error for InvokeError {}
impl std::error::Error for RuntimeError {} impl std::error::Error for RuntimeError {}

View File

@ -28,9 +28,9 @@ pub mod raw {
} }
use crate::codegen::{BreakpointInfo, BreakpointMap}; use crate::codegen::{BreakpointInfo, BreakpointMap};
use crate::error::{InvokeError, RuntimeError};
use crate::state::x64::{build_instance_image, read_stack, X64Register, GPR}; use crate::state::x64::{build_instance_image, read_stack, X64Register, GPR};
use crate::state::{CodeVersion, ExecutionStateImage, InstanceImage}; use crate::state::{CodeVersion, ExecutionStateImage, InstanceImage};
use crate::error::{RuntimeError, InvokeError};
use crate::vm; use crate::vm;
use libc::{mmap, mprotect, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE}; use libc::{mmap, mprotect, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE};
use nix::sys::signal::{ use nix::sys::signal::{
@ -344,11 +344,13 @@ extern "C" fn signal_trap_handler(
// breakpoint // breakpoint
let out: Option<Result<(), RuntimeError>> = let out: Option<Result<(), RuntimeError>> =
with_breakpoint_map(|bkpt_map| -> Option<Result<(), RuntimeError>> { with_breakpoint_map(|bkpt_map| -> Option<Result<(), RuntimeError>> {
bkpt_map.and_then(|x| x.get(&(fault.ip.get()))).map(|x| -> Result<(), RuntimeError> { bkpt_map.and_then(|x| x.get(&(fault.ip.get()))).map(
|x| -> Result<(), RuntimeError> {
x(BreakpointInfo { x(BreakpointInfo {
fault: Some(&fault), fault: Some(&fault),
}) })
}) },
)
}); });
match out { match out {
Some(Ok(())) => { Some(Ok(())) => {

View File

@ -603,7 +603,7 @@ pub(crate) fn call_func_with_index_inner(
Ok(()) Ok(())
} else { } else {
let error: RuntimeError = error_out let error: RuntimeError = error_out
.map(RuntimeError::InvokeError) .map(Into::into)
.unwrap_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError)); .unwrap_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError));
Err(error.into()) Err(error.into())
} }

View File

@ -699,13 +699,13 @@ pub mod x64 {
pub use super::x64_decl::*; pub use super::x64_decl::*;
use super::*; use super::*;
use crate::codegen::BreakpointMap; use crate::codegen::BreakpointMap;
use crate::error::RuntimeError;
use crate::fault::{ use crate::fault::{
catch_unsafe_unwind, get_boundary_register_preservation, run_on_alternative_stack, catch_unsafe_unwind, get_boundary_register_preservation, run_on_alternative_stack,
}; };
use crate::structures::TypedIndex; use crate::structures::TypedIndex;
use crate::types::LocalGlobalIndex; use crate::types::LocalGlobalIndex;
use crate::vm::Ctx; use crate::vm::Ctx;
use crate::error::RuntimeError;
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
unsafe fn compute_vmctx_deref(vmctx: *const Ctx, seq: &[usize]) -> u64 { unsafe fn compute_vmctx_deref(vmctx: *const Ctx, seq: &[usize]) -> u64 {

View File

@ -590,7 +590,7 @@ macro_rules! impl_traits {
) { ) {
Ok(Rets::from_ret_array(rets)) Ok(Rets::from_ret_array(rets))
} else { } else {
Err(error_out.map_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError), RuntimeError::InvokeError)) Err(error_out.map_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError), Into::into))
} }
} }
} }

View File

@ -26,9 +26,9 @@ use wasmer_runtime_core::{
}, },
cache::{Artifact, Error as CacheError}, cache::{Artifact, Error as CacheError},
codegen::*, codegen::*,
error::{InvokeError, RuntimeError},
fault::{self, raw::register_preservation_trampoline}, fault::{self, raw::register_preservation_trampoline},
loader::CodeMemory, loader::CodeMemory,
error::{InvokeError, RuntimeError},
memory::MemoryType, memory::MemoryType,
module::{ModuleInfo, ModuleInner}, module::{ModuleInfo, ModuleInner},
state::{ state::{

View File

@ -20,8 +20,8 @@ use wasmer_runtime::{
}; };
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
error::RuntimeError,
backend::{Compiler, CompilerConfig, MemoryBoundCheckMode}, backend::{Compiler, CompilerConfig, MemoryBoundCheckMode},
error::RuntimeError,
loader::{Instance as LoadedInstance, LocalLoader}, loader::{Instance as LoadedInstance, LocalLoader},
Module, Module,
}; };

View File

@ -268,7 +268,7 @@ wasmer_backends! {
let result = foo.call(); let result = foo.call();
if let Err(RuntimeError(e)) = result { if let Err(RuntimeError::User(e)) = result {
let exit_code = e.downcast::<ExitCode>().unwrap(); let exit_code = e.downcast::<ExitCode>().unwrap();
assert_eq!(exit_code.code, 42); assert_eq!(exit_code.code, 42);
} else { } else {

View File

@ -52,7 +52,7 @@ macro_rules! call_and_assert {
expected_value, expected_value,
concat!("Expected right when calling `", stringify!($function), "`.") concat!("Expected right when calling `", stringify!($function), "`.")
), ),
(Err(RuntimeError(data)), Err(RuntimeError(expected_data))) => { (Err(RuntimeError::User(data)), Err(RuntimeError::User(expected_data))) => {
if let (Some(data), Some(expected_data)) = ( if let (Some(data), Some(expected_data)) = (
data.downcast_ref::<&str>(), data.downcast_ref::<&str>(),
expected_data.downcast_ref::<&str>(), expected_data.downcast_ref::<&str>(),
@ -406,7 +406,7 @@ wasmer_backends! {
test!( test_fn, function_fn(i32) -> i32, (1) == Ok(2)); test!( test_fn, function_fn(i32) -> i32, (1) == Ok(2));
test!( test_closure, function_closure(i32) -> i32, (1) == Ok(2)); test!( test_closure, function_closure(i32) -> i32, (1) == Ok(2));
test!( test_fn_dynamic, function_fn_dynamic(i32) -> i32, (1) == Ok(2)); test!( test_fn_dynamic, function_fn_dynamic(i32) -> i32, (1) == Ok(2));
test!( test_fn_dynamic_panic, function_fn_dynamic_panic(i32) -> i32, (1) == Err(RuntimeError(Box::new("test")))); test!( test_fn_dynamic_panic, function_fn_dynamic_panic(i32) -> i32, (1) == Err(RuntimeError::User(Box::new("test"))));
test!( test!(
test_closure_dynamic_0, test_closure_dynamic_0,
@ -460,31 +460,31 @@ wasmer_backends! {
test_fn_trap, test_fn_trap,
function_fn_trap(i32) -> i32, function_fn_trap(i32) -> i32,
(1) == Err(RuntimeError(Box::new(format!("foo {}", 2)))) (1) == Err(RuntimeError::User(Box::new(format!("foo {}", 2))))
); );
test!( test!(
test_closure_trap, test_closure_trap,
function_closure_trap(i32) -> i32, function_closure_trap(i32) -> i32,
(1) == Err(RuntimeError(Box::new(format!("bar {}", 2)))) (1) == Err(RuntimeError::User(Box::new(format!("bar {}", 2))))
); );
test!( test!(
test_fn_trap_with_vmctx, test_fn_trap_with_vmctx,
function_fn_trap_with_vmctx(i32) -> i32, function_fn_trap_with_vmctx(i32) -> i32,
(1) == Err(RuntimeError(Box::new(format!("baz {}", 2 + SHIFT)))) (1) == Err(RuntimeError::User(Box::new(format!("baz {}", 2 + SHIFT))))
); );
test!( test!(
test_closure_trap_with_vmctx, test_closure_trap_with_vmctx,
function_closure_trap_with_vmctx(i32) -> i32, function_closure_trap_with_vmctx(i32) -> i32,
(1) == Err(RuntimeError(Box::new(format!("qux {}", 2 + SHIFT)))) (1) == Err(RuntimeError::User(Box::new(format!("qux {}", 2 + SHIFT))))
); );
test!( test!(
test_closure_trap_with_vmctx_and_env, test_closure_trap_with_vmctx_and_env,
function_closure_trap_with_vmctx_and_env(i32) -> i32, function_closure_trap_with_vmctx_and_env(i32) -> i32,
(1) == Err(RuntimeError(Box::new(format!("! {}", 2 + shift + SHIFT)))) (1) == Err(RuntimeError::User(Box::new(format!("! {}", 2 + shift + SHIFT))))
); );
#[test] #[test]

View File

@ -6,6 +6,7 @@ use wasmer::wasm::Func;
use wasmer_middleware_common::metering::*; use wasmer_middleware_common::metering::*;
use wasmer_runtime_core::codegen::ModuleCodeGenerator; use wasmer_runtime_core::codegen::ModuleCodeGenerator;
use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler}; use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
use wasmer_runtime_core::error::RuntimeError;
use wasmer_runtime_core::fault::{pop_code_version, push_code_version}; use wasmer_runtime_core::fault::{pop_code_version, push_code_version};
use wasmer_runtime_core::state::CodeVersion; use wasmer_runtime_core::state::CodeVersion;
@ -179,10 +180,13 @@ fn middleware_test_traps_after_costly_call(backend: &'static str, compiler: impl
} }
let err = result.unwrap_err(); let err = result.unwrap_err();
assert!(err if let RuntimeError::Metering(metering_err) = err {
.0 assert!(metering_err
.downcast_ref::<ExecutionLimitExceededError>() .downcast_ref::<ExecutionLimitExceededError>()
.is_some()); .is_some());
} else {
assert!(false, "metering error not found");
}
// verify it used the correct number of points // verify it used the correct number of points
assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking. assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking.