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