Skip to content

[RemoteInspection] Make MemoryReader::readRemoteAddress virtual #83122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions include/swift/Remote/MemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,10 @@ class MemoryReader {
/// Returns false if the operator failed.
template <typename IntegerType>
bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) {
IntegerType buf;
if (!readInteger(address, &buf))
return false;

out = RemoteAddress((uint64_t)buf, address.getAddressSpace());
return true;
constexpr std::size_t integerSize = sizeof(IntegerType);
static_assert((integerSize == 4 || integerSize == 8) &&
"Only 32 or 64 bit architectures are supported!");
return readRemoteAddressImpl(address, out, integerSize);
}

/// Attempts to read an integer from the given address in the remote
Expand Down Expand Up @@ -338,6 +336,40 @@ class MemoryReader {
}

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
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Remote/RemoteAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RemoteAddress {
}

bool inRange(const RemoteAddress &begin, const RemoteAddress &end) const {
assert(begin.AddressSpace != end.AddressSpace &&
assert(begin.AddressSpace == end.AddressSpace &&
"Unexpected address spaces");
if (AddressSpace != begin.AddressSpace)
return false;
Expand Down