This commit is contained in:
Lachlan Sneff
2019-02-07 10:45:48 -08:00
parent bca702794c
commit e147983ece
4 changed files with 25 additions and 40 deletions

View File

@@ -36,24 +36,24 @@ unsafe impl Sync for HandlerData {}
pub struct HandlerData {
pub trap_data: TrapSink,
buffer_ptr: *const c_void,
buffer_size: usize,
exec_buffer_ptr: *const c_void,
exec_buffer_size: usize,
}
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 {
trap_data,
buffer_ptr,
buffer_size,
exec_buffer_ptr,
exec_buffer_size,
}
}
pub fn lookup(&self, ip: *const c_void) -> Option<TrapData> {
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;
self.trap_data.lookup(offset)
} else {
@@ -90,18 +90,9 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
table: TableIndex::new(0),
},
TrapCode::HeapOutOfBounds => {
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),
addr: addr as u32,
}
} 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")
RuntimeError::OutOfBoundsAccess {
memory: MemoryIndex::new(0),
addr: 0,
}
}
TrapCode::TableOutOfBounds => RuntimeError::TableOutOfBounds {
@@ -112,17 +103,9 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
},
},
Ok(SIGSEGV) | Ok(SIGBUS) => {
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),
addr: addr as u32,
}
} 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")
RuntimeError::OutOfBoundsAccess {
memory: MemoryIndex::new(0),
addr: 0,
}
}
Ok(SIGFPE) => RuntimeError::IllegalArithmeticOperation,