Skip to content

[Reflection] Prevent symbolic reference mangling induced crash #37732

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
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
13 changes: 7 additions & 6 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class TypeRefBuilder {
private:
std::vector<ReflectionInfo> ReflectionInfos;

std::string normalizeReflectionName(RemoteRef<char> name);
llvm::Optional<std::string> normalizeReflectionName(RemoteRef<char> name);
bool reflectionNameMatches(RemoteRef<char> reflectionName,
StringRef searchName);

Expand All @@ -654,7 +654,7 @@ class TypeRefBuilder {
// TypeRefBuilder struct, to isolate its template-ness from the rest of
// TypeRefBuilder.
unsigned PointerSize;
std::function<Demangle::Node * (RemoteRef<char>)>
std::function<Demangle::Node * (RemoteRef<char>, bool)>
TypeRefDemangler;
std::function<const TypeRef* (uint64_t, unsigned)>
OpaqueUnderlyingTypeReader;
Expand All @@ -665,10 +665,10 @@ class TypeRefBuilder {
: TC(*this),
PointerSize(sizeof(typename Runtime::StoredPointer)),
TypeRefDemangler(
[this, &reader](RemoteRef<char> string) -> Demangle::Node * {
[this, &reader](RemoteRef<char> string, bool useOpaqueTypeSymbolicReferences) -> Demangle::Node * {
return reader.demangle(string,
remote::MangledNameKind::Type,
Dem, /*useOpaqueTypeSymbolicReferences*/ true);
Dem, useOpaqueTypeSymbolicReferences);
}),
OpaqueUnderlyingTypeReader(
[&reader](uint64_t descriptorAddr, unsigned ordinal) -> const TypeRef* {
Expand All @@ -677,8 +677,9 @@ class TypeRefBuilder {
})
{}

Demangle::Node *demangleTypeRef(RemoteRef<char> string) {
return TypeRefDemangler(string);
Demangle::Node *demangleTypeRef(RemoteRef<char> string,
bool useOpaqueTypeSymbolicReferences = true) {
return TypeRefDemangler(string, useOpaqueTypeSymbolicReferences);
}

TypeConverter &getTypeConverter() { return TC; }
Expand Down
27 changes: 19 additions & 8 deletions stdlib/public/Reflection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,22 @@ RemoteRef<char> TypeRefBuilder::readTypeRef(uint64_t remoteAddr) {
}

/// Load and normalize a mangled name so it can be matched with string equality.
std::string
llvm::Optional<std::string>
TypeRefBuilder::normalizeReflectionName(RemoteRef<char> reflectionName) {
// Remangle the reflection name to resolve symbolic references.
if (auto node = demangleTypeRef(reflectionName)) {
auto result = mangleNode(node);
clearNodeFactory();
return result;
if (auto node = demangleTypeRef(reflectionName,
/*useOpaqueTypeSymbolicReferences*/ false)) {
switch (node->getKind()) {
case Node::Kind::TypeSymbolicReference:
case Node::Kind::ProtocolSymbolicReference:
case Node::Kind::OpaqueTypeDescriptorSymbolicReference:
// Symbolic references cannot be mangled, return a failure.
return {};
default:
auto result = mangleNode(node);
clearNodeFactory();
return result;
}
}

// Fall back to the raw string.
Expand All @@ -102,7 +111,9 @@ bool
TypeRefBuilder::reflectionNameMatches(RemoteRef<char> reflectionName,
StringRef searchName) {
auto normalized = normalizeReflectionName(reflectionName);
return searchName.equals(normalized);
if (!normalized)
return false;
return searchName.equals(*normalized);
}

const TypeRef * TypeRefBuilder::
Expand Down Expand Up @@ -194,8 +205,8 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
if (!FD->hasMangledTypeName())
continue;
auto CandidateMangledName = readTypeRef(FD, FD->MangledTypeName);
auto NormalizedName = normalizeReflectionName(CandidateMangledName);
FieldTypeInfoCache[NormalizedName] = FD;
if (auto NormalizedName = normalizeReflectionName(CandidateMangledName))
FieldTypeInfoCache[*NormalizedName] = FD;
}
}

Expand Down