Miri: properly check return-position noalias#106212
Closed
RalfJung wants to merge 3 commits into
Closed
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
As discussed in rust-lang/unsafe-code-guidelines#385,
Box-returning functions get anoaliason their return value, which has a bunch of extra aliasing rules that Miri did not properly check.We now do this by marking the post-return retags as
FnReturn, and then when retagging aBoxin that vein we clear the borrow stack of all the other tags. On its own this change is incompatible with the weak protectors onBoxarguments that I recently introduced to fixBoxnoaliasenforcement, so we replace those protectors by also doing such a "clear" retagging inFnEntryforBox. Conveniently this means we can remove weak protectors entirely while still properly enforcingnoalias(as far as I can tell).Basically this means that
Boxcannot be reborrowed; when they are passed to and from a function, they always must be the only pointer used henceforth for this allocation. WithBoxnot being a reference type, this kind of makes sense I guess. There is a chance that this might break even more unsafe code that usesBoxin creative ways, though that seems unlikely -- when aBoxis passed to a function, I would expect that function to either deallocate or return theBox; the new point now is that the caller must use the returned box rather than considering this a "reborrow" and sticking to some sneaky copy of the old box. Such code would be unsound with return-positionnoaliasanyway.@saethlin I hope this doesn't break the tag cache in subtle ways, though given that it can handle clearing the stack for the "unknown bottom" situation, I assume that will also work here.
One odd thing that can now happen is that the "base tag" of an allocation might cease to be valid for that allocation. But I don't think we ever assumed that it would always remain valid, and this does seem to match the semantics of return-position
noalias, so I think this is fine.r? @oli-obk @saethlin