From 8f345dc2843758c430fcbb976f793aeb23a73c2f Mon Sep 17 00:00:00 2001 From: Syrus Date: Sat, 18 May 2019 12:38:22 -0700 Subject: [PATCH 1/6] Updated imports required for jq. Added get_str method in VarArgs --- lib/emscripten/src/env/mod.rs | 17 +++++++++ lib/emscripten/src/lib.rs | 4 +++ lib/emscripten/src/math.rs | 5 +++ lib/emscripten/src/syscalls/mod.rs | 49 +++++++++++++++++--------- lib/emscripten/src/syscalls/unix.rs | 36 +++++++------------ lib/emscripten/src/syscalls/windows.rs | 10 +++--- lib/emscripten/src/time.rs | 14 +++++++- lib/emscripten/src/varargs.rs | 10 ++++++ 8 files changed, 97 insertions(+), 48 deletions(-) diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 897fb38f016..74f6caddc0e 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -112,3 +112,20 @@ pub fn ___assert_fail(_ctx: &mut Ctx, _a: c_int, _b: c_int, _c: c_int, _d: c_int // TODO: Implement like emscripten expects regarding memory/page size // TODO raise an error } + +pub fn _fpathconf(_ctx: &mut Ctx, _fildes: c_int, name: c_int) -> c_int { + debug!("emscripten::_fpathconf {} {}", _fildes, name); + match name { + 0 => 32000, + 1 | 2 | 3 => 255, + 4 | 5 | 16 | 17 | 18 => 4096, + 6 | 7 | 20 => 1, + 8 => 0, + 9 | 10 | 11 | 12 | 14 | 15 | 19 => -1, + 13 => 64, + _ => { + // ___setErrNo(22); + -1 + } + } +} diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 6b034051302..3970611bed6 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -547,6 +547,8 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject "_sysconf" => func!(crate::env::_sysconf), "_getaddrinfo" => func!(crate::env::_getaddrinfo), "_times" => func!(crate::env::_times), + "_pathconf" => func!(crate::env::_fpathconf), + "_fpathconf" => func!(crate::env::_fpathconf), // Syscalls "___syscall1" => func!(crate::syscalls::___syscall1), @@ -689,6 +691,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject "_asctime_r" => func!(crate::time::_asctime_r), "_localtime" => func!(crate::time::_localtime), "_time" => func!(crate::time::_time), + "_timegm" => func!(crate::time::_timegm), "_strftime" => func!(crate::time::_strftime), "_strftime_l" => func!(crate::time::_strftime_l), "_localtime_r" => func!(crate::time::_localtime_r), @@ -707,6 +710,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject "_llvm_exp2_f32" => func!(crate::math::_llvm_exp2_f32), "_llvm_exp2_f64" => func!(crate::math::_llvm_exp2_f64), "_llvm_trunc_f64" => func!(crate::math::_llvm_trunc_f64), + "_llvm_fma_f64" => func!(crate::math::_llvm_fma_f64), "_emscripten_random" => func!(crate::math::_emscripten_random), // Jump diff --git a/lib/emscripten/src/math.rs b/lib/emscripten/src/math.rs index 3256b49c53b..d266afe701e 100644 --- a/lib/emscripten/src/math.rs +++ b/lib/emscripten/src/math.rs @@ -49,6 +49,11 @@ pub fn _llvm_trunc_f64(_ctx: &mut Ctx, value: f64) -> f64 { value.trunc() } +pub fn _llvm_fma_f64(_ctx: &mut Ctx, value: f64, a: f64, b: f64) -> f64 { + debug!("emscripten::_llvm_fma_f64"); + value.mul_add(a, b) +} + pub fn _emscripten_random(_ctx: &mut Ctx) -> f64 { debug!("emscripten::_emscripten_random"); -1.0 diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index 5654a12995d..afffa2e4a57 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -38,6 +38,7 @@ use libc::{ // writev, stat, write, + // readlink, }; use wasmer_runtime_core::vm::Ctx; @@ -93,9 +94,8 @@ pub fn ___syscall6(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int // chdir pub fn ___syscall12(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall12 (chdir) {}", _which); - let path_addr: i32 = varargs.get(ctx); + let path_ptr = varargs.get_str(ctx); unsafe { - let path_ptr = emscripten_memory_pointer!(ctx.memory(0), path_addr) as *const i8; let _path = std::ffi::CStr::from_ptr(path_ptr); let ret = chdir(path_ptr); debug!("=> path: {:?}, ret: {}", _path, ret); @@ -122,10 +122,8 @@ pub fn ___syscall20(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { // rename pub fn ___syscall38(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall38 (rename)"); - let old_path_addr: u32 = varargs.get(ctx); - let new_path_addr: u32 = varargs.get(ctx); - let old_path = emscripten_memory_pointer!(ctx.memory(0), old_path_addr) as *const i8; - let new_path = emscripten_memory_pointer!(ctx.memory(0), new_path_addr) as *const i8; + let old_path = varargs.get_str(ctx); + let new_path = varargs.get_str(ctx); let result = unsafe { rename(old_path, new_path) }; debug!( "=> old_path: {}, new_path: {}, result: {}", @@ -139,8 +137,7 @@ pub fn ___syscall38(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { // rmdir pub fn ___syscall40(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall40 (rmdir)"); - let pathname: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; + let pathname_addr = varargs.get_str(ctx); unsafe { rmdir(pathname_addr) } } @@ -201,9 +198,30 @@ pub fn ___syscall75(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { -1 } -pub fn ___syscall85(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { - debug!("emscripten::___syscall85"); - -1 +// readlink +pub fn ___syscall85(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { + debug!("emscripten::___syscall85 (readlink)"); + let _path = varargs.get_str(ctx); + let buf = varargs.get_str(ctx); + // let buf_addr: i32 = varargs.get(ctx); + let buf_size: i32 = varargs.get(ctx); + + let fd = 3; + let ret = unsafe { read(fd, buf as _, buf_size as _) as i32 }; + debug!("=> buf: {}, buf_size: {}, return: {} ", unsafe { std::ffi::CStr::from_ptr(buf as _).to_str().unwrap() }, buf_size, ret); + + // let ret = unsafe { + // readlink(path, buf as _, buf_size as _) as i32 + // }; + // debug!("=> path: {}, buf: {}, buf_size: {}, return: {} ", + // unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }, + // unsafe { std::ffi::CStr::from_ptr(buf as _).to_str().unwrap() }, + // // std::ffi::CStr::from_ptr(buf).to_str().unwrap(), + // // buf, + // buf_size, + // ret); + + ret } pub fn ___syscall91(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { @@ -397,19 +415,16 @@ pub fn ___syscall199(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { // stat64 pub fn ___syscall195(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall195 (stat64) {}", _which); - let pathname: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(ctx); let buf: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; - unsafe { let mut _stat: stat = std::mem::zeroed(); let ret = stat(pathname_addr, &mut _stat); debug!( - "=> pathname: {}, buf: {}, path: {} = {}\nlast os error: {}", - pathname, - buf, + "=> pathname: {}, buf: {} = {}, last os error: {}", std::ffi::CStr::from_ptr(pathname_addr).to_str().unwrap(), + buf, ret, Error::last_os_error() ); diff --git a/lib/emscripten/src/syscalls/unix.rs b/lib/emscripten/src/syscalls/unix.rs index ec8b98f3f90..83a972dfa9e 100644 --- a/lib/emscripten/src/syscalls/unix.rs +++ b/lib/emscripten/src/syscalls/unix.rs @@ -104,19 +104,17 @@ const SO_NOSIGPIPE: c_int = 0; /// open pub fn ___syscall5(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall5 (open) {}", _which); - let pathname: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(ctx); let flags: i32 = varargs.get(ctx); let mode: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; let _path_str = unsafe { std::ffi::CStr::from_ptr(pathname_addr).to_str().unwrap() }; let fd = unsafe { open(pathname_addr, flags, mode) }; debug!( - "=> pathname: {}, flags: {}, mode: {} = fd: {}\npath: {}\nlast os error: {}", - pathname, + "=> path: {}, flags: {}, mode: {} = fd: {}, last os error: {}", + _path_str, flags, mode, fd, - _path_str, Error::last_os_error(), ); fd @@ -126,10 +124,8 @@ pub fn ___syscall5(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int pub fn ___syscall9(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall9 (link) {}", _which); - let oldname: c_int = varargs.get(ctx); - let newname: c_int = varargs.get(ctx); - let oldname_ptr = emscripten_memory_pointer!(ctx.memory(0), oldname) as *const i8; - let newname_ptr = emscripten_memory_pointer!(ctx.memory(0), newname) as *const i8; + let oldname_ptr = varargs.get_str(ctx); + let newname_ptr = varargs.get_str(ctx); let result = unsafe { link(oldname_ptr, newname_ptr) }; debug!( "=> oldname: {}, newname: {}, result: {}", @@ -156,10 +152,8 @@ pub fn ___syscall77(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int pub fn ___syscall83(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall83 (symlink) {}", _which); - let path1_ptr: c_int = varargs.get(ctx); - let path2_ptr: c_int = varargs.get(ctx); - let path1 = emscripten_memory_pointer!(ctx.memory(0), path1_ptr) as *mut i8; - let path2 = emscripten_memory_pointer!(ctx.memory(0), path2_ptr) as *mut i8; + let path1 = varargs.get_str(ctx); + let path2 = varargs.get_str(ctx); let result = unsafe { symlink(path1, path2) }; debug!( "=> path1: {}, path2: {}, result: {}", @@ -186,10 +180,9 @@ pub fn ___syscall194(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in /// lchown pub fn ___syscall198(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall198 (lchown) {}", _which); - let path: c_int = varargs.get(ctx); + let path_ptr = varargs.get_str(ctx); let uid: uid_t = varargs.get(ctx); let gid: gid_t = varargs.get(ctx); - let path_ptr = emscripten_memory_pointer!(ctx.memory(0), path) as *const i8; let result = unsafe { lchown(path_ptr, uid, gid) }; debug!( "=> path: {}, uid: {}, gid: {}, result: {}", @@ -222,12 +215,10 @@ pub fn ___syscall205(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in pub fn ___syscall212(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", _which); - let pathname: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(ctx); let owner: u32 = varargs.get(ctx); let group: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; - unsafe { chown(pathname_addr, owner, group) } } @@ -247,9 +238,8 @@ pub fn ___syscall219(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in /// access pub fn ___syscall33(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall33 (access) {}", _which); - let path_ptr: c_int = varargs.get(ctx); + let path = varargs.get_str(ctx); let amode: c_int = varargs.get(ctx); - let path = emscripten_memory_pointer!(ctx.memory(0), path_ptr) as *const i8; let result = unsafe { access(path, amode) }; debug!( "=> path: {}, amode: {}, result: {}", @@ -270,9 +260,8 @@ pub fn ___syscall34(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int // mkdir pub fn ___syscall39(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall39 (mkdir) {}", _which); - let pathname: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(ctx); let mode: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; unsafe { mkdir(pathname_addr, mode as _) } } @@ -771,9 +760,8 @@ pub fn ___syscall122(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in /// lstat64 pub fn ___syscall196(ctx: &mut Ctx, _which: i32, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall196 (lstat64) {}", _which); - let path_ptr: c_int = varargs.get(ctx); + let path = varargs.get_str(ctx); let buf_ptr: u32 = varargs.get(ctx); - let path = emscripten_memory_pointer!(ctx.memory(0), path_ptr) as *const i8; unsafe { let mut stat: stat = std::mem::zeroed(); diff --git a/lib/emscripten/src/syscalls/windows.rs b/lib/emscripten/src/syscalls/windows.rs index a7ac8c11c30..d74f3c55b24 100644 --- a/lib/emscripten/src/syscalls/windows.rs +++ b/lib/emscripten/src/syscalls/windows.rs @@ -18,10 +18,9 @@ pub fn ___syscall5(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall5 (open) {}", which); #[cfg(not(feature = "debug"))] let _ = which; - let pathname: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(ctx); let flags: i32 = varargs.get(ctx); let mode: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; let path_str = unsafe { std::ffi::CStr::from_ptr(pathname_addr).to_str().unwrap() }; match path_str { "/dev/urandom" => { @@ -43,7 +42,7 @@ pub fn ___syscall5(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { let fd = unsafe { open(raw_pointer_to_urandom_file, flags, mode) }; debug!( "=> pathname: {}, flags: {}, mode: {} = fd: {}", - pathname, flags, mode, fd + path_str, flags, mode, fd ); fd } @@ -51,7 +50,7 @@ pub fn ___syscall5(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { let fd = unsafe { open(pathname_addr, flags, mode) }; debug!( "=> pathname: {}, flags: {}, mode: {} = fd: {}\npath: {}", - pathname, flags, mode, fd, path_str + path_str, flags, mode, fd, path_str ); fd } @@ -95,8 +94,7 @@ pub fn ___syscall39(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int debug!("emscripten::___syscall39 (mkdir) {}", which); #[cfg(not(feature = "debug"))] let _ = which; - let pathname: u32 = varargs.get(ctx); - let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; + let pathname_addr = varargs.get_str(ctx); unsafe { mkdir(pathname_addr) } } diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index 32e7a61c639..eef9046f25d 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -4,7 +4,7 @@ use std::mem; use std::time::SystemTime; #[cfg(not(target_os = "windows"))] -use libc::{clockid_t, time as libc_time}; +use libc::{clockid_t, time as libc_time, timegm as libc_timegm}; #[cfg(target_os = "windows")] use libc::time_t; @@ -17,6 +17,8 @@ type clockid_t = c_int; extern "C" { #[link_name = "time"] pub fn libc_time(s: *const time_t) -> time_t; + #[link_name = "timegm"] + pub fn libc_timegm(s: *const time_t) -> time_t; } use time; @@ -298,6 +300,16 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 { } } +/// emscripten: _timegm +pub fn _timegm(ctx: &mut Ctx, time_ptr: u32) -> i32 { + debug!("emscripten::_timegm {}", time_ptr); + + unsafe { + let time_p_addr = emscripten_memory_pointer!(ctx.memory(0), time_ptr) as *mut guest_tm; + libc_timegm(time_p_addr as _) as i32 // TODO review i64 + } +} + /// emscripten: _strftime pub fn _strftime( ctx: &mut Ctx, diff --git a/lib/emscripten/src/varargs.rs b/lib/emscripten/src/varargs.rs index a6a1fe46b10..8daf78dc893 100644 --- a/lib/emscripten/src/varargs.rs +++ b/lib/emscripten/src/varargs.rs @@ -1,5 +1,7 @@ use std::mem; use wasmer_runtime_core::{types::WasmExternType, vm::Ctx}; +// use std::ffi::CStr; +use std::os::raw::c_char; #[repr(transparent)] #[derive(Copy, Clone)] @@ -13,6 +15,14 @@ impl VarArgs { self.pointer += mem::size_of::() as u32; unsafe { (ptr as *const T).read() } } + + // pub fn getStr<'a>(&mut self, ctx: &mut Ctx) -> &'a CStr { + pub fn get_str(&mut self, ctx: &mut Ctx) -> *const c_char { + let ptr_addr: u32 = self.get(ctx); + let ptr = emscripten_memory_pointer!(ctx.memory(0), ptr_addr) as *const c_char; + ptr + // unsafe { CStr::from_ptr(ptr) } + } } unsafe impl WasmExternType for VarArgs { From c7dceba0d8ce54fabc7496ed3c06509f781b6910 Mon Sep 17 00:00:00 2001 From: Syrus Date: Fri, 24 May 2019 12:09:00 -0700 Subject: [PATCH 2/6] Improved formatting --- lib/emscripten/src/syscalls/mod.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index afffa2e4a57..cd7ddd10aa7 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -205,22 +205,27 @@ pub fn ___syscall85(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { let buf = varargs.get_str(ctx); // let buf_addr: i32 = varargs.get(ctx); let buf_size: i32 = varargs.get(ctx); - + let fd = 3; let ret = unsafe { read(fd, buf as _, buf_size as _) as i32 }; - debug!("=> buf: {}, buf_size: {}, return: {} ", unsafe { std::ffi::CStr::from_ptr(buf as _).to_str().unwrap() }, buf_size, ret); + debug!( + "=> buf: {}, buf_size: {}, return: {} ", + unsafe { std::ffi::CStr::from_ptr(buf as _).to_str().unwrap() }, + buf_size, + ret + ); // let ret = unsafe { // readlink(path, buf as _, buf_size as _) as i32 // }; - // debug!("=> path: {}, buf: {}, buf_size: {}, return: {} ", + // debug!("=> path: {}, buf: {}, buf_size: {}, return: {} ", // unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }, // unsafe { std::ffi::CStr::from_ptr(buf as _).to_str().unwrap() }, // // std::ffi::CStr::from_ptr(buf).to_str().unwrap(), // // buf, // buf_size, // ret); - + ret } From dd9c5774f3c0999a12a67a62d87e8a5d4cb8ec1d Mon Sep 17 00:00:00 2001 From: Syrus Date: Fri, 24 May 2019 12:15:52 -0700 Subject: [PATCH 3/6] Separate fpathconf from pathconf --- lib/emscripten/src/env/mod.rs | 23 +++++++++++++++++++++++ lib/emscripten/src/lib.rs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 74f6caddc0e..98bc39dc8f0 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -10,6 +10,8 @@ pub use self::unix::*; #[cfg(windows)] pub use self::windows::*; +use libc::c_char; + use crate::{allocate_on_stack, EmscriptenData}; use std::os::raw::c_int; use wasmer_runtime_core::vm::Ctx; @@ -113,6 +115,27 @@ pub fn ___assert_fail(_ctx: &mut Ctx, _a: c_int, _b: c_int, _c: c_int, _d: c_int // TODO raise an error } +pub fn _pathconf(ctx: &mut Ctx, path_addr: c_int, name: c_int) -> c_int { + debug!( + "emscripten::_pathconf {} {} - UNIMPLEMENTED", + path_addr, name + ); + let _path = emscripten_memory_pointer!(ctx.memory(0), path_addr) as *const c_char; + match name { + 0 => 32000, + 1 | 2 | 3 => 255, + 4 | 5 | 16 | 17 | 18 => 4096, + 6 | 7 | 20 => 1, + 8 => 0, + 9 | 10 | 11 | 12 | 14 | 15 | 19 => -1, + 13 => 64, + _ => { + // ___setErrNo(22); + -1 + } + } +} + pub fn _fpathconf(_ctx: &mut Ctx, _fildes: c_int, name: c_int) -> c_int { debug!("emscripten::_fpathconf {} {}", _fildes, name); match name { diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index cbc8810194f..400bcd94f97 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -545,7 +545,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject "_sysconf" => func!(crate::env::_sysconf), "_getaddrinfo" => func!(crate::env::_getaddrinfo), "_times" => func!(crate::env::_times), - "_pathconf" => func!(crate::env::_fpathconf), + "_pathconf" => func!(crate::env::_pathconf), "_fpathconf" => func!(crate::env::_fpathconf), // Syscalls From dd3059535805f6dc3179bd35523b05fde3bede2e Mon Sep 17 00:00:00 2001 From: Syrus Date: Fri, 24 May 2019 13:30:41 -0700 Subject: [PATCH 4/6] Trying to fix issues --- lib/emscripten/src/syscalls/mod.rs | 3 --- lib/emscripten/src/time.rs | 14 +++++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index cd7ddd10aa7..057a8ac1961 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -205,7 +205,6 @@ pub fn ___syscall85(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { let buf = varargs.get_str(ctx); // let buf_addr: i32 = varargs.get(ctx); let buf_size: i32 = varargs.get(ctx); - let fd = 3; let ret = unsafe { read(fd, buf as _, buf_size as _) as i32 }; debug!( @@ -214,7 +213,6 @@ pub fn ___syscall85(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { buf_size, ret ); - // let ret = unsafe { // readlink(path, buf as _, buf_size as _) as i32 // }; @@ -225,7 +223,6 @@ pub fn ___syscall85(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> i32 { // // buf, // buf_size, // ret); - ret } diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index eef9046f25d..d2bce33761f 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -17,8 +17,6 @@ type clockid_t = c_int; extern "C" { #[link_name = "time"] pub fn libc_time(s: *const time_t) -> time_t; - #[link_name = "timegm"] - pub fn libc_timegm(s: *const time_t) -> time_t; } use time; @@ -301,7 +299,8 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 { } /// emscripten: _timegm -pub fn _timegm(ctx: &mut Ctx, time_ptr: u32) -> i32 { +#[cfg(not(target_os = "windows"))] +pub fn _timegm(ctx: &mut Ctx, time_ptr: c_int) -> i32 { debug!("emscripten::_timegm {}", time_ptr); unsafe { @@ -310,6 +309,15 @@ pub fn _timegm(ctx: &mut Ctx, time_ptr: u32) -> i32 { } } +#[cfg(target_os = "windows")] +pub fn _timegm(ctx: &mut Ctx, time_ptr: c_int) -> i32 { + debug!( + "emscripten::_timegm - UNIMPLEMENTED IN WINDOWS {}", + time_ptr + ); + -1 +} + /// emscripten: _strftime pub fn _strftime( ctx: &mut Ctx, From 6fd6ad92fa098ce3b36319c0b31d4ccd3b95697b Mon Sep 17 00:00:00 2001 From: Syrus Date: Mon, 27 May 2019 12:09:04 -0700 Subject: [PATCH 5/6] Trying to fix timegm --- lib/emscripten/src/time.rs | 40 ++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index d2bce33761f..b3791d32cd9 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -4,7 +4,9 @@ use std::mem; use std::time::SystemTime; #[cfg(not(target_os = "windows"))] -use libc::{clockid_t, time as libc_time, timegm as libc_timegm}; +use libc::{clockid_t, time as libc_time, timegm as libc_timegm, tm as libc_tm}; +#[cfg(not(target_os = "windows"))] +use std::ffi::CString; #[cfg(target_os = "windows")] use libc::time_t; @@ -305,15 +307,45 @@ pub fn _timegm(ctx: &mut Ctx, time_ptr: c_int) -> i32 { unsafe { let time_p_addr = emscripten_memory_pointer!(ctx.memory(0), time_ptr) as *mut guest_tm; - libc_timegm(time_p_addr as _) as i32 // TODO review i64 + + let x: *mut c_char = CString::new("").expect("CString::new failed").into_raw(); + let mut rust_tm = libc_tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 0, + tm_mon: 0, + tm_year: 0, + tm_wday: 0, + tm_yday: 0, + tm_isdst: 0, + tm_gmtoff: 0, + tm_zone: x, + }; + + let result = libc_timegm(&mut rust_tm) as i32; + if result != 0 { + (*time_p_addr).tm_sec = rust_tm.tm_sec; + (*time_p_addr).tm_min = rust_tm.tm_min; + (*time_p_addr).tm_hour = rust_tm.tm_hour; + (*time_p_addr).tm_mday = rust_tm.tm_mday; + (*time_p_addr).tm_mon = rust_tm.tm_mon; + (*time_p_addr).tm_year = rust_tm.tm_year; + (*time_p_addr).tm_wday = rust_tm.tm_wday; + (*time_p_addr).tm_yday = rust_tm.tm_yday; + (*time_p_addr).tm_isdst = rust_tm.tm_isdst; + (*time_p_addr).tm_gmtoff = rust_tm.tm_gmtoff as _; + (*time_p_addr).tm_zone = 0; + } + result } } #[cfg(target_os = "windows")] -pub fn _timegm(ctx: &mut Ctx, time_ptr: c_int) -> i32 { +pub fn _timegm(_ctx: &mut Ctx, _time_ptr: c_int) -> i32 { debug!( "emscripten::_timegm - UNIMPLEMENTED IN WINDOWS {}", - time_ptr + _time_ptr ); -1 } From fe3db46173f37b04f438b6702cc6c6031b5c87a1 Mon Sep 17 00:00:00 2001 From: Syrus Date: Mon, 27 May 2019 12:38:41 -0700 Subject: [PATCH 6/6] Fixed time ptr --- lib/emscripten/src/time.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index b3791d32cd9..c35d7959601 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -302,7 +302,8 @@ pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 { /// emscripten: _timegm #[cfg(not(target_os = "windows"))] -pub fn _timegm(ctx: &mut Ctx, time_ptr: c_int) -> i32 { +#[allow(clippy::cast_ptr_alignment)] +pub fn _timegm(ctx: &mut Ctx, time_ptr: u32) -> i32 { debug!("emscripten::_timegm {}", time_ptr); unsafe {