Created
November 30, 2012 09:52
-
-
Save anonymous/4174858 to your computer and use it in GitHub Desktop.
exhibit JIT compile bug in jdk 1.6.0_21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.Arrays; | |
import sun.misc.Unsafe; | |
public class Inline | |
{ | |
private static final int BASE_LENGTH = 7; | |
private static final int[] LENGTHS = new int[] {1, 2, 3, 4, 5, 6}; | |
private static final Unsafe UNSAFE; | |
private static final long ARRAY_BASE_OFFSET; | |
static | |
{ | |
try | |
{ | |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); | |
f.setAccessible(true); | |
UNSAFE = (Unsafe) f.get(null); | |
ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class); | |
} | |
catch (Exception e) | |
{ | |
throw new AssertionError(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
final Inline inline = new Inline(); | |
final byte[] targetBuffer = new byte[64]; | |
final byte[] sourceBuffer = new byte[64]; | |
Arrays.fill(sourceBuffer, (byte) 7); | |
for(int i = 0; i < 500000; i++) | |
{ | |
writeInt(targetBuffer, 0, i % 15); | |
} | |
System.out.println(readInt(targetBuffer, 0)); | |
for(int i = 0; i < 500000; i++) | |
{ | |
if(!inline.createArray(targetBuffer, sourceBuffer)) | |
{ | |
System.err.println("Failed"); | |
return; | |
} | |
} | |
} | |
private boolean createArray(final byte[] buffer, final byte[] sourceBuffer) | |
{ | |
try | |
{ | |
final int length = getLength(); | |
writeInt(buffer, 0, length); | |
System.arraycopy(sourceBuffer, 0, buffer, 0, length); | |
} | |
catch(RuntimeException e) | |
{ | |
e.printStackTrace(); | |
return false; | |
} | |
return true; | |
} | |
private int getLength() | |
{ | |
return BASE_LENGTH + LENGTHS[((int)System.currentTimeMillis()) % LENGTHS.length]; | |
} | |
private static void writeInt(byte[] buffer, int offset, int i) | |
{ | |
UNSAFE.putInt(buffer, ARRAY_BASE_OFFSET + offset, toBigEndian(i)); | |
} | |
private static int readInt(byte[] buffer, int offset) | |
{ | |
return toBigEndian(UNSAFE.getInt(buffer, ARRAY_BASE_OFFSET + offset)); | |
} | |
private static int toBigEndian(int i) | |
{ | |
return Integer.reverseBytes(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment