Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Store immediate 0 during stack probes instead of writing %rsp (#11840)

jiang19977f8607eb742b
diff --git a/cranelift/codegen/src/isa/x64/abi.rs b/cranelift/codegen/src/isa/x64/abi.rs
index f29e0c4d0e..a29d8a478d 100644
--- a/cranelift/codegen/src/isa/x64/abi.rs
+++ b/cranelift/codegen/src/isa/x64/abi.rs
@@ -33,13 +33,12 @@ impl X64ABIMachineSpec {
// See: https://github.com/bytecodealliance/wasmtime/issues/7454
insts.extend(Self::gen_sp_reg_adjust(-(guard_size as i32)));
- // TODO: It would be nice if we could store the imm 0, but we don't have insts for those
- // so store the stack pointer. Any register will do, since the stack is undefined at this point
- insts.push(Inst::store(
- I32,
- regs::rsp(),
- Amode::imm_reg(0, regs::rsp()),
- ));
+ // Touch the current page by storing an immediate zero.
+ // mov [rsp], 0
+ insts.push(Inst::External {
+ inst: asm::inst::movl_mi::new(Amode::imm_reg(0, regs::rsp()), 0i32.cast_unsigned())
+ .into(),
+ });
}
// Restore the stack pointer to its original value
diff --git a/cranelift/codegen/src/isa/x64/inst/emit.rs b/cranelift/codegen/src/isa/x64/inst/emit.rs
index a922cf7377..e4afbd2a14 100644
--- a/cranelift/codegen/src/isa/x64/inst/emit.rs
+++ b/cranelift/codegen/src/isa/x64/inst/emit.rs
@@ -358,7 +358,7 @@ pub(crate) fn emit(
// sub tmp_reg, guard_size * probe_count
// .loop_start:
// sub rsp, guard_size
- // mov [rsp], rsp
+ // mov [rsp], 0
// cmp rsp, tmp_reg
// jne .loop_start
// add rsp, guard_size * probe_count
@@ -383,11 +383,9 @@ pub(crate) fn emit(
.expect("`guard_size` is too large to fit in a 32-bit immediate");
Inst::subq_mi(rsp, guard_size_).emit(sink, info, state);
- // TODO: `mov [rsp], 0` would be better, but we don't have that instruction
- // Probe the stack! We don't use Inst::gen_store_stack here because we need a predictable
- // instruction size.
- // mov [rsp], rsp
- asm::inst::movl_mr::new(Amode::imm_reg(0, regs::rsp()), Gpr::RSP)
+ // Touch the current page by storing an immediate zero.
+ // mov [rsp], 0
+ asm::inst::movl_mi::new(Amode::imm_reg(0, regs::rsp()), 0i32.cast_unsigned())
.emit(sink, info, state);
// Compare and jump if we are not done yet
diff --git a/cranelift/filetests/filetests/isa/x64/inline-probestack-large.clif b/cranelift/filetests/filetests/isa/x64/inline-probestack-large.clif
index c4a456b2d9..61d415a7d7 100644
--- a/cranelift/filetests/filetests/isa/x64/inline-probestack-large.clif
+++ b/cranelift/filetests/filetests/isa/x64/inline-probestack-large.clif
@@ -51,11 +51,11 @@ block0:
; pushq %rbp
; movq %rsp, %rbp
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; addq $0x30000, %rsp
; subq $0x30000, %rsp
; block0:
@@ -70,14 +70,14 @@ block0:
; pushq %rbp
; movq %rsp, %rbp
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; addq $0x30000, %rsp
; subq $0x30000, %rsp
-; block1: ; offset 0x30
+; block1: ; offset 0x3c
; leaq (%rsp), %rax
; addq $0x30000, %rsp
; movq %rbp, %rsp
@@ -111,15 +111,14 @@ block0:
; movq %rsp, %r11
; subq $0x200000, %r11
; subq $0x10000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; cmpq %rsp, %r11
; jne 0xe
; addq $0x200000, %rsp
; subq $0x200000, %rsp
-; block1: ; offset 0x2f
+; block1: ; offset 0x33
; leaq (%rsp), %rax
; addq $0x200000, %rsp
; movq %rbp, %rsp
; popq %rbp
; retq
-
diff --git a/cranelift/filetests/filetests/isa/x64/inline-probestack.clif b/cranelift/filetests/filetests/isa/x64/inline-probestack.clif
index c5af1c894b..b63d566bec 100644
--- a/cranelift/filetests/filetests/isa/x64/inline-probestack.clif
+++ b/cranelift/filetests/filetests/isa/x64/inline-probestack.clif
@@ -50,11 +50,11 @@ block0:
; pushq %rbp
; movq %rsp, %rbp
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0x0, (%rsp)
; addq $0x3000, %rsp
; subq $0x3000, %rsp
; block0:
@@ -69,14 +69,14 @@ block0:
; pushq %rbp
; movq %rsp, %rbp
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; addq $0x3000, %rsp
; subq $0x3000, %rsp
-; block1: ; offset 0x30
+; block1: ; offset 0x3c
; leaq (%rsp), %rax
; addq $0x3000, %rsp
; movq %rbp, %rsp
@@ -110,15 +110,14 @@ block0:
; movq %rsp, %r11
; subq $0x19000, %r11
; subq $0x1000, %rsp
-; movl %esp, (%rsp)
+; movl $0, (%rsp)
; cmpq %rsp, %r11
; jne 0xe
; addq $0x19000, %rsp
; subq $0x186a0, %rsp
-; block1: ; offset 0x2f
+; block1: ; offset 0x33
; leaq (%rsp), %rax
; addq $0x186a0, %rsp
; movq %rbp, %rsp
; popq %rbp
; retq
-