JitAlloc

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(
        &self,
        ptr: *const u8,
        size: usize,
        access: ProtectJitAccess,
    );
    unsafe fn flush_instruction_cache(&self, 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( &self, 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(&self, 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.

Implementors§

Source§

impl JitAlloc for GlobalJitAlloc

Available on crate feature global_jit_alloc only.
Source§

impl JitAlloc for ThreadJitAlloc

Available on crate features std and default_jit_alloc only.
Source§

impl<J: JitAlloc, D: Deref<Target = J>> JitAlloc for D