Skip to content

[Runtime] Fix premature snapshot destruction in StableAddressConcurrentReadableHashMap::find. #35345

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
Jan 11, 2021
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
19 changes: 14 additions & 5 deletions include/swift/Runtime/Concurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,13 @@ template <class ElemTy> struct ConcurrentReadableArray {
~Snapshot() {
Array->decrementReaders();
}

const ElemTy *begin() { return Start; }
const ElemTy *end() { return Start + Count; }

// These are marked as ref-qualified (the &) to make sure they can't be
// called on temporaries, since the temporary would be destroyed before the
// return value can be used, making it invalid.
const ElemTy *begin() & { return Start; }
const ElemTy *end() & { return Start + Count; }

size_t count() { return Count; }
};

Expand Down Expand Up @@ -974,7 +978,11 @@ struct ConcurrentReadableHashMap {

/// Search for an element matching the given key. Returns a pointer to the
/// found element, or nullptr if no matching element exists.
template <class KeyTy> const ElemTy *find(const KeyTy &key) {
//
// This is marked as ref-qualified (the &) to make sure it can't be called
// on temporaries, since the temporary would be destroyed before the return
// value can be used, making it invalid.
template <class KeyTy> const ElemTy *find(const KeyTy &key) & {
if (!Indices.Value || !ElementCount || !Elements)
return nullptr;
return ConcurrentReadableHashMap::find(key, Indices, ElementCount,
Expand Down Expand Up @@ -1181,7 +1189,8 @@ struct StableAddressConcurrentReadableHashMap
}

template <class KeyTy> ElemTy *find(const KeyTy &key) {
auto result = this->snapshot().find(key);
auto snapshot = this->snapshot();
auto result = snapshot.find(key);
if (!result)
return nullptr;
return result->Ptr;
Expand Down