Skip to content

Fix lookupDirect() use-after-scope bugs #20557

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

Conversation

beccadax
Copy link
Contributor

In #7530, NominalTypeDecl::lookupDirect() started returning TinyPtrVector instead of ArrayRef. Unfortunately, some callees assigned its return value to a variable explicitly declared to be an ArrayRef; C++ happily converted the TinyPtrVector to an ArrayRef and then treated the TinyPtrVector as out-of-scope, so the ArrayRef would now point to an out-of-scope stack object.

This PR should fix ASan stack-use-after-scope test failures whose backtraces include one of:

  • CSGen.cpp:3511
  • CastOptimizer.cpp:333
  • CastOptimizer.cpp:375

There may still be other ASan failures after this PR. There should not be any normal test failures.

Resolves rdar://46031343.

In swiftlang#7530, NominalTypeDecl::lookupDirect() started returning TinyPtrVector instead of ArrayRef so that it wouldn’t be returning a pointer into a mutable data structure. Unfortunately, some callees assigned its return value into an ArrayRef; C++ happily converted the TinyPtrVector to an ArrayRef and then treated the TinyPtrVector as out-of-scope, so the ArrayRef would now point to an out-of-scope object. Oops.
@beccadax
Copy link
Contributor Author

@swift-ci please smoke test

@xedin
Copy link
Contributor

xedin commented Nov 14, 2018

@swift-ci please asan test

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

Nice work!

@@ -7048,7 +7048,7 @@ void SwiftDeclConverter::importInheritedConstructors(

auto curObjCClass = cast<clang::ObjCInterfaceDecl>(classDecl->getClangDecl());

auto inheritConstructors = [&](ArrayRef<ValueDecl *> members,
auto inheritConstructors = [&](TinyPtrVector<ValueDecl *> members,
Copy link
Contributor

Choose a reason for hiding this comment

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

This one should be okay, no? Passing something down the stack should always be okay (because of C++'s "destruction happens at the end of the full-expression" rule.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ASan didn’t show any failures here—I just thought it looked suspicious and would be fairly cheap to recode defensively—so if that’s the rule, this one is probably unnecessary.

@DougGregor
Copy link
Member

Thank you!

@gottesmm
Copy link
Contributor

Looks good! Merging to try to fix bots.

@gottesmm gottesmm merged commit e379ab9 into swiftlang:master Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants