Skip to content

Commit

Permalink
Merge pull request #24 from wasmerio/feature/type-safety
Browse files Browse the repository at this point in the history
Add VarArgs newtype to simplify emscripten varargs.
  • Loading branch information
syrusakbary authored Nov 23, 2018
2 parents 0d23a17 + 6121370 commit 3dc9119
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/apis/emscripten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod memory;
mod process;
mod syscalls;
mod utils;
mod varargs;

// SYSCALLS
pub use self::utils::is_emscripten_module;
Expand Down
42 changes: 15 additions & 27 deletions src/apis/emscripten/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,47 @@
use libc::{c_int, c_void, ssize_t, write};

use crate::webassembly::Instance;

// A macro to retrieve variadic arguments given a varargs offset
macro_rules! vararg {
($name:ident, $type:ident, $instance:ident, $varargs:ident) => (
let ($name, $varargs) = unsafe {
use std::ptr;
let ptr = $instance.memory_offset_addr(0, $varargs as usize);
let ret = ptr::read(ptr as *const $type);
(ret, $varargs + 4)
};
)
}
use super::varargs::VarArgs;

/// sys_read
pub extern "C" fn ___syscall3(_which: c_int, _varargs: c_int, _instance: &mut Instance) -> ssize_t {
pub extern "C" fn ___syscall3(_which: c_int, mut _varargs: VarArgs, _instance: &mut Instance) -> ssize_t {
debug!("emscripten::___syscall3");
0
}

/// sys_write
pub extern "C" fn ___syscall4(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall4");
vararg!(fd, i32, instance, varargs);
vararg!(buf_ptr, u32, instance, varargs);
vararg!(count, u32, instance, varargs);
pub extern "C" fn ___syscall4(_which: c_int, mut varargs: VarArgs, instance: &mut Instance) -> c_int {
let fd: i32 = varargs.get(instance);
let buf_ptr: u32 = varargs.get(instance);
let count: u32 = varargs.get(instance);
debug!("fd: {}, buf_ptr: {}, count: {}", fd, buf_ptr, count);
let buf = instance.memory_offset_addr(0, buf_ptr as usize) as *const c_void;
unsafe { write(fd, buf, count as usize) as i32 }
}

/// sys_open
pub extern "C" fn ___syscall5(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
pub extern "C" fn ___syscall5(_which: c_int, mut varargs: VarArgs, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall5");
vararg!(pathname, u32, instance, varargs);
vararg!(flags, u32, instance, varargs);
vararg!(mode, u32, instance, varargs);
let pathname: u32 = varargs.get(instance);
let flags: u32 = varargs.get(instance);
let mode: u32 = varargs.get(instance);
debug!("pathname: {}, flags: {}, mode: {}", pathname, flags, mode);
-2
}

// sys_ioctl
pub extern "C" fn ___syscall54(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
pub extern "C" fn ___syscall54(_which: c_int, mut varargs: VarArgs, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall54");
vararg!(stream, u32, instance, varargs);
vararg!(op, u32, instance, varargs);
let stream: u32 = varargs.get(instance);
let op: u32 = varargs.get(instance);
debug!("stream: {}, op: {}", stream, op);
0
}

// sys_newuname
pub extern "C" fn ___syscall122(_which: c_int, varargs: c_int, instance: &mut Instance) -> c_int {
pub extern "C" fn ___syscall122(_which: c_int, mut varargs: VarArgs, instance: &mut Instance) -> c_int {
debug!("emscripten::___syscall122");
vararg!(buf, u32, instance, varargs);
let buf: u32 = varargs.get(instance);
debug!("buf: {}", buf);
0
}
15 changes: 15 additions & 0 deletions src/apis/emscripten/varargs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::webassembly::Instance;
use std::mem;

#[repr(transparent)]
pub struct VarArgs {
pointer: u32, // assuming 32bit wasm
}

impl VarArgs {
pub fn get<T: Copy>(&mut self, instance: &mut Instance) -> T {
let ptr = instance.memory_offset_addr(0, self.pointer as usize);
self.pointer += mem::size_of::<T>() as u32;
unsafe { (ptr as *const T).read() }
}
}
14 changes: 3 additions & 11 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ macro_rules! include_wast2wasm_bytes {
}};
}

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug {
($fmt:expr) => (println!(concat!("Wasmer::", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("Wasmer::", $fmt, "\n"), $($arg)*));
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug {
($fmt:expr) => {};
($fmt:expr, $($arg:tt)*) => {};
}
($fmt:expr) => (if cfg!(debug_assertions) { println!(concat!("Wasmer::", $fmt)) });
($fmt:expr, $($arg:tt)*) => (if cfg!(debug_assertions) { println!(concat!("Wasmer::", $fmt, "\n"), $($arg)*) });
}

0 comments on commit 3dc9119

Please sign in to comment.