Skip to content

Miri: properly check return-position noalias#106212

Closed
RalfJung wants to merge 3 commits into
rust-lang:masterfrom
RalfJung:ret-position-noalias
Closed

Miri: properly check return-position noalias#106212
RalfJung wants to merge 3 commits into
rust-lang:masterfrom
RalfJung:ret-position-noalias

Conversation

@RalfJung

Copy link
Copy Markdown
Member

As discussed in rust-lang/unsafe-code-guidelines#385, Box-returning functions get a noalias on 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 a Box in that vein we clear the borrow stack of all the other tags. On its own this change is incompatible with the weak protectors on Box arguments that I recently introduced to fix Box noalias enforcement, so we replace those protectors by also doing such a "clear" retagging in FnEntry for Box. Conveniently this means we can remove weak protectors entirely while still properly enforcing noalias (as far as I can tell).

Basically this means that Box cannot be reborrowed; when they are passed to and from a function, they always must be the only pointer used henceforth for this allocation. With Box not being a reference type, this kind of makes sense I guess. There is a chance that this might break even more unsafe code that uses Box in creative ways, though that seems unlikely -- when a Box is passed to a function, I would expect that function to either deallocate or return the Box; 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-position noalias anyway.

@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

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants