From 93432bdb129e6d138606ef07ea4b79a88fd143f2 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 25 Mar 2019 16:13:41 -0700 Subject: [PATCH] Make reading database files work too! --- lib/emscripten/src/syscalls/mod.rs | 50 ++++++++++++++++++-------- lib/emscripten/src/syscalls/unix.rs | 46 ++++++++++++++++++++++-- lib/emscripten/src/syscalls/windows.rs | 12 +++++++ 3 files changed, 91 insertions(+), 17 deletions(-) diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index 806ea8187..e4fc8fb03 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -28,6 +28,7 @@ use libc::{ getpid, // iovec, lseek, + off_t, // open, read, // readv, @@ -41,6 +42,8 @@ use wasmer_runtime_core::vm::Ctx; use super::env; use std::cell::Cell; +#[allow(unused_imports)] +use std::io::Error; use std::slice; /// exit @@ -254,10 +257,26 @@ pub fn ___syscall140(ctx: &mut Ctx, _which: i32, mut varargs: VarArgs) -> i32 { // -> c_int debug!("emscripten::___syscall140 (lseek) {}", _which); let fd: i32 = varargs.get(ctx); - let offset: i32 = varargs.get(ctx); + let _ = varargs.get::(ctx); // ignore high offset + let offset_low: i32 = varargs.get(ctx); + let result_ptr_value = varargs.get::(ctx); let whence: i32 = varargs.get(ctx); - debug!("=> fd: {}, offset: {}, whence = {}", fd, offset, whence); - unsafe { lseek(fd, offset as _, whence) as _ } + let offset = offset_low as off_t; + let ret = unsafe { lseek(fd, offset, whence) as i32 }; + let result_ptr = emscripten_memory_pointer!(ctx.memory(0), result_ptr_value) as *mut i32; + unsafe { + *result_ptr = ret; + } + debug!( + "=> fd: {}, offset: {}, result_ptr: {}, whence: {} = {}\nlast os error: {}", + fd, + offset, + result_ptr_value, + whence, + 0, + Error::last_os_error(), + ); + 0 } /// readv @@ -343,16 +362,6 @@ pub fn ___syscall191(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { -1 } -pub fn ___syscall194(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { - debug!("emscripten::___syscall194 - stub"); - -1 -} - -pub fn ___syscall196(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { - debug!("emscripten::___syscall194 - stub"); - -1 -} - pub fn ___syscall199(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall199 - stub"); -1 @@ -369,7 +378,14 @@ pub fn ___syscall195(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in unsafe { let mut _stat: stat = std::mem::zeroed(); let ret = stat(pathname_addr, &mut _stat); - debug!("ret: {}", ret); + debug!( + "=> pathname: {}, buf: {}, path: {} = {}\nlast os error: {}", + pathname, + buf, + std::ffi::CStr::from_ptr(pathname_addr).to_str().unwrap(), + ret, + Error::last_os_error() + ); if ret != 0 { return ret; } @@ -408,8 +424,14 @@ pub fn ___syscall221(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in // fcntl64 let _fd: i32 = varargs.get(ctx); let cmd: u32 = varargs.get(ctx); + // (FAPPEND - 0x08 + // |FASYNC - 0x40 + // |FFSYNC - 0x80 + // |FNONBLOCK - 0x04 + debug!("=> fd: {}, cmd: {}", _fd, cmd); match cmd { 2 => 0, + 13 | 14 => 0, // pretend file locking worked _ => -1, } } diff --git a/lib/emscripten/src/syscalls/unix.rs b/lib/emscripten/src/syscalls/unix.rs index fd09220f1..c8fa04101 100644 --- a/lib/emscripten/src/syscalls/unix.rs +++ b/lib/emscripten/src/syscalls/unix.rs @@ -73,6 +73,8 @@ use libc::{ }; use wasmer_runtime_core::vm::Ctx; +#[allow(unused_imports)] +use std::io::Error; use std::mem; // Linking to functions that are not provided by rust libc @@ -82,10 +84,11 @@ extern "C" { pub fn wait4(pid: pid_t, status: *mut c_int, options: c_int, rusage: *mut rusage) -> pid_t; pub fn madvise(addr: *mut c_void, len: size_t, advice: c_int) -> c_int; pub fn fdatasync(fd: c_int) -> c_int; + pub fn lstat64(path: *const c_char, buf: *mut c_void) -> c_int; } #[cfg(not(target_os = "macos"))] -use libc::{fallocate, fdatasync, madvise, wait4}; +use libc::{fallocate, fdatasync, ftruncate64, lstat64, madvise, wait4}; // Another conditional constant for name resolution: Macos et iOS use // SO_NOSIGPIPE as a setsockopt flag to disable SIGPIPE emission on socket. @@ -105,8 +108,13 @@ pub fn ___syscall5(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int 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: {}", - pathname, flags, mode, fd, _path_str + "=> pathname: {}, flags: {}, mode: {} = fd: {}\npath: {}\nlast os error: {}", + pathname, + flags, + mode, + fd, + _path_str, + Error::last_os_error(), ); fd } @@ -159,6 +167,19 @@ pub fn ___syscall83(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int result } +/// ftruncate64 +pub fn ___syscall194(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { + debug!("emscripten::___syscall194 (ftruncate64) {}", _which); + let _fd: c_int = varargs.get(ctx); + let _length: i64 = varargs.get(ctx); + #[cfg(not(target_os = "macos"))] + unsafe { + ftruncate64(_fd, _length) + } + #[cfg(target_os = "macos")] + unimplemented!() +} + /// lchown pub fn ___syscall198(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall198 (lchown) {}", _which); @@ -738,6 +759,25 @@ pub fn ___syscall122(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in unsafe { uname(buf_addr) } } +/// 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 buf_ptr: c_int = varargs.get(ctx); + let path = emscripten_memory_pointer!(ctx.memory(0), path_ptr) as *const c_char; + let buf = emscripten_memory_pointer!(ctx.memory(0), buf_ptr) as *mut c_void; + let result = unsafe { lstat64(path, buf as _) }; + debug!( + "=> path: {}, buf: {} = fd: {}\npath: {}\nlast os error: {}", + path_ptr, + buf_ptr, + result, + unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }, + Error::last_os_error(), + ); + result +} + /// fallocate pub fn ___syscall324(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall324 (fallocate) {}", _which); diff --git a/lib/emscripten/src/syscalls/windows.rs b/lib/emscripten/src/syscalls/windows.rs index 641311d9a..ffa611f99 100644 --- a/lib/emscripten/src/syscalls/windows.rs +++ b/lib/emscripten/src/syscalls/windows.rs @@ -64,6 +64,12 @@ pub fn ___syscall9(_ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int unimplemented!() } +/// ftruncate64 +pub fn ___syscall194(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { + debug!("emscripten::___syscall194 - stub"); + unimplemented!() +} + // chown pub fn ___syscall212(_ctx: &mut Ctx, which: c_int, mut _varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", which); @@ -239,6 +245,12 @@ pub fn ___syscall122(_ctx: &mut Ctx, which: c_int, mut _varargs: VarArgs) -> c_i -1 } +/// lstat64 +pub fn ___syscall196(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { + debug!("emscripten::___syscall196 (lstat64) - stub"); + -1 +} + /// fchown pub fn ___syscall207(_ctx: &mut Ctx, _which: c_int, _varargs: VarArgs) -> c_int { debug!("emscripten::___syscall207 (fchown) {}", _which);