Skip to content

Commit 182259d

Browse files
authored
Merge pull request #11456 from felipepiovezan/felipe/ptr_metadata_fixes_6_2_1
[lldb] Cherry-pick patches for pointer authentication support in 6.2.1
2 parents 51b25e5 + 31724b1 commit 182259d

File tree

29 files changed

+546
-149
lines changed

29 files changed

+546
-149
lines changed

lldb/include/lldb/Expression/IRMemoryMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class IRMemoryMap {
5959
size_t size, Status &error);
6060
void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar,
6161
size_t size, Status &error);
62-
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address,
62+
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t pointer,
6363
Status &error);
6464
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size,
6565
Status &error);

lldb/include/lldb/Target/ABI.h

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

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

145149
virtual void

lldb/include/lldb/Target/Process.h

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

1466+
/// Strip pointer metadata except for the bits necessary to authenticate a
1467+
/// memory access. This is useful, for example, if `address` requires
1468+
/// authentication and it is going to be consumed in JITed code.
1469+
lldb::addr_t FixAnyAddressPreservingAuthentication(lldb::addr_t address);
1470+
14661471
/// Get the Modification ID of the process.
14671472
///
14681473
/// \return

lldb/include/lldb/Target/StackID.h

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +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

16+
class Process;
17+
1718
class StackID {
1819
public:
19-
// Constructors and Destructors
2020
StackID() = default;
2121

22-
StackID(const StackID &rhs) = default;
22+
explicit StackID(lldb::addr_t pc, lldb::addr_t cfa,
23+
SymbolContextScope *symbol_scope, Process *process);
2324

2425
~StackID() = default;
2526

2627
lldb::addr_t GetPC() const { return m_pc; }
2728

28-
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; }
2934

3035
SymbolContextScope *GetSymbolContextScope() const { return m_symbol_scope; }
3136

@@ -46,17 +51,6 @@ class StackID {
4651

4752
void Dump(Stream *s);
4853

49-
// Operators
50-
const StackID &operator=(const StackID &rhs) {
51-
if (this != &rhs) {
52-
m_pc = rhs.m_pc;
53-
m_cfa = rhs.m_cfa;
54-
m_cfa_on_stack = rhs.m_cfa_on_stack;
55-
m_symbol_scope = rhs.m_symbol_scope;
56-
}
57-
return *this;
58-
}
59-
6054
/// Check if the CFA is on the stack, or elsewhere in the process, such as on
6155
/// the heap.
6256
bool IsCFAOnStack(Process &process) const;
@@ -68,34 +62,34 @@ class StackID {
6862
protected:
6963
friend class StackFrame;
7064

71-
explicit StackID(lldb::addr_t pc, lldb::addr_t cfa) : m_pc(pc), m_cfa(cfa) {}
72-
73-
void SetPC(lldb::addr_t pc) { m_pc = pc; }
74-
75-
void SetCFA(lldb::addr_t cfa) { m_cfa = cfa; }
76-
77-
lldb::addr_t m_pc =
78-
LLDB_INVALID_ADDRESS; // The pc value for the function/symbol for this
79-
// frame. This will
80-
// only get used if the symbol scope is nullptr (the code where we are
81-
// stopped is not represented by any function or symbol in any shared
82-
// library).
83-
lldb::addr_t m_cfa =
84-
LLDB_INVALID_ADDRESS; // The call frame address (stack pointer) value
85-
// at the beginning of the function that uniquely
86-
// identifies this frame (along with m_symbol_scope
87-
// below)
88-
// True if the CFA is an address on the stack, false if it's an address
89-
// elsewhere (ie heap).
65+
void SetPC(lldb::addr_t pc, Process *process);
66+
void SetCFA(lldb::addr_t cfa, Process *process);
67+
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).
9091
mutable LazyBool m_cfa_on_stack = eLazyBoolCalculate;
91-
SymbolContextScope *m_symbol_scope =
92-
nullptr; // If nullptr, there is no block or symbol for this frame.
93-
// If not nullptr, this will either be the scope for the
94-
// lexical block for the frame, or the scope for the
95-
// symbol. Symbol context scopes are always be unique
96-
// pointers since the are part of the Block and Symbol
97-
// objects and can easily be used to tell if a stack ID
98-
// is the same as another.
92+
// END SWIFT
9993
};
10094

10195
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
@@ -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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,6 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
11311131
lldb::addr_t pointer_value =
11321132
process->ReadPointerFromMemory(pointer_addr, error);
11331133
if (pointer_value != LLDB_INVALID_ADDRESS) {
1134-
if (ABISP abi_sp = process->GetABI())
1135-
pointer_value = abi_sp->FixCodeAddress(pointer_value);
11361134
stack.back().GetScalar() = pointer_value;
11371135
stack.back().ClearContext();
11381136
} else {
@@ -2279,7 +2277,7 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
22792277
// Note that we don't have to parse FDEs because this DWARF expression
22802278
// is commonly evaluated with a valid stack frame.
22812279
StackID id = frame->GetStackID();
2282-
addr_t cfa = id.GetCallFrameAddress();
2280+
addr_t cfa = id.GetCallFrameAddressWithMetadata();
22832281
if (cfa != LLDB_INVALID_ADDRESS) {
22842282
stack.push_back(Scalar(cfa));
22852283
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
@@ -635,10 +635,19 @@ void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
635635
}
636636

637637
void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
638-
lldb::addr_t address, Status &error) {
638+
lldb::addr_t pointer, Status &error) {
639639
error.Clear();
640640

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

643652
WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
644653
}

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

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -811,42 +811,48 @@ ValueObjectSP ABIMacOSX_arm64::GetReturnValueObjectImpl(
811811
return return_valobj_sp;
812812
}
813813

814-
addr_t ABIMacOSX_arm64::FixCodeAddress(addr_t pc) {
815-
addr_t pac_sign_extension = 0x0080000000000000ULL;
816-
addr_t tbi_mask = 0xff80000000000000ULL;
817-
addr_t mask = 0;
818-
819-
if (ProcessSP process_sp = GetProcessSP()) {
820-
mask = process_sp->GetCodeAddressMask();
821-
if (pc & pac_sign_extension) {
822-
addr_t highmem_mask = process_sp->GetHighmemCodeAddressMask();
823-
if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
824-
mask = highmem_mask;
825-
}
826-
}
814+
constexpr addr_t tbi_mask = 0xff80000000000000ULL;
815+
constexpr addr_t pac_sign_extension = 0x0080000000000000ULL;
816+
817+
/// Consults the process for its {code, data} address masks and applies it to
818+
/// `addr`.
819+
static addr_t DoFixAddr(addr_t addr, bool is_code, ProcessSP process_sp) {
820+
if (!process_sp)
821+
return addr;
822+
823+
addr_t mask = is_code ? process_sp->GetCodeAddressMask()
824+
: process_sp->GetDataAddressMask();
827825
if (mask == LLDB_INVALID_ADDRESS_MASK)
828826
mask = tbi_mask;
829827

830-
return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
828+
if (addr & pac_sign_extension) {
829+
addr_t highmem_mask = is_code ? process_sp->GetHighmemCodeAddressMask()
830+
: process_sp->GetHighmemCodeAddressMask();
831+
if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
832+
return addr | highmem_mask;
833+
return addr | mask;
834+
}
835+
836+
return addr & (~mask);
831837
}
832838

833-
addr_t ABIMacOSX_arm64::FixDataAddress(addr_t pc) {
834-
addr_t pac_sign_extension = 0x0080000000000000ULL;
835-
addr_t tbi_mask = 0xff80000000000000ULL;
836-
addr_t mask = 0;
837-
838-
if (ProcessSP process_sp = GetProcessSP()) {
839-
mask = process_sp->GetDataAddressMask();
840-
if (pc & pac_sign_extension) {
841-
addr_t highmem_mask = process_sp->GetHighmemDataAddressMask();
842-
if (highmem_mask != LLDB_INVALID_ADDRESS_MASK)
843-
mask = highmem_mask;
844-
}
845-
}
846-
if (mask == LLDB_INVALID_ADDRESS_MASK)
847-
mask = tbi_mask;
839+
addr_t ABIMacOSX_arm64::FixCodeAddress(addr_t pc) {
840+
ProcessSP process_sp = GetProcessSP();
841+
return DoFixAddr(pc, true /*is_code*/, GetProcessSP());
842+
}
843+
844+
addr_t ABIMacOSX_arm64::FixDataAddress(addr_t addr) {
845+
ProcessSP process_sp = GetProcessSP();
846+
return DoFixAddr(addr, false /*is_code*/, GetProcessSP());
847+
}
848+
849+
addr_t ABIMacOSX_arm64::FixAnyAddressPreservingAuthentication(addr_t addr) {
850+
// Save the old MTE tag and restore it later.
851+
constexpr addr_t mte_mask = 0x0f00000000000000ULL;
852+
addr_t old_mte_tag = addr & mte_mask;
848853

849-
return (pc & pac_sign_extension) ? pc | mask : pc & (~mask);
854+
addr_t fixed_addr = FixDataAddress(addr);
855+
return old_mte_tag | (fixed_addr & (~mte_mask));
850856
}
851857

852858
void ABIMacOSX_arm64::Initialize() {

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

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

6565
lldb::addr_t FixCodeAddress(lldb::addr_t pc) override;
6666
lldb::addr_t FixDataAddress(lldb::addr_t pc) override;
67+
lldb::addr_t FixAnyAddressPreservingAuthentication(lldb::addr_t pc) override;
6768

6869
// Static Functions
6970

lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
863863
m_ts->GetTypeFromMangledTypename(ConstString("$sSVD"));
864864

865865
addr_t value = m_task_ptr;
866+
if (auto process_sp = m_backend.GetProcessSP())
867+
value = process_sp->FixDataAddress(value);
866868
DataExtractor data{reinterpret_cast<const void *>(&value),
867869
sizeof(value), endian::InlHostByteOrder(),
868870
sizeof(void *)};
@@ -903,7 +905,7 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
903905
parent_addr = 0;
904906
}
905907

906-
addr_t value = parent_addr;
908+
addr_t value = process_sp->FixDataAddress(parent_addr);
907909
DataExtractor data{reinterpret_cast<const void *>(&value),
908910
sizeof(value), endian::InlHostByteOrder(),
909911
sizeof(void *)};

0 commit comments

Comments
 (0)