Skip to content

[5.4][Runtime] Fix race condition in protocol conformance lookups that caused false negatives. #35850

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
4 changes: 3 additions & 1 deletion include/swift/Demangling/TypeLookupError.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class TypeLookupError {

Fn(Context, Command::DestroyContext, nullptr);
Fn = other.Fn;
Context = Fn(Context, Command::CopyContext, nullptr);
Context = Fn(other.Context, Command::CopyContext, nullptr);

return *this;
}
Expand Down Expand Up @@ -181,6 +181,8 @@ template <typename T> class TypeLookupErrorOr {
TaggedUnion<T, TypeLookupError> Value;

public:
TypeLookupErrorOr() : Value(TypeLookupError("freshly constructed error")) {}

TypeLookupErrorOr(const T &t) : Value(t) {
if (!t)
Value = TypeLookupError("unknown error");
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5006,7 +5006,7 @@ swift_getAssociatedTypeWitnessSlowImpl(
Demangle::makeSymbolicMangledNameStringRef(mangledNameBase);

// Demangle the associated type.
TypeLookupErrorOr<TypeInfo> result = TypeInfo();
TypeLookupErrorOr<TypeInfo> result;
if (inProtocolContext) {
// The protocol's Self is the only generic parameter that can occur in the
// type.
Expand Down
24 changes: 18 additions & 6 deletions stdlib/public/runtime/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Basic/Unreachable.h"
#include "llvm/ADT/DenseMap.h"
#include "CompatibilityOverride.h"
#include "ImageInspection.h"
#include "Private.h"
Expand Down Expand Up @@ -470,6 +471,7 @@ swift_conformsToProtocolImpl(const Metadata *const type,
return found.second;

// Scan conformance records.
llvm::SmallDenseMap<const Metadata *, const WitnessTable *> foundWitnesses;
auto processSection = [&](const ConformanceSection &section) {
// Eagerly pull records for nondependent witnesses into our cache.
auto processDescriptor = [&](const ProtocolConformanceDescriptor &descriptor) {
Expand All @@ -484,6 +486,7 @@ swift_conformsToProtocolImpl(const Metadata *const type,
if (auto *matchingType = candidate.getMatchingType(type)) {
auto witness = descriptor.getWitnessTable(matchingType);
C.cacheResult(matchingType, protocol, witness, /*always cache*/ 0);
foundWitnesses.insert({matchingType, witness});
}
};

Expand All @@ -505,14 +508,23 @@ swift_conformsToProtocolImpl(const Metadata *const type,
processSection(section);
}

// Try the search again to look for the most specific cached conformance.
found = searchInConformanceCache(type, protocol);
// Find the most specific conformance that was scanned.
const WitnessTable *foundWitness = nullptr;
const Metadata *searchType = type;
while (!foundWitness && searchType) {
foundWitness = foundWitnesses.lookup(searchType);

// If it's not authoritative, then add an authoritative entry for this type.
if (!found.first)
C.cacheResult(type, protocol, found.second, snapshot.count());
// If there's no entry here, move up to the superclass (if any).
if (!foundWitness)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test will fail to distinguish between "value not found" and "NULL found".

Copy link
Contributor

@grynspan grynspan Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, C is not the same as foundWitnesses. foundWitnesses is local. It's a local variable. It's a variable that's only used locally.

searchType = _swift_class_getSuperclass(searchType);
}

// If it's for a superclass or if we didn't find anything, then add an
// authoritative entry for this type.
if (searchType != type)
C.cacheResult(type, protocol, foundWitness, snapshot.count());

return found.second;
return foundWitness;
}

const ContextDescriptor *
Expand Down