2019-06-04 17:38:35 +00:00
|
|
|
//! Trampoline emitter for transforming function calls.
|
|
|
|
|
2019-06-03 13:59:33 +00:00
|
|
|
use std::ffi::c_void;
|
2019-06-04 17:38:35 +00:00
|
|
|
use std::mem;
|
2019-06-03 13:59:33 +00:00
|
|
|
use wasmer_runtime_core::trampoline::*;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct wasmer_trampoline_buffer_builder_t;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct wasmer_trampoline_buffer_t;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct wasmer_trampoline_callable_t;
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Creates a new trampoline builder.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-03 13:59:33 +00:00
|
|
|
pub extern "C" fn wasmer_trampoline_buffer_builder_new() -> *mut wasmer_trampoline_buffer_builder_t
|
|
|
|
{
|
|
|
|
Box::into_raw(Box::new(TrampolineBufferBuilder::new())) as *mut _
|
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Adds a context trampoline to the builder.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-04 17:25:37 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_context_trampoline(
|
2019-06-04 17:38:35 +00:00
|
|
|
builder: *mut wasmer_trampoline_buffer_builder_t,
|
|
|
|
func: *const wasmer_trampoline_callable_t,
|
2019-06-03 13:59:33 +00:00
|
|
|
ctx: *const c_void,
|
|
|
|
) -> usize {
|
2019-06-04 17:38:35 +00:00
|
|
|
let builder = &mut *(builder as *mut TrampolineBufferBuilder);
|
|
|
|
builder.add_context_trampoline(func as *const CallTarget, ctx as *const CallContext)
|
2019-06-04 17:25:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Adds a callinfo trampoline to the builder.
|
2019-06-04 17:25:37 +00:00
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_callinfo_trampoline(
|
2019-06-04 17:38:35 +00:00
|
|
|
builder: *mut wasmer_trampoline_buffer_builder_t,
|
|
|
|
func: *const wasmer_trampoline_callable_t,
|
2019-06-04 17:25:37 +00:00
|
|
|
ctx: *const c_void,
|
|
|
|
num_params: u32,
|
|
|
|
) -> usize {
|
2019-06-04 17:38:35 +00:00
|
|
|
let builder = &mut *(builder as *mut TrampolineBufferBuilder);
|
|
|
|
builder.add_callinfo_trampoline(mem::transmute(func), ctx as *const CallContext, num_params)
|
2019-06-03 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Finalizes the trampoline builder into an executable buffer.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-03 13:59:33 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_build(
|
2019-06-04 17:38:35 +00:00
|
|
|
builder: *mut wasmer_trampoline_buffer_builder_t,
|
2019-06-03 13:59:33 +00:00
|
|
|
) -> *mut wasmer_trampoline_buffer_t {
|
2019-06-04 17:38:35 +00:00
|
|
|
let builder = Box::from_raw(builder as *mut TrampolineBufferBuilder);
|
|
|
|
Box::into_raw(Box::new(builder.build())) as *mut _
|
2019-06-03 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Destroys the trampoline buffer if not null.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-04 17:38:35 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_buffer_destroy(buffer: *mut wasmer_trampoline_buffer_t) {
|
|
|
|
if !buffer.is_null() {
|
|
|
|
Box::from_raw(buffer);
|
|
|
|
}
|
2019-06-03 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Returns the callable pointer for the trampoline with index `idx`.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-03 13:59:33 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_buffer_get_trampoline(
|
2019-06-04 17:38:35 +00:00
|
|
|
buffer: *const wasmer_trampoline_buffer_t,
|
2019-06-03 13:59:33 +00:00
|
|
|
idx: usize,
|
|
|
|
) -> *const wasmer_trampoline_callable_t {
|
2019-06-04 17:38:35 +00:00
|
|
|
let buffer = &*(buffer as *const TrampolineBuffer);
|
|
|
|
buffer.get_trampoline(idx) as _
|
2019-06-03 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 17:38:35 +00:00
|
|
|
/// Returns the context added by `add_context_trampoline`, from within the callee function.
|
2019-06-03 13:59:33 +00:00
|
|
|
#[no_mangle]
|
2019-06-03 14:32:35 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-06-03 13:59:33 +00:00
|
|
|
pub unsafe extern "C" fn wasmer_trampoline_get_context() -> *mut c_void {
|
|
|
|
get_context() as *const c_void as *mut c_void
|
|
|
|
}
|