1 #![doc(hidden)]
2 
3 use crate::runtime::vm::instance::InstanceAndStore;
4 use crate::runtime::vm::vmcontext::VMContext;
5 use core::ptr::NonNull;
6 use wasmtime_environ::{EntityRef, MemoryIndex};
7 use wasmtime_versioned_export_macros::versioned_export;
8 
9 static mut VMCTX_AND_MEMORY: (NonNull<VMContext>, usize) = (NonNull::dangling(), 0);
10 
11 // These implementations are referenced from C code in "helpers.c". The symbols defined
12 // there (prefixed by "wasmtime_") are the real 'public' interface used in the debug info.
13 
14 #[versioned_export]
15 pub unsafe extern "C" fn resolve_vmctx_memory_ptr(p: *const u32) -> *const u8 {
16     let ptr = std::ptr::read(p);
17     assert!(
18         VMCTX_AND_MEMORY.0 != NonNull::dangling(),
19         "must call `__vmctx->set()` before resolving Wasm pointers"
20     );
21     InstanceAndStore::from_vmctx(VMCTX_AND_MEMORY.0, |handle| {
22         let (handle, _) = handle.unpack_mut();
23         assert!(
24             VMCTX_AND_MEMORY.1 < handle.env_module().memories.len(),
25             "memory index for debugger is out of bounds"
26         );
27         let index = MemoryIndex::new(VMCTX_AND_MEMORY.1);
28         let mem = handle.get_memory(index);
29         mem.base.as_ptr().add(ptr as usize)
30     })
31 }
32 
33 #[versioned_export]
34 pub unsafe extern "C" fn set_vmctx_memory(vmctx_ptr: *mut VMContext) {
35     // TODO multi-memory
36     VMCTX_AND_MEMORY = (NonNull::new(vmctx_ptr).unwrap(), 0);
37 }
38 
39 /// A bit of a hack around various linkage things. The goal here is to force the
40 /// `wasmtime_*` symbols defined in `helpers.c` to actually get exported. That
41 /// means they need to be referenced for the linker to include them which is
42 /// what this function does with trickery in C.
43 pub fn init() {
44     unsafe extern "C" {
45         #[wasmtime_versioned_export_macros::versioned_link]
46         fn wasmtime_debug_builtins_init();
47     }
48 
49     unsafe {
50         wasmtime_debug_builtins_init();
51     }
52 }
53