--- old/src/share/vm/classfile/classFileParser.cpp 2016-08-03 07:54:48.993830289 -0400 +++ new/src/share/vm/classfile/classFileParser.cpp 2016-08-03 07:54:47.798426849 -0400 @@ -5402,6 +5402,67 @@ debug_only(ik->verify();) } +// For an anonymous class that is in the null package, move it to its host class's +// package by prepending its host class's package name to its class name and setting +// its _class_name field. +void ClassFileParser::prepend_host_package_name(const Klass* host_klass, TRAPS) { + ResourceMark rm(THREAD); + assert(strrchr(_class_name->as_C_string(), '/') == NULL, + "Anonymous class should not be in a package"); + int host_pkg_len = 0; + const char* host_pkg_name = + (const char*)InstanceKlass::package_from_name(host_klass->name(), CHECK); + if (host_pkg_name != NULL) { + char* new_anon_name = + NEW_RESOURCE_ARRAY(char, host_pkg_len + 1 + _class_name->utf8_length()); + // Copy host package name and trailing /. + strncpy(new_anon_name, host_pkg_name, host_pkg_len); + new_anon_name[host_pkg_len] = '/'; + // Append anonymous class name. The anonymous class name can contain odd + // characters. So, do a strncpy instead of using sprintf("%s..."). + strncpy(new_anon_name + host_pkg_len + 1, (char *)_class_name->base(), + _class_name->utf8_length()); + + // Create a symbol and update the anonymous class name. + _class_name = SymbolTable::lookup(new_anon_name, + host_pkg_len + 1 + _class_name->utf8_length(), + CHECK); + } +} + +// If the host class and the anonymous class are in the same package then do +// nothing. If the anonymous class is in the null package then move it to its +// host's package. If the classes are in different packages then throw an IAE +// exception. +void ClassFileParser::fix_anonymous_class_name(TRAPS) { + assert(_host_klass != NULL, "Expected an anonymous class"); + const jbyte* anon_last_slash = UTF8::strrchr(_class_name->base(), + _class_name->utf8_length(), '/'); + const Klass* host_klass; + if (_host_klass->is_objArray_klass()) { + host_klass = ObjArrayKlass::cast(_host_klass)->element_klass(); + } else { + host_klass = _host_klass; + } + assert(host_klass->is_instance_klass(), "host klass is not an instance class"); + + if (anon_last_slash == NULL) { // Unnamed package + prepend_host_package_name(host_klass, CHECK); + + } else { + if (!InstanceKlass::is_same_class_package(host_klass->class_loader(), + host_klass->name(), + host_klass->class_loader(), + _class_name)) { + ResourceMark rm(THREAD); + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), + err_msg("Host class %s and anonymous class %s are in different packages", + _host_klass->name()->as_C_string(), _class_name->as_C_string())); + } + } +} + + ClassFileParser::ClassFileParser(ClassFileStream* stream, Symbol* name, ClassLoaderData* loader_data, @@ -5681,6 +5742,13 @@ return; } + // if this is an anonymous class fix up its name if it's in the unnamed + // package. Otherwise, throw IAE if it is in a different package than + // its host class. + if (_host_klass != NULL) { + fix_anonymous_class_name(CHECK); + } + // Verification prevents us from creating names with dots in them, this // asserts that that's the case. assert(is_internal_format(_class_name), "external class name format used internally"); --- old/src/share/vm/classfile/classFileParser.hpp 2016-08-03 07:55:04.262072345 -0400 +++ new/src/share/vm/classfile/classFileParser.hpp 2016-08-03 07:55:02.576094668 -0400 @@ -155,6 +155,9 @@ ConstantPool* cp, TRAPS); + void prepend_host_package_name(const Klass* host_klass, TRAPS); + void fix_anonymous_class_name(TRAPS); + void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS); void set_klass(InstanceKlass* instance); --- old/src/share/vm/prims/unsafe.cpp 2016-08-03 07:55:16.526355484 -0400 +++ new/src/share/vm/prims/unsafe.cpp 2016-08-03 07:55:15.279990205 -0400 @@ -787,8 +787,12 @@ // link to any member of U. Just after U is loaded, the only way to use it is reflectively, // through java.lang.Class methods like Class.newInstance. +// The package of an anonymous class must either match its host's class's package or be in the +// unnamed package. If it is in the unnamed package then it will be put in its host class's +// package. +// + // Access checks for linkage sites within U continue to follow the same rules as for named classes. -// The package of an anonymous class is given by the package qualifier on the name under which it was loaded. // An anonymous class also has special privileges to access any member of its host class. // This is the main reason why this loading operation is unsafe. The purpose of this is to // allow language implementations to simulate "open classes"; a host class in effect gets --- old/test/compiler/jsr292/CallSiteDepContextTest.java 2016-08-03 07:55:31.736324377 -0400 +++ new/test/compiler/jsr292/CallSiteDepContextTest.java 2016-08-03 07:55:30.659428049 -0400 @@ -62,7 +62,7 @@ public class CallSiteDepContextTest { static final Unsafe UNSAFE = Unsafe.getUnsafe(); static final MethodHandles.Lookup LOOKUP = MethodHandleHelper.IMPL_LOOKUP; - static final String CLASS_NAME = "java/lang/invoke/Test"; + static final String CLASS_NAME = "compiler/jsr292/Test"; static final String METHOD_NAME = "m"; static final MethodType TYPE = MethodType.methodType(int.class); @@ -129,8 +129,8 @@ } public static void testSharedCallSite() throws Throwable { - Class cls1 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("CS_1"), null); - Class cls2 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("CS_2"), null); + Class cls1 = UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("CS_1"), null); + Class cls2 = UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("CS_2"), null); MethodHandle[] mhs = new MethodHandle[] { LOOKUP.findStatic(cls1, METHOD_NAME, TYPE), @@ -151,7 +151,7 @@ execute(1, mh); // mcs.context == cls1 - Class cls1 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("NonBound_1"), null); + Class cls1 = UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("NonBound_1"), null); MethodHandle mh1 = LOOKUP.findStatic(cls1, METHOD_NAME, TYPE); execute(1, mh1); @@ -170,8 +170,8 @@ mcs = new MutableCallSite(LOOKUP.findStatic(T.class, "f1", TYPE)); Class[] cls = new Class[] { - UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_1" + id), null), - UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_2" + id), null), + UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("GC_1" + id), null), + UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("GC_2" + id), null), }; MethodHandle[] mhs = new MethodHandle[] { @@ -185,7 +185,7 @@ execute(1, mhs); ref = new PhantomReference<>(cls[0], rq); - cls[0] = UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_3" + id), null); + cls[0] = UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("GC_3" + id), null); mhs[0] = LOOKUP.findStatic(cls[0], METHOD_NAME, TYPE); do { --- /dev/null 2016-07-14 12:44:45.952133411 -0400 +++ new/test/runtime/defineAnonClass/DefineAnon.java 2016-08-03 07:55:45.224059051 -0400 @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016, 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 DefineAnon + * @bug 8058575 + * @library /testlibrary + * @modules java.base/jdk.internal.org.objectweb.asm + * java.management + * @compile -XDignore.symbol.file=true DefineAnon.java + * @run main/othervm p1.DefineAnon + */ + +package p1; + +import jdk.internal.org.objectweb.asm.ClassWriter; +import jdk.internal.org.objectweb.asm.MethodVisitor; +import jdk.internal.org.objectweb.asm.Opcodes; +import sun.misc.Unsafe; +import jdk.test.lib.*; + + +class T { + static protected void test0() { System.out.println("test0 (public)"); } + static protected void test1() { System.out.println("test1 (protected)"); } + static /*package-private*/ void test2() { System.out.println("test2 (package)"); } + static private void test3() { System.out.println("test3 (private)"); } +} + +public class DefineAnon { + + private static Unsafe getUnsafe() { + try { + java.lang.reflect.Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe"); + singleoneInstanceField.setAccessible(true); + return (Unsafe) singleoneInstanceField.get(null); + } catch (Throwable ex) { + throw new RuntimeException("Was unable to get Unsafe instance."); + } + } + + static Unsafe UNSAFE = DefineAnon.getUnsafe(); + + static Class getAnonClass(Class hostClass, final String className) { + final String superName = "java/lang/Object"; + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null); + + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "test", "()V", null, null); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test0", "()V", false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test1", "()V", false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test2", "()V", false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test3", "()V", false); + mv.visitInsn(Opcodes.RETURN); + mv.visitMaxs(0, 0); + mv.visitEnd(); + + final byte[] classBytes = cw.toByteArray(); + Class invokerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, new Object[0]); + UNSAFE.ensureClassInitialized(invokerClass); + return invokerClass; + } + + public static void main(String[] args) throws Throwable { + Throwable fail = null; + + // Anonymous class has the privileges of its host class, so test[0123] should all work. + System.out.println("Injecting from the same package (p1):"); + Class p1cls = getAnonClass(T.class, "p1/AnonClass"); + try { + p1cls.getMethod("test").invoke(null); + } catch (Throwable ex) { + ex.printStackTrace(); + fail = ex; // throw this to make test fail, since subtest failed + } + + // Anonymous class has different package name from host class. Should throw + // IllegalArgumentException. + System.out.println("Injecting from the wrong package (p2):"); + try { + Class p2cls = getAnonClass(DefineAnon.class, "p2/AnonClass"); + p2cls.getMethod("test").invoke(null); + System.out.println("Failed, did not get expected IllegalArgumentException"); + } catch (java.lang.IllegalArgumentException e) { + System.out.println("Got expected IllegalArgumentException: " + e.getMessage()); + } catch (Throwable ex) { + ex.printStackTrace(); + fail = ex; // throw this to make test fail, since subtest failed + } + + // Inject a class in the unnamed package into p1.T. It should be able + // to access all methods in p1.T. + System.out.println("Injecting unnamed package into correct host class:"); + try { + Class p3cls = getAnonClass(T.class, "AnonClass"); + p3cls.getMethod("test").invoke(null); + } catch (Throwable ex) { + ex.printStackTrace(); + fail = ex; // throw this to make test fail, since subtest failed + } + + if (fail != null) throw fail; // make test fail, since subtest failed + } +}