Skip to content

[Reflection] Check that the offset is within the section. #24574

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 2 commits into from
May 7, 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
10 changes: 6 additions & 4 deletions include/swift/Reflection/Records.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ class FieldRecord {
(const char *)((uintptr_t)MangledTypeName.get() + Offset));
}

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

bool isIndirectCase() const {
Expand Down
10 changes: 7 additions & 3 deletions stdlib/public/Reflection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ bool TypeRefBuilder::getFieldTypeRefs(
- FD.second->TypeReference.SectionOffset;
auto FieldOffset = FD.second->Field.SectionOffset
- FD.second->ReflectionString.SectionOffset;
auto FieldName = Field.getFieldName(FieldOffset);
auto Low = (uintptr_t)(FD.second->ReflectionString.Metadata.startAddress());
auto High = (uintptr_t)(FD.second->ReflectionString.Metadata.endAddress());
auto FieldName = Field.getFieldName(FieldOffset, Low, High);

// Empty cases of enums do not have a type
if (FD.first->isEnum() && !Field.hasMangledTypeName()) {
Expand Down Expand Up @@ -339,8 +341,10 @@ void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
OS << '-';
OS << '\n';
for (auto &field : descriptor) {
OS << std::string(field.getFieldName(NameOffset).begin(),
field.getFieldName(NameOffset).end());
auto Low = (uintptr_t)sections.ReflectionString.Metadata.startAddress();
auto High = (uintptr_t)sections.ReflectionString.Metadata.endAddress();
OS << std::string(field.getFieldName(NameOffset, Low, High).begin(),
field.getFieldName(NameOffset, Low, High).end());
if (field.hasMangledTypeName()) {
OS << ": ";
dumpTypeRef(field.getMangledTypeName(TypeRefOffset), OS);
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/runtime/ReflectionMirror.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ static bool _shouldReportMissingReflectionMetadataWarnings() {

const FieldDescriptor &descriptor = *fields;
auto &field = descriptor.getFields()[index];
auto name = field.getFieldName(0);
// Bounds are always valid as the offset is constant.
auto name = field.getFieldName(0, 0, std::numeric_limits<uint64_t>::max());

// Enum cases don't always have types.
if (!field.hasMangledTypeName())
Expand Down