Skip to content

Reflection: try holding 32/64 address offset in target machine pointers. #22899

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 1 commit into from
Feb 27, 2019
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
14 changes: 6 additions & 8 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS32> {
using Section = const struct llvm::ELF::Elf32_Shdr;
using Offset = llvm::ELF::Elf32_Off;
using Size = llvm::ELF::Elf32_Word;
using Address = llvm::ELF::Elf32_Addr;
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS32;
};

Expand All @@ -69,7 +68,6 @@ template <> struct ELFTraits<llvm::ELF::ELFCLASS64> {
using Section = const struct llvm::ELF::Elf64_Shdr;
using Offset = llvm::ELF::Elf64_Off;
using Size = llvm::ELF::Elf64_Xword;
using Address = llvm::ELF::Elf64_Addr;
static constexpr unsigned char ELFClass = llvm::ELF::ELFCLASS64;
};

Expand Down Expand Up @@ -202,7 +200,7 @@ class ReflectionContext
RangeEnd - RangeStart);

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) {
auto S = reinterpret_cast<typename T::Section *>(
SectionsBuf + (I * sizeof(typename T::Section)));
Expand Down Expand Up @@ -233,8 +231,8 @@ class ReflectionContext
ReflStrMdSec.first.first == nullptr)
return false;

auto LocalStartAddress = reinterpret_cast<uintptr_t>(SectBuf.get());
auto RemoteStartAddress = static_cast<uintptr_t>(RangeStart);
auto LocalStartAddress = reinterpret_cast<uint64_t>(SectBuf.get());
auto RemoteStartAddress = static_cast<uint64_t>(RangeStart);

ReflectionInfo info = {
{{FieldMdSec.first.first, FieldMdSec.first.second}, 0},
Expand Down Expand Up @@ -332,7 +330,7 @@ class ReflectionContext
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());

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.
for (const typename T::Section *Hdr : SecHdrVec) {
uint32_t Offset = Hdr->sh_name;
Expand Down Expand Up @@ -367,9 +365,9 @@ class ReflectionContext
ReflStrMdSec.first.first == nullptr)
return false;

auto LocalStartAddress = reinterpret_cast<uintptr_t>(Buf.get());
auto LocalStartAddress = reinterpret_cast<uint64_t>(Buf.get());
auto RemoteStartAddress =
static_cast<uintptr_t>(ImageStart.getAddressData());
static_cast<uint64_t>(ImageStart.getAddressData());

ReflectionInfo info = {
{{FieldMdSec.first.first, FieldMdSec.first.second}, FieldMdSec.second},
Expand Down
20 changes: 10 additions & 10 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,36 @@ using GenericSection = ReflectionSection<const void *>;
struct ReflectionInfo {
struct {
FieldSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} Field;

struct {
AssociatedTypeSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} AssociatedType;

struct {
BuiltinTypeSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} Builtin;

struct {
CaptureSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} Capture;

struct {
GenericSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} TypeReference;

struct {
GenericSection Metadata;
uintptr_t SectionOffset;
uint64_t SectionOffset;
} ReflectionString;

uintptr_t LocalStartAddress;
uintptr_t RemoteStartAddress;
uint64_t LocalStartAddress;
uint64_t RemoteStartAddress;
};

struct ClosureContextInfo {
Expand Down Expand Up @@ -506,11 +506,11 @@ class TypeRefBuilder {

/// Get the raw capture descriptor for a remote capture descriptor
/// address.
const CaptureDescriptor *getCaptureDescriptor(uintptr_t RemoteAddress);
const CaptureDescriptor *getCaptureDescriptor(uint64_t RemoteAddress);

/// Get the unsubstituted capture types for a closure context.
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD,
uintptr_t Offset);
uint64_t Offset);

///
/// Dumping typerefs, field declarations, associated types
Expand Down
18 changes: 9 additions & 9 deletions include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
extern "C" {
#endif

typedef uintptr_t swift_typeref_t;
typedef uint64_t swift_typeref_t;

/// Represents one of the Swift reflection sections of an image.
typedef struct swift_reflection_section {
Expand All @@ -37,37 +37,37 @@ typedef struct swift_reflection_section {
typedef struct swift_reflection_info {
struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} field;

struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} associated_types;

struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} builtin_types;

struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} capture;

struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} type_references;

struct {
swift_reflection_section_t section;
uintptr_t offset;
uint64_t offset;
} reflection_strings;

// Start address in local and remote address spaces.
uintptr_t LocalStartAddress;
uintptr_t RemoteStartAddress;
uint64_t LocalStartAddress;
uint64_t RemoteStartAddress;
} swift_reflection_info_t;

/// The layout kind of a Swift type.
Expand Down
56 changes: 28 additions & 28 deletions stdlib/public/Reflection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {
// Find what type ref section the pointer resides in, if any.
const ReflectionInfo *containingInfo = nullptr;
for (auto &info : ReflectionInfos) {
auto start = (uintptr_t)info.TypeReference.Metadata.startAddress();
auto size = (uintptr_t)info.TypeReference.Metadata.size();
if (start <= (uintptr_t)pointer && (uintptr_t)pointer < start + size) {
auto start = (uint64_t)info.TypeReference.Metadata.startAddress();
auto size = (uint64_t)info.TypeReference.Metadata.size();
if (start <= (uint64_t)pointer && (uint64_t)pointer < start + size) {
containingInfo = &info;
break;
}
}

if (!containingInfo)
return 0;
return (uintptr_t)pointer

return (uint64_t)pointer
+ containingInfo->RemoteStartAddress
- containingInfo->LocalStartAddress
+ containingInfo->TypeReference.SectionOffset;
Expand Down Expand Up @@ -86,10 +86,10 @@ lookupTypeWitness(const std::string &MangledTypeName,
// Cache missed - we need to look through all of the assocty sections
// for all images that we've been notified about.
for (auto &Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.AssociatedType.SectionOffset
- Info.TypeReference.SectionOffset;
uintptr_t NameOffset = Info.AssociatedType.SectionOffset
- Info.ReflectionString.SectionOffset;
uint64_t TypeRefOffset = Info.AssociatedType.SectionOffset
- Info.TypeReference.SectionOffset;
uint64_t NameOffset = Info.AssociatedType.SectionOffset
- Info.ReflectionString.SectionOffset;
for (const auto &AssocTyDescriptor : Info.AssociatedType.Metadata) {
if (!reflectionNameMatches(Dem,
AssocTyDescriptor.getMangledConformingTypeName(TypeRefOffset),
Expand Down Expand Up @@ -158,8 +158,8 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
// On failure, fill out the cache with everything we know about.
std::vector<std::pair<std::string, const TypeRef *>> Fields;
for (auto &Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.Field.SectionOffset
- Info.TypeReference.SectionOffset;
uint64_t TypeRefOffset = Info.Field.SectionOffset
- Info.TypeReference.SectionOffset;
for (auto &FD : Info.Field.Metadata) {
if (!FD.hasMangledTypeName())
continue;
Expand Down Expand Up @@ -232,8 +232,8 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
return nullptr;

for (auto Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.Builtin.SectionOffset
- Info.TypeReference.SectionOffset;
uint64_t TypeRefOffset = Info.Builtin.SectionOffset
- Info.TypeReference.SectionOffset;
for (auto &BuiltinTypeDescriptor : Info.Builtin.Metadata) {
assert(BuiltinTypeDescriptor.Size > 0);
assert(BuiltinTypeDescriptor.getAlignment() > 0);
Expand All @@ -252,10 +252,10 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
}

const CaptureDescriptor *
TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
TypeRefBuilder::getCaptureDescriptor(uint64_t RemoteAddress) {
for (auto Info : ReflectionInfos) {
for (auto &CD : Info.Capture.Metadata) {
auto OtherAddr = (reinterpret_cast<uintptr_t>(&CD) -
auto OtherAddr = (reinterpret_cast<uint64_t>(&CD) -
Info.LocalStartAddress + Info.RemoteStartAddress);
if (OtherAddr == RemoteAddress)
return &CD;
Expand All @@ -268,7 +268,7 @@ TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
/// Get the unsubstituted capture types for a closure context.
ClosureContextInfo
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD,
uintptr_t TypeRefOffset) {
uint64_t TypeRefOffset) {
ClosureContextInfo Info;

for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
Expand Down Expand Up @@ -326,10 +326,10 @@ TypeRefBuilder::dumpTypeRef(StringRef MangledName,

void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Field.SectionOffset
- sections.TypeReference.SectionOffset;
uintptr_t NameOffset = sections.Field.SectionOffset
- sections.ReflectionString.SectionOffset;
uint64_t TypeRefOffset = sections.Field.SectionOffset
- sections.TypeReference.SectionOffset;
uint64_t NameOffset = sections.Field.SectionOffset
- sections.ReflectionString.SectionOffset;
for (const auto &descriptor : sections.Field.Metadata) {
auto TypeDemangling = Dem.demangleType(
dropSwiftManglingPrefix(descriptor.getMangledTypeName(TypeRefOffset)));
Expand All @@ -354,10 +354,10 @@ void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {

void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.AssociatedType.SectionOffset
- sections.TypeReference.SectionOffset;
uintptr_t NameOffset = sections.AssociatedType.SectionOffset
- sections.ReflectionString.SectionOffset;
uint64_t TypeRefOffset = sections.AssociatedType.SectionOffset
- sections.TypeReference.SectionOffset;
uint64_t NameOffset = sections.AssociatedType.SectionOffset
- sections.ReflectionString.SectionOffset;
for (const auto &descriptor : sections.AssociatedType.Metadata) {
auto conformingTypeNode = Dem.demangleType(
descriptor.getMangledConformingTypeName(TypeRefOffset));
Expand All @@ -381,8 +381,8 @@ void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {

void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Builtin.SectionOffset
- sections.TypeReference.SectionOffset;
uint64_t TypeRefOffset = sections.Builtin.SectionOffset
- sections.TypeReference.SectionOffset;
for (const auto &descriptor : sections.Builtin.Metadata) {
auto typeName =
Demangle::demangleTypeAsString(
Expand Down Expand Up @@ -426,8 +426,8 @@ void ClosureContextInfo::dump(std::ostream &OS) const {

void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) {
for (const auto &sections : ReflectionInfos) {
uintptr_t TypeRefOffset = sections.Capture.SectionOffset
- sections.TypeReference.SectionOffset;
uint64_t TypeRefOffset = sections.Capture.SectionOffset
- sections.TypeReference.SectionOffset;
for (const auto &descriptor : sections.Capture.Metadata) {
auto info = getClosureContextInfo(descriptor, TypeRefOffset);
info.dump(OS);
Expand Down