2019-07-26 18:50:49 +00:00
|
|
|
use super::stackmap::{self, StackmapRegistry, StkMapRecord, StkSizeRecord};
|
2019-02-26 02:07:22 +00:00
|
|
|
use crate::intrinsics::Intrinsics;
|
2019-07-30 21:47:53 +00:00
|
|
|
use crate::structs::{Callbacks, LLVMModule, LLVMResult, MemProtect};
|
2019-02-16 00:02:20 +00:00
|
|
|
use inkwell::{
|
2019-03-01 01:20:18 +00:00
|
|
|
memory_buffer::MemoryBuffer,
|
2019-02-16 00:02:20 +00:00
|
|
|
module::Module,
|
2019-02-26 02:07:22 +00:00
|
|
|
targets::{CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine},
|
|
|
|
OptimizationLevel,
|
2019-02-16 00:02:20 +00:00
|
|
|
};
|
2019-07-30 21:47:53 +00:00
|
|
|
use libc::c_char;
|
2019-02-28 20:31:39 +00:00
|
|
|
use std::{
|
2019-03-04 21:10:28 +00:00
|
|
|
any::Any,
|
2019-07-30 14:25:15 +00:00
|
|
|
collections::BTreeMap,
|
2019-04-09 22:53:01 +00:00
|
|
|
ffi::{c_void, CString},
|
2019-08-08 23:05:17 +00:00
|
|
|
fs::File,
|
|
|
|
io::Write,
|
2019-03-01 23:48:43 +00:00
|
|
|
mem,
|
2019-04-19 20:54:48 +00:00
|
|
|
ops::Deref,
|
2019-02-28 20:31:39 +00:00
|
|
|
ptr::{self, NonNull},
|
2019-03-02 01:11:20 +00:00
|
|
|
slice, str,
|
2019-04-19 20:54:48 +00:00
|
|
|
sync::{Arc, Once},
|
2019-02-28 20:31:39 +00:00
|
|
|
};
|
2019-02-16 00:02:20 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-04-19 20:54:48 +00:00
|
|
|
backend::{
|
|
|
|
sys::{Memory, Protect},
|
|
|
|
CacheGen, RunnableModule,
|
|
|
|
},
|
|
|
|
cache::Error as CacheError,
|
2019-04-12 17:05:50 +00:00
|
|
|
module::ModuleInfo,
|
2019-07-18 18:02:15 +00:00
|
|
|
state::ModuleStateMap,
|
2019-02-28 20:31:39 +00:00
|
|
|
structures::TypedIndex,
|
2019-04-09 22:53:01 +00:00
|
|
|
typed_func::{Wasm, WasmTrapInfo},
|
2019-04-12 17:05:50 +00:00
|
|
|
types::{LocalFuncIndex, SigIndex},
|
|
|
|
vm, vmcalls,
|
2019-02-16 00:02:20 +00:00
|
|
|
};
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
extern "C" {
|
2019-03-01 01:20:18 +00:00
|
|
|
fn module_load(
|
2019-02-28 20:31:39 +00:00
|
|
|
mem_ptr: *const u8,
|
|
|
|
mem_size: usize,
|
2019-03-01 01:20:18 +00:00
|
|
|
callbacks: Callbacks,
|
2019-02-28 20:31:39 +00:00
|
|
|
module_out: &mut *mut LLVMModule,
|
|
|
|
) -> LLVMResult;
|
2019-03-01 01:20:18 +00:00
|
|
|
fn module_delete(module: *mut LLVMModule);
|
2019-02-28 20:31:39 +00:00
|
|
|
fn get_func_symbol(module: *mut LLVMModule, name: *const c_char) -> *const vm::Func;
|
2019-07-18 18:02:15 +00:00
|
|
|
fn llvm_backend_get_stack_map_ptr(module: *const LLVMModule) -> *const u8;
|
|
|
|
fn llvm_backend_get_stack_map_size(module: *const LLVMModule) -> usize;
|
|
|
|
fn llvm_backend_get_code_ptr(module: *const LLVMModule) -> *const u8;
|
|
|
|
fn llvm_backend_get_code_size(module: *const LLVMModule) -> usize;
|
2019-03-02 22:16:02 +00:00
|
|
|
|
2019-08-01 19:12:53 +00:00
|
|
|
fn throw_trap(ty: i32) -> !;
|
|
|
|
fn throw_breakpoint(ty: i64) -> !;
|
2019-04-10 21:17:10 +00:00
|
|
|
|
|
|
|
/// This should be the same as spliting up the fat pointer into two arguments,
|
|
|
|
/// but this is cleaner, I think?
|
2019-04-11 19:07:54 +00:00
|
|
|
#[cfg_attr(nightly, unwind(allowed))]
|
|
|
|
#[allow(improper_ctypes)]
|
2019-04-10 21:17:10 +00:00
|
|
|
fn throw_any(data: *mut dyn Any) -> !;
|
2019-03-02 22:16:02 +00:00
|
|
|
|
2019-04-09 22:53:01 +00:00
|
|
|
#[allow(improper_ctypes)]
|
2019-03-02 20:57:35 +00:00
|
|
|
fn invoke_trampoline(
|
2019-04-09 22:53:01 +00:00
|
|
|
trampoline: unsafe extern "C" fn(*mut vm::Ctx, NonNull<vm::Func>, *const u64, *mut u64),
|
2019-03-02 20:57:35 +00:00
|
|
|
vmctx_ptr: *mut vm::Ctx,
|
2019-04-09 22:53:01 +00:00
|
|
|
func_ptr: NonNull<vm::Func>,
|
2019-03-02 20:57:35 +00:00
|
|
|
params: *const u64,
|
|
|
|
results: *mut u64,
|
2019-04-09 22:53:01 +00:00
|
|
|
trap_out: *mut WasmTrapInfo,
|
2019-04-22 22:06:40 +00:00
|
|
|
user_error: *mut Option<Box<dyn Any>>,
|
2019-04-09 22:53:01 +00:00
|
|
|
invoke_env: Option<NonNull<c_void>>,
|
2019-03-03 03:08:15 +00:00
|
|
|
) -> bool;
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_callbacks() -> Callbacks {
|
|
|
|
extern "C" fn alloc_memory(
|
|
|
|
size: usize,
|
|
|
|
protect: MemProtect,
|
|
|
|
ptr_out: &mut *mut u8,
|
|
|
|
size_out: &mut usize,
|
|
|
|
) -> LLVMResult {
|
2019-07-30 21:47:53 +00:00
|
|
|
unsafe { crate::platform::alloc_memory(size, protect, ptr_out, size_out) }
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn protect_memory(ptr: *mut u8, size: usize, protect: MemProtect) -> LLVMResult {
|
2019-07-30 21:47:53 +00:00
|
|
|
unsafe { crate::platform::protect_memory(ptr, size, protect) }
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn dealloc_memory(ptr: *mut u8, size: usize) -> LLVMResult {
|
2019-07-30 21:47:53 +00:00
|
|
|
unsafe { crate::platform::dealloc_memory(ptr, size) }
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 01:11:20 +00:00
|
|
|
extern "C" fn lookup_vm_symbol(name_ptr: *const c_char, length: usize) -> *const vm::Func {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
macro_rules! fn_name {
|
|
|
|
($s:literal) => {
|
|
|
|
concat!("_", $s)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
macro_rules! fn_name {
|
|
|
|
($s:literal) => {
|
|
|
|
$s
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let name_slice = unsafe { slice::from_raw_parts(name_ptr as *const u8, length) };
|
|
|
|
let name = str::from_utf8(name_slice).unwrap();
|
|
|
|
|
|
|
|
match name {
|
|
|
|
fn_name!("vm.memory.grow.dynamic.local") => vmcalls::local_dynamic_memory_grow as _,
|
|
|
|
fn_name!("vm.memory.size.dynamic.local") => vmcalls::local_dynamic_memory_size as _,
|
|
|
|
fn_name!("vm.memory.grow.static.local") => vmcalls::local_static_memory_grow as _,
|
|
|
|
fn_name!("vm.memory.size.static.local") => vmcalls::local_static_memory_size as _,
|
2019-03-02 20:57:35 +00:00
|
|
|
|
2019-08-14 02:07:55 +00:00
|
|
|
fn_name!("vm.memory.grow.dynamic.import") => vmcalls::imported_dynamic_memory_grow as _,
|
|
|
|
fn_name!("vm.memory.size.dynamic.import") => vmcalls::imported_dynamic_memory_size as _,
|
|
|
|
fn_name!("vm.memory.grow.static.import") => vmcalls::imported_static_memory_grow as _,
|
|
|
|
fn_name!("vm.memory.size.static.import") => vmcalls::imported_static_memory_size as _,
|
|
|
|
|
2019-03-02 22:16:02 +00:00
|
|
|
fn_name!("vm.exception.trap") => throw_trap as _,
|
2019-08-01 19:12:53 +00:00
|
|
|
fn_name!("vm.breakpoint") => throw_breakpoint as _,
|
2019-03-02 22:16:02 +00:00
|
|
|
|
2019-03-02 01:11:20 +00:00
|
|
|
_ => ptr::null(),
|
|
|
|
}
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 18:56:02 +00:00
|
|
|
extern "C" fn visit_fde(fde: *mut u8, size: usize, visitor: extern "C" fn(*mut u8)) {
|
|
|
|
unsafe {
|
|
|
|
crate::platform::visit_fde(fde, size, visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
Callbacks {
|
|
|
|
alloc_memory,
|
|
|
|
protect_memory,
|
|
|
|
dealloc_memory,
|
|
|
|
lookup_vm_symbol,
|
2019-03-02 18:56:02 +00:00
|
|
|
visit_fde,
|
2019-02-28 20:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 20:54:48 +00:00
|
|
|
pub enum Buffer {
|
|
|
|
LlvmMemory(MemoryBuffer),
|
|
|
|
Memory(Memory),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Buffer {
|
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
match self {
|
|
|
|
Buffer::LlvmMemory(mem_buffer) => mem_buffer.as_slice(),
|
|
|
|
Buffer::Memory(memory) => unsafe { memory.as_slice() },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
unsafe impl Send for LLVMBackend {}
|
|
|
|
unsafe impl Sync for LLVMBackend {}
|
|
|
|
|
2019-02-16 00:02:20 +00:00
|
|
|
pub struct LLVMBackend {
|
2019-02-28 20:31:39 +00:00
|
|
|
module: *mut LLVMModule,
|
2019-03-01 01:20:18 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-19 20:54:48 +00:00
|
|
|
buffer: Arc<Buffer>,
|
2019-07-18 18:02:15 +00:00
|
|
|
msm: Option<ModuleStateMap>,
|
2019-08-08 20:26:17 +00:00
|
|
|
local_func_id_to_offset: Vec<usize>,
|
2019-02-16 00:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LLVMBackend {
|
2019-07-18 18:02:15 +00:00
|
|
|
pub fn new(
|
|
|
|
module: Module,
|
|
|
|
_intrinsics: Intrinsics,
|
2019-07-22 18:55:43 +00:00
|
|
|
stackmaps: &StackmapRegistry,
|
2019-07-26 18:50:49 +00:00
|
|
|
module_info: &ModuleInfo,
|
2019-07-18 18:02:15 +00:00
|
|
|
) -> (Self, LLVMCache) {
|
2019-02-26 02:07:22 +00:00
|
|
|
Target::initialize_x86(&InitializationConfig {
|
|
|
|
asm_parser: true,
|
|
|
|
asm_printer: true,
|
|
|
|
base: true,
|
|
|
|
disassembler: true,
|
|
|
|
info: true,
|
|
|
|
machine_code: true,
|
|
|
|
});
|
|
|
|
let triple = TargetMachine::get_default_triple().to_string();
|
|
|
|
let target = Target::from_triple(&triple).unwrap();
|
|
|
|
let target_machine = target
|
|
|
|
.create_target_machine(
|
|
|
|
&triple,
|
|
|
|
&TargetMachine::get_host_cpu_name().to_string(),
|
|
|
|
&TargetMachine::get_host_cpu_features().to_string(),
|
2019-03-05 01:37:05 +00:00
|
|
|
OptimizationLevel::Aggressive,
|
2019-02-26 02:07:22 +00:00
|
|
|
RelocMode::PIC,
|
|
|
|
CodeModel::Default,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let memory_buffer = target_machine
|
|
|
|
.write_to_memory_buffer(&module, FileType::Object)
|
|
|
|
.unwrap();
|
2019-02-28 20:31:39 +00:00
|
|
|
let mem_buf_slice = memory_buffer.as_slice();
|
2019-02-26 02:07:22 +00:00
|
|
|
|
2019-08-08 23:05:17 +00:00
|
|
|
if let Some(path) = unsafe { &crate::GLOBAL_OPTIONS.obj_file } {
|
|
|
|
let mut file = File::create(path).unwrap();
|
|
|
|
let mut pos = 0;
|
|
|
|
while pos < mem_buf_slice.len() {
|
|
|
|
pos += file.write(&mem_buf_slice[pos..]).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
let callbacks = get_callbacks();
|
|
|
|
let mut module: *mut LLVMModule = ptr::null_mut();
|
|
|
|
|
|
|
|
let res = unsafe {
|
2019-03-01 01:20:18 +00:00
|
|
|
module_load(
|
2019-02-28 20:31:39 +00:00
|
|
|
mem_buf_slice.as_ptr(),
|
|
|
|
mem_buf_slice.len(),
|
2019-03-01 01:20:18 +00:00
|
|
|
callbacks,
|
2019-02-28 20:31:39 +00:00
|
|
|
&mut module,
|
|
|
|
)
|
|
|
|
};
|
2019-02-26 02:07:22 +00:00
|
|
|
|
2019-08-09 18:32:14 +00:00
|
|
|
/*
|
|
|
|
|
2019-03-06 00:50:49 +00:00
|
|
|
static SIGNAL_HANDLER_INSTALLED: Once = Once::new();
|
|
|
|
|
|
|
|
SIGNAL_HANDLER_INSTALLED.call_once(|| unsafe {
|
2019-03-03 01:00:05 +00:00
|
|
|
crate::platform::install_signal_handler();
|
2019-08-09 18:32:14 +00:00
|
|
|
});*/
|
2019-03-03 01:00:05 +00:00
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
if res != LLVMResult::OK {
|
|
|
|
panic!("failed to load object")
|
|
|
|
}
|
2019-02-26 02:07:22 +00:00
|
|
|
|
2019-04-19 20:54:48 +00:00
|
|
|
let buffer = Arc::new(Buffer::LlvmMemory(memory_buffer));
|
|
|
|
|
2019-07-18 18:02:15 +00:00
|
|
|
let raw_stackmap = unsafe {
|
|
|
|
::std::slice::from_raw_parts(
|
|
|
|
llvm_backend_get_stack_map_ptr(module),
|
|
|
|
llvm_backend_get_stack_map_size(module),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if raw_stackmap.len() > 0 {
|
2019-07-22 18:55:43 +00:00
|
|
|
let map = stackmap::StackMap::parse(raw_stackmap).unwrap();
|
|
|
|
|
|
|
|
let (code_ptr, code_size) = unsafe {
|
|
|
|
(
|
|
|
|
llvm_backend_get_code_ptr(module),
|
|
|
|
llvm_backend_get_code_size(module),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let mut msm = ModuleStateMap {
|
|
|
|
local_functions: Default::default(),
|
|
|
|
total_size: code_size,
|
|
|
|
};
|
2019-07-24 18:44:28 +00:00
|
|
|
|
2019-08-09 19:23:29 +00:00
|
|
|
let num_local_functions =
|
|
|
|
module_info.func_assoc.len() - module_info.imported_functions.len();
|
|
|
|
let mut local_func_id_to_addr: Vec<usize> = Vec::with_capacity(num_local_functions);
|
2019-07-26 18:50:49 +00:00
|
|
|
|
|
|
|
// All local functions.
|
|
|
|
for index in module_info.imported_functions.len()..module_info.func_assoc.len() {
|
|
|
|
let name = if cfg!(target_os = "macos") {
|
|
|
|
format!("_fn{}", index)
|
|
|
|
} else {
|
|
|
|
format!("fn{}", index)
|
|
|
|
};
|
|
|
|
|
|
|
|
let c_str = CString::new(name).unwrap();
|
|
|
|
let ptr = unsafe { get_func_symbol(module, c_str.as_ptr()) };
|
|
|
|
|
|
|
|
assert!(!ptr.is_null());
|
|
|
|
local_func_id_to_addr.push(ptr as usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut addr_to_size_record: BTreeMap<usize, &StkSizeRecord> = BTreeMap::new();
|
|
|
|
|
|
|
|
for record in &map.stk_size_records {
|
|
|
|
addr_to_size_record.insert(record.function_address as usize, record);
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:44:28 +00:00
|
|
|
let mut map_records: BTreeMap<usize, &StkMapRecord> = BTreeMap::new();
|
2019-07-26 18:50:49 +00:00
|
|
|
|
2019-07-24 18:44:28 +00:00
|
|
|
for r in &map.stk_map_records {
|
|
|
|
map_records.insert(r.patchpoint_id as usize, r);
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:25:15 +00:00
|
|
|
for ((start_id, start_entry), (end_id, end_entry)) in stackmaps
|
|
|
|
.entries
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.step_by(2)
|
|
|
|
.zip(stackmaps.entries.iter().enumerate().skip(1).step_by(2))
|
|
|
|
{
|
|
|
|
if let Some(map_record) = map_records.get(&start_id) {
|
|
|
|
assert_eq!(start_id, map_record.patchpoint_id as usize);
|
|
|
|
assert!(start_entry.is_start);
|
|
|
|
assert!(!end_entry.is_start);
|
|
|
|
|
|
|
|
let end_record = map_records.get(&end_id);
|
|
|
|
|
|
|
|
let addr = local_func_id_to_addr[start_entry.local_function_id];
|
|
|
|
let size_record = *addr_to_size_record
|
|
|
|
.get(&addr)
|
|
|
|
.expect("size_record not found");
|
|
|
|
|
|
|
|
start_entry.populate_msm(
|
2019-08-01 15:28:39 +00:00
|
|
|
module_info,
|
2019-07-22 18:55:43 +00:00
|
|
|
code_ptr as usize,
|
|
|
|
&map,
|
|
|
|
size_record,
|
|
|
|
map_record,
|
2019-07-30 14:25:15 +00:00
|
|
|
end_record.map(|x| (end_entry, *x)),
|
2019-07-22 18:55:43 +00:00
|
|
|
&mut msm,
|
|
|
|
);
|
2019-07-26 18:50:49 +00:00
|
|
|
} else {
|
|
|
|
// TODO: optimized out?
|
2019-07-22 18:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-26 18:50:49 +00:00
|
|
|
|
2019-08-08 20:26:17 +00:00
|
|
|
let code_ptr = unsafe { llvm_backend_get_code_ptr(module) } as usize;
|
|
|
|
let code_len = unsafe { llvm_backend_get_code_size(module) } as usize;
|
|
|
|
|
|
|
|
let local_func_id_to_offset: Vec<usize> = local_func_id_to_addr
|
|
|
|
.iter()
|
|
|
|
.map(|&x| {
|
|
|
|
assert!(x >= code_ptr && x < code_ptr + code_len);
|
|
|
|
x - code_ptr
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-07-26 18:50:49 +00:00
|
|
|
//println!("MSM: {:?}", msm);
|
|
|
|
|
|
|
|
(
|
|
|
|
Self {
|
|
|
|
module,
|
|
|
|
buffer: Arc::clone(&buffer),
|
|
|
|
msm: Some(msm),
|
2019-08-08 20:26:17 +00:00
|
|
|
local_func_id_to_offset,
|
2019-07-26 18:50:49 +00:00
|
|
|
},
|
|
|
|
LLVMCache { buffer },
|
|
|
|
)
|
2019-07-18 18:02:15 +00:00
|
|
|
} else {
|
|
|
|
eprintln!("WARNING: No stack map");
|
2019-07-26 18:50:49 +00:00
|
|
|
(
|
|
|
|
Self {
|
|
|
|
module,
|
|
|
|
buffer: Arc::clone(&buffer),
|
|
|
|
msm: None,
|
2019-08-08 20:26:17 +00:00
|
|
|
local_func_id_to_offset: vec![],
|
2019-07-26 18:50:49 +00:00
|
|
|
},
|
|
|
|
LLVMCache { buffer },
|
|
|
|
)
|
2019-07-18 18:02:15 +00:00
|
|
|
}
|
2019-04-19 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn from_buffer(memory: Memory) -> Result<(Self, LLVMCache), String> {
|
|
|
|
let callbacks = get_callbacks();
|
|
|
|
let mut module: *mut LLVMModule = ptr::null_mut();
|
|
|
|
|
2019-04-21 01:37:39 +00:00
|
|
|
let slice = memory.as_slice();
|
2019-04-19 20:54:48 +00:00
|
|
|
|
|
|
|
let res = module_load(slice.as_ptr(), slice.len(), callbacks, &mut module);
|
|
|
|
|
|
|
|
if res != LLVMResult::OK {
|
|
|
|
return Err("failed to load object".to_string());
|
2019-04-12 17:05:50 +00:00
|
|
|
}
|
2019-04-19 20:54:48 +00:00
|
|
|
|
|
|
|
static SIGNAL_HANDLER_INSTALLED: Once = Once::new();
|
|
|
|
|
2019-04-21 01:37:39 +00:00
|
|
|
SIGNAL_HANDLER_INSTALLED.call_once(|| {
|
2019-04-19 20:54:48 +00:00
|
|
|
crate::platform::install_signal_handler();
|
|
|
|
});
|
|
|
|
|
|
|
|
let buffer = Arc::new(Buffer::Memory(memory));
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
|
|
|
module,
|
|
|
|
buffer: Arc::clone(&buffer),
|
2019-07-18 18:02:15 +00:00
|
|
|
msm: None,
|
2019-08-08 20:26:17 +00:00
|
|
|
local_func_id_to_offset: vec![],
|
2019-04-19 20:54:48 +00:00
|
|
|
},
|
|
|
|
LLVMCache { buffer },
|
|
|
|
))
|
2019-02-16 00:02:20 +00:00
|
|
|
}
|
2019-04-12 17:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LLVMBackend {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { module_delete(self.module) }
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 00:02:20 +00:00
|
|
|
|
2019-04-12 17:05:50 +00:00
|
|
|
impl RunnableModule for LLVMBackend {
|
|
|
|
fn get_func(
|
2019-02-26 02:07:22 +00:00
|
|
|
&self,
|
2019-02-28 20:31:39 +00:00
|
|
|
info: &ModuleInfo,
|
2019-02-26 02:07:22 +00:00
|
|
|
local_func_index: LocalFuncIndex,
|
|
|
|
) -> Option<NonNull<vm::Func>> {
|
2019-03-04 22:41:56 +00:00
|
|
|
let index = info.imported_functions.len() + local_func_index.index();
|
2019-02-28 20:31:39 +00:00
|
|
|
let name = if cfg!(target_os = "macos") {
|
2019-02-26 02:07:22 +00:00
|
|
|
format!("_fn{}", index)
|
|
|
|
} else {
|
|
|
|
format!("fn{}", index)
|
|
|
|
};
|
|
|
|
|
2019-02-28 20:31:39 +00:00
|
|
|
let c_str = CString::new(name).ok()?;
|
|
|
|
let ptr = unsafe { get_func_symbol(self.module, c_str.as_ptr()) };
|
|
|
|
|
|
|
|
NonNull::new(ptr as _)
|
|
|
|
}
|
2019-03-01 23:48:43 +00:00
|
|
|
|
2019-04-12 17:05:50 +00:00
|
|
|
fn get_trampoline(&self, _: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm> {
|
2019-04-09 22:53:01 +00:00
|
|
|
let trampoline: unsafe extern "C" fn(
|
|
|
|
*mut vm::Ctx,
|
|
|
|
NonNull<vm::Func>,
|
|
|
|
*const u64,
|
|
|
|
*mut u64,
|
|
|
|
) = unsafe {
|
|
|
|
let name = if cfg!(target_os = "macos") {
|
|
|
|
format!("_trmp{}", sig_index.index())
|
|
|
|
} else {
|
|
|
|
format!("trmp{}", sig_index.index())
|
|
|
|
};
|
|
|
|
|
|
|
|
let c_str = CString::new(name).unwrap();
|
|
|
|
let symbol = get_func_symbol(self.module, c_str.as_ptr());
|
|
|
|
assert!(!symbol.is_null());
|
|
|
|
|
|
|
|
mem::transmute(symbol)
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(unsafe { Wasm::from_raw_parts(trampoline, invoke_trampoline, None) })
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:02:15 +00:00
|
|
|
fn get_code(&self) -> Option<&[u8]> {
|
|
|
|
Some(unsafe {
|
|
|
|
::std::slice::from_raw_parts(
|
|
|
|
llvm_backend_get_code_ptr(self.module),
|
|
|
|
llvm_backend_get_code_size(self.module),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:26:17 +00:00
|
|
|
fn get_local_function_offsets(&self) -> Option<Vec<usize>> {
|
|
|
|
Some(self.local_func_id_to_offset.clone())
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:02:15 +00:00
|
|
|
fn get_module_state_map(&self) -> Option<ModuleStateMap> {
|
|
|
|
self.msm.clone()
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:17:10 +00:00
|
|
|
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> ! {
|
2019-04-11 19:13:36 +00:00
|
|
|
throw_any(Box::leak(data))
|
2019-03-01 23:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 20:54:48 +00:00
|
|
|
unsafe impl Send for LLVMCache {}
|
|
|
|
unsafe impl Sync for LLVMCache {}
|
|
|
|
|
|
|
|
pub struct LLVMCache {
|
|
|
|
buffer: Arc<Buffer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheGen for LLVMCache {
|
|
|
|
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), CacheError> {
|
|
|
|
let mut memory = Memory::with_size_protect(self.buffer.len(), Protect::ReadWrite)
|
|
|
|
.map_err(CacheError::SerializeError)?;
|
|
|
|
|
|
|
|
let buffer = self.buffer.deref();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
memory.as_slice_mut()[..buffer.len()].copy_from_slice(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(([].as_ref().into(), memory))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:20 +00:00
|
|
|
#[cfg(feature = "disasm")]
|
2019-02-28 20:31:39 +00:00
|
|
|
unsafe fn disass_ptr(ptr: *const u8, size: usize, inst_count: usize) {
|
|
|
|
use capstone::arch::BuildsCapstone;
|
|
|
|
let mut cs = capstone::Capstone::new() // Call builder-pattern
|
|
|
|
.x86() // X86 architecture
|
|
|
|
.mode(capstone::arch::x86::ArchMode::Mode64) // 64-bit mode
|
|
|
|
.detail(true) // Generate extra instruction details
|
|
|
|
.build()
|
|
|
|
.expect("Failed to create Capstone object");
|
|
|
|
|
|
|
|
// Get disassembled instructions
|
|
|
|
let insns = cs
|
|
|
|
.disasm_count(
|
|
|
|
std::slice::from_raw_parts(ptr, size),
|
|
|
|
ptr as u64,
|
|
|
|
inst_count,
|
|
|
|
)
|
|
|
|
.expect("Failed to disassemble");
|
|
|
|
|
|
|
|
println!("count = {}", insns.len());
|
|
|
|
for insn in insns.iter() {
|
|
|
|
println!(
|
|
|
|
"0x{:x}: {:6} {}",
|
|
|
|
insn.address(),
|
|
|
|
insn.mnemonic().unwrap_or(""),
|
|
|
|
insn.op_str().unwrap_or("")
|
|
|
|
);
|
2019-02-16 00:02:20 +00:00
|
|
|
}
|
2019-02-26 02:07:22 +00:00
|
|
|
}
|