From 8ec65117430954be7c5c1baf84a253c49c45771b Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 20 Sep 2021 15:04:51 +0100 Subject: [PATCH 1/4] Exports symbols to be shared with external GDB/JIT debugging interface tools. Windows O/S specific requirement. --- crates/runtime/src/helpers.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/runtime/src/helpers.c b/crates/runtime/src/helpers.c index daf47dda11..5778280488 100644 --- a/crates/runtime/src/helpers.c +++ b/crates/runtime/src/helpers.c @@ -68,7 +68,14 @@ void wasmtime_longjmp(void *JmpBuf) { // Note the `weak` linkage here, though, which is intended to let other code // override this symbol if it's defined elsewhere, since this definition doesn't // matter. -#ifndef CFG_TARGET_OS_windows + + + + +#ifdef CFG_TARGET_OS_windows + // export required for external access. +__declspec(dllexport) +#else __attribute__((weak, noinline)) #endif void __jit_debug_register_code() { @@ -87,11 +94,16 @@ struct JITDescriptor { // Note the `weak` linkage here which is the same purpose as above. We want to // let other runtimes be able to override this since our own definition isn't // important. -#ifndef CFG_TARGET_OS_windows +#ifdef CFG_TARGET_OS_windows + // export required for external access. +__declspec(dllexport) +#else __attribute__((weak)) #endif struct JITDescriptor __jit_debug_descriptor = {1, 0, NULL, NULL}; + + struct JITDescriptor* wasmtime_jit_debug_descriptor() { return &__jit_debug_descriptor; } From 20f54bc2528c2b1fece6cd4d4e51a1314fa70814 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 20 Sep 2021 15:48:15 +0100 Subject: [PATCH 2/4] Moved comments into platform specific compiler directive sections. --- crates/runtime/src/helpers.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/crates/runtime/src/helpers.c b/crates/runtime/src/helpers.c index 5778280488..66b87a150b 100644 --- a/crates/runtime/src/helpers.c +++ b/crates/runtime/src/helpers.c @@ -61,21 +61,16 @@ void wasmtime_longjmp(void *JmpBuf) { platform_longjmp(*buf, 1); } -// Just in case cross-language LTO is enabled we set the `noinline` attribute -// and also try to have some sort of side effect in this function with a dummy -// `asm` statement. -// -// Note the `weak` linkage here, though, which is intended to let other code -// override this symbol if it's defined elsewhere, since this definition doesn't -// matter. - - - - #ifdef CFG_TARGET_OS_windows // export required for external access. __declspec(dllexport) #else + // Note the `weak` linkage here, though, which is intended to let other code + // override this symbol if it's defined elsewhere, since this definition doesn't + // matter. + // Just in case cross-language LTO is enabled we set the `noinline` attribute + // and also try to have some sort of side effect in this function with a dummy + // `asm` statement. __attribute__((weak, noinline)) #endif void __jit_debug_register_code() { @@ -91,14 +86,14 @@ struct JITDescriptor { void* first_entry_; }; -// Note the `weak` linkage here which is the same purpose as above. We want to -// let other runtimes be able to override this since our own definition isn't -// important. #ifdef CFG_TARGET_OS_windows // export required for external access. -__declspec(dllexport) + __declspec(dllexport) #else -__attribute__((weak)) + // Note the `weak` linkage here which is the same purpose as above. We want to + // let other runtimes be able to override this since our own definition isn't + // important. + __attribute__((weak)) #endif struct JITDescriptor __jit_debug_descriptor = {1, 0, NULL, NULL}; From 92a10d1ace70fe75c3e2c646ea613168fc417851 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 11 Oct 2021 09:08:14 +0100 Subject: [PATCH 3/4] Added resolve_vmctx_memory function to enable debuggers to resolve sandbox pointers - required because sandbox 'this' pointer cannot be resolved by lldb any other way as lldb expects "this" and "self" to be standard pointers, not sandbox handles. --- crates/runtime/src/debug_builtins.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/runtime/src/debug_builtins.rs b/crates/runtime/src/debug_builtins.rs index 187b6f918d..39778fae7c 100644 --- a/crates/runtime/src/debug_builtins.rs +++ b/crates/runtime/src/debug_builtins.rs @@ -6,6 +6,19 @@ use wasmtime_environ::{EntityRef, MemoryIndex}; static mut VMCTX_AND_MEMORY: (*mut VMContext, usize) = (std::ptr::null_mut(), 0); +#[no_mangle] +pub unsafe extern "C" fn resolve_vmctx_memory(ptr: usize) -> *const u8 { + let handle = InstanceHandle::from_vmctx(VMCTX_AND_MEMORY.0); + assert!( + VMCTX_AND_MEMORY.1 < handle.module().memory_plans.len(), + "memory index for debugger is out of bounds" + ); + let index = MemoryIndex::new(VMCTX_AND_MEMORY.1); + let mem = handle.instance().get_memory(index); + mem.base.add(ptr) +} + + #[no_mangle] pub unsafe extern "C" fn resolve_vmctx_memory_ptr(p: *const u32) -> *const u8 { let ptr = std::ptr::read(p); @@ -36,5 +49,6 @@ pub fn ensure_exported() { unsafe { std::ptr::read_volatile(resolve_vmctx_memory_ptr as *const u8); std::ptr::read_volatile(set_vmctx_memory as *const u8); + std::ptr::read_volatile(resolve_vmctx_memory as *const u8); } } From 807619a87405ab9b29329c13848a074a82c01082 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 11 Oct 2021 19:57:07 +0100 Subject: [PATCH 4/4] as requested: cargo fmt --- crates/runtime/src/debug_builtins.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/runtime/src/debug_builtins.rs b/crates/runtime/src/debug_builtins.rs index 39778fae7c..411300008b 100644 --- a/crates/runtime/src/debug_builtins.rs +++ b/crates/runtime/src/debug_builtins.rs @@ -14,11 +14,10 @@ pub unsafe extern "C" fn resolve_vmctx_memory(ptr: usize) -> *const u8 { "memory index for debugger is out of bounds" ); let index = MemoryIndex::new(VMCTX_AND_MEMORY.1); - let mem = handle.instance().get_memory(index); + let mem = handle.instance().get_memory(index); mem.base.add(ptr) } - #[no_mangle] pub unsafe extern "C" fn resolve_vmctx_memory_ptr(p: *const u32) -> *const u8 { let ptr = std::ptr::read(p);