References:
Memory Consistency: the problem of defining how parallel threads can observe their shared memory state.
A memory consistency model is a contract between the hardware and software. The hardware promises to only reorder operations in ways allowed by the model, and in return, the software acknowledges that all such reorderings are possible and that it needs to account for them.
00
.Relaxed Memory Models
Total store ordering (TSO): on-core store buffer;
00
ARM: weak ordering + barriers.
ARM memory model is notoriously underspecified, but is essentially a form of weak odering.
Barrier: a barrier instruction forces all memory opertions before it to complete before any memory operation after it can begin. That is, a barrier instruction effectively reinstates sequential consistency at a particular point in program execution.
RISC-V Weak Memory Ordering
Defined in terms of the global memory order, a total ordering of the memory operations produced by all harts.
Under RVWMO, code running on a signle hart appears to execute in order from the perspective of other memory instructions in the same hart, but memory instructions from another hart may observe the memory instructions from the first hart being executed in a different order.
Therefore, multithreaded code may require explicit synchronization to guarantee ordering between memory instructions from different harts.
Compilers also reorder memory operations.
Program 1:
X = 0
for i in range(100):
X = 1
print X
Program 2 (an optimized version of 1):
X = 1
for i in range(100):
print X
with parallel, another threads write X as 0 during the execution of Program 1⁄2.
Program 1 outputs: 111011111...
Program 2 outputs: 111000000...
in C++/Java:
In fact, languages such as C++ and Java offer a guarantee known as sequential consistency for data-race-free programs (or the buzzwordy version, “SC for DRF”). This guarantee says that if your program has no data races, the compiler will insert all the necessary fences to preserve the appearance of sequential consistency. If your program does have data races, however, all bets are off, and the compiler is free to do whatever it likes. The intuition is that programs with data races are quite likely to be buggy, and there’s no need to provide strong guarantees for such buggy programs. If a program has deliberate data races, the programmer likely knows what they’re doing anyway, and so can be responsible for memory ordering issues themselves.
If you could revise
the fundmental principles of
computer system design
to improve security...
... what would you change?