Merge pull request #6 from sunfishcode/index-fix

Merge the index-fix branch
This commit is contained in:
Dan Gohman
2018-05-19 15:57:26 -07:00
committed by GitHub
2 changed files with 38 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ extern crate region;
extern crate wasmstandalone_runtime; extern crate wasmstandalone_runtime;
use cretonne_codegen::isa::TargetIsa; use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::binemit::Reloc;
use std::mem::transmute; use std::mem::transmute;
use region::Protection; use region::Protection;
use region::protect; use region::protect;
@@ -41,11 +42,22 @@ fn relocate(compilation: &mut Compilation, relocations: &wasmstandalone_runtime:
for ref r in function_relocs { for ref r in function_relocs {
let target_func_address: isize = compilation.functions[r.func_index].as_ptr() as isize; let target_func_address: isize = compilation.functions[r.func_index].as_ptr() as isize;
let body = &mut compilation.functions[i]; let body = &mut compilation.functions[i];
unsafe { match r.reloc {
let reloc_address = body.as_mut_ptr().offset(r.offset as isize + 4) as isize; Reloc::Abs8 => unsafe {
let reloc_addend = r.addend as isize; let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as i64;
let reloc_delta_i32 = (target_func_address - reloc_address + reloc_addend) as i32; let reloc_addend = r.addend as i64;
write_unaligned(reloc_address as *mut i32, reloc_delta_i32); let reloc_abs = target_func_address as i64 + reloc_addend;
write_unaligned(reloc_address as *mut i64, reloc_abs);
},
Reloc::X86PCRel4 => unsafe {
let reloc_address = body.as_mut_ptr().offset(r.offset as isize) as isize;
let reloc_addend = r.addend as isize;
// TODO: Handle overflow.
let reloc_delta_i32 = (target_func_address - reloc_address + reloc_addend) as
i32;
write_unaligned(reloc_address as *mut i32, reloc_delta_i32);
},
_ => panic!("unsupported reloc kind"),
} }
} }
} }
@@ -72,23 +84,28 @@ pub fn execute(
let start_index = compilation.module.start_func.ok_or_else(|| { let start_index = compilation.module.start_func.ok_or_else(|| {
String::from("No start function defined, aborting execution") String::from("No start function defined, aborting execution")
})?; })?;
let code_buf = &compilation.functions[start_index]; // TODO: Put all the function bodies into a page-aligned memory region, and
match unsafe { // then make them ReadExecute rather than ReadWriteExecute.
protect( for code_buf in &compilation.functions {
code_buf.as_ptr(), match unsafe {
code_buf.len(), protect(
Protection::ReadWriteExecute, code_buf.as_ptr(),
) code_buf.len(),
} { Protection::ReadWriteExecute,
Ok(()) => (), )
Err(err) => { } {
return Err(format!( Ok(()) => (),
"failed to give executable permission to code: {}", Err(err) => {
err return Err(format!(
)) "failed to give executable permission to code: {}",
err
))
}
} }
} }
let code_buf = &compilation.functions[start_index];
let vmctx = make_vmctx(instance); let vmctx = make_vmctx(instance);
// Rather than writing inline assembly to jump to the code region, we use the fact that // Rather than writing inline assembly to jump to the code region, we use the fact that

View File

@@ -569,7 +569,8 @@ impl<'data, 'module> ModuleTranslation<'data, 'module> {
) -> Result<(Compilation<'module>, Relocations), String> { ) -> Result<(Compilation<'module>, Relocations), String> {
let mut functions = Vec::new(); let mut functions = Vec::new();
let mut relocations = Vec::new(); let mut relocations = Vec::new();
for (func_index, input) in self.lazy.function_body_inputs.iter().enumerate() { for (i, input) in self.lazy.function_body_inputs.iter().enumerate() {
let func_index = i + self.module.imported_funcs.len();
let mut context = cretonne_codegen::Context::new(); let mut context = cretonne_codegen::Context::new();
context.func.name = get_func_name(func_index); context.func.name = get_func_name(func_index);
context.func.signature = self.module.signatures[self.module.functions[func_index]] context.func.signature = self.module.signatures[self.module.functions[func_index]]