1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @library /test/lib
27 * @summary Test that retransforming and redefining anonymous classes gets UnmodifiableClassException
28 * @modules java.base/jdk.internal.misc
29 * @modules java.instrument
30 *          jdk.jartool/sun.tools.jar
31 * @run main ModifyAnonymous buildagent
32 * @run main/othervm -javaagent:redefineagent.jar ModifyAnonymous
33 */
34
35import java.io.FileNotFoundException;
36import java.io.PrintWriter;
37import java.lang.RuntimeException;
38import java.lang.instrument.ClassDefinition;
39import java.lang.instrument.ClassFileTransformer;
40import java.lang.instrument.IllegalClassFormatException;
41import java.lang.instrument.Instrumentation;
42import java.security.ProtectionDomain;
43
44import jdk.test.lib.compiler.InMemoryJavaCompiler;
45
46public class ModifyAnonymous {
47
48    public static class LambdaTransformer implements ClassFileTransformer {
49        @Override
50        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
51                                ProtectionDomain protectionDomain, byte[] classfileBuffer)
52        throws IllegalClassFormatException {
53            return null;
54        }
55    }
56
57    static Instrumentation inst = null;
58    static volatile boolean done = false;
59
60    public static void premain(String args, Instrumentation instrumentation) {
61
62        inst = instrumentation;
63        System.out.println("javaagent in da house!");
64        instrumentation.addTransformer(new LambdaTransformer());
65    }
66
67    private static void buildAgent() {
68        try {
69            ClassFileInstaller.main("ModifyAnonymous");
70        } catch (Exception e) {
71            throw new RuntimeException("Could not write agent classfile", e);
72        }
73
74        try {
75            PrintWriter pw = new PrintWriter("MANIFEST.MF");
76            pw.println("Premain-Class: ModifyAnonymous");
77            pw.println("Agent-Class: ModifyAnonymous");
78            pw.println("Can-Retransform-Classes: true");
79            pw.println("Can-Redefine-Classes: true");
80            pw.close();
81        } catch (FileNotFoundException e) {
82            throw new RuntimeException("Could not write manifest file for the agent", e);
83        }
84
85        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
86        if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "ModifyAnonymous.class" })) {
87            throw new RuntimeException("Could not write the agent jar file");
88        }
89    }
90
91    public static class InstanceMethodCallSiteApp {
92
93        public static void test() throws InterruptedException {
94            for (int i = 0; i < 2; i++) {
95                InstanceMethodCallSiteApp app = new InstanceMethodCallSiteApp();
96                Runnable r = app::doWork;   // this creates an anonymous class
97                while (!done) {
98                    r.run();
99                    Thread.sleep(10);
100                }
101            }
102        }
103
104        public void doWork() {
105            System.out.print(".");
106        }
107    }
108
109    static void runTest() {
110        while (!done) {
111            Class[] allLoadedClasses = inst.getAllLoadedClasses();
112            for (Class clazz : allLoadedClasses) {
113                final String name = clazz.getName();
114                if (name.contains("$$Lambda$") && name.contains("App")) {
115                    if (inst.isModifiableClass(clazz)) {
116                        throw new RuntimeException ("Class should not be modifiable");
117                    }
118                    // Try to modify them anyway.
119                    try {
120                        System.out.println("retransform called for " + name);
121                        inst.retransformClasses(clazz);
122                    } catch(java.lang.instrument.UnmodifiableClassException t) {
123                        System.out.println("PASSED: expecting UnmodifiableClassException");
124                        t.printStackTrace();
125                    }
126                    try {
127                        System.out.println("redefine called for " + name);
128                        String newclass = "class Dummy {}";
129                        byte[] bytecode = InMemoryJavaCompiler.compile("Dummy", newclass);
130                        ClassDefinition cld = new ClassDefinition(clazz, bytecode);
131                        inst.redefineClasses(new ClassDefinition[] { cld });
132                    } catch(java.lang.instrument.UnmodifiableClassException t) {
133                        System.out.println("PASSED: expecting UnmodifiableClassException");
134                        t.printStackTrace();
135                    } catch(java.lang.ClassNotFoundException e) {
136                        throw new RuntimeException ("ClassNotFoundException thrown");
137                    }
138                    done = true;
139                }
140            }
141        }
142    }
143
144    public static void main(String argv[]) throws InterruptedException, RuntimeException {
145        if (argv.length == 1 && argv[0].equals("buildagent")) {
146            buildAgent();
147            return;
148        }
149
150        if (inst == null) {
151            throw new RuntimeException("Instrumentation object was null");
152        }
153
154        new Thread() {
155            public void run() {
156                runTest();
157            }
158        }.start();
159
160        // Test that NCDFE is not thrown for anonymous class:
161        // ModifyAnonymous$InstanceMethodCallSiteApp$$Lambda$18
162        try {
163            ModifyAnonymous test = new ModifyAnonymous();
164            InstanceMethodCallSiteApp.test();
165        } catch (NoClassDefFoundError e) {
166            throw new RuntimeException("FAILED: NoClassDefFoundError thrown for " + e.getMessage());
167        }
168        System.out.println("PASSED: NoClassDefFound error not thrown");
169    }
170}
171