From 1c18d2312b2a08c52578652867c15996e8052016 Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Fri, 4 Dec 2020 11:41:53 +0000 Subject: [PATCH] Pointer tagging should be OR not left-shift. Co-authored-by: Lukas Diekmann . --- ykrt/src/mt.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ykrt/src/mt.rs b/ykrt/src/mt.rs index cc1f8ed6d..f1dbc4fa6 100644 --- a/ykrt/src/mt.rs +++ b/ykrt/src/mt.rs @@ -296,7 +296,7 @@ impl MTThread { // FIXME: free up the memory when the trace is no longer used. let ptr = Box::into_raw(Box::new(ct)) as usize; - let new_pack = PHASE_COMPILED | (ptr << PHASE_NUM_BITS); + let new_pack = ptr | PHASE_COMPILED; loc.pack.store(new_pack, Ordering::Release); // Free the small block of memory we used as a Location ID. unsafe { Box::from_raw(loc_id) }; @@ -304,12 +304,15 @@ impl MTThread { return None; } PHASE_COMPILED => { - let ptr = (lp >> PHASE_NUM_BITS) as *const u8; - let bct = unsafe { Box::from_raw(ptr as *mut CompiledTrace) }; - let tptr = bct.ptr(); - let func: fn(&mut I) -> bool = unsafe { mem::transmute(tptr) }; + let p = (lp & !PHASE_TAG) as *const (); + // Retrieve the CompiledTrace to gain access to the memory containing the + // trace. + let bct = unsafe { Box::from_raw(p as *mut CompiledTrace) }; + let f = unsafe { mem::transmute::<_, fn(&mut I) -> bool>(bct.ptr()) }; + // Forget the CompiledTrace again to make sure it isn't dropped before we had a + // chance to execute it. mem::forget(bct); - return Some(func); + return Some(f); } _ => unreachable!(), }