From 420d429838f5a83976da06a232c208f3b3f710b4 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Thu, 23 May 2024 19:24:06 +1000 Subject: [PATCH] Update IPASIR callback wrappers to use Box::leak instead of mem::forget --- crates/pindakaas-derive/src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/pindakaas-derive/src/lib.rs b/crates/pindakaas-derive/src/lib.rs index a4f28fe136..46efffffa8 100644 --- a/crates/pindakaas-derive/src/lib.rs +++ b/crates/pindakaas-derive/src/lib.rs @@ -101,16 +101,16 @@ pub fn ipasir_solver_derive(input: TokenStream) -> TokenStream { crate::solver::SlvTermSignal::Terminate => std::ffi::c_int::from(1), } }; - let data = &mut wrapped_cb as *mut _ as *mut std::ffi::c_void; + let trampoline = crate::solver::libloading::get_trampoline0(&wrapped_cb); + // WARNING: Any data in the callback now exists forever + let data = Box::leak(Box::new(wrapped_cb)) as *mut _ as *mut std::ffi::c_void; unsafe { #krate::ipasir_set_terminate( #ptr, data, - Some(crate::solver::libloading::get_trampoline0(&wrapped_cb)), + Some(trampoline), ) } - // WARNING: Any data in the callback now exists forever - std::mem::forget(wrapped_cb); } else { unsafe { #krate::ipasir_set_terminate(#ptr, std::ptr::null_mut(), None) } } @@ -135,17 +135,17 @@ pub fn ipasir_solver_derive(input: TokenStream) -> TokenStream { .map(|i: i32| crate::Lit(std::num::NonZeroI32::new(i).unwrap())); cb(&mut iter) }; - let data = &mut wrapped_cb as *mut _ as *mut std::ffi::c_void; + let trampoline = crate::solver::libloading::get_trampoline1(&wrapped_cb); + // WARNING: Any data in the callback now exists forever + let data = Box::leak(Box::new(wrapped_cb)) as *mut _ as *mut std::ffi::c_void; unsafe { #krate::ipasir_set_learn( #ptr, data, MAX_LEN, - Some(crate::solver::libloading::get_trampoline1(&wrapped_cb)), + Some(trampoline), ) } - // WARNING: Any data in the callback now exists forever - std::mem::forget(wrapped_cb); } else { unsafe { #krate::ipasir_set_learn(#ptr, std::ptr::null_mut(), MAX_LEN, None) } }