Merge pull request #83123 from augusto2112/virtual-read-remote-addr-6.2

[NFC][RemoteInspection] Make MemoryReader::readRemoteAddress virtual
This commit is contained in:
fredriss
2025-07-18 15:12:35 -07:00
committed by GitHub
3 changed files with 57 additions and 24 deletions

View File

@@ -64,12 +64,10 @@ public:
/// Returns false if the operator failed. /// Returns false if the operator failed.
template <typename IntegerType> template <typename IntegerType>
bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) { bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) {
IntegerType buf; constexpr std::size_t integerSize = sizeof(IntegerType);
if (!readInteger(address, &buf)) static_assert((integerSize == 4 || integerSize == 8) &&
return false; "Only 32 or 64 bit architectures are supported!");
return readRemoteAddressImpl(address, out, integerSize);
out = RemoteAddress((uint64_t)buf, address.getAddressSpace());
return true;
} }
/// Attempts to read an integer from the given address in the remote /// Attempts to read an integer from the given address in the remote
@@ -280,6 +278,40 @@ public:
} }
virtual ~MemoryReader() = default; virtual ~MemoryReader() = default;
protected:
/// Implementation detail of remoteRemoteAddress. This exists because
/// templated functions cannot be virtual.
///
/// Attempts to read a remote address of a given size from the given address
/// in the remote process.
///
/// Returns false if the operator failed.
virtual bool readRemoteAddressImpl(RemoteAddress address, RemoteAddress &out,
std::size_t integerSize) {
assert((integerSize == 4 || integerSize == 8) &&
"Only 32 or 64 bit architectures are supported!");
auto Buf = std::malloc(integerSize);
if (!Buf)
return false;
// Free Buf when this function return.
ReadBytesResult Result(
Buf, [](const void *ptr) { free(const_cast<void *>(ptr)); });
if (!readBytes(address, reinterpret_cast<uint8_t *>(Buf), integerSize))
return false;
if (integerSize == 4)
out = RemoteAddress(*reinterpret_cast<uint32_t *>(Buf),
address.getAddressSpace());
else if (integerSize == 8)
out = RemoteAddress(*reinterpret_cast<uint64_t *>(Buf),
address.getAddressSpace());
else
return false;
return true;
}
}; };
} // end namespace remote } // end namespace remote

View File

@@ -801,8 +801,8 @@ public:
// Non-inline (box'ed) representation. // Non-inline (box'ed) representation.
// The first word of the container stores the address to the box. // The first word of the container stores the address to the box.
RemoteAddress BoxAddress; RemoteAddress BoxAddress;
if (!Reader->readRemoteAddress<StoredPointer>(ExistentialAddress, if (!Reader->template readRemoteAddress<StoredPointer>(ExistentialAddress,
BoxAddress)) BoxAddress))
return std::nullopt; return std::nullopt;
auto AlignmentMask = VWT->getAlignmentMask(); auto AlignmentMask = VWT->getAlignmentMask();
@@ -1694,7 +1694,7 @@ public:
StoredPointer tag) -> std::optional<RemoteAddress> { StoredPointer tag) -> std::optional<RemoteAddress> {
RemoteAddress addr = base + tag * sizeof(StoredPointer); RemoteAddress addr = base + tag * sizeof(StoredPointer);
RemoteAddress isa; RemoteAddress isa;
if (!Reader->readRemoteAddress<StoredPointer>(addr, isa)) if (!Reader->template readRemoteAddress<StoredPointer>(addr, isa))
return std::nullopt; return std::nullopt;
return isa; return isa;
}; };
@@ -1724,7 +1724,7 @@ public:
return readMetadataFromTaggedPointer(objectAddress); return readMetadataFromTaggedPointer(objectAddress);
RemoteAddress isa; RemoteAddress isa;
if (!Reader->readRemoteAddress<StoredPointer>(objectAddress, isa)) if (!Reader->template readRemoteAddress<StoredPointer>(objectAddress, isa))
return std::nullopt; return std::nullopt;
switch (getIsaEncoding()) { switch (getIsaEncoding()) {
@@ -1773,8 +1773,8 @@ public:
RemoteAddress(IndexedClassesPointer RemoteAddress(IndexedClassesPointer
+ classIndex * sizeof(StoredPointer)); + classIndex * sizeof(StoredPointer));
RemoteAddress metadataPointer; RemoteAddress metadataPointer;
if (!Reader->readRemoteAddress<StoredPointer>(eltPointer, if (!Reader->template readRemoteAddress<StoredPointer>(eltPointer,
metadataPointer)) metadataPointer))
return std::nullopt; return std::nullopt;
return metadataPointer; return metadataPointer;
@@ -1922,7 +1922,7 @@ public:
case TypeReferenceKind::IndirectObjCClass: { case TypeReferenceKind::IndirectObjCClass: {
RemoteAddress classRef; RemoteAddress classRef;
if (!Reader->readRemoteAddress<StoredPointer>(ref, classRef)) if (!Reader->template readRemoteAddress<StoredPointer>(ref, classRef))
return std::nullopt; return std::nullopt;
auto metadata = readMetadata(classRef); auto metadata = readMetadata(classRef);
@@ -1970,8 +1970,8 @@ public:
return std::nullopt; return std::nullopt;
RemoteAddress genericArgAddress; RemoteAddress genericArgAddress;
if (!Reader->readRemoteAddress<StoredPointer>(addressOfGenericArgAddress, if (!Reader->template readRemoteAddress<StoredPointer>(
genericArgAddress)) addressOfGenericArgAddress, genericArgAddress))
return std::nullopt; return std::nullopt;
return genericArgAddress; return genericArgAddress;
@@ -2107,8 +2107,8 @@ protected:
// Read the name pointer. // Read the name pointer.
RemoteAddress namePtr; RemoteAddress namePtr;
if (!Reader->readRemoteAddress<StoredPointer>(roDataPtr + OffsetToName, if (!Reader->template readRemoteAddress<StoredPointer>(
namePtr)) roDataPtr + OffsetToName, namePtr))
return false; return false;
// If the name pointer is null, treat that as an error. // If the name pointer is null, treat that as an error.
@@ -2172,8 +2172,8 @@ protected:
// the generalization arguments are. // the generalization arguments are.
RemoteAddress shapeAddress = address + sizeof(StoredPointer); RemoteAddress shapeAddress = address + sizeof(StoredPointer);
RemoteAddress signedShapePtr; RemoteAddress signedShapePtr;
if (!Reader->readRemoteAddress<StoredPointer>(shapeAddress, if (!Reader->template readRemoteAddress<StoredPointer>(shapeAddress,
signedShapePtr)) signedShapePtr))
return nullptr; return nullptr;
auto shapePtr = stripSignedPointer(signedShapePtr); auto shapePtr = stripSignedPointer(signedShapePtr);
@@ -2637,8 +2637,8 @@ private:
// Low bit set in the offset indicates that the offset leads to the absolute // Low bit set in the offset indicates that the offset leads to the absolute
// address in memory. // address in memory.
if (indirect) { if (indirect) {
if (!Reader->readRemoteAddress<StoredPointer>(resultAddress, if (!Reader->template readRemoteAddress<StoredPointer>(resultAddress,
resultAddress)) resultAddress))
return RemoteAddress(); return RemoteAddress();
} }
@@ -3152,7 +3152,8 @@ private:
--numGenericArgs; --numGenericArgs;
RemoteAddress arg; RemoteAddress arg;
if (!Reader->readRemoteAddress<StoredPointer>(genericArgsAddr, arg)) { if (!Reader->template readRemoteAddress<StoredPointer>(
genericArgsAddr, arg)) {
return {}; return {};
} }
genericArgsAddr += sizeof(StoredPointer); genericArgsAddr += sizeof(StoredPointer);
@@ -3281,7 +3282,7 @@ private:
#if SWIFT_OBJC_INTEROP #if SWIFT_OBJC_INTEROP
RemoteAddress dataPtr; RemoteAddress dataPtr;
if (!Reader->readRemoteAddress<StoredPointer>( if (!Reader->template readRemoteAddress<StoredPointer>(
classAddress + TargetClassMetadataObjCInterop::offsetToData(), classAddress + TargetClassMetadataObjCInterop::offsetToData(),
dataPtr)) dataPtr))
return RemoteAddress(); return RemoteAddress();

View File

@@ -56,7 +56,7 @@ public:
} }
bool inRange(const RemoteAddress &begin, const RemoteAddress &end) const { bool inRange(const RemoteAddress &begin, const RemoteAddress &end) const {
assert(begin.AddressSpace != end.AddressSpace && assert(begin.AddressSpace == end.AddressSpace &&
"Unexpected address spaces"); "Unexpected address spaces");
if (AddressSpace != begin.AddressSpace) if (AddressSpace != begin.AddressSpace)
return false; return false;