jdk/test/java/lang/instrument/NativeMethodPrefixAgent.java
author shurailine
Tue, 27 Oct 2015 20:06:02 -0700
changeset 33402 1156d495a525
parent 30376 2ccf2cf7ea48
child 36427 1be706dbb3b8
permissions -rw-r--r--
8140336: Add @modules for exported dependencies to jdk_core tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26205
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6263319
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test setNativeMethodPrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Robert Field, Sun Microsystems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26205
diff changeset
    30
 * @modules java.base/jdk.internal.org.objectweb.asm
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30376
diff changeset
    31
 *          java.management
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30376
diff changeset
    32
 *          java.instrument
277
9d9c1747de00 6528548: 4/4 NativeMethodPrefixAgent.java times out intermittently in nightly
dcubed
parents: 2
diff changeset
    33
 * @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.instrument.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    41
import asmlib.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class NativeMethodPrefixAgent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static ClassFileTransformer t0, t1, t2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static Instrumentation inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static class Tr implements ClassFileTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final String trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final int transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Tr(int transformId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            this.trname = "tr" + transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            this.transformId = transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        transform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            boolean redef = classBeingRedefined != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println(trname + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                               (redef? "Retransforming " : "Loading ") + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (className != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                try {
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    69
                    byte[] newcf = Instrumentor.instrFor(classfileBuffer)
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    70
                                   .addNativeMethodTrackingInjection(
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    71
                                        "wrapped_" + trname + "_",
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    72
                                        (h)->{
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    73
                                            h.push(h.getName());
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    74
                                            h.push(transformId);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    75
                                            h.invokeStatic("bootreporter/StringIdCallbackReporter", "tracker", "(Ljava/lang/String;I)V", false);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    76
                                        })
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    77
                                   .apply();
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    78
                    /*** debugging ...
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    79
                    if (newcf != null) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    80
                        String fname = trname + (redef?"_redef" : "") + "/" + className;
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    81
                        System.err.println("dumping to: " + fname);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    82
                        write_buffer(fname + "_before.class", classfileBuffer);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    83
                        write_buffer(fname + "_instr.class", newcf);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    84
                    }
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    85
                    ***/
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return redef? null : newcf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    System.err.println("ERROR: Injection failure: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    System.err.println("Returning bad class file, to cause test failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // for debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void write_buffer(String fname, byte[]buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   103
            File f = new File(fname);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   104
            if (!f.getParentFile().exists()) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   105
                f.getParentFile().mkdirs();
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   106
            }
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   107
            try (FileOutputStream outStream = new FileOutputStream(f)) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   108
                outStream.write(buffer, 0, buffer.length);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   109
            }
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   110
        } catch (IOException ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.err.println("EXCEPTION in write_buffer: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    premain (String agentArgs, Instrumentation instArg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        throws IOException, IllegalClassFormatException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ClassNotFoundException, UnmodifiableClassException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        inst = instArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Premain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        t1 = new Tr(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        t2 = new Tr(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        t0 = new Tr(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        inst.addTransformer(t1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        inst.addTransformer(t2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        inst.addTransformer(t0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // warm up: cause load of transformer classes before used during class load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        instArg.retransformClasses(Runtime.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}