46

With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order.

Edit: [Instruction reordering can produce counter-intuitive results. Many CPU architectures can reorder the memory interactions of machine instructions which leads to similar unexpected results even if the compiler did not change the instruction order. Thus, the term memory reordering may be a better fit than instruction reordering.]

So my question is:

Can someone provide an example Java program/snippet, that reliably shows an instruction reordering problem, that is not caused also by other synchronization issues ( such as caching/visibility or non-atomic r/w, as in my failed attempt at such a demo in my previous question )

To emphasize, I am not looking for examples of theoretical reordering issues. What I am looking for is a way to actually demonstrate them by seeing incorrect or unexpected results of a running program.

Barring a faulty behavior example, just showing actual reordering happening in the assembly of a simple program could also be nice.

19
  • 6
    It wouldn't be a bug. Instruction reordering can be visible from other threads if proper synchronization/memory barriers aren't in place. Commented Oct 4, 2018 at 14:13
  • 1
    that would be tremendously hard to show IMO on x86, but a very nice question Commented Oct 4, 2018 at 14:22
  • 4
    @Thilo I take it that OP wants an example where improper synchronization triggers a problem caused specifically by instruction reordering, not one caused by non-atomicity of reads or some other synchronization issue. There are many specific reasons improper synchronization can be a problem; they're interested in this particular one. Commented Oct 4, 2018 at 14:22
  • 4
    How do you expect to see instruction reordering happening in the bytecode? It's something done by the JIT compiler. javac does not perform any instruction reordering. Commented Oct 10, 2018 at 17:38
  • 2
    I found an example but for C++. Maybe you can translate it to Java: preshing.com/20120515/memory-reordering-caught-in-the-act Commented Oct 10, 2018 at 17:55

3 Answers 3

13

This demonstrates reordering of certain assignments, out of 1M iterations there is usually couple of printed lines.

public class App {

    public static void main(String[] args) {

        for (int i = 0; i < 1000_000; i++) {
            final State state = new State();

            // a = 0, b = 0, c = 0

            // Write values
            new Thread(() -> {
                state.a = 1;
                // a = 1, b = 0, c = 0
                state.b = 1;
                // a = 1, b = 1, c = 0
                state.c = state.a + 1;
                // a = 1, b = 1, c = 2
            }).start();

            // Read values - this should never happen, right?
            new Thread(() -> {
                // copy in reverse order so if we see some invalid state we know this is caused by reordering and not by a race condition in reads/writes
                // we don't know if the reordered statements are the writes or reads (we will se it is writes later)
                int tmpC = state.c;
                int tmpB = state.b;
                int tmpA = state.a;

                if (tmpB == 1 && tmpA == 0) {
                    System.out.println("Hey wtf!! b == 1 && a == 0");
                }
                if (tmpC == 2 && tmpB == 0) {
                    System.out.println("Hey wtf!! c == 2 && b == 0");
                }
                if (tmpC == 2 && tmpA == 0) {
                    System.out.println("Hey wtf!! c == 2 && a == 0");
                }
            }).start();

        }
        System.out.println("done");
    }

    static class State {
        int a = 0;
        int b = 0;
        int c = 0;
    }

}

Printing the assembly for the write lambda gets this output (among other..)

                                                ; {metadata('com/example/App$$Lambda$1')}
  0x00007f73b51a0100: 752b                jne       7f73b51a012dh
                                                ;*invokeinterface run
                                                ; - java.lang.Thread::run@11 (line 748)

  0x00007f73b51a0102: 458b530c            mov       r10d,dword ptr [r11+0ch]
                                                ;*getfield arg$1
                                                ; - com.example.App$$Lambda$1/1831932724::run@1
                                                ; - java.lang.Thread::run@-1 (line 747)

  0x00007f73b51a0106: 43c744d41402000000  mov       dword ptr [r12+r10*8+14h],2h
                                                ;*putfield c
                                                ; - com.example.App::lambda$main$0@17 (line 18)
                                                ; - com.example.App$$Lambda$1/1831932724::run@4
                                                ; - java.lang.Thread::run@-1 (line 747)
                                                ; implicit exception: dispatches to 0x00007f73b51a01b5
  0x00007f73b51a010f: 43c744d40c01000000  mov       dword ptr [r12+r10*8+0ch],1h
                                                ;*putfield a
                                                ; - com.example.App::lambda$main$0@2 (line 14)
                                                ; - com.example.App$$Lambda$1/1831932724::run@4
                                                ; - java.lang.Thread::run@-1 (line 747)

  0x00007f73b51a0118: 43c744d41001000000  mov       dword ptr [r12+r10*8+10h],1h
                                                ;*synchronization entry
                                                ; - java.lang.Thread::run@-1 (line 747)

  0x00007f73b51a0121: 4883c420            add       rsp,20h
  0x00007f73b51a0125: 5d                  pop       rbp
  0x00007f73b51a0126: 8505d41eb016        test      dword ptr [7f73cbca2000h],eax
                                                ;   {poll_return}
  0x00007f73b51a012c: c3                  ret
  0x00007f73b51a012d: 4181f885f900f8      cmp       r8d,0f800f985h

I am not sure why the last mov dword ptr [r12+r10*8+10h],1h is not marked with putfield b and line 16, but you can see the swapped assignment of b and c (c right after a).

EDIT: Because writes happen in order a,b,c and reads happen in reverse order c,b,a you should never see an invalid state unless the writes (or reads) are reordered.

Writes performed by single cpu (or core) are visible in same order by all processors, see e.g. this answer, which points to Intel System Programming Guide Volume 3 section 8.2.2.

Writes by a single processor are observed in the same order by all processors.

Sign up to request clarification or add additional context in comments.

11 Comments

I don't mean to be difficult, since it really seems from the assembly that the writes are reordered, but how do you rule out, for example, that c == 2 and b == 0 is not caused by b not being updated from thread cache to main memory?
State properties are neither volatile nor Atomic. And there is no happens-before relation between writes in one thread and reads in another. So the fact that sometimes results is different could be cause by improper synchronization, no?
@Ivan because there is no happens-before relation between reads and writes the jit compiler is allowed to reorder the instructions. You would never get the weird result without instruction reordering and it is not a case of normal race condition as with OP's attempt in linked question.
@frant.hartm 10x. Very nice.
@PiotrMichalczyk no, this is not possible on Intel architecture. All writes are observed in order.
|
9
+100

Test

I wrote a JUnit 5 test that checks whether instruction reordering took place after two threads terminate.

  • The test must pass if no instruction reordering happened.
  • The test must fail if instruction reordering occurred.

public class InstructionReorderingTest {

    static int x, y, a, b;

    @org.junit.jupiter.api.BeforeEach
    public void init() {
        x = y = a = b = 0;
    }

    @org.junit.jupiter.api.Test
    public void test() throws InterruptedException {
        Thread threadA = new Thread(() -> {
            a = 1;
            x = b;
        });
        Thread threadB = new Thread(() -> {
            b = 1;
            y = a;
        });

        threadA.start();
        threadB.start();

        threadA.join();
        threadB.join();

        org.junit.jupiter.api.Assertions.assertFalse(x == 0 && y == 0);
    }

}

Results

I ran the test until it fails several times. The results are as follows:

InstructionReorderingTest.test [*] (12s 222ms): 29144 total, 1 failed, 29143 passed.
InstructionReorderingTest.test [*] (26s 678ms): 69513 total, 1 failed, 69512 passed.
InstructionReorderingTest.test [*] (12s 161ms): 27878 total, 1 failed, 27877 passed.

Explanation

The results we expect are

  • x = 0, y = 1: threadA runs to completion before threadB starts.
  • x = 1, y = 0: threadB runs to completion before threadA starts.
  • x = 1, y = 1: their instructions are interleaved.

No one can expect x = 0, y = 0, which may happen as the test results showed.

The actions in each thread have no dataflow dependence on each other, and accordingly can be executed out of order. (Even if they are executed in order, the timing by which caches are flushed to main memory can make it appear, from the perspective of threadB, that the assignments in threadA occurred in the opposite order.)

enter image description here Java Concurrency in Practice, Brian Goetz

8 Comments

I think the assert inspires more confidence this way: Assert.assertFalse(x == 0 && y == 0 && a == 1 && b == 1); // Just to make sure some other weirdness is not happening
Thanks for your answer. I modified it to use a loop and dropped JUnit, and it eventually it fails on my machine as well ( though at times it passes 10 mil iteratrions ). I did wonder how to rule out an unflushed cache as the reason, but this gee.cs.oswego.edu/dl/cpj/jmm.html states "if one thread synchronizes on the termination of another thread using Thread.join, then it is guaranteed to see the effects made by that thread"
Althogh it looks great, it is an example of concurrency and threading, NOT instruction reordering... Those are completely different things. Reorder takes place in a single code block and therefore in a single thread not across multiple ones.
@Mark if you look at the figure, you might notice the reordering of a = 1 and x = b in threadA which is "a single code block"
@Mark and we need a multithreaded context to have different perspectives on the code where reordering may occur potentially
|
-1

For single thread executions reordering is not a problem at all, because of Java Memory Model (JMM) (guarantee that any reads actions related to writes are total ordered) and cannot lead to unexpected results.

For concurrent execution, rules are completely different and things gets more complicated to understand (even by providing simple example which will raise even more questions). But even this are totally described by JMM with all corner cases, so, unexpected results also forbidden. Generally, forbidden if all barriers are placed right.

For better understanding reordering I strongly recommend this subject with plenty examples insides.

1 Comment

I think you mean instruction reordering isn't a problem if your code isn't already broken. Yeah, I think that's true; anything inter-thread memory ordering you get from lack of instruction reordering can only help you on a strongly-ordered asm memory model like x86, so such a program that happened to work on x86 (with one JVM JIT version / options) wouldn't work on ARM, MIPS, PowerPC, etc., if you didn't tell the compiler about the necessary memory ordering semantics so it could use special instructions. preshing.com/20120930/weak-vs-strong-memory-models

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.