-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
The implementation in fast_local.rs
seems to violate pointer aliasing rule
#124317
Comments
The code is sound if it uses |
Edit: I think what I want to ask about is the following example, which is almost identical to the original question and passes miri's check: use std::cell::Cell;
struct S(Cell<i32>);
struct U(S);
fn main() {
let u = U(S(Cell::new(10)));
let p = &u as *const _ as *mut U;
let s = unsafe { &mut (*p).0 };
println!("{}", s.0.take());
} |
@NichtsHsu No, you should not do that. |
So this is what is happening in the standard library: There is a structure like In In
In the entire process, we haven’t touched on anything related to |
Yes, |
I should clarify that my "should not" here is that it can potentially pass miri, but my understanding is that it is brittle and relying on the operational semantics being a certain way, rather than being a "necessarily true" interpretation of Rust's semantics. Programs which are slight modifications easily become unsound, and libstd unfortunately has slight modifications done to it all the time, so... aiui this code currently passes miri because we do run miri on libstd's tests, but is not what I would consider "durable". |
( probably. ) |
…-thread-locals, r=joboet thread_local: be excruciatingly explicit in dtor code Use raw pointers to accomplish internal mutability, and clearly split references where applicable. This reduces the likelihood that any of these parts are misunderstood, either by humans or the compiler's optimizations. Fixes rust-lang#124317 r? `@joboet`
Rollup merge of rust-lang#124387 - workingjubilee:use-raw-pointers-in-thread-locals, r=joboet thread_local: be excruciatingly explicit in dtor code Use raw pointers to accomplish internal mutability, and clearly split references where applicable. This reduces the likelihood that any of these parts are misunderstood, either by humans or the compiler's optimizations. Fixes rust-lang#124317 r? ``@joboet``
rust/library/std/src/sys/thread_local/fast_local.rs
Line 210 in c1feb3e
At
#1
, acquire a pointer of type* mut u8
from a shared reference&Key<T>
, and subsequently, in thedestroy_value
, we reinterpret* mut u8
as* mut Key<T>
and use it as a mutable pointerrust/library/std/src/sys/thread_local/fast_local.rs
Line 240 in c1feb3e
At
#2
the methodtake
of the fieldinner
requires& mut self
as defined inrust/library/std/src/sys/thread_local/mod.rs
Line 98 in c1feb3e
The whole process can be that we use
&Key<T>
to acquire a& mut Key<T>
and call a mutable method on that variable, which seems problematic.The text was updated successfully, but these errors were encountered: