Fixed issue when there are no WebAssembly functions to allocate in memory

This commit is contained in:
Syrus Akbary
2018-10-15 11:51:38 +02:00
parent 69efaaaddd
commit ad31e295dd

View File

@ -107,29 +107,34 @@ impl Instance {
context_and_offsets.push((func_context, code_size_offset)); context_and_offsets.push((func_context, code_size_offset));
} }
// Allocate the total memory for this functions // We only want to allocate in memory if there is more than
let map = MmapMut::map_anon(total_size).unwrap(); // 0 functions. Otherwise reserving a 0-sized memory
let region_start = map.as_ptr(); // cause a panic error
if total_size > 0 {
// Allocate the total memory for this functions
let map = MmapMut::map_anon(total_size).unwrap();
let region_start = map.as_ptr();
// // Emit this functions to memory // // Emit this functions to memory
for (ref func_context, func_offset) in context_and_offsets.iter() { for (ref func_context, func_offset) in context_and_offsets.iter() {
let mut trap_sink = TrapSink::new(*func_offset); let mut trap_sink = TrapSink::new(*func_offset);
let mut reloc_sink = RelocSink::new(); let mut reloc_sink = RelocSink::new();
unsafe {
func_context.emit_to_memory(
&*isa,
(region_start as usize + func_offset) as *mut u8,
&mut reloc_sink,
&mut trap_sink,
);
};
}
// Set protection of this memory region to Read + Execute
// so we are able to execute the functions emitted to memory
unsafe { unsafe {
func_context.emit_to_memory( region::protect(region_start, total_size, region::Protection::ReadExecute)
&*isa, .expect("unable to make memory readable+executable");
(region_start as usize + func_offset) as *mut u8, }
&mut reloc_sink,
&mut trap_sink,
);
};
}
// Set protection of this memory region to Read + Execute
// so we are able to execute the functions emitted to memory
unsafe {
region::protect(region_start, total_size, region::Protection::ReadExecute)
.expect("unable to make memory readable+executable");
} }
} }