Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5073497

Casting long to int on 64-bit machine sometimes gives wrong result

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 6
    • hotspot
    • mustang
    • itanium
    • linux



        Name: gm110360 Date: 07/13/2004


        FULL PRODUCT VERSION :
        java version "1.5.0-beta2"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
        Java HotSpot(TM) 64-Bit Server VM (build 1.5.0-beta2-b51, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Linux amy.nz.reeltwo.com 2.6.7-rc2 #2 SMP Fri Jun 4 16:36:26 NZST 2004 x86_64 x86_64 x86_64 GNU/Linux

        CPU is Opteron.

        A DESCRIPTION OF THE PROBLEM :
        Sometimes when casting a long to an int on an int, 0 is incorrectly returned as the result.
        For example the test code below sometimes produces 0 for the result of the cast (int) 0x800000020001165fL.

        I have produced this behaviour in both 1.5.0 and 1.4.2 on 64-bit machines. I was NOT able to reproduce the problem on any 32-bit machines.

        The nature of the failure to me looks like a JIT/HotSpot optimization problem, since the fault is intermittent and highly dependent on adjacent code. In particular, in the instructions,

              final long v = t[z];
              final long oldv = v;
              t[z] = 0L;
              if (v > 0) {
                ;
              } else {
                 x = (int) v;
              }

        Possibly something gets confused underneath so that when the cast is performed the new value of the array (i.e. 0) rather than the original value (i.e. v) is used. The v>0 conditional also seems necessary to cause the problem.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile the sample code. I've used both jikes -source 1.4 and javac.

        Run the sample code below. It is important to use this exact test case since I have found that trivial variations to the test code make the problem disappear.

        If everything runs as expected then there will be no output. When the problem exists a single line appears showing the bad cast, for example:

        $ java CastBug
        Bad cast: 0 == (int) 0x800000020001165fL z was 59

        (the exact cast that fails may vary)

        You may need to run the code several times to cause the fault. On my machine the fault occurs nine times out of ten.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The expected output is no output.
        ACTUAL -
        $ java CastBug
        Bad cast: 0 == (int) 0x800000020001165fL z was 59


        REPRODUCIBILITY :
        This bug can be reproduced often.

        ---------- BEGIN SOURCE ----------
        /**
         * Demonstrate intermittent problem casting long to int.
         */
        public class CastBug {

          /** Double the size of internal arrays and repack. */
          public static void main(String[] args) {
            int i = 0;
            long[] t = new long[100];
            for (int j = 0; j < t.length; j++) {
              t[j] = 0x8000000200011624L + (long) j;
            }
            while (++i < 100000) {
              final int z = i % t.length;
              final long v = t[z];
              final long oldv = v;
              t[z] = 0L;
              if (v > 0) {
                ;
              } else {
                int secondaryIndex = (int) v;
                if (secondaryIndex == 0) {
                  System.err.println("Bad cast: " + secondaryIndex + " == (int) 0x" + Long.toHexString(v) + "L z was " + z);
                  return;
                }
                for (int j = 0; j < 2; j++) {
                  secondaryIndex = ~secondaryIndex;
                }
              }
              t[z] = oldv;
            }
          }

        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Performing the cast twice and using the second result might work, but I would'nt rely on it.
        (Incident Review ID: 284817)
        ======================================================================

              sdeversunw Steve Dever (Inactive)
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: