@@ -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
0 commit comments