-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Miri: properly check return-position noalias #106212
Conversation
This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras The Miri subtree was changed cc @rust-lang/miri Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
src/tools/miri/tests/fail/stacked_borrows/box_noalias_violation.stderr
Outdated
Show resolved
Hide resolved
22e19f0
to
6dacaf0
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
Can we just stop emitting ret-position noalias instead? I'm not fundamentally opposed to having semantics as suggested here, but I can't really claim to understand all the consequences of it and actually supporting allocators reasonably well needs a lot more work |
I mean, we could, but I won't pursue that for now. This is obviously fine for safe code, and this PR is a way to learn how bad it is for unsafe code.
I am not fundamentally opposed to rid Box of all its aliasing requirements, but that RFC needs to be written by someone else. I also think there should be a type for "Box without noalias", aka " NonNull with deallocation on drop", but making that type the same as Box leaves a lot of optimization potential on the table in safe code. We have references and raw pointers for people to make that choice themselves, it would make sense to likewise have a "raw box" type.
|
Ah, but this is a problem for boxes with custom allocators. |
Yes, I think the new clear-on-return logic is correctly handled by the same code path that handles the creation of an unknown bottom. Bless whoever thought of factoring the code in this way instead of writing the base tag over the whole cache ;) |
Working on it, slowly. Keep mentioning this to fuel the fire. I'm hoping I'll shop around a pre-RFC at the end of January. |
You got your wish. ;) This PR is superseded by #106371. |
tweaks to retag diagnostic handling Salvaged from rust-lang/rust#106212
tweaks to retag diagnostic handling Salvaged from rust-lang#106212
As discussed in rust-lang/unsafe-code-guidelines#385,
Box
-returning functions get anoalias
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 aBox
in that vein we clear the borrow stack of all the other tags. On its own this change is incompatible with the weak protectors onBox
arguments that I recently introduced to fixBox
noalias
enforcement, so we replace those protectors by also doing such a "clear" retagging inFnEntry
forBox
. Conveniently this means we can remove weak protectors entirely while still properly enforcingnoalias
(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. WithBox
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 usesBox
in creative ways, though that seems unlikely -- when aBox
is 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-positionnoalias
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