Skip to content

Commit 45d6d45

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
1 parent 8dfff8c commit 45d6d45

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
@@ -135,12 +135,10 @@ class MemoryReader {
135135
/// Returns false if the operator failed.
136136
template <typename IntegerType>
137137
bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) {
138-
IntegerType buf;
139-
if (!readInteger(address, &buf))
140-
return false;
141-
142-
out = RemoteAddress((uint64_t)buf, address.getAddressSpace());
143-
return true;
138+
constexpr std::size_t integerSize = sizeof(IntegerType);
139+
static_assert((integerSize == 4 || integerSize == 8) &&
140+
"Only 32 or 64 bit architectures are supported!");
141+
return readRemoteAddressImpl(address, out, integerSize);
144142
}
145143

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

340338
virtual ~MemoryReader() = default;
339+
340+
protected:
341+
/// Implementation detail of remoteRemoteAddress. This exists because
342+
/// templated functions cannot be virtual.
343+
///
344+
/// Attempts to read a remote address of a given size from the given address
345+
/// in the remote process.
346+
///
347+
/// Returns false if the operator failed.
348+
virtual bool readRemoteAddressImpl(RemoteAddress address, RemoteAddress &out,
349+
std::size_t integerSize) {
350+
assert((integerSize == 4 || integerSize == 8) &&
351+
"Only 32 or 64 bit architectures are supported!");
352+
auto Buf = std::malloc(integerSize);
353+
if (!Buf)
354+
return false;
355+
356+
// Free Buf when this function return.
357+
ReadBytesResult Result(
358+
Buf, [](const void *ptr) { free(const_cast<void *>(ptr)); });
359+
if (!readBytes(address, reinterpret_cast<uint8_t *>(Buf), integerSize))
360+
return false;
361+
362+
if (integerSize == 4)
363+
out = RemoteAddress(*reinterpret_cast<uint32_t *>(Buf),
364+
address.getAddressSpace());
365+
else if (integerSize == 8)
366+
out = RemoteAddress(*reinterpret_cast<uint64_t *>(Buf),
367+
address.getAddressSpace());
368+
else
369+
return false;
370+
371+
return true;
372+
}
341373
};
342374

343375
} // 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)