From 47ac725d9a3f5d688546c301cb162f03d089eb0e Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Fri, 25 Feb 2022 16:42:34 +0000 Subject: [PATCH] Fix stack probing in the singlepass compiler 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. --- lib/compiler-singlepass/src/codegen.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/compiler-singlepass/src/codegen.rs b/lib/compiler-singlepass/src/codegen.rs index 81ba5ecb9de..afec6f37cc3 100644 --- a/lib/compiler-singlepass/src/codegen.rs +++ b/lib/compiler-singlepass/src/codegen.rs @@ -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. @@ -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);