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