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