1 /* 2 * Copyright (c) 2016, 2018, 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 35 import java.io.FileNotFoundException; 36 import java.io.PrintWriter; 37 import java.lang.RuntimeException; 38 import java.lang.instrument.ClassDefinition; 39 import java.lang.instrument.ClassFileTransformer; 40 import java.lang.instrument.IllegalClassFormatException; 41 import java.lang.instrument.Instrumentation; 42 import java.security.ProtectionDomain; 43 44 import jdk.test.lib.compiler.InMemoryJavaCompiler; 45 46 public 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 int len = allLoadedClasses.length; 113 for (int idx = 0; idx < len; idx++) { 114 Class clazz = allLoadedClasses[idx]; 115 if (clazz == null) { 116 System.out.println(" null clazz, allLoadedClasses[] length: " + 117 len + " index: " + idx); 118 continue; 119 } 120 final String name = clazz.getName(); 121 if (name.contains("$$Lambda$") && name.contains("App")) { 122 if (inst.isModifiableClass(clazz)) { 123 throw new RuntimeException ("Class should not be modifiable"); 124 } 125 // Try to modify them anyway. 126 try { 127 System.out.println("retransform called for " + name); 128 inst.retransformClasses(clazz); 129 } catch(java.lang.instrument.UnmodifiableClassException t) { 130 System.out.println("PASSED: expecting UnmodifiableClassException"); 131 t.printStackTrace(); 132 } 133 try { 134 System.out.println("redefine called for " + name); 135 String newclass = "class Dummy {}"; 136 byte[] bytecode = InMemoryJavaCompiler.compile("Dummy", newclass); 137 ClassDefinition cld = new ClassDefinition(clazz, bytecode); 138 inst.redefineClasses(new ClassDefinition[] { cld }); 139 } catch(java.lang.instrument.UnmodifiableClassException t) { 140 System.out.println("PASSED: expecting UnmodifiableClassException"); 141 t.printStackTrace(); 142 } catch(java.lang.ClassNotFoundException e) { 143 throw new RuntimeException ("ClassNotFoundException thrown"); 144 } 145 done = true; 146 } 147 } 148 } 149 } 150 151 public static void main(String argv[]) throws InterruptedException, RuntimeException { 152 if (argv.length == 1 && argv[0].equals("buildagent")) { 153 buildAgent(); 154 return; 155 } 156 157 if (inst == null) { 158 throw new RuntimeException("Instrumentation object was null"); 159 } 160 161 new Thread() { 162 public void run() { 163 runTest(); 164 } 165 }.start(); 166 167 // Test that NCDFE is not thrown for anonymous class: 168 // ModifyAnonymous$InstanceMethodCallSiteApp$$Lambda$18 169 try { 170 ModifyAnonymous test = new ModifyAnonymous(); 171 InstanceMethodCallSiteApp.test(); 172 } catch (NoClassDefFoundError e) { 173 throw new RuntimeException("FAILED: NoClassDefFoundError thrown for " + e.getMessage()); 174 } 175 System.out.println("PASSED: NoClassDefFound error not thrown"); 176 } 177 }