Skip to content

Commit c507ec1

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)
1 parent 5ecc9f1 commit c507ec1

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
@@ -318,7 +318,7 @@ lldb::addr_t SBFrame::GetCFA() const {
318318

319319
StackFrame *frame = exe_ctx.GetFramePtr();
320320
if (frame)
321-
return frame->GetStackID().GetCallFrameAddress();
321+
return frame->GetStackID().GetCallFrameAddressWithoutMetadata();
322322
return LLDB_INVALID_ADDRESS;
323323
}
324324

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
22792279
// Note that we don't have to parse FDEs because this DWARF expression
22802280
// is commonly evaluated with a valid stack frame.
22812281
StackID id = frame->GetStackID();
2282-
addr_t cfa = id.GetCallFrameAddress();
2282+
addr_t cfa = id.GetCallFrameAddressWithMetadata();
22832283
if (cfa != LLDB_INVALID_ADDRESS) {
22842284
stack.push_back(Scalar(cfa));
22852285
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
@@ -2002,8 +2002,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20022002
reg_info, cfa_reg_contents, reg_info->byte_size, reg_value);
20032003
if (error.Success()) {
20042004
address = reg_value.GetAsUInt64();
2005-
if (abi_sp)
2006-
address = abi_sp->FixCodeAddress(address);
20072005
UnwindLogMsg(
20082006
"CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
20092007
", CFA value is 0x%" PRIx64,
@@ -2024,8 +2022,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20242022
RegisterNumber cfa_reg(m_thread, row_register_kind,
20252023
fa.GetRegisterNumber());
20262024
if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2027-
if (abi_sp)
2028-
cfa_reg_contents = abi_sp->FixDataAddress(cfa_reg_contents);
20292025
if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 ||
20302026
cfa_reg_contents == 1) {
20312027
UnwindLogMsg(
@@ -2060,9 +2056,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
20602056
dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
20612057
if (result) {
20622058
address = result->GetScalar().ULongLong();
2063-
if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2064-
address = abi_sp->FixCodeAddress(address);
2065-
20662059
UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64,
20672060
address);
20682061
return true;
@@ -2102,7 +2095,6 @@ bool RegisterContextUnwind::ReadFrameAddress(
21022095
}
21032096
case UnwindPlan::Row::FAValue::isConstant: {
21042097
address = fa.GetConstant();
2105-
address = m_thread.GetProcess()->FixDataAddress(address);
21062098
UnwindLogMsg("CFA value set by constant is 0x%" PRIx64, address);
21072099
return true;
21082100
}

lldb/source/Target/StackFrameList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ bool StackFrameList::FetchFramesUpTo(uint32_t end_idx,
448448
}
449449
} else {
450450
unwind_frame_sp = m_frames.front();
451-
cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
451+
cfa = unwind_frame_sp->m_id.GetCallFrameAddressWithoutMetadata();
452452
}
453453
} else {
454454
// 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)