From 5a6954abbabcd644ad2639ea11e843da5b17a11d Mon Sep 17 00:00:00 2001 From: Zhuo Wang Date: Mon, 29 Jun 2020 10:15:45 -0400 Subject: [PATCH] 8246051: SIGBUS by unaligned Unsafe compare_and_swap Reviewed-by: aph --- src/hotspot/share/prims/unsafe.cpp | 2 + .../unsafe/TestUnsafeUnalignedSwap.java | 80 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java diff --git a/src/hotspot/share/prims/unsafe.cpp b/src/hotspot/share/prims/unsafe.cpp index 003b73e6785..72d81ec9d6c 100644 --- a/src/hotspot/share/prims/unsafe.cpp +++ b/src/hotspot/share/prims/unsafe.cpp @@ -950,6 +950,7 @@ UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetReference(JNIEnv *env, jobject unsafe 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_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobj 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; diff --git a/test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java b/test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java new file mode 100644 index 00000000000..1b345894e7c --- /dev/null +++ b/test/hotspot/jtreg/compiler/unsafe/TestUnsafeUnalignedSwap.java @@ -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(); + } +}