jdk-24/test/jdk/java/lang/instrument/RetransformAgent.java

159 lines
6.5 KiB
Java
Raw Normal View History

2007-12-01 00:00:00 +00:00
/*
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
2007-12-01 00:00:00 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
2007-12-01 00:00:00 +00:00
*/
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
2007-12-01 00:00:00 +00:00
import java.lang.instrument.*;
import java.security.ProtectionDomain;
import java.io.*;
import asmlib.*;
2007-12-01 00:00:00 +00:00
import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.CD_void;
2007-12-01 00:00:00 +00:00
class RetransformAgent {
static ClassFileTransformer t1, t2, t3, t4;
static Instrumentation inst;
static boolean succeeded = true;
static int markCount = 0;
static int[] markGolden = {30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40, 20,
11, 40, 20, 11, 40, 20, 11, 40, 20, 11, 40, 20};
static class Tr implements ClassFileTransformer {
private static final ClassDesc CD_RetransformAgent = RetransformAgent.class.describeConstable().orElseThrow();
private static final MethodTypeDesc MTD_void_int = MethodTypeDesc.of(CD_void, CD_int);
2007-12-01 00:00:00 +00:00
final String trname;
final boolean onLoad;
final int loadIndex;
final boolean onRedef;
final int redefIndex;
final String cname;
final String nname;
Tr(String trname, boolean onLoad, int loadIndex, boolean onRedef, int redefIndex,
String cname, String nname) {
this.trname = trname;
this.onLoad = onLoad;
this.loadIndex = loadIndex;
this.onRedef = onRedef;
this.redefIndex = redefIndex;
this.cname = cname;
this.nname = nname;
}
8142968: Module System implementation Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Co-authored-by: Alex Buckley <alex.buckley@oracle.com> Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com> Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com> Co-authored-by: Mandy Chung <mandy.chung@oracle.com> Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com> Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com> Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com> Co-authored-by: Amy Lu <amy.lu@oracle.com> Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com> Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com> Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com> Co-authored-by: Harold Seigel <harold.seigel@oracle.com> Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com> Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com> Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com> Co-authored-by: James Laskey <james.laskey@oracle.com> Co-authored-by: Lois Foltan <lois.foltan@oracle.com> Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com> Co-authored-by: Huaming Li <huaming.li@oracle.com> Co-authored-by: Sean Mullan <sean.mullan@oracle.com> Co-authored-by: Naoto Sato <naoto.sato@oracle.com> Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com> Co-authored-by: Peter Levart <peter.levart@gmail.com> Co-authored-by: Philip Race <philip.race@oracle.com> Co-authored-by: Claes Redestad <claes.redestad@oracle.com> Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com> Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com> Co-authored-by: Volker Simonis <volker.simonis@gmail.com> Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com> Co-authored-by: Stuart Marks <stuart.marks@oracle.com> Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com> Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com> Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com> Co-authored-by: Valerie Peng <valerie.peng@oracle.com> Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com> Co-authored-by: Weijun Wang <weijun.wang@oracle.com> Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com> Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com> Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com> Co-authored-by: Felix Yang <felix.yang@oracle.com> Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com> Co-authored-by: Frank Yuan <frank.yuan@oracle.com> Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com> Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com> Co-authored-by: Tiantian Du <tiantian.du@oracle.com> Co-authored-by: Sha Jiang <sha.jiang@oracle.com> Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
public byte[] transform(Module module,
ClassLoader loader,
2007-12-01 00:00:00 +00:00
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
boolean redef = classBeingRedefined != null;
8142968: Module System implementation Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Co-authored-by: Alex Buckley <alex.buckley@oracle.com> Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com> Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com> Co-authored-by: Mandy Chung <mandy.chung@oracle.com> Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com> Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com> Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com> Co-authored-by: Amy Lu <amy.lu@oracle.com> Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com> Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com> Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com> Co-authored-by: Harold Seigel <harold.seigel@oracle.com> Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com> Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com> Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com> Co-authored-by: James Laskey <james.laskey@oracle.com> Co-authored-by: Lois Foltan <lois.foltan@oracle.com> Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com> Co-authored-by: Huaming Li <huaming.li@oracle.com> Co-authored-by: Sean Mullan <sean.mullan@oracle.com> Co-authored-by: Naoto Sato <naoto.sato@oracle.com> Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com> Co-authored-by: Peter Levart <peter.levart@gmail.com> Co-authored-by: Philip Race <philip.race@oracle.com> Co-authored-by: Claes Redestad <claes.redestad@oracle.com> Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com> Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com> Co-authored-by: Volker Simonis <volker.simonis@gmail.com> Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com> Co-authored-by: Stuart Marks <stuart.marks@oracle.com> Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com> Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com> Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com> Co-authored-by: Valerie Peng <valerie.peng@oracle.com> Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com> Co-authored-by: Weijun Wang <weijun.wang@oracle.com> Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com> Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com> Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com> Co-authored-by: Felix Yang <felix.yang@oracle.com> Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com> Co-authored-by: Frank Yuan <frank.yuan@oracle.com> Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com> Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com> Co-authored-by: Tiantian Du <tiantian.du@oracle.com> Co-authored-by: Sha Jiang <sha.jiang@oracle.com> Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
2007-12-01 00:00:00 +00:00
// System.err.println("hook " + trname + ": " + className +
// (redef? " REDEF" : " LOAD"));
if ((redef? onRedef : onLoad) && className != null && className.equals(cname)) {
int fixedIndex = redef ? redefIndex : loadIndex;
2007-12-01 00:00:00 +00:00
try {
byte[] newcf = Instrumentor.instrFor(classfileBuffer)
.addMethodEntryInjection(
nname,
cb -> {
cb.loadConstant(fixedIndex);
cb.invokestatic(
CD_RetransformAgent,
"callTracker", MTD_void_int);
})
.apply();
2007-12-01 00:00:00 +00:00
/*** debugging ...
if (newcf != null) {
String fname = trname + (redef?"_redef" : "") + "/" + className;
System.err.println("dumping to: " + fname);
write_buffer(fname + "_before.class", classfileBuffer);
write_buffer(fname + "_instr.class", newcf);
}
2007-12-01 00:00:00 +00:00
***/
System.err.println(trname + ": " + className + " index: " + fixedIndex +
2007-12-01 00:00:00 +00:00
(redef? " REDEF" : " LOAD") +
" len before: " + classfileBuffer.length +
" after: " + newcf.length);
return newcf;
} catch (Throwable ex) {
System.err.println("Injection failure: " + ex);
ex.printStackTrace();
}
}
return null;
}
}
static void write_buffer(String fname, byte[]buffer) {
try {
File f = new File(fname);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
try (FileOutputStream outStream = new FileOutputStream(f)) {
outStream.write(buffer, 0, buffer.length);
}
} catch (IOException ex) {
2007-12-01 00:00:00 +00:00
System.err.println("EXCEPTION in write_buffer: " + ex);
}
}
public static void premain (String agentArgs, Instrumentation instArg) {
inst = instArg;
System.err.println("Premain");
t1 = new Tr("TR1", false, 10, true, 11, "RetransformApp", "foo");
inst.addTransformer(t1, true);
t2 = new Tr("TN2", true, 20, true, 21, "RetransformApp", "foo");
inst.addTransformer(t2, false);
t3 = new Tr("TR3", true, 30, true, 31, "RetransformApp", "foo");
inst.addTransformer(t3, true);
t4 = new Tr("TN4", true, 40, true, 41, "RetransformApp", "foo");
inst.addTransformer(t4, false);
}
public static void undo() {
inst.removeTransformer(t3);
try {
System.err.println("RETRANSFORM");
inst.retransformClasses(new RetransformApp().getClass());
} catch (Exception ex) {
System.err.println("EXCEPTION on undo: " + ex);
}
}
public static boolean succeeded() {
return succeeded && markCount == markGolden.length;
}
public static void callTracker(int mark) {
System.err.println("got mark " + mark);
if (markCount >= markGolden.length || mark != markGolden[markCount++]) {
succeeded = false;
}
}
}