summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rlbox_lucet_sandbox/src/callback.rs
blob: b9f42aab47e8e014fbd6cea8583570faf11ac51f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::types::{LucetFunctionSignature, LucetSandboxInstance, SizedBuffer};

use lucet_module::Signature;
use lucet_runtime_internals::instance::InstanceInternal;

use std::convert::TryFrom;
use std::ffi::c_void;

#[no_mangle]
pub extern "C" fn lucet_get_reserved_callback_slot_val(
    inst_ptr: *mut c_void,
    slot_number: u32,
) -> usize {
    let inst = unsafe { &mut *(inst_ptr as *mut LucetSandboxInstance) };

    let name = format!("sandboxReservedCallbackSlot{}", slot_number);
    let func = inst
        .instance_handle
        .module()
        .get_export_func(&name)
        .unwrap();
    return func.ptr.as_usize();
}

#[no_mangle]
pub extern "C" fn lucet_get_function_pointer_table(inst_ptr: *mut c_void) -> SizedBuffer {
    let inst = unsafe { &mut *(inst_ptr as *mut LucetSandboxInstance) };

    let elems = inst.instance_handle.module().table_elements().unwrap();

    SizedBuffer {
        data: elems.as_ptr() as *mut c_void,
        length: elems.len(),
    }
}

#[no_mangle]
pub extern "C" fn lucet_get_function_type_index(
    inst_ptr: *mut c_void,
    csig: LucetFunctionSignature,
) -> i32 {
    let inst = unsafe { &mut *(inst_ptr as *mut LucetSandboxInstance) };

    let conv_sig: Signature = csig.into();
    let index = inst.signatures.iter().position(|r| *r == conv_sig);

    match index {
        Some(x) => i32::try_from(x).unwrap_or(-1),
        _ => -1,
    }
}