From 21165d81c9030ed9b291a1021a367214d2942c90 Mon Sep 17 00:00:00 2001 From: Afonso Bordado Date: Wed, 5 Oct 2022 16:25:37 +0100 Subject: [PATCH] Make clear_cache safe --- cranelift/jit/src/memory.rs | 5 +---- crates/jit-icache-coherence/src/lib.rs | 9 +-------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/cranelift/jit/src/memory.rs b/cranelift/jit/src/memory.rs index a479ea1c1e..24e6ef9968 100644 --- a/cranelift/jit/src/memory.rs +++ b/cranelift/jit/src/memory.rs @@ -176,10 +176,7 @@ impl Memory { // Do this before marking the memory as R+X, technically we should be able to do it after // but there are some CPU's that have had errata about doing this with read only memory. for &PtrLen { ptr, len, .. } in self.non_protected_allocations_iter() { - unsafe { - icache_coherence::clear_cache(ptr as *const c_void, len) - .expect("Failed cache clear") - }; + icache_coherence::clear_cache(ptr as *const c_void, len).expect("Failed cache clear") } // Flush any in-flight instructions from the pipeline diff --git a/crates/jit-icache-coherence/src/lib.rs b/crates/jit-icache-coherence/src/lib.rs index fe9e23b107..bd05cd0bb3 100644 --- a/crates/jit-icache-coherence/src/lib.rs +++ b/crates/jit-icache-coherence/src/lib.rs @@ -42,7 +42,6 @@ //! # addr: &code[0] as *const u8 as *const c_void, //! # len: code.len(), //! # }]; -//! # unsafe { //! // Invalidate the cache for all the newly written pages where we wrote our new code. //! for page in newly_written_pages { //! clear_cache(page.addr, page.len)?; @@ -53,7 +52,6 @@ //! //! // We can now safely execute our new code. //! run_code(); -//! # } //! # Ok(()) //! # } //! ``` @@ -98,11 +96,6 @@ pub fn pipeline_flush() -> Result<()> { /// Flushes the instruction cache for a region of memory. /// /// If the architecture does not require an instruction cache flush, this function does nothing. -/// -/// # Unsafe -/// -/// You must always call [pipeline_flush] calling just [clear_cache] -/// is not sufficient. -pub unsafe fn clear_cache(ptr: *const c_void, len: usize) -> Result<()> { +pub fn clear_cache(ptr: *const c_void, len: usize) -> Result<()> { imp::clear_cache(ptr, len) }