-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add safe volatile functions. #47975
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
Add safe volatile functions. #47975
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
eeb07a6
to
81045f2
Compare
81045f2
to
2c09c2f
Compare
I'm not sure we should land these without first establishing a more formal memory model. In particular, afaict a types-as-contracts-based interpretation would make |
@cramerj I see |
@clarcharr Cell and other !Freeze types are a special exception to this rule that the compiler understands. |
I share the doubts about how useful a volatile load through a safe reference is. I don't believe it's likely to be miscompiled -- in practice LLVM will probably let I also have some doubts about the robustness of the functions that are intended to partially replace |
In terms of review I'd personally like to see these functions used to see whether in practice this works or not first as well. |
It feels like there's a moderate amount of response to not accept this PR; am I reading the comments correct? Is the team leaning towards closing this PR, perhaps in favor of discussion on IRLO? |
@cramertj @alexcrichton @rkruppe Should this PR be closed in favor of an RFC? The author has offered to draft one. |
Yeah let's close this for now, I think this may want to have more discussion on an RFC or perhaps on internals before we add to libstd. In the meantime as well this can certainly be added as a crate on crates.io! |
This adds safe equivalents of
read_volatile
andwrite_volatile
tostd::mem
, using references instead of pointers.The
move_opaque
anddrop_opaque
functions are thevalue_fence
andcompute_and_drop
mentioned in rust-lang/rfcs#1484 (comment).move_opaque
is roughly equivalent to the unstabletest::black_box
.copy_opaque
andreplace_opaque
are added for completeness, as they are useful equivalentsread_volatile
andwrite_volatile
that will work in most cases. This means that volatile reads and writes don't have to be annotated withunsafe
on each occurrence, and that the programmer simply has to verify that the pointers are correct and cast them to references (probably with'static
lifetime).If an RFC is required to add these, I'd be willing to write one. However, I think they're reasonable enough to have a PR for now.