2 ms·
That's very interesting. I would have intuitively expected that memory reordering could have been used to subvert memory safety. Can you point me to more discus
by gpderetta 5y ago
That's very interesting. I would have intuitively expected that memory reordering could have been used to subvert memory safety. Can you point me to more discussion on the topic?
- exDM69 5y agoAtomics and mutexes constrain memory reordering. Stores and loads while holding a mutex can't be reordered so that they happen outside the mutex lock (because there are compiler/cpu memory barriers in mutex lock/unlock). Given that Rust's "shared xor mutable" is guarded with mutexes, atomics or other synchronization primitives, memory reordering can't violate Rust's memory safety guarantees (assuming the sync primitives are not buggy, of course). But this does not guarantee that your program logic is correct if you are sloppy with memory ordering. If you do two atomic stores, they are not guaranteed to occur in that order if they use "relaxed" ordering. This gets more complex if you're writing device drivers and communicating with peripheral devices using memory mapped i/o.
- gpderetta 5y agoBut atomicptr can be used with relaxed constraints, so in principle a consumer can see the pointed object before it was initialized. I think rust gets away because atomic ptr is a raw ptr so it can only be dereferenced in unsafe code. So in practice relaxed atomics require unsafe where it matter (which makes sense to me).