diff --git a/include/swift/Remote/MemoryReader.h b/include/swift/Remote/MemoryReader.h index 6b2184f03962e..fa2c201cf9eb6 100644 --- a/include/swift/Remote/MemoryReader.h +++ b/include/swift/Remote/MemoryReader.h @@ -135,12 +135,10 @@ class MemoryReader { /// Returns false if the operator failed. template 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 @@ -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(ptr)); }); + if (!readBytes(address, reinterpret_cast(Buf), integerSize)) + return false; + + if (integerSize == 4) + out = RemoteAddress(*reinterpret_cast(Buf), + address.getAddressSpace()); + else if (integerSize == 8) + out = RemoteAddress(*reinterpret_cast(Buf), + address.getAddressSpace()); + else + return false; + + return true; + } }; } // end namespace remote diff --git a/include/swift/Remote/RemoteAddress.h b/include/swift/Remote/RemoteAddress.h index 91313cbd31b92..ccb886d6f83b7 100644 --- a/include/swift/Remote/RemoteAddress.h +++ b/include/swift/Remote/RemoteAddress.h @@ -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;