JDK-6442942 : Casts get wrongfully optimized away
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-06-23
  • Updated: 2010-04-02
  • Resolved: 2006-06-23
Related Reports
Duplicate :  
Description
% javac -g JVM16bug.java
% java -showversion -XX:CICompilerCount=1 -XX:+PrintCompilation -client JVM16bug
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b89)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b89, mixed mode)
  1       java.lang.String::hashCode (60 bytes)
  2       java.lang.String::equals (88 bytes)
  3       java.lang.String::indexOf (151 bytes)
  4       java.lang.String::charAt (33 bytes)
  5       java.lang.String::lastIndexOf (156 bytes)
  6       java.io.UnixFileSystem::normalize (75 bytes)
  7       java.lang.String::indexOf (166 bytes)
  8       JVM16bug$Memory::read (23 bytes)
  1%      JVM16bug::run @ 15 (134 bytes)
Unexpected value at i = 26499 : f62a
Unexpected value at i = 26500 : f62a
Unexpected value at i = 26501 : f62a
Unexpected value at i = 26502 : f62a
Unexpected value at i = 26503 : f62a
Unexpected value at i = 26504 : f62a
Unexpected value at i = 26505 : f62a
Unexpected value at i = 26506 : f62a
Unexpected value at i = 26507 : f62a
Unexpected value at i = 26508 : f62a
Stopping after 10 errors


/*
Start JVM16bug.java

FULL PRODUCT VERSION :
java version "1.6.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode, sharing)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The client VM optimizes away some casts, such as (short)value. In that case, the integer 'value' doesn't get sign extended so the result is wrong.

THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: No

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the included test case.

You will frequently see the value f62a in the console output instead of the correct value fffff62a. The correct value only appears at first, before the wrong compilation/optimization seems to set in.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected result:
A long list of value 'fffff62a'

Actual result:
At first, the correct value 'fffff62a'
After a while, the wrong value 'f62a'
REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

*/
public class JVM16bug {

    class Memory {
        
        int[] m = { 0xF6, 0x2A };

        public int read(int addr) {
            return (m[addr & 1] << 8) | m[(addr & 1) + 1];
        }
    }

    Memory mem = new Memory();

    public JVM16bug() {
        super();

    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        JVM16bug bug = new JVM16bug();

        for (int i = 0; i < 100000; i++) {
            bug.run();
        }
        System.out.println(System.getProperty("java.version"));
    }

    private void run() {
        int PC = 0;
        int eaExpected = 0xfffff62a;
        int errors = 0;
        final int emax = 10;
        for (int i = 0; i < 100000; i++) {
            int ea = ((short) (mem.read(PC) & 0xFFFF));
            PC += 2;
            if (ea != eaExpected) {
                System.out.println("Unexpected value at i = " + i +
                                   " : " + Integer.toHexString(ea));
                if (++errors >= emax) {
                    System.out.println("Stopping after " + errors + " errors");
                    System.exit(errors);
                }
            }
        }
    }
}
/*
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
There's no workaround I know of.
*/