FIX: JIT Loses Shift Results in Arrays

ID: Q214621


The information in this article applies to:


SYMPTOMS

When you perform a left-shift, a right-shift, or execute code that would result in a right-shift (such as multiplication on an element of a byte or short array where the result is immediately assigned back to the array), you receive incorrect results. This problem only occurs in the Just In Time (JIT) compiler found in versions 3100 - 3158 of the Microsoft virtual machine.


CAUSE

This is caused by a bug in the JIT where certain shift operations are incorrectly optimized. As a result, the shift operation is not executed, effectively "commenting out" the instructions.


RESOLUTION

Create a temporary variable where the results of the operation can be stored and then assigned back to the array element.

Another possible workaround is to turn off the JIT, although this can cause some Java programs to run at a slower speed.

For additional information about the JIT, please see the following article in the Microsoft Knowledge Base:

Q154580 Description of the Just-In- Time Compiler


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

This problem was corrected in Microsoft virtual machine builds 3165 and greater.


MORE INFORMATION

Steps to Reproduce Behavior

The following code reproduces the JIT bug:

public class jitbug
{
   static byte b[] = new byte[1];

   public static void main(String args[])
   {
      System.out.println("Reproduces JIT Bug");

      System.out.println ();
      System.out.println("Multiple by 8:");
      b[0]=8;
      b[0] = (byte) (b[0] * 8);
      System.out.println("b[0] = b[0] * 8");
      System.out.println("Results = " + b[0]);

      System.out.println ();
      System.out.println("Left-shift by 3:");
      b[0]=8;
      b[0] = (byte) (b[0] << 3);
      System.out.println("b[0] = b[0] << 3");
      System.out.println("Results = " + b[0]);

      System.out.println ();
      System.out.println("Right-shift by 3:");
      b[0]=8;
      b[0] = (byte) (b[0] >> 3);
      System.out.println("b[0] = b[0] >> 3");
      System.out.println("Results = " + b[0]);

//  Remove the comment block from around the following line to
//  see the workaround.
/*
      System.out.println ();
      System.out.println("Workaround to JIT Bug");

      System.out.println ();
      System.out.println("Multiple by 8:");
      b[0]=8;
      byte answer = (byte) (b[0] * 8);
      b[0]=answer;
      System.out.println("answer = (byte) (b[0] * 8)");
      System.out.println("b[0] = answer");
      System.out.println("Results = " + b[0]);

      System.out.println ();
      System.out.println("Left-shift by 3:");
      b[0]=8;
      byte answer2 = (byte) (b[0] << 3);
      b[0]=answer2;
      System.out.println("answer2 = (byte) (b[0] << 3)");
      System.out.println("b[0] = answer2");
      System.out.println("Results = " + b[0]);

      System.out.println ();
      System.out.println("Right-shift by 3:");
      b[0]=8;
      byte answer3 = (byte) (b[0] >> 3);
      b[0]=answer3;
      System.out.println("answer3 = (byte) (b[0] >> 3)");
      System.out.println("b[0] = answer3");
      System.out.println("Results = " + b[0]);
*/ 
   }
} 


NOTE: This issue applies to any algorithm that performs bit operations in the manner shown above, which is common in image manipulation.


REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following pages on the Microsoft Technical Support site:

Support Highlights for Visual J++

Support Highlights for Java

Additional query words: Microsoft VM


Keywords          : kbJIT kbGrpJava 
Version           : WINDOWS:
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: August 8, 1999