1 /*
   2  * Copyright (c) 2014, 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 import java.security.*;
  25 import java.lang.instrument.*;
  26 import java.lang.reflect.*;
  27 import java.lang.management.ManagementFactory;
  28 import com.sun.tools.attach.VirtualMachine;
  29 
  30 class A {
  31     void m() {
  32     }
  33 }
  34 
  35 class B extends A {
  36     void m() {
  37     }
  38 }
  39 
  40 class C extends A {
  41     void m() {
  42     }
  43 }
  44 
  45 class Test {
  46 
  47     static public void m() throws Exception {
  48         for (int i = 0; i < 20000; i++) {
  49             m1(a);
  50         }
  51         for (int i = 0; i < 4; i++) {
  52             m1(b);
  53         }
  54     }
  55 
  56     static boolean m1(A a) {
  57         boolean res =  Agent.m2(a);
  58         return res;
  59     }
  60 
  61     static public A a = new A();
  62     static public B b = new B();
  63     static public C c = new C();
  64 }
  65 
  66 public class Agent implements ClassFileTransformer {
  67 
  68 
  69     static public boolean m2(A a) {
  70         boolean res = false;
  71         if (a.getClass() == B.class) {
  72             a.m();
  73         } else {
  74             res = true;
  75         }
  76         return res;
  77     }
  78 
  79     static public void main(String[] args) throws Exception {
  80         // Create speculative trap entries
  81         Test.m();
  82 
  83         String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
  84         int p = nameOfRunningVM.indexOf('@');
  85         String pid = nameOfRunningVM.substring(0, p);
  86 
  87         // Make the nmethod go away
  88         for (int i = 0; i < 10; i++) {
  89             System.gc();
  90         }
  91 
  92         // Redefine class
  93         try {
  94             VirtualMachine vm = VirtualMachine.attach(pid);
  95             vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
  96             vm.detach();
  97         } catch (Exception e) {
  98             throw new RuntimeException(e);
  99         }
 100 
 101         Test.m();
 102         // GC will hit dead method pointer
 103         for (int i = 0; i < 10; i++) {
 104             System.gc();
 105         }
 106     }
 107 
 108     public synchronized byte[] transform(final ClassLoader classLoader,
 109                                          final String className,
 110                                          Class<?> classBeingRedefined,
 111                                          ProtectionDomain protectionDomain,
 112                                          byte[] classfileBuffer) {
 113         System.out.println("Transforming class " + className);
 114         return classfileBuffer;
 115     }
 116 
 117     public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
 118 
 119         try {
 120             instrumentation.retransformClasses(to_redefine);
 121         } catch (Exception e) {
 122             e.printStackTrace();
 123         }
 124 
 125     }
 126 
 127     public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
 128         Agent transformer = new Agent();
 129         instrumentation.addTransformer(transformer, true);
 130 
 131         redefine(agentArgs, instrumentation, Test.class);
 132     }
 133 }