more emscripten stubs and urandom hack (#199)

This commit is contained in:
Mackenzie Clark
2019-02-22 10:42:38 -08:00
committed by GitHub
parent e9d72740c0
commit a6d72bdec9
9 changed files with 599 additions and 17 deletions

View File

@ -1,10 +1,62 @@
use crate::utils::copy_cstr_into_wasm;
use crate::utils::read_string_from_wasm;
use crate::varargs::VarArgs;
use libc::mkdir;
use libc::open;
use rand::Rng;
use std::env;
use std::ffi::CStr;
use std::ffi::CString;
use std::fs::File;
use std::io::Write;
use std::os::raw::c_int;
use wasmer_runtime_core::vm::Ctx;
type pid_t = c_int;
/// 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 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" => {
// create a fake urandom file for windows, super hacky
// put it in the temp directory so we can just forget about it
let mut tmp_dir = env::temp_dir();
tmp_dir.push("urandom");
let tmp_dir_str = tmp_dir.to_str().unwrap();
let tmp_dir_c_str = CString::new(tmp_dir_str).unwrap();
let ptr = tmp_dir_c_str.as_ptr() as *const i8;
let mut urandom_file = File::create(tmp_dir).unwrap();
// create some random bytes and put them into the file
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
urandom_file.write_all(&random_bytes);
// put the file path string into wasm memory
let urandom_file_offset = unsafe { copy_cstr_into_wasm(ctx, ptr) };
let raw_pointer_to_urandom_file =
emscripten_memory_pointer!(ctx.memory(0), urandom_file_offset) as *const i8;
let fd = unsafe { open(raw_pointer_to_urandom_file, flags, mode) };
debug!(
"=> pathname: {}, flags: {}, mode: {} = fd: {}\npath: {}",
pathname, flags, mode, fd, s
);
fd
}
_ => {
let fd = unsafe { open(pathname_addr, flags, mode) };
debug!(
"=> pathname: {}, flags: {}, mode: {} = fd: {}\npath: {}",
pathname, flags, mode, fd, path_str
);
fd
}
}
}
// chown
pub fn ___syscall212(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int {
debug!("emscripten::___syscall212 (chown) {}", which);