@@ -229,7 +229,16 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
229
229
? LLDB_FILE_ADDRESS_BIT
230
230
: std::prev (pair_iterator)->first ;
231
231
232
- uint64_t tagged_address = start_tagged_address + addr.GetFileAddress ();
232
+ auto *section_list = module_containing_pointer->GetSectionList ();
233
+ if (section_list->GetSize () == 0 ) {
234
+ LLDB_LOG (log,
235
+ " [MemoryReader] Module with empty section list." );
236
+ return {};
237
+ }
238
+
239
+ uint64_t tagged_address =
240
+ start_tagged_address + addr.GetFileAddress () -
241
+ section_list->GetSectionAtIndex (0 )->GetFileAddress ();
233
242
234
243
if (tagged_address >= std::get<uint64_t >(*pair_iterator)) {
235
244
// If the tagged address invades the next image's tagged address space,
@@ -241,11 +250,19 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
241
250
return process_pointer;
242
251
}
243
252
253
+ swift::remote::RemoteAbsolutePointer tagged_pointer (" " , tagged_address);
254
+ if (tagged_address !=
255
+ (uint64_t )signedPointerStripper (tagged_pointer).getOffset ()) {
256
+ lldb_assert (false , " Tagged pointer runs into pointer authentication mask!" ,
257
+ __FUNCTION__, __FILE__, __LINE__);
258
+ return process_pointer;
259
+ }
260
+
244
261
LLDB_LOGV (log,
245
262
" [MemoryReader] Successfully resolved pointer {0:x} read from "
246
263
" {1:x} to tagged address {2:x}." ,
247
264
readValue, address.getAddressData (), tagged_address);
248
- return swift::remote::RemoteAbsolutePointer ( " " , tagged_address) ;
265
+ return tagged_pointer ;
249
266
}
250
267
251
268
bool LLDBMemoryReader::readBytes (swift::remote::RemoteAddress address,
@@ -454,13 +471,28 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
454
471
if (section_list_size == 0 )
455
472
return {};
456
473
474
+ auto first_section = section_list->GetSectionAtIndex (0 );
457
475
auto last_section =
458
476
section_list->GetSectionAtIndex (section_list->GetSize () - 1 );
459
- // The virtual file address + the size of last section gives us the total size
460
- // of this image in memory.
461
- uint64_t size = last_section->GetFileAddress () + last_section->GetByteSize ();
477
+
478
+ // The total size is the last section's file address plus size, subtracting the
479
+ // first section's file address.
480
+ auto start_file_address = first_section->GetFileAddress ();
481
+ uint64_t end_file_address =
482
+ last_section->GetFileAddress () + last_section->GetByteSize ();
483
+ auto size = end_file_address - start_file_address;
462
484
auto module_end_address = module_start_address + size;
463
485
486
+ if (module_end_address !=
487
+ (uint64_t )signedPointerStripper (
488
+ swift::remote::RemoteAbsolutePointer (" " , module_end_address))
489
+ .getOffset ()) {
490
+ lldb_assert (false ,
491
+ " LLDBMemoryReader module to address map ran into pointer "
492
+ " authentication mask!" ,
493
+ __FUNCTION__, __FILE__, __LINE__);
494
+ return {};
495
+ }
464
496
// The address for the next image is the next pointer aligned address
465
497
// available after the end of the current image.
466
498
uint64_t next_module_start_address = llvm::alignTo (module_end_address, 8 );
@@ -502,15 +534,26 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
502
534
}
503
535
504
536
ModuleSP module = pair_iterator->second ;
537
+ auto *section_list = module ->GetSectionList ();
538
+ if (section_list->GetSize () == 0 ) {
539
+ LLDB_LOG (log,
540
+ " [MemoryReader] Module with empty section list." );
541
+ return {};
542
+ }
505
543
uint64_t file_address;
506
544
if (pair_iterator == m_range_module_map.begin ())
507
545
// Since this is the first registered module,
508
546
// clearing the tag bit will give the virtual file address.
509
547
file_address = tagged_address & ~LLDB_FILE_ADDRESS_BIT;
510
548
else
511
549
// The end of the previous section is the start of the current one.
550
+ // We also need to add the first section's file address since we remove it
551
+ // when constructing the range to module map.
512
552
file_address = tagged_address - std::prev (pair_iterator)->first ;
513
553
554
+ // We also need to add the module's file address, since we subtract it when
555
+ // building the range to module map.
556
+ file_address += section_list->GetSectionAtIndex (0 )->GetFileAddress ();
514
557
return {{file_address, module }};
515
558
}
516
559
0 commit comments