Skip to content

[Reflection] Try to read only the reflection sections. #21255

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 3 commits into from
Dec 14, 2018
Merged
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
66 changes: 51 additions & 15 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,60 @@ class ReflectionContext
if (!Command)
return false;

// Read everything including the __TEXT segment.
Buf = this->getReader().readBytes(ImageStart, Command->vmsize);
auto Start = reinterpret_cast<const char *>(Buf.get());
// Find the load command offset.
auto loadCmdOffset = ImageStart.getAddressData() + Offset + sizeof(typename T::Header);

// Read the load command.
auto LoadCmdAddress = reinterpret_cast<const char *>(loadCmdOffset);
auto LoadCmdBuf = this->getReader().readBytes(
RemoteAddress(LoadCmdAddress), sizeof(typename T::SegmentCmd));
auto LoadCmd = reinterpret_cast<typename T::SegmentCmd *>(LoadCmdBuf.get());

// The sections start immediately after the load command.
unsigned NumSect = LoadCmd->nsects;
auto SectAddress = reinterpret_cast<const char *>(loadCmdOffset) +
sizeof(typename T::SegmentCmd);
auto Sections = this->getReader().readBytes(
RemoteAddress(SectAddress), NumSect * sizeof(typename T::Section));

auto Slide = ImageStart.getAddressData() - Command->vmaddr;
std::string Prefix = "__swift5";
uint64_t RangeStart = UINT64_MAX;
uint64_t RangeEnd = UINT64_MAX;
auto SectionsBuf = reinterpret_cast<const char *>(Sections.get());
for (unsigned I = 0; I < NumSect; ++I) {
auto S = reinterpret_cast<typename T::Section *>(
SectionsBuf + (I * sizeof(typename T::Section)));
if (strncmp(S->sectname, Prefix.c_str(), strlen(Prefix.c_str())) != 0)
continue;
if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX) {
RangeStart = S->addr + Slide;
RangeEnd = S->addr + S->size + Slide;
continue;
}
RangeStart = std::min(RangeStart, (uint64_t)S->addr + Slide);
RangeEnd = std::max(RangeEnd, (uint64_t)(S->addr + S->size + Slide));
}

if (RangeStart == UINT64_MAX && RangeEnd == UINT64_MAX)
return false;

auto SectBuf = this->getReader().readBytes(RemoteAddress(RangeStart),
RangeEnd - RangeStart);

auto findMachOSectionByName = [&](std::string Name)
-> std::pair<std::pair<const char *, const char *>, uint32_t> {
auto cmdOffset = Start + Offset + sizeof(typename T::Header);
auto SegCmd = reinterpret_cast<typename T::SegmentCmd *>(cmdOffset);
auto SectAddress = reinterpret_cast<const char *>(cmdOffset) +
sizeof(typename T::SegmentCmd);
for (unsigned I = 0; I < SegCmd->nsects; ++I) {
for (unsigned I = 0; I < NumSect; ++I) {
auto S = reinterpret_cast<typename T::Section *>(
SectAddress + (I * sizeof(typename T::Section)));
SectionsBuf + (I * sizeof(typename T::Section)));
if (strncmp(S->sectname, Name.c_str(), strlen(Name.c_str())) != 0)
continue;
auto Slide = ImageStart.getAddressData() - Command->vmaddr;
auto RemoteSecStart = S->addr + Slide;
auto LocalSecStart = RemoteSecStart - ImageStart.getAddressData() + Start;
auto SecSize = S->size;
return {{LocalSecStart, LocalSecStart + SecSize}, 0};
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}, 0};
}
return {{nullptr, nullptr}, 0};
};
Expand All @@ -178,8 +212,8 @@ class ReflectionContext
ReflStrMdSec.first.first == nullptr)
return false;

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

ReflectionInfo info = {
{{FieldMdSec.first.first, FieldMdSec.first.second}, 0},
Expand Down Expand Up @@ -212,6 +246,8 @@ class ReflectionContext
}

savedBuffers.push_back(std::move(Buf));
savedBuffers.push_back(std::move(SectBuf));
savedBuffers.push_back(std::move(Sections));
return true;
}

Expand Down