8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit

Reviewed-by: gtriantafill, dholmes
This commit is contained in:
Christian Tornqvist 2015-01-23 11:44:21 -08:00
parent 5f8b5ceaee
commit 2c2593b757
20 changed files with 1199 additions and 0 deletions

View file

@ -0,0 +1,63 @@
/*
* 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
* @summary Verifies the behaviour of Unsafe.allocateInstance
* @library /testlibrary
* @run main AllocateInstance
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class AllocateInstance {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
// allocateInstance() should not result in a call to the constructor
TestClass tc = (TestClass)unsafe.allocateInstance(TestClass.class);
assertFalse(tc.calledConstructor);
// allocateInstance() on an abstract class should result in an InstantiationException
try {
AbstractClass ac = (AbstractClass)unsafe.allocateInstance(AbstractClass.class);
throw new RuntimeException("Did not get expected InstantiationException");
} catch (InstantiationException e) {
// Expected
}
}
class TestClass {
public boolean calledConstructor = false;
public TestClass() {
calledConstructor = true;
}
}
abstract class AbstractClass {
public AbstractClass() {}
}
}

View file

@ -0,0 +1,66 @@
/*
* 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
* @summary Verifies behaviour of Unsafe.allocateMemory
* @library /testlibrary
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:MallocMaxTestWords=20m AllocateMemory
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class AllocateMemory {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
// Allocate a byte, write to the location and read back the value
long address = unsafe.allocateMemory(1);
assertNotEquals(address, 0L);
unsafe.putByte(address, Byte.MAX_VALUE);
assertEquals(Byte.MAX_VALUE, unsafe.getByte(address));
unsafe.freeMemory(address);
// Call to allocateMemory() with a negative value should result in an IllegalArgumentException
try {
address = unsafe.allocateMemory(-1);
throw new RuntimeException("Did not get expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
assertNotEquals(address, 0L);
}
// allocateMemory() should throw an OutOfMemoryError when the underlying malloc fails,
// we test this by limiting the malloc using -XX:MallocMaxTestWords
try {
address = unsafe.allocateMemory(20 * 1024 * 1024 * 8);
} catch (OutOfMemoryError e) {
// Expected
return;
}
throw new RuntimeException("Did not get expected OutOfMemoryError");
}
}

View file

@ -0,0 +1,56 @@
/*
* 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
* @summary Verifies behaviour of Unsafe.copyMemory
* @library /testlibrary
* @run main CopyMemory
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class CopyMemory {
final static int LENGTH = 8;
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
long src = unsafe.allocateMemory(LENGTH);
long dst = unsafe.allocateMemory(LENGTH);
assertNotEquals(src, 0L);
assertNotEquals(dst, 0L);
// call copyMemory() with different lengths and verify the contents of
// the destination array
for (int i = 0; i < LENGTH; i++) {
unsafe.putByte(src + i, (byte)i);
unsafe.copyMemory(src, dst, i);
for (int j = 0; j < i; j++) {
assertEquals(unsafe.getByte(src + j), unsafe.getByte(src + j));
}
}
unsafe.freeMemory(src);
unsafe.freeMemory(dst);
}
}

View file

@ -0,0 +1,99 @@
/*
* 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
* @summary Verifies the behaviour of Unsafe.defineClass
* @library /testlibrary
* @run main DefineClass
*/
import java.security.ProtectionDomain;
import java.io.InputStream;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class DefineClass {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
TestClassLoader classloader = new TestClassLoader();
ProtectionDomain pd = new ProtectionDomain(null, null);
byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }");
// Invalid class data
try {
unsafe.defineClass(null, klassbuf, 4, klassbuf.length - 4, classloader, pd);
throw new RuntimeException("defineClass did not throw expected ClassFormatError");
} catch (ClassFormatError e) {
// Expected
}
// Negative offset
try {
unsafe.defineClass(null, klassbuf, -1, klassbuf.length, classloader, pd);
throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}
// Negative length
try {
unsafe.defineClass(null, klassbuf, 0, -1, classloader, pd);
throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}
// Offset greater than klassbuf.length
try {
unsafe.defineClass(null, klassbuf, klassbuf.length + 1, klassbuf.length, classloader, pd);
throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}
// Length greater than klassbuf.length
try {
unsafe.defineClass(null, klassbuf, 0, klassbuf.length + 1, classloader, pd);
throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
}
Class klass = unsafe.defineClass(null, klassbuf, 0, klassbuf.length, classloader, pd);
assertEquals(klass.getClassLoader(), classloader);
assertEquals(klass.getProtectionDomain(), pd);
}
private static class TestClassLoader extends ClassLoader {
public TestClassLoader(ClassLoader parent) {
super(parent);
}
public TestClassLoader() {
super();
}
}
}

View file

@ -0,0 +1,65 @@
/*
* 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
* @summary Verifies the behaviour of Unsafe.fieldOffset
* @library /testlibrary
* @run main FieldOffset
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import java.lang.reflect.*;
import static com.oracle.java.testlibrary.Asserts.*;
public class FieldOffset {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Field fields[] = Test.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
int offset = unsafe.fieldOffset(fields[i]);
// Ensure we got a valid offset value back
assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET);
// Make sure the field offset is unique
for (int j = 0; j < i; j++) {
assertNotEquals(offset, unsafe.fieldOffset(fields[j]));
}
}
}
class Test {
boolean booleanField;
byte byteField;
char charField;
double doubleField;
float floatField;
int intField;
long longField;
Object objectField;
short shortField;
}
}

View file

@ -0,0 +1,46 @@
/*
* 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
* @summary Verifies behaviour of Unsafe.getField
* @library /testlibrary
* @run main GetField
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import java.lang.reflect.*;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetField {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
// Unsafe.INVALID_FIELD_OFFSET is a static final int field,
// make sure getField returns the correct field
Field field = Unsafe.class.getField("INVALID_FIELD_OFFSET");
assertNotEquals(field.getModifiers() & Modifier.FINAL, 0);
assertNotEquals(field.getModifiers() & Modifier.STATIC, 0);
assertEquals(field.getType(), int.class);
}
}

View file

@ -0,0 +1,52 @@
/*
* 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
* Verify behaviour of Unsafe.get/putAddress and Unsafe.addressSize
* @library /testlibrary
* @run main GetPutAddress
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutAddress {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
int addressSize = unsafe.addressSize();
// Ensure the size returned from Unsafe.addressSize is correct
assertEquals(unsafe.addressSize(), Platform.is32bit() ? 4 : 8);
// Write the address, read it back and make sure it's the same value
long address = unsafe.allocateMemory(addressSize);
unsafe.putAddress(address, address);
long readAddress = unsafe.getAddress(address);
if (addressSize == 4) {
readAddress &= 0x00000000FFFFFFFFL;
}
assertEquals(address, readAddress);
unsafe.freeMemory(address);
}
}

View file

@ -0,0 +1,59 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putBoolean
* @library /testlibrary
* @run main GetPutBoolean
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutBoolean {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("b1");
int offset = unsafe.fieldOffset(field);
assertEquals(false, unsafe.getBoolean(t, offset));
unsafe.putBoolean(t, offset, true);
assertEquals(true, unsafe.getBoolean(t, offset));
boolean arrayBoolean[] = { true, false, false, true };
int scale = unsafe.arrayIndexScale(arrayBoolean.getClass());
offset = unsafe.arrayBaseOffset(arrayBoolean.getClass());
for (int i = 0; i < arrayBoolean.length; i++) {
assertEquals(unsafe.getBoolean(arrayBoolean, offset), arrayBoolean[i]);
offset += scale;
}
}
static class Test {
public boolean b1 = false;
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putByte
* @library /testlibrary
* @run main GetPutByte
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutByte {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("b");
int offset = unsafe.fieldOffset(field);
assertEquals((byte)0, unsafe.getByte(t, offset));
unsafe.putByte(t, offset, (byte)1);
assertEquals((byte)1, unsafe.getByte(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putByte(address, (byte)2);
assertEquals((byte)2, unsafe.getByte(address));
unsafe.freeMemory(address);
byte arrayByte[] = { -1, 0, 1, 2 };
int scale = unsafe.arrayIndexScale(arrayByte.getClass());
offset = unsafe.arrayBaseOffset(arrayByte.getClass());
for (int i = 0; i < arrayByte.length; i++) {
assertEquals(unsafe.getByte(arrayByte, offset), arrayByte[i]);
offset += scale;
}
}
static class Test {
public byte b = 0;
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putChar
* @library /testlibrary
* @run main GetPutChar
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutChar {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("c");
int offset = unsafe.fieldOffset(field);
assertEquals('\u0000', unsafe.getChar(t, offset));
unsafe.putChar(t, offset, '\u0001');
assertEquals('\u0001', unsafe.getChar(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putChar(address, '\u0002');
assertEquals('\u0002', unsafe.getChar(address));
unsafe.freeMemory(address);
char arrayChar[] = { '\uabcd', '\u00ff', '\uff00', };
int scale = unsafe.arrayIndexScale(arrayChar.getClass());
offset = unsafe.arrayBaseOffset(arrayChar.getClass());
for (int i = 0; i < arrayChar.length; i++) {
assertEquals(unsafe.getChar(arrayChar, offset), arrayChar[i]);
offset += scale;
}
}
static class Test {
public char c = '\u0000';
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putDouble
* @library /testlibrary
* @run main GetPutDouble
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutDouble {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("d");
int offset = unsafe.fieldOffset(field);
assertEquals(-1.0, unsafe.getDouble(t, offset));
unsafe.putDouble(t, offset, 0.0);
assertEquals(0.0, unsafe.getDouble(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putDouble(address, 1.0);
assertEquals(1.0, unsafe.getDouble(address));
unsafe.freeMemory(address);
double arrayDouble[] = { -1.0, 0.0, 1.0, 2.0 };
int scale = unsafe.arrayIndexScale(arrayDouble.getClass());
offset = unsafe.arrayBaseOffset(arrayDouble.getClass());
for (int i = 0; i < arrayDouble.length; i++) {
assertEquals(unsafe.getDouble(arrayDouble, offset), arrayDouble[i]);
offset += scale;
}
}
static class Test {
public double d = -1.0;
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putFloat
* @library /testlibrary
* @run main GetPutFloat
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutFloat {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("f");
int offset = unsafe.fieldOffset(field);
assertEquals(-1.0f, unsafe.getFloat(t, offset));
unsafe.putFloat(t, offset, 0.0f);
assertEquals(0.0f, unsafe.getFloat(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putFloat(address, 1.0f);
assertEquals(1.0f, unsafe.getFloat(address));
unsafe.freeMemory(address);
float arrayFloat[] = { -1.0f, 0.0f, 1.0f, 2.0f };
int scale = unsafe.arrayIndexScale(arrayFloat.getClass());
offset = unsafe.arrayBaseOffset(arrayFloat.getClass());
for (int i = 0; i < arrayFloat.length; i++) {
assertEquals(unsafe.getFloat(arrayFloat, offset), arrayFloat[i]);
offset += scale;
}
}
static class Test {
public float f = -1.0f;
}
}

View file

@ -0,0 +1,63 @@
/*
* 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
* @library /testlibrary
* @run main GetPutInt
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutInt {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("i");
int offset = unsafe.fieldOffset(field);
assertEquals(-1, unsafe.getInt(t, offset));
unsafe.putInt(t, offset, 0);
assertEquals(0, unsafe.getInt(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putInt(address, 1);
assertEquals(1, unsafe.getInt(address));
unsafe.freeMemory(address);
int arrayInt[] = { -1, 0, 1, 2 };
int scale = unsafe.arrayIndexScale(arrayInt.getClass());
offset = unsafe.arrayBaseOffset(arrayInt.getClass());
for (int i = 0; i < arrayInt.length; i++) {
assertEquals(unsafe.getInt(arrayInt, offset), arrayInt[i]);
offset += scale;
}
}
static class Test {
public int i = -1;
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putLong
* @library /testlibrary
* @run main GetPutLong
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutLong {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("l");
int offset = unsafe.fieldOffset(field);
assertEquals(-1L, unsafe.getLong(t, offset));
unsafe.putLong(t, offset, 0L);
assertEquals(0L, unsafe.getLong(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putLong(address, 1L);
assertEquals(1L, unsafe.getLong(address));
unsafe.freeMemory(address);
long arrayLong[] = { -1, 0, 1, 2 };
int scale = unsafe.arrayIndexScale(arrayLong.getClass());
offset = unsafe.arrayBaseOffset(arrayLong.getClass());
for (int i = 0; i < arrayLong.length; i++) {
assertEquals(unsafe.getLong(arrayLong, offset), arrayLong[i]);
offset += scale;
}
}
static class Test {
public long l = -1L;
}
}

View file

@ -0,0 +1,61 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putObject
* @library /testlibrary
* @run main GetPutObject
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutObject {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Object o = new Object();
Field field = Test.class.getField("o");
int offset = unsafe.fieldOffset(field);
assertEquals(t.o, unsafe.getObject(t, offset));
unsafe.putObject(t, offset, o);
assertEquals(o, unsafe.getObject(t, offset));
Object arrayObject[] = { unsafe, null, new Object() };
int scale = unsafe.arrayIndexScale(arrayObject.getClass());
offset = unsafe.arrayBaseOffset(arrayObject.getClass());
for (int i = 0; i < arrayObject.length; i++) {
assertEquals(unsafe.getObject(arrayObject, offset), arrayObject[i]);
offset += scale;
}
}
static class Test {
public Object o = new Object();
}
}

View file

@ -0,0 +1,64 @@
/*
* 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
* @summary Verify behaviour of Unsafe.get/putShort
* @library /testlibrary
* @run main GetPutShort
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetPutShort {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
Test t = new Test();
Field field = Test.class.getField("s");
int offset = unsafe.fieldOffset(field);
assertEquals((short)-1, unsafe.getShort(t, offset));
unsafe.putShort(t, offset, (short)0);
assertEquals((short)0, unsafe.getShort(t, offset));
long address = unsafe.allocateMemory(8);
unsafe.putShort(address, (short)1);
assertEquals((short)1, unsafe.getShort(address));
unsafe.freeMemory(address);
short arrayShort[] = { -1, 0, 1, 2 };
int scale = unsafe.arrayIndexScale(arrayShort.getClass());
offset = unsafe.arrayBaseOffset(arrayShort.getClass());
for (int i = 0; i < arrayShort.length; i++) {
assertEquals(unsafe.getShort(arrayShort, offset), arrayShort[i]);
offset += scale;
}
}
static class Test {
public short s = -1;
}
}

View file

@ -0,0 +1,44 @@
/*
* 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
* @summary Verifies that getUnsafe() actually throws SecurityException when unsafeAccess is prohibited.
* @library /testlibrary
* @run main GetUnsafe
*/
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class GetUnsafe {
public static void main(String args[]) throws Exception {
try {
Unsafe unsafe = Unsafe.getUnsafe();
} catch (SecurityException e) {
// Expected
return;
}
throw new RuntimeException("Did not get expected SecurityException");
}
}

View file

@ -0,0 +1,48 @@
/*
* 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
* @summary Make sure pageSize() returns a value that is a power of two
* @library /testlibrary
* @run main PageSize
*/
import java.lang.reflect.Field;
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class PageSize {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
int pageSize = unsafe.pageSize();
for (int n = 1; n != 0; n <<= 1) {
if (pageSize == n) {
return;
}
}
throw new RuntimeException("Expected pagesize to be a power of two, actual pagesize:" + pageSize);
}
}

View file

@ -0,0 +1,44 @@
/*
* 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
* @summary Verifies that setMemory works correctly
* @library /testlibrary
* @run main SetMemory
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class SetMemory {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
long address = unsafe.allocateMemory(1);
assertNotEquals(address, 0L);
unsafe.setMemory(address, 1, (byte)17);
assertEquals((byte)17, unsafe.getByte(address));
unsafe.freeMemory(address);
}
}

View file

@ -0,0 +1,49 @@
/*
* 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
* @summary Verify that throwException() can throw an exception
* @library /testlibrary
* @run main ThrowException
*/
import com.oracle.java.testlibrary.*;
import sun.misc.Unsafe;
import static com.oracle.java.testlibrary.Asserts.*;
public class ThrowException {
public static void main(String args[]) throws Exception {
Unsafe unsafe = Utils.getUnsafe();
try {
unsafe.throwException(new TestException());
} catch (Throwable t) {
if (t instanceof TestException) {
return;
}
throw t;
}
throw new RuntimeException("Did not throw expected TestException");
}
static class TestException extends Exception {}
}