Skip to content

[Reflection] Prevent symbolic reference mangling induced crashes #37514

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

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented May 19, 2021

Based on lldb crash logs, TypeRefBuilder is crashing on instances of symbolic references in mangled names.

The crash happens when a symbolic reference is attempted to be mangled (via mangleNode()), which leads to a call to unreachable which aborts. For library usage, such as from lldb, these cases need to be handled without an abort.

The improvements to gracefully handle symbolic references are:

  1. Change the return type of normalizeReflectionName from std::string to llvm::Optional<std::string>
  2. Thread through a bool flag named useOpaqueTypeSymbolicReferences

Both of these start in normalizeReflectionName.

First, the call to demangleTypeRef() now sets useOpaqueTypeSymbolicReferences to false. Without this, demangleTypeRef() can return a node of kind OpaqueTypeDescriptorSymbolicReference, which is guaranteed to fail in this code path when in subsequent call to mangleNode().

Second, if the result of demangleTypeRef() is one of the symbolic reference kinds, then the function exits early with a value of None. Callers of normalizeReflectionName() are now forced to handle such cases, where the mangled name could not be normalized.

In TypeRefBuilder::getFieldTypeInfo(), if normalizeReflectionName() fails, then the corresponding field is not supported, or in other words, dropped.

rdar://77613304

@adrian-prantl
Copy link
Contributor

The fix here is to avoid calling mangleNode() for symbolic reference nodes, since that is guaranteed to be bad.

Do we understand what that means, i.e., when we hit the mitigation, do we create incomplete mangled names that might cause crashes later?
For the longer-term perspective, should mangleNode be able to return errors?

@slavapestov
Copy link
Contributor

@jckarter any ideas on how to handle symbolic references here?

@kastiglione kastiglione changed the title [Reflection] Prevent type reference manlging induced creash [Reflection] Prevent symbolic reference manlging induced creash May 19, 2021
@kastiglione kastiglione changed the title [Reflection] Prevent symbolic reference manlging induced creash [Reflection] Prevent symbolic reference manlging induced crashes May 19, 2021
@kastiglione
Copy link
Contributor Author

Do we understand what that means, i.e., when we hit the mitigation, do we create incomplete mangled names that might cause crashes later?

Good question, and no. I am working on being able to reproduce the bug reports, but without that we don't know what the downstream effects are.

@kastiglione kastiglione changed the title [Reflection] Prevent symbolic reference manlging induced crashes [Reflection] Prevent symbolic reference mangling induced crashes May 19, 2021
@mikeash
Copy link
Contributor

mikeash commented May 20, 2021

You want to call the mangleNode overload that takes a SymbolicResolver. We have a few pre-built resolvers in Private.h (ResolveToDemanglingForContext, ResolveAsSymbolicReference, ExpandResolvedSymbolicReferences) but those are all in-process. It shouldn't be too hard to make a templatized version of _buildDemanglingForSymbolicReference that works remotely. Of course, if you ever encounter SymbolicReferenceKind::AccessorFunctionReference then you're just completely doomed in the remote case, so you'll have to have a failure case regardless.

I am very confused by all this, though. Does normalizeReflectionName just not work, but we get away with it because it rarely encounters symbolic references? Or is there some hidden path I haven't spotted which handles the common cases?

@kastiglione kastiglione marked this pull request as ready for review May 28, 2021 22:56
@kastiglione kastiglione requested a review from a team as a code owner May 28, 2021 22:56
@kastiglione
Copy link
Contributor Author

@mikeash I have updated this PR to handle the one potential case we discussed, where normalizeReflectionName returns a node of kind OpaqueTypeDescriptorSymbolicReference. If that were to happen it would be certain crash.

The fix is to thread through useOpaqueTypeSymbolicReferences, with a value of false – from normalizeReflectionName. This prevents one potential crash.

@kastiglione
Copy link
Contributor Author

@swift-ci test

@kastiglione kastiglione force-pushed the Reflection-Prevent-type-reference-manlging-induced-creash branch from d9bc399 to 265c15a Compare May 28, 2021 23:18
@kastiglione kastiglione changed the base branch from release/5.5 to release/5.5-05142021 May 28, 2021 23:18
@kastiglione
Copy link
Contributor Author

@swift-ci nominate

@adrian-prantl adrian-prantl requested a review from jckarter May 28, 2021 23:37
@kastiglione
Copy link
Contributor Author

@swift-ci test

@adrian-prantl adrian-prantl requested review from xedin and eeckstein May 29, 2021 00:10
Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

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

Looks good. Thank you!

@kastiglione
Copy link
Contributor Author

Closing for this branch only. See linked PRs for main and release/5.5.

@kastiglione kastiglione closed this Jun 2, 2021
@kastiglione kastiglione deleted the Reflection-Prevent-type-reference-manlging-induced-creash branch June 2, 2021 00:10
@kastiglione kastiglione restored the Reflection-Prevent-type-reference-manlging-induced-creash branch June 3, 2021 18:37
@kastiglione kastiglione reopened this Jun 3, 2021
@kastiglione
Copy link
Contributor Author

@swift-ci test

@DougGregor DougGregor merged commit 3177b7f into release/5.5-05142021 Jun 4, 2021
@DougGregor DougGregor deleted the Reflection-Prevent-type-reference-manlging-induced-creash branch June 4, 2021 16:16
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