Align functions according to their ISA's requirements (#4826)
Add a function_alignment function to the TargetIsa trait, and use it to align functions when generating objects. Additionally, collect the maximum alignment required for pc-relative constants in functions and pass that value out. Use the max of these two values when padding functions for alignment. This fixes a bug on x86_64 where rip-relative loads to sse registers could cause a segfault, as functions weren't always guaranteed to be aligned to 16-byte addresses. Fixes #4812
This commit is contained in:
@@ -271,7 +271,8 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
&mut func_env,
|
||||
)?;
|
||||
|
||||
let (_, code_buf) = compile_maybe_cached(&mut context, isa, cache_ctx.as_mut())?;
|
||||
let (code, code_buf) = compile_maybe_cached(&mut context, isa, cache_ctx.as_mut())?;
|
||||
let alignment = code.alignment;
|
||||
|
||||
let compiled_code = context.compiled_code().unwrap();
|
||||
let func_relocs = compiled_code
|
||||
@@ -333,6 +334,7 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
stack_maps,
|
||||
start: 0,
|
||||
length,
|
||||
alignment,
|
||||
},
|
||||
address_map: address_transform,
|
||||
}))
|
||||
|
||||
@@ -95,7 +95,11 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
func: &'a CompiledFunction,
|
||||
) -> (SymbolId, Range<u64>) {
|
||||
let body_len = func.body.len() as u64;
|
||||
let off = self.text.append(labeled, &func.body, None);
|
||||
let off = self.text.append(
|
||||
labeled,
|
||||
&func.body,
|
||||
self.isa.function_alignment().max(func.info.alignment),
|
||||
);
|
||||
|
||||
let symbol_id = self.obj.add_symbol(Symbol {
|
||||
name,
|
||||
@@ -198,7 +202,7 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
if padding == 0 {
|
||||
return;
|
||||
}
|
||||
self.text.append(false, &vec![0; padding], Some(1));
|
||||
self.text.append(false, &vec![0; padding], 1);
|
||||
}
|
||||
|
||||
/// Indicates that the text section has been written completely and this
|
||||
|
||||
Reference in New Issue
Block a user