From 4dd64ed72e3e5bee94e2f75675d7d3ccb152d62d Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 18 Apr 2019 10:00:15 -0700 Subject: [PATCH 1/5] Remove UserTrapper trait --- lib/clif-backend/src/signal/mod.rs | 16 ++++------------ lib/llvm-backend/src/backend.rs | 10 +--------- lib/runtime-core/src/backend.rs | 6 +----- lib/runtime-core/src/module.rs | 5 ----- lib/runtime-core/src/typed_func.rs | 13 +------------ lib/runtime-core/src/vm.rs | 2 +- lib/runtime/examples/call.rs | 4 ++-- lib/singlepass-backend/src/codegen_x64.rs | 16 ++++------------ 8 files changed, 14 insertions(+), 58 deletions(-) diff --git a/lib/clif-backend/src/signal/mod.rs b/lib/clif-backend/src/signal/mod.rs index f5fcf5d5207..8081f7cf849 100644 --- a/lib/clif-backend/src/signal/mod.rs +++ b/lib/clif-backend/src/signal/mod.rs @@ -4,7 +4,7 @@ use crate::trampoline::Trampolines; use libc::c_void; use std::{any::Any, cell::Cell, ptr::NonNull, sync::Arc}; use wasmer_runtime_core::{ - backend::{RunnableModule, UserTrapper}, + backend::RunnableModule, module::ModuleInfo, typed_func::{Wasm, WasmTrapInfo}, types::{LocalFuncIndex, SigIndex}, @@ -27,15 +27,6 @@ thread_local! { pub static TRAP_EARLY_DATA: Cell>> = Cell::new(None); } -pub struct Trapper; - -impl UserTrapper for Trapper { - unsafe fn do_early_trap(&self, data: Box) -> ! { - TRAP_EARLY_DATA.with(|cell| cell.set(Some(data))); - trigger_trap() - } -} - pub struct Caller { handler_data: HandlerData, trampolines: Arc, @@ -101,8 +92,9 @@ impl RunnableModule for Caller { }) } - fn get_early_trapper(&self) -> Box { - Box::new(Trapper) + unsafe fn do_early_trap(&self, data: Box) -> ! { + TRAP_EARLY_DATA.with(|cell| cell.set(Some(data))); + trigger_trap() } } diff --git a/lib/llvm-backend/src/backend.rs b/lib/llvm-backend/src/backend.rs index 172d667263c..0baaeacffb3 100644 --- a/lib/llvm-backend/src/backend.rs +++ b/lib/llvm-backend/src/backend.rs @@ -18,7 +18,7 @@ use std::{ sync::Once, }; use wasmer_runtime_core::{ - backend::{RunnableModule, UserTrapper}, + backend::RunnableModule, module::ModuleInfo, structures::TypedIndex, typed_func::{Wasm, WasmTrapInfo}, @@ -317,14 +317,6 @@ impl RunnableModule for LLVMBackend { Some(unsafe { Wasm::from_raw_parts(trampoline, invoke_trampoline, None) }) } - fn get_early_trapper(&self) -> Box { - Box::new(Placeholder) - } -} - -struct Placeholder; - -impl UserTrapper for Placeholder { unsafe fn do_early_trap(&self, data: Box) -> ! { throw_any(Box::leak(data)) } diff --git a/lib/runtime-core/src/backend.rs b/lib/runtime-core/src/backend.rs index c6ecffcb06e..48c7e1c5f9a 100644 --- a/lib/runtime-core/src/backend.rs +++ b/lib/runtime-core/src/backend.rs @@ -65,10 +65,6 @@ pub trait Compiler { unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result; } -pub trait UserTrapper { - unsafe fn do_early_trap(&self, data: Box) -> !; -} - pub trait RunnableModule: Send + Sync { /// This returns a pointer to the function designated by the `local_func_index` /// parameter. @@ -83,7 +79,7 @@ pub trait RunnableModule: Send + Sync { /// signature and an invoke function that can call the trampoline. fn get_trampoline(&self, info: &ModuleInfo, sig_index: SigIndex) -> Option; - fn get_early_trapper(&self) -> Box; + unsafe fn do_early_trap(&self, data: Box) -> !; } pub trait CacheGen: Send + Sync { diff --git a/lib/runtime-core/src/module.rs b/lib/runtime-core/src/module.rs index 9e9e93052b6..40cde6fbf77 100644 --- a/lib/runtime-core/src/module.rs +++ b/lib/runtime-core/src/module.rs @@ -4,7 +4,6 @@ use crate::{ error, import::ImportObject, structures::{Map, TypedIndex}, - typed_func::EARLY_TRAPPER, types::{ FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer, @@ -92,10 +91,6 @@ pub struct Module { impl Module { pub(crate) fn new(inner: Arc) -> Self { - unsafe { - EARLY_TRAPPER - .with(|ucell| *ucell.get() = Some(inner.runnable_module.get_early_trapper())); - } Module { inner } } diff --git a/lib/runtime-core/src/typed_func.rs b/lib/runtime-core/src/typed_func.rs index 5e38db62822..59c610773ee 100644 --- a/lib/runtime-core/src/typed_func.rs +++ b/lib/runtime-core/src/typed_func.rs @@ -1,5 +1,4 @@ use crate::{ - backend::UserTrapper, error::RuntimeError, export::{Context, Export, FuncPointer}, import::IsExport, @@ -8,7 +7,6 @@ use crate::{ }; use std::{ any::Any, - cell::UnsafeCell, ffi::c_void, fmt, marker::PhantomData, @@ -17,10 +15,6 @@ use std::{ sync::Arc, }; -thread_local! { - pub static EARLY_TRAPPER: UnsafeCell>> = UnsafeCell::new(None); -} - #[repr(C)] pub enum WasmTrapInfo { Unreachable = 0, @@ -354,12 +348,7 @@ macro_rules! impl_traits { }; unsafe { - if let Some(early_trapper) = &*EARLY_TRAPPER.with(|ucell| ucell.get()) { - early_trapper.do_early_trap(err) - } else { - eprintln!("panic handling not setup"); - std::process::exit(1) - } + (&*ctx.module).runnable_module.do_early_trap(err) } } diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index 1d955d40d23..0c9676358d3 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -22,7 +22,7 @@ pub struct Ctx { local_backing: *mut LocalBacking, import_backing: *mut ImportBacking, - module: *const ModuleInner, + pub(crate) module: *const ModuleInner, pub data: *mut c_void, pub data_finalizer: Option, diff --git a/lib/runtime/examples/call.rs b/lib/runtime/examples/call.rs index 894fa4ab1b0..12f14906281 100644 --- a/lib/runtime/examples/call.rs +++ b/lib/runtime/examples/call.rs @@ -59,9 +59,9 @@ fn main() -> Result<(), error::Error> { }, })?; - let foo = instance.dyn_func("dbz")?; + let foo: Func<(), i32> = instance.func("dbz")?; - let result = foo.call(&[]); + let result = foo.call(); println!("result: {:?}", result); diff --git a/lib/singlepass-backend/src/codegen_x64.rs b/lib/singlepass-backend/src/codegen_x64.rs index 8c6a18857ab..10f9f16af93 100644 --- a/lib/singlepass-backend/src/codegen_x64.rs +++ b/lib/singlepass-backend/src/codegen_x64.rs @@ -11,7 +11,7 @@ use smallvec::SmallVec; use std::ptr::NonNull; use std::{any::Any, collections::HashMap, sync::Arc}; use wasmer_runtime_core::{ - backend::{RunnableModule, UserTrapper}, + backend::RunnableModule, memory::MemoryType, module::ModuleInfo, structures::{Map, TypedIndex}, @@ -249,17 +249,9 @@ impl RunnableModule for X64ExecutionContext { }) } - fn get_early_trapper(&self) -> Box { - pub struct Trapper; - - impl UserTrapper for Trapper { - unsafe fn do_early_trap(&self, data: Box) -> ! { - protect_unix::TRAP_EARLY_DATA.with(|x| x.set(Some(data))); - protect_unix::trigger_trap(); - } - } - - Box::new(Trapper) + unsafe fn do_early_trap(&self, data: Box) -> ! { + protect_unix::TRAP_EARLY_DATA.with(|x| x.set(Some(data))); + protect_unix::trigger_trap(); } } From f0e0255b3ffc62bddedd1e61c3e61499d3fdcee4 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 18 Apr 2019 10:08:17 -0700 Subject: [PATCH 2/5] Fix vm test --- lib/runtime-core/src/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index 0c9676358d3..344d2e64ce4 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -563,7 +563,7 @@ mod vm_ctx_tests { fn get_trampoline(&self, _module: &ModuleInfo, _sig_index: SigIndex) -> Option { unimplemented!() } - fn get_early_trapper(&self) -> Box { + unsafe fn do_early_trap(&self, _: Box) -> ! { unimplemented!() } } From 6279dd8e82385c9a7390cf59ea260b0ab81fa177 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 18 Apr 2019 10:14:25 -0700 Subject: [PATCH 3/5] Fix test again, whoops --- lib/runtime-core/src/vm.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index 344d2e64ce4..6a408cd0484 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -544,11 +544,12 @@ mod vm_ctx_tests { fn generate_module() -> ModuleInner { use super::Func; - use crate::backend::{sys::Memory, Backend, CacheGen, RunnableModule, UserTrapper}; + use crate::backend::{sys::Memory, Backend, CacheGen, RunnableModule}; use crate::cache::Error as CacheError; use crate::typed_func::Wasm; use crate::types::{LocalFuncIndex, SigIndex}; use hashbrown::HashMap; + use std::any::Any; use std::ptr::NonNull; struct Placeholder; impl RunnableModule for Placeholder { From 016ef1c7b94895b0caee94c9f7690a53a735f4d0 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 18 Apr 2019 10:16:11 -0700 Subject: [PATCH 4/5] Add changelog item --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c283e930be..de55c4233b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file. Blocks of changes will separated by version increments. ## **[Unreleased]** +- [#000]() Remove `UserTrapper` trait to fix [#365](https://github.com/wasmerio/wasmer/issues/365). - [#348](https://github.com/wasmerio/wasmer/pull/348) Refactor internal runtime ↔️ backend abstraction. - [#355](https://github.com/wasmerio/wasmer/pull/355) Misc changes to `Cargo.toml`s for publishing - [#352](https://github.com/wasmerio/wasmer/pull/352) Bump version numbers to 0.3.0 From 36d73d1b48856b99c4caf991ec1a79d1c5d107b6 Mon Sep 17 00:00:00 2001 From: Lachlan Sneff Date: Thu, 18 Apr 2019 10:18:36 -0700 Subject: [PATCH 5/5] Update changelog to point to pr --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de55c4233b8..2545d092654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All PRs to the Wasmer repository must add to this file. Blocks of changes will separated by version increments. ## **[Unreleased]** -- [#000]() Remove `UserTrapper` trait to fix [#365](https://github.com/wasmerio/wasmer/issues/365). +- [#366](https://github.com/wasmerio/wasmer/pull/366) Remove `UserTrapper` trait to fix [#365](https://github.com/wasmerio/wasmer/issues/365). - [#348](https://github.com/wasmerio/wasmer/pull/348) Refactor internal runtime ↔️ backend abstraction. - [#355](https://github.com/wasmerio/wasmer/pull/355) Misc changes to `Cargo.toml`s for publishing - [#352](https://github.com/wasmerio/wasmer/pull/352) Bump version numbers to 0.3.0