changeset 59996:dc7dd368849b

8246051: SIGBUS by unaligned Unsafe compare_and_swap Reviewed-by: aph Contributed-by: zhuoren.wz@alibaba-inc.com
author aph
date Mon, 29 Jun 2020 10:15:45 -0400
parents 24057288b9ad
children 6e6e6635a970
files src/hotspot/share/prims/unsafe.cpp test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java
diffstat 2 files changed, 82 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/prims/unsafe.cpp	Mon Jun 29 14:22:01 2020 -0700
+++ b/src/hotspot/share/prims/unsafe.cpp	Mon Jun 29 10:15:45 2020 -0400
@@ -950,6 +950,7 @@
 
 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) {
   oop p = JNIHandles::resolve(obj);
+  GuardUnsafeAccess guard(thread);
   if (p == NULL) {
     volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
     return RawAccess<>::atomic_cmpxchg(addr, e, x) == e;
@@ -961,6 +962,7 @@
 
 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) {
   oop p = JNIHandles::resolve(obj);
+  GuardUnsafeAccess guard(thread);
   if (p == NULL) {
     volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
     return RawAccess<>::atomic_cmpxchg(addr, e, x) == e;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java	Mon Jun 29 10:15:45 2020 -0400
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Alibaba designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+/* @test
+ * @library / /test/lib
+ * @bug 8246051
+ * @summary A test for SIGBUS in aarch64 by unalgined unsafe access
+ * @requires os.arch=="aarch64"
+ * @run main/othervm/timeout=200 -XX:-Inline TestUnsafeUnalignedSwap
+ */
+
+import sun.misc.Unsafe;
+import java.lang.reflect.Field;
+import java.util.*;
+import jdk.test.lib.Asserts;
+
+public class TestUnsafeUnalignedSwap {
+    private final static Unsafe U;
+    private static long sum = 4;
+    static volatile long[] arrayLong = new long[1001];
+    static volatile int[] arrayInt = new int[1001];
+    static {
+        try {
+            Field f = Unsafe.class.getDeclaredField("theUnsafe");
+            f.setAccessible(true);
+            U = (Unsafe) f.get(null);
+        } catch (ReflectiveOperationException e) {
+            throw new InternalError(e);
+        }
+    }
+    // Bug 8246051 : Unsafe.compareAndSwapLong should not crash
+    public static void testCompareAndSwapLong() {
+        try {
+            if (U.compareAndSwapLong(arrayLong, Unsafe.ARRAY_LONG_BASE_OFFSET + 1, 3243, 2334)) {
+                sum++;
+            } else {
+                sum--;
+            }
+        } catch (InternalError e) {
+            System.out.println(e.getMessage());
+        }
+    }
+    public static void testCompareAndSwapInt() {
+        try {
+            if (U.compareAndSwapInt(arrayInt, Unsafe.ARRAY_INT_BASE_OFFSET + 1, 3243, 2334)) {
+                sum++;
+            } else {
+                sum--;
+            }
+        } catch (InternalError e) {
+            System.out.println(e.getMessage());
+        }
+    }
+    public static void test() {
+        testCompareAndSwapLong();
+        testCompareAndSwapInt();
+    }
+    public static void main(String[] args) {
+        test();
+    }
+}