Path: blob/main/cranelift/jit/src/memory/mod.rs
3050 views
use cranelift_module::{ModuleError, ModuleResult};1use std::io;23mod arena;4mod system;56pub use arena::ArenaMemoryProvider;7pub use system::SystemMemoryProvider;89/// Type of branch protection to apply to executable memory.10#[derive(Clone, Copy, Debug, PartialEq)]11pub enum BranchProtection {12/// No protection.13None,14/// Use the Branch Target Identification extension of the Arm architecture.15BTI,16}1718pub enum JITMemoryKind {19/// Allocate memory that will be executable once finalized.20Executable,21/// Allocate writable memory.22Writable,23/// Allocate memory that will be read-only once finalized.24ReadOnly,25}2627/// A provider of memory for the JIT.28pub trait JITMemoryProvider {29/// Allocate memory30fn allocate(&mut self, size: usize, align: u64, kind: JITMemoryKind) -> io::Result<*mut u8>;3132/// Free the memory region.33unsafe fn free_memory(&mut self);34/// Finalize the memory region and apply memory protections.35fn finalize(&mut self, branch_protection: BranchProtection) -> ModuleResult<()>;36}3738/// Marks the memory region as readable and executable.39///40/// This function deals with applies branch protection and clears the icache,41/// but *doesn't* flush the pipeline. Callers have to ensure that42/// [`wasmtime_jit_icache_coherence::pipeline_flush_mt`] is called before the43/// mappings are used.44pub(crate) fn set_readable_and_executable(45ptr: *mut u8,46len: usize,47branch_protection: BranchProtection,48) -> ModuleResult<()> {49// Clear all the newly allocated code from cache if the processor requires it50//51// Do this before marking the memory as R+X, technically we should be able to do it after52// but there are some CPU's that have had errata about doing this with read only memory.53unsafe {54wasmtime_jit_icache_coherence::clear_cache(ptr as *const libc::c_void, len)55.expect("Failed cache clear")56};5758unsafe {59region::protect(ptr, len, region::Protection::READ_EXECUTE).map_err(|e| {60ModuleError::Backend(61anyhow::Error::new(e).context("unable to make memory readable+executable"),62)63})?;64}6566// If BTI is requested, and the architecture supports it, use mprotect to set the PROT_BTI flag.67if branch_protection == BranchProtection::BTI {68#[cfg(all(target_arch = "aarch64", target_os = "linux"))]69if std::arch::is_aarch64_feature_detected!("bti") {70let prot = libc::PROT_EXEC | libc::PROT_READ | /* PROT_BTI */ 0x10;7172unsafe {73if libc::mprotect(ptr as *mut libc::c_void, len, prot) < 0 {74return Err(ModuleError::Backend(75anyhow::Error::new(io::Error::last_os_error())76.context("unable to make memory readable+executable"),77));78}79}80}81}8283Ok(())84}858687