Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some infrastructure to generate EH_Frame in singlepass compiler #2811

Merged
merged 18 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/compiler-singlepass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ wasmer-vm = { path = "../vm", version = "=2.2.0" }
wasmer-types = { path = "../types", version = "=2.2.0", default-features = false, features = ["std"] }
rayon = { version = "1.5", optional = true }
hashbrown = { version = "0.11", optional = true }
gimli = { version = "0.25", optional = true }
more-asserts = "0.2"
dynasm = "1.2.1"
dynasmrt = "1.2.1"
Expand All @@ -32,8 +33,9 @@ target-lexicon = { version = "0.12.2", default-features = false }
maintenance = { status = "actively-developed" }

[features]
default = ["std", "rayon", "avx"]
default = ["std", "rayon", "unwind", "avx"]
std = ["wasmer-compiler/std", "wasmer-types/std"]
core = ["hashbrown", "wasmer-types/core"]
unwind = ["gimli"]
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
sse = []
avx = []
31 changes: 19 additions & 12 deletions lib/compiler-singlepass/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::address_map::get_function_address_map;
use crate::location::{Location, Reg};
use crate::machine::{CodegenError, Label, Machine, MachineStackOffset, NATIVE_PAGE_SIZE};
use crate::{common_decl::*, config::Singlepass};
use gimli::write::FrameDescriptionEntry;
use smallvec::{smallvec, SmallVec};
use std::cmp;
use std::iter;
Expand Down Expand Up @@ -5850,7 +5851,10 @@ impl<'a, M: Machine> FuncGen<'a, M> {
Ok(())
}

pub fn finalize(mut self, data: &FunctionBodyData) -> CompiledFunction {
pub fn finalize(
mut self,
data: &FunctionBodyData,
) -> (CompiledFunction, Option<FrameDescriptionEntry>) {
// Generate actual code for special labels.
self.machine
.emit_label(self.special_labels.integer_division_by_zero);
Expand Down Expand Up @@ -5895,18 +5899,21 @@ impl<'a, M: Machine> FuncGen<'a, M> {
let traps = self.machine.collect_trap_information();
let body = self.machine.assembler_finalize();

CompiledFunction {
body: FunctionBody {
body: body,
unwind_info: None,
},
relocations: self.relocations.clone(),
jt_offsets: SecondaryMap::new(),
frame_info: CompiledFunctionFrameInfo {
traps: traps,
address_map,
(
CompiledFunction {
body: FunctionBody {
body: body,
unwind_info: None,
},
relocations: self.relocations.clone(),
jt_offsets: SecondaryMap::new(),
frame_info: CompiledFunctionFrameInfo {
traps: traps,
address_map,
},
},
}
None,
)
}
// FIXME: This implementation seems to be not enough to resolve all kinds of register dependencies
// at call place.
Expand Down
69 changes: 59 additions & 10 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@

use crate::codegen::FuncGen;
use crate::config::Singlepass;
#[cfg(feature = "unwind")]
use crate::dwarf::WriterRelocate;
use crate::machine::Machine;
use crate::machine::{
gen_import_call_trampoline, gen_std_dynamic_import_trampoline, gen_std_trampoline, CodegenError,
};
use crate::machine_arm64::MachineARM64;
use crate::machine_x64::MachineX86_64;
#[cfg(feature = "unwind")]
use crate::unwind::create_systemv_cie;
#[cfg(feature = "unwind")]
use gimli::write::{EhFrame, FrameTable};
use loupe::MemoryUsage;
#[cfg(feature = "rayon")]
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::{
Architecture, CallingConvention, Compilation, CompileError, CompileModuleInfo,
CompiledFunction, Compiler, CompilerConfig, CpuFeature, FunctionBinaryReader, FunctionBody,
FunctionBodyData, MiddlewareBinaryReader, ModuleMiddleware, ModuleMiddlewareChain,
ModuleTranslationState, OperatingSystem, SectionIndex, Target, TrapInformation,
CompiledFunction, Compiler, CompilerConfig, CpuFeature, Dwarf, FunctionBinaryReader,
FunctionBody, FunctionBodyData, MiddlewareBinaryReader, ModuleMiddleware,
ModuleMiddlewareChain, ModuleTranslationState, OperatingSystem, SectionIndex, Target,
TrapInformation,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{
Expand Down Expand Up @@ -94,11 +101,34 @@ impl Compiler for SinglepassCompiler {
_ => panic!("Unsupported Calling convention for Singlepass compiler"),
};

// Generate the frametable
#[cfg(feature = "unwind")]
let dwarf_frametable = if function_body_inputs.is_empty() {
// If we have no function body inputs, we don't need to
// construct the `FrameTable`. Constructing it, with empty
// FDEs will cause some issues in Linux.
None
} else {
match target.triple().default_calling_convention() {
Ok(CallingConvention::SystemV) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about CallingConvention::AppleAarch64? Maybe just check for OS == Windows instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work for now on AppleAarch64. The unwind fails, a pointer fail the authentication check (with autlib opcode).

match create_systemv_cie(target.triple().architecture) {
Some(cie) => {
let mut dwarf_frametable = FrameTable::default();
let cie_id = dwarf_frametable.add_cie(cie);
Some((dwarf_frametable, cie_id))
}
None => None,
}
}
_ => None,
}
};

let memory_styles = &compile_info.memory_styles;
let table_styles = &compile_info.table_styles;
let vmoffsets = VMOffsets::new(8, &compile_info.module);
let module = &compile_info.module;
let import_trampolines: PrimaryMap<SectionIndex, _> = (0..module.num_imported_functions)
let mut custom_sections: PrimaryMap<SectionIndex, _> = (0..module.num_imported_functions)
.map(FunctionIndex::new)
.collect::<Vec<_>>()
.into_par_iter_if_rayon()
Expand All @@ -114,7 +144,7 @@ impl Compiler for SinglepassCompiler {
.collect::<Vec<_>>()
.into_iter()
.collect();
let functions = function_body_inputs
let (functions, fdes): (Vec<CompiledFunction>, Vec<_>) = function_body_inputs
.iter()
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
.into_par_iter_if_rayon()
Expand Down Expand Up @@ -185,9 +215,9 @@ impl Compiler for SinglepassCompiler {
_ => unimplemented!(),
}
})
.collect::<Result<Vec<CompiledFunction>, CompileError>>()?
.collect::<Result<Vec<_>, CompileError>>()?
.into_iter()
.collect::<PrimaryMap<LocalFunctionIndex, CompiledFunction>>();
.unzip();

let function_call_trampolines = module
.signatures
Expand Down Expand Up @@ -215,12 +245,31 @@ impl Compiler for SinglepassCompiler {
.into_iter()
.collect::<PrimaryMap<FunctionIndex, FunctionBody>>();

#[cfg(feature = "unwind")]
let dwarf = if let Some((mut dwarf_frametable, cie_id)) = dwarf_frametable {
for fde in fdes {
if let Some(fde) = fde {
dwarf_frametable.add_fde(cie_id, fde);
}
}
let mut eh_frame = EhFrame(WriterRelocate::new(target.triple().endianness().ok()));
dwarf_frametable.write_eh_frame(&mut eh_frame).unwrap();

let eh_frame_section = eh_frame.0.into_section();
custom_sections.push(eh_frame_section);
Some(Dwarf::new(SectionIndex::new(custom_sections.len() - 1)))
} else {
None
};
#[cfg(not(feature = "unwind"))]
let dwarf = None;

Ok(Compilation::new(
functions,
import_trampolines,
functions.into_iter().collect(),
custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
None,
dwarf,
))
}
}
Expand Down
102 changes: 102 additions & 0 deletions lib/compiler-singlepass/src/dwarf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use gimli::write::{Address, EndianVec, Result, Writer};
use gimli::{RunTimeEndian, SectionId};
use wasmer_compiler::{CustomSection, CustomSectionProtection, SectionBody};
use wasmer_compiler::{Endianness, Relocation, RelocationKind, RelocationTarget};
use wasmer_types::entity::EntityRef;
use wasmer_types::LocalFunctionIndex;

#[derive(Clone, Debug)]
pub struct WriterRelocate {
pub relocs: Vec<Relocation>,
writer: EndianVec<RunTimeEndian>,
}

impl WriterRelocate {
pub const FUNCTION_SYMBOL: usize = 0;
pub fn new(endianness: Option<Endianness>) -> Self {
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
let endianness = match endianness {
Some(Endianness::Little) => RunTimeEndian::Little,
Some(Endianness::Big) => RunTimeEndian::Big,
// We autodetect it, based on the host
None => RunTimeEndian::default(),
};
WriterRelocate {
relocs: Vec::new(),
writer: EndianVec::new(endianness),
}
}

pub fn into_section(mut self) -> CustomSection {
// GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
self.writer.write_u32(0).unwrap();
let data = self.writer.into_vec();
CustomSection {
protection: CustomSectionProtection::Read,
bytes: SectionBody::new_with_vec(data),
relocations: self.relocs,
}
}
}

impl Writer for WriterRelocate {
type Endian = RunTimeEndian;

fn endian(&self) -> Self::Endian {
self.writer.endian()
}

fn len(&self) -> usize {
self.writer.len()
}

fn write(&mut self, bytes: &[u8]) -> Result<()> {
self.writer.write(bytes)
}

fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
self.writer.write_at(offset, bytes)
}

fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
match address {
Address::Constant(val) => self.write_udata(val, size),
Address::Symbol { symbol, addend } => {
// Is a function relocation
if symbol == Self::FUNCTION_SYMBOL {
// We use the addend to detect the function index
let function_index = LocalFunctionIndex::new(addend as _);
let reloc_target = RelocationTarget::LocalFunc(function_index);
let offset = self.len() as u32;
let kind = match size {
8 => RelocationKind::Abs8,
_ => unimplemented!("dwarf relocation size not yet supported: {}", size),
};
let addend = 0;
self.relocs.push(Relocation {
kind,
reloc_target,
offset,
addend,
});
self.write_udata(addend as u64, size)
} else {
unreachable!("Symbol {} in DWARF not recognized", symbol);
}
}
}
}

fn write_offset(&mut self, _val: usize, _section: SectionId, _size: u8) -> Result<()> {
unimplemented!("write_offset not yet implemented");
}

fn write_offset_at(
&mut self,
_offset: usize,
_val: usize,
_section: SectionId,
_size: u8,
) -> Result<()> {
unimplemented!("write_offset_at not yet implemented");
}
}
3 changes: 3 additions & 0 deletions lib/compiler-singlepass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ mod codegen;
mod common_decl;
mod compiler;
mod config;
#[cfg(feature = "unwind")]
mod dwarf;
mod emitter_arm64;
mod emitter_x64;
mod location;
mod machine;
mod machine_arm64;
mod machine_x64;
mod unwind;
mod x64_decl;

pub use crate::compiler::SinglepassCompiler;
Expand Down
13 changes: 13 additions & 0 deletions lib/compiler-singlepass/src/machine_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::emitter_x64::*;
use crate::location::Location as AbstractLocation;
use crate::location::Reg;
use crate::machine::*;
use crate::unwind::UnwindOps;
use crate::x64_decl::new_machine_state;
use crate::x64_decl::{ArgumentRegisterAllocator, X64Register, GPR, XMM};
use dynasmrt::{x64::X64Relocation, DynasmError, VecAssembler};
Expand Down Expand Up @@ -66,6 +67,8 @@ pub struct MachineX86_64 {
instructions_address_map: Vec<InstructionAddressMap>,
/// The source location for the current operator.
src_loc: u32,
/// Vector of unwind operations with offset
unwind_ops: Vec<(usize, UnwindOps)>,
}

impl MachineX86_64 {
Expand All @@ -77,6 +80,7 @@ impl MachineX86_64 {
trap_table: TrapTable::default(),
instructions_address_map: vec![],
src_loc: 0,
unwind_ops: vec![],
}
}
pub fn emit_relaxed_binop(
Expand Down Expand Up @@ -2180,7 +2184,16 @@ impl Machine for MachineX86_64 {

fn emit_function_prolog(&mut self) {
self.emit_push(Size::S64, Location::GPR(GPR::RBP));
self.unwind_ops
.push((self.get_offset().0, UnwindOps::PushFP { up_to_sp: 16 }));
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
self.move_location(Size::S64, Location::GPR(GPR::RSP), Location::GPR(GPR::RBP));
self.unwind_ops.push((
self.get_offset().0,
UnwindOps::DefineNewFrame {
up_to_sp: 0,
down_to_clobber: 0,
},
));
}

fn emit_function_epilog(&mut self) {
Expand Down
32 changes: 32 additions & 0 deletions lib/compiler-singlepass/src/unwind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use gimli::{write::CallFrameInstruction, write::CommonInformationEntry, Encoding, Format, X86_64};
use std::fmt::Debug;
use wasmer_compiler::Architecture;

#[derive(Clone, Debug)]
pub enum UnwindOps {
PushFP { up_to_sp: u32 },
DefineNewFrame { up_to_sp: u32, down_to_clobber: u32 },
}

/// generate a default systemv cie
pub fn create_systemv_cie(arch: Architecture) -> Option<gimli::write::CommonInformationEntry> {
match arch {
Architecture::X86_64 => {
let mut entry = CommonInformationEntry::new(
Encoding {
address_size: 8,
format: Format::Dwarf32,
version: 1,
},
1,
-8,
X86_64::RA,
);
entry.add_instruction(CallFrameInstruction::Cfa(X86_64::RSP, 8));
entry.add_instruction(CallFrameInstruction::Offset(X86_64::RA, -8));
Some(entry)
}
Architecture::Aarch64(_) => None,
_ => None,
}
}
Loading