1 /* 2 * Copyright (c) 2018, 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 8211320 27 * @summary Aarch64: unsafe.compareAndSetByte() and unsafe.compareAndSetShort() c2 intrinsics broken with negative expected value 28 * 29 * @modules java.base/jdk.internal.misc 30 * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation CASandCAEwithNegExpected 31 */ 32 33 import java.lang.reflect.Field; 34 import jdk.internal.misc.Unsafe; 35 36 public class CASandCAEwithNegExpected { 37 public volatile int f_int = -1; 38 public volatile long f_long = -1; 39 public volatile byte f_byte = -1; 40 public volatile short f_short = -1; 41 42 public static Unsafe unsafe = Unsafe.getUnsafe(); 43 public static Field f_int_field; 44 public static Field f_long_field; 45 public static Field f_byte_field; 46 public static Field f_short_field; 47 public static long f_int_off; 48 public static long f_long_off; 49 public static long f_byte_off; 50 public static long f_short_off; 51 52 static { 53 try { 54 f_int_field = CASandCAEwithNegExpected.class.getField("f_int"); 55 f_long_field = CASandCAEwithNegExpected.class.getField("f_long"); 56 f_byte_field = CASandCAEwithNegExpected.class.getField("f_byte"); 57 f_short_field = CASandCAEwithNegExpected.class.getField("f_short"); 58 f_int_off = unsafe.objectFieldOffset(f_int_field); 59 f_long_off = unsafe.objectFieldOffset(f_long_field); 60 f_byte_off = unsafe.objectFieldOffset(f_byte_field); 61 f_short_off = unsafe.objectFieldOffset(f_short_field); 62 } catch (Exception e) { 63 System.out.println("reflection failed " + e); 64 e.printStackTrace(); 65 } 66 } 67 68 static public void main(String[] args) { 69 CASandCAEwithNegExpected t = new CASandCAEwithNegExpected(); 70 for (int i = 0; i < 20_000; i++) { 71 t.test(); 72 } 73 } 74 75 // check proper handling of sign extension of expected value in comparison 76 void test() { 77 f_int = -1; 78 f_long = -1; 79 f_byte = -1; 80 f_short = -1; 81 82 unsafe.compareAndSetInt(this, f_int_off, -1, 42); 83 if (f_int != 42) { 84 throw new RuntimeException("CAS failed"); 85 } 86 unsafe.compareAndSetLong(this, f_long_off, -1, 42); 87 if (f_long != 42) { 88 throw new RuntimeException("CAS failed"); 89 } 90 unsafe.compareAndSetByte(this, f_byte_off, (byte)-1, (byte)42); 91 if (f_byte != 42) { 92 throw new RuntimeException("CAS failed"); 93 } 94 unsafe.compareAndSetShort(this, f_short_off, (short)-1, (short)42); 95 if (f_short != 42) { 96 throw new RuntimeException("CAS failed"); 97 } 98 99 f_int = -1; 100 f_long = -1; 101 f_byte = -1; 102 f_short = -1; 103 104 unsafe.compareAndExchangeInt(this, f_int_off, -1, 42); 105 if (f_int != 42) { 106 throw new RuntimeException("CAE failed"); 107 } 108 unsafe.compareAndExchangeLong(this, f_long_off, -1, 42); 109 if (f_long != 42) { 110 throw new RuntimeException("CAE failed"); 111 } 112 unsafe.compareAndExchangeByte(this, f_byte_off, (byte)-1, (byte)42); 113 if (f_byte != 42) { 114 throw new RuntimeException("CAE failed"); 115 } 116 unsafe.compareAndExchangeShort(this, f_short_off, (short)-1, (short)42); 117 if (f_short != 42) { 118 throw new RuntimeException("CAE failed"); 119 } 120 } 121 122 }