Skip to content

Commit b6943cf

Browse files
authored
Merge pull request #10981 from augusto2112/change-remote-addr-6.2
[NFC][lldb] Adapt LLDB to RemoteInspection's RemoteAddress changes
2 parents 67b573a + fdc430c commit b6943cf

File tree

7 files changed

+144
-122
lines changed

7 files changed

+144
-122
lines changed

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

Lines changed: 63 additions & 71 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
}
@@ -84,7 +79,7 @@ swift::remote::RemoteAddress
8479
LLDBMemoryReader::getSymbolAddress(const std::string &name) {
8580
lldbassert(!name.empty());
8681
if (name.empty())
87-
return swift::remote::RemoteAddress(nullptr);
82+
return swift::remote::RemoteAddress();
8883

8984
Log *log = GetLog(LLDBLog::Types);
9085

@@ -97,7 +92,7 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
9792
name_cs, lldb::eSymbolTypeAny, sc_list);
9893
if (!sc_list.GetSize()) {
9994
LLDB_LOGV(log, "[MemoryReader] symbol resolution failed {0}", name);
100-
return swift::remote::RemoteAddress(nullptr);
95+
return swift::remote::RemoteAddress();
10196
}
10297

10398
SymbolContext sym_ctx;
@@ -118,14 +113,15 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
118113
if (sym_ctx.symbol) {
119114
auto load_addr = sym_ctx.symbol->GetLoadAddress(&m_process.GetTarget());
120115
LLDB_LOGV(log, "[MemoryReader] symbol resolved to {0:x}", load_addr);
121-
return swift::remote::RemoteAddress(load_addr);
116+
return swift::remote::RemoteAddress(
117+
load_addr, swift::remote::RemoteAddress::DefaultAddressSpace);
122118
}
123119
}
124120

125121
// Empty list, resolution failed.
126122
if (sc_list.GetSize() == 0) {
127123
LLDB_LOGV(log, "[MemoryReader] symbol resolution failed {0}", name);
128-
return swift::remote::RemoteAddress(nullptr);
124+
return swift::remote::RemoteAddress();
129125
}
130126

131127
// If there's a single symbol, then we're golden. If there's more than
@@ -140,11 +136,12 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
140136
load_addr, m_process.GetAddressByteSize(), 0, error, true);
141137
if (sym_value != other_sym_value) {
142138
LLDB_LOGV(log, "[MemoryReader] symbol resolution failed {0}", name);
143-
return swift::remote::RemoteAddress(nullptr);
139+
return swift::remote::RemoteAddress();
144140
}
145141
}
146142
LLDB_LOGV(log, "[MemoryReader] symbol resolved to {0}", load_addr);
147-
return swift::remote::RemoteAddress(load_addr);
143+
return swift::remote::RemoteAddress(
144+
load_addr, swift::remote::RemoteAddress::DefaultAddressSpace);
148145
}
149146

150147
static std::unique_ptr<swift::SwiftObjectFileFormat>
@@ -179,8 +176,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
179176
if (!target.GetSwiftUseReflectionSymbols())
180177
return {};
181178

182-
std::optional<Address> maybeAddr =
183-
resolveRemoteAddress(address.getAddressData());
179+
std::optional<Address> maybeAddr = remoteAddressToLLDBAddress(address);
184180
// This is not an assert, but should never happen.
185181
if (!maybeAddr)
186182
return {};
@@ -191,7 +187,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
191187
addr = *maybeAddr;
192188
} else {
193189
// `address` is a real load address.
194-
if (!target.ResolveLoadAddress(address.getAddressData(), addr))
190+
if (!target.ResolveLoadAddress(address.getRawAddress(), addr))
195191
return {};
196192
}
197193

@@ -214,7 +210,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
214210
// aware of local symbols, so avoid returning those.
215211
using namespace swift::Demangle;
216212
if (isSwiftSymbol(mangledName) && !isOldFunctionTypeMangling(mangledName))
217-
return {{mangledName, 0}};
213+
return swift::remote::RemoteAbsolutePointer{mangledName, 0, address};
218214
}
219215

220216
return {};
@@ -228,23 +224,28 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
228224
// We may have gotten a pointer to a process address, try to map it back
229225
// to a tagged address so further memory reads originating from it benefit
230226
// from the file-cache optimization.
231-
swift::remote::RemoteAbsolutePointer process_pointer("", readValue);
227+
swift::remote::RemoteAbsolutePointer process_pointer{
228+
swift::remote::RemoteAddress{readValue, address.getAddressSpace()}};
232229

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

236237
// Try to strip the pointer before checking if we have it mapped.
237238
auto strippedPointer = signedPointerStripper(process_pointer);
238-
if (strippedPointer.isResolved())
239-
readValue = strippedPointer.getOffset();
239+
if (auto resolved = strippedPointer.getResolvedAddress())
240+
readValue = resolved.getRawAddress();
240241

241242
auto &target = m_process.GetTarget();
242243
Address addr;
243244
if (!target.ResolveLoadAddress(readValue, addr)) {
244245
LLDB_LOGV(log,
245246
"[MemoryReader] Could not resolve load address of pointer {0:x} "
246247
"read from {1:x}.",
247-
readValue, address.getAddressData());
248+
readValue, address.getRawAddress());
248249
return process_pointer;
249250
}
250251

@@ -262,15 +263,15 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
262263
LLDB_LOG(log,
263264
"[MemoryReader] Could not resolve find module containing pointer "
264265
"{0:x} read from {1:x}.",
265-
readValue, address.getAddressData());
266+
readValue, address.getRawAddress());
266267
return process_pointer;
267268
}
268269

269270
// If the containing image is the first registered one, the image's tagged
270-
// start address for it is the first tagged address. Otherwise, the previous
271-
// 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.
272273
uint64_t start_tagged_address = pair_iterator == m_range_module_map.begin()
273-
? LLDB_FILE_ADDRESS_BIT
274+
? 0
274275
: std::prev(pair_iterator)->first;
275276

276277
auto *section_list = module_containing_pointer->GetSectionList();
@@ -289,13 +290,16 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
289290
LLDB_LOG(log,
290291
"[MemoryReader] Pointer {0:x} read from {1:x} resolved to tagged "
291292
"address {2:x}, which is outside its image address space.",
292-
readValue, address.getAddressData(), tagged_address);
293+
readValue, address.getRawAddress(), tagged_address);
293294
return process_pointer;
294295
}
295296

296-
swift::remote::RemoteAbsolutePointer tagged_pointer("", tagged_address);
297-
if (tagged_address !=
298-
(uint64_t)signedPointerStripper(tagged_pointer).getOffset()) {
297+
swift::remote::RemoteAbsolutePointer tagged_pointer{
298+
swift::remote::RemoteAddress{tagged_address, LLDBAddressSpace}};
299+
300+
if (tagged_address != (uint64_t)signedPointerStripper(tagged_pointer)
301+
.getResolvedAddress()
302+
.getRawAddress()) {
299303
lldbassert(false &&
300304
"Tagged pointer runs into pointer authentication mask!");
301305
return process_pointer;
@@ -304,7 +308,7 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
304308
LLDB_LOGV(log,
305309
"[MemoryReader] Successfully resolved pointer {0:x} read from "
306310
"{1:x} to tagged address {2:x}.",
307-
readValue, address.getAddressData(), tagged_address);
311+
readValue, address.getRawAddress(), tagged_address);
308312
return tagged_pointer;
309313
}
310314

@@ -313,7 +317,7 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
313317
Log *log = GetLog(LLDBLog::Types);
314318
if (m_local_buffer) {
315319
bool overflow = false;
316-
auto addr = address.getAddressData();
320+
auto addr = address.getRawAddress();
317321
auto end = llvm::SaturatingAdd(addr, size, &overflow);
318322
if (overflow) {
319323
LLDB_LOGV(log, "[MemoryReader] address {0:x} + size {1} overflows", addr,
@@ -331,16 +335,16 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
331335
}
332336

333337
LLDB_LOGV(log, "[MemoryReader] asked to read {0} bytes at address {1:x}",
334-
size, address.getAddressData());
338+
size, address.getRawAddress());
335339
std::optional<Address> maybeAddr =
336-
resolveRemoteAddressFromSymbolObjectFile(address.getAddressData());
340+
resolveRemoteAddressFromSymbolObjectFile(address);
337341

338342
if (!maybeAddr)
339-
maybeAddr = resolveRemoteAddress(address.getAddressData());
343+
maybeAddr = remoteAddressToLLDBAddress(address);
340344

341345
if (!maybeAddr) {
342346
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
343-
address.getAddressData());
347+
address.getRawAddress());
344348
return false;
345349
}
346350
auto addr = *maybeAddr;
@@ -407,17 +411,17 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
407411
return std::string(stream.GetData());
408412
};
409413
LLDB_LOGV(log, "[MemoryReader] asked to read string data at address {0:x}",
410-
address.getAddressData());
414+
address.getRawAddress());
411415

412416
std::optional<Address> maybeAddr =
413-
resolveRemoteAddressFromSymbolObjectFile(address.getAddressData());
417+
resolveRemoteAddressFromSymbolObjectFile(address);
414418

415419
if (!maybeAddr)
416-
maybeAddr = resolveRemoteAddress(address.getAddressData());
420+
maybeAddr = remoteAddressToLLDBAddress(address);
417421

418422
if (!maybeAddr) {
419423
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
420-
address.getAddressData());
424+
address.getRawAddress());
421425
return false;
422426
}
423427
auto addr = *maybeAddr;
@@ -453,7 +457,7 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
453457

454458
MemoryReaderLocalBufferHolder::~MemoryReaderLocalBufferHolder() {
455459
if (m_memory_reader)
456-
m_memory_reader->popLocalBuffer();
460+
m_memory_reader->popLocalBuffer();
457461
}
458462

459463
MemoryReaderLocalBufferHolder
@@ -482,12 +486,9 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
482486
"Trying to register symbol object file, but reading from it is "
483487
"disabled!");
484488

485-
// The first available address is the mask, since subsequent images are mapped
486-
// in ascending order, all of them will contain this mask.
487-
uint64_t module_start_address = LLDB_FILE_ADDRESS_BIT;
489+
uint64_t module_start_address = 0;
488490
if (!m_range_module_map.empty())
489-
// We map the images contiguously one after the other, all with the tag bit
490-
// set.
491+
// We map the images contiguously one after the other.
491492
// The address that maps the last module is exactly the address the new
492493
// module should start at.
493494
module_start_address = m_range_module_map.back().first;
@@ -533,15 +534,6 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
533534
auto size = end_file_address - start_file_address;
534535
auto module_end_address = module_start_address + size;
535536

536-
if (module_end_address !=
537-
(uint64_t)signedPointerStripper(
538-
swift::remote::RemoteAbsolutePointer("", module_end_address))
539-
.getOffset()) {
540-
LLDB_LOG(GetLog(LLDBLog::Types),
541-
"[MemoryReader] module to address map ran into pointer "
542-
"authentication mask!");
543-
return {};
544-
}
545537
// The address for the next image is the next pointer aligned address
546538
// available after the end of the current image.
547539
uint64_t next_module_start_address = llvm::alignTo(module_end_address, 8);
@@ -555,18 +547,18 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
555547

556548
std::optional<std::pair<uint64_t, lldb::ModuleSP>>
557549
LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
558-
uint64_t tagged_address) const {
550+
swift::remote::RemoteAddress tagged_address) const {
559551
Log *log(GetLog(LLDBLog::Types));
560552

561553
if (!readMetadataFromFileCacheEnabled())
562554
return {};
563555

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

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

571563
// Explicitly compare only the addresses, never the modules in the pairs.
572564
auto pair_iterator = std::lower_bound(
@@ -578,7 +570,7 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
578570
LLDB_LOG(log,
579571
"[MemoryReader] Address {0:x} is larger than the upper bound "
580572
"address of the mapped in modules",
581-
tagged_address);
573+
tagged_address.getRawAddress());
582574
return {};
583575
}
584576

@@ -590,14 +582,13 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
590582
}
591583
uint64_t file_address;
592584
if (pair_iterator == m_range_module_map.begin())
593-
// Since this is the first registered module,
594-
// clearing the tag bit will give the virtual file address.
595-
file_address = tagged_address & ~LLDB_FILE_ADDRESS_BIT;
585+
file_address = tagged_address.getRawAddress();
596586
else
597587
// The end of the previous section is the start of the current one.
598588
// We also need to add the first section's file address since we remove it
599589
// when constructing the range to module map.
600-
file_address = tagged_address - std::prev(pair_iterator)->first;
590+
file_address =
591+
(tagged_address - std::prev(pair_iterator)->first).getRawAddress();
601592

602593
// We also need to add the module's file address, since we subtract it when
603594
// building the range to module map.
@@ -609,27 +600,28 @@ std::optional<swift::reflection::RemoteAddress>
609600
LLDBMemoryReader::resolveRemoteAddress(
610601
swift::reflection::RemoteAddress address) const {
611602
std::optional<Address> lldb_address =
612-
LLDBMemoryReader::resolveRemoteAddress(address.getAddressData());
603+
LLDBMemoryReader::remoteAddressToLLDBAddress(address);
613604
if (!lldb_address)
614605
return {};
615606
lldb::addr_t addr = lldb_address->GetLoadAddress(&m_process.GetTarget());
616607
if (addr != LLDB_INVALID_ADDRESS)
617-
return swift::reflection::RemoteAddress(addr);
608+
return swift::reflection::RemoteAddress(
609+
addr, swift::reflection::RemoteAddress::DefaultAddressSpace);
618610
return {};
619611
}
620612

621-
std::optional<Address>
622-
LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
613+
std::optional<Address> LLDBMemoryReader::remoteAddressToLLDBAddress(
614+
swift::remote::RemoteAddress address) const {
623615
Log *log(GetLog(LLDBLog::Types));
624616
auto maybe_pair = getFileAddressAndModuleForTaggedAddress(address);
625617
if (!maybe_pair)
626-
return Address(address);
618+
return Address(address.getRawAddress());
627619

628620
uint64_t file_address = maybe_pair->first;
629621
ModuleSP module = maybe_pair->second;
630622

631623
if (m_modules_with_metadata_in_symbol_obj_file.count(module))
632-
return Address(address);
624+
return Address(address.getRawAddress());
633625

634626
auto *object_file = module->GetObjectFile();
635627
if (!object_file)
@@ -645,7 +637,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
645637
LLDB_LOGV(log,
646638
"[MemoryReader] Successfully resolved mapped address {0:x} into "
647639
"file address {1:x}",
648-
address, resolved.GetFileAddress());
640+
address.getRawAddress(), resolved.GetFileAddress());
649641
return resolved;
650642
}
651643
auto *sec_list = module->GetSectionList();
@@ -689,7 +681,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
689681

690682
std::optional<Address>
691683
LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
692-
uint64_t address) const {
684+
swift::remote::RemoteAddress address) const {
693685
Log *log(GetLog(LLDBLog::Types));
694686

695687
if (!m_process.GetTarget().GetSwiftReadMetadataFromDSYM())
@@ -733,7 +725,7 @@ LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
733725
LLDB_LOGV(log,
734726
"[MemoryReader] Successfully resolved mapped address {0:x} into "
735727
"file address {1:x} from symbol object file.",
736-
address, file_address);
728+
address.getRawAddress(), file_address);
737729
return resolved;
738730
}
739731

0 commit comments

Comments
 (0)