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