From 485da4c7019e0d101cce90e16f634966bf981b93 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 7 Dec 2018 04:08:17 +0100 Subject: [PATCH] More debugging --- src/apis/emscripten/mod.rs | 2 +- src/apis/emscripten/utils.rs | 22 +++++++++++++++++++--- src/apis/mod.rs | 2 +- src/bin/wasmer.rs | 8 ++++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/apis/emscripten/mod.rs b/src/apis/emscripten/mod.rs index 22d71a581..81620df1b 100644 --- a/src/apis/emscripten/mod.rs +++ b/src/apis/emscripten/mod.rs @@ -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; diff --git a/src/apis/emscripten/utils.rs b/src/apis/emscripten/utils.rs index 4418ec264..4f13c3f61 100644 --- a/src/apis/emscripten/utils.rs +++ b/src/apis/emscripten/utils.rs @@ -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::()) 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::()) 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)); diff --git a/src/apis/mod.rs b/src/apis/mod.rs index 588b2fc1f..ad236483e 100644 --- a/src/apis/mod.rs +++ b/src/apis/mod.rs @@ -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}; diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 344078544..932cdd37c 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -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);