Merge pull request #155 from wasmerio/fix/signature-index-wrong-map

Fix signature index issue
This commit is contained in:
Syrus Akbary
2019-02-07 11:58:11 -08:00
committed by GitHub
4 changed files with 31 additions and 46 deletions

View File

@ -36,24 +36,28 @@ unsafe impl Sync for HandlerData {}
pub struct HandlerData { pub struct HandlerData {
pub trap_data: TrapSink, pub trap_data: TrapSink,
buffer_ptr: *const c_void, exec_buffer_ptr: *const c_void,
buffer_size: usize, exec_buffer_size: usize,
} }
impl HandlerData { impl HandlerData {
pub fn new(trap_data: TrapSink, buffer_ptr: *const c_void, buffer_size: usize) -> Self { pub fn new(
trap_data: TrapSink,
exec_buffer_ptr: *const c_void,
exec_buffer_size: usize,
) -> Self {
Self { Self {
trap_data, trap_data,
buffer_ptr, exec_buffer_ptr,
buffer_size, exec_buffer_size,
} }
} }
pub fn lookup(&self, ip: *const c_void) -> Option<TrapData> { pub fn lookup(&self, ip: *const c_void) -> Option<TrapData> {
let ip = ip as usize; let ip = ip as usize;
let buffer_ptr = self.buffer_ptr as usize; let buffer_ptr = self.exec_buffer_ptr as usize;
if buffer_ptr <= ip && ip < buffer_ptr + self.buffer_size { if buffer_ptr <= ip && ip < buffer_ptr + self.exec_buffer_size {
let offset = ip - buffer_ptr; let offset = ip - buffer_ptr;
self.trap_data.lookup(offset) self.trap_data.lookup(offset)
} else { } else {
@ -89,21 +93,10 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
TrapCode::IndirectCallToNull => RuntimeError::IndirectCallToNull { TrapCode::IndirectCallToNull => RuntimeError::IndirectCallToNull {
table: TableIndex::new(0), table: TableIndex::new(0),
}, },
TrapCode::HeapOutOfBounds => { TrapCode::HeapOutOfBounds => RuntimeError::OutOfBoundsAccess {
let addr =
(faulting_addr as usize) - (handler_data.buffer_ptr as usize);
if addr <= handler_data.buffer_size {
// in the memory
RuntimeError::OutOfBoundsAccess {
memory: MemoryIndex::new(0), memory: MemoryIndex::new(0),
addr: addr as u32, addr: 0,
} },
} else {
// if there's an invalid access outside of the memory, including guard pages
// just kill the process.
panic!("invalid memory access, way out of bounds")
}
}
TrapCode::TableOutOfBounds => RuntimeError::TableOutOfBounds { TrapCode::TableOutOfBounds => RuntimeError::TableOutOfBounds {
table: TableIndex::new(0), table: TableIndex::new(0),
}, },
@ -111,20 +104,10 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
msg: "unknown trap".to_string(), msg: "unknown trap".to_string(),
}, },
}, },
Ok(SIGSEGV) | Ok(SIGBUS) => { Ok(SIGSEGV) | Ok(SIGBUS) => RuntimeError::OutOfBoundsAccess {
let addr = (faulting_addr as usize) - (handler_data.buffer_ptr as usize);
if addr <= handler_data.buffer_size {
// in the memory
RuntimeError::OutOfBoundsAccess {
memory: MemoryIndex::new(0), memory: MemoryIndex::new(0),
addr: addr as u32, addr: 0,
} },
} else {
// if there's an invalid access outside of the memory, including guard pages
// just kill the process.
panic!("invalid memory access, way out of bounds")
}
}
Ok(SIGFPE) => RuntimeError::IllegalArithmeticOperation, Ok(SIGFPE) => RuntimeError::IllegalArithmeticOperation,
_ => unimplemented!(), _ => unimplemented!(),
} }

View File

@ -95,7 +95,7 @@ pub struct EmscriptenData<'a> {
pub memset: Func<'a, (u32, u32, u32), u32>, pub memset: Func<'a, (u32, u32, u32), u32>,
pub stack_alloc: Func<'a, u32, u32>, pub stack_alloc: Func<'a, u32, u32>,
pub jumps: Vec<UnsafeCell<[u8; 27]>>, pub jumps: Vec<UnsafeCell<[u32; 27]>>,
} }
impl<'a> EmscriptenData<'a> { impl<'a> EmscriptenData<'a> {
@ -144,6 +144,8 @@ pub fn run_emscripten_instance(
instance.call("___emscripten_environ_constructor", &[])?; instance.call("___emscripten_environ_constructor", &[])?;
} }
// println!("running emscripten instance");
let main_func = instance.dyn_func("_main")?; let main_func = instance.dyn_func("_main")?;
let num_params = main_func.signature().params().len(); let num_params = main_func.signature().params().len();
let _result = match num_params { let _result = match num_params {

View File

@ -113,7 +113,7 @@ impl Instance {
.func_assoc .func_assoc
.get(*func_index) .get(*func_index)
.expect("broken invariant, incorrect func index"); .expect("broken invariant, incorrect func index");
let signature = SigRegistry.lookup_signature(sig_index); let signature = &self.module.info.signatures[sig_index];
if signature.params() != Args::types() || signature.returns() != Rets::types() { if signature.params() != Args::types() || signature.returns() != Rets::types() {
Err(ResolveError::Signature { Err(ResolveError::Signature {