1 /*
   2  * Copyright (c) 2015, 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  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  27  * @compile CodeInstallerTest.java
  28  * @run junit/othervm -da:jdk.vm.ci... -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI compiler.jvmci.errors.TestInvalidDebugInfo
  29  */
  30 
  31 package compiler.jvmci.errors;
  32 
  33 import static jdk.vm.ci.code.CompilationResult.Infopoint;
  34 
  35 import jdk.vm.ci.code.BytecodeFrame;
  36 import jdk.vm.ci.code.CompilationResult;
  37 import jdk.vm.ci.code.DebugInfo;
  38 import jdk.vm.ci.code.InfopointReason;
  39 import jdk.vm.ci.code.Location;
  40 import jdk.vm.ci.code.Register;
  41 import jdk.vm.ci.code.StackSlot;
  42 import jdk.vm.ci.code.VirtualObject;
  43 import jdk.vm.ci.hotspot.HotSpotReferenceMap;
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.JavaValue;
  47 import jdk.vm.ci.meta.LIRKind;
  48 import jdk.vm.ci.meta.ResolvedJavaType;
  49 import jdk.vm.ci.meta.Value;
  50 import jdk.vm.ci.common.JVMCIError;
  51 
  52 import org.junit.Test;
  53 
  54 /**
  55  * Tests for errors in debug info.
  56  */
  57 public class TestInvalidDebugInfo extends CodeInstallerTest {
  58 
  59     private static class UnknownJavaValue implements JavaValue {
  60     }
  61 
  62     private void test(JavaValue[] values, JavaKind[] slotKinds, int locals, int stack, int locks) {
  63         test(null, values, slotKinds, locals, stack, locks);
  64     }
  65 
  66     private void test(VirtualObject[] vobj, JavaValue[] values, JavaKind[] slotKinds, int locals, int stack, int locks) {
  67         BytecodeFrame frame = new BytecodeFrame(null, dummyMethod, 0, false, false, values, slotKinds, locals, stack, locks);
  68         DebugInfo info = new DebugInfo(frame, vobj);
  69         info.setReferenceMap(new HotSpotReferenceMap(new Location[0], new Location[0], new int[0], 8));
  70 
  71         CompilationResult result = createEmptyCompilationResult();
  72         result.addInfopoint(new Infopoint(0, info, InfopointReason.SAFEPOINT));
  73         installCode(result);
  74     }
  75 
  76     @Test(expected = NullPointerException.class)
  77     public void testNullValues() {
  78         test(null, new JavaKind[0], 0, 0, 0);
  79     }
  80 
  81     @Test(expected = NullPointerException.class)
  82     public void testNullSlotKinds() {
  83         test(new JavaValue[0], null, 0, 0, 0);
  84     }
  85 
  86     @Test(expected = JVMCIError.class)
  87     public void testUnexpectedScopeValuesLength() {
  88         test(new JavaValue[]{JavaConstant.FALSE}, new JavaKind[0], 0, 0, 0);
  89     }
  90 
  91     @Test(expected = JVMCIError.class)
  92     public void testUnexpectedScopeSlotKindsLength() {
  93         test(new JavaValue[0], new JavaKind[]{JavaKind.Boolean}, 0, 0, 0);
  94     }
  95 
  96     @Test(expected = NullPointerException.class)
  97     public void testNullValue() {
  98         test(new JavaValue[]{null}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
  99     }
 100 
 101     @Test(expected = NullPointerException.class)
 102     public void testNullSlotKind() {
 103         test(new JavaValue[]{JavaConstant.INT_0}, new JavaKind[]{null}, 1, 0, 0);
 104     }
 105 
 106     @Test(expected = NullPointerException.class)
 107     public void testNullMonitor() {
 108         test(new JavaValue[]{null}, new JavaKind[0], 0, 0, 1);
 109     }
 110 
 111     @Test(expected = JVMCIError.class)
 112     public void testWrongMonitorType() {
 113         test(new JavaValue[]{JavaConstant.INT_0}, new JavaKind[0], 0, 0, 1);
 114     }
 115 
 116     @Test(expected = JVMCIError.class)
 117     public void testUnexpectedIllegalValue() {
 118         test(new JavaValue[]{Value.ILLEGAL}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
 119     }
 120 
 121     @Test(expected = JVMCIError.class)
 122     public void testUnexpectedTypeInCPURegister() {
 123         Register reg = getRegister(arch.getPlatformKind(JavaKind.Int), 0);
 124         test(new JavaValue[]{reg.asValue()}, new JavaKind[]{JavaKind.Illegal}, 1, 0, 0);
 125     }
 126 
 127     @Test(expected = JVMCIError.class)
 128     public void testUnexpectedTypeInFloatRegister() {
 129         Register reg = getRegister(arch.getPlatformKind(JavaKind.Float), 0);
 130         test(new JavaValue[]{reg.asValue()}, new JavaKind[]{JavaKind.Illegal}, 1, 0, 0);
 131     }
 132 
 133     @Test(expected = JVMCIError.class)
 134     public void testUnexpectedTypeOnStack() {
 135         LIRKind kind = codeCache.getTarget().getLIRKind(JavaKind.Int);
 136         StackSlot value = StackSlot.get(kind, 8, false);
 137         test(new JavaValue[]{value}, new JavaKind[]{JavaKind.Illegal}, 1, 0, 0);
 138     }
 139 
 140     @Test(expected = JVMCIError.class)
 141     public void testWrongConstantType() {
 142         test(new JavaValue[]{JavaConstant.INT_0}, new JavaKind[]{JavaKind.Object}, 1, 0, 0);
 143     }
 144 
 145     @Test(expected = JVMCIError.class)
 146     public void testUnsupportedConstantType() {
 147         test(new JavaValue[]{JavaConstant.forShort((short) 0)}, new JavaKind[]{JavaKind.Short}, 1, 0, 0);
 148     }
 149 
 150     @Test(expected = JVMCIError.class)
 151     public void testUnexpectedNull() {
 152         test(new JavaValue[]{JavaConstant.NULL_POINTER}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
 153     }
 154 
 155     @Test(expected = JVMCIError.class)
 156     public void testUnexpectedObject() {
 157         JavaValue wrapped = constantReflection.forObject(this);
 158         test(new JavaValue[]{wrapped}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
 159     }
 160 
 161     @Test(expected = JVMCIError.class)
 162     public void testUnknownJavaValue() {
 163         test(new JavaValue[]{new UnknownJavaValue()}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
 164     }
 165 
 166     @Test(expected = JVMCIError.class)
 167     public void testMissingIllegalAfterDouble() {
 168         test(new JavaValue[]{JavaConstant.DOUBLE_0, JavaConstant.INT_0}, new JavaKind[]{JavaKind.Double, JavaKind.Int}, 2, 0, 0);
 169     }
 170 
 171     @Test(expected = JVMCIError.class)
 172     public void testInvalidVirtualObjectId() {
 173         ResolvedJavaType obj = metaAccess.lookupJavaType(Object.class);
 174         VirtualObject o = VirtualObject.get(obj, 5);
 175         o.setValues(new JavaValue[0], new JavaKind[0]);
 176 
 177         test(new VirtualObject[]{o}, new JavaValue[0], new JavaKind[0], 0, 0, 0);
 178     }
 179 
 180     @Test(expected = JVMCIError.class)
 181     public void testDuplicateVirtualObject() {
 182         ResolvedJavaType obj = metaAccess.lookupJavaType(Object.class);
 183         VirtualObject o1 = VirtualObject.get(obj, 0);
 184         o1.setValues(new JavaValue[0], new JavaKind[0]);
 185 
 186         VirtualObject o2 = VirtualObject.get(obj, 0);
 187         o2.setValues(new JavaValue[0], new JavaKind[0]);
 188 
 189         test(new VirtualObject[]{o1, o2}, new JavaValue[0], new JavaKind[0], 0, 0, 0);
 190     }
 191 
 192     @Test(expected = JVMCIError.class)
 193     public void testUnexpectedVirtualObject() {
 194         ResolvedJavaType obj = metaAccess.lookupJavaType(Object.class);
 195         VirtualObject o = VirtualObject.get(obj, 0);
 196         o.setValues(new JavaValue[0], new JavaKind[0]);
 197 
 198         test(new VirtualObject[]{o}, new JavaValue[]{o}, new JavaKind[]{JavaKind.Int}, 1, 0, 0);
 199     }
 200 
 201     @Test(expected = JVMCIError.class)
 202     public void testUndefinedVirtualObject() {
 203         ResolvedJavaType obj = metaAccess.lookupJavaType(Object.class);
 204         VirtualObject o0 = VirtualObject.get(obj, 0);
 205         o0.setValues(new JavaValue[0], new JavaKind[0]);
 206 
 207         VirtualObject o1 = VirtualObject.get(obj, 1);
 208         o1.setValues(new JavaValue[0], new JavaKind[0]);
 209 
 210         test(new VirtualObject[]{o0}, new JavaValue[]{o1}, new JavaKind[]{JavaKind.Object}, 1, 0, 0);
 211     }
 212 }