Skip to content

Commit b957cc0

Browse files
committed
[lldb] Provide lr value in faulting frame on arm64 (#138805)
Re-landing this patch with small tweaks to address CI bot failures as it was run on many different configurations. I think the test may run on aarch64 Linux systems now. When a frameless function faults or is interrupted asynchronously, the UnwindPlan MAY have no register location rule for the return address register (lr on arm64); the value is simply live in the lr register when it was interrupted, and the frame below this on the stack -- e.g. sigtramp on a Unix system -- has the full register context, including that register. RegisterContextUnwind::SavedLocationForRegister, when asked to find the caller's pc value, will first see if there is a pc register location. If there isn't, on a Return Address Register architecture like arm/mips/riscv, we rewrite the register request from "pc" to "RA register", and search for a location. On frame 0 (the live frame) and an interrupted frame, the UnwindPlan may have no register location rule for the RA Reg, that is valid. A frameless function that never calls another may simply keep the return address in the live register the whole way. Our instruction emulation unwind plans explicitly add a rule (see Pavel's May 2024 change #91321 ), but an UnwindPlan sourced from debug_frame may not. I've got a case where this exactly happens - clang debug_frame for arm64 where there is no register location for the lr in a frameless function. There is a fault in the middle of this frameless function and we only get the lr value from the fault handler below this frame if lr has a register location of `IsSame`, in line with Pavel's 2024 change. Similar to how we see a request of the RA Reg from frame 0 after failing to find an unwind location for the pc register, the same style of special casing is needed when this is a function that was interrupted. Without this change, we can find the pc of the frame that was executing when it was interrupted, but we need $lr to find its caller, and we don't descend down to the trap handler to get that value, truncating the stack. rdar://145614545
1 parent 87b4cac commit b957cc0

File tree

5 files changed

+306
-10
lines changed

5 files changed

+306
-10
lines changed

lldb/source/Target/RegisterContextUnwind.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void RegisterContextUnwind::InitializeZerothFrame() {
248248
active_row =
249249
m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
250250
row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
251+
PropagateTrapHandlerFlagFromUnwindPlan(m_full_unwind_plan_sp);
251252
if (active_row && log) {
252253
StreamString active_row_strm;
253254
active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
@@ -279,7 +280,7 @@ void RegisterContextUnwind::InitializeZerothFrame() {
279280
call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
280281
process->GetTarget(), m_thread);
281282

282-
if (call_site_unwind_plan != nullptr) {
283+
if (call_site_unwind_plan.get() != nullptr) {
283284
m_fallback_unwind_plan_sp = call_site_unwind_plan;
284285
if (TryFallbackUnwindPlan())
285286
cfa_status = true;
@@ -1375,6 +1376,7 @@ RegisterContextUnwind::SavedLocationForRegister(
13751376
}
13761377
}
13771378

1379+
// Check if the active_row has a register location listed.
13781380
if (regnum.IsValid() && active_row &&
13791381
active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
13801382
unwindplan_regloc)) {
@@ -1388,11 +1390,10 @@ RegisterContextUnwind::SavedLocationForRegister(
13881390
// This is frame 0 and we're retrieving the PC and it's saved in a Return
13891391
// Address register and it hasn't been saved anywhere yet -- that is,
13901392
// it's still live in the actual register. Handle this specially.
1391-
13921393
if (!have_unwindplan_regloc && return_address_reg.IsValid() &&
1393-
IsFrameZero()) {
1394-
if (return_address_reg.GetAsKind(eRegisterKindLLDB) !=
1395-
LLDB_INVALID_REGNUM) {
1394+
return_address_reg.GetAsKind(eRegisterKindLLDB) !=
1395+
LLDB_INVALID_REGNUM) {
1396+
if (IsFrameZero()) {
13961397
lldb_private::UnwindLLDB::ConcreteRegisterLocation new_regloc;
13971398
new_regloc.type = UnwindLLDB::ConcreteRegisterLocation::
13981399
eRegisterInLiveRegisterContext;
@@ -1406,6 +1407,17 @@ RegisterContextUnwind::SavedLocationForRegister(
14061407
return_address_reg.GetAsKind(eRegisterKindLLDB),
14071408
return_address_reg.GetAsKind(eRegisterKindLLDB));
14081409
return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1410+
} else if (BehavesLikeZerothFrame()) {
1411+
// This function was interrupted asynchronously -- it faulted,
1412+
// an async interrupt, a timer fired, a debugger expression etc.
1413+
// The caller's pc is in the Return Address register, but the
1414+
// UnwindPlan for this function may have no location rule for
1415+
// the RA reg.
1416+
// This means that the caller's return address is in the RA reg
1417+
// when the function was interrupted--descend down one stack frame
1418+
// to retrieve it from the trap handler's saved context.
1419+
unwindplan_regloc.SetSame();
1420+
have_unwindplan_regloc = true;
14091421
}
14101422
}
14111423

@@ -1722,10 +1734,10 @@ RegisterContextUnwind::SavedLocationForRegister(
17221734
// tricky frame and our usual techniques can continue to be used.
17231735

17241736
bool RegisterContextUnwind::TryFallbackUnwindPlan() {
1725-
if (m_fallback_unwind_plan_sp == nullptr)
1737+
if (m_fallback_unwind_plan_sp.get() == nullptr)
17261738
return false;
17271739

1728-
if (m_full_unwind_plan_sp == nullptr)
1740+
if (m_full_unwind_plan_sp.get() == nullptr)
17291741
return false;
17301742

17311743
if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
@@ -1773,7 +1785,7 @@ bool RegisterContextUnwind::TryFallbackUnwindPlan() {
17731785
// fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
17741786
// at the top -- the only way it became nullptr since then is via
17751787
// SavedLocationForRegister().
1776-
if (m_fallback_unwind_plan_sp == nullptr)
1788+
if (m_fallback_unwind_plan_sp.get() == nullptr)
17771789
return true;
17781790

17791791
// Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
@@ -1862,10 +1874,10 @@ bool RegisterContextUnwind::TryFallbackUnwindPlan() {
18621874
}
18631875

18641876
bool RegisterContextUnwind::ForceSwitchToFallbackUnwindPlan() {
1865-
if (m_fallback_unwind_plan_sp == nullptr)
1877+
if (m_fallback_unwind_plan_sp.get() == nullptr)
18661878
return false;
18671879

1868-
if (m_full_unwind_plan_sp == nullptr)
1880+
if (m_full_unwind_plan_sp.get() == nullptr)
18691881
return false;
18701882

18711883
if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
@@ -1922,6 +1934,7 @@ void RegisterContextUnwind::PropagateTrapHandlerFlagFromUnwindPlan(
19221934
}
19231935

19241936
m_frame_type = eTrapHandlerFrame;
1937+
UnwindLogMsg("This frame is marked as a trap handler via its UnwindPlan");
19251938

19261939
if (m_current_offset_backed_up_one != m_current_offset) {
19271940
// We backed up the pc by 1 to compute the symbol context, but
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
C_SOURCES := main.c
2+
3+
interrupt-and-trap-funcs.o: interrupt-and-trap-funcs.s
4+
$(CC) $(CFLAGS) -E -o interrupt-and-trap-funcs.s $(SRCDIR)/interrupt-and-trap-funcs.s
5+
$(CC) $(CFLAGS) -c -o interrupt-and-trap-funcs.o interrupt-and-trap-funcs.s
6+
7+
include Makefile.rules
8+
9+
a.out: interrupt-and-trap-funcs.o
10+
11+
# Needs to come after include
12+
OBJECTS += interrupt-and-trap-funcs.o
13+
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""Test that lldb backtraces a frameless function that faults correctly."""
2+
3+
import lldbsuite.test.lldbutil as lldbutil
4+
from lldbsuite.test.lldbtest import *
5+
from lldbsuite.test.decorators import *
6+
import shutil
7+
import os
8+
9+
10+
class TestUnwindFramelessFaulted(TestBase):
11+
NO_DEBUG_INFO_TESTCASE = True
12+
13+
@skipIf(oslist=no_match([lldbplatformutil.getDarwinOSTriples(), "linux"]))
14+
@skipIf(archs=no_match(["aarch64", "arm64", "arm64e"]))
15+
16+
# The static linker in Xcode 15.0-15.2 on macOS 14 will mislink
17+
# the eh_frame addresses; ld-classic in those tools is one workaround.
18+
# This issue was fixed in Xcode 15.3, but it's not straightforward
19+
# to test for the linker version or Xcode version so tie this to
20+
# macOS 15 which uses Xcode 16 and does not have the issues.
21+
@skipIf(macos_version=["<", "15.0"])
22+
23+
def test_frameless_faulted_unwind(self):
24+
self.build()
25+
26+
(target, process, thread, bp) = lldbutil.run_to_name_breakpoint(
27+
self, "main", only_one_thread=False
28+
)
29+
30+
# The test program will have a backtrace like this at its deepest:
31+
#
32+
# * frame #0: 0x0000000102adc468 a.out`break_to_debugger + 4
33+
# frame #1: 0x0000000102adc458 a.out`trap + 16
34+
# frame #2: 0x0000000102adc440 a.out`to_be_interrupted + 20
35+
# frame #3: 0x0000000102adc418 a.out`main at main.c:4:7
36+
# frame #4: 0x0000000193b7eb4c dyld`start + 6000
37+
38+
correct_frames = ["break_to_debugger", "trap", "to_be_interrupted", "main"]
39+
40+
# Keep track of when main has branch & linked, instruction step until we're
41+
# back in main()
42+
main_has_bl_ed = False
43+
44+
# Instruction step through the binary until we are in a function not
45+
# listed in correct_frames.
46+
frame = thread.GetFrameAtIndex(0)
47+
step_count = 0
48+
max_step_count = 200
49+
while (
50+
process.GetState() == lldb.eStateStopped
51+
and frame.name in correct_frames
52+
and step_count < max_step_count
53+
):
54+
starting_index = 0
55+
if self.TraceOn():
56+
self.runCmd("bt")
57+
58+
# Find which index into correct_frames the current stack frame is
59+
for idx, name in enumerate(correct_frames):
60+
if frame.name == name:
61+
starting_index = idx
62+
63+
# Test that all frames after the current frame listed in
64+
# correct_frames appears in the backtrace.
65+
frame_idx = 0
66+
for expected_frame in correct_frames[starting_index:]:
67+
self.assertEqual(thread.GetFrameAtIndex(frame_idx).name, expected_frame)
68+
frame_idx = frame_idx + 1
69+
70+
# When we're at our deepest level, test that register passing of
71+
# x0 and x20 follow the by-hand UnwindPlan rules.
72+
# In this test program, we can get x0 in the middle of the stack
73+
# and we CAN'T get x20. The opposites of the normal AArch64 SysV
74+
# ABI.
75+
if frame.name == "break_to_debugger":
76+
tbi_frame = thread.GetFrameAtIndex(2)
77+
self.assertEqual(tbi_frame.name, "to_be_interrupted")
78+
# The original argument to to_be_interrupted(), 10
79+
# Normally can't get x0 mid-stack, but UnwindPlans have
80+
# special rules to make this possible.
81+
x0_reg = tbi_frame.register["x0"]
82+
self.assertTrue(x0_reg.IsValid())
83+
self.assertEqual(x0_reg.GetValueAsUnsigned(), 10)
84+
# The incremented return value from to_be_interrupted(), 11
85+
x24_reg = tbi_frame.register["x24"]
86+
self.assertTrue(x24_reg.IsValid())
87+
self.assertEqual(x24_reg.GetValueAsUnsigned(), 11)
88+
# x20 can normally be fetched mid-stack, but the UnwindPlan
89+
# has a rule saying it can't be fetched.
90+
x20_reg = tbi_frame.register["x20"]
91+
self.assertTrue(x20_reg.error.fail)
92+
93+
trap_frame = thread.GetFrameAtIndex(1)
94+
self.assertEqual(trap_frame.name, "trap")
95+
# Confirm that we can fetch x0 from trap() which
96+
# is normally not possible w/ SysV AbI, but special
97+
# UnwindPlans in use.
98+
x0_reg = trap_frame.register["x0"]
99+
self.assertTrue(x0_reg.IsValid())
100+
self.assertEqual(x0_reg.GetValueAsUnsigned(), 10)
101+
x1_reg = trap_frame.register["x1"]
102+
self.assertTrue(x1_reg.error.fail)
103+
104+
main_frame = thread.GetFrameAtIndex(3)
105+
self.assertEqual(main_frame.name, "main")
106+
# x20 can normally be fetched mid-stack, but the UnwindPlan
107+
# has a rule saying it can't be fetched.
108+
x20_reg = main_frame.register["x20"]
109+
self.assertTrue(x20_reg.error.fail)
110+
# x21 can be fetched mid-stack.
111+
x21_reg = main_frame.register["x21"]
112+
self.assertTrue(x21_reg.error.success)
113+
114+
# manually move past the BRK instruction in
115+
# break_to_debugger(). lldb-server doesn't
116+
# advance past the builtin_debugtrap() BRK
117+
# instruction.
118+
if (
119+
thread.GetStopReason() == lldb.eStopReasonException
120+
and frame.name == "break_to_debugger"
121+
):
122+
frame.SetPC(frame.GetPC() + 4)
123+
124+
if self.TraceOn():
125+
print("StepInstruction")
126+
thread.StepInstruction(False)
127+
frame = thread.GetFrameAtIndex(0)
128+
step_count = step_count + 1
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// This is assembly code that needs to be run
2+
// through the preprocessor, for simplicity of
3+
// preprocessing it's named .c to start with.
4+
//
5+
// clang-format off
6+
7+
8+
#define DW_CFA_register 0x9
9+
#define ehframe_x0 0
10+
#define ehframe_x20 20
11+
#define ehframe_x22 22
12+
#define ehframe_x23 23
13+
#define ehframe_pc 32
14+
15+
#if defined(__APPLE__)
16+
#define TO_BE_INTERRUPTED _to_be_interrupted
17+
#define TRAP _trap
18+
#define BREAK_TO_DEBUGGER _break_to_debugger
19+
#else
20+
#define TO_BE_INTERRUPTED to_be_interrupted
21+
#define TRAP trap
22+
#define BREAK_TO_DEBUGGER break_to_debugger
23+
#endif
24+
25+
.text
26+
//--------------------------------------
27+
// to_be_interrupted() a frameless function that does a non-ABI
28+
// function call to trap(), simulating an async signal/interrup/exception/fault.
29+
// Before it branches to trap(), put the return address in x23.
30+
// trap() knows to branch back to $x23 when it has finished.
31+
//--------------------------------------
32+
.globl TO_BE_INTERRUPTED
33+
#if defined(__APPLE__)
34+
.p2align 2
35+
#endif
36+
TO_BE_INTERRUPTED:
37+
.cfi_startproc
38+
39+
// This is a garbage entry to ensure that eh_frame is emitted.
40+
// If there's no eh_frame, lldb can use the assembly emulation scan,
41+
// which always includes a rule for $lr, and we won't replicate the
42+
// bug we're testing for.
43+
.cfi_escape DW_CFA_register, ehframe_x22, ehframe_x23
44+
mov x24, x0
45+
add x24, x24, #1
46+
47+
#if defined(__APPLE__)
48+
adrp x23, L_.return@PAGE // put return address in x23
49+
add x23, x23, L_.return@PAGEOFF
50+
#else
51+
adrp x23, .L.return
52+
add x23, x23, :lo12:.L.return
53+
#endif
54+
55+
b TRAP // branch to trap handler, fake async interrupt
56+
57+
#if defined(__APPLE__)
58+
L_.return:
59+
#else
60+
.L.return:
61+
#endif
62+
mov x0, x24
63+
ret
64+
.cfi_endproc
65+
66+
67+
68+
//--------------------------------------
69+
// trap() trap handler function, sets up stack frame
70+
// with special unwind rule for the pc value of the
71+
// "interrupted" stack frame (it's in x23), then calls
72+
// break_to_debugger().
73+
//--------------------------------------
74+
.globl TRAP
75+
#if defined(__APPLE__)
76+
.p2align 2
77+
#endif
78+
TRAP:
79+
.cfi_startproc
80+
.cfi_signal_frame
81+
82+
// The pc value when we were interrupted is in x23
83+
.cfi_escape DW_CFA_register, ehframe_pc, ehframe_x23
84+
85+
// For fun, mark x0 as unmodified so the caller can
86+
// retrieve the value if it wants.
87+
.cfi_same_value ehframe_x0
88+
89+
// Mark x20 as undefined. This is a callee-preserved
90+
// (non-volatile) register by the SysV AArch64 ABI, but
91+
// it'll be fun to see lldb not passing a value past this
92+
// point on the stack.
93+
.cfi_undefined ehframe_x20
94+
95+
// standard prologue save of fp & lr so we can call
96+
// break_to_debugger()
97+
sub sp, sp, #32
98+
stp x29, x30, [sp, #16]
99+
add x29, sp, #16
100+
.cfi_def_cfa w29, 16
101+
.cfi_offset w30, -8
102+
.cfi_offset w29, -16
103+
104+
bl BREAK_TO_DEBUGGER
105+
106+
ldp x29, x30, [sp, #16]
107+
.cfi_same_value x29
108+
.cfi_same_value x30
109+
.cfi_def_cfa sp, 32
110+
add sp, sp, #32
111+
.cfi_same_value sp
112+
.cfi_def_cfa sp, 0
113+
114+
// jump back to $x23 to resume execution of to_be_interrupted
115+
br x23
116+
.cfi_endproc
117+
118+
//--------------------------------------
119+
// break_to_debugger() executes a BRK instruction
120+
//--------------------------------------
121+
.globl BREAK_TO_DEBUGGER
122+
#if defined(__APPLE__)
123+
.p2align 2
124+
#endif
125+
BREAK_TO_DEBUGGER:
126+
.cfi_startproc
127+
128+
// For fun, mark x0 as unmodified so the caller can
129+
// retrieve the value if it wants.
130+
.cfi_same_value ehframe_x0
131+
132+
brk #0xf000 // __builtin_debugtrap aarch64 instruction
133+
134+
ret
135+
.cfi_endproc
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int to_be_interrupted(int);
2+
3+
int main() {
4+
int c = 10;
5+
c = to_be_interrupted(c);
6+
return c;
7+
}

0 commit comments

Comments
 (0)