Skip to content

Commit fc14bbe

Browse files
[lldb] Track CFA pointer metadata in StackID (llvm#157498)
In this commit: 9c8e716 [lldb] Make StackID call Fix{Code,Data} pointers (llvm#152796) We made StackID keep track of the CFA without any pointer metadata in it. This is necessary when comparing two StackIDs to determine which one is "younger". However, the CFA inside StackIDs is also used in other contexts through the method StackID::GetCallFrameAddress. One notable case is DWARFExpression: the computation of `DW_OP_call_frame_address` is done using StackID. This feeds into many other places, e.g. expression evaluation may require the address of a variable that is computed from the CFA; to access the variable without faulting, we may need to preserve the pointer metadata. As such, StackID must be able to provide both versions of the CFA. In the spirit of allowing consumers of pointers to decide what to do with pointer metadata, this patch changes StackID to store both versions of the cfa pointer. Two getter methods are provided, and all call sites except DWARFExpression preserve their existing behavior (stripped pointer). Other alternatives were considered: * Just store the raw pointer. This would require changing the comparisong operator `<` to also receive a Process, as the comparison requires stripped pointers. It wasn't clear if all call-sites had a non-null process, whereas we know we have a process when creating a StackID. * Store a weak pointer to the process inside the class, and then strip metadata as needed. This would require a `weak_ptr::lock` in many operations of LLDB, and it felt wasteful. It also prevents stripping of the pointer if the process has gone away. This patch also changes RegisterContextUnwind::ReadFrameAddress, which is the method computing the CFA fed into StackID, to also preserve the signature pointers. (cherry picked from commit 5d088ba) (cherry picked from commit c507ec1)
1 parent 825738f commit fc14bbe

File tree

9 files changed

+293
-19
lines changed

9 files changed

+293
-19
lines changed

lldb/include/lldb/Target/StackID.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class StackID {
2626

2727
lldb::addr_t GetPC() const { return m_pc; }
2828

29-
lldb::addr_t GetCallFrameAddress() const { return m_cfa; }
29+
lldb::addr_t GetCallFrameAddressWithMetadata() const {
30+
return m_cfa_with_metadata;
31+
}
32+
33+
lldb::addr_t GetCallFrameAddressWithoutMetadata() const { return m_cfa; }
3034

3135
SymbolContextScope *GetSymbolContextScope() const { return m_symbol_scope; }
3236

@@ -71,6 +75,9 @@ class StackID {
7175
/// below)
7276
lldb::addr_t m_cfa = LLDB_INVALID_ADDRESS;
7377

78+
/// The cfa with metadata (i.e. prior to Process::FixAddress).
79+
lldb::addr_t m_cfa_with_metadata = LLDB_INVALID_ADDRESS;
80+
7481
/// If nullptr, there is no block or symbol for this frame. If not nullptr,
7582
/// this will either be the scope for the lexical block for the frame, or the
7683
/// scope for the symbol. Symbol context scopes are always be unique pointers

lldb/source/API/SBFrame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ lldb::addr_t SBFrame::GetCFA() const {
275275
}
276276

277277
if (StackFrame *frame = exe_ctx->GetFramePtr())
278-
return frame->GetStackID().GetCallFrameAddress();
278+
return frame->GetStackID().GetCallFrameAddressWithoutMetadata();
279279
return LLDB_INVALID_ADDRESS;
280280
}
281281

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
22782278
// Note that we don't have to parse FDEs because this DWARF expression
22792279
// is commonly evaluated with a valid stack frame.
22802280
StackID id = frame->GetStackID();
2281-
addr_t cfa = id.GetCallFrameAddress();
2281+
addr_t cfa = id.GetCallFrameAddressWithMetadata();
22822282
if (cfa != LLDB_INVALID_ADDRESS) {
22832283
stack.push_back(Scalar(cfa));
22842284
stack.back().SetValueType(Value::ValueType::LoadAddress);

lldb/source/Target/RegisterContextUnwind.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,8 +2009,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20092009
reg_info, reg_to_deref_contents, reg_info->byte_size, reg_value);
20102010
if (error.Success()) {
20112011
address = reg_value.GetAsUInt64();
2012-
if (abi_sp)
2013-
address = abi_sp->FixCodeAddress(address);
20142012
UnwindLogMsg(
20152013
"CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
20162014
", CFA value is 0x%" PRIx64,
@@ -2033,8 +2031,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20332031
RegisterNumber cfa_reg(m_thread, row_register_kind,
20342032
fa.GetRegisterNumber());
20352033
if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2036-
if (abi_sp)
2037-
cfa_reg_contents = abi_sp->FixDataAddress(cfa_reg_contents);
20382034
if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 ||
20392035
cfa_reg_contents == 1) {
20402036
UnwindLogMsg(
@@ -2069,9 +2065,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20692065
dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
20702066
if (result) {
20712067
address = result->GetScalar().ULongLong();
2072-
if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2073-
address = abi_sp->FixCodeAddress(address);
2074-
20752068
UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64,
20762069
address);
20772070
return true;
@@ -2111,7 +2104,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
21112104
}
21122105
case UnwindPlan::Row::FAValue::isConstant: {
21132106
address = fa.GetConstant();
2114-
address = m_thread.GetProcess()->FixDataAddress(address);
21152107
UnwindLogMsg("CFA value set by constant is 0x%" PRIx64, address);
21162108
return true;
21172109
}

lldb/source/Target/StackFrameList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ bool StackFrameList::FetchFramesUpTo(uint32_t end_idx,
461461
}
462462
} else {
463463
unwind_frame_sp = m_frames.front();
464-
cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
464+
cfa = unwind_frame_sp->m_id.GetCallFrameAddressWithoutMetadata();
465465
}
466466
} else {
467467
// Check for interruption when building the frames.

lldb/source/Target/StackID.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ bool StackID::IsCFAOnStack(Process &process) const {
3535

3636
StackID::StackID(lldb::addr_t pc, lldb::addr_t cfa,
3737
SymbolContextScope *symbol_scope, Process *process)
38-
: m_pc(pc), m_cfa(cfa), m_symbol_scope(symbol_scope) {
38+
: m_pc(pc), m_cfa(cfa), m_cfa_with_metadata(cfa),
39+
m_symbol_scope(symbol_scope) {
3940
if (process) {
4041
m_pc = process->FixCodeAddress(m_pc);
4142
m_cfa = process->FixDataAddress(m_cfa);
@@ -47,6 +48,7 @@ void StackID::SetPC(lldb::addr_t pc, Process *process) {
4748
}
4849

4950
void StackID::SetCFA(lldb::addr_t cfa, Process *process) {
51+
m_cfa_with_metadata = cfa;
5052
m_cfa = process ? process->FixDataAddress(cfa) : cfa;
5153
}
5254

@@ -67,7 +69,8 @@ void StackID::Dump(Stream *s) {
6769
}
6870

6971
bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) {
70-
if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
72+
if (lhs.GetCallFrameAddressWithoutMetadata() !=
73+
rhs.GetCallFrameAddressWithoutMetadata())
7174
return false;
7275

7376
SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
@@ -134,8 +137,8 @@ CompareHeapCFAs(const StackID &lhs, const StackID &rhs, Process &process) {
134137
if (!lhs_cfa_on_stack && rhs_cfa_on_stack)
135138
return HeapCFAComparisonResult::Older;
136139

137-
const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
138-
const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
140+
const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddressWithoutMetadata();
141+
const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddressWithoutMetadata();
139142
// If the cfas are the same, fallback to the usual scope comparison.
140143
if (lhs_cfa == rhs_cfa)
141144
return HeapCFAComparisonResult::NoOpinion;
@@ -170,9 +173,9 @@ bool StackID::IsYounger(const StackID &lhs, const StackID &rhs,
170173
break;
171174
}
172175
// END SWIFT
173-
174-
const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
175-
const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
176+
//
177+
const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddressWithoutMetadata();
178+
const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddressWithoutMetadata();
176179

177180
// FIXME: We are assuming that the stacks grow downward in memory. That's not
178181
// necessary, but true on
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ASM_SOURCES := main.s
2+
3+
# This is to appease Makefile.rules, there is no main.c
4+
C_SOURCES := main.c
5+
6+
ASM_OBJS := $(ASM_SOURCES:.s=.o)
7+
8+
%.o: %.s
9+
$(CC) -c -x assembler $< -o $@
10+
11+
include Makefile.rules
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
@skipUnlessDarwin
8+
@skipIf(archs=no_match(["arm64"]))
9+
class TestArmPointerMetadataStripping(TestBase):
10+
def test(self):
11+
self.build()
12+
target, process, thread, bkpt = lldbutil.run_to_name_breakpoint(self, "foo")
13+
14+
# Step over the first two instructions of foo in order to
15+
# toggle the bit of fp and save it on the stack:
16+
# orr x29, x29, #0x1000000000000000
17+
# stp x29, x30, [sp, #-16]!
18+
# This is effectively adding metadata to the CFA of the caller frame (main).
19+
thread.StepInstruction(False)
20+
thread.StepInstruction(False)
21+
22+
# The location of `argv` has been artificially made equal to the CFA of the frame.
23+
# As such, it should have the metadata artificially set previously.
24+
argv_addr = thread.frames[1].GetValueForVariablePath("&argv")
25+
self.assertTrue(argv_addr.IsValid())
26+
argv_addr_uint = argv_addr.GetValueAsUnsigned()
27+
self.assertNotEqual((argv_addr_uint & (1 << 60)), 0)
28+
29+
# GetCFA strips metadata.
30+
cfa = thread.frames[1].GetCFA()
31+
self.assertEqual((cfa & (1 << 60)), 0)
32+
33+
# If the test worked correctly, the cfa and the location should be identical,
34+
# modulo the metadata.
35+
self.assertEqual(cfa | (1 << 60), argv_addr_uint)

0 commit comments

Comments
 (0)