mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
8143628: Fork sun.misc.Unsafe and jdk.internal.misc.Unsafe native method tables
Reviewed-by: shade, dholmes, alanb, chegar, mchung, roland
This commit is contained in:
parent
1f2a9c1407
commit
4d4c7ad974
22 changed files with 3690 additions and 9 deletions
|
@ -107,7 +107,8 @@ char* NativeLookup::long_jni_name(const methodHandle& method) {
|
|||
}
|
||||
|
||||
extern "C" {
|
||||
void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls);
|
||||
void JNICALL JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafecls);
|
||||
void JNICALL JVM_RegisterSunMiscUnsafeMethods(JNIEnv *env, jclass unsafecls);
|
||||
void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls);
|
||||
void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
|
||||
void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass);
|
||||
|
@ -121,8 +122,8 @@ extern "C" {
|
|||
#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
|
||||
|
||||
static JNINativeMethod lookup_special_native_methods[] = {
|
||||
{ CC"Java_jdk_internal_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterUnsafeMethods) },
|
||||
{ CC"Java_sun_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterUnsafeMethods) },
|
||||
{ CC"Java_jdk_internal_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterJDKInternalMiscUnsafeMethods) },
|
||||
{ CC"Java_sun_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterSunMiscUnsafeMethods) },
|
||||
{ CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) },
|
||||
{ CC"Java_sun_misc_Perf_registerNatives", NULL, FN_PTR(JVM_RegisterPerfMethods) },
|
||||
{ CC"Java_sun_hotspot_WhiteBox_registerNatives", NULL, FN_PTR(JVM_RegisterWhiteBoxMethods) },
|
||||
|
|
|
@ -1227,8 +1227,76 @@ UNSAFE_END
|
|||
{CC "put" #Byte, CC "(" ADR#B ")V", FN_PTR(Unsafe_SetNative##Byte)}
|
||||
|
||||
|
||||
static JNINativeMethod sun_misc_Unsafe_methods[] = {
|
||||
{CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)},
|
||||
{CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)},
|
||||
{CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)},
|
||||
{CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObjectVolatile)},
|
||||
|
||||
static JNINativeMethod methods[] = {
|
||||
{CC "getUncompressedObject", CC "(" ADR ")" OBJ, FN_PTR(Unsafe_GetUncompressedObject)},
|
||||
{CC "getJavaMirror", CC "(" ADR ")" CLS, FN_PTR(Unsafe_GetJavaMirror)},
|
||||
{CC "getKlassPointer", CC "(" OBJ ")" ADR, FN_PTR(Unsafe_GetKlassPointer)},
|
||||
|
||||
DECLARE_GETPUTOOP(Boolean, Z),
|
||||
DECLARE_GETPUTOOP(Byte, B),
|
||||
DECLARE_GETPUTOOP(Short, S),
|
||||
DECLARE_GETPUTOOP(Char, C),
|
||||
DECLARE_GETPUTOOP(Int, I),
|
||||
DECLARE_GETPUTOOP(Long, J),
|
||||
DECLARE_GETPUTOOP(Float, F),
|
||||
DECLARE_GETPUTOOP(Double, D),
|
||||
|
||||
DECLARE_GETPUTNATIVE(Byte, B),
|
||||
DECLARE_GETPUTNATIVE(Short, S),
|
||||
DECLARE_GETPUTNATIVE(Char, C),
|
||||
DECLARE_GETPUTNATIVE(Int, I),
|
||||
DECLARE_GETPUTNATIVE(Long, J),
|
||||
DECLARE_GETPUTNATIVE(Float, F),
|
||||
DECLARE_GETPUTNATIVE(Double, D),
|
||||
|
||||
{CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)},
|
||||
{CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)},
|
||||
|
||||
{CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)},
|
||||
{CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)},
|
||||
{CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)},
|
||||
|
||||
{CC "objectFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset)},
|
||||
{CC "staticFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset)},
|
||||
{CC "staticFieldBase", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
|
||||
{CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)},
|
||||
{CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)},
|
||||
{CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)},
|
||||
{CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)},
|
||||
{CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)},
|
||||
|
||||
{CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)},
|
||||
{CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)},
|
||||
{CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)},
|
||||
{CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
|
||||
{CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
|
||||
{CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
|
||||
{CC "putOrderedObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetOrderedObject)},
|
||||
{CC "putOrderedInt", CC "(" OBJ "JI)V", FN_PTR(Unsafe_SetOrderedInt)},
|
||||
{CC "putOrderedLong", CC "(" OBJ "JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
|
||||
{CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)},
|
||||
{CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)},
|
||||
|
||||
{CC "getLoadAverage", CC "([DI)I", FN_PTR(Unsafe_Loadavg)},
|
||||
|
||||
{CC "copyMemory", CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory)},
|
||||
{CC "setMemory", CC "(" OBJ "JJB)V", FN_PTR(Unsafe_SetMemory)},
|
||||
|
||||
{CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
|
||||
|
||||
{CC "shouldBeInitialized",CC "(" CLS ")Z", FN_PTR(Unsafe_ShouldBeInitialized)},
|
||||
|
||||
{CC "loadFence", CC "()V", FN_PTR(Unsafe_LoadFence)},
|
||||
{CC "storeFence", CC "()V", FN_PTR(Unsafe_StoreFence)},
|
||||
{CC "fullFence", CC "()V", FN_PTR(Unsafe_FullFence)},
|
||||
};
|
||||
|
||||
static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
|
||||
{CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)},
|
||||
{CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)},
|
||||
{CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)},
|
||||
|
@ -1316,17 +1384,27 @@ static JNINativeMethod methods[] = {
|
|||
#undef DECLARE_GETPUTNATIVE
|
||||
|
||||
|
||||
// This one function is exported, used by NativeLookup.
|
||||
// These two functions are exported, used by NativeLookup.
|
||||
// The Unsafe_xxx functions above are called only from the interpreter.
|
||||
// The optimizer looks at names and signatures to recognize
|
||||
// individual functions.
|
||||
|
||||
JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafeclass))
|
||||
UnsafeWrapper("JVM_RegisterUnsafeMethods");
|
||||
JVM_ENTRY(void, JVM_RegisterSunMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
|
||||
UnsafeWrapper("JVM_RegisterSunMiscUnsafeMethods");
|
||||
{
|
||||
ThreadToNativeFromVM ttnfv(thread);
|
||||
|
||||
int ok = env->RegisterNatives(unsafeclass, methods, sizeof(methods)/sizeof(JNINativeMethod));
|
||||
guarantee(ok == 0, "register unsafe natives");
|
||||
int ok = env->RegisterNatives(unsafeclass, sun_misc_Unsafe_methods, sizeof(sun_misc_Unsafe_methods)/sizeof(JNINativeMethod));
|
||||
guarantee(ok == 0, "register sun.misc.Unsafe natives");
|
||||
}
|
||||
JVM_END
|
||||
|
||||
JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
|
||||
UnsafeWrapper("JVM_RegisterJDKInternalMiscUnsafeMethods");
|
||||
{
|
||||
ThreadToNativeFromVM ttnfv(thread);
|
||||
|
||||
int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod));
|
||||
guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives");
|
||||
}
|
||||
JVM_END
|
||||
|
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for boolean
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestBoolean
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestBoolean {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(boolean[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(boolean[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static boolean static_v;
|
||||
|
||||
boolean v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestBoolean t = new JdkInternalMiscUnsafeAccessTestBoolean();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
boolean[] array = new boolean[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putBoolean(base, offset, true);
|
||||
boolean x = UNSAFE.getBoolean(base, offset);
|
||||
assertEquals(x, true, "set boolean value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putBooleanVolatile(base, offset, false);
|
||||
boolean x = UNSAFE.getBooleanVolatile(base, offset);
|
||||
assertEquals(x, false, "putVolatile boolean value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for byte
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestByte
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestByte {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestByte.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestByte.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(byte[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static byte static_v;
|
||||
|
||||
byte v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestByte t = new JdkInternalMiscUnsafeAccessTestByte();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
byte[] array = new byte[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putByte(base, offset, (byte)1);
|
||||
byte x = UNSAFE.getByte(base, offset);
|
||||
assertEquals(x, (byte)1, "set byte value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putByteVolatile(base, offset, (byte)2);
|
||||
byte x = UNSAFE.getByteVolatile(base, offset);
|
||||
assertEquals(x, (byte)2, "putVolatile byte value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putByte(address, (byte)1);
|
||||
byte x = UNSAFE.getByte(address);
|
||||
assertEquals(x, (byte)1, "set byte value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for char
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestChar
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestChar {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestChar.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestChar.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(char[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(char[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static char static_v;
|
||||
|
||||
char v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestChar t = new JdkInternalMiscUnsafeAccessTestChar();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
char[] array = new char[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putChar(base, offset, 'a');
|
||||
char x = UNSAFE.getChar(base, offset);
|
||||
assertEquals(x, 'a', "set char value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putCharVolatile(base, offset, 'b');
|
||||
char x = UNSAFE.getCharVolatile(base, offset);
|
||||
assertEquals(x, 'b', "putVolatile char value");
|
||||
}
|
||||
|
||||
|
||||
// Unaligned
|
||||
{
|
||||
UNSAFE.putCharUnaligned(base, offset, 'b');
|
||||
char x = UNSAFE.getCharUnaligned(base, offset);
|
||||
assertEquals(x, 'b', "putUnaligned char value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putCharUnaligned(base, offset, 'a', true);
|
||||
char x = UNSAFE.getCharUnaligned(base, offset, true);
|
||||
assertEquals(x, 'a', "putUnaligned big endian char value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putCharUnaligned(base, offset, 'b', false);
|
||||
char x = UNSAFE.getCharUnaligned(base, offset, false);
|
||||
assertEquals(x, 'b', "putUnaligned little endian char value");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putChar(address, 'a');
|
||||
char x = UNSAFE.getChar(address);
|
||||
assertEquals(x, 'a', "set char value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for double
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestDouble
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestDouble {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestDouble.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestDouble.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(double[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(double[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static double static_v;
|
||||
|
||||
double v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestDouble t = new JdkInternalMiscUnsafeAccessTestDouble();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
double[] array = new double[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putDouble(base, offset, 1.0d);
|
||||
double x = UNSAFE.getDouble(base, offset);
|
||||
assertEquals(x, 1.0d, "set double value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putDoubleVolatile(base, offset, 2.0d);
|
||||
double x = UNSAFE.getDoubleVolatile(base, offset);
|
||||
assertEquals(x, 2.0d, "putVolatile double value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putDouble(address, 1.0d);
|
||||
double x = UNSAFE.getDouble(address);
|
||||
assertEquals(x, 1.0d, "set double value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for float
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestFloat
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestFloat {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestFloat.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestFloat.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(float[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(float[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static float static_v;
|
||||
|
||||
float v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestFloat t = new JdkInternalMiscUnsafeAccessTestFloat();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
float[] array = new float[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putFloat(base, offset, 1.0f);
|
||||
float x = UNSAFE.getFloat(base, offset);
|
||||
assertEquals(x, 1.0f, "set float value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putFloatVolatile(base, offset, 2.0f);
|
||||
float x = UNSAFE.getFloatVolatile(base, offset);
|
||||
assertEquals(x, 2.0f, "putVolatile float value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putFloat(address, 1.0f);
|
||||
float x = UNSAFE.getFloat(address);
|
||||
assertEquals(x, 1.0f, "set float value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for int
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestInt
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestInt {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestInt.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestInt.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(int[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(int[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static int static_v;
|
||||
|
||||
int v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestInt t = new JdkInternalMiscUnsafeAccessTestInt();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
int[] array = new int[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1, "set int value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putIntVolatile(base, offset, 2);
|
||||
int x = UNSAFE.getIntVolatile(base, offset);
|
||||
assertEquals(x, 2, "putVolatile int value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedInt(base, offset, 1);
|
||||
int x = UNSAFE.getIntVolatile(base, offset);
|
||||
assertEquals(x, 1, "putRelease int value");
|
||||
}
|
||||
|
||||
// Unaligned
|
||||
{
|
||||
UNSAFE.putIntUnaligned(base, offset, 2);
|
||||
int x = UNSAFE.getIntUnaligned(base, offset);
|
||||
assertEquals(x, 2, "putUnaligned int value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putIntUnaligned(base, offset, 1, true);
|
||||
int x = UNSAFE.getIntUnaligned(base, offset, true);
|
||||
assertEquals(x, 1, "putUnaligned big endian int value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putIntUnaligned(base, offset, 2, false);
|
||||
int x = UNSAFE.getIntUnaligned(base, offset, false);
|
||||
assertEquals(x, 2, "putUnaligned little endian int value");
|
||||
}
|
||||
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 2);
|
||||
assertEquals(r, true, "success compareAndSwap int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 2, "success compareAndSwap int value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 3);
|
||||
assertEquals(r, false, "failing compareAndSwap int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 2, "failing compareAndSwap int value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
int o = UNSAFE.getAndSetInt(base, offset, 1);
|
||||
assertEquals(o, 2, "getAndSet int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1, "getAndSet int value");
|
||||
}
|
||||
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
|
||||
// get and add, add and get
|
||||
{
|
||||
int o = UNSAFE.getAndAddInt(base, offset, 2);
|
||||
assertEquals(o, 1, "getAndAdd int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1 + 2, "weakCompareAndSwapRelease int");
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putInt(address, 1);
|
||||
int x = UNSAFE.getInt(address);
|
||||
assertEquals(x, 1, "set int value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for long
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestLong
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestLong {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestLong.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestLong.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(long[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(long[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static long static_v;
|
||||
|
||||
long v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestLong t = new JdkInternalMiscUnsafeAccessTestLong();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
long[] array = new long[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L, "set long value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putLongVolatile(base, offset, 2L);
|
||||
long x = UNSAFE.getLongVolatile(base, offset);
|
||||
assertEquals(x, 2L, "putVolatile long value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedLong(base, offset, 1L);
|
||||
long x = UNSAFE.getLongVolatile(base, offset);
|
||||
assertEquals(x, 1L, "putRelease long value");
|
||||
}
|
||||
|
||||
// Unaligned
|
||||
{
|
||||
UNSAFE.putLongUnaligned(base, offset, 2L);
|
||||
long x = UNSAFE.getLongUnaligned(base, offset);
|
||||
assertEquals(x, 2L, "putUnaligned long value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putLongUnaligned(base, offset, 1L, true);
|
||||
long x = UNSAFE.getLongUnaligned(base, offset, true);
|
||||
assertEquals(x, 1L, "putUnaligned big endian long value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putLongUnaligned(base, offset, 2L, false);
|
||||
long x = UNSAFE.getLongUnaligned(base, offset, false);
|
||||
assertEquals(x, 2L, "putUnaligned little endian long value");
|
||||
}
|
||||
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 2L);
|
||||
assertEquals(r, true, "success compareAndSwap long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 2L, "success compareAndSwap long value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 3L);
|
||||
assertEquals(r, false, "failing compareAndSwap long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 2L, "failing compareAndSwap long value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
long o = UNSAFE.getAndSetLong(base, offset, 1L);
|
||||
assertEquals(o, 2L, "getAndSet long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L, "getAndSet long value");
|
||||
}
|
||||
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
|
||||
// get and add, add and get
|
||||
{
|
||||
long o = UNSAFE.getAndAddLong(base, offset, 2L);
|
||||
assertEquals(o, 1L, "getAndAdd long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putLong(address, 1L);
|
||||
long x = UNSAFE.getLong(address);
|
||||
assertEquals(x, 1L, "set long value");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for Object
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestObject
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestObject {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestObject.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestObject.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(Object[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(Object[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static Object static_v;
|
||||
|
||||
Object v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestObject t = new JdkInternalMiscUnsafeAccessTestObject();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
Object[] array = new Object[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putObject(base, offset, "foo");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "foo", "set Object value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putObjectVolatile(base, offset, "bar");
|
||||
Object x = UNSAFE.getObjectVolatile(base, offset);
|
||||
assertEquals(x, "bar", "putVolatile Object value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedObject(base, offset, "foo");
|
||||
Object x = UNSAFE.getObjectVolatile(base, offset);
|
||||
assertEquals(x, "foo", "putRelease Object value");
|
||||
}
|
||||
|
||||
|
||||
UNSAFE.putObject(base, offset, "foo");
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "bar");
|
||||
assertEquals(r, true, "success compareAndSwap Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "bar", "success compareAndSwap Object value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "baz");
|
||||
assertEquals(r, false, "failing compareAndSwap Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "bar", "failing compareAndSwap Object value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
Object o = UNSAFE.getAndSetObject(base, offset, "foo");
|
||||
assertEquals(o, "bar", "getAndSet Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "foo", "getAndSet Object value");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for short
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestShort
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class JdkInternalMiscUnsafeAccessTestShort {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final jdk.internal.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = JdkInternalMiscUnsafeAccessTestShort.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = JdkInternalMiscUnsafeAccessTestShort.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(short[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(short[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static short static_v;
|
||||
|
||||
short v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
JdkInternalMiscUnsafeAccessTestShort t = new JdkInternalMiscUnsafeAccessTestShort();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
short[] array = new short[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putShort(base, offset, (short)1);
|
||||
short x = UNSAFE.getShort(base, offset);
|
||||
assertEquals(x, (short)1, "set short value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putShortVolatile(base, offset, (short)2);
|
||||
short x = UNSAFE.getShortVolatile(base, offset);
|
||||
assertEquals(x, (short)2, "putVolatile short value");
|
||||
}
|
||||
|
||||
|
||||
// Unaligned
|
||||
{
|
||||
UNSAFE.putShortUnaligned(base, offset, (short)2);
|
||||
short x = UNSAFE.getShortUnaligned(base, offset);
|
||||
assertEquals(x, (short)2, "putUnaligned short value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putShortUnaligned(base, offset, (short)1, true);
|
||||
short x = UNSAFE.getShortUnaligned(base, offset, true);
|
||||
assertEquals(x, (short)1, "putUnaligned big endian short value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.putShortUnaligned(base, offset, (short)2, false);
|
||||
short x = UNSAFE.getShortUnaligned(base, offset, false);
|
||||
assertEquals(x, (short)2, "putUnaligned little endian short value");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putShort(address, (short)1);
|
||||
short x = UNSAFE.getShort(address);
|
||||
assertEquals(x, (short)1, "set short value");
|
||||
}
|
||||
}
|
||||
}
|
135
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestBoolean.java
Normal file
135
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestBoolean.java
Normal file
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for boolean
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestBoolean
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestBoolean
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestBoolean {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestBoolean.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestBoolean.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(boolean[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(boolean[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static boolean static_v;
|
||||
|
||||
boolean v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestBoolean t = new SunMiscUnsafeAccessTestBoolean();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
boolean[] array = new boolean[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putBoolean(base, offset, true);
|
||||
boolean x = UNSAFE.getBoolean(base, offset);
|
||||
assertEquals(x, true, "set boolean value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putBooleanVolatile(base, offset, false);
|
||||
boolean x = UNSAFE.getBooleanVolatile(base, offset);
|
||||
assertEquals(x, false, "putVolatile boolean value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestByte.java
Normal file
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestByte.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for byte
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestByte
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestByte
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestByte {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestByte.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestByte.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(byte[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static byte static_v;
|
||||
|
||||
byte v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestByte t = new SunMiscUnsafeAccessTestByte();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
byte[] array = new byte[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putByte(base, offset, (byte)1);
|
||||
byte x = UNSAFE.getByte(base, offset);
|
||||
assertEquals(x, (byte)1, "set byte value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putByteVolatile(base, offset, (byte)2);
|
||||
byte x = UNSAFE.getByteVolatile(base, offset);
|
||||
assertEquals(x, (byte)2, "putVolatile byte value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putByte(address, (byte)1);
|
||||
byte x = UNSAFE.getByte(address);
|
||||
assertEquals(x, (byte)1, "set byte value");
|
||||
}
|
||||
}
|
||||
}
|
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestChar.java
Normal file
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestChar.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for char
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestChar
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestChar
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestChar {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestChar.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestChar.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(char[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(char[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static char static_v;
|
||||
|
||||
char v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestChar t = new SunMiscUnsafeAccessTestChar();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
char[] array = new char[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putChar(base, offset, 'a');
|
||||
char x = UNSAFE.getChar(base, offset);
|
||||
assertEquals(x, 'a', "set char value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putCharVolatile(base, offset, 'b');
|
||||
char x = UNSAFE.getCharVolatile(base, offset);
|
||||
assertEquals(x, 'b', "putVolatile char value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putChar(address, 'a');
|
||||
char x = UNSAFE.getChar(address);
|
||||
assertEquals(x, 'a', "set char value");
|
||||
}
|
||||
}
|
||||
}
|
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestDouble.java
Normal file
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestDouble.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for double
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestDouble
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestDouble
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestDouble {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestDouble.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestDouble.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(double[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(double[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static double static_v;
|
||||
|
||||
double v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestDouble t = new SunMiscUnsafeAccessTestDouble();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
double[] array = new double[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putDouble(base, offset, 1.0d);
|
||||
double x = UNSAFE.getDouble(base, offset);
|
||||
assertEquals(x, 1.0d, "set double value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putDoubleVolatile(base, offset, 2.0d);
|
||||
double x = UNSAFE.getDoubleVolatile(base, offset);
|
||||
assertEquals(x, 2.0d, "putVolatile double value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putDouble(address, 1.0d);
|
||||
double x = UNSAFE.getDouble(address);
|
||||
assertEquals(x, 1.0d, "set double value");
|
||||
}
|
||||
}
|
||||
}
|
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestFloat.java
Normal file
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestFloat.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for float
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestFloat
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestFloat
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestFloat {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestFloat.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestFloat.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(float[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(float[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static float static_v;
|
||||
|
||||
float v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestFloat t = new SunMiscUnsafeAccessTestFloat();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
float[] array = new float[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putFloat(base, offset, 1.0f);
|
||||
float x = UNSAFE.getFloat(base, offset);
|
||||
assertEquals(x, 1.0f, "set float value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putFloatVolatile(base, offset, 2.0f);
|
||||
float x = UNSAFE.getFloatVolatile(base, offset);
|
||||
assertEquals(x, 2.0f, "putVolatile float value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putFloat(address, 1.0f);
|
||||
float x = UNSAFE.getFloat(address);
|
||||
assertEquals(x, 1.0f, "set float value");
|
||||
}
|
||||
}
|
||||
}
|
211
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestInt.java
Normal file
211
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestInt.java
Normal file
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for int
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestInt
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestInt
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestInt {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestInt.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestInt.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(int[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(int[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static int static_v;
|
||||
|
||||
int v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestInt t = new SunMiscUnsafeAccessTestInt();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
int[] array = new int[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1, "set int value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putIntVolatile(base, offset, 2);
|
||||
int x = UNSAFE.getIntVolatile(base, offset);
|
||||
assertEquals(x, 2, "putVolatile int value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedInt(base, offset, 1);
|
||||
int x = UNSAFE.getIntVolatile(base, offset);
|
||||
assertEquals(x, 1, "putRelease int value");
|
||||
}
|
||||
|
||||
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 2);
|
||||
assertEquals(r, true, "success compareAndSwap int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 2, "success compareAndSwap int value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 3);
|
||||
assertEquals(r, false, "failing compareAndSwap int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 2, "failing compareAndSwap int value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
int o = UNSAFE.getAndSetInt(base, offset, 1);
|
||||
assertEquals(o, 2, "getAndSet int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1, "getAndSet int value");
|
||||
}
|
||||
|
||||
UNSAFE.putInt(base, offset, 1);
|
||||
|
||||
// get and add, add and get
|
||||
{
|
||||
int o = UNSAFE.getAndAddInt(base, offset, 2);
|
||||
assertEquals(o, 1, "getAndAdd int");
|
||||
int x = UNSAFE.getInt(base, offset);
|
||||
assertEquals(x, 1 + 2, "weakCompareAndSwapRelease int");
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putInt(address, 1);
|
||||
int x = UNSAFE.getInt(address);
|
||||
assertEquals(x, 1, "set int value");
|
||||
}
|
||||
}
|
||||
}
|
211
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestLong.java
Normal file
211
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestLong.java
Normal file
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for long
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestLong
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestLong
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestLong {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestLong.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestLong.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(long[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(long[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static long static_v;
|
||||
|
||||
long v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestLong t = new SunMiscUnsafeAccessTestLong();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
long[] array = new long[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L, "set long value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putLongVolatile(base, offset, 2L);
|
||||
long x = UNSAFE.getLongVolatile(base, offset);
|
||||
assertEquals(x, 2L, "putVolatile long value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedLong(base, offset, 1L);
|
||||
long x = UNSAFE.getLongVolatile(base, offset);
|
||||
assertEquals(x, 1L, "putRelease long value");
|
||||
}
|
||||
|
||||
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 2L);
|
||||
assertEquals(r, true, "success compareAndSwap long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 2L, "success compareAndSwap long value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 3L);
|
||||
assertEquals(r, false, "failing compareAndSwap long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 2L, "failing compareAndSwap long value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
long o = UNSAFE.getAndSetLong(base, offset, 1L);
|
||||
assertEquals(o, 2L, "getAndSet long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L, "getAndSet long value");
|
||||
}
|
||||
|
||||
UNSAFE.putLong(base, offset, 1L);
|
||||
|
||||
// get and add, add and get
|
||||
{
|
||||
long o = UNSAFE.getAndAddLong(base, offset, 2L);
|
||||
assertEquals(o, 1L, "getAndAdd long");
|
||||
long x = UNSAFE.getLong(base, offset);
|
||||
assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putLong(address, 1L);
|
||||
long x = UNSAFE.getLong(address);
|
||||
assertEquals(x, 1L, "set long value");
|
||||
}
|
||||
}
|
||||
}
|
165
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestObject.java
Normal file
165
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestObject.java
Normal file
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for Object
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestObject
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestObject
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestObject {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestObject.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestObject.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(Object[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(Object[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static Object static_v;
|
||||
|
||||
Object v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestObject t = new SunMiscUnsafeAccessTestObject();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
Object[] array = new Object[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putObject(base, offset, "foo");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "foo", "set Object value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putObjectVolatile(base, offset, "bar");
|
||||
Object x = UNSAFE.getObjectVolatile(base, offset);
|
||||
assertEquals(x, "bar", "putVolatile Object value");
|
||||
}
|
||||
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrderedObject(base, offset, "foo");
|
||||
Object x = UNSAFE.getObjectVolatile(base, offset);
|
||||
assertEquals(x, "foo", "putRelease Object value");
|
||||
}
|
||||
|
||||
|
||||
UNSAFE.putObject(base, offset, "foo");
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "bar");
|
||||
assertEquals(r, true, "success compareAndSwap Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "bar", "success compareAndSwap Object value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "baz");
|
||||
assertEquals(r, false, "failing compareAndSwap Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "bar", "failing compareAndSwap Object value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
Object o = UNSAFE.getAndSetObject(base, offset, "foo");
|
||||
assertEquals(o, "bar", "getAndSet Object");
|
||||
Object x = UNSAFE.getObject(base, offset);
|
||||
assertEquals(x, "foo", "getAndSet Object value");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestShort.java
Normal file
172
hotspot/test/compiler/unsafe/SunMiscUnsafeAccessTestShort.java
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for short
|
||||
* @modules java.base/sun.misc
|
||||
* @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestShort
|
||||
* @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestShort
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class SunMiscUnsafeAccessTestShort {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = (sun.misc.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = SunMiscUnsafeAccessTestShort.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = SunMiscUnsafeAccessTestShort.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset(short[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale(short[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static short static_v;
|
||||
|
||||
short v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
SunMiscUnsafeAccessTestShort t = new SunMiscUnsafeAccessTestShort();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
short[] array = new short[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putShort(base, offset, (short)1);
|
||||
short x = UNSAFE.getShort(base, offset);
|
||||
assertEquals(x, (short)1, "set short value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.putShortVolatile(base, offset, (short)2);
|
||||
short x = UNSAFE.getShortVolatile(base, offset);
|
||||
assertEquals(x, (short)2, "putVolatile short value");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.putShort(address, (short)1);
|
||||
short x = UNSAFE.getShort(address);
|
||||
assertEquals(x, (short)1, "set short value");
|
||||
}
|
||||
}
|
||||
}
|
247
hotspot/test/compiler/unsafe/X-UnsafeAccessTest.java.template
Normal file
247
hotspot/test/compiler/unsafe/X-UnsafeAccessTest.java.template
Normal file
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8143628
|
||||
* @summary Test unsafe access for $type$
|
||||
* @modules java.base/$package$
|
||||
* @run testng/othervm -Diters=100 -Xint $Qualifier$UnsafeAccessTest$Type$
|
||||
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 $Qualifier$UnsafeAccessTest$Type$
|
||||
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation $Qualifier$UnsafeAccessTest$Type$
|
||||
* @run testng/othervm -Diters=20000 $Qualifier$UnsafeAccessTest$Type$
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class $Qualifier$UnsafeAccessTest$Type$ {
|
||||
static final int ITERS = Integer.getInteger("iters", 1);
|
||||
|
||||
static final $package$.Unsafe UNSAFE;
|
||||
|
||||
static final long V_OFFSET;
|
||||
|
||||
static final Object STATIC_V_BASE;
|
||||
|
||||
static final long STATIC_V_OFFSET;
|
||||
|
||||
static int ARRAY_OFFSET;
|
||||
|
||||
static int ARRAY_SHIFT;
|
||||
|
||||
static {
|
||||
try {
|
||||
Field f = $package$.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
UNSAFE = ($package$.Unsafe) f.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to get Unsafe instance.", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field staticVField = $Qualifier$UnsafeAccessTest$Type$.class.getDeclaredField("static_v");
|
||||
STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
|
||||
STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
Field vField = $Qualifier$UnsafeAccessTest$Type$.class.getDeclaredField("v");
|
||||
V_OFFSET = UNSAFE.objectFieldOffset(vField);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
ARRAY_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
|
||||
int ascale = UNSAFE.arrayIndexScale($type$[].class);
|
||||
ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
|
||||
}
|
||||
|
||||
static $type$ static_v;
|
||||
|
||||
$type$ v;
|
||||
|
||||
@Test
|
||||
public void testFieldInstance() {
|
||||
$Qualifier$UnsafeAccessTest$Type$ t = new $Qualifier$UnsafeAccessTest$Type$();
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(t, V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldStatic() {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() {
|
||||
$type$[] array = new $type$[10];
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if[!Object]
|
||||
#if[!boolean]
|
||||
@Test
|
||||
public void testArrayOffHeap() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayOffHeapDirect() {
|
||||
int size = 10;
|
||||
long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
|
||||
try {
|
||||
for (int c = 0; c < ITERS; c++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
testAccess((((long) i) << ARRAY_SHIFT) + address);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
}
|
||||
#end[!boolean]
|
||||
#end[!Object]
|
||||
|
||||
static void testAccess(Object base, long offset) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.put$Type$(base, offset, $value1$);
|
||||
$type$ x = UNSAFE.get$Type$(base, offset);
|
||||
assertEquals(x, $value1$, "set $type$ value");
|
||||
}
|
||||
|
||||
// Volatile
|
||||
{
|
||||
UNSAFE.put$Type$Volatile(base, offset, $value2$);
|
||||
$type$ x = UNSAFE.get$Type$Volatile(base, offset);
|
||||
assertEquals(x, $value2$, "putVolatile $type$ value");
|
||||
}
|
||||
|
||||
#if[Ordered]
|
||||
// Lazy
|
||||
{
|
||||
UNSAFE.putOrdered$Type$(base, offset, $value1$);
|
||||
$type$ x = UNSAFE.get$Type$Volatile(base, offset);
|
||||
assertEquals(x, $value1$, "putRelease $type$ value");
|
||||
}
|
||||
#end[Ordered]
|
||||
|
||||
#if[JdkInternalMisc]
|
||||
#if[Unaligned]
|
||||
// Unaligned
|
||||
{
|
||||
UNSAFE.put$Type$Unaligned(base, offset, $value2$);
|
||||
$type$ x = UNSAFE.get$Type$Unaligned(base, offset);
|
||||
assertEquals(x, $value2$, "putUnaligned $type$ value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.put$Type$Unaligned(base, offset, $value1$, true);
|
||||
$type$ x = UNSAFE.get$Type$Unaligned(base, offset, true);
|
||||
assertEquals(x, $value1$, "putUnaligned big endian $type$ value");
|
||||
}
|
||||
|
||||
{
|
||||
UNSAFE.put$Type$Unaligned(base, offset, $value2$, false);
|
||||
$type$ x = UNSAFE.get$Type$Unaligned(base, offset, false);
|
||||
assertEquals(x, $value2$, "putUnaligned little endian $type$ value");
|
||||
}
|
||||
#end[Unaligned]
|
||||
#end[JdkInternalMisc]
|
||||
|
||||
#if[CAS]
|
||||
UNSAFE.put$Type$(base, offset, $value1$);
|
||||
|
||||
// Compare
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value2$);
|
||||
assertEquals(r, true, "success compareAndSwap $type$");
|
||||
$type$ x = UNSAFE.get$Type$(base, offset);
|
||||
assertEquals(x, $value2$, "success compareAndSwap $type$ value");
|
||||
}
|
||||
|
||||
{
|
||||
boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value3$);
|
||||
assertEquals(r, false, "failing compareAndSwap $type$");
|
||||
$type$ x = UNSAFE.get$Type$(base, offset);
|
||||
assertEquals(x, $value2$, "failing compareAndSwap $type$ value");
|
||||
}
|
||||
|
||||
// Compare set and get
|
||||
{
|
||||
$type$ o = UNSAFE.getAndSet$Type$(base, offset, $value1$);
|
||||
assertEquals(o, $value2$, "getAndSet $type$");
|
||||
$type$ x = UNSAFE.get$Type$(base, offset);
|
||||
assertEquals(x, $value1$, "getAndSet $type$ value");
|
||||
}
|
||||
#end[CAS]
|
||||
|
||||
#if[AtomicAdd]
|
||||
UNSAFE.put$Type$(base, offset, $value1$);
|
||||
|
||||
// get and add, add and get
|
||||
{
|
||||
$type$ o = UNSAFE.getAndAdd$Type$(base, offset, $value2$);
|
||||
assertEquals(o, $value1$, "getAndAdd $type$");
|
||||
$type$ x = UNSAFE.get$Type$(base, offset);
|
||||
assertEquals(x, $value1$ + $value2$, "weakCompareAndSwapRelease $type$");
|
||||
}
|
||||
#end[AtomicAdd]
|
||||
}
|
||||
|
||||
#if[!Object]
|
||||
#if[!boolean]
|
||||
static void testAccess(long address) {
|
||||
// Plain
|
||||
{
|
||||
UNSAFE.put$Type$(address, $value1$);
|
||||
$type$ x = UNSAFE.get$Type$(address);
|
||||
assertEquals(x, $value1$, "set $type$ value");
|
||||
}
|
||||
}
|
||||
#end[!boolean]
|
||||
#end[!Object]
|
||||
}
|
119
hotspot/test/compiler/unsafe/generate-unsafe-access-tests.sh
Normal file
119
hotspot/test/compiler/unsafe/generate-unsafe-access-tests.sh
Normal file
|
@ -0,0 +1,119 @@
|
|||
#!/bin/bash
|
||||
|
||||
#
|
||||
# Copyright (c) 2015, Oracle and/or its affiliates. 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.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
# or visit www.oracle.com if you need additional information or have any
|
||||
# questions.
|
||||
#
|
||||
|
||||
javac -d . ../../../../jdk/make/src/classes/build/tools/spp/Spp.java
|
||||
|
||||
SPP=build.tools.spp.Spp
|
||||
|
||||
# Generates unsafe access tests for objects and all primitive types
|
||||
# $1 = package name to Unsafe, sun.misc | jdk.internal.misc
|
||||
# $2 = test class qualifier name, SunMisc | JdkInternalMisc
|
||||
function generate {
|
||||
package=$1
|
||||
Qualifier=$2
|
||||
|
||||
for type in boolean byte short char int long float double Object
|
||||
do
|
||||
Type="$(tr '[:lower:]' '[:upper:]' <<< ${type:0:1})${type:1}"
|
||||
args="-K$type -Dtype=$type -DType=$Type"
|
||||
|
||||
case $type in
|
||||
Object|int|long)
|
||||
args="$args -KCAS -KOrdered"
|
||||
;;
|
||||
esac
|
||||
|
||||
case $type in
|
||||
int|long)
|
||||
args="$args -KAtomicAdd"
|
||||
;;
|
||||
esac
|
||||
|
||||
case $type in
|
||||
short|char|int|long)
|
||||
args="$args -KUnaligned"
|
||||
;;
|
||||
esac
|
||||
|
||||
case $type in
|
||||
boolean)
|
||||
value1=true
|
||||
value2=false
|
||||
value3=false
|
||||
;;
|
||||
byte)
|
||||
value1=(byte)1
|
||||
value2=(byte)2
|
||||
value3=(byte)3
|
||||
;;
|
||||
short)
|
||||
value1=(short)1
|
||||
value2=(short)2
|
||||
value3=(short)3
|
||||
;;
|
||||
char)
|
||||
value1=\'a\'
|
||||
value2=\'b\'
|
||||
value3=\'c\'
|
||||
;;
|
||||
int)
|
||||
value1=1
|
||||
value2=2
|
||||
value3=3
|
||||
;;
|
||||
long)
|
||||
value1=1L
|
||||
value2=2L
|
||||
value3=3L
|
||||
;;
|
||||
float)
|
||||
value1=1.0f
|
||||
value2=2.0f
|
||||
value3=3.0f
|
||||
;;
|
||||
double)
|
||||
value1=1.0d
|
||||
value2=2.0d
|
||||
value3=3.0d
|
||||
;;
|
||||
Object)
|
||||
value1=\"foo\"
|
||||
value2=\"bar\"
|
||||
value3=\"baz\"
|
||||
;;
|
||||
esac
|
||||
|
||||
args="$args -Dvalue1=$value1 -Dvalue2=$value2 -Dvalue3=$value3"
|
||||
|
||||
echo $args
|
||||
java $SPP -nel -K$Qualifier -Dpackage=$package -DQualifier=$Qualifier \
|
||||
$args < X-UnsafeAccessTest.java.template > ${Qualifier}UnsafeAccessTest${Type}.java
|
||||
done
|
||||
}
|
||||
|
||||
generate sun.misc SunMisc
|
||||
generate jdk.internal.misc JdkInternalMisc
|
||||
|
||||
rm -fr build
|
Loading…
Add table
Add a link
Reference in a new issue