diff --git a/picoplugin/src/transport/rpc/server.rs b/picoplugin/src/transport/rpc/server.rs index ca6c8533bf05c4419299aed6fa899a0657aec856..ee2cc450fa70e6812bd016f6d3528768cc550943 100644 --- a/picoplugin/src/transport/rpc/server.rs +++ b/picoplugin/src/transport/rpc/server.rs @@ -254,7 +254,7 @@ impl FfiRpcHandler { { // This is safe. To verify see `register_rpc_handler` above. let closure_pointer: *mut F = unsafe { (*handler).closure_pointer.cast::<F>() }; - let mut closure = unsafe { Box::from_raw(closure_pointer) }; + let closure = unsafe { &*closure_pointer }; let input = unsafe { input.as_bytes() }; let context = unsafe { &*context }; let mut context = Context::new(context); @@ -285,6 +285,11 @@ impl FfiRpcHandler { let closure = Box::from_raw(closure_pointer); drop(closure); + if cfg!(debug_assertions) { + // Overwrite the pointer with garbage so that we fail loudly is case of a bug + (*handler).closure_pointer = 0xcccccccccccccccc_u64 as _; + } + let (pointer, capacity) = (*handler).string_storage.into_raw_parts(); // Note: we pretend the original Vec was filled to capacity which // may or may not be true, there might have been some unitialized