1 /* 2 * Copyright (c) 2014, 2015, 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 * @library /testlibrary 29 * @modules java.base/sun.misc 30 * java.management 31 * @run main/othervm -Xbatch UnsafeRaw 32 */ 33 34 import com.oracle.java.testlibrary.Utils; 35 import java.util.Random; 36 37 public class UnsafeRaw { 38 public static class Tests { 39 public static int int_index(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 40 return unsafe.getInt(base + (index << 2)); 41 } 42 public static int long_index(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 43 return unsafe.getInt(base + (index << 2)); 44 } 45 public static int int_index_back_ashift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 46 return unsafe.getInt(base + (index >> 2)); 47 } 48 public static int int_index_back_lshift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 49 return unsafe.getInt(base + (index >>> 2)); 50 } 51 public static int long_index_back_ashift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 52 return unsafe.getInt(base + (index >> 2)); 53 } 54 public static int long_index_back_lshift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 55 return unsafe.getInt(base + (index >>> 2)); 56 } 57 public static int int_const_12345678_index(sun.misc.Unsafe unsafe, long base) throws Exception { 58 int idx4 = 0x12345678; 59 return unsafe.getInt(base + idx4); 60 } 61 public static int long_const_1234567890abcdef_index(sun.misc.Unsafe unsafe, long base) throws Exception { 62 long idx5 = 0x1234567890abcdefL; 63 return unsafe.getInt(base + idx5); 64 } 65 public static int int_index_mul(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 66 return unsafe.getInt(base + (index * 4)); 67 } 68 public static int long_index_mul(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 69 return unsafe.getInt(base + (index * 4)); 70 } 71 public static int int_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 72 return unsafe.getInt(base + (index * 16)); 73 } 74 public static int long_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 75 return unsafe.getInt(base + (index * 16)); 76 } 77 } 78 79 public static void main(String[] args) throws Exception { 80 sun.misc.Unsafe unsafe = Utils.getUnsafe(); 81 final int array_size = 128; 82 final int element_size = 4; 83 final int magic = 0x12345678; 84 85 Random rnd = Utils.getRandomInstance(); 86 87 long array = unsafe.allocateMemory(array_size * element_size); // 128 ints 88 long addr = array + array_size * element_size / 2; // something in the middle to work with 89 unsafe.putInt(addr, magic); 90 for (int j = 0; j < 100000; j++) { 91 if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception(); 92 if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception(); 93 if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception(); 94 if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception(); 95 { 96 long idx1 = rnd.nextLong(); 97 long addr1 = addr - (idx1 << 2); 98 if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception(); 99 } 100 { 101 long idx2 = rnd.nextLong(); 102 long addr2 = addr - (idx2 >> 2); 103 if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception(); 104 } 105 { 106 long idx3 = rnd.nextLong(); 107 long addr3 = addr - (idx3 >>> 2); 108 if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception(); 109 } 110 { 111 long idx4 = 0x12345678; 112 long addr4 = addr - idx4; 113 if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception(); 114 } 115 { 116 long idx5 = 0x1234567890abcdefL; 117 long addr5 = addr - idx5; 118 if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception(); 119 } 120 { 121 int idx6 = rnd.nextInt(); 122 long addr6 = addr - (idx6 >> 2); 123 if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception(); 124 } 125 { 126 int idx7 = rnd.nextInt(); 127 long addr7 = addr - (idx7 >>> 2); 128 if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception(); 129 } 130 { 131 int idx8 = rnd.nextInt(); 132 long addr8 = addr - (idx8 * 16); 133 if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception(); 134 } 135 { 136 long idx9 = rnd.nextLong(); 137 long addr9 = addr - (idx9 * 16); 138 if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception(); 139 } 140 } 141 } 142 }