wasmer/src/apis/emscripten/syscalls.rs

78 lines
2.9 KiB
Rust
Raw Normal View History

2018-11-22 10:43:38 +01:00
/// NOTE: TODO: These syscalls only support wasm_32 for now because they take u32 offset
2018-11-21 20:58:32 -08:00
/// Syscall list: https://www.cs.utexas.edu/~bismith/test/syscalls/syscalls32.html
2018-11-22 18:00:05 +01:00
use libc::{
c_int, c_void, utsname,
ssize_t, write, exit, read,
open, close, ioctl,
uname,
};
2018-11-22 10:43:38 +01:00
#[macro_use]
use macros;
2018-11-21 20:59:23 -08:00
use crate::webassembly::Instance;
2018-11-22 10:43:38 +01:00
2018-11-22 18:00:05 +01:00
/// sys_exit
pub extern "C" fn ___syscall1(_which: c_int, varargs: c_int, instance: &mut Instance) {
debug!("emscripten::___syscall1");
let status = vararg!(i32, instance, varargs);
unsafe { exit(status); }
}
2018-11-21 20:58:32 -08:00
/// sys_read
2018-11-22 18:00:05 +01:00
pub extern "C" fn ___syscall3(_which: c_int, varargs: c_int, instance: &mut Instance) -> ssize_t {
2018-11-21 20:58:32 -08:00
debug!("emscripten::___syscall3");
2018-11-22 18:00:05 +01:00
let fd = vararg!(i32, instance, varargs);
let buf = vararg!(u32, instance, varargs);
let count = vararg!(usize, instance, varargs);
debug!("fd: {}, buf_offset: {}, count: {}", fd, buf, count);
let buf_addr = instance.memory_offset_addr(0, buf as usize) as *mut c_void;
unsafe { read(fd, buf_addr, count) }
2018-11-21 15:10:03 -08:00
}
2018-11-21 20:58:32 -08:00
/// sys_write
2018-11-22 18:00:05 +01:00
pub extern "C" fn ___syscall4(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
2018-11-22 10:43:38 +01:00
debug!("emscripten::___syscall4");
2018-11-22 18:00:05 +01:00
let fd = vararg!(i32, instance, varargs);
let buf = vararg!(u32, instance, varargs);
let count = vararg!(u32, instance, varargs);
debug!("fd: {}, buf: {}, count: {}", fd, buf, count);
let buf_addr = instance.memory_offset_addr(0, buf as usize) as *const c_void;
unsafe { write(fd, buf_addr, count as usize) as i32 }
2018-11-21 15:10:03 -08:00
}
2018-11-21 20:58:32 -08:00
/// sys_open
2018-11-22 18:00:05 +01:00
pub extern "C" fn ___syscall5(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
2018-11-21 20:58:32 -08:00
debug!("emscripten::___syscall5");
2018-11-22 18:00:05 +01:00
let pathname = vararg!(u32, instance, varargs);
let flags = vararg!(i32, instance, varargs);
let mode = vararg!(u32, instance, varargs);
2018-11-21 20:58:32 -08:00
debug!("pathname: {}, flags: {}, mode: {}", pathname, flags, mode);
2018-11-22 18:00:05 +01:00
let pathname_addr = instance.memory_offset_addr(0, pathname as usize) as *const i8;
unsafe { open(pathname_addr, flags, mode) }
}
/// sys_close
pub extern "C" fn ___syscall6(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall1");
let fd = vararg!(i32, instance, varargs);
debug!("fd: {}", fd);
unsafe { close(fd) }
2018-11-21 15:10:03 -08:00
}
2018-11-21 20:58:32 -08:00
// sys_ioctl
2018-11-22 18:00:05 +01:00
pub extern "C" fn ___syscall54(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
2018-11-21 20:58:32 -08:00
debug!("emscripten::___syscall54");
2018-11-22 18:00:05 +01:00
let fd = vararg!(i32, instance, varargs);
let request = vararg!(u64, instance, varargs);
debug!("fd: {}, op: {}", fd, request);
unsafe { ioctl(fd, request) }
2018-11-21 20:58:32 -08:00
}
// sys_newuname
2018-11-22 18:00:05 +01:00
pub extern "C" fn ___syscall122(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
2018-11-21 20:58:32 -08:00
debug!("emscripten::___syscall122");
2018-11-22 18:00:05 +01:00
let buf = vararg!(u32, instance, varargs);
2018-11-21 20:58:32 -08:00
debug!("buf: {}", buf);
2018-11-22 18:00:05 +01:00
let buf_addr = instance.memory_offset_addr(0, buf as usize) as *mut utsname;
unsafe { uname(buf_addr) } // TODO: Fix implementation
2018-11-21 20:58:32 -08:00
}