jdk/test/java/lang/instrument/RetransformAgent.java
author sspitsyn
Sat, 09 Jul 2016 21:41:14 -0700
changeset 39739 5d058d6bd245
parent 36511 9d0388c6b336
child 44545 83b611b88ac8
permissions -rw-r--r--
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method Summary: Add ClassLoader parameter to new ClassFileTransformer transform method Reviewed-by: alanb, dholmes, dcubed, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36237
963d1115678b 8150998: Fix module dependences in java/lang tests
shurailine
parents: 30376
diff changeset
     2
 * Copyright (c) 2005, 2016, 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 6274264 6274241 5070281
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test retransformClasses
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
36237
963d1115678b 8150998: Fix module dependences in java/lang tests
shurailine
parents: 30376
diff changeset
    31
 *          java.instrument
277
9d9c1747de00 6528548: 4/4 NativeMethodPrefixAgent.java times out intermittently in nightly
dcubed
parents: 2
diff changeset
    32
 * @run shell/timeout=240 MakeJAR2.sh RetransformAgent RetransformApp 'Can-Retransform-Classes: true'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main/othervm -javaagent:RetransformAgent.jar RetransformApp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.instrument.*;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36237
diff changeset
    37
import java.lang.reflect.Module;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    40
import asmlib.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class RetransformAgent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static ClassFileTransformer t1, t2, t3, t4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static Instrumentation inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static boolean succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int markCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static int[] markGolden = {30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40, 20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                               11, 40, 20, 11, 40, 20, 11, 40, 20, 11, 40, 20};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static class Tr implements ClassFileTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        final String trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final boolean onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final int loadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final boolean onRedef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final int redefIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        final String cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final String nname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Tr(String trname, boolean onLoad, int loadIndex, boolean onRedef, int redefIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           String cname, String nname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.trname = trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            this.onLoad = onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.loadIndex = loadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.onRedef = onRedef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.redefIndex = redefIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.cname = cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            this.nname = nname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36237
diff changeset
    71
        public byte[] transform(Module module,
39739
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 36511
diff changeset
    72
                                ClassLoader loader,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            boolean redef = classBeingRedefined != null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 36237
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // System.err.println("hook " + trname + ": " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            //                    (redef? " REDEF" : " LOAD"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if ((redef? onRedef : onLoad) && className != null && className.equals(cname)) {
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    82
                int fixedIndex = redef ? redefIndex : loadIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                try {
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    84
                    byte[] newcf = Instrumentor.instrFor(classfileBuffer)
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    85
                                   .addMethodEntryInjection(
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    86
                                        nname,
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    87
                                        (h)->{
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    88
                                           h.push(fixedIndex);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    89
                                           h.invokeStatic("RetransformAgent", "callTracker", "(I)V", false);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    90
                                        })
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    91
                                   .apply();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    /*** debugging ...
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    93
                         if (newcf != null) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    94
                            String fname = trname + (redef?"_redef" : "") + "/" + className;
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    95
                            System.err.println("dumping to: " + fname);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    96
                            write_buffer(fname + "_before.class", classfileBuffer);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    97
                            write_buffer(fname + "_instr.class", newcf);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
    98
                         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    ***/
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   100
                    System.err.println(trname + ": " + className + " index: " + fixedIndex +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                       (redef? " REDEF" : " LOAD") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                       " len before: " + classfileBuffer.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                       " after: " + newcf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    return newcf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    System.err.println("Injection failure: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static void write_buffer(String fname, byte[]buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        try {
26205
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   116
            File f = new File(fname);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   117
            if (!f.getParentFile().exists()) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   118
                f.getParentFile().mkdirs();
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   119
            }
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   120
            try (FileOutputStream outStream = new FileOutputStream(f)) {
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   121
                outStream.write(buffer, 0, buffer.length);
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   122
            }
c073791a67de 8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents: 5506
diff changeset
   123
        } catch (IOException ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.err.println("EXCEPTION in write_buffer: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static void premain (String agentArgs, Instrumentation instArg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        inst = instArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.err.println("Premain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        t1 = new Tr("TR1", false, 10, true, 11, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        inst.addTransformer(t1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        t2 = new Tr("TN2", true,  20, true, 21, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        inst.addTransformer(t2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        t3 = new Tr("TR3", true,  30, true, 31, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        inst.addTransformer(t3, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        t4 = new Tr("TN4", true,  40, true, 41, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        inst.addTransformer(t4, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static void undo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        inst.removeTransformer(t3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.err.println("RETRANSFORM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            inst.retransformClasses(new RetransformApp().getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.err.println("EXCEPTION on undo: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static boolean succeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return succeeded && markCount == markGolden.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static void callTracker(int mark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.err.println("got mark " + mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (markCount >= markGolden.length || mark != markGolden[markCount++]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}