Add EmscriptenData to instance and update usages

This commit is contained in:
Brandon Fish
2019-01-23 23:49:34 -06:00
parent 82e7ab6394
commit 99bc454c5b
5 changed files with 132 additions and 146 deletions

View File

@ -1,8 +1,10 @@
use wasmer_runtime_core::{module::Module, vm::Ctx};
//use wasmer_runtime_core::Instance;
use super::env;
use super::env::get_emscripten_data;
use libc::stat;
use std::ffi::CStr;
use std::mem::size_of;
use std::os::raw::c_char;
use std::slice;
/// We check if a provided module is an Emscripten generated one
@ -15,8 +17,8 @@ pub fn is_emscripten_module(module: &Module) -> bool {
false
}
pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, vmctx: &mut Ctx) -> u32 {
let buf_addr = vmctx.memory(0)[buf as usize] as *mut c_char;
pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, ctx: &mut Ctx) -> u32 {
let buf_addr = ctx.memory(0)[buf as usize] as *mut c_char;
for i in 0..max {
*buf_addr.add(i as _) = *string.add(i as _);
@ -26,11 +28,11 @@ pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, vmctx: &mu
}
/// This function expects nullbyte to be appended.
pub unsafe fn copy_cstr_into_wasm(vmctx: &mut Ctx, cstr: *const c_char) -> u32 {
pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, cstr: *const c_char) -> u32 {
let s = CStr::from_ptr(cstr).to_str().unwrap();
let cstr_len = s.len();
let space_offset = env::call_malloc((cstr_len as i32) + 1, vmctx);
let raw_memory = vmctx.memory(0)[space_offset as usize] as *mut u8;
let space_offset = env::call_malloc((cstr_len as i32) + 1, ctx);
let raw_memory = ctx.memory(0)[space_offset as usize] as *mut u8;
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
for (byte, loc) in s.bytes().zip(slice.iter_mut()) {
@ -44,20 +46,16 @@ pub unsafe fn copy_cstr_into_wasm(vmctx: &mut Ctx, cstr: *const c_char) -> u32 {
space_offset
}
pub unsafe fn allocate_on_stack<'a, T: Copy>(count: u32, vmctx: &'a Ctx) -> (u32, &'a mut [T]) {
unimplemented!("allocate_on_stack not implemented")
// let offset = (instance.emscripten_data().as_ref().unwrap().stack_alloc)(
// count * (size_of::<T>() as u32),
// vmctx,
// );
// let addr = vmctx.memory(0)[offset as usize] as *mut T;
// let slice = slice::from_raw_parts_mut(addr, count as usize);
//
// (offset, slice)
pub unsafe fn allocate_on_stack<'a, T: Copy>(count: u32, ctx: &'a mut Ctx) -> (u32, &'a mut [T]) {
let offset = (get_emscripten_data(ctx).stack_alloc)(count * (size_of::<T>() as u32), ctx);
let addr = ctx.memory(0)[offset as usize] as *mut T;
let slice = slice::from_raw_parts_mut(addr, count as usize);
(offset, slice)
}
pub unsafe fn allocate_cstr_on_stack<'a>(s: &str, vmctx: &'a Ctx) -> (u32, &'a [u8]) {
let (offset, slice) = allocate_on_stack((s.len() + 1) as u32, vmctx);
pub unsafe fn allocate_cstr_on_stack<'a>(s: &str, ctx: &'a mut Ctx) -> (u32, &'a [u8]) {
let (offset, slice) = allocate_on_stack((s.len() + 1) as u32, ctx);
use std::iter;
for (byte, loc) in s.bytes().chain(iter::once(0)).zip(slice.iter_mut()) {
@ -67,7 +65,7 @@ pub unsafe fn allocate_cstr_on_stack<'a>(s: &str, vmctx: &'a Ctx) -> (u32, &'a [
(offset, slice)
}
pub unsafe fn copy_terminated_array_of_cstrs(_vmctx: &mut Ctx, cstrs: *mut *mut c_char) -> u32 {
pub unsafe fn copy_terminated_array_of_cstrs(_ctx: &mut Ctx, cstrs: *mut *mut c_char) -> u32 {
let total_num = {
let mut ptr = cstrs;
let mut counter = 0;
@ -105,8 +103,8 @@ pub struct GuestStat {
}
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn copy_stat_into_wasm(vmctx: &mut Ctx, buf: u32, stat: &stat) {
let stat_ptr = vmctx.memory(0)[buf as usize] as *mut GuestStat;
pub unsafe fn copy_stat_into_wasm(ctx: &mut Ctx, buf: u32, stat: &stat) {
let stat_ptr = ctx.memory(0)[buf as usize] as *mut GuestStat;
(*stat_ptr).st_dev = stat.st_dev as _;
(*stat_ptr).__st_dev_padding = 0;
(*stat_ptr).__st_ino_truncated = stat.st_ino as _;