Skip to content

[NFC][RemoteInspection] Add an opaque AddressSpace field to RemoteAddress #82997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/RelativePointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@
#ifndef SWIFT_BASIC_RELATIVEPOINTER_H
#define SWIFT_BASIC_RELATIVEPOINTER_H

#include <cassert>
#include <cstdint>
#include <type_traits>
#include <utility>

namespace swift {

Expand Down
14 changes: 6 additions & 8 deletions include/swift/Remote/CMemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CMemoryReader final : public MemoryReader {
// that we're likely failing to strip a signed pointer when reading from it.
bool hasSignatureBits(RemoteAddress address) {
return false;
uint64_t addressData = address.getAddressData();
uint64_t addressData = address.getRawAddress();
return addressData != (addressData & getPtrauthMask());
}

Expand All @@ -89,13 +89,12 @@ class CMemoryReader final : public MemoryReader {
RemoteAddress getSymbolAddress(const std::string &name) override {
auto addressData = Impl.getSymbolAddress(Impl.reader_context,
name.c_str(), name.size());
return RemoteAddress(addressData);
return RemoteAddress(addressData, RemoteAddress::DefaultAddressSpace);
}

uint64_t getStringLength(RemoteAddress address) {
assert(!hasSignatureBits(address));
return Impl.getStringLength(Impl.reader_context,
address.getAddressData());
return Impl.getStringLength(Impl.reader_context, address.getRawAddress());
}

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

auto Free = Impl.free;
Expand All @@ -134,8 +133,7 @@ class CMemoryReader final : public MemoryReader {
return ReadBytesResult(Ptr, freeLambda);
}
};

}
}
} // namespace remote
} // namespace swift

#endif
2 changes: 1 addition & 1 deletion include/swift/Remote/Failure.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Failure {
case ArgStorageKind::Address: {
result += '0';
result += 'x';
uint64_t address = Args[argIndex].Address.getAddressData();
uint64_t address = Args[argIndex].Address.getRawAddress();
unsigned max = ((address >> 32) != 0 ? 16 : 8);
for (unsigned i = 0; i != max; ++i) {
result += "0123456789abcdef"[(address >> (max - 1 - i) * 4) & 0xF];
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Remote/InProcessMemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class InProcessMemoryReader final : public MemoryReader {
return ReadBytesResult(address.getLocalPointer<void>(), [](const void *) {});
}
};
}
}

} // namespace remote
} // namespace swift

#endif
27 changes: 23 additions & 4 deletions include/swift/Remote/MemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,31 @@ class MemoryReader {
///
/// Returns false if the operation failed.
virtual bool readString(RemoteAddress address, std::string &dest) = 0;


/// Attempts to read a remote address from the given address in the remote
/// process.
///
/// Returns false if the operator failed.
template <typename IntegerType>
bool readRemoteAddress(RemoteAddress address, RemoteAddress &out) {
IntegerType buf;
if (!readInteger(address, &buf))
return false;

out = RemoteAddress((uint64_t)buf, address.getAddressSpace());
return true;
}

/// Attempts to read an integer from the given address in the remote
/// process.
///
/// Returns false if the operation failed.
template <typename IntegerType>
bool readInteger(RemoteAddress address, IntegerType *dest) {
static_assert(!std::is_same<RemoteAddress, IntegerType>(),
"RemoteAddress cannot be read in directly, use "
"readRemoteAddress instead.");

return readBytes(address, reinterpret_cast<uint8_t*>(dest),
sizeof(IntegerType));
}
Expand Down Expand Up @@ -147,7 +165,8 @@ class MemoryReader {
virtual RemoteAbsolutePointer resolvePointer(RemoteAddress address,
uint64_t readValue) {
// Default implementation returns the read value as is.
return RemoteAbsolutePointer("", readValue);
return RemoteAbsolutePointer(
RemoteAddress(readValue, address.getAddressSpace()));
}

/// Performs the inverse operation of \ref resolvePointer.
Expand All @@ -166,7 +185,7 @@ class MemoryReader {
virtual RemoteAbsolutePointer getSymbol(RemoteAddress address) {
if (auto symbol = resolvePointerAsSymbol(address))
return *symbol;
return RemoteAbsolutePointer("", address.getAddressData());
return RemoteAbsolutePointer(address);
}

/// Lookup a dynamic symbol name (ie dynamic loader binding) for the given
Expand Down Expand Up @@ -263,7 +282,7 @@ class MemoryReader {
virtual ~MemoryReader() = default;
};

} // end namespace reflection
} // end namespace remote
} // end namespace swift

#endif // SWIFT_REFLECTION_READER_H
Expand Down
Loading