Squashed commit of the following:

commit 62d9da4e3e02251a0f61c904e826bc06cf964ff7
Author: Syrus <me@syrusakbary.com>
Date:   Sun Jul 7 18:16:34 2019 -0700

    Fixed syscall221

commit a8fde9148d50d89616d8a85a68110b89e3273229
Author: Syrus <me@syrusakbary.com>
Date:   Sun Jul 7 18:16:04 2019 -0700

    Improved ioctl use case

commit 5ad109d39838624ad84232a4c17714b885835893
Merge: 61526e2c 5cab8161
Author: Syrus <me@syrusakbary.com>
Date:   Sun Jul 7 17:44:25 2019 -0700

    Merge branch 'command/dash' into feature/emscripten-update

commit 5cab816193
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 20:11:49 2019 +0200

    Generic IOCTLs mapping

commit 5a0dc0374c
Merge: 61cfed79 9d120ed3
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 17:15:02 2019 +0200

    Merge remote-tracking branch 'origin/master' into command/dash

commit 61cfed7916
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 13:04:04 2019 +0200

    Fixed implementation for syscalls 200, 201 and 202

commit 91e26d1a0e
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 13:03:26 2019 +0200

    Improved debug messages

commit 53a8fbeb2a
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 13:03:04 2019 +0200

    [___syscall146] Move loop out of `unsafe` zone

commit d6dd3696f1
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 13:01:31 2019 +0200

    [___syscall140] Fixed types

commit c827a6a993
Merge: 2bc16826 5e18d04d
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Sat Jul 6 12:21:33 2019 +0200

    Merge remote-tracking branch 'origin/master' into command/dash

commit 2bc16826b5
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Thu Jul 4 07:05:00 2019 +0200

    Implement `getpgid` syscall

commit d464954f58
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Thu Jul 4 07:04:36 2019 +0200

    [fcntl64] Replace mock for real implementation

commit 3fe0183d85
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Thu Jul 4 07:03:39 2019 +0200

    [ioctl] No-of for `TIOCSPGRP` command & code clean-up

commit cc83ec9ac1
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Thu Jul 4 07:02:47 2019 +0200

    [___syscall5] debug messages

commit 91587c8bde
Author: Jesús Leganés-Combarro 'piranna <piranna@gmail.com>
Date:   Thu Jul 4 07:02:20 2019 +0200

    [___syscall57] debug messages
This commit is contained in:
Syrus
2019-07-07 19:35:09 -07:00
parent 61526e2c45
commit f4e60c09c1
3 changed files with 127 additions and 79 deletions

View File

@ -8,6 +8,7 @@ use libc::{
access,
bind,
c_int,
c_ulong,
c_void,
chown,
// fcntl, setsockopt, getppid
@ -19,6 +20,8 @@ use libc::{
fcntl,
// ENOTTY,
fsync,
getegid,
geteuid,
getgid,
getgroups,
getpeername,
@ -72,8 +75,38 @@ use libc::{
F_SETFD,
SOL_SOCKET,
TIOCGWINSZ,
TIOCSPGRP,
// TCGETS,
// TCSETSW,
};
const TCGETS: u64 = 0x5401;
const TCSETSW: u64 = 0x5403;
// `libc` constants as provided by `emscripten`. Maybe move to own file?
const WASM_FIONBIO: u32 = 0x5421;
const WASM_FIOCLEX: u32 = 0x5451;
const WASM_TIOCSPGRP: u32 = 0x5410;
const WASM_TIOCGWINSZ: u32 = 0x5413;
const WASM_TCGETS: u32 = 0x5401;
const WASM_TCSETSW: u32 = 0x5403;
// Based on @syrusakbary sugerence at
// https://github.com/wasmerio/wasmer/pull/532#discussion_r300837800
fn translate_ioctl(wasm_ioctl: u32) -> c_ulong {
match wasm_ioctl {
WASM_FIOCLEX => FIOCLEX,
WASM_TIOCGWINSZ => TIOCGWINSZ,
WASM_TIOCSPGRP => TIOCSPGRP,
WASM_FIONBIO => FIONBIO,
WASM_TCGETS => TCGETS,
WASM_TCSETSW => TCSETSW,
_otherwise => {
unimplemented!("The ioctl {} is not yet implemented", wasm_ioctl);
}
}
}
#[allow(unused_imports)]
use std::ffi::CStr;
use wasmer_runtime_core::{memory::ptr::WasmPtr, vm::Ctx};
@ -120,13 +153,12 @@ pub fn ___syscall5(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int
let _path_str = unsafe { std::ffi::CStr::from_ptr(real_path).to_str().unwrap() };
let fd = unsafe { open(real_path, flags, mode) };
debug!(
"=> path: {}, flags: {}, mode: {} = fd: {}, last os error: {}",
_path_str,
flags,
mode,
fd,
Error::last_os_error(),
"=> path: {}, flags: {}, mode: {} = fd: {}",
_path_str, flags, mode, fd,
);
if fd == -1 {
debug!("=> last os error: {}", Error::last_os_error(),);
}
fd
}
@ -347,28 +379,28 @@ pub fn ___syscall41(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int
unsafe { dup(fd) }
}
/// getgid
/// getgid32
pub fn ___syscall200(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {
debug!("emscripten::___syscall200 (getgid)");
debug!("emscripten::___syscall200 (getgid32)");
unsafe { getgid() as i32 }
}
// getgid
// geteuid32
pub fn ___syscall201(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {
debug!("emscripten::___syscall201 (getgid)");
debug!("emscripten::___syscall201 (geteuid32)");
unsafe {
// Maybe fix: Emscripten returns 0 always
getgid() as i32
geteuid() as i32
}
}
// getgid32
// getegid32
pub fn ___syscall202(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {
// gid_t
debug!("emscripten::___syscall202 (getgid32)");
debug!("emscripten::___syscall202 (getegid32)");
unsafe {
// Maybe fix: Emscripten returns 0 always
getgid() as _
getegid() as _
}
}
@ -418,45 +450,29 @@ pub fn ___syscall330(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> pid_
/// ioctl
pub fn ___syscall54(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int {
debug!("emscripten::___syscall54 (ioctl) {}", _which);
let fd: i32 = varargs.get(ctx);
let request: u32 = varargs.get(ctx);
debug!("=> fd: {}, op: {}", fd, request);
// Got the equivalents here: https://code.woboq.org/linux/linux/include/uapi/asm-generic/ioctls.h.html
// let argp: u32 = varargs.get(ctx);
// let argp_ptr = emscripten_memory_pointer!(ctx.memory(0), argp) as *mut c_void;
// let ret = unsafe { ioctl(fd, request as _, argp_ptr) };
// debug!("=> {}", ret);
// ret
match request as _ {
21537 => {
// FIONBIO
match request {
WASM_FIOCLEX | WASM_FIONBIO | WASM_TIOCGWINSZ | WASM_TIOCSPGRP | WASM_TCGETS | WASM_TCSETSW => {
let argp: u32 = varargs.get(ctx);
let argp_ptr = emscripten_memory_pointer!(ctx.memory(0), argp) as *mut c_void;
let ret = unsafe { ioctl(fd, FIONBIO, argp_ptr) };
debug!("ret(FIONBIO): {}", ret);
ret
// 0
}
21523 => {
// TIOCGWINSZ
let argp: u32 = varargs.get(ctx);
let argp_ptr = emscripten_memory_pointer!(ctx.memory(0), argp) as *mut c_void;
let ret = unsafe { ioctl(fd, TIOCGWINSZ, argp_ptr) };
debug!("ret(TIOCGWINSZ): {} (harcoded to 0)", ret);
// ret
let translated_request = translate_ioctl(request);
let ret = unsafe { ioctl(fd, translated_request, argp_ptr) };
debug!(" => request: {}, translated: {}, return: {}", request, translated_request, ret);
// TODO: We hardcode the value to have emscripten tests pass, as for some reason
// when the capturer is active, ioctl returns -1 instead of 0
if ret == -1 {
0
} else {
ret
if request == WASM_TIOCGWINSZ && ret == -1 {
return 0
}
ret
}
_ => {
debug!(
"emscripten::___syscall54 -> non implemented case {}",
request
);
debug!(" => not implemented case {} (noop, hardcoded to 0)", request);
0
}
}
@ -500,7 +516,7 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
if ty_and_flags & SOCK_CLOEXC != 0 {
// set_cloexec
unsafe {
ioctl(fd, FIOCLEX);
ioctl(fd, translate_ioctl(WASM_FIOCLEX));
};
}
@ -607,7 +623,7 @@ pub fn ___syscall102(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
// why is this here?
// set_cloexec
unsafe {
ioctl(fd, FIOCLEX);
ioctl(fd, translate_ioctl(WASM_FIOCLEX));
};
debug!(
@ -918,9 +934,16 @@ pub fn ___syscall148(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_in
// setpgid
pub fn ___syscall57(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int {
debug!("emscripten::___syscall57 (setpgid) {}", _which);
let pid: i32 = varargs.get(ctx);
let pgid: i32 = varargs.get(ctx);
unsafe { setpgid(pid, pgid) }
let ret = unsafe { setpgid(pid, pgid) };
debug!("=> pid: {}, pgid: {} = {}", pid, pgid, ret);
if ret == -1 {
debug!("=> last os error: {}", Error::last_os_error(),);
}
ret
}
/// uname