More debugging

This commit is contained in:
Steve Akinyemi 2018-12-07 04:08:17 +01:00
parent 03cff50a40
commit 485da4c701
4 changed files with 25 additions and 9 deletions

View File

@ -19,7 +19,7 @@ mod utils;
mod varargs;
pub use self::storage::{align_memory, static_alloc};
pub use self::utils::{is_emscripten_module, copy_cstr_array_into_wasm};
pub use self::utils::{is_emscripten_module, copy_cstr_array_into_wasm_stack};
// TODO: Magic number - how is this calculated?
const TOTAL_STACK: u32 = 5242880;

View File

@ -18,6 +18,22 @@ pub fn is_emscripten_module(module: &Module) -> bool {
}
pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char) -> u32 {
let s = CStr::from_ptr(cstr).to_str().unwrap();
let cstr_len = s.len();
let space_offset = (instance.emscripten_data.as_ref().unwrap().malloc)(cstr_len as _, instance);
let raw_memory = instance.memory_offset_addr(0, space_offset as _) as *mut u8;
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
for (byte, loc) in s.bytes().zip(slice.iter_mut()) {
*loc = byte;
}
*raw_memory.add(cstr_len) = 0;
space_offset
}
pub unsafe fn copy_cstr_into_wasm_stack(instance: &mut Instance, cstr: *const c_char) -> u32 {
let s = CStr::from_ptr(cstr).to_str().unwrap();
let cstr_len = s.len();
let space_offset = (instance.emscripten_data.as_ref().unwrap().stack_alloc)((cstr_len as u32) + 1, instance);
@ -33,8 +49,8 @@ pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char)
space_offset
}
pub unsafe fn copy_cstr_array_into_wasm(array_count: u32, array: *mut *mut c_char, instance: &mut Instance) -> u32 {
let array_offset = (instance.emscripten_data.as_ref().unwrap().stack_alloc)((array_count as usize * size_of::<u32>()) as _, instance);
pub unsafe fn copy_cstr_array_into_wasm_stack(array_count: u32, array: *mut *mut c_char, instance: &mut Instance) -> u32 {
let array_offset = (instance.emscripten_data.as_ref().unwrap().stack_alloc)(((array_count as usize + 1) * size_of::<u32>()) as _, instance);
let array_addr = instance.memory_offset_addr(0, array_offset as _) as *mut u32;
let array_slice = slice::from_raw_parts_mut(array_addr, array_count as usize);
@ -45,7 +61,7 @@ pub unsafe fn copy_cstr_array_into_wasm(array_count: u32, array: *mut *mut c_cha
// println!("###### x = {:?}", *array_addr.add(array_count as usize));
// *array_addr.add(array_count as usize) = 0;
*array_addr.add(array_count as usize) = 0;
// let arg_addr = instance.memory_offset_addr(0, *array_addr.offset(0) as _) as *const i8;
// debug!("###### argv[0] = {:?}", CStr::from_ptr(arg_addr));

View File

@ -1,4 +1,4 @@
pub mod emscripten;
pub mod host;
pub use self::emscripten::{align_memory, generate_emscripten_env, is_emscripten_module, copy_cstr_array_into_wasm};
pub use self::emscripten::{align_memory, generate_emscripten_env, is_emscripten_module, copy_cstr_array_into_wasm_stack};

View File

@ -7,7 +7,7 @@ use std::io::Read;
use std::path::PathBuf;
use std::process::exit;
use apis::emscripten::copy_cstr_array_into_wasm;
use apis::emscripten::copy_cstr_array_into_wasm_stack;
use structopt::StructOpt;
use wasmer::*;
@ -88,12 +88,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
_ => panic!("_main emscripten function not found"),
};
let main: extern "C" fn(u32, u32, u32, &webassembly::Instance) =
let main: extern "C" fn(u32, u32, &webassembly::Instance) =
get_instance_function!(instance, func_index);
let (argc, argv) = get_module_arguments(options, &mut instance);
return call_protected!(main(argc, argv, 0, &instance)).map_err(|err| format!("{}", err));
return call_protected!(main(argc, argv, &instance)).map_err(|err| format!("{}", err));
// TODO: We should implement emscripten __ATEXIT__
} else {
let func_index =
@ -153,7 +153,7 @@ fn get_module_arguments(options: &Run, instance: &mut webassembly::Instance) ->
// Copy the the arguments into the wasm memory and get offset
let argv_offset = unsafe {
copy_cstr_array_into_wasm(argc, argv, instance)
copy_cstr_array_into_wasm_stack(argc, argv, instance)
};
debug!("argc = {:?}", argc);