Skip to content

Commit fdc430c

Browse files
committed
[lldb] Use RemoteAddress's address space for the file cache optimization
Now that RemoteAddress carries its address space, instead of using a high bit to turn on the filecache optimization, use that instead. This way we are guaranteed to not clash with any pointer authentication masks. rdar://148361743 (cherry picked from commit b283038e2aad625ff455a10f2590ed2501c987eb)
1 parent 3788bcb commit fdc430c

File tree

3 files changed

+45
-69
lines changed

3 files changed

+45
-69
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ bool LLDBMemoryReader::queryDataLayout(DataLayoutQueryType type, void *inBuffer,
3232
return false;
3333
// The mask returned by the process masks out the non-addressable bits.
3434
uint64_t mask_pattern = ~ptrauth_mask;
35-
// LLDBMemoryReader sets LLDB_FILE_ADDRESS_BIT to distinguish process
36-
// addresses and file addresses that point into a reflection section on
37-
// disk. Setting the bit in the mask ensures it isn't accidentally cleared
38-
// by ptrauth stripping.
39-
mask_pattern |= LLDB_FILE_ADDRESS_BIT;
4035
memcpy(outBuffer, &mask_pattern, m_process.GetAddressByteSize());
4136
return true;
4237
}
@@ -181,8 +176,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
181176
if (!target.GetSwiftUseReflectionSymbols())
182177
return {};
183178

184-
std::optional<Address> maybeAddr =
185-
resolveRemoteAddress(address.getRawAddress());
179+
std::optional<Address> maybeAddr = remoteAddressToLLDBAddress(address);
186180
// This is not an assert, but should never happen.
187181
if (!maybeAddr)
188182
return {};
@@ -231,11 +225,14 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
231225
// to a tagged address so further memory reads originating from it benefit
232226
// from the file-cache optimization.
233227
swift::remote::RemoteAbsolutePointer process_pointer{
234-
swift::remote::RemoteAddress{
235-
readValue, swift::remote::RemoteAddress::DefaultAddressSpace}};
228+
swift::remote::RemoteAddress{readValue, address.getAddressSpace()}};
236229

237-
if (!readMetadataFromFileCacheEnabled())
230+
if (!readMetadataFromFileCacheEnabled()) {
231+
assert(address.getAddressSpace() ==
232+
swift::remote::RemoteAddress::DefaultAddressSpace &&
233+
"Unexpected address space!");
238234
return process_pointer;
235+
}
239236

240237
// Try to strip the pointer before checking if we have it mapped.
241238
auto strippedPointer = signedPointerStripper(process_pointer);
@@ -271,10 +268,10 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
271268
}
272269

273270
// If the containing image is the first registered one, the image's tagged
274-
// start address for it is the first tagged address. Otherwise, the previous
275-
// pair's address is the start tagged address.
271+
// start address for it is zero. Otherwise, the previous pair's address is the
272+
// start of the new address.
276273
uint64_t start_tagged_address = pair_iterator == m_range_module_map.begin()
277-
? LLDB_FILE_ADDRESS_BIT
274+
? 0
278275
: std::prev(pair_iterator)->first;
279276

280277
auto *section_list = module_containing_pointer->GetSectionList();
@@ -298,8 +295,7 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
298295
}
299296

300297
swift::remote::RemoteAbsolutePointer tagged_pointer{
301-
swift::remote::RemoteAddress{
302-
tagged_address, swift::remote::RemoteAddress::DefaultAddressSpace}};
298+
swift::remote::RemoteAddress{tagged_address, LLDBAddressSpace}};
303299

304300
if (tagged_address != (uint64_t)signedPointerStripper(tagged_pointer)
305301
.getResolvedAddress()
@@ -341,10 +337,10 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
341337
LLDB_LOGV(log, "[MemoryReader] asked to read {0} bytes at address {1:x}",
342338
size, address.getRawAddress());
343339
std::optional<Address> maybeAddr =
344-
resolveRemoteAddressFromSymbolObjectFile(address.getRawAddress());
340+
resolveRemoteAddressFromSymbolObjectFile(address);
345341

346342
if (!maybeAddr)
347-
maybeAddr = resolveRemoteAddress(address.getRawAddress());
343+
maybeAddr = remoteAddressToLLDBAddress(address);
348344

349345
if (!maybeAddr) {
350346
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
@@ -418,10 +414,10 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
418414
address.getRawAddress());
419415

420416
std::optional<Address> maybeAddr =
421-
resolveRemoteAddressFromSymbolObjectFile(address.getRawAddress());
417+
resolveRemoteAddressFromSymbolObjectFile(address);
422418

423419
if (!maybeAddr)
424-
maybeAddr = resolveRemoteAddress(address.getRawAddress());
420+
maybeAddr = remoteAddressToLLDBAddress(address);
425421

426422
if (!maybeAddr) {
427423
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
@@ -461,7 +457,7 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
461457

462458
MemoryReaderLocalBufferHolder::~MemoryReaderLocalBufferHolder() {
463459
if (m_memory_reader)
464-
m_memory_reader->popLocalBuffer();
460+
m_memory_reader->popLocalBuffer();
465461
}
466462

467463
MemoryReaderLocalBufferHolder
@@ -490,12 +486,9 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
490486
"Trying to register symbol object file, but reading from it is "
491487
"disabled!");
492488

493-
// The first available address is the mask, since subsequent images are mapped
494-
// in ascending order, all of them will contain this mask.
495-
uint64_t module_start_address = LLDB_FILE_ADDRESS_BIT;
489+
uint64_t module_start_address = 0;
496490
if (!m_range_module_map.empty())
497-
// We map the images contiguously one after the other, all with the tag bit
498-
// set.
491+
// We map the images contiguously one after the other.
499492
// The address that maps the last module is exactly the address the new
500493
// module should start at.
501494
module_start_address = m_range_module_map.back().first;
@@ -541,18 +534,6 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
541534
auto size = end_file_address - start_file_address;
542535
auto module_end_address = module_start_address + size;
543536

544-
if (module_end_address !=
545-
signedPointerStripper(
546-
swift::remote::RemoteAbsolutePointer{swift::remote::RemoteAddress{
547-
module_end_address,
548-
swift::reflection::RemoteAddress::DefaultAddressSpace}})
549-
.getResolvedAddress()
550-
.getRawAddress()) {
551-
LLDB_LOG(GetLog(LLDBLog::Types),
552-
"[MemoryReader] module to address map ran into pointer "
553-
"authentication mask!");
554-
return {};
555-
}
556537
// The address for the next image is the next pointer aligned address
557538
// available after the end of the current image.
558539
uint64_t next_module_start_address = llvm::alignTo(module_end_address, 8);
@@ -566,18 +547,18 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
566547

567548
std::optional<std::pair<uint64_t, lldb::ModuleSP>>
568549
LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
569-
uint64_t tagged_address) const {
550+
swift::remote::RemoteAddress tagged_address) const {
570551
Log *log(GetLog(LLDBLog::Types));
571552

572553
if (!readMetadataFromFileCacheEnabled())
573554
return {};
574555

575-
// If the address contains our mask, this is an image we registered.
576-
if (!(tagged_address & LLDB_FILE_ADDRESS_BIT))
556+
if (tagged_address.getAddressSpace() != LLDBAddressSpace)
577557
return {};
578558

579559
// Dummy pair with the address we're looking for.
580-
auto comparison_pair = std::make_pair(tagged_address, ModuleSP());
560+
auto comparison_pair =
561+
std::make_pair(tagged_address.getRawAddress(), ModuleSP());
581562

582563
// Explicitly compare only the addresses, never the modules in the pairs.
583564
auto pair_iterator = std::lower_bound(
@@ -589,7 +570,7 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
589570
LLDB_LOG(log,
590571
"[MemoryReader] Address {0:x} is larger than the upper bound "
591572
"address of the mapped in modules",
592-
tagged_address);
573+
tagged_address.getRawAddress());
593574
return {};
594575
}
595576

@@ -601,14 +582,13 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
601582
}
602583
uint64_t file_address;
603584
if (pair_iterator == m_range_module_map.begin())
604-
// Since this is the first registered module,
605-
// clearing the tag bit will give the virtual file address.
606-
file_address = tagged_address & ~LLDB_FILE_ADDRESS_BIT;
585+
file_address = tagged_address.getRawAddress();
607586
else
608587
// The end of the previous section is the start of the current one.
609588
// We also need to add the first section's file address since we remove it
610589
// when constructing the range to module map.
611-
file_address = tagged_address - std::prev(pair_iterator)->first;
590+
file_address =
591+
(tagged_address - std::prev(pair_iterator)->first).getRawAddress();
612592

613593
// We also need to add the module's file address, since we subtract it when
614594
// building the range to module map.
@@ -620,27 +600,28 @@ std::optional<swift::reflection::RemoteAddress>
620600
LLDBMemoryReader::resolveRemoteAddress(
621601
swift::reflection::RemoteAddress address) const {
622602
std::optional<Address> lldb_address =
623-
LLDBMemoryReader::resolveRemoteAddress(address.getRawAddress());
603+
LLDBMemoryReader::remoteAddressToLLDBAddress(address);
624604
if (!lldb_address)
625605
return {};
626606
lldb::addr_t addr = lldb_address->GetLoadAddress(&m_process.GetTarget());
627607
if (addr != LLDB_INVALID_ADDRESS)
628-
return swift::reflection::RemoteAddress(addr, swift::reflection::RemoteAddress::DefaultAddressSpace);
608+
return swift::reflection::RemoteAddress(
609+
addr, swift::reflection::RemoteAddress::DefaultAddressSpace);
629610
return {};
630611
}
631612

632-
std::optional<Address>
633-
LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
613+
std::optional<Address> LLDBMemoryReader::remoteAddressToLLDBAddress(
614+
swift::remote::RemoteAddress address) const {
634615
Log *log(GetLog(LLDBLog::Types));
635616
auto maybe_pair = getFileAddressAndModuleForTaggedAddress(address);
636617
if (!maybe_pair)
637-
return Address(address);
618+
return Address(address.getRawAddress());
638619

639620
uint64_t file_address = maybe_pair->first;
640621
ModuleSP module = maybe_pair->second;
641622

642623
if (m_modules_with_metadata_in_symbol_obj_file.count(module))
643-
return Address(address);
624+
return Address(address.getRawAddress());
644625

645626
auto *object_file = module->GetObjectFile();
646627
if (!object_file)
@@ -656,7 +637,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
656637
LLDB_LOGV(log,
657638
"[MemoryReader] Successfully resolved mapped address {0:x} into "
658639
"file address {1:x}",
659-
address, resolved.GetFileAddress());
640+
address.getRawAddress(), resolved.GetFileAddress());
660641
return resolved;
661642
}
662643
auto *sec_list = module->GetSectionList();
@@ -700,7 +681,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
700681

701682
std::optional<Address>
702683
LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
703-
uint64_t address) const {
684+
swift::remote::RemoteAddress address) const {
704685
Log *log(GetLog(LLDBLog::Types));
705686

706687
if (!m_process.GetTarget().GetSwiftReadMetadataFromDSYM())
@@ -744,7 +725,7 @@ LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
744725
LLDB_LOGV(log,
745726
"[MemoryReader] Successfully resolved mapped address {0:x} into "
746727
"file address {1:x} from symbol object file.",
747-
address, file_address);
728+
address.getRawAddress(), file_address);
748729
return resolved;
749730
}
750731

lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class MemoryReaderLocalBufferHolder {
5151

5252
class LLDBMemoryReader : public swift::remote::MemoryReader {
5353
public:
54+
static constexpr uint8_t LLDBAddressSpace = 1;
5455

5556
LLDBMemoryReader(Process &p,
5657
std::function<swift::remote::RemoteAbsolutePointer(
@@ -105,7 +106,8 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
105106
/// Gets the file address and module that were mapped to a given tagged
106107
/// address.
107108
std::optional<std::pair<uint64_t, lldb::ModuleSP>>
108-
getFileAddressAndModuleForTaggedAddress(uint64_t tagged_address) const;
109+
getFileAddressAndModuleForTaggedAddress(
110+
swift::remote::RemoteAddress tagged_address) const;
109111

110112
/// Resolves the address by either mapping a tagged address back to an LLDB
111113
/// Address with section + offset, or, in case the address is not tagged,
@@ -114,12 +116,13 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
114116
/// tagged address back, an Address with just an offset if the address was not
115117
/// tagged, and None if the address was tagged but we couldn't convert it back
116118
/// to an Address.
117-
std::optional<Address> resolveRemoteAddress(uint64_t address) const;
119+
std::optional<Address>
120+
remoteAddressToLLDBAddress(swift::remote::RemoteAddress address) const;
118121

119122
/// Reads memory from the symbol rich binary from the address into dest.
120123
/// \return true if it was able to successfully read memory.
121-
std::optional<Address>
122-
resolveRemoteAddressFromSymbolObjectFile(uint64_t address) const;
124+
std::optional<Address> resolveRemoteAddressFromSymbolObjectFile(
125+
swift::remote::RemoteAddress address) const;
123126

124127
Process &m_process;
125128
size_t m_max_read_amount;
@@ -144,14 +147,6 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
144147
/// The set of modules where we should read memory from the symbol file's
145148
/// object file instead of the main object file.
146149
llvm::SmallSet<lldb::ModuleSP, 8> m_modules_with_metadata_in_symbol_obj_file;
147-
148-
/// The bit used to tag LLDB's virtual addresses as such. See \c
149-
/// m_range_module_map.
150-
const static uint64_t LLDB_FILE_ADDRESS_BIT = 0x2000000000000000;
151-
static_assert(LLDB_FILE_ADDRESS_BIT & SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK,
152-
"LLDB file address bit not in spare bits mask!");
153-
static_assert(LLDB_FILE_ADDRESS_BIT & SWIFT_ABI_ARM64_SWIFT_SPARE_BITS_MASK,
154-
"LLDB file address bit not in spare bits mask!");
155150
};
156151
} // namespace lldb_private
157152
#endif

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,8 @@ std::optional<uint32_t> SwiftLanguageRuntime::AddObjectFileToReflectionContext(
727727
assert(address <= end_address && "Address outside of range!");
728728

729729
swift::remote::RemoteRef<void> remote_ref(
730-
swift::remote::RemoteAddress(
731-
address, swift::remote::RemoteAddress::DefaultAddressSpace),
730+
swift::remote::RemoteAddress(address,
731+
LLDBMemoryReader::LLDBAddressSpace),
732732
Buf);
733733
return {remote_ref, size};
734734
}

0 commit comments

Comments
 (0)