closure_ffi::jit_alloc

Trait JitAlloc

Source
pub trait JitAlloc {
    // Required methods
    fn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>;
    unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>;
    unsafe fn protect_jit_memory(
        ptr: *const u8,
        size: usize,
        access: ProtectJitAccess,
    );
    unsafe fn flush_instruction_cache(rx_ptr: *const u8, size: usize);
}
Expand description

Generic allocator providing virtual memory suitable for emitting code at runtime.

The API is meant to be a thin abstraction over the jit-allocator crate’s API, to allow it to be swapped with other allocators.

Required Methods§

Source

fn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>

Allocates size bytes in the executable memory region. Returns two pointers. One points to Read-Execute mapping and another to Read-Write mapping. All code writes must go to the Read-Write mapping.

Source

unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>

Releases the memory allocated by alloc.

§Safety
  • rx_ptr must have been returned from alloc
  • rx_ptr must have been allocated from this allocator
  • rx_ptr must not have been passed to release before
  • rx_ptr must point to read-execute part of memory returned from alloc.
Source

unsafe fn protect_jit_memory( ptr: *const u8, size: usize, access: ProtectJitAccess, )

On hardened architectures with MAP_JIT-like memory flags, set the access for the current thread.

This is expected to be a no-op on most platforms, but should be called before writing or executing JIT memory.

§Safety
  • ptr must point at least size bytes of readable memory.
Source

unsafe fn flush_instruction_cache(rx_ptr: *const u8, size: usize)

Flushes the instruction cache for (at least) the given slice of executable memory. Should be called after the JIT memory is ready to be executed.

On architectures with shared data/instruction caches, like x86_64, this is a no-op.

§Safety
  • rx_ptr must point at least size bytes of Read-Execute memory.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl JitAlloc for RefCell<JitAllocator>

Source§

fn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>

Source§

unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>

Source§

unsafe fn flush_instruction_cache(rx_ptr: *const u8, size: usize)

Source§

unsafe fn protect_jit_memory( _ptr: *const u8, _size: usize, access: ProtectJitAccess, )

Source§

impl JitAlloc for Mutex<JitAllocator>

Source§

fn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>

Source§

unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>

Source§

unsafe fn flush_instruction_cache(rx_ptr: *const u8, size: usize)

Source§

unsafe fn protect_jit_memory( _ptr: *const u8, _size: usize, access: ProtectJitAccess, )

Source§

impl JitAlloc for RwLock<JitAllocator>

Source§

fn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>

Source§

unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>

Source§

unsafe fn flush_instruction_cache(rx_ptr: *const u8, size: usize)

Source§

unsafe fn protect_jit_memory( _ptr: *const u8, _size: usize, access: ProtectJitAccess, )

Source§

impl<J: JitAlloc> JitAlloc for &J

Implementors§