|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 8240908 |
| 27 | + * |
| 28 | + * @library /test/lib |
| 29 | + * @run compile -g -parameters RetransformWithMethodParametersTest.java |
| 30 | + * @run shell MakeJAR.sh retransformAgent |
| 31 | + * |
| 32 | + * @run main/othervm -javaagent:retransformAgent.jar RetransformWithMethodParametersTest |
| 33 | + */ |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | +import java.io.FileOutputStream; |
| 37 | +import java.lang.instrument.ClassFileTransformer; |
| 38 | +import java.lang.reflect.Method; |
| 39 | +import java.lang.reflect.Parameter; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.security.ProtectionDomain; |
| 43 | +import java.util.Arrays; |
| 44 | + |
| 45 | +import jdk.test.lib.JDKToolLauncher; |
| 46 | +import jdk.test.lib.process.ProcessTools; |
| 47 | +import jdk.test.lib.process.OutputAnalyzer; |
| 48 | +import jdk.test.lib.util.ClassTransformer; |
| 49 | + |
| 50 | +/* |
| 51 | + * The test verifies Instrumentation.retransformClasses() (and JVMTI function RetransformClasses) |
| 52 | + * correctly handles MethodParameter attribute: |
| 53 | + * 1) classfile bytes passed to transformers (and JVMTI ClassFileLoadHook event callback) contain the attribute; |
| 54 | + * 2) the attribute is updated if new version has the attribute with different values; |
| 55 | + * 3) the attribute is removed if new version doesn't contain the attribute. |
| 56 | + */ |
| 57 | + |
| 58 | +// See ClassTransformer.transform(int) comment for @1 tag explanations. |
| 59 | +class MethodParametersTarget { |
| 60 | + // The class contains the only method, so we don't have issue with method sorting |
| 61 | + // and ClassFileReconstituter should restore the same bytes as original classbytes. |
| 62 | + public void method1( |
| 63 | + int intParam1, String stringParam1 // @1 commentout |
| 64 | + // @1 uncomment int intParam2, String stringParam2 |
| 65 | + ) |
| 66 | + { |
| 67 | + // @1 uncomment System.out.println(stringParam2); // change CP |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public class RetransformWithMethodParametersTest extends ATransformerManagementTestCase { |
| 72 | + |
| 73 | + public static void main (String[] args) throws Throwable { |
| 74 | + ATestCaseScaffold test = new RetransformWithMethodParametersTest(); |
| 75 | + test.runTest(); |
| 76 | + } |
| 77 | + |
| 78 | + private String targetClassName = "MethodParametersTarget"; |
| 79 | + private String classFileName = targetClassName + ".class"; |
| 80 | + private String sourceFileName = "RetransformWithMethodParametersTest.java"; |
| 81 | + private Class targetClass; |
| 82 | + private byte[] originalClassBytes; |
| 83 | + |
| 84 | + private byte[] seenClassBytes; |
| 85 | + private byte[] newClassBytes; |
| 86 | + |
| 87 | + public RetransformWithMethodParametersTest() throws Throwable { |
| 88 | + super("RetransformWithMethodParametersTest"); |
| 89 | + |
| 90 | + File origClassFile = new File(System.getProperty("test.classes", "."), classFileName); |
| 91 | + log("Reading test class from " + origClassFile); |
| 92 | + originalClassBytes = Files.readAllBytes(origClassFile.toPath()); |
| 93 | + log("Read " + originalClassBytes.length + " bytes."); |
| 94 | + } |
| 95 | + |
| 96 | + private void log(Object o) { |
| 97 | + System.out.println(String.valueOf(o)); |
| 98 | + } |
| 99 | + |
| 100 | + private Parameter[] getTargetMethodParameters() throws ClassNotFoundException { |
| 101 | + Class cls = Class.forName(targetClassName); |
| 102 | + // the class contains 1 method (method1) |
| 103 | + Method method = cls.getDeclaredMethods()[0]; |
| 104 | + Parameter[] params = method.getParameters(); |
| 105 | + log("Params of " + method.getName() + " method (" + params.length + "):"); |
| 106 | + for (int i = 0; i < params.length; i++) { |
| 107 | + log(" " + i + ": " + params[i].getName() |
| 108 | + + " (" + (params[i].isNamePresent() ? "present" : "absent") + ")"); |
| 109 | + } |
| 110 | + return params; |
| 111 | + } |
| 112 | + |
| 113 | + // Verifies MethodParameters attribute is present and contains the expected values. |
| 114 | + private void verifyPresentMethodParams(String... expectedNames) throws Throwable { |
| 115 | + Parameter[] params = getTargetMethodParameters(); |
| 116 | + assertEquals(expectedNames.length, params.length); |
| 117 | + for (int i = 0; i < params.length; i++) { |
| 118 | + assertTrue(params[i].isNamePresent()); |
| 119 | + assertEquals(expectedNames[i], params[i].getName()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Verifies MethodParameters attribute is absent. |
| 124 | + private void verifyAbsentMethodParams() throws Throwable { |
| 125 | + Parameter[] params = getTargetMethodParameters(); |
| 126 | + for (int i = 0; i < params.length; i++) { |
| 127 | + assertTrue(!params[i].isNamePresent()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Retransforms target class using provided class bytes; |
| 132 | + // Returns class bytes passed to the transformer. |
| 133 | + private byte[] retransform(byte[] classBytes) throws Throwable { |
| 134 | + seenClassBytes = null; |
| 135 | + newClassBytes = classBytes; |
| 136 | + fInst.retransformClasses(targetClass); |
| 137 | + assertTrue(targetClassName + " was not seen by transform()", seenClassBytes != null); |
| 138 | + return seenClassBytes; |
| 139 | + } |
| 140 | + |
| 141 | + // Prints dissassembled class bytes. |
| 142 | + private void printDisassembled(String description, byte[] bytes) throws Exception { |
| 143 | + log(description + " -------------------"); |
| 144 | + |
| 145 | + File f = new File(classFileName); |
| 146 | + try (FileOutputStream fos = new FileOutputStream(f)) { |
| 147 | + fos.write(bytes); |
| 148 | + } |
| 149 | + JDKToolLauncher javap = JDKToolLauncher.create("javap") |
| 150 | + .addToolArg("-verbose") |
| 151 | + .addToolArg("-p") // Shows all classes and members. |
| 152 | + //.addToolArg("-c") // Prints out disassembled code |
| 153 | + //.addToolArg("-s") // Prints internal type signatures. |
| 154 | + .addToolArg(f.toString()); |
| 155 | + ProcessBuilder pb = new ProcessBuilder(javap.getCommand()); |
| 156 | + OutputAnalyzer out = ProcessTools.executeProcess(pb); |
| 157 | + out.shouldHaveExitValue(0); |
| 158 | + try { |
| 159 | + Files.delete(f.toPath()); |
| 160 | + } catch (Exception ex) { |
| 161 | + // ignore |
| 162 | + } |
| 163 | + out.asLines().forEach(s -> log(s)); |
| 164 | + log("=========================================="); |
| 165 | + } |
| 166 | + |
| 167 | + // Verifies class bytes are equal. |
| 168 | + private void compareClassBytes(byte[] expected, byte[] actual) throws Exception { |
| 169 | + |
| 170 | + int pos = Arrays.mismatch(expected, actual); |
| 171 | + if (pos < 0) { |
| 172 | + log("Class bytes are identical."); |
| 173 | + return; |
| 174 | + } |
| 175 | + log("Class bytes are different."); |
| 176 | + printDisassembled("expected", expected); |
| 177 | + printDisassembled("expected", actual); |
| 178 | + fail(targetClassName + " did not match .class file"); |
| 179 | + } |
| 180 | + |
| 181 | + protected final void doRunTest() throws Throwable { |
| 182 | + beVerbose(); |
| 183 | + |
| 184 | + ClassLoader loader = getClass().getClassLoader(); |
| 185 | + targetClass = loader.loadClass(targetClassName); |
| 186 | + // sanity check |
| 187 | + assertEquals(targetClassName, targetClass.getName()); |
| 188 | + // sanity check |
| 189 | + verifyPresentMethodParams("intParam1", "stringParam1"); |
| 190 | + |
| 191 | + addTransformerToManager(fInst, new Transformer(), true); |
| 192 | + |
| 193 | + { |
| 194 | + log("Testcase 1: ensure ClassFileReconstituter restores MethodParameters attribute"); |
| 195 | + |
| 196 | + byte[] classBytes = retransform(null); |
| 197 | + compareClassBytes(originalClassBytes, classBytes); |
| 198 | + |
| 199 | + log(""); |
| 200 | + } |
| 201 | + |
| 202 | + { |
| 203 | + log("Testcase 2: redefine class with changed parameter names"); |
| 204 | + |
| 205 | + byte[] classBytes = Files.readAllBytes(Paths.get( |
| 206 | + ClassTransformer.fromTestSource(sourceFileName) |
| 207 | + .transform(1, targetClassName, "-g", "-parameters"))); |
| 208 | + retransform(classBytes); |
| 209 | + // MethodParameters attribute should be updated. |
| 210 | + verifyPresentMethodParams("intParam2", "stringParam2"); |
| 211 | + |
| 212 | + log(""); |
| 213 | + } |
| 214 | + |
| 215 | + { |
| 216 | + log("Testcase 3: redefine class with no parameter names"); |
| 217 | + // compile without "-parameters" |
| 218 | + byte[] classBytes = Files.readAllBytes(Paths.get( |
| 219 | + ClassTransformer.fromTestSource(sourceFileName) |
| 220 | + .transform(1, targetClassName, "-g"))); |
| 221 | + retransform(classBytes); |
| 222 | + // MethodParameters attribute should be dropped. |
| 223 | + verifyAbsentMethodParams(); |
| 224 | + |
| 225 | + log(""); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | + public class Transformer implements ClassFileTransformer { |
| 231 | + public Transformer() { |
| 232 | + } |
| 233 | + |
| 234 | + public String toString() { |
| 235 | + return Transformer.this.getClass().getName(); |
| 236 | + } |
| 237 | + |
| 238 | + public byte[] transform(ClassLoader loader, String className, |
| 239 | + Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { |
| 240 | + |
| 241 | + if (className.equals(targetClassName)) { |
| 242 | + log(this + ".transform() sees '" + className |
| 243 | + + "' of " + classfileBuffer.length + " bytes."); |
| 244 | + seenClassBytes = classfileBuffer; |
| 245 | + if (newClassBytes != null) { |
| 246 | + log(this + ".transform() sets new classbytes for '" + className |
| 247 | + + "' of " + newClassBytes.length + " bytes."); |
| 248 | + } |
| 249 | + return newClassBytes; |
| 250 | + } |
| 251 | + |
| 252 | + return null; |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments