Debug segfaulting stackallocation

This commit is contained in:
Steve Akinyemi 2018-12-07 03:10:26 +01:00
parent 28a668f38c
commit 03cff50a40
3 changed files with 22 additions and 36 deletions

View File

@ -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::<u32>();
// 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 _));

View File

@ -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::<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);
@ -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
}

View File

@ -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 =