Tue, 23 Sep 2014 17:24:34 -0700
8059002: 8058744 needs a test case
Summary: Added a test case the UnsafeRawOp intrinsics
Reviewed-by: kvn
test/compiler/unsafe/UnsafeRaw.java | file | annotate | diff | comparison | revisions |
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/unsafe/UnsafeRaw.java Tue Sep 23 17:24:34 2014 -0700 1.3 @@ -0,0 +1,140 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8058744 1.30 + * @summary Invalid pattern-matching of address computations in raw unsafe 1.31 + * @library /testlibrary 1.32 + * @run main/othervm -Xbatch UnsafeRaw 1.33 + */ 1.34 + 1.35 +import com.oracle.java.testlibrary.Utils; 1.36 +import java.util.Random; 1.37 + 1.38 +public class UnsafeRaw { 1.39 + public static class Tests { 1.40 + public static int int_index(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 1.41 + return unsafe.getInt(base + (index << 2)); 1.42 + } 1.43 + public static int long_index(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 1.44 + return unsafe.getInt(base + (index << 2)); 1.45 + } 1.46 + public static int int_index_back_ashift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 1.47 + return unsafe.getInt(base + (index >> 2)); 1.48 + } 1.49 + public static int int_index_back_lshift(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 1.50 + return unsafe.getInt(base + (index >>> 2)); 1.51 + } 1.52 + public static int long_index_back_ashift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 1.53 + return unsafe.getInt(base + (index >> 2)); 1.54 + } 1.55 + public static int long_index_back_lshift(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 1.56 + return unsafe.getInt(base + (index >>> 2)); 1.57 + } 1.58 + public static int int_const_12345678_index(sun.misc.Unsafe unsafe, long base) throws Exception { 1.59 + int idx4 = 0x12345678; 1.60 + return unsafe.getInt(base + idx4); 1.61 + } 1.62 + public static int long_const_1234567890abcdef_index(sun.misc.Unsafe unsafe, long base) throws Exception { 1.63 + long idx5 = 0x1234567890abcdefL; 1.64 + return unsafe.getInt(base + idx5); 1.65 + } 1.66 + public static int int_index_mul(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 1.67 + return unsafe.getInt(base + (index * 4)); 1.68 + } 1.69 + public static int long_index_mul(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 1.70 + return unsafe.getInt(base + (index * 4)); 1.71 + } 1.72 + public static int int_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, int index) throws Exception { 1.73 + return unsafe.getInt(base + (index * 16)); 1.74 + } 1.75 + public static int long_index_mul_scale_16(sun.misc.Unsafe unsafe, long base, long index) throws Exception { 1.76 + return unsafe.getInt(base + (index * 16)); 1.77 + } 1.78 + } 1.79 + 1.80 + public static void main(String[] args) throws Exception { 1.81 + sun.misc.Unsafe unsafe = Utils.getUnsafe(); 1.82 + final int array_size = 128; 1.83 + final int element_size = 4; 1.84 + final int magic = 0x12345678; 1.85 + 1.86 + Random rnd = new Random(); 1.87 + 1.88 + long array = unsafe.allocateMemory(array_size * element_size); // 128 ints 1.89 + long addr = array + array_size * element_size / 2; // something in the middle to work with 1.90 + unsafe.putInt(addr, magic); 1.91 + for (int j = 0; j < 100000; j++) { 1.92 + if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception(); 1.93 + if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception(); 1.94 + if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception(); 1.95 + if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception(); 1.96 + { 1.97 + long idx1 = rnd.nextLong(); 1.98 + long addr1 = addr - (idx1 << 2); 1.99 + if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception(); 1.100 + } 1.101 + { 1.102 + long idx2 = rnd.nextLong(); 1.103 + long addr2 = addr - (idx2 >> 2); 1.104 + if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception(); 1.105 + } 1.106 + { 1.107 + long idx3 = rnd.nextLong(); 1.108 + long addr3 = addr - (idx3 >>> 2); 1.109 + if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception(); 1.110 + } 1.111 + { 1.112 + long idx4 = 0x12345678; 1.113 + long addr4 = addr - idx4; 1.114 + if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception(); 1.115 + } 1.116 + { 1.117 + long idx5 = 0x1234567890abcdefL; 1.118 + long addr5 = addr - idx5; 1.119 + if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception(); 1.120 + } 1.121 + { 1.122 + int idx6 = rnd.nextInt(); 1.123 + long addr6 = addr - (idx6 >> 2); 1.124 + if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception(); 1.125 + } 1.126 + { 1.127 + int idx7 = rnd.nextInt(); 1.128 + long addr7 = addr - (idx7 >>> 2); 1.129 + if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception(); 1.130 + } 1.131 + { 1.132 + int idx8 = rnd.nextInt(); 1.133 + long addr8 = addr - (idx8 * 16); 1.134 + if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception(); 1.135 + } 1.136 + { 1.137 + long idx9 = rnd.nextLong(); 1.138 + long addr9 = addr - (idx9 * 16); 1.139 + if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception(); 1.140 + } 1.141 + } 1.142 + } 1.143 +}