1 /*
   2  * Copyright (c) 2005, 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 /**
  25  * @test
  26  * @bug 6263319
  27  * @summary test setNativeMethodPrefix
  28  * @author Robert Field, Sun Microsystems
  29  *
  30  * @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'
  31  * @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp
  32  */
  33 
  34 import java.lang.instrument.*;
  35 import java.security.ProtectionDomain;
  36 import java.io.*;
  37 
  38 import asmlib.*;
  39 
  40 class NativeMethodPrefixAgent {
  41 
  42     static ClassFileTransformer t0, t1, t2;
  43     static Instrumentation inst;
  44 
  45     static class Tr implements ClassFileTransformer {
  46         final String trname;
  47         final int transformId;
  48 
  49         Tr(int transformId) {
  50             this.trname = "tr" + transformId;
  51             this.transformId = transformId;
  52         }
  53 
  54         public byte[]
  55         transform(
  56             ClassLoader loader,
  57             String className,
  58             Class<?> classBeingRedefined,
  59             ProtectionDomain    protectionDomain,
  60             byte[] classfileBuffer) {
  61             boolean redef = classBeingRedefined != null;
  62             System.out.println(trname + ": " +
  63                                (redef? "Retransforming " : "Loading ") + className);
  64             if (className != null) {
  65                 try {
  66                     byte[] newcf = Instrumentor.instrFor(classfileBuffer)
  67                                    .addNativeMethodTrackingInjection(
  68                                         "wrapped_" + trname + "_",
  69                                         (h)->{
  70                                             h.push(h.getName());
  71                                             h.push(transformId);
  72                                             h.invokeStatic("bootreporter/StringIdCallbackReporter", "tracker", "(Ljava/lang/String;I)V", false);
  73                                         })
  74                                    .apply();
  75                     /*** debugging ...
  76                     if (newcf != null) {
  77                         String fname = trname + (redef?"_redef" : "") + "/" + className;
  78                         System.err.println("dumping to: " + fname);
  79                         write_buffer(fname + "_before.class", classfileBuffer);
  80                         write_buffer(fname + "_instr.class", newcf);
  81                     }
  82                     ***/
  83 
  84                     return redef? null : newcf;
  85                 } catch (Throwable ex) {
  86                     System.err.println("ERROR: Injection failure: " + ex);
  87                     ex.printStackTrace();
  88                     System.err.println("Returning bad class file, to cause test failure");
  89                     return new byte[0];
  90                 }
  91             }
  92             return null;
  93         }
  94 
  95     }
  96 
  97     // for debugging
  98     static void write_buffer(String fname, byte[]buffer) {
  99         try {
 100             File f = new File(fname);
 101             if (!f.getParentFile().exists()) {
 102                 f.getParentFile().mkdirs();
 103             }
 104             try (FileOutputStream outStream = new FileOutputStream(f)) {
 105                 outStream.write(buffer, 0, buffer.length);
 106             }
 107         } catch (IOException ex) {
 108             System.err.println("EXCEPTION in write_buffer: " + ex);
 109         }
 110     }
 111 
 112     public static void
 113     premain (String agentArgs, Instrumentation instArg)
 114         throws IOException, IllegalClassFormatException,
 115         ClassNotFoundException, UnmodifiableClassException {
 116         inst = instArg;
 117         System.out.println("Premain");
 118 
 119         t1 = new Tr(1);
 120         t2 = new Tr(2);
 121         t0 = new Tr(0);
 122         inst.addTransformer(t1, true);
 123         inst.addTransformer(t2, false);
 124         inst.addTransformer(t0, true);
 125         instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");
 126         instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");
 127         instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");
 128 
 129         // warm up: cause load of transformer classes before used during class load
 130         instArg.retransformClasses(Runtime.class);
 131     }
 132 }