Skip to content

Reflection: Traffic in RemoteRefs. #27369

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
Sep 26, 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
157 changes: 10 additions & 147 deletions include/swift/Reflection/Records.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ class FieldRecordFlags {

class FieldRecord {
const FieldRecordFlags Flags;

public:
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> FieldName;

public:
FieldRecord() = delete;

bool hasMangledTypeName() const {
Expand All @@ -84,11 +85,8 @@ class FieldRecord {
return Demangle::makeSymbolicMangledNameStringRef(MangledTypeName.get());
}

StringRef getFieldName(uintptr_t Low, uintptr_t High) const {
uintptr_t nameAddr = (uintptr_t)FieldName.get();
if (nameAddr < Low || nameAddr > High)
return "";
return (const char *)nameAddr;
StringRef getFieldName() const {
return FieldName.get();
}

bool isIndirectCase() const {
Expand Down Expand Up @@ -162,10 +160,10 @@ class FieldDescriptor {
return reinterpret_cast<const FieldRecord *>(this + 1);
}

public:
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> Superclass;

public:
FieldDescriptor() = delete;

const FieldDescriptorKind Kind;
Expand Down Expand Up @@ -227,46 +225,13 @@ class FieldDescriptor {
}
};

class FieldDescriptorIterator
: public std::iterator<std::forward_iterator_tag, FieldDescriptor> {
public:
const void *Cur;
const void * const End;
FieldDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}

const FieldDescriptor &operator*() const {
return *reinterpret_cast<const FieldDescriptor *>(Cur);
}

const FieldDescriptor *operator->() const {
return reinterpret_cast<const FieldDescriptor *>(Cur);
}

FieldDescriptorIterator &operator++() {
const auto &FR = this->operator*();
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(FieldDescriptor) + FR.NumFields * FR.FieldRecordSize;
Cur = Next;
return *this;
}

bool operator==(FieldDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}

bool operator!=(FieldDescriptorIterator const &other) const {
return !(*this == other);
}
};

// Associated type records describe the mapping from an associated
// type to the type witness of a conformance.
class AssociatedTypeRecord {
public:
const RelativeDirectPointer<const char> Name;
const RelativeDirectPointer<const char> SubstitutedTypeName;

public:
StringRef getName() const {
return Name.get();
}
Expand Down Expand Up @@ -322,10 +287,9 @@ struct AssociatedTypeRecordIterator {
// An associated type descriptor contains a collection of associated
// type records for a conformance.
struct AssociatedTypeDescriptor {
private:
public:
const RelativeDirectPointer<const char> ConformingTypeName;
const RelativeDirectPointer<const char> ProtocolTypeName;
public:

uint32_t NumAssociatedTypes;
uint32_t AssociatedTypeRecordSize;
Expand Down Expand Up @@ -357,46 +321,12 @@ struct AssociatedTypeDescriptor {
}
};

class AssociatedTypeIterator
: public std::iterator<std::forward_iterator_tag, AssociatedTypeDescriptor> {
public:
const void *Cur;
const void * const End;
AssociatedTypeIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}

const AssociatedTypeDescriptor &operator*() const {
return *reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
}

const AssociatedTypeDescriptor *operator->() const {
return reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
}

AssociatedTypeIterator &operator++() {
const auto &ATR = this->operator*();
size_t Size = sizeof(AssociatedTypeDescriptor) +
ATR.NumAssociatedTypes * ATR.AssociatedTypeRecordSize;
const void *Next = reinterpret_cast<const char *>(Cur) + Size;
Cur = Next;
return *this;
}

bool operator==(AssociatedTypeIterator const &other) const {
return Cur == other.Cur && End == other.End;
}

bool operator!=(AssociatedTypeIterator const &other) const {
return !(*this == other);
}
};

// Builtin type records describe basic layout information about
// any builtin types referenced from the other sections.
class BuiltinTypeDescriptor {
public:
const RelativeDirectPointer<const char> TypeName;

public:
uint32_t Size;

// - Least significant 16 bits are the alignment.
Expand Down Expand Up @@ -424,42 +354,10 @@ class BuiltinTypeDescriptor {
}
};

class BuiltinTypeDescriptorIterator
: public std::iterator<std::forward_iterator_tag, BuiltinTypeDescriptor> {
public:
const void *Cur;
const void * const End;
BuiltinTypeDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}

const BuiltinTypeDescriptor &operator*() const {
return *reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);
}

const BuiltinTypeDescriptor *operator->() const {
return reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);;
}

BuiltinTypeDescriptorIterator &operator++() {
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(BuiltinTypeDescriptor);
Cur = Next;
return *this;
}

bool operator==(BuiltinTypeDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}

bool operator!=(BuiltinTypeDescriptorIterator const &other) const {
return !(*this == other);
}
};

class CaptureTypeRecord {
public:
const RelativeDirectPointer<const char> MangledTypeName;

public:
CaptureTypeRecord() = delete;

bool hasMangledTypeName() const {
Expand Down Expand Up @@ -502,10 +400,10 @@ struct CaptureTypeRecordIterator {
};

class MetadataSourceRecord {
public:
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> MangledMetadataSource;

public:
MetadataSourceRecord() = delete;

bool hasMangledTypeName() const {
Expand Down Expand Up @@ -608,41 +506,6 @@ class CaptureDescriptor {
}
};

class CaptureDescriptorIterator
: public std::iterator<std::forward_iterator_tag, CaptureDescriptor> {
public:
const void *Cur;
const void * const End;
CaptureDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}

const CaptureDescriptor &operator*() const {
return *reinterpret_cast<const CaptureDescriptor *>(Cur);
}

const CaptureDescriptor *operator->() const {
return reinterpret_cast<const CaptureDescriptor *>(Cur);
}

CaptureDescriptorIterator &operator++() {
const auto &CR = this->operator*();
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(CaptureDescriptor)
+ CR.NumCaptureTypes * sizeof(CaptureTypeRecord)
+ CR.NumMetadataSources * sizeof(MetadataSourceRecord);
Cur = Next;
return *this;
}

bool operator==(CaptureDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}

bool operator!=(CaptureDescriptorIterator const &other) const {
return !(*this == other);
}
};

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

Expand Down
57 changes: 28 additions & 29 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class ReflectionContext
RangeEnd - RangeStart);

auto findMachOSectionByName = [&](std::string Name)
-> std::pair<const char *, const char *> {
-> std::pair<RemoteRef<void>, uint64_t> {
for (unsigned I = 0; I < NumSect; ++I) {
auto S = reinterpret_cast<typename T::Section *>(
SectionsBuf + (I * sizeof(typename T::Section)));
Expand All @@ -216,10 +216,11 @@ class ReflectionContext
auto SectBufData = reinterpret_cast<const char *>(SectBuf.get());
auto LocalSectStart =
reinterpret_cast<const char *>(SectBufData + RemoteSecStart - RangeStart);
auto LocalSectEnd = reinterpret_cast<const char *>(LocalSectStart + S->size);
return {LocalSectStart, LocalSectEnd};

auto StartRef = RemoteRef<void>(RemoteSecStart, LocalSectStart);
return {StartRef, S->size};
}
return {nullptr, nullptr};
return {nullptr, 0};
};

auto FieldMdSec = findMachOSectionByName("__swift5_fieldmd");
Expand Down Expand Up @@ -298,7 +299,7 @@ class ReflectionContext
sizeof(llvm::object::coff_section) * COFFFileHdr->NumberOfSections);

auto findCOFFSectionByName = [&](llvm::StringRef Name)
-> std::pair<const char *, const char *> {
-> std::pair<RemoteRef<void>, uint64_t> {
for (size_t i = 0; i < COFFFileHdr->NumberOfSections; ++i) {
const llvm::object::coff_section *COFFSec =
reinterpret_cast<const llvm::object::coff_section *>(
Expand All @@ -313,34 +314,30 @@ class ReflectionContext
auto Addr = ImageStart.getAddressData() + COFFSec->VirtualAddress;
auto Buf = this->getReader().readBytes(RemoteAddress(Addr),
COFFSec->VirtualSize);
const char *Begin = reinterpret_cast<const char *>(Buf.get());
const char *End = Begin + COFFSec->VirtualSize;
auto BufStart = Buf.get();
savedBuffers.push_back(std::move(Buf));

auto Begin = RemoteRef<void>(Addr, BufStart);
auto Size = COFFSec->VirtualSize;

// FIXME: This code needs to be cleaned up and updated
// to make it work for 32 bit platforms.
if (SectionName != ".sw5cptr" && SectionName != ".sw5bltn") {
Begin += 8;
End -= 8;
Begin = Begin.atByteOffset(8);
Size -= 16;
}

return {Begin, End};
return {Begin, Size};
}
return {nullptr, nullptr};
return {nullptr, 0};
};

std::pair<const char *, const char *> CaptureSec =
findCOFFSectionByName(".sw5cptr");
std::pair<const char *, const char *> TypeRefMdSec =
findCOFFSectionByName(".sw5tyrf");
std::pair<const char *, const char *> FieldMdSec =
findCOFFSectionByName(".sw5flmd");
std::pair<const char *, const char *> AssocTySec =
findCOFFSectionByName(".sw5asty");
std::pair<const char *, const char *> BuiltinTySec =
findCOFFSectionByName(".sw5bltn");
std::pair<const char *, const char *> ReflStrMdSec =
findCOFFSectionByName(".sw5rfst");
auto CaptureSec = findCOFFSectionByName(".sw5cptr");
auto TypeRefMdSec = findCOFFSectionByName(".sw5tyrf");
auto FieldMdSec = findCOFFSectionByName(".sw5flmd");
auto AssocTySec = findCOFFSectionByName(".sw5asty");
auto BuiltinTySec = findCOFFSectionByName(".sw5bltn");
auto ReflStrMdSec = findCOFFSectionByName(".sw5rfst");

if (FieldMdSec.first == nullptr &&
AssocTySec.first == nullptr &&
Expand Down Expand Up @@ -434,7 +431,7 @@ class ReflectionContext
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());

auto findELFSectionByName = [&](std::string Name)
-> std::pair<const char *, const char *> {
-> std::pair<RemoteRef<void>, uint64_t> {
// Now for all the sections, find their name.
for (const typename T::Section *Hdr : SecHdrVec) {
uint32_t Offset = Hdr->sh_name;
Expand All @@ -445,10 +442,12 @@ class ReflectionContext
RemoteAddress(ImageStart.getAddressData() + Hdr->sh_addr);
auto SecSize = Hdr->sh_size;
auto SecBuf = this->getReader().readBytes(SecStart, SecSize);
auto SecContents = reinterpret_cast<const char *>(SecBuf.get());
return {SecContents, SecContents + SecSize};
auto SecContents = RemoteRef<void>(SecStart.getAddressData(),
SecBuf.get());
savedBuffers.push_back(std::move(SecBuf));
return {SecContents, SecSize};
}
return {nullptr, nullptr};
return {nullptr, 0};
};

auto FieldMdSec = findELFSectionByName("swift5_fieldmd");
Expand Down Expand Up @@ -634,11 +633,11 @@ class ReflectionContext
//
// Non-generic SIL boxes share metadata among types with compatible
// layout, but we need some way to get an outgoing pointer map for them.
auto *CD = getBuilder().getCaptureDescriptor(*CDAddr);
auto CD = getBuilder().getCaptureDescriptor(*CDAddr);
if (CD == nullptr)
return nullptr;

auto Info = getBuilder().getClosureContextInfo(*CD);
auto Info = getBuilder().getClosureContextInfo(CD);

return getClosureContextInfo(ObjectAddress, Info);
}
Expand Down
Loading