1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8058744 27 * @summary Invalid pattern-matching of address computations in raw unsafe 28 * @run main/othervm -Xbatch UnsafeRaw 29 * 30 */ 31 32 import sun.misc.Unsafe; 33 import java.util.Random; 34 35 public class UnsafeRaw { 36 private static sun.misc.Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException { 37 java.lang.reflect.Field f = sun.misc 38 .Unsafe.class.getDeclaredField("theUnsafe"); 39 f.setAccessible(true); 40 sun.misc.Unsafe unsafe = (Unsafe) f.get(null); 41 return unsafe; 42 } 43 public static class Tests { 44 public static int int_index(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 45 return unsafe.getInt(base + (index << 2)); 46 } 47 public static int long_index(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 48 return unsafe.getInt(base + (index << 2)); 49 } 50 public static int int_index_back_ashift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 51 return unsafe.getInt(base + (index >> 2)); 52 } 53 public static int int_index_back_lshift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 54 return unsafe.getInt(base + (index >>> 2)); 55 } 56 public static int long_index_back_ashift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 57 return unsafe.getInt(base + (index >> 2)); 58 } 59 public static int long_index_back_lshift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 60 return unsafe.getInt(base + (index >>> 2)); 61 } 62 public static int int_const_12345678_index(sun.misc.Unsafe unsafe, long base) throws Exception { 63 int idx4 = 0x12345678; 64 return unsafe.getInt(base + idx4); 65 } 66 public static int long_const_1234567890abcdef_index(sun.misc.Unsafe unsafe, long base) throws Exception { 67 long idx5 = 0x1234567890abcdefL; 68 return unsafe.getInt(base + idx5); 69 } 70 } 71 72 public static void main(String[] args) throws Exception { 73 sun.misc.Unsafe unsafe = getUnsafe(); 74 final int array_size = 128; 75 final int element_size = 4; 76 final int magic = 0x12345678; 77 78 Random rnd = new Random(); 79 80 long array = unsafe.allocateMemory(array_size * element_size); // 128 ints 81 long addr = array + array_size * element_size / 2; // something in the middle to work with 82 unsafe.putInt(addr, magic); 83 for (int j = 0; j < 100000; j++) { 84 if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception(); 85 if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception(); 86 { 87 long idx1 = rnd.nextLong(); 88 long addr1 = addr - (idx1 << 2); 89 if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception(); 90 } 91 { 92 long idx2 = rnd.nextLong(); 93 long addr2 = addr - (idx2 >> 2); 94 if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception(); 95 } 96 { 97 long idx3 = rnd.nextLong(); 98 long addr3 = addr - (idx3 >>> 2); 99 if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception(); 100 } 101 { 102 long idx4 = 0x12345678; 103 long addr4 = addr - idx4; 104 if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception(); 105 } 106 { 107 long idx5 = 0x1234567890abcdefL; 108 long addr5 = addr - idx5; 109 if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception(); 110 } 111 { 112 int idx6 = rnd.nextInt(); 113 long addr6 = addr - (idx6 >> 2); 114 if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception(); 115 } 116 { 117 int idx7 = rnd.nextInt(); 118 long addr7 = addr - (idx7 >>> 2); 119 if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception(); 120 } 121 } 122 } 123 }