Merge pull request #22899 from drodriguez/reflection-context-fix-2

Reflection: try holding 32/64 address offset in target machine pointers.
This commit is contained in:
Daniel Rodríguez Troitiño
2019-02-27 11:34:36 -08:00
committed by GitHub
4 changed files with 53 additions and 55 deletions

View File

@@ -62,7 +62,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS32> {
using Section = const struct llvm::ELF::Elf32_Shdr; using Section = const struct llvm::ELF::Elf32_Shdr;
using Offset = llvm::ELF::Elf32_Off; using Offset = llvm::ELF::Elf32_Off;
using Size = llvm::ELF::Elf32_Word; using Size = llvm::ELF::Elf32_Word;
using Address = llvm::ELF::Elf32_Addr;
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS32; static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS32;
}; };
@@ -71,7 +70,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS64> {
using Section = const struct llvm::ELF::Elf64_Shdr; using Section = const struct llvm::ELF::Elf64_Shdr;
using Offset = llvm::ELF::Elf64_Off; using Offset = llvm::ELF::Elf64_Off;
using Size = llvm::ELF::Elf64_Xword; using Size = llvm::ELF::Elf64_Xword;
using Address = llvm::ELF::Elf64_Addr;
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS64; static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS64;
}; };
@@ -204,7 +202,7 @@ public:
RangeEnd - RangeStart); RangeEnd - RangeStart);
auto findMachOSectionByName = [&](std::string Name) auto findMachOSectionByName = [&](std::string Name)
-> std::pair<std::pair<const char *, const char *>, uint32_t> { -> std::pair<std::pair<const char *, const char *>, uint64_t> {
for (unsigned I = 0; I < NumSect; ++I) { for (unsigned I = 0; I < NumSect; ++I) {
auto S = reinterpret_cast<typename T::Section *>( auto S = reinterpret_cast<typename T::Section *>(
SectionsBuf + (I * sizeof(typename T::Section))); SectionsBuf + (I * sizeof(typename T::Section)));
@@ -235,8 +233,8 @@ public:
ReflStrMdSec.first.first == nullptr) ReflStrMdSec.first.first == nullptr)
return false; return false;
auto LocalStartAddress = reinterpret_cast<uintptr_t>(SectBuf.get()); auto LocalStartAddress = reinterpret_cast<uint64_t>(SectBuf.get());
auto RemoteStartAddress = static_cast<uintptr_t>(RangeStart); auto RemoteStartAddress = static_cast<uint64_t>(RangeStart);
ReflectionInfo info = { ReflectionInfo info = {
{{FieldMdSec.first.first, FieldMdSec.first.second}, 0}, {{FieldMdSec.first.first, FieldMdSec.first.second}, 0},
@@ -428,7 +426,7 @@ public:
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get()); auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());
auto findELFSectionByName = [&](std::string Name) auto findELFSectionByName = [&](std::string Name)
-> std::pair<std::pair<const char *, const char *>, typename T::Address> { -> std::pair<std::pair<const char *, const char *>, uint64_t> {
// Now for all the sections, find their name. // Now for all the sections, find their name.
for (const typename T::Section *Hdr : SecHdrVec) { for (const typename T::Section *Hdr : SecHdrVec) {
uint32_t Offset = Hdr->sh_name; uint32_t Offset = Hdr->sh_name;
@@ -463,9 +461,9 @@ public:
ReflStrMdSec.first.first == nullptr) ReflStrMdSec.first.first == nullptr)
return false; return false;
auto LocalStartAddress = reinterpret_cast<uintptr_t>(Buf.get()); auto LocalStartAddress = reinterpret_cast<uint64_t>(Buf.get());
auto RemoteStartAddress = auto RemoteStartAddress =
static_cast<uintptr_t>(ImageStart.getAddressData()); static_cast<uint64_t>(ImageStart.getAddressData());
ReflectionInfo info = { ReflectionInfo info = {
{{FieldMdSec.first.first, FieldMdSec.first.second}, FieldMdSec.second}, {{FieldMdSec.first.first, FieldMdSec.first.second}, FieldMdSec.second},

View File

@@ -82,36 +82,36 @@ using GenericSection = ReflectionSection<const void *>;
struct ReflectionInfo { struct ReflectionInfo {
struct { struct {
FieldSection Metadata; FieldSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} Field; } Field;
struct { struct {
AssociatedTypeSection Metadata; AssociatedTypeSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} AssociatedType; } AssociatedType;
struct { struct {
BuiltinTypeSection Metadata; BuiltinTypeSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} Builtin; } Builtin;
struct { struct {
CaptureSection Metadata; CaptureSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} Capture; } Capture;
struct { struct {
GenericSection Metadata; GenericSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} TypeReference; } TypeReference;
struct { struct {
GenericSection Metadata; GenericSection Metadata;
uintptr_t SectionOffset; uint64_t SectionOffset;
} ReflectionString; } ReflectionString;
uintptr_t LocalStartAddress; uint64_t LocalStartAddress;
uintptr_t RemoteStartAddress; uint64_t RemoteStartAddress;
}; };
struct ClosureContextInfo { struct ClosureContextInfo {
@@ -506,11 +506,11 @@ public:
/// Get the raw capture descriptor for a remote capture descriptor /// Get the raw capture descriptor for a remote capture descriptor
/// address. /// address.
const CaptureDescriptor *getCaptureDescriptor(uintptr_t RemoteAddress); const CaptureDescriptor *getCaptureDescriptor(uint64_t RemoteAddress);
/// Get the unsubstituted capture types for a closure context. /// Get the unsubstituted capture types for a closure context.
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD, ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD,
uintptr_t Offset); uint64_t Offset);
/// ///
/// Dumping typerefs, field declarations, associated types /// Dumping typerefs, field declarations, associated types

View File

@@ -24,7 +24,7 @@
extern "C" { extern "C" {
#endif #endif
typedef uintptr_t swift_typeref_t; typedef uint64_t swift_typeref_t;
/// Represents one of the Swift reflection sections of an image. /// Represents one of the Swift reflection sections of an image.
typedef struct swift_reflection_section { typedef struct swift_reflection_section {
@@ -37,37 +37,37 @@ typedef struct swift_reflection_section {
typedef struct swift_reflection_info { typedef struct swift_reflection_info {
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} field; } field;
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} associated_types; } associated_types;
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} builtin_types; } builtin_types;
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} capture; } capture;
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} type_references; } type_references;
struct { struct {
swift_reflection_section_t section; swift_reflection_section_t section;
uintptr_t offset; uint64_t offset;
} reflection_strings; } reflection_strings;
// Start address in local and remote address spaces. // Start address in local and remote address spaces.
uintptr_t LocalStartAddress; uint64_t LocalStartAddress;
uintptr_t RemoteStartAddress; uint64_t RemoteStartAddress;
} swift_reflection_info_t; } swift_reflection_info_t;
/// The layout kind of a Swift type. /// The layout kind of a Swift type.

View File

@@ -31,9 +31,9 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {
// Find what type ref section the pointer resides in, if any. // Find what type ref section the pointer resides in, if any.
const ReflectionInfo *containingInfo = nullptr; const ReflectionInfo *containingInfo = nullptr;
for (auto &info : ReflectionInfos) { for (auto &info : ReflectionInfos) {
auto start = (uintptr_t)info.TypeReference.Metadata.startAddress(); auto start = (uint64_t)info.TypeReference.Metadata.startAddress();
auto size = (uintptr_t)info.TypeReference.Metadata.size(); auto size = (uint64_t)info.TypeReference.Metadata.size();
if (start <= (uintptr_t)pointer && (uintptr_t)pointer < start + size) { if (start <= (uint64_t)pointer && (uint64_t)pointer < start + size) {
containingInfo = &info; containingInfo = &info;
break; break;
} }
@@ -42,7 +42,7 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {
if (!containingInfo) if (!containingInfo)
return 0; return 0;
return (uintptr_t)pointer return (uint64_t)pointer
+ containingInfo->RemoteStartAddress + containingInfo->RemoteStartAddress
- containingInfo->LocalStartAddress - containingInfo->LocalStartAddress
+ containingInfo->TypeReference.SectionOffset; + containingInfo->TypeReference.SectionOffset;
@@ -86,9 +86,9 @@ lookupTypeWitness(const std::string &MangledTypeName,
// Cache missed - we need to look through all of the assocty sections // Cache missed - we need to look through all of the assocty sections
// for all images that we've been notified about. // for all images that we've been notified about.
for (auto &Info : ReflectionInfos) { for (auto &Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.AssociatedType.SectionOffset uint64_t TypeRefOffset = Info.AssociatedType.SectionOffset
- Info.TypeReference.SectionOffset; - Info.TypeReference.SectionOffset;
uintptr_t NameOffset = Info.AssociatedType.SectionOffset uint64_t NameOffset = Info.AssociatedType.SectionOffset
- Info.ReflectionString.SectionOffset; - Info.ReflectionString.SectionOffset;
for (const auto &AssocTyDescriptor : Info.AssociatedType.Metadata) { for (const auto &AssocTyDescriptor : Info.AssociatedType.Metadata) {
if (!reflectionNameMatches(Dem, if (!reflectionNameMatches(Dem,
@@ -158,7 +158,7 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
// On failure, fill out the cache with everything we know about. // On failure, fill out the cache with everything we know about.
std::vector<std::pair<std::string, const TypeRef *>> Fields; std::vector<std::pair<std::string, const TypeRef *>> Fields;
for (auto &Info : ReflectionInfos) { for (auto &Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.Field.SectionOffset uint64_t TypeRefOffset = Info.Field.SectionOffset
- Info.TypeReference.SectionOffset; - Info.TypeReference.SectionOffset;
for (auto &FD : Info.Field.Metadata) { for (auto &FD : Info.Field.Metadata) {
if (!FD.hasMangledTypeName()) if (!FD.hasMangledTypeName())
@@ -232,7 +232,7 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
return nullptr; return nullptr;
for (auto Info : ReflectionInfos) { for (auto Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.Builtin.SectionOffset uint64_t TypeRefOffset = Info.Builtin.SectionOffset
- Info.TypeReference.SectionOffset; - Info.TypeReference.SectionOffset;
for (auto &BuiltinTypeDescriptor : Info.Builtin.Metadata) { for (auto &BuiltinTypeDescriptor : Info.Builtin.Metadata) {
assert(BuiltinTypeDescriptor.Size > 0); assert(BuiltinTypeDescriptor.Size > 0);
@@ -252,10 +252,10 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
} }
const CaptureDescriptor * const CaptureDescriptor *
TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) { TypeRefBuilder::getCaptureDescriptor(uint64_t RemoteAddress) {
for (auto Info : ReflectionInfos) { for (auto Info : ReflectionInfos) {
for (auto &CD : Info.Capture.Metadata) { for (auto &CD : Info.Capture.Metadata) {
auto OtherAddr = (reinterpret_cast<uintptr_t>(&CD) - auto OtherAddr = (reinterpret_cast<uint64_t>(&CD) -
Info.LocalStartAddress + Info.RemoteStartAddress); Info.LocalStartAddress + Info.RemoteStartAddress);
if (OtherAddr == RemoteAddress) if (OtherAddr == RemoteAddress)
return &CD; return &CD;
@@ -268,7 +268,7 @@ TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
/// Get the unsubstituted capture types for a closure context. /// Get the unsubstituted capture types for a closure context.
ClosureContextInfo ClosureContextInfo
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD, TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD,
uintptr_t TypeRefOffset) { uint64_t TypeRefOffset) {
ClosureContextInfo Info; ClosureContextInfo Info;
for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) { for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
@@ -326,9 +326,9 @@ TypeRefBuilder::dumpTypeRef(StringRef MangledName,
void TypeRefBuilder::dumpFieldSection(std::ostream &OS) { void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) { for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Field.SectionOffset uint64_t TypeRefOffset = sections.Field.SectionOffset
- sections.TypeReference.SectionOffset; - sections.TypeReference.SectionOffset;
uintptr_t NameOffset = sections.Field.SectionOffset uint64_t NameOffset = sections.Field.SectionOffset
- sections.ReflectionString.SectionOffset; - sections.ReflectionString.SectionOffset;
for (const auto &descriptor : sections.Field.Metadata) { for (const auto &descriptor : sections.Field.Metadata) {
auto TypeDemangling = Dem.demangleType( auto TypeDemangling = Dem.demangleType(
@@ -354,9 +354,9 @@ void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) { void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) { for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.AssociatedType.SectionOffset uint64_t TypeRefOffset = sections.AssociatedType.SectionOffset
- sections.TypeReference.SectionOffset; - sections.TypeReference.SectionOffset;
uintptr_t NameOffset = sections.AssociatedType.SectionOffset uint64_t NameOffset = sections.AssociatedType.SectionOffset
- sections.ReflectionString.SectionOffset; - sections.ReflectionString.SectionOffset;
for (const auto &descriptor : sections.AssociatedType.Metadata) { for (const auto &descriptor : sections.AssociatedType.Metadata) {
auto conformingTypeNode = Dem.demangleType( auto conformingTypeNode = Dem.demangleType(
@@ -381,7 +381,7 @@ void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) { void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) { for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Builtin.SectionOffset uint64_t TypeRefOffset = sections.Builtin.SectionOffset
- sections.TypeReference.SectionOffset; - sections.TypeReference.SectionOffset;
for (const auto &descriptor : sections.Builtin.Metadata) { for (const auto &descriptor : sections.Builtin.Metadata) {
auto typeName = auto typeName =
@@ -426,7 +426,7 @@ void ClosureContextInfo::dump(std::ostream &OS) const {
void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) { void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) { for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Capture.SectionOffset uint64_t TypeRefOffset = sections.Capture.SectionOffset
- sections.TypeReference.SectionOffset; - sections.TypeReference.SectionOffset;
for (const auto &descriptor : sections.Capture.Metadata) { for (const auto &descriptor : sections.Capture.Metadata) {
auto info = getClosureContextInfo(descriptor, TypeRefOffset); auto info = getClosureContextInfo(descriptor, TypeRefOffset);