Skip to content

Commit 9ac29a0

Browse files
Merge pull request llvm#11493 from felipepiovezan/felipe/misisng-cherry-picks
🍒 [lldb] Cherry pick ptr metadata work from upstream
2 parents fc73949 + 865121c commit 9ac29a0

File tree

21 files changed

+447
-69
lines changed

21 files changed

+447
-69
lines changed

lldb/include/lldb/Expression/IRMemoryMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class IRMemoryMap {
6565
size_t size, Status &error);
6666
void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar,
6767
size_t size, Status &error);
68-
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address,
68+
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t pointer,
6969
Status &error);
7070
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size,
7171
Status &error);

lldb/include/lldb/Target/ABI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ class ABI : public PluginInterface {
141141
return FixDataAddress(pc);
142142
}
143143

144+
virtual lldb::addr_t FixAnyAddressPreservingAuthentication(lldb::addr_t pc) {
145+
return FixAnyAddress(pc);
146+
}
147+
144148
llvm::MCRegisterInfo &GetMCRegisterInfo() { return *m_mc_register_info_up; }
145149

146150
virtual void

lldb/include/lldb/Target/Process.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,11 @@ class Process : public std::enable_shared_from_this<Process>,
14731473
/// platforms where there is a difference (only Arm Thumb at this time).
14741474
lldb::addr_t FixAnyAddress(lldb::addr_t pc);
14751475

1476+
/// Strip pointer metadata except for the bits necessary to authenticate a
1477+
/// memory access. This is useful, for example, if `address` requires
1478+
/// authentication and it is going to be consumed in JITed code.
1479+
lldb::addr_t FixAnyAddressPreservingAuthentication(lldb::addr_t address);
1480+
14761481
/// Get the Modification ID of the process.
14771482
///
14781483
/// \return

lldb/include/lldb/Target/StackID.h

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@
1010
#define LLDB_TARGET_STACKID_H
1111

1212
#include "lldb/Core/AddressRange.h"
13-
#include "lldb/lldb-private.h"
1413

1514
namespace lldb_private {
1615

1716
class Process;
1817

1918
class StackID {
2019
public:
21-
// Constructors and Destructors
2220
StackID() = default;
2321

2422
explicit StackID(lldb::addr_t pc, lldb::addr_t cfa,
2523
SymbolContextScope *symbol_scope, Process *process);
2624

27-
StackID(const StackID &rhs) = default;
28-
2925
~StackID() = default;
3026

3127
lldb::addr_t GetPC() const { return m_pc; }
3228

33-
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; }
3434

3535
SymbolContextScope *GetSymbolContextScope() const { return m_symbol_scope; }
3636

@@ -51,17 +51,6 @@ class StackID {
5151

5252
void Dump(Stream *s);
5353

54-
// Operators
55-
const StackID &operator=(const StackID &rhs) {
56-
if (this != &rhs) {
57-
m_pc = rhs.m_pc;
58-
m_cfa = rhs.m_cfa;
59-
m_cfa_on_stack = rhs.m_cfa_on_stack;
60-
m_symbol_scope = rhs.m_symbol_scope;
61-
}
62-
return *this;
63-
}
64-
6554
/// Check if the CFA is on the stack, or elsewhere in the process, such as on
6655
/// the heap.
6756
bool IsCFAOnStack(Process &process) const;
@@ -76,28 +65,31 @@ class StackID {
7665
void SetPC(lldb::addr_t pc, Process *process);
7766
void SetCFA(lldb::addr_t cfa, Process *process);
7867

79-
lldb::addr_t m_pc =
80-
LLDB_INVALID_ADDRESS; // The pc value for the function/symbol for this
81-
// frame. This will
82-
// only get used if the symbol scope is nullptr (the code where we are
83-
// stopped is not represented by any function or symbol in any shared
84-
// library).
85-
lldb::addr_t m_cfa =
86-
LLDB_INVALID_ADDRESS; // The call frame address (stack pointer) value
87-
// at the beginning of the function that uniquely
88-
// identifies this frame (along with m_symbol_scope
89-
// below)
90-
// True if the CFA is an address on the stack, false if it's an address
91-
// elsewhere (ie heap).
68+
/// The pc value for the function/symbol for this frame. This will only get
69+
/// used if the symbol scope is nullptr (the code where we are stopped is not
70+
/// represented by any function or symbol in any shared library).
71+
lldb::addr_t m_pc = LLDB_INVALID_ADDRESS;
72+
73+
/// The call frame address (stack pointer) value at the beginning of the
74+
/// function that uniquely identifies this frame (along with m_symbol_scope
75+
/// below)
76+
lldb::addr_t m_cfa = LLDB_INVALID_ADDRESS;
77+
78+
/// The cfa with metadata (i.e. prior to Process::FixAddress).
79+
lldb::addr_t m_cfa_with_metadata = LLDB_INVALID_ADDRESS;
80+
81+
/// If nullptr, there is no block or symbol for this frame. If not nullptr,
82+
/// this will either be the scope for the lexical block for the frame, or the
83+
/// scope for the symbol. Symbol context scopes are always be unique pointers
84+
/// since the are part of the Block and Symbol objects and can easily be used
85+
/// to tell if a stack ID is the same as another.
86+
SymbolContextScope *m_symbol_scope = nullptr;
87+
88+
// BEGIN SWIFT
89+
/// True if the CFA is an address on the stack, false if it's an address
90+
/// elsewhere (ie heap).
9291
mutable LazyBool m_cfa_on_stack = eLazyBoolCalculate;
93-
SymbolContextScope *m_symbol_scope =
94-
nullptr; // If nullptr, there is no block or symbol for this frame.
95-
// If not nullptr, this will either be the scope for the
96-
// lexical block for the frame, or the scope for the
97-
// symbol. Symbol context scopes are always be unique
98-
// pointers since the are part of the Block and Symbol
99-
// objects and can easily be used to tell if a stack ID
100-
// is the same as another.
92+
// END SWIFT
10193
};
10294

10395
bool operator==(const StackID &lhs, const StackID &rhs);

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/Expression/IRMemoryMap.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,19 @@ void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
637637
}
638638

639639
void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
640-
lldb::addr_t address, Status &error) {
640+
lldb::addr_t pointer, Status &error) {
641641
error.Clear();
642642

643-
Scalar scalar(address);
643+
/// Only ask the Process to fix `pointer` if the address belongs to the
644+
/// process. An address belongs to the process if the Allocation policy is not
645+
/// eAllocationPolicyHostOnly.
646+
auto it = FindAllocation(pointer, 1);
647+
if (it == m_allocations.end() ||
648+
it->second.m_policy != AllocationPolicy::eAllocationPolicyHostOnly)
649+
if (auto process_sp = GetProcessWP().lock())
650+
pointer = process_sp->FixAnyAddressPreservingAuthentication(pointer);
651+
652+
Scalar scalar(pointer);
644653

645654
WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
646655
}

lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,15 @@ addr_t ABIMacOSX_arm64::FixDataAddress(addr_t addr) {
792792
return DoFixAddr(addr, false /*is_code*/, GetProcessSP());
793793
}
794794

795+
addr_t ABIMacOSX_arm64::FixAnyAddressPreservingAuthentication(addr_t addr) {
796+
// Save the old MTE tag and restore it later.
797+
constexpr addr_t mte_mask = 0x0f00000000000000ULL;
798+
addr_t old_mte_tag = addr & mte_mask;
799+
800+
addr_t fixed_addr = FixDataAddress(addr);
801+
return old_mte_tag | (fixed_addr & (~mte_mask));
802+
}
803+
795804
void ABIMacOSX_arm64::Initialize() {
796805
PluginManager::RegisterPlugin(GetPluginNameStatic(), pluginDesc,
797806
CreateInstance);

lldb/source/Plugins/ABI/AArch64/ABIMacOSX_arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ABIMacOSX_arm64 : public ABIAArch64 {
5959

6060
lldb::addr_t FixCodeAddress(lldb::addr_t pc) override;
6161
lldb::addr_t FixDataAddress(lldb::addr_t pc) override;
62+
lldb::addr_t FixAnyAddressPreservingAuthentication(lldb::addr_t pc) override;
6263

6364
// Static Functions
6465

lldb/source/Target/Process.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6133,6 +6133,12 @@ addr_t Process::FixAnyAddress(addr_t addr) {
61336133
return addr;
61346134
}
61356135

6136+
addr_t Process::FixAnyAddressPreservingAuthentication(addr_t addr) {
6137+
if (ABISP abi_sp = GetABI())
6138+
addr = abi_sp->FixAnyAddressPreservingAuthentication(addr);
6139+
return addr;
6140+
}
6141+
61366142
void Process::DidExec() {
61376143
Log *log = GetLog(LLDBLog::Process);
61386144
LLDB_LOGF(log, "Process::%s()", __FUNCTION__);

0 commit comments

Comments
 (0)