Skip to content

Limit the recursion depth when trying to get the mangling for a context descriptor #35314

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 2 commits into from
Feb 9, 2021

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Jan 8, 2021

This should help guard against corrupted data in the target app when debugging.

Resolves rdar://66442021

…xt descriptor

This should help guard against corrupted data in the target app when debugging.
@@ -1131,7 +1131,7 @@ class MetadataReader {
Demangle::NodePointer
buildContextMangling(ContextDescriptorRef descriptor,
Demangler &dem) {
auto demangling = buildContextDescriptorMangling(descriptor, dem);
auto demangling = buildContextDescriptorMangling(descriptor, dem, 50);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jckarter Does 50 seem like a sufficient limit here?

@tbkka tbkka requested review from jckarter and mikeash January 8, 2021 19:11
@tbkka
Copy link
Contributor Author

tbkka commented Jan 8, 2021

@swift-ci Please test

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.

Approved with sadness.

@swift-ci
Copy link
Contributor

swift-ci commented Jan 8, 2021

Build failed
Swift Test OS X Platform
Git Sha - b4829c3

@tbkka
Copy link
Contributor Author

tbkka commented Jan 8, 2021

@swift-ci Please test Windows platform

@tbkka
Copy link
Contributor Author

tbkka commented Jan 8, 2021

@swift-ci Please test macOS

@tbkka
Copy link
Contributor Author

tbkka commented Jan 8, 2021

Restarted testing; looks like there are random test timeouts keeping this from finishing.

if (descriptor.isResolved()) {
return buildContextDescriptorMangling(descriptor.getResolved(), dem);
return buildContextDescriptorMangling(descriptor.getResolved(), dem, recursion_limit - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

You're cutting the actual recursion limit in half by decrementing here too, since every time we try to resolve a parent we'll end up decrementing recursion_limit twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Fixed.

@jckarter
Copy link
Contributor

A type could legitimately be scoped 25 levels deep; maybe that's unlikely, but it'd be unfortunate for debugging to mysteriously stop working if it does happen. Maybe we need a more robust way of detecting valid pointers into the remote object. We know that context descriptors can only ever be in the immutable __TEXT segment of images, so if MetadataReader were enhanced to track what regions of the remote address space are immutable, then we could immediately discard any context descriptor pointers that don't point into one of those regions.

@tbkka
Copy link
Contributor Author

tbkka commented Jan 11, 2021

@jckarter I agree that more checks are needed here, but I don't think we can avoid some kind of recursion limit since malformed data could potentially have cycles, and simply checking for valid pointers can't guard against that.

I've fixed the double-counting, so we're now supporting 50 levels of nesting. Based on your comment, I'm guessing that should be sufficient?

@jckarter
Copy link
Contributor

If we're primarily concerned about cycles, then could we track a set of already-visited pointers, and error out if we see a real cycle? If we're going to put an arbitrary limit here, then IMO we should also impose the limit consistently in the compiler and other tools.

@jckarter
Copy link
Contributor

(Or, you don't actually need to track a set; you can do the old "find cycles in a linked list" trick by keeping another pointer that you update at double speed through the list and check if the two pointers ever line up)

@tbkka
Copy link
Contributor Author

tbkka commented Jan 11, 2021

@swift-ci Please test

@mikeash
Copy link
Contributor

mikeash commented Jan 11, 2021

Even if we restrict this to known immutable memory, we could get a bad pointer into something that happens to be immutable memory, and end up chasing things down in what would essentially be garbage data.

I really like the idea of detecting cycles explicitly, but I'm also afraid that we'll encounter garbage that just happens to present as a non-cyclic structure deep enough to overflow the stack.

@tbkka
Copy link
Contributor Author

tbkka commented Jan 11, 2021

@swift-ci Please test

@tbkka
Copy link
Contributor Author

tbkka commented Jan 11, 2021

@swift-ci Please test Windows platform

1 similar comment
@tbkka
Copy link
Contributor Author

tbkka commented Jan 11, 2021

@swift-ci Please test Windows platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b5f84de

@tbkka
Copy link
Contributor Author

tbkka commented Feb 8, 2021

@swift-ci Please test

1 similar comment
@tbkka
Copy link
Contributor Author

tbkka commented Feb 8, 2021

@swift-ci Please test

@swift-ci
Copy link
Contributor

swift-ci commented Feb 9, 2021

Build failed
Swift Test OS X Platform
Git Sha - b5f84de

@tbkka
Copy link
Contributor Author

tbkka commented Feb 9, 2021

@swift-ci Please test macOS Platform

1 similar comment
@tbkka
Copy link
Contributor Author

tbkka commented Feb 9, 2021

@swift-ci Please test macOS Platform

@swift-ci
Copy link
Contributor

swift-ci commented Feb 9, 2021

Build failed
Swift Test Linux Platform
Git Sha - b5f84de

@tbkka
Copy link
Contributor Author

tbkka commented Feb 9, 2021

@swift-ci Please test Linux platform

@tbkka tbkka merged commit 04b21d0 into swiftlang:main Feb 9, 2021
@tbkka tbkka deleted the tbkka/limitContextDescriptorRecursion branch February 9, 2021 19:07
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.

4 participants