This commit is contained in:
Wolfgang Silbermayr
2022-08-04 20:39:13 +02:00
parent 89bba319a9
commit 9c7b4343ea
27 changed files with 193 additions and 176 deletions

View File

@@ -75,7 +75,8 @@ pub fn ___build_environment(mut ctx: FunctionEnvMut<EmEnv>, environ: c_int) {
debug!("emscripten::___build_environment {}", environ);
const MAX_ENV_VALUES: u32 = 64;
const TOTAL_ENV_SIZE: u32 = 1024;
let environment = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), environ) as *mut c_int;
let environment =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), environ) as *mut c_int;
let (mut pool_offset, env_ptr, mut pool_ptr) = unsafe {
let (pool_offset, _pool_slice): (u32, &mut [u8]) =
allocate_on_stack(&mut ctx, TOTAL_ENV_SIZE as u32);
@@ -136,7 +137,8 @@ pub fn _pathconf(ctx: FunctionEnvMut<EmEnv>, path_addr: c_int, name: c_int) -> c
"emscripten::_pathconf {} {} - UNIMPLEMENTED",
path_addr, name
);
let _path = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), path_addr) as *const c_char;
let _path =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), path_addr) as *const c_char;
match name {
0 => 32000,
1 | 2 | 3 => 255,

View File

@@ -17,7 +17,8 @@ use wasmer::{FunctionEnvMut, WasmPtr};
pub fn _getenv(mut ctx: FunctionEnvMut<EmEnv>, name: i32) -> u32 {
debug!("emscripten::_getenv");
let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
let name_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) });
@@ -33,8 +34,10 @@ pub fn _getenv(mut ctx: FunctionEnvMut<EmEnv>, name: i32) -> u32 {
pub fn _setenv(ctx: FunctionEnvMut<EmEnv>, name: c_int, value: c_int, overwrite: c_int) -> c_int {
debug!("emscripten::_setenv");
let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
let value_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), value) as *const c_char;
let name_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
let value_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), value) as *const c_char;
debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) });
debug!("=> value({:?})", unsafe { CStr::from_ptr(value_addr) });
@@ -46,7 +49,8 @@ pub fn _setenv(ctx: FunctionEnvMut<EmEnv>, name: c_int, value: c_int, overwrite:
pub fn _putenv(ctx: FunctionEnvMut<EmEnv>, name: c_int) -> c_int {
debug!("emscripten::_putenv");
let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
let name_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) });
@@ -57,7 +61,8 @@ pub fn _putenv(ctx: FunctionEnvMut<EmEnv>, name: c_int) -> c_int {
pub fn _unsetenv(ctx: FunctionEnvMut<EmEnv>, name: c_int) -> c_int {
debug!("emscripten::_unsetenv");
let name_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
let name_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), name) as *const c_char;
debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) });
@@ -155,9 +160,7 @@ pub fn _gai_strerror(mut ctx: FunctionEnvMut<EmEnv>, ecode: i32) -> i32 {
let string_on_guest: WasmPtr<c_char> = call_malloc_with_cast(&mut ctx, bytes.len() as _);
let memory = ctx.data().memory_view(0, &ctx);
let writer = string_on_guest
.slice(&memory, bytes.len() as _)
.unwrap();
let writer = string_on_guest.slice(&memory, bytes.len() as _).unwrap();
for (i, byte) in bytes.iter().enumerate() {
writer.index(i as u64).write(*byte as _).unwrap();
}
@@ -192,7 +195,10 @@ pub fn _getaddrinfo(
let hints = if hints_ptr.is_null() {
None
} else {
let hints_guest = hints_ptr.deref(&ctx.data().memory_view(0, &ctx)).read().unwrap();
let hints_guest = hints_ptr
.deref(&ctx.data().memory_view(0, &ctx))
.read()
.unwrap();
Some(addrinfo {
ai_flags: hints_guest.ai_flags,
ai_family: hints_guest.ai_family,

View File

@@ -73,7 +73,7 @@ pub fn _getpwnam(mut ctx: FunctionEnvMut<EmEnv>, name_ptr: c_int) -> c_int {
debug!("emscripten::_getpwnam {}", name_ptr);
#[cfg(not(feature = "debug"))]
let _ = name_ptr;
#[repr(C)]
struct GuestPasswd {
pw_name: u32,
@@ -108,7 +108,7 @@ pub fn _getgrnam(mut ctx: FunctionEnvMut<EmEnv>, name_ptr: c_int) -> c_int {
debug!("emscripten::_getgrnam {}", name_ptr);
#[cfg(not(feature = "debug"))]
let _ = name_ptr;
#[repr(C)]
struct GuestGroup {
gr_name: u32,

View File

@@ -11,7 +11,8 @@ pub fn _emscripten_memcpy_big(ctx: FunctionEnvMut<EmEnv>, dest: u32, src: u32, l
"emscripten::_emscripten_memcpy_big {}, {}, {}",
dest, src, len
);
let dest_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), dest) as *mut c_void;
let dest_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), dest) as *mut c_void;
let src_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), src) as *mut c_void;
unsafe {
memcpy(dest_addr, src_addr, len as size_t);
@@ -90,7 +91,7 @@ pub fn sbrk(mut ctx: FunctionEnvMut<EmEnv>, increment: i32) -> i32 {
.unwrap()
.globals
.dynamictop_ptr;
let dynamictop_ptr = WasmPtr::<i32>::new(top_ptr);
let old_dynamic_top = {
let memory = ctx.data().memory_view(0, &ctx);
@@ -118,7 +119,7 @@ pub fn sbrk(mut ctx: FunctionEnvMut<EmEnv>, increment: i32) -> i32 {
}
// re-borrow the top ptr
let memory = ctx.data().memory_view(0, &ctx);
let dynamictop_ptr = WasmPtr::<i32>::new(top_ptr).deref( &memory);
let dynamictop_ptr = WasmPtr::<i32>::new(top_ptr).deref(&memory);
dynamictop_ptr.write(new_dynamic_top).unwrap();
old_dynamic_top as _
}

View File

@@ -80,7 +80,8 @@ pub fn ___syscall4(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: VarAr
let buf: i32 = varargs.get(&ctx);
let count: i32 = varargs.get(&ctx);
debug!("=> fd: {}, buf: {}, count: {}", fd, buf, count);
let buf_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *const c_void;
let buf_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *const c_void;
unsafe { write(fd, buf_addr, count as _) as i32 }
}
@@ -384,7 +385,8 @@ pub fn ___syscall192(mut ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs:
// ENOMEM
return -12;
}
let real_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr) as *const u8;
let real_ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr) as *const u8;
env::call_memset(&mut ctx, ptr, 0, len);
for i in 0..(len as usize) {
unsafe {
@@ -450,9 +452,10 @@ pub fn ___syscall145(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: Var
let guest_iov_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (iov + i * 8))
as *mut GuestIovec;
let iov_base =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (*guest_iov_addr).iov_base)
as *mut c_void;
let iov_base = emscripten_memory_pointer!(
ctx.data().memory_view(0, &ctx),
(*guest_iov_addr).iov_base
) as *mut c_void;
let iov_len = (*guest_iov_addr).iov_len as _;
// debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len);
let curr = read(fd, iov_base, iov_len);
@@ -488,9 +491,10 @@ pub fn ___syscall146(ctx: FunctionEnvMut<EmEnv>, _which: i32, mut varargs: VarAr
let guest_iov_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (iov + i * 8))
as *mut GuestIovec;
let iov_base =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), (*guest_iov_addr).iov_base)
as *const c_void;
let iov_base = emscripten_memory_pointer!(
ctx.data().memory_view(0, &ctx),
(*guest_iov_addr).iov_base
) as *const c_void;
let iov_len = (*guest_iov_addr).iov_len as _;
// debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len);
let curr = write(fd, iov_base, iov_len);
@@ -518,7 +522,8 @@ pub fn ___syscall191(ctx: FunctionEnvMut<EmEnv>, _which: i32, mut varargs: VarAr
_resource
);
let rlim_emptr: i32 = varargs.get(&ctx);
let rlim_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rlim_emptr) as *mut u8;
let rlim_ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rlim_emptr) as *mut u8;
let rlim = unsafe { slice::from_raw_parts_mut(rlim_ptr, 16) };
// set all to RLIM_INIFINTY
@@ -722,7 +727,8 @@ pub fn ___syscall340(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: Var
if old_limit != 0 {
// just report no limits
let buf_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), old_limit) as *mut u8;
let buf_ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), old_limit) as *mut u8;
let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, 16) };
LittleEndian::write_i64(&mut *buf, val);

View File

@@ -203,7 +203,8 @@ pub fn ___syscall77(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: VarA
let resource: c_int = varargs.get(&ctx);
let rusage_ptr: c_int = varargs.get(&ctx);
#[allow(clippy::cast_ptr_alignment)]
let rusage = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage_ptr) as *mut rusage;
let rusage =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage_ptr) as *mut rusage;
assert_eq!(8, mem::align_of_val(&rusage));
unsafe { getrusage(resource, rusage) }
}
@@ -779,8 +780,7 @@ pub fn ___syscall102(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: Var
let value: u32 = socket_varargs.get(&ctx);
let option_len: u32 = socket_varargs.get(&ctx);
let value_addr = emscripten_memory_pointer!(memory, value) as _;
let option_len_addr =
emscripten_memory_pointer!(memory, option_len) as *mut socklen_t;
let option_len_addr = emscripten_memory_pointer!(memory, option_len) as *mut socklen_t;
unsafe { getsockopt(socket, level, name, value_addr, option_len_addr) }
}
16 => {
@@ -927,9 +927,11 @@ pub fn ___syscall114(ctx: FunctionEnvMut<EmEnv>, _which: c_int, mut varargs: Var
let status: u32 = varargs.get(&ctx);
let options: c_int = varargs.get(&ctx);
let rusage: u32 = varargs.get(&ctx);
let status_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), status) as *mut c_int;
let status_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), status) as *mut c_int;
let rusage_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage) as *mut rusage;
let rusage_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), rusage) as *mut rusage;
let res = unsafe { wait4(pid, status_addr, options, rusage_addr) };
debug!(
"=> pid: {}, status: {:?}, options: {}, rusage: {:?} = pid: {}",

View File

@@ -28,7 +28,7 @@ pub fn ___syscall5(mut ctx: FunctionEnvMut<EmEnv>, which: c_int, mut varargs: Va
let flags: i32 = varargs.get(&ctx);
let mode: u32 = varargs.get(&ctx);
let path_str = unsafe { std::ffi::CStr::from_ptr(real_path).to_str().unwrap() };
match path_str {
"/dev/urandom" => {
// create a fake urandom file for windows, super hacky

View File

@@ -178,7 +178,8 @@ pub fn _tvset(mut _ctx: FunctionEnvMut<EmEnv>) {
/// formats time as a C string
#[allow(clippy::cast_ptr_alignment)]
unsafe fn fmt_time(ctx: FunctionEnvMut<EmEnv>, time: u32) -> *const c_char {
let date = &*(emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time) as *mut guest_tm);
let date =
&*(emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time) as *mut guest_tm);
let days = vec!["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let months = vec![
@@ -241,15 +242,17 @@ pub fn _localtime(mut ctx: FunctionEnvMut<EmEnv>, time_p: u32) -> c_int {
// https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r
let timespec = unsafe {
let time_p_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64;
let time_p_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64;
let seconds = *time_p_addr;
time::OffsetDateTime::from_unix_timestamp(seconds)
};
unsafe {
let tm_struct_offset = env::call_malloc(&mut ctx, mem::size_of::<guest_tm>() as _);
let tm_struct_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_struct_offset)
as *mut guest_tm;
let tm_struct_ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_struct_offset)
as *mut guest_tm;
// debug!(
// ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}",
// result_tm.tm_sec, result_tm.tm_min, result_tm.tm_hour, result_tm.tm_mday,
@@ -279,7 +282,8 @@ pub fn _localtime_r(ctx: FunctionEnvMut<EmEnv>, time_p: u32, result: u32) -> c_i
// https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r
unsafe {
let seconds = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *const i32;
let seconds =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *const i32;
let timespec = time::OffsetDateTime::from_unix_timestamp_nanos(*seconds as _);
// debug!(
@@ -313,7 +317,8 @@ pub fn _time(ctx: FunctionEnvMut<EmEnv>, time_p: u32) -> i32 {
debug!("emscripten::_time {}", time_p);
unsafe {
let time_p_addr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64;
let time_p_addr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), time_p) as *mut i64;
libc_time(time_p_addr) as i32 // TODO review i64
}
}
@@ -403,7 +408,8 @@ pub fn _strftime(
#[allow(clippy::cast_ptr_alignment)]
let s = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), s_ptr) as *mut c_char;
#[allow(clippy::cast_ptr_alignment)]
let format = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), format_ptr) as *const c_char;
let format =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), format_ptr) as *const c_char;
#[allow(clippy::cast_ptr_alignment)]
let tm = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), tm_ptr) as *const guest_tm;

View File

@@ -8,7 +8,7 @@ use std::mem::size_of;
use std::os::raw::c_char;
use std::path::PathBuf;
use std::slice;
use wasmer::{FunctionEnvMut, GlobalInit, Module, Pages, WasmPtr, MemoryView};
use wasmer::{FunctionEnvMut, GlobalInit, MemoryView, Module, Pages, WasmPtr};
/// We check if a provided module is an Emscripten generated one
pub fn is_emscripten_module(module: &Module) -> bool {
@@ -204,7 +204,8 @@ pub struct GuestStat {
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn copy_stat_into_wasm(ctx: FunctionEnvMut<EmEnv>, buf: u32, stat: &stat) {
let stat_ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) as *mut GuestStat;
let stat_ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), buf) 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 _;

View File

@@ -21,7 +21,8 @@ impl VarArgs {
// pub fn getStr<'a>(&mut self, ctx: &mut Ctx) -> &'a CStr {
pub fn get_str(&mut self, ctx: &FunctionEnvMut<EmEnv>) -> *const c_char {
let ptr_addr: u32 = self.get(ctx);
let ptr = emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr_addr) as *const c_char;
let ptr =
emscripten_memory_pointer!(ctx.data().memory_view(0, &ctx), ptr_addr) as *const c_char;
ptr
// unsafe { CStr::from_ptr(ptr) }
}