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 /*
 25  * @test
 26  * @modules jdk.incubator.code
 27  * @enablePreview
 28  * @run junit TestSynchronizedOp
 29  */
 30 
 31 import jdk.incubator.code.Reflect;
 32 import jdk.incubator.code.Op;
 33 import jdk.incubator.code.bytecode.BytecodeGenerator;
 34 import jdk.incubator.code.dialect.core.CoreOp;
 35 import org.junit.jupiter.api.Assertions;
 36 import org.junit.jupiter.api.Test;
 37 
 38 import java.lang.classfile.ClassFile;
 39 import java.lang.classfile.CodeModel;
 40 import java.lang.classfile.MethodModel;
 41 import java.lang.classfile.Opcode;
 42 import java.lang.classfile.instruction.MonitorInstruction;
 43 import java.lang.invoke.MethodHandle;
 44 import java.lang.invoke.MethodHandles;
 45 import java.lang.reflect.Method;
 46 import java.util.Map;
 47 import java.util.Optional;
 48 import java.util.stream.Collectors;
 49 import java.util.stream.Stream;
 50 
 51 public class TestSynchronizedOp {
 52 
 53     @Reflect
 54     static int f(Object o, int i, int[] a) {
 55         synchronized (o) {
 56             if (i < 0) {
 57                 throw new RuntimeException();
 58             }
 59             a[0] = ++i;
 60         }
 61         return i;
 62     }
 63 
 64     @Test
 65     public void testInstructions() {
 66         CoreOp.FuncOp f = getFuncOp("f");
 67 
 68         byte[] classdata = BytecodeGenerator.generateClassData(MethodHandles.lookup(), f);
 69         CodeModel cmf = ClassFile.of().parse(classdata).methods().stream()
 70                 .filter(mm -> mm.methodName().equalsString("f"))
 71                 .findFirst().flatMap(MethodModel::code).orElseThrow();
 72         Map<Opcode, Long> monitorCount = cmf.elementStream().<Opcode>mapMulti((i, c) -> {
 73             if (i instanceof MonitorInstruction mi) {
 74                 c.accept(mi.opcode());
 75             }
 76         }).collect(Collectors.groupingBy(oc -> oc, Collectors.counting()));
 77         Assertions.assertEquals(Map.of(
 78                 Opcode.MONITORENTER, 1L,
 79                 Opcode.MONITOREXIT, 2L), monitorCount);
 80     }
 81 
 82     @Test
 83     public void testExecution() throws Throwable {
 84         CoreOp.FuncOp f = getFuncOp("f");
 85 
 86         MethodHandle mf = BytecodeGenerator.generate(MethodHandles.lookup(), f);
 87 
 88         Object monitor = new Object();
 89         int[] a = new int[1];
 90         Assertions.assertEquals(1, (int) mf.invoke(monitor, 0, a));
 91         a[0] = 0;
 92         Assertions.assertThrows(RuntimeException.class, () -> {
 93             int i = (int) mf.invoke(monitor, -1, a);
 94         });
 95         Assertions.assertEquals(0, a[0]);
 96     }
 97 
 98     static CoreOp.FuncOp getFuncOp(String name) {
 99         Optional<Method> om = Stream.of(TestSynchronizedOp.class.getDeclaredMethods())
100                 .filter(m -> m.getName().equals(name))
101                 .findFirst();
102 
103         Method m = om.get();
104         return Op.ofMethod(m).get();
105     }
106 }