From 93082b3d52b4b2489e75be80347a0ea2a0ddb607 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Mon, 18 May 2020 15:16:27 +0300 Subject: [PATCH] ntdll: Fix arm64 unwind across ELF/PE boundaries. The previous implementation might have given the impression of working, as long in some cases where the PE code actually used frame pointers, but turned out to be subly wrong. This essentially reverts the functional aspects of 1c9fdaab0f4. Use the new value of the Lr register, after fetching the registers from unw_step, as the return value. To make single-stepping unwinding work properly, treat the registers consistently: - Make RtlCaptureContext store the current values of x29/Fp and x30/Lr from within the function, not the ones backed up from the stack. - After unwinding one step, first fetch the new values of all registers, including the new value of Lr - then use this value of Lr to set the new value of Pc (the address to actually return to). This makes the unwinding actually coherent in reading unwind opcodes and return addresses from one single function; previously these were out of sync where the return address ended up being read from the function one step further up in the call stack. This fixes unwinding for setjmp for binaries compiled with clang (in mingw mode). Signed-off-by: Martin Storsjo Signed-off-by: Alexandre Julliard --- dlls/ntdll/signal_arm64.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dlls/ntdll/signal_arm64.c b/dlls/ntdll/signal_arm64.c index 9f84d3fbfd1..30d445b4081 100644 --- a/dlls/ntdll/signal_arm64.c +++ b/dlls/ntdll/signal_arm64.c @@ -265,8 +265,7 @@ __ASM_STDCALL_FUNC( RtlCaptureContext, 8, "stp x23, x24, [x0, #0xc0]\n\t" /* context->X23,X24 */ "stp x25, x26, [x0, #0xd0]\n\t" /* context->X25,X26 */ "stp x27, x28, [x0, #0xe0]\n\t" /* context->X27,X28 */ - "ldp x1, x2, [x29]\n\t" - "stp x1, x2, [x0, #0xf0]\n\t" /* context->Fp,Lr */ + "stp x29, x30, [x0, #0xf0]\n\t" /* context->Fp,Lr */ "add x1, x29, #0x10\n\t" "stp x1, x30, [x0, #0x100]\n\t" /* context->Sp,Pc */ "mov w1, #0x400000\n\t" /* CONTEXT_ARM64 */ @@ -545,7 +544,6 @@ static NTSTATUS libunwind_virtual_unwind( ULONG_PTR ip, ULONG_PTR *frame, CONTEX *handler = (void *)info.handler; *handler_data = (void *)info.lsda; *frame = context->Sp; - context->Pc = context->u.s.Lr; unw_get_reg( &cursor, UNW_AARCH64_X0, (unw_word_t *)&context->u.s.X0 ); unw_get_reg( &cursor, UNW_AARCH64_X1, (unw_word_t *)&context->u.s.X1 ); unw_get_reg( &cursor, UNW_AARCH64_X2, (unw_word_t *)&context->u.s.X2 ); @@ -578,6 +576,7 @@ static NTSTATUS libunwind_virtual_unwind( ULONG_PTR ip, ULONG_PTR *frame, CONTEX unw_get_reg( &cursor, UNW_AARCH64_X29, (unw_word_t *)&context->u.s.Fp ); unw_get_reg( &cursor, UNW_AARCH64_X30, (unw_word_t *)&context->u.s.Lr ); unw_get_reg( &cursor, UNW_AARCH64_SP, (unw_word_t *)&context->Sp ); + context->Pc = context->u.s.Lr; TRACE( "next function pc=%016lx%s\n", context->Pc, rc ? "" : " (last frame)" ); TRACE(" x0=%016lx x1=%016lx x2=%016lx x3=%016lx\n",