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 * @run junit TestUninitializedVariable
28 */
29
30 import jdk.incubator.code.CodeReflection;
31 import jdk.incubator.code.Op;
32 import jdk.incubator.code.OpTransformer;
33 import jdk.incubator.code.analysis.SSA;
34 import jdk.incubator.code.dialect.core.CoreOp;
35 import jdk.incubator.code.interpreter.Interpreter;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.MethodSource;
39
40 import java.lang.invoke.MethodHandles;
41 import java.lang.reflect.Method;
42 import java.util.Optional;
43 import java.util.concurrent.atomic.AtomicBoolean;
44 import java.util.stream.Stream;
45
46 public class TestUninitializedVariable {
47
48 @CodeReflection
49 static int simple(int i) {
50 int x;
51 x = i; // drop store
52 return x;
53 }
54
55 @CodeReflection
56 static int controlFlow(int i) {
57 int x;
58 if (i > 0) {
59 x = i; // drop store
60 } else {
61 x = -i;
62 }
63 return x;
64 }
65
66 static Stream<String> methods() {
67 return Stream.of("simple", "controlFlow");
68 }
69
70 @ParameterizedTest
71 @MethodSource("methods")
72 public void testInterpret(String method) {
73 CoreOp.FuncOp f = removeFirstStore(getFuncOp(method).transform(OpTransformer.LOWERING_TRANSFORMER));
74 System.out.println(f.toText());
75
76 Assertions.assertThrows(Interpreter.InterpreterException.class, () -> Interpreter.invoke(MethodHandles.lookup(), f, 1));
77 }
78
79 @ParameterizedTest
80 @MethodSource("methods")
81 public void testSSA(String method) {
82 CoreOp.FuncOp f = removeFirstStore(getFuncOp(method).transform(OpTransformer.LOWERING_TRANSFORMER));
83 System.out.println(f.toText());
84
85 Assertions.assertThrows(IllegalStateException.class, () -> SSA.transform(f));
86 }
87
88 static CoreOp.FuncOp removeFirstStore(CoreOp.FuncOp f) {
89 AtomicBoolean b = new AtomicBoolean();
90 return f.transform((block, op) -> {
91 if (op instanceof CoreOp.VarAccessOp.VarStoreOp vop && !b.getAndSet(true)) {
92 // Drop first encountered var store
93 } else {
94 block.op(op);
95 }
96 return block;
97 });
98 }
99
100 static CoreOp.FuncOp getFuncOp(String name) {
101 Optional<Method> om = Stream.of(TestUninitializedVariable.class.getDeclaredMethods())
102 .filter(m -> m.getName().equals(name))
103 .findFirst();
104
105 Method m = om.get();
106 return Op.ofMethod(m).get();
107 }
108 }