From 4288a141b14350f1c4db02a196a6c3f427d0f19a Mon Sep 17 00:00:00 2001 From: Quentin <837334+xiaoxiao921@users.noreply.github.com> Date: Sun, 7 Dec 2025 20:21:11 +0100 Subject: [PATCH 1/2] Fix stack misalignment before calls in get_win32_restore_stack thunk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The thunk pushed RBP (−8) and then reserved only 32 bytes of shadow space, leaving RSP misaligned by 8 bytes. This violated the Win64 ABI requirement that RSP be 16-byte aligned at every call instruction. As a result, the calls to _resetstkoflw, mono_tls_get_jit_tls, and mono_restore_context were executed with an unaligned stack. Increase the reservation to 40 bytes (32 shadow + 8 alignment fixup) to restore correct 16-byte alignment after the initial push and ensure all call sites are ABI-compliant. --- mono/mini/mini-windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mono/mini/mini-windows.c b/mono/mini/mini-windows.c index a3ec782159e2..75cf830a8d46 100644 --- a/mono/mini/mini-windows.c +++ b/mono/mini/mini-windows.c @@ -125,7 +125,7 @@ get_win32_restore_stack (void) amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8); /* push 32 bytes of stack space for Win64 calling convention */ - amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32); + amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 40); /* restore guard page */ amd64_mov_reg_imm (code, AMD64_R11, _resetstkoflw); From b88b47f979c894b1fd3126419b22303835156cdb Mon Sep 17 00:00:00 2001 From: Quentin <837334+xiaoxiao921@users.noreply.github.com> Date: Sun, 7 Dec 2025 20:32:28 +0100 Subject: [PATCH 2/2] Update comment for stack alignment in Win64 --- mono/mini/mini-windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mono/mini/mini-windows.c b/mono/mini/mini-windows.c index 75cf830a8d46..5d534a83c246 100644 --- a/mono/mini/mini-windows.c +++ b/mono/mini/mini-windows.c @@ -124,7 +124,7 @@ get_win32_restore_stack (void) amd64_push_reg (code, AMD64_RBP); amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8); - /* push 32 bytes of stack space for Win64 calling convention */ + /* Align stack for Win64 calling convention */ amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 40); /* restore guard page */