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  * @enablePreview
 27  * @run testng TestSynchronizedOp
 28  */
 29 
 30 import org.testng.Assert;
 31 import org.testng.annotations.Test;
 32 
 33 import java.lang.classfile.ClassFile;
 34 import java.lang.classfile.CodeModel;
 35 import java.lang.classfile.MethodModel;
 36 import java.lang.classfile.Opcode;
 37 import java.lang.classfile.instruction.MonitorInstruction;
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodHandles;
 40 import java.lang.reflect.Method;
 41 import java.lang.reflect.code.OpTransformer;
 42 import java.lang.reflect.code.bytecode.BytecodeGenerator;
 43 import java.lang.reflect.code.op.CoreOp;
 44 import java.lang.runtime.CodeReflection;
 45 import java.util.Map;
 46 import java.util.Optional;
 47 import java.util.stream.Collectors;
 48 import java.util.stream.Stream;
 49 
 50 public class TestSynchronizedOp {
 51 
 52     @CodeReflection
 53     static int f(Object o, int i, int[] a) {
 54         synchronized (o) {
 55             if (i < 0) {
 56                 throw new RuntimeException();
 57             }
 58             a[0] = ++i;
 59         }
 60         return i;
 61     }
 62 
 63     @Test
 64     public void testInstructions() {
 65         CoreOp.FuncOp f = getFuncOp("f");
 66         f = f.transform(OpTransformer.LOWERING_TRANSFORMER);
 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         Assert.assertEquals(monitorCount, Map.of(
 78                 Opcode.MONITORENTER, 1L,
 79                 Opcode.MONITOREXIT, 2L));
 80     }
 81 
 82     @Test
 83     public void testExecution() throws Throwable {
 84         CoreOp.FuncOp f = getFuncOp("f");
 85         f = f.transform(OpTransformer.LOWERING_TRANSFORMER);
 86 
 87         MethodHandle mf = BytecodeGenerator.generate(MethodHandles.lookup(), f);
 88 
 89         Object monitor = new Object();
 90         int[] a = new int[1];
 91         Assert.assertEquals((int) mf.invoke(monitor, 0, a), 1);
 92         a[0] = 0;
 93         Assert.assertThrows(RuntimeException.class, () -> {
 94             int i = (int) mf.invoke(monitor, -1, a);
 95         });
 96         Assert.assertEquals(a[0], 0);
 97     }
 98 
 99     static CoreOp.FuncOp getFuncOp(String name) {
100         Optional<Method> om = Stream.of(TestSynchronizedOp.class.getDeclaredMethods())
101                 .filter(m -> m.getName().equals(name))
102                 .findFirst();
103 
104         Method m = om.get();
105         return m.getCodeModel().get();
106     }
107 }