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§
Sourcefn alloc(&self, size: usize) -> Result<(*const u8, *mut u8), JitAllocError>
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.
Sourceunsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>
unsafe fn release(&self, rx_ptr: *const u8) -> Result<(), JitAllocError>
Releases the memory allocated by alloc.
§Safety
rx_ptrmust have been returned fromallocrx_ptrmust have been allocated from this allocatorrx_ptrmust not have been passed toreleasebeforerx_ptrmust point to read-execute part of memory returned fromalloc.
Sourceunsafe fn protect_jit_memory(
&self,
ptr: *const u8,
size: usize,
access: ProtectJitAccess,
)
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
ptrmust point at leastsizebytes of readable memory.
Sourceunsafe fn flush_instruction_cache(&self, rx_ptr: *const u8, size: usize)
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_ptrmust point at leastsizebytes of Read-Execute memory.