Skip to content

Commit 2ec8f05

Browse files
committed
[RemoteInspection] Make MemoryReader::readRemoteAddress virtual
Allow subclasses to override the behavior of readRemoteAddress. LLDB in particular needs this to correctly set the address space of the result remote address. rdar://148361743 (cherry picked from commit 45d6d45)
1 parent ac62ffc commit 2ec8f05

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

include/swift/Remote/MemoryReader.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ class MemoryReader {
6464
/// Returns false if the operator failed.
6565
template <typename IntegerType>
6666
bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) {
67-
IntegerType buf;
68-
if (!readInteger(address, &buf))
69-
return false;
70-
71-
out = RemoteAddress((uint64_t)buf, address.getAddressSpace());
72-
return true;
67+
constexpr std::size_t integerSize = sizeof(IntegerType);
68+
static_assert((integerSize == 4 || integerSize == 8) &&
69+
"Only 32 or 64 bit architectures are supported!");
70+
return readRemoteAddressImpl(address, out, integerSize);
7371
}
7472

7573
/// Attempts to read an integer from the given address in the remote
@@ -280,6 +278,40 @@ class MemoryReader {
280278
}
281279

282280
virtual ~MemoryReader() = default;
281+
282+
protected:
283+
/// Implementation detail of remoteRemoteAddress. This exists because
284+
/// templated functions cannot be virtual.
285+
///
286+
/// Attempts to read a remote address of a given size from the given address
287+
/// in the remote process.
288+
///
289+
/// Returns false if the operator failed.
290+
virtual bool readRemoteAddressImpl(RemoteAddress address, RemoteAddress &out,
291+
std::size_t integerSize) {
292+
assert((integerSize == 4 || integerSize == 8) &&
293+
"Only 32 or 64 bit architectures are supported!");
294+
auto Buf = std::malloc(integerSize);
295+
if (!Buf)
296+
return false;
297+
298+
// Free Buf when this function return.
299+
ReadBytesResult Result(
300+
Buf, [](const void *ptr) { free(const_cast<void *>(ptr)); });
301+
if (!readBytes(address, reinterpret_cast<uint8_t *>(Buf), integerSize))
302+
return false;
303+
304+
if (integerSize == 4)
305+
out = RemoteAddress(*reinterpret_cast<uint32_t *>(Buf),
306+
address.getAddressSpace());
307+
else if (integerSize == 8)
308+
out = RemoteAddress(*reinterpret_cast<uint64_t *>(Buf),
309+
address.getAddressSpace());
310+
else
311+
return false;
312+
313+
return true;
314+
}
283315
};
284316

285317
} // end namespace remote

include/swift/Remote/RemoteAddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class RemoteAddress {
5656
}
5757

5858
bool inRange(const RemoteAddress &begin, const RemoteAddress &end) const {
59-
assert(begin.AddressSpace != end.AddressSpace &&
59+
assert(begin.AddressSpace == end.AddressSpace &&
6060
"Unexpected address spaces");
6161
if (AddressSpace != begin.AddressSpace)
6262
return false;

0 commit comments

Comments
 (0)