1 /* 2 * Copyright (c) 2019, Red Hat, Inc. 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 8216549 27 * @summary Mismatched unsafe access to non escaping object fails 28 * 29 * @modules java.base/jdk.internal.misc 30 * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation MismatchedUnsafeLoadFromNewObject 31 */ 32 33 import java.lang.reflect.Field; 34 import jdk.internal.misc.Unsafe; 35 36 public class MismatchedUnsafeLoadFromNewObject { 37 public volatile int f_int = -1; 38 public volatile int f_int2 = -1; 39 40 public static Unsafe unsafe = Unsafe.getUnsafe(); 41 public static final long f_int_off; 42 public static final long f_int2_off; 43 44 static { 45 Field f_int_field = null; 46 Field f_int2_field = null; 47 try { 48 f_int_field = MismatchedUnsafeLoadFromNewObject.class.getField("f_int"); 49 f_int2_field = MismatchedUnsafeLoadFromNewObject.class.getField("f_int2"); 50 } catch (Exception e) { 51 System.out.println("reflection failed " + e); 52 e.printStackTrace(); 53 } 54 f_int_off = unsafe.objectFieldOffset(f_int_field); 55 f_int2_off = unsafe.objectFieldOffset(f_int2_field); 56 } 57 58 static public void main(String[] args) { 59 for (int i = 0; i < 20_000; i++) { 60 byte res = test1(); 61 if (res != -1) { 62 throw new RuntimeException("Incorrect result: " + res); 63 } 64 res = test2(); 65 if (res != -1) { 66 throw new RuntimeException("Incorrect result: " + res); 67 } 68 int res2 = test3(); 69 if (res2 != -1) { 70 throw new RuntimeException("Incorrect result: " + res2); 71 } 72 } 73 } 74 75 static byte test1() { 76 MismatchedUnsafeLoadFromNewObject t = new MismatchedUnsafeLoadFromNewObject(); 77 return unsafe.getByte(t, f_int_off); 78 } 79 80 static byte test2() { 81 MismatchedUnsafeLoadFromNewObject t = new MismatchedUnsafeLoadFromNewObject(); 82 return unsafe.getByte(t, f_int_off+1); 83 } 84 85 static int test3() { 86 MismatchedUnsafeLoadFromNewObject t = new MismatchedUnsafeLoadFromNewObject(); 87 if (f_int_off < f_int2_off) { 88 return unsafe.getIntUnaligned(t, f_int_off+1); 89 } else { 90 return unsafe.getIntUnaligned(t, f_int2_off+1); 91 } 92 } 93 }