1 /* 2 * Copyright (c) 2024, 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 import java.lang.classfile.ClassFile; 25 import java.lang.classfile.Instruction; 26 import java.lang.classfile.Label; 27 import java.lang.classfile.MethodModel; 28 import java.lang.classfile.Opcode; 29 import java.lang.classfile.attribute.CodeAttribute; 30 import jdk.internal.classfile.components.ClassPrinter; 31 import java.lang.classfile.instruction.*; 32 import java.lang.invoke.MethodHandles; 33 import jdk.incubator.code.bytecode.BytecodeGenerator; 34 import jdk.incubator.code.bytecode.BytecodeLift; 35 import jdk.incubator.code.dialect.core.CoreOp; 36 import java.net.URI; 37 import java.nio.file.FileSystem; 38 import java.nio.file.FileSystems; 39 import java.nio.file.Files; 40 import java.nio.file.Path; 41 import java.util.*; 42 import java.util.stream.Collectors; 43 import java.util.stream.Stream; 44 import org.testng.Assert; 45 import org.testng.annotations.Ignore; 46 import org.testng.annotations.Test; 47 48 /* 49 * @test 50 * @modules jdk.incubator.code 51 * @modules java.base/java.lang.invoke:open 52 * @modules java.base/jdk.internal.classfile.components 53 * @enablePreview 54 * @run testng TestSmallCorpus 55 */ 56 public class TestSmallCorpus { 57 58 private static final String ROOT_PATH = "modules/java.base/"; 59 private static final String CLASS_NAME_SUFFIX = ".class"; 60 private static final String METHOD_NAME = null; 61 private static final int ROUNDS = 3; 62 63 private static final FileSystem JRT = FileSystems.getFileSystem(URI.create("jrt:/")); 64 private static final ClassFile CF = ClassFile.of(); 65 private static final int COLUMN_WIDTH = 150; 66 private static final MethodHandles.Lookup TRUSTED_LOOKUP; 67 static { 68 try { 69 var lf = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); 70 lf.setAccessible(true); 71 TRUSTED_LOOKUP = (MethodHandles.Lookup)lf.get(null); 72 } catch (ReflectiveOperationException e) { 73 throw new RuntimeException(e); 74 } 75 } 76 77 private MethodModel bytecode; 78 CoreOp.FuncOp reflection; 79 private int stable, unstable; 80 private Long[] stats = new Long[6]; 81 82 @Ignore 83 @Test 84 public void testRoundTripStability() throws Exception { 85 stable = 0; 86 unstable = 0; 87 Arrays.fill(stats, 0l); 88 for (Path p : Files.walk(JRT.getPath(ROOT_PATH)) 89 .filter(p -> Files.isRegularFile(p) && p.toString().endsWith(CLASS_NAME_SUFFIX)) 90 .toList()) { 91 testRoundTripStability(p); 92 } 93 94 System.out.println(""" 95 statistics original generated 96 code length: %1$,10d %4$,10d 97 max locals: %2$,10d %5$,10d 98 max stack: %3$,10d %6$,10d 99 """.formatted((Object[])stats)); 100 101 // Roundtrip is 100% stable after 3 rounds, no exceptions, no verification errors 102 Assert.assertTrue(stable > 54500 && unstable == 0, String.format("stable: %d unstable: %d", stable, unstable)); 103 } 104 105 private void testRoundTripStability(Path path) throws Exception { 106 var clm = CF.parse(path); 107 for (var originalModel : clm.methods()) { 108 if (originalModel.code().isPresent() && (METHOD_NAME == null || originalModel.methodName().equalsString(METHOD_NAME))) try { 109 bytecode = originalModel; 110 reflection = null; 111 MethodModel prevBytecode = null; 112 CoreOp.FuncOp prevReflection = null; 113 for (int round = 1; round <= ROUNDS; round++) try { 114 prevBytecode = bytecode; 115 prevReflection = reflection; 116 lift(); 117 verifyReflection(); 118 generate(); 119 verifyBytecode(); 120 } catch (UnsupportedOperationException uoe) { 121 throw uoe; 122 } catch (Throwable t) { 123 System.out.println(" at " + path + " " + originalModel.methodName() + originalModel.methodType() + " round " + round); 124 throw t; 125 } 126 if (ROUNDS > 0) { 127 var normPrevBytecode = normalize(prevBytecode); 128 var normBytecode = normalize(bytecode); 129 if (normPrevBytecode.equals(normBytecode)) { 130 stable++; 131 } else { 132 unstable++; 133 System.out.println("Unstable code " + path + " " + originalModel.methodName() + originalModel.methodType() + " after " + ROUNDS +" round(s)"); 134 if (prevReflection != null) printInColumns(prevReflection, reflection); 135 printInColumns(normPrevBytecode, normBytecode); 136 System.out.println(); 137 } 138 var ca = (CodeAttribute)originalModel.code().get(); 139 stats[0] += ca.codeLength(); 140 stats[1] += ca.maxLocals(); 141 stats[2] += ca.maxStack(); 142 ca = (CodeAttribute)bytecode.code().get(); 143 stats[3] += ca.codeLength(); 144 stats[4] += ca.maxLocals(); 145 stats[5] += ca.maxStack(); 146 } 147 } catch (UnsupportedOperationException uoe) { 148 // InvokeOp when InvokeKind == SUPER 149 } 150 } 151 } 152 153 private void verifyReflection() { 154 var errors = Verifier.verify(TRUSTED_LOOKUP, reflection); 155 if (!errors.isEmpty()) { 156 printBytecode(); 157 System.out.println("Code reflection model verification failed:"); 158 errors.forEach(e -> System.out.println(e.getMessage())); 159 System.out.println(errors.getFirst().getPrintedContext()); 160 throw new AssertionError("Code reflection model verification failed"); 161 } 162 } 163 164 private void verifyBytecode() { 165 var errors = ClassFile.of().verify(bytecode.parent().get()).stream() 166 .filter(e -> !e.getMessage().contains("Illegal call to internal method")).toList(); 167 if (!errors.isEmpty()) { 168 printReflection(); 169 System.out.println("Bytecode verification failed:"); 170 errors.forEach(e -> System.out.println(e.getMessage())); 171 printBytecode(); 172 throw new AssertionError("Bytecode verification failed"); 173 } 174 } 175 176 private static void printInColumns(CoreOp.FuncOp first, CoreOp.FuncOp second) { 177 printInColumns(first.toText().lines().toList(), second.toText().lines().toList()); 178 } 179 180 private static void printInColumns(List<String> first, List<String> second) { 181 System.out.println("-".repeat(COLUMN_WIDTH ) + "--+-" + "-".repeat(COLUMN_WIDTH )); 182 for (int i = 0; i < first.size() || i < second.size(); i++) { 183 String f = i < first.size() ? first.get(i) : ""; 184 String s = i < second.size() ? second.get(i) : ""; 185 System.out.println(" " + f + (f.length() < COLUMN_WIDTH ? " ".repeat(COLUMN_WIDTH - f.length()) : "") + (f.equals(s) ? " | " : " x ") + s); 186 } 187 } 188 189 private void lift() { 190 try { 191 reflection = BytecodeLift.lift(bytecode); 192 } catch (Throwable t) { 193 printReflection(); 194 printBytecode(); 195 System.out.println("Lift failed"); 196 throw t; 197 } 198 } 199 200 private void generate() { 201 try { 202 bytecode = CF.parse(BytecodeGenerator.generateClassData( 203 TRUSTED_LOOKUP, 204 reflection)).methods().getFirst(); 205 } catch (UnsupportedOperationException uoe) { 206 throw uoe; 207 } catch (Throwable t) { 208 printBytecode(); 209 printReflection(); 210 System.out.println("Generation failed"); 211 throw t; 212 } 213 } 214 215 private void printBytecode() { 216 ClassPrinter.toYaml(bytecode, ClassPrinter.Verbosity.CRITICAL_ATTRIBUTES, System.out::print); 217 } 218 219 private void printReflection() { 220 if (reflection != null) System.out.println(reflection.toText()); 221 } 222 223 public static List<String> normalize(MethodModel mm) { 224 record El(int index, String format, Label... targets) { 225 public El(int index, Instruction i, Object format, Label... targets) { 226 this(index, trim(i.opcode()) + " " + format, targets); 227 } 228 public String toString(Map<Label, Integer> targetsMap) { 229 return "%3d: ".formatted(index) + (targets.length == 0 ? format : format.formatted(Stream.of(targets).map(l -> targetsMap.get(l)).toArray())); 230 } 231 } 232 233 Map<Label, Integer> targetsMap = new HashMap<>(); 234 List<El> elements = new ArrayList<>(); 235 Label lastLabel = null; 236 int i = 0; 237 for (var e : mm.code().orElseThrow()) { 238 var er = switch (e) { 239 case LabelTarget lt -> { 240 lastLabel = lt.label(); 241 yield null; 242 } 243 case ExceptionCatch ec -> 244 new El(i++, "ExceptionCatch start: @%d end: @%d handler: @%d" + ec.catchType().map(ct -> " catch type: " + ct.asInternalName()).orElse(""), ec.tryStart(), ec.tryEnd(), ec.handler()); 245 case BranchInstruction ins -> 246 new El(i++, ins, "@%d", ins.target()); 247 case ConstantInstruction ins -> 248 new El(i++, "LDC " + ins.constantValue()); 249 case FieldInstruction ins -> 250 new El(i++, ins, ins.owner().asInternalName() + "." + ins.name().stringValue()); 251 case InvokeDynamicInstruction ins -> 252 new El(i++, ins, ins.name().stringValue() + ins.typeSymbol() + " " + ins.bootstrapMethod() + "(" + ins.bootstrapArgs() + ")"); 253 case InvokeInstruction ins -> 254 new El(i++, ins, ins.owner().asInternalName() + "::" + ins.name().stringValue() + ins.typeSymbol().displayDescriptor()); 255 case LoadInstruction ins -> 256 new El(i++, ins, "#" + ins.slot()); 257 case StoreInstruction ins -> 258 new El(i++, ins, "#" + ins.slot()); 259 case IncrementInstruction ins -> 260 new El(i++, ins, "#" + ins.slot() + " " + ins.constant()); 261 case LookupSwitchInstruction ins -> 262 new El(i++, ins, "default: @%d" + ins.cases().stream().map(c -> ", " + c.caseValue() + ": @%d").collect(Collectors.joining()), 263 Stream.concat(Stream.of(ins.defaultTarget()), ins.cases().stream().map(SwitchCase::target)).toArray(Label[]::new)); 264 case NewMultiArrayInstruction ins -> 265 new El(i++, ins, ins.arrayType().asInternalName() + "(" + ins.dimensions() + ")"); 266 case NewObjectInstruction ins -> 267 new El(i++, ins, ins.className().asInternalName()); 268 case NewPrimitiveArrayInstruction ins -> 269 new El(i++, ins, ins.typeKind()); 270 case NewReferenceArrayInstruction ins -> 271 new El(i++, ins, ins.componentType().asInternalName()); 272 case TableSwitchInstruction ins -> 273 new El(i++, ins, "default: @%d" + ins.cases().stream().map(c -> ", " + c.caseValue() + ": @%d").collect(Collectors.joining()), 274 Stream.concat(Stream.of(ins.defaultTarget()), ins.cases().stream().map(SwitchCase::target)).toArray(Label[]::new)); 275 case TypeCheckInstruction ins -> 276 new El(i++, ins, ins.type().asInternalName()); 277 case Instruction ins -> 278 new El(i++, ins, ""); 279 default -> null; 280 }; 281 if (er != null) { 282 if (lastLabel != null) { 283 targetsMap.put(lastLabel, elements.size()); 284 lastLabel = null; 285 } 286 elements.add(er); 287 } 288 } 289 return elements.stream().map(el -> el.toString(targetsMap)).toList(); 290 } 291 292 private static String trim(Opcode opcode) { 293 var name = opcode.toString(); 294 int i = name.indexOf('_'); 295 return i > 2 ? name.substring(0, i) : name; 296 } 297 }