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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::{
relocation::{TrapData, TrapSink},
resolver::FuncResolver,
trampoline::Trampolines,
};
use libc::c_void;
use std::{any::Any, cell::Cell, ptr::NonNull, sync::Arc};
use wasmer_runtime_core::{
backend::RunnableModule,
module::ModuleInfo,
typed_func::{Trampoline, Wasm},
types::{LocalFuncIndex, SigIndex},
vm,
};
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
pub use self::unix::*;
#[cfg(windows)]
pub use self::windows::*;
thread_local! {
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any + Send>>> = Cell::new(None);
}
pub struct CallProtError(pub Box<dyn Any + Send>);
pub struct Caller {
handler_data: HandlerData,
trampolines: Arc<Trampolines>,
resolver: FuncResolver,
}
impl Caller {
pub fn new(
handler_data: HandlerData,
trampolines: Arc<Trampolines>,
resolver: FuncResolver,
) -> Self {
Self {
handler_data,
trampolines,
resolver,
}
}
}
impl RunnableModule for Caller {
fn get_func(&self, _: &ModuleInfo, func_index: LocalFuncIndex) -> Option<NonNull<vm::Func>> {
self.resolver.lookup(func_index)
}
fn get_trampoline(&self, _: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm> {
unsafe extern "C" fn invoke(
trampoline: Trampoline,
ctx: *mut vm::Ctx,
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
error_out: *mut Option<Box<dyn Any + Send>>,
invoke_env: Option<NonNull<c_void>>,
) -> bool {
let handler_data = &*invoke_env.unwrap().cast().as_ptr();
#[cfg(not(target_os = "windows"))]
let res = call_protected(handler_data, || {
trampoline(ctx, func, args, rets);
});
#[cfg(target_os = "windows")]
let res = call_protected(handler_data, trampoline, ctx, func, args, rets);
match res {
Err(err) => {
*error_out = Some(err.0);
false
}
Ok(()) => true,
}
}
let trampoline = self
.trampolines
.lookup(sig_index)
.expect("that trampoline doesn't exist");
Some(unsafe {
Wasm::from_raw_parts(
trampoline,
invoke,
Some(NonNull::from(&self.handler_data).cast()),
)
})
}
unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> ! {
TRAP_EARLY_DATA.with(|cell| cell.set(Some(data)));
trigger_trap()
}
}
unsafe impl Send for HandlerData {}
unsafe impl Sync for HandlerData {}
#[derive(Clone)]
pub struct HandlerData {
pub trap_data: Arc<TrapSink>,
exec_buffer_ptr: *const c_void,
exec_buffer_size: usize,
}
impl HandlerData {
pub fn new(
trap_data: Arc<TrapSink>,
exec_buffer_ptr: *const c_void,
exec_buffer_size: usize,
) -> Self {
Self {
trap_data,
exec_buffer_ptr,
exec_buffer_size,
}
}
pub fn lookup(&self, ip: *const c_void) -> Option<TrapData> {
let ip = ip as usize;
let buffer_ptr = self.exec_buffer_ptr as usize;
if buffer_ptr <= ip && ip < buffer_ptr + self.exec_buffer_size {
let offset = ip - buffer_ptr;
self.trap_data.lookup(offset)
} else {
None
}
}
}