Skip to content

[lldb] Move resolvePointer code to resolvePointerAsSymbol and implement resolvePointer to map process addresses to tagged addressess #3882

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
83 changes: 72 additions & 11 deletions lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,22 @@ LLDBMemoryReader::getSymbolAddress(const std::string &name) {
return swift::remote::RemoteAddress(load_addr);
}

swift::remote::RemoteAbsolutePointer
LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
uint64_t readValue) {
llvm::Optional<swift::remote::RemoteAbsolutePointer>
LLDBMemoryReader::resolvePointerAsSymbol(swift::remote::RemoteAddress address) {
// If an address has a symbol, that symbol provides additional useful data to
// MetadataReader. Without the symbol, MetadataReader can derive the symbol
// by loading other parts of reflection metadata, but that work has a cost.
// For lldb, that data loading can be a significant performance hit. Providing
// a symbol greatly reduces memory read traffic to the process.
auto pointer = swift::remote::RemoteAbsolutePointer("", readValue);

auto &target = m_process.GetTarget();
if (!target.GetSwiftUseReflectionSymbols())
return pointer;
return {};

llvm::Optional<Address> maybeAddr =
resolveRemoteAddress(address.getAddressData());
// This is not an assert, but should never happen.
if (!maybeAddr)
return pointer;
return {};

Address addr;
if (maybeAddr->IsSectionOffset()) {
Expand All @@ -146,21 +143,85 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
} else {
// `address` is a real load address.
if (!target.ResolveLoadAddress(address.getAddressData(), addr))
return pointer;
return {};
}

if (!addr.GetSection()->CanContainSwiftReflectionData())
return pointer;
return {};

if (auto *symbol = addr.CalculateSymbolContextSymbol()) {
auto mangledName = symbol->GetMangled().GetMangledName().GetStringRef();
// MemoryReader requires this to be a Swift symbol. LLDB can also be
// aware of local symbols, so avoid returning those.
if (swift::Demangle::isSwiftSymbol(mangledName))
return {mangledName, 0};
return {{mangledName, 0}};
}

return {};
}

swift::remote::RemoteAbsolutePointer
LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,
uint64_t readValue) {
Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));

// We may have gotten a pointer to a process address, try to map it back
// to a tagged address so further memory reads originating from it benefit
// from the file-cache optimization.
swift::remote::RemoteAbsolutePointer process_pointer("", readValue);

auto &target = m_process.GetTarget();
Address addr;
if (!target.ResolveLoadAddress(readValue, addr)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for an instance pointer pointing into the heap or stack, this is expected to fail, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens with a pointer to global variable that would be in a non-const __DATA section?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e, should there be a check that the address is in a read-only section?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't be necessary, because we only read from the file-cache if the section containing the address is read-only.

LLDB_LOGV(log,
"[MemoryReader] Could not resolve load address of pointer {0:x} "
"read from {1:x}.",
readValue, address.getAddressData());
return process_pointer;
}

auto module_containing_pointer = addr.GetSection()->GetModule();

// Check if the module containing the pointer is registered with
// LLDBMemoryReader.
auto pair_iterator = std::find_if(
m_range_module_map.begin(), m_range_module_map.end(), [&](auto pair) {
return std::get<ModuleSP>(pair) == module_containing_pointer;
});

// We haven't registered the image that contains the pointer.
if (pair_iterator == m_range_module_map.end()) {
LLDB_LOG(log,
"[MemoryReader] Could not resolve find module containing pointer "
"{0:x} read from {1:x}.",
readValue, address.getAddressData());
return process_pointer;
}

// If the containing image is the first registered one, the image's tagged
// start address for it is the first tagged address. Otherwise, the previous
// pair's address is the start tagged address.
uint64_t start_tagged_address = pair_iterator == m_range_module_map.begin()
? LLDB_FILE_ADDRESS_BIT
: std::prev(pair_iterator)->first;

uint64_t tagged_address = start_tagged_address + addr.GetFileAddress();

if (tagged_address >= std::get<uint64_t>(*pair_iterator)) {
// If the tagged address invades the next image's tagged address space,
// something went wrong. Log it and just return the process address.
LLDB_LOG(log,
"[MemoryReader] Pointer {0:x} read from {1:x} resolved to tagged "
"address {2:x}, which is outside its image address space.",
readValue, address.getAddressData(), tagged_address);
return process_pointer;
}

return pointer;
LLDB_LOGV(log,
"[MemoryReader] Successfully resolved pointer {0:x} read from "
"{1:x} to tagged address {2:x}.",
readValue, address.getAddressData(), tagged_address);
return swift::remote::RemoteAbsolutePointer("", tagged_address);
}

bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
swift::remote::RemoteAddress
getSymbolAddress(const std::string &name) override;

llvm::Optional<swift::remote::RemoteAbsolutePointer>
resolvePointerAsSymbol(swift::remote::RemoteAddress address) override;

swift::remote::RemoteAbsolutePointer
resolvePointer(swift::remote::RemoteAddress address,
uint64_t readValue) override;
Expand Down