-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Infinite loop detection for const evaluation #51702
Merged
bors
merged 14 commits into
rust-lang:master
from
ecstatic-morse:infinite-loop-detection
Jul 11, 2018
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
db025c1
Refactor EvalContext stack and heap into inner struct
ecstatic-morse 6c0f502
Implement Clone, Eq and Hash for the heap and stack
ecstatic-morse 7f9b01a
Add miri infinite loop detection
ecstatic-morse 788c5f3
Revert "Refactor EvalContext stack and heap into inner struct"
ecstatic-morse 0f1c61c
Improve correctness of `Frame` and `Memory` equality
ecstatic-morse f7e9d2a
Add an `InfiniteLoop` variant to `EvalErrorKind`
ecstatic-morse c6aea93
Enable loop detector in step loop
ecstatic-morse 10f2171
Rename `bloom` to `hashes`
ecstatic-morse d36302d
Add a UI test for #50637
ecstatic-morse 647ba29
Explain reason behind error span
ecstatic-morse b3b04b8
Avoid overflow in step counter
ecstatic-morse 0d0e021
Derive Eq and Hash for types used in Miri's evaluator
ecstatic-morse c395044
Shorten error message and add link to test
ecstatic-morse cf5eaa7
Move `Eq + Hash + Clone` bounds to `Machine`
ecstatic-morse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is problematic. Hashing an
AllocId
(inside a relocation of the alloc) will hash the actualId
. This means that if we ever call a function in a loop, since we keep getting newAllocId
s for the function's locals, we'll never realize we're in a loop (ok we will outside the function, but imagine the function returning a raw pointer to its local, and we keep updating a field in the outer function).I think it's fine for now, and will probably go away once we get around to implementing local
AllocId
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So right now, this hashes both the
AllocId
and theAllocation
(which can be crazy slow btw). Would simply hashingAllocation
be correct?I could maybe improve performance by memoizing the hash of an
Allocation
. This would involve setting aCell<Option<u64>>
toNone
whenever anAllocation
is mutated, even when the loop detector isn't running. This might not be too bad, since there's no immediate data dependency on the cached value, and it will often beNone
during regular execution so the branch predictor will be on our side.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could benchmark it. For now let's do the slow version. We are already in "this might take a while territory"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might not even have to hash the memory, if we transitively hash through AllocIds by hashing their allocation instead of the id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I'm a sucker for a premature optimization :).
Can you elaborate on this? I don't quite understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm caught up now; I'll repeat what you said for my own clarity.
alloc_map
contains allAllocation
s which are visible to a givenEvalContext
indexed byAllocId
. The naive approach, comparing thealloc_map
s witheq
, will correctly identify some infinite loops. However, if an object is reallocated, and all pointers to it are updated to point to the new value, the program states are equivalent but our equality function will fail.Instead, we can look at the program heap as a graph of allocations whose roots are
Value::ByRef
s on the stack and whose edges are theAllocId
s inRelocations
. Equality overEvalSnaphost
Frame
s is the same except when it comes to theseByRef
variants. We would push them onto a separate list for each frame, then do a simultaneous BFS/DFS, comparing eachAllocation
along the way. This would make equality invariant over changingAllocId
s.It is much easier to implement
Hash
thanPartialEq
with this approach: we simply omit anyAllocId
s--either on the stack in aValue::ByRef
or the heap in aRelocations
--from the hash.I think I will implement this naively at first (not handling object reallocation), then try to come up with a test case where the more robust case would be necessary to detect infinite loops.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I belive that the result of casting a reference to
usize
during CTFE is still up in the air. If theAllocId
is used for that, the more complex strategy will no longer be correct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following test case fails as you predicted.
There must be a subtler way of triggering this, but I can't come up with one at the moment. I don't think this class of programs is worth implementing a custom equality function which ignores
AllocId
s. Let me know if you disagree.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can at least check for each AllocId that we hash, whether it is dangling, and then just hash some default value (e.g.
u64::max_value
), unfortunately that requires the same changes as walking down AllocIds...If you open an issue about the not working example and the explanation of the full fix, we can merge this PR as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll open an issue. I plan to keep working on this btw.