Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8246051: SIGBUS by unaligned Unsafe compare_and_swap
Reviewed-by: aph
  • Loading branch information
sandlerwang authored and Andrew Haley committed Jun 29, 2020
1 parent 144267d commit a7e352b554078a11195b434ea9a601210b5b33cd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
@@ -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;
@@ -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();
}
}

0 comments on commit a7e352b

Please sign in to comment.