@@ -32,11 +32,6 @@ bool LLDBMemoryReader::queryDataLayout(DataLayoutQueryType type, void *inBuffer,
32
32
return false ;
33
33
// The mask returned by the process masks out the non-addressable bits.
34
34
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;
40
35
memcpy (outBuffer, &mask_pattern, m_process.GetAddressByteSize ());
41
36
return true ;
42
37
}
@@ -84,7 +79,7 @@ swift::remote::RemoteAddress
84
79
LLDBMemoryReader::getSymbolAddress (const std::string &name) {
85
80
lldbassert (!name.empty ());
86
81
if (name.empty ())
87
- return swift::remote::RemoteAddress (nullptr );
82
+ return swift::remote::RemoteAddress ();
88
83
89
84
Log *log = GetLog (LLDBLog::Types);
90
85
@@ -97,7 +92,7 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
97
92
name_cs, lldb::eSymbolTypeAny, sc_list);
98
93
if (!sc_list.GetSize ()) {
99
94
LLDB_LOGV (log, " [MemoryReader] symbol resolution failed {0}" , name);
100
- return swift::remote::RemoteAddress (nullptr );
95
+ return swift::remote::RemoteAddress ();
101
96
}
102
97
103
98
SymbolContext sym_ctx;
@@ -118,14 +113,15 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
118
113
if (sym_ctx.symbol ) {
119
114
auto load_addr = sym_ctx.symbol ->GetLoadAddress (&m_process.GetTarget ());
120
115
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);
122
118
}
123
119
}
124
120
125
121
// Empty list, resolution failed.
126
122
if (sc_list.GetSize () == 0 ) {
127
123
LLDB_LOGV (log, " [MemoryReader] symbol resolution failed {0}" , name);
128
- return swift::remote::RemoteAddress (nullptr );
124
+ return swift::remote::RemoteAddress ();
129
125
}
130
126
131
127
// 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) {
140
136
load_addr, m_process.GetAddressByteSize (), 0 , error, true );
141
137
if (sym_value != other_sym_value) {
142
138
LLDB_LOGV (log, " [MemoryReader] symbol resolution failed {0}" , name);
143
- return swift::remote::RemoteAddress (nullptr );
139
+ return swift::remote::RemoteAddress ();
144
140
}
145
141
}
146
142
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);
148
145
}
149
146
150
147
static std::unique_ptr<swift::SwiftObjectFileFormat>
@@ -179,8 +176,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
179
176
if (!target.GetSwiftUseReflectionSymbols ())
180
177
return {};
181
178
182
- std::optional<Address> maybeAddr =
183
- resolveRemoteAddress (address.getAddressData ());
179
+ std::optional<Address> maybeAddr = remoteAddressToLLDBAddress (address);
184
180
// This is not an assert, but should never happen.
185
181
if (!maybeAddr)
186
182
return {};
@@ -191,7 +187,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
191
187
addr = *maybeAddr;
192
188
} else {
193
189
// `address` is a real load address.
194
- if (!target.ResolveLoadAddress (address.getAddressData (), addr))
190
+ if (!target.ResolveLoadAddress (address.getRawAddress (), addr))
195
191
return {};
196
192
}
197
193
@@ -214,7 +210,7 @@ LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
214
210
// aware of local symbols, so avoid returning those.
215
211
using namespace swift ::Demangle;
216
212
if (isSwiftSymbol (mangledName) && !isOldFunctionTypeMangling (mangledName))
217
- return {{ mangledName, 0 } };
213
+ return swift::remote::RemoteAbsolutePointer{ mangledName, 0 , address };
218
214
}
219
215
220
216
return {};
@@ -228,23 +224,28 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
228
224
// We may have gotten a pointer to a process address, try to map it back
229
225
// to a tagged address so further memory reads originating from it benefit
230
226
// 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 ()}};
232
229
233
- if (!readMetadataFromFileCacheEnabled ())
230
+ if (!readMetadataFromFileCacheEnabled ()) {
231
+ assert (address.getAddressSpace () ==
232
+ swift::remote::RemoteAddress::DefaultAddressSpace &&
233
+ " Unexpected address space!" );
234
234
return process_pointer;
235
+ }
235
236
236
237
// Try to strip the pointer before checking if we have it mapped.
237
238
auto strippedPointer = signedPointerStripper (process_pointer);
238
- if (strippedPointer.isResolved ())
239
- readValue = strippedPointer. getOffset ();
239
+ if (auto resolved = strippedPointer.getResolvedAddress ())
240
+ readValue = resolved. getRawAddress ();
240
241
241
242
auto &target = m_process.GetTarget ();
242
243
Address addr;
243
244
if (!target.ResolveLoadAddress (readValue, addr)) {
244
245
LLDB_LOGV (log,
245
246
" [MemoryReader] Could not resolve load address of pointer {0:x} "
246
247
" read from {1:x}." ,
247
- readValue, address.getAddressData ());
248
+ readValue, address.getRawAddress ());
248
249
return process_pointer;
249
250
}
250
251
@@ -262,15 +263,15 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
262
263
LLDB_LOG (log,
263
264
" [MemoryReader] Could not resolve find module containing pointer "
264
265
" {0:x} read from {1:x}." ,
265
- readValue, address.getAddressData ());
266
+ readValue, address.getRawAddress ());
266
267
return process_pointer;
267
268
}
268
269
269
270
// 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.
272
273
uint64_t start_tagged_address = pair_iterator == m_range_module_map.begin ()
273
- ? LLDB_FILE_ADDRESS_BIT
274
+ ? 0
274
275
: std::prev (pair_iterator)->first ;
275
276
276
277
auto *section_list = module_containing_pointer->GetSectionList ();
@@ -289,13 +290,16 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
289
290
LLDB_LOG (log,
290
291
" [MemoryReader] Pointer {0:x} read from {1:x} resolved to tagged "
291
292
" address {2:x}, which is outside its image address space." ,
292
- readValue, address.getAddressData (), tagged_address);
293
+ readValue, address.getRawAddress (), tagged_address);
293
294
return process_pointer;
294
295
}
295
296
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 ()) {
299
303
lldbassert (false &&
300
304
" Tagged pointer runs into pointer authentication mask!" );
301
305
return process_pointer;
@@ -304,7 +308,7 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
304
308
LLDB_LOGV (log,
305
309
" [MemoryReader] Successfully resolved pointer {0:x} read from "
306
310
" {1:x} to tagged address {2:x}." ,
307
- readValue, address.getAddressData (), tagged_address);
311
+ readValue, address.getRawAddress (), tagged_address);
308
312
return tagged_pointer;
309
313
}
310
314
@@ -313,7 +317,7 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
313
317
Log *log = GetLog (LLDBLog::Types);
314
318
if (m_local_buffer) {
315
319
bool overflow = false ;
316
- auto addr = address.getAddressData ();
320
+ auto addr = address.getRawAddress ();
317
321
auto end = llvm::SaturatingAdd (addr, size, &overflow);
318
322
if (overflow) {
319
323
LLDB_LOGV (log, " [MemoryReader] address {0:x} + size {1} overflows" , addr,
@@ -331,16 +335,16 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
331
335
}
332
336
333
337
LLDB_LOGV (log, " [MemoryReader] asked to read {0} bytes at address {1:x}" ,
334
- size, address.getAddressData ());
338
+ size, address.getRawAddress ());
335
339
std::optional<Address> maybeAddr =
336
- resolveRemoteAddressFromSymbolObjectFile (address. getAddressData () );
340
+ resolveRemoteAddressFromSymbolObjectFile (address);
337
341
338
342
if (!maybeAddr)
339
- maybeAddr = resolveRemoteAddress (address. getAddressData () );
343
+ maybeAddr = remoteAddressToLLDBAddress (address);
340
344
341
345
if (!maybeAddr) {
342
346
LLDB_LOGV (log, " [MemoryReader] could not resolve address {0:x}" ,
343
- address.getAddressData ());
347
+ address.getRawAddress ());
344
348
return false ;
345
349
}
346
350
auto addr = *maybeAddr;
@@ -407,17 +411,17 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
407
411
return std::string (stream.GetData ());
408
412
};
409
413
LLDB_LOGV (log, " [MemoryReader] asked to read string data at address {0:x}" ,
410
- address.getAddressData ());
414
+ address.getRawAddress ());
411
415
412
416
std::optional<Address> maybeAddr =
413
- resolveRemoteAddressFromSymbolObjectFile (address. getAddressData () );
417
+ resolveRemoteAddressFromSymbolObjectFile (address);
414
418
415
419
if (!maybeAddr)
416
- maybeAddr = resolveRemoteAddress (address. getAddressData () );
420
+ maybeAddr = remoteAddressToLLDBAddress (address);
417
421
418
422
if (!maybeAddr) {
419
423
LLDB_LOGV (log, " [MemoryReader] could not resolve address {0:x}" ,
420
- address.getAddressData ());
424
+ address.getRawAddress ());
421
425
return false ;
422
426
}
423
427
auto addr = *maybeAddr;
@@ -453,7 +457,7 @@ bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
453
457
454
458
MemoryReaderLocalBufferHolder::~MemoryReaderLocalBufferHolder () {
455
459
if (m_memory_reader)
456
- m_memory_reader->popLocalBuffer ();
460
+ m_memory_reader->popLocalBuffer ();
457
461
}
458
462
459
463
MemoryReaderLocalBufferHolder
@@ -482,12 +486,9 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
482
486
" Trying to register symbol object file, but reading from it is "
483
487
" disabled!" );
484
488
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 ;
488
490
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.
491
492
// The address that maps the last module is exactly the address the new
492
493
// module should start at.
493
494
module_start_address = m_range_module_map.back ().first ;
@@ -533,15 +534,6 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
533
534
auto size = end_file_address - start_file_address;
534
535
auto module_end_address = module_start_address + size;
535
536
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
- }
545
537
// The address for the next image is the next pointer aligned address
546
538
// available after the end of the current image.
547
539
uint64_t next_module_start_address = llvm::alignTo (module_end_address, 8 );
@@ -555,18 +547,18 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
555
547
556
548
std::optional<std::pair<uint64_t , lldb::ModuleSP>>
557
549
LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress (
558
- uint64_t tagged_address) const {
550
+ swift::remote::RemoteAddress tagged_address) const {
559
551
Log *log (GetLog (LLDBLog::Types));
560
552
561
553
if (!readMetadataFromFileCacheEnabled ())
562
554
return {};
563
555
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)
566
557
return {};
567
558
568
559
// 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 ());
570
562
571
563
// Explicitly compare only the addresses, never the modules in the pairs.
572
564
auto pair_iterator = std::lower_bound (
@@ -578,7 +570,7 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
578
570
LLDB_LOG (log,
579
571
" [MemoryReader] Address {0:x} is larger than the upper bound "
580
572
" address of the mapped in modules" ,
581
- tagged_address);
573
+ tagged_address. getRawAddress () );
582
574
return {};
583
575
}
584
576
@@ -590,14 +582,13 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
590
582
}
591
583
uint64_t file_address;
592
584
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 ();
596
586
else
597
587
// The end of the previous section is the start of the current one.
598
588
// We also need to add the first section's file address since we remove it
599
589
// 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 ();
601
592
602
593
// We also need to add the module's file address, since we subtract it when
603
594
// building the range to module map.
@@ -609,27 +600,28 @@ std::optional<swift::reflection::RemoteAddress>
609
600
LLDBMemoryReader::resolveRemoteAddress (
610
601
swift::reflection::RemoteAddress address) const {
611
602
std::optional<Address> lldb_address =
612
- LLDBMemoryReader::resolveRemoteAddress (address. getAddressData () );
603
+ LLDBMemoryReader::remoteAddressToLLDBAddress (address);
613
604
if (!lldb_address)
614
605
return {};
615
606
lldb::addr_t addr = lldb_address->GetLoadAddress (&m_process.GetTarget ());
616
607
if (addr != LLDB_INVALID_ADDRESS)
617
- return swift::reflection::RemoteAddress (addr);
608
+ return swift::reflection::RemoteAddress (
609
+ addr, swift::reflection::RemoteAddress::DefaultAddressSpace);
618
610
return {};
619
611
}
620
612
621
- std::optional<Address>
622
- LLDBMemoryReader::resolveRemoteAddress ( uint64_t address) const {
613
+ std::optional<Address> LLDBMemoryReader::remoteAddressToLLDBAddress (
614
+ swift::remote::RemoteAddress address) const {
623
615
Log *log (GetLog (LLDBLog::Types));
624
616
auto maybe_pair = getFileAddressAndModuleForTaggedAddress (address);
625
617
if (!maybe_pair)
626
- return Address (address);
618
+ return Address (address. getRawAddress () );
627
619
628
620
uint64_t file_address = maybe_pair->first ;
629
621
ModuleSP module = maybe_pair->second ;
630
622
631
623
if (m_modules_with_metadata_in_symbol_obj_file.count (module ))
632
- return Address (address);
624
+ return Address (address. getRawAddress () );
633
625
634
626
auto *object_file = module ->GetObjectFile ();
635
627
if (!object_file)
@@ -645,7 +637,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
645
637
LLDB_LOGV (log,
646
638
" [MemoryReader] Successfully resolved mapped address {0:x} into "
647
639
" file address {1:x}" ,
648
- address, resolved.GetFileAddress ());
640
+ address. getRawAddress () , resolved.GetFileAddress ());
649
641
return resolved;
650
642
}
651
643
auto *sec_list = module ->GetSectionList ();
@@ -689,7 +681,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
689
681
690
682
std::optional<Address>
691
683
LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile (
692
- uint64_t address) const {
684
+ swift::remote::RemoteAddress address) const {
693
685
Log *log (GetLog (LLDBLog::Types));
694
686
695
687
if (!m_process.GetTarget ().GetSwiftReadMetadataFromDSYM ())
@@ -733,7 +725,7 @@ LLDBMemoryReader::resolveRemoteAddressFromSymbolObjectFile(
733
725
LLDB_LOGV (log,
734
726
" [MemoryReader] Successfully resolved mapped address {0:x} into "
735
727
" file address {1:x} from symbol object file." ,
736
- address, file_address);
728
+ address. getRawAddress () , file_address);
737
729
return resolved;
738
730
}
739
731
0 commit comments