vlivanov@7288: /* vlivanov@7288: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. vlivanov@7288: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. vlivanov@7288: * vlivanov@7288: * This code is free software; you can redistribute it and/or modify it vlivanov@7288: * under the terms of the GNU General Public License version 2 only, as vlivanov@7288: * published by the Free Software Foundation. vlivanov@7288: * vlivanov@7288: * This code is distributed in the hope that it will be useful, but WITHOUT vlivanov@7288: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or vlivanov@7288: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License vlivanov@7288: * version 2 for more details (a copy is included in the LICENSE file that vlivanov@7288: * accompanied this code). vlivanov@7288: * vlivanov@7288: * You should have received a copy of the GNU General Public License version vlivanov@7288: * 2 along with this work; if not, write to the Free Software Foundation, vlivanov@7288: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. vlivanov@7288: * vlivanov@7288: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA vlivanov@7288: * or visit www.oracle.com if you need additional information or have any vlivanov@7288: * questions. vlivanov@7288: */ vlivanov@7288: vlivanov@7288: /** vlivanov@7288: * @test vlivanov@7288: * @bug 8058828 vlivanov@7288: * @run main/bootclasspath -Xbatch VMAnonymousClasses vlivanov@7288: */ vlivanov@7288: vlivanov@7288: import jdk.internal.org.objectweb.asm.ClassWriter; vlivanov@7288: import jdk.internal.org.objectweb.asm.MethodVisitor; vlivanov@7288: import jdk.internal.org.objectweb.asm.Opcodes; vlivanov@7288: import sun.misc.Unsafe; vlivanov@7288: vlivanov@7288: import java.lang.invoke.ConstantCallSite; vlivanov@7288: import java.lang.invoke.MethodHandle; vlivanov@7288: import java.lang.invoke.MethodHandles; vlivanov@7288: import java.lang.invoke.MethodType; vlivanov@7288: import java.lang.invoke.MutableCallSite; vlivanov@7288: import java.lang.invoke.VolatileCallSite; vlivanov@7288: vlivanov@7288: public class VMAnonymousClasses { vlivanov@7288: static final String TEST_METHOD_NAME = "constant"; vlivanov@7288: vlivanov@7288: static final Unsafe UNSAFE = Unsafe.getUnsafe(); vlivanov@7288: vlivanov@7288: static int getConstantPoolSize(byte[] classFile) { vlivanov@7288: // The first few bytes: vlivanov@7288: // u4 magic; vlivanov@7288: // u2 minor_version; vlivanov@7288: // u2 major_version; vlivanov@7288: // u2 constant_pool_count; vlivanov@7288: return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF); vlivanov@7288: } vlivanov@7288: vlivanov@7288: static void test(Object value) throws ReflectiveOperationException { vlivanov@7288: System.out.printf("Test: %s", value != null ? value.getClass() : "null"); vlivanov@7288: vlivanov@7288: ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); vlivanov@7288: cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "Test", null, "java/lang/Object", null); vlivanov@7288: vlivanov@7288: MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, TEST_METHOD_NAME, "()Ljava/lang/Object;", null, null); vlivanov@7288: vlivanov@7288: String placeholder = "CONSTANT"; vlivanov@7288: int index = cw.newConst(placeholder); vlivanov@7288: mv.visitLdcInsn(placeholder); vlivanov@7288: mv.visitInsn(Opcodes.ARETURN); vlivanov@7288: vlivanov@7288: mv.visitMaxs(0, 0); vlivanov@7288: mv.visitEnd(); vlivanov@7288: vlivanov@7288: byte[] classFile = cw.toByteArray(); vlivanov@7288: vlivanov@7288: Object[] cpPatches = new Object[getConstantPoolSize(classFile)]; vlivanov@7288: cpPatches[index] = value; vlivanov@7288: vlivanov@7288: Class test = UNSAFE.defineAnonymousClass(VMAnonymousClasses.class, classFile, cpPatches); vlivanov@7288: vlivanov@7288: Object expectedResult = (value != null) ? value : placeholder; vlivanov@7288: for (int i = 0; i<15000; i++) { vlivanov@7288: Object result = test.getMethod(TEST_METHOD_NAME).invoke(null); vlivanov@7288: if (result != expectedResult) { vlivanov@7288: throw new AssertionError(String.format("Wrong value returned: %s != %s", value, result)); vlivanov@7288: } vlivanov@7288: } vlivanov@7288: System.out.println(" PASSED"); vlivanov@7288: } vlivanov@7288: vlivanov@7288: public static void main(String[] args) throws ReflectiveOperationException { vlivanov@7288: // Objects vlivanov@7288: test(new Object()); vlivanov@7288: test("TEST"); vlivanov@7288: test(new VMAnonymousClasses()); vlivanov@7288: test(null); vlivanov@7288: vlivanov@7288: // Class vlivanov@7288: test(String.class); vlivanov@7288: vlivanov@7288: // Arrays vlivanov@7288: test(new boolean[0]); vlivanov@7288: test(new byte[0]); vlivanov@7288: test(new char[0]); vlivanov@7288: test(new short[0]); vlivanov@7288: test(new int[0]); vlivanov@7288: test(new long[0]); vlivanov@7288: test(new float[0]); vlivanov@7288: test(new double[0]); vlivanov@7288: test(new Object[0]); vlivanov@7288: vlivanov@7288: // Multi-dimensional arrays vlivanov@7288: test(new byte[0][0]); vlivanov@7288: test(new Object[0][0]); vlivanov@7288: vlivanov@7288: // MethodHandle-related vlivanov@7288: MethodType mt = MethodType.methodType(void.class, String[].class); vlivanov@7288: MethodHandle mh = MethodHandles.lookup().findStatic(VMAnonymousClasses.class, "main", mt); vlivanov@7288: test(mt); vlivanov@7288: test(mh); vlivanov@7288: test(new ConstantCallSite(mh)); vlivanov@7288: test(new MutableCallSite(MethodType.methodType(void.class))); vlivanov@7288: test(new VolatileCallSite(MethodType.methodType(void.class))); vlivanov@7288: vlivanov@7288: System.out.println("TEST PASSED"); vlivanov@7288: } vlivanov@7288: }