mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-24 22:21:32 +00:00
Call malloc, memalign, memset from exports instead of emscripten_data
This commit is contained in:
@ -9,7 +9,8 @@ use std::mem;
|
|||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
|
|
||||||
use super::utils::{allocate_on_stack, copy_cstr_into_wasm, copy_terminated_array_of_cstrs};
|
use super::utils::{allocate_on_stack, copy_cstr_into_wasm, copy_terminated_array_of_cstrs};
|
||||||
use crate::runtime::Instance;
|
use crate::apis::emscripten::env;
|
||||||
|
use crate::runtime::{types::Value, Instance};
|
||||||
use crate::webassembly::instance::EmscriptenData;
|
use crate::webassembly::instance::EmscriptenData;
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
@ -95,10 +96,7 @@ pub extern "C" fn _getpwnam(name_ptr: c_int, instance: &mut Instance) -> c_int {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let passwd = &*libc_getpwnam(name.as_ptr());
|
let passwd = &*libc_getpwnam(name.as_ptr());
|
||||||
let passwd_struct_offset = (instance.emscripten_data().as_ref().unwrap().malloc)(
|
let passwd_struct_offset = call_malloc(mem::size_of::<GuestPasswd>() as _, instance);
|
||||||
mem::size_of::<GuestPasswd>() as _,
|
|
||||||
instance,
|
|
||||||
);
|
|
||||||
|
|
||||||
let passwd_struct_ptr =
|
let passwd_struct_ptr =
|
||||||
instance.memory_offset_addr(0, passwd_struct_offset as _) as *mut GuestPasswd;
|
instance.memory_offset_addr(0, passwd_struct_offset as _) as *mut GuestPasswd;
|
||||||
@ -132,10 +130,7 @@ pub extern "C" fn _getgrnam(name_ptr: c_int, instance: &mut Instance) -> c_int {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let group = &*libc_getgrnam(name.as_ptr());
|
let group = &*libc_getgrnam(name.as_ptr());
|
||||||
let group_struct_offset = (instance.emscripten_data().as_ref().unwrap().malloc)(
|
let group_struct_offset = call_malloc(mem::size_of::<GuestGroup>() as _, instance);
|
||||||
mem::size_of::<GuestGroup>() as _,
|
|
||||||
instance,
|
|
||||||
);
|
|
||||||
|
|
||||||
let group_struct_ptr =
|
let group_struct_ptr =
|
||||||
instance.memory_offset_addr(0, group_struct_offset as _) as *mut GuestGroup;
|
instance.memory_offset_addr(0, group_struct_offset as _) as *mut GuestGroup;
|
||||||
@ -148,6 +143,49 @@ pub extern "C" fn _getgrnam(name_ptr: c_int, instance: &mut Instance) -> c_int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn call_malloc(size: i32, instance: &mut Instance) -> u32 {
|
||||||
|
let ret = instance
|
||||||
|
.call("_malloc", &[Value::I32(size)])
|
||||||
|
.expect("_malloc call failed");
|
||||||
|
if let Some(Value::I32(ptr)) = ret {
|
||||||
|
ptr as u32
|
||||||
|
} else {
|
||||||
|
panic!("unexpected value from _malloc: {:?}", ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_memalign(alignment: u32, size: u32, instance: &mut Instance) -> u32 {
|
||||||
|
let ret = instance
|
||||||
|
.call(
|
||||||
|
"_memalign",
|
||||||
|
&[Value::I32(alignment as i32), Value::I32(size as i32)],
|
||||||
|
)
|
||||||
|
.expect("_memalign call failed");
|
||||||
|
if let Some(Value::I32(res)) = ret {
|
||||||
|
res as u32
|
||||||
|
} else {
|
||||||
|
panic!("unexpected value from _memalign {:?}", ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_memset(pointer: u32, value: i32, size: u32, instance: &mut Instance) -> u32 {
|
||||||
|
let ret = instance
|
||||||
|
.call(
|
||||||
|
"_memset",
|
||||||
|
&[
|
||||||
|
Value::I32(pointer as i32),
|
||||||
|
Value::I32(value),
|
||||||
|
Value::I32(size as i32),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.expect("_memset call failed");
|
||||||
|
if let Some(Value::I32(res)) = ret {
|
||||||
|
res as u32
|
||||||
|
} else {
|
||||||
|
panic!("unexpected value from _memset {:?}", ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub extern "C" fn _getpagesize() -> u32 {
|
pub extern "C" fn _getpagesize() -> u32 {
|
||||||
debug!("emscripten::_getpagesize");
|
debug!("emscripten::_getpagesize");
|
||||||
16384
|
16384
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use super::process::_abort;
|
use super::process::_abort;
|
||||||
|
use crate::apis::emscripten::env;
|
||||||
use crate::webassembly::Instance;
|
use crate::webassembly::Instance;
|
||||||
|
|
||||||
/// emscripten: ___cxa_allocate_exception
|
/// emscripten: ___cxa_allocate_exception
|
||||||
pub extern "C" fn ___cxa_allocate_exception(size: u32, instance: &mut Instance) -> u32 {
|
pub extern "C" fn ___cxa_allocate_exception(size: u32, instance: &mut Instance) -> u32 {
|
||||||
debug!("emscripten::___cxa_allocate_exception");
|
debug!("emscripten::___cxa_allocate_exception");
|
||||||
(instance.emscripten_data().as_ref().unwrap().malloc)(size as _, instance)
|
env::call_malloc(size as _, instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// emscripten: ___cxa_throw
|
/// emscripten: ___cxa_throw
|
||||||
|
@ -71,6 +71,7 @@ use libc::{
|
|||||||
TIOCGWINSZ,
|
TIOCGWINSZ,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::apis::emscripten::env;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
// use std::sys::fd::FileDesc;
|
// use std::sys::fd::FileDesc;
|
||||||
@ -645,17 +646,12 @@ pub extern "C" fn ___syscall192(
|
|||||||
addr, len, prot, flags, fd, off
|
addr, len, prot, flags, fd, off
|
||||||
);
|
);
|
||||||
|
|
||||||
let (memalign, memset) = {
|
|
||||||
let emscripten_data = &instance.emscripten_data().as_ref().unwrap();
|
|
||||||
(emscripten_data.memalign, emscripten_data.memset)
|
|
||||||
};
|
|
||||||
|
|
||||||
if fd == -1 {
|
if fd == -1 {
|
||||||
let ptr = memalign(16384, len, instance);
|
let ptr = env::call_memalign(16384, len, instance);
|
||||||
if ptr == 0 {
|
if ptr == 0 {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memset(ptr, 0, len, instance);
|
env::call_memset(ptr, 0, len, instance);
|
||||||
ptr as _
|
ptr as _
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
|
@ -5,6 +5,7 @@ use std::time::SystemTime;
|
|||||||
|
|
||||||
use time;
|
use time;
|
||||||
|
|
||||||
|
use crate::apis::emscripten::env;
|
||||||
use crate::webassembly::Instance;
|
use crate::webassembly::Instance;
|
||||||
|
|
||||||
/// emscripten: _gettimeofday
|
/// emscripten: _gettimeofday
|
||||||
@ -163,10 +164,7 @@ pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> c_int {
|
|||||||
let result_tm = time::at(timespec);
|
let result_tm = time::at(timespec);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let tm_struct_offset = (instance.emscripten_data().as_ref().unwrap().malloc)(
|
let tm_struct_offset = env::call_malloc(mem::size_of::<guest_tm>() as _, instance);
|
||||||
mem::size_of::<guest_tm>() as _,
|
|
||||||
instance,
|
|
||||||
);
|
|
||||||
let tm_struct_ptr = instance.memory_offset_addr(0, tm_struct_offset as _) as *mut guest_tm;
|
let tm_struct_ptr = instance.memory_offset_addr(0, tm_struct_offset as _) as *mut guest_tm;
|
||||||
// debug!(
|
// debug!(
|
||||||
// ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}",
|
// ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::runtime::{Instance, Module};
|
use crate::runtime::{Instance, Module};
|
||||||
//use crate::webassembly::Instance;
|
//use crate::webassembly::Instance;
|
||||||
|
use crate::apis::emscripten::env;
|
||||||
use byteorder::{ByteOrder, LittleEndian};
|
use byteorder::{ByteOrder, LittleEndian};
|
||||||
use libc::stat;
|
use libc::stat;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
@ -31,8 +32,7 @@ pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance:
|
|||||||
pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char) -> u32 {
|
pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char) -> u32 {
|
||||||
let s = CStr::from_ptr(cstr).to_str().unwrap();
|
let s = CStr::from_ptr(cstr).to_str().unwrap();
|
||||||
let cstr_len = s.len();
|
let cstr_len = s.len();
|
||||||
let space_offset =
|
let space_offset = env::call_malloc((cstr_len as i32) + 1, instance);
|
||||||
(instance.emscripten_data().as_ref().unwrap().malloc)((cstr_len as i32) + 1, instance);
|
|
||||||
let raw_memory = instance.memory_offset_addr(0, space_offset as _) as *mut u8;
|
let raw_memory = instance.memory_offset_addr(0, space_offset as _) as *mut u8;
|
||||||
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
|
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ pub fn get_isa() -> Box<isa::TargetIsa> {
|
|||||||
isa::lookup(triple!("x86_64")).unwrap().finish(flags)
|
isa::lookup(triple!("x86_64")).unwrap().finish(flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn store_module_arguments(path: &str, args: Vec<&str>, instance: &Instance) -> (u32, u32) {
|
fn store_module_arguments(path: &str, args: Vec<&str>, instance: &mut Instance) -> (u32, u32) {
|
||||||
let argc = args.len() + 1;
|
let argc = args.len() + 1;
|
||||||
|
|
||||||
let (argv_offset, argv_slice): (_, &mut [u32]) =
|
let (argv_offset, argv_slice): (_, &mut [u32]) =
|
||||||
|
Reference in New Issue
Block a user