Does an open-source technique exist to simulate all possible memory-access-ordering permutations to exhaustively unit test threaded C or C++ code?
REMARKS AND EXAMPLE
One answer is: "Use a memory-safe/functional/very-high-level language. Avoid C and C++." However, that answer answers a different question. Rust, Go, Erlang, Haskell, etc., are great; but my question regards unconstrained C, C++, assembly, or the like.
Another answer is: "Avoid the question by using future
, promise
, etc." However, my question regards testing methods, not coding methods. I do not seek a code to help me satisfy a paradigm, but a paradigm (or tool) to help me test my code.
The file memory-barriers.txt in the Linux kernel's source documentation affords a suitable example:
[C]onsider the following sequence of events:
CPU 1 CPU 2
=============== ===============
{ A == 1; B == 2 }
A = 3; x = B;
B = 4; y = A;
The set of accesses as seen by the memory system in the middle can be arranged in 24 different combinations:
STORE A=3, STORE B=4, y=LOAD A->3, x=LOAD B->4
STORE A=3, STORE B=4, x=LOAD B->4, y=LOAD A->3
STORE A=3, y=LOAD A->3, STORE B=4, x=LOAD B->4
STORE A=3, y=LOAD A->3, x=LOAD B->2, STORE B=4
STORE A=3, x=LOAD B->2, STORE B=4, y=LOAD A->3
STORE A=3, x=LOAD B->2, y=LOAD A->3, STORE B=4
STORE B=4, STORE A=3, y=LOAD A->3, x=LOAD B->4
STORE B=4, ...
...
and can thus result in four different combinations of values:
x == 2, y == 1
x == 2, y == 3
x == 4, y == 1
x == 4, y == 3
Rather than waiting for a Heisenbug to appear, for simple code like the above, one would rather just simulate all 24 cases in advance. Does an open-source technique exist to simulate the 24 cases?
If the technique does not exist, then could it exist? Or would combinatorial explosion render such a technique useless in practice? If the latter, then how do disciplined C++ programmers instead unit test threaded logic when coding for maximum performance?
Various authors have warned how hard it is to think right about concurrency. An open-source technique or tool to systematically expose potential faults in concrete cases would help, if it existed. Does it?
REFERENCES
- There exists this answer, but it's seven years old and anyway does not much help.
- There also exist these answers, but they're just as old and do not regard open source.
- Here is a question that wasn't precise enough.
- Here is a very interesting question with various partial answers, but it's nine years old. (No one really had an answer back then, apparently.)
- Here is someone off-site who at least understands the question.
- There exist various questions and answers on-site for Java, but Java is not what I was asking about.
- This answer re Objective C is straightforward, illuminating and recent, though not quite on target.