Marks the memory region as readable and executable. This function deals with applies branch protection and clears the icache, but *doesn't* flush the pipeline. Callers have to ensure that [`wasmtime_jit_icache_coherence::pipeline_flush_mt`] is called before the mappings are used.
(
ptr: *mut u8,
len: usize,
branch_protection: BranchProtection,
)
| 43 | /// [`wasmtime_jit_icache_coherence::pipeline_flush_mt`] is called before the |
| 44 | /// mappings are used. |
| 45 | pub(crate) fn set_readable_and_executable( |
| 46 | ptr: *mut u8, |
| 47 | len: usize, |
| 48 | branch_protection: BranchProtection, |
| 49 | ) -> ModuleResult<()> { |
| 50 | // Clear all the newly allocated code from cache if the processor requires it |
| 51 | // |
| 52 | // Do this before marking the memory as R+X, technically we should be able to do it after |
| 53 | // but there are some CPU's that have had errata about doing this with read only memory. |
| 54 | unsafe { |
| 55 | wasmtime_jit_icache_coherence::clear_cache(ptr as *const libc::c_void, len) |
| 56 | .expect("Failed cache clear") |
| 57 | }; |
| 58 | |
| 59 | unsafe { |
| 60 | region::protect(ptr, len, region::Protection::READ_EXECUTE).map_err(|e| { |
| 61 | ModuleError::Backend( |
| 62 | anyhow::Error::new(e).context("unable to make memory readable+executable"), |
| 63 | ) |
| 64 | })?; |
| 65 | } |
| 66 | |
| 67 | // If BTI is requested, and the architecture supports it, use mprotect to set the PROT_BTI flag. |
| 68 | if branch_protection == BranchProtection::BTI { |
| 69 | #[cfg(all(target_arch = "aarch64", target_os = "linux"))] |
| 70 | if std::arch::is_aarch64_feature_detected!("bti") { |
| 71 | let prot = libc::PROT_EXEC | libc::PROT_READ | /* PROT_BTI */ 0x10; |
| 72 | |
| 73 | unsafe { |
| 74 | if libc::mprotect(ptr as *mut libc::c_void, len, prot) < 0 { |
| 75 | return Err(ModuleError::Backend( |
| 76 | anyhow::Error::new(io::Error::last_os_error()) |
| 77 | .context("unable to make memory readable+executable"), |
| 78 | )); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Ok(()) |
| 85 | } |