# HG changeset patch # User ctornqvi # Date 1421419411 18000 # Fri Jan 16 09:43:31 2015 -0500 # Node ID 58d3d10b1650d29b84be0a054ed4828016811d3d # Parent 48e04c239aa4baf509328e983b8b8720821e30d8 [mq]: unsafe diff --git a/test/runtime/Unsafe/AddressSize.java b/test/runtime/Unsafe/AddressSize.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/AddressSize.java @@ -0,0 +1,40 @@ +/* + * 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 AddressSize + */ + +import com.oracle.java.testlibrary.*; +import sun.misc.Unsafe; +import static com.oracle.java.testlibrary.Asserts.*; + +public class AddressSize { + public static void main(String args[]) throws Exception { + Unsafe unsafe = Utils.getUnsafe(); + int expectedSize = Platform.is32bit() ? 4 : 8; + assertEquals(unsafe.addressSize(), expectedSize); + } +} diff --git a/test/runtime/Unsafe/AllocateInstance.java b/test/runtime/Unsafe/AllocateInstance.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/AllocateInstance.java @@ -0,0 +1,60 @@ +/* + * 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 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(); + + TestClass tc = (TestClass)unsafe.allocateInstance(TestClass.class); + assertFalse(tc.calledConstructor); + + 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() {} + } +} diff --git a/test/runtime/Unsafe/AllocateMemory.java b/test/runtime/Unsafe/AllocateMemory.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/AllocateMemory.java @@ -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 + * @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(); + + long address = unsafe.allocateMemory(1); + assertNotEquals(address, 0L); + + unsafe.putByte(address, Byte.MAX_VALUE); + assertEquals(Byte.MAX_VALUE, unsafe.getByte(address)); + unsafe.freeMemory(address); + + try { + address = unsafe.allocateMemory(-1); + throw new RuntimeException("Did not get expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + // Expected + assertNotEquals(address, 0L); + } + + try { + address = unsafe.allocateMemory(20 * 1024 * 1024 * 8); + } catch (OutOfMemoryError e) { + // Expected + return; + } + throw new RuntimeException("Did not get expected OutOfMemoryError"); + } +} diff --git a/test/runtime/Unsafe/CopyMemory.java b/test/runtime/Unsafe/CopyMemory.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/CopyMemory.java @@ -0,0 +1,53 @@ +/* + * 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 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); + + 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); + } +} diff --git a/test/runtime/Unsafe/DefineClass.java b/test/runtime/Unsafe/DefineClass.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/DefineClass.java @@ -0,0 +1,98 @@ +/* + * 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 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(); + } + } +} diff --git a/test/runtime/Unsafe/FieldOffset.java b/test/runtime/Unsafe/FieldOffset.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/FieldOffset.java @@ -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 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]); + assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET); + + 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; + } + +} diff --git a/test/runtime/Unsafe/GetField.java b/test/runtime/Unsafe/GetField.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetField.java @@ -0,0 +1,43 @@ +/* + * 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 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(); + 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); + } +} diff --git a/test/runtime/Unsafe/GetPutAddress.java b/test/runtime/Unsafe/GetPutAddress.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutAddress.java @@ -0,0 +1,47 @@ +/* + * 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 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(); + 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); + } +} diff --git a/test/runtime/Unsafe/GetPutBoolean.java b/test/runtime/Unsafe/GetPutBoolean.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutBoolean.java @@ -0,0 +1,58 @@ +/* + * 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 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; + } +} diff --git a/test/runtime/Unsafe/GetPutByte.java b/test/runtime/Unsafe/GetPutByte.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutByte.java @@ -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 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; + } +} diff --git a/test/runtime/Unsafe/GetPutChar.java b/test/runtime/Unsafe/GetPutChar.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutChar.java @@ -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 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'; + } +} diff --git a/test/runtime/Unsafe/GetPutDouble.java b/test/runtime/Unsafe/GetPutDouble.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutDouble.java @@ -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 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; + } +} diff --git a/test/runtime/Unsafe/GetPutFloat.java b/test/runtime/Unsafe/GetPutFloat.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutFloat.java @@ -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 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; + } +} diff --git a/test/runtime/Unsafe/GetPutInt.java b/test/runtime/Unsafe/GetPutInt.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutInt.java @@ -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; + } +} diff --git a/test/runtime/Unsafe/GetPutLong.java b/test/runtime/Unsafe/GetPutLong.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutLong.java @@ -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 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; + } +} diff --git a/test/runtime/Unsafe/GetPutObject.java b/test/runtime/Unsafe/GetPutObject.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutObject.java @@ -0,0 +1,60 @@ +/* + * 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 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(); + } +} diff --git a/test/runtime/Unsafe/GetPutShort.java b/test/runtime/Unsafe/GetPutShort.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetPutShort.java @@ -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 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; + } +} diff --git a/test/runtime/Unsafe/GetUnsafe.java b/test/runtime/Unsafe/GetUnsafe.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/GetUnsafe.java @@ -0,0 +1,43 @@ +/* + * 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 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"); + } +} diff --git a/test/runtime/Unsafe/PageSize.java b/test/runtime/Unsafe/PageSize.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/PageSize.java @@ -0,0 +1,47 @@ +/* + * 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 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); + } +} diff --git a/test/runtime/Unsafe/SetMemory.java b/test/runtime/Unsafe/SetMemory.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/SetMemory.java @@ -0,0 +1,43 @@ +/* + * 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 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); + } +} diff --git a/test/runtime/Unsafe/ThrowException.java b/test/runtime/Unsafe/ThrowException.java new file mode 100644 --- /dev/null +++ b/test/runtime/Unsafe/ThrowException.java @@ -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 + * @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 {} +}