Skip to content

Commit

Permalink
Fix stack probing in the singlepass compiler
Browse files Browse the repository at this point in the history
Stack probes must be done before the stack pointer is adjusted. This
ensures that the stack pointer is still within the bounds of the stack
when inspected by the signal handler.
  • Loading branch information
Amanieu authored and ptitSeb committed Oct 20, 2022
1 parent acf826c commit 47ac725
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/compiler-singlepass/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@ impl<'a, M: Machine> FuncGen<'a, M> {

// Allocate save area, without actually writing to it.
static_area_size = self.machine.round_stack_adjust(static_area_size);

// Stack probe.
//
// `rep stosq` writes data from low address to high address and may skip the stack guard page.
// so here we probe it explicitly when needed.
for i in (sig.params().len()..n)
.step_by(NATIVE_PAGE_SIZE / 8)
.skip(0)
{
self.machine.zero_location(Size::S64, locations[i]);
}

self.machine.adjust_stack(static_area_size as _);

// Save callee-saved registers.
Expand Down Expand Up @@ -586,17 +598,6 @@ impl<'a, M: Machine> FuncGen<'a, M> {
Location::GPR(self.machine.get_vmctx_reg()),
);

// Stack probe.
//
// `rep stosq` writes data from low address to high address and may skip the stack guard page.
// so here we probe it explicitly when needed.
for i in (sig.params().len()..n)
.step_by(NATIVE_PAGE_SIZE / 8)
.skip(1)
{
self.machine.zero_location(Size::S64, locations[i]);
}

// Initialize all normal locals to zero.
let mut init_stack_loc_cnt = 0;
let mut last_stack_loc = Location::Memory(self.machine.local_pointer(), i32::MAX);
Expand Down

0 comments on commit 47ac725

Please sign in to comment.