Skip to content

Address unleaking of heap data causing miri test failures #75

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 5 commits into from
Nov 14, 2022
Merged

Conversation

jamesmunns
Copy link
Collaborator

Fixes #74

It seems that the changes made to tests in #71 to unleak the heap buffers and allow disabling -Zmiri-ignore-leaks did so in a way that now makes miri angery.

This is currently a minimal commit disabling the leak step (in a hacky way). I'm going to investigate if there is a sound way to do this.

@jamesmunns jamesmunns requested a review from phil-opp November 14, 2022 12:11
@jamesmunns
Copy link
Collaborator Author

jamesmunns commented Nov 14, 2022

@phil-opp this is now ready for review! I was able to restore the leak checks, the root cause here was that we were previously keeping a live (edit: and aliasing!) reference to the heap allocation in order to drop it in the closure.

I'm also... not sure how the closure-to-unleak previously worked? Seeing as the closure was never explicitly called. This came back to bite me in 3f8f9fc. I've gone ahead and made this a type with a Drop impl instead of a closure so that we know it will be called now. Let me know if I missed something on this.

edit: The fix is to not keep a reference, and instead allow for aliasing pointers instead. As long as the heap itself is dropped before we begin re-using the pointer (to unleak and drop the allocation), this is sound as per stacked borrows.

In general, we need to be really careful about ever mixing references and pointers.

Copy link
Member

@phil-opp phil-opp left a comment

Choose a reason for hiding this comment

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

Looks very good! Thanks a lot for investigating and fixing this so quickly!

@phil-opp
Copy link
Member

I'm also... not sure how the closure-to-unleak previously worked? Seeing as the closure was never explicitly called. This came back to bite me in 3f8f9fc.

The closure was not called, but it owned the Box. So the memory was freed again when the closure was dropped. Clippy was okay with this approach, but apparently that was just a false negative.

I've gone ahead and made this a type with a Drop impl instead of a closure so that we know it will be called now. Let me know if I missed something on this.

That seems like a much clearer solution!

edit: The fix is to not keep a reference, and instead allow for aliasing pointers instead. As long as the heap itself is dropped before we begin re-using the pointer (to unleak and drop the allocation), this is sound as per stacked borrows.

In general, we need to be really careful about ever mixing references and pointers.

Makes sense. Agreed!

@phil-opp phil-opp merged commit c2aa6ec into main Nov 14, 2022
@phil-opp phil-opp deleted the fix-74 branch November 14, 2022 15:54
@phil-opp
Copy link
Member

Given that this only affects the test framework, we should not need a new crates.io release for this, right?

@jamesmunns
Copy link
Collaborator Author

jamesmunns commented Nov 14, 2022 via email

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.

Miri test failure: Stacked Borrow rules violated
2 participants