Skip to content

Commit

Permalink
Merge pull request #4559 from xdoardo/4519-singlepass-panics-impossib…
Browse files Browse the repository at this point in the history
…lerelocation

Fix ImpossibleRelocation panics in singlepass/aarch64
  • Loading branch information
xdoardo authored Apr 15, 2024
2 parents a5736af + 23c1bda commit 47f580f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
36 changes: 36 additions & 0 deletions lib/compiler-singlepass/src/emitter_arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ pub trait EmitterARM64 {
reg: Location,
label: Label,
) -> Result<(), CompileError>;
fn emit_cbz_label_far(
&mut self,
sz: Size,
reg: Location,
label: Label,
) -> Result<(), CompileError>;
fn emit_tbz_label(
&mut self,
sz: Size,
Expand Down Expand Up @@ -2607,6 +2613,36 @@ impl EmitterARM64 for Assembler {
}
Ok(())
}
fn emit_cbz_label_far(
&mut self,
sz: Size,
reg: Location,
label: Label,
) -> Result<(), CompileError> {
let near_label: Label = self.get_label();
let continue_label: Label = self.get_label();

match (sz, reg) {
(Size::S32, Location::GPR(reg)) => {
let reg = reg.into_index() as u32;

dynasm!(self ; cbz W(reg), => near_label);
dynasm!(self ; b => continue_label);
}
(Size::S64, Location::GPR(reg)) => {
let reg = reg.into_index() as u32;
dynasm!(self ; cbz W(reg), => label);
dynasm!(self ; b => continue_label);
}
_ => codegen_error!("singlepass can't emit CBZ {:?} {:?} {:?}", sz, reg, label),
}
self.emit_label(near_label)?;
dynasm!(self ; b => label );

self.emit_label(continue_label)?;

Ok(())
}
fn emit_tbz_label(
&mut self,
sz: Size,
Expand Down
8 changes: 4 additions & 4 deletions lib/compiler-singlepass/src/machine_arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,7 +2810,7 @@ impl Machine for MachineARM64 {
let dest = self.location_to_reg(Size::S32, ret, &mut temps, ImmType::None, false, None)?;

self.assembler
.emit_cbz_label(Size::S32, src2, integer_division_by_zero)?;
.emit_cbz_label_far(Size::S32, src2, integer_division_by_zero)?;
let offset = self.mark_instruction_with_trap_code(TrapCode::IntegerOverflow);
self.assembler.emit_udiv(Size::S32, src1, src2, dest)?;
if ret != dest {
Expand All @@ -2835,7 +2835,7 @@ impl Machine for MachineARM64 {
let dest = self.location_to_reg(Size::S32, ret, &mut temps, ImmType::None, false, None)?;

self.assembler
.emit_cbz_label(Size::S32, src2, integer_division_by_zero)?;
.emit_cbz_label_far(Size::S32, src2, integer_division_by_zero)?;
let label_nooverflow = self.assembler.get_label();
let tmp = self.location_to_reg(
Size::S32,
Expand Down Expand Up @@ -2887,7 +2887,7 @@ impl Machine for MachineARM64 {
dest
};
self.assembler
.emit_cbz_label(Size::S32, src2, integer_division_by_zero)?;
.emit_cbz_label_far(Size::S32, src2, integer_division_by_zero)?;
let offset = self.mark_instruction_with_trap_code(TrapCode::IntegerOverflow);
self.assembler.emit_udiv(Size::S32, src1, src2, dest)?;
// unsigned remainder : src1 - (src1/src2)*src2
Expand Down Expand Up @@ -2925,7 +2925,7 @@ impl Machine for MachineARM64 {
dest
};
self.assembler
.emit_cbz_label(Size::S32, src2, integer_division_by_zero)?;
.emit_cbz_label_far(Size::S32, src2, integer_division_by_zero)?;
let offset = self.mark_instruction_with_trap_code(TrapCode::IntegerOverflow);
self.assembler.emit_sdiv(Size::S32, src1, src2, dest)?;
// unsigned remainder : src1 - (src1/src2)*src2
Expand Down
Binary file added tests/compilers/data/4519_singlepass_panic.wasm
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/compilers/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,20 @@ fn large_number_local(mut config: crate::Config) -> Result<()> {
assert_eq!(&Value::I64(1_i64), result.get(0).unwrap());
Ok(())
}

#[cfg(target_arch = "aarch64")]
#[compiler_test(issues)]
/// Singlepass panics on aarch64 for long relocations.
///
/// Note: this one is specific to Singlepass, but we want to test in all
/// available compilers.
///
/// https://github.com/wasmerio/wasmer/issues/4519
fn issue_4519(mut config: crate::Config) -> Result<()> {
let wasm = include_bytes!("./data/4519_singlepass_panic.wasm");

let mut store = config.store();
let module = Module::new(&store, wasm)?;

Ok(())
}

0 comments on commit 47f580f

Please sign in to comment.