From 03cff50a40b64f011c849b06be264c79f38b4ba5 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 7 Dec 2018 03:10:26 +0100 Subject: [PATCH] Debug segfaulting stackallocation --- src/apis/emscripten/mod.rs | 35 +++++++++++++---------------------- src/apis/emscripten/utils.rs | 19 +++++++------------ src/bin/wasmer.rs | 4 ++-- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/apis/emscripten/mod.rs b/src/apis/emscripten/mod.rs index 81de9d7ca..22d71a581 100644 --- a/src/apis/emscripten/mod.rs +++ b/src/apis/emscripten/mod.rs @@ -23,10 +23,10 @@ pub use self::utils::{is_emscripten_module, copy_cstr_array_into_wasm}; // TODO: Magic number - how is this calculated? const TOTAL_STACK: u32 = 5242880; -// TODO: Magic number stolen from the generated JS - how is this calculated? +// TODO: Magic number - how is this calculated? const DYNAMICTOP_PTR_DIFF: u32 = 1088; - -const STATIC_BUMP: u32 = 215536; // TODO: make this variable +// TODO: make this variable +const STATIC_BUMP: u32 = 215536; fn stacktop(static_bump: u32) -> u32 { align_memory(dynamictop_ptr(static_bump) + 4) @@ -54,10 +54,18 @@ pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { let dynamictop_ptr = dynamictop_ptr(STATIC_BUMP) as usize; let dynamictop_ptr_offset = dynamictop_ptr + mem::size_of::(); + // println!("value = {:?}"); + // We avoid failures of setting the u32 in our memory if it's out of bounds if dynamictop_ptr_offset > memory.len() { - return; + return; // TODO: We should panic instead? } + + // debug!("###### dynamic_base = {:?}", dynamic_base(STATIC_BUMP)); + // debug!("###### dynamictop_ptr = {:?}", dynamictop_ptr); + // debug!("###### dynamictop_ptr_offset = {:?}", dynamictop_ptr_offset); + + let mem = &mut memory[dynamictop_ptr..dynamictop_ptr_offset]; LittleEndian::write_u32(mem, dynamic_base(STATIC_BUMP)); } @@ -74,23 +82,7 @@ macro_rules! mock_external { pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> { let mut import_object = ImportObject::new(); - // Global - import_object.set( - "env", - "global1", - ImportValue::Global(24), // TODO - ); - import_object.set( - "env", - "global2", - ImportValue::Global(50), // TODO - ); - import_object.set( - "env", - "global3", - ImportValue::Global(67), // TODO - ); - + // Globals import_object.set( "env", "STACKTOP", @@ -107,7 +99,6 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> { ImportValue::Global(dynamictop_ptr(STATIC_BUMP) as _), ); import_object.set("env", "tableBase", ImportValue::Global(0)); - // Print functions import_object.set("env", "printf", ImportValue::Func(io::printf as _)); import_object.set("env", "putchar", ImportValue::Func(io::putchar as _)); diff --git a/src/apis/emscripten/utils.rs b/src/apis/emscripten/utils.rs index d6b4bc83f..4418ec264 100644 --- a/src/apis/emscripten/utils.rs +++ b/src/apis/emscripten/utils.rs @@ -27,15 +27,14 @@ pub unsafe fn copy_cstr_into_wasm(instance: &mut Instance, cstr: *const c_char) 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_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); - 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); @@ -44,16 +43,12 @@ pub unsafe fn copy_cstr_array_into_wasm(array_count: u32, array: *mut *mut c_cha *ptr = offset; } - // for i in 0..array_count { - // let offset = copy_cstr_into_wasm( - // instance, - // *array.offset(i as isize) - // ); - // *array_addr.offset(i as isize) = offset; - // } + // println!("###### x = {:?}", *array_addr.add(array_count as usize)); - // let first_arg_addr = instance.memory_offset_addr(0, *array_addr.offset(0) as _) as *const i8; - // debug!("###### argv[0] = {:?}", CStr::from_ptr(first_arg_addr)); + // *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)); array_offset } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index f2b9ef70b..344078544 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -88,12 +88,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> { _ => panic!("_main emscripten function not found"), }; - let main: extern "C" fn(u32, u32, &webassembly::Instance) = + let main: extern "C" fn(u32, 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, &instance)).map_err(|err| format!("{}", err)); + return call_protected!(main(argc, argv, 0, &instance)).map_err(|err| format!("{}", err)); // TODO: We should implement emscripten __ATEXIT__ } else { let func_index =