Skip to content

Commit f4913ea

Browse files
committed
[RemoteMirrors] Add hook for resolving indirect addresses
Adds a hook so implementations of memory reader can add logic to resolving remote addresses. This is needed because of an interaction between LLDB, which tries to read memory from files instead of process memory whenever possible and the DYLD shared cache. The shared cache will merge pointers in the GOT sections from multiple images into one location, and update the relative offsets to point to the new location. LLDB, will have initially read the offset pointing to the "old" location, which will be zeroed out in live memory. This gives LLDB the opportunity to re-read the relative offset, but from live memory, so it can return the right pointer in the shared cache. rdar://160837587
1 parent c4af8bf commit f4913ea

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

include/swift/Remote/MemoryReader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ class MemoryReader {
192192
return ReadObjResult<T>(reinterpret_cast<const T *>(ptr), deleter);
193193
}
194194

195+
/// Resolves an indirect address at the given relative offset.
196+
///
197+
/// \param address The base address which contains the relative offset.
198+
/// \param offset The offset read.
199+
/// \param directnessEncodedInOffset Whether the relative offset encodes the
200+
/// directness as the last bit. Note that this is not the offset passed in as
201+
/// a parameter, but whether the offset read at address would have the last
202+
/// bit set.
203+
virtual RemoteAddress resolveIndirectAddressAtOffset(RemoteAddress address, uint64_t offset, bool directnessEncodedInOffset) {
204+
return address + offset;
205+
}
206+
195207
/// Attempts to read 'size' bytes from the given address in the remote process.
196208
///
197209
/// Returns a pointer to the requested data and a function that must be called to

include/swift/Remote/MetadataReader.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,18 +457,20 @@ class MetadataReader {
457457
swift::Demangle::NodePointer {
458458
// Resolve the reference to a remote address.
459459
auto offsetInMangledName =
460-
(const char *)base - mangledName.getLocalBuffer();
461-
auto remoteAddress =
462-
mangledName.getRemoteAddress() + offsetInMangledName + offset;
460+
(const char *)base - mangledName.getLocalBuffer();
461+
auto offsetAddress = mangledName.getRemoteAddress() + offsetInMangledName;
463462

464463
RemoteAbsolutePointer resolved;
465464
if (directness == Directness::Indirect) {
465+
auto remoteAddress = Reader->resolveIndirectAddressAtOffset(
466+
offsetAddress, offset, /*directnessEncodedInOffset=*/false);
466467
if (auto indirectAddress = readPointer(remoteAddress)) {
467468
resolved = stripSignedPointer(*indirectAddress);
468469
} else {
469470
return nullptr;
470471
}
471472
} else {
473+
auto remoteAddress = offsetAddress + offset;
472474
resolved = Reader->getSymbol(remoteAddress);
473475
}
474476

@@ -2084,17 +2086,19 @@ class MetadataReader {
20842086

20852087
using SignedPointer = typename std::make_signed<StoredPointer>::type;
20862088

2087-
RemoteAddress resultAddress = getAddress(fieldRef) + (SignedPointer)offset;
2088-
20892089
// Low bit set in the offset indicates that the offset leads to the absolute
20902090
// address in memory.
20912091
if (indirect) {
2092+
RemoteAddress resultAddress = Reader->resolveIndirectAddressAtOffset(
2093+
getAddress(fieldRef), (SignedPointer)offset,
2094+
/*directnessEncodedInOffset=*/true);
20922095
if (auto ptr = readPointer(resultAddress)) {
20932096
return stripSignedPointer(*ptr);
20942097
}
20952098
return std::nullopt;
20962099
}
20972100

2101+
RemoteAddress resultAddress = getAddress(fieldRef) + (SignedPointer)offset;
20982102
return RemoteAbsolutePointer(resultAddress);
20992103
}
21002104

0 commit comments

Comments
 (0)