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