Skip to content

Commit 58df553

Browse files
committed
[NFC][RemoteInspection] Add an opaque AddressSpace field to RemoteAddress
Add an extra opaque field to AddressSpace, which can be used by clients of RemoteInspection to distinguish between different address spaces. LLDB employs an optimization where it reads memory from files instead of the running process whenever it can to speed up memory reads (these can be slow when debugging something over a network). To do this, it needs to keep track whether an address originated from a process or a file. It currently distinguishes addresses by setting an unused high bit on the address, but because of pointer authentication this is not a reliable solution. In order to keep this optimization working, this patch adds an extra opaque AddressSpace field to RemoteAddress, which LLDB can use on its own implementation of MemoryReader to distinguish between addresses. This patch is NFC for the other RemoteInspection clients, as it adds extra information to RemoteAddress, which is entirely optional and if unused should not change the behavior of the library. Although this patch is quite big the changes are largely mechanical, replacing threading StoredPointer with RemoteAddress. rdar://148361743
1 parent 2edec10 commit 58df553

File tree

18 files changed

+979
-718
lines changed

18 files changed

+979
-718
lines changed

include/swift/Basic/RelativePointer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@
132132
#ifndef SWIFT_BASIC_RELATIVEPOINTER_H
133133
#define SWIFT_BASIC_RELATIVEPOINTER_H
134134

135+
#include <cassert>
135136
#include <cstdint>
137+
#include <type_traits>
138+
#include <utility>
136139

137140
namespace swift {
138141

include/swift/Remote/CMemoryReader.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CMemoryReader final : public MemoryReader {
4545
// Check to see if an address has bits outside the ptrauth mask. This suggests
4646
// that we're likely failing to strip a signed pointer when reading from it.
4747
bool hasSignatureBits(RemoteAddress address) {
48-
uint64_t addressData = address.getAddressData();
48+
uint64_t addressData = address.getRawAddress();
4949
uint64_t mask = getPtrAuthMask().value_or(~uint64_t(0));
5050
return addressData != (addressData & mask);
5151
}
@@ -66,13 +66,12 @@ class CMemoryReader final : public MemoryReader {
6666
RemoteAddress getSymbolAddress(const std::string &name) override {
6767
auto addressData = Impl.getSymbolAddress(Impl.reader_context,
6868
name.c_str(), name.size());
69-
return RemoteAddress(addressData);
69+
return RemoteAddress(addressData, RemoteAddress::DefaultAddressSpace);
7070
}
7171

7272
uint64_t getStringLength(RemoteAddress address) {
7373
assert(!hasSignatureBits(address));
74-
return Impl.getStringLength(Impl.reader_context,
75-
address.getAddressData());
74+
return Impl.getStringLength(Impl.reader_context, address.getRawAddress());
7675
}
7776

7877
bool readString(RemoteAddress address, std::string &dest) override {
@@ -97,7 +96,7 @@ class CMemoryReader final : public MemoryReader {
9796
ReadBytesResult readBytes(RemoteAddress address, uint64_t size) override {
9897
assert(!hasSignatureBits(address));
9998
void *FreeContext;
100-
auto Ptr = Impl.readBytes(Impl.reader_context, address.getAddressData(),
99+
auto Ptr = Impl.readBytes(Impl.reader_context, address.getRawAddress(),
101100
size, &FreeContext);
102101

103102
auto Free = Impl.free;
@@ -111,8 +110,7 @@ class CMemoryReader final : public MemoryReader {
111110
return ReadBytesResult(Ptr, freeLambda);
112111
}
113112
};
114-
115-
}
116-
}
113+
} // namespace remote
114+
} // namespace swift
117115

118116
#endif

include/swift/Remote/Failure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class Failure {
288288
case ArgStorageKind::Address: {
289289
result += '0';
290290
result += 'x';
291-
uint64_t address = Args[argIndex].Address.getAddressData();
291+
uint64_t address = Args[argIndex].Address.getRawAddress();
292292
unsigned max = ((address >> 32) != 0 ? 16 : 8);
293293
for (unsigned i = 0; i != max; ++i) {
294294
result += "0123456789abcdef"[(address >> (max - 1 - i) * 4) & 0xF];

include/swift/Remote/InProcessMemoryReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ class InProcessMemoryReader final : public MemoryReader {
105105
return ReadBytesResult(address.getLocalPointer<void>(), [](const void *) {});
106106
}
107107
};
108-
109-
}
110-
}
108+
109+
} // namespace remote
110+
} // namespace swift
111111

112112
#endif

include/swift/Remote/MemoryReader.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,31 @@ class MemoryReader {
128128
///
129129
/// Returns false if the operation failed.
130130
virtual bool readString(RemoteAddress address, std::string &dest) = 0;
131-
131+
132+
/// Attempts to read a remote address from the given address in the remote
133+
/// process.
134+
///
135+
/// Returns false if the operator failed.
136+
template <typename IntegerType>
137+
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;
144+
}
145+
132146
/// Attempts to read an integer from the given address in the remote
133147
/// process.
134148
///
135149
/// Returns false if the operation failed.
136150
template <typename IntegerType>
137151
bool readInteger(RemoteAddress address, IntegerType *dest) {
152+
static_assert(!std::is_same<RemoteAddress, IntegerType>(),
153+
"RemoteAddress cannot be read in directly, use "
154+
"readRemoteAddress instead.");
155+
138156
return readBytes(address, reinterpret_cast<uint8_t*>(dest),
139157
sizeof(IntegerType));
140158
}
@@ -218,7 +236,8 @@ class MemoryReader {
218236
virtual RemoteAbsolutePointer resolvePointer(RemoteAddress address,
219237
uint64_t readValue) {
220238
// Default implementation returns the read value as is.
221-
return RemoteAbsolutePointer(RemoteAddress(readValue));
239+
return RemoteAbsolutePointer(
240+
RemoteAddress(readValue, address.getAddressSpace()));
222241
}
223242

224243
/// Performs the inverse operation of \ref resolvePointer.
@@ -321,7 +340,7 @@ class MemoryReader {
321340
virtual ~MemoryReader() = default;
322341
};
323342

324-
} // end namespace reflection
343+
} // end namespace remote
325344
} // end namespace swift
326345

327346
#endif // SWIFT_REFLECTION_READER_H

0 commit comments

Comments
 (0)