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 testng TestUninitializedVariable
 28  */
 29 
 30 import jdk.incubator.code.Op;
 31 import org.testng.Assert;
 32 import org.testng.annotations.DataProvider;
 33 import org.testng.annotations.Test;
 34 
 35 import java.lang.invoke.MethodHandles;
 36 import java.lang.reflect.Method;
 37 import jdk.incubator.code.OpTransformer;
 38 import jdk.incubator.code.analysis.SSA;
 39 import jdk.incubator.code.interpreter.Interpreter;
 40 import jdk.incubator.code.op.CoreOp;
 41 import jdk.incubator.code.CodeReflection;
 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     @DataProvider
 67     Object[][] methods() {
 68         return new Object[][] {
 69                 { "simple" },
 70                 { "controlFlow" }
 71         };
 72     }
 73 
 74     @Test(dataProvider = "methods")
 75     public void testInterpret(String method) {
 76         CoreOp.FuncOp f = removeFirstStore(getFuncOp(method).transform(OpTransformer.LOWERING_TRANSFORMER));
 77         f.writeTo(System.out);
 78 
 79         Assert.assertThrows(Interpreter.InterpreterException.class, () -> Interpreter.invoke(MethodHandles.lookup(), f, 1));
 80     }
 81 
 82     @Test(dataProvider = "methods")
 83     public void testSSA(String method) {
 84         CoreOp.FuncOp f = removeFirstStore(getFuncOp(method).transform(OpTransformer.LOWERING_TRANSFORMER));
 85         f.writeTo(System.out);
 86 
 87         Assert.assertThrows(IllegalStateException.class, () -> SSA.transform(f));
 88     }
 89 
 90     static CoreOp.FuncOp removeFirstStore(CoreOp.FuncOp f) {
 91         AtomicBoolean b = new AtomicBoolean();
 92         return f.transform((block, op) -> {
 93             if (op instanceof CoreOp.VarAccessOp.VarStoreOp vop && !b.getAndSet(true)) {
 94                 // Drop first encountered var store
 95             } else {
 96                 block.op(op);
 97             }
 98             return block;
 99         });
100     }
101 
102     static CoreOp.FuncOp getFuncOp(String name) {
103         Optional<Method> om = Stream.of(TestUninitializedVariable.class.getDeclaredMethods())
104                 .filter(m -> m.getName().equals(name))
105                 .findFirst();
106 
107         Method m = om.get();
108         return Op.ofMethod(m).get();
109     }
110 }