Skip to content

Commit 841737b

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 (cherry picked from commit 58df553)
1 parent a6eafcb commit 841737b

File tree

18 files changed

+971
-710
lines changed

18 files changed

+971
-710
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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ class CMemoryReader final : public MemoryReader {
8989
RemoteAddress getSymbolAddress(const std::string &name) override {
9090
auto addressData = Impl.getSymbolAddress(Impl.reader_context,
9191
name.c_str(), name.size());
92-
return RemoteAddress(addressData);
92+
return RemoteAddress(addressData, RemoteAddress::DefaultAddressSpace);
9393
}
9494

9595
uint64_t getStringLength(RemoteAddress address) {
9696
assert(!hasSignatureBits(address));
97-
return Impl.getStringLength(Impl.reader_context,
98-
address.getAddressData());
97+
return Impl.getStringLength(Impl.reader_context, address.getRawAddress());
9998
}
10099

101100
bool readString(RemoteAddress address, std::string &dest) override {
@@ -120,7 +119,7 @@ class CMemoryReader final : public MemoryReader {
120119
ReadBytesResult readBytes(RemoteAddress address, uint64_t size) override {
121120
assert(!hasSignatureBits(address));
122121
void *FreeContext;
123-
auto Ptr = Impl.readBytes(Impl.reader_context, address.getAddressData(),
122+
auto Ptr = Impl.readBytes(Impl.reader_context, address.getRawAddress(),
124123
size, &FreeContext);
125124

126125
auto Free = Impl.free;
@@ -134,8 +133,7 @@ class CMemoryReader final : public MemoryReader {
134133
return ReadBytesResult(Ptr, freeLambda);
135134
}
136135
};
137-
138-
}
139-
}
136+
} // namespace remote
137+
} // namespace swift
140138

141139
#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
@@ -57,13 +57,31 @@ class MemoryReader {
5757
///
5858
/// Returns false if the operation failed.
5959
virtual bool readString(RemoteAddress address, std::string &dest) = 0;
60-
60+
61+
/// Attempts to read a remote address from the given address in the remote
62+
/// process.
63+
///
64+
/// Returns false if the operator failed.
65+
template <typename IntegerType>
66+
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;
73+
}
74+
6175
/// Attempts to read an integer from the given address in the remote
6276
/// process.
6377
///
6478
/// Returns false if the operation failed.
6579
template <typename IntegerType>
6680
bool readInteger(RemoteAddress address, IntegerType *dest) {
81+
static_assert(!std::is_same<RemoteAddress, IntegerType>(),
82+
"RemoteAddress cannot be read in directly, use "
83+
"readRemoteAddress instead.");
84+
6785
return readBytes(address, reinterpret_cast<uint8_t*>(dest),
6886
sizeof(IntegerType));
6987
}
@@ -147,7 +165,8 @@ class MemoryReader {
147165
virtual RemoteAbsolutePointer resolvePointer(RemoteAddress address,
148166
uint64_t readValue) {
149167
// Default implementation returns the read value as is.
150-
return RemoteAbsolutePointer(RemoteAddress(readValue));
168+
return RemoteAbsolutePointer(
169+
RemoteAddress(readValue, address.getAddressSpace()));
151170
}
152171

153172
/// Performs the inverse operation of \ref resolvePointer.
@@ -263,7 +282,7 @@ class MemoryReader {
263282
virtual ~MemoryReader() = default;
264283
};
265284

266-
} // end namespace reflection
285+
} // end namespace remote
267286
} // end namespace swift
268287

269288
#endif // SWIFT_REFLECTION_READER_H

0 commit comments

Comments
 (0)