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.hotspot
  28  *          jdk.vm.ci/jdk.vm.ci.code
  29  *          jdk.vm.ci/jdk.vm.ci.code.site
  30  *          jdk.vm.ci/jdk.vm.ci.meta
  31  *          jdk.vm.ci/jdk.vm.ci.runtime
  32  *          jdk.vm.ci/jdk.vm.ci.common
  33  * @compile CodeInstallerTest.java
  34  * @build compiler.jvmci.errors.TestInvalidCompilationResult
  35  * @run junit/othervm -da:jdk.vm.ci... -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI compiler.jvmci.errors.TestInvalidCompilationResult
  36  */
  37 
  38 package compiler.jvmci.errors;
  39 
  40 import jdk.vm.ci.code.StackSlot;
  41 import jdk.vm.ci.code.site.ConstantReference;
  42 import jdk.vm.ci.code.site.DataPatch;
  43 import jdk.vm.ci.code.site.DataSectionReference;
  44 import jdk.vm.ci.code.site.Infopoint;
  45 import jdk.vm.ci.code.site.InfopointReason;
  46 import jdk.vm.ci.code.site.Mark;
  47 import jdk.vm.ci.code.site.Reference;
  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.HotSpotConstant;
  52 import jdk.vm.ci.meta.Assumptions.Assumption;
  53 import jdk.vm.ci.meta.VMConstant;
  54 
  55 import org.junit.Test;
  56 
  57 /**
  58  * Tests for errors in the code installer.
  59  */
  60 public class TestInvalidCompilationResult extends CodeInstallerTest {
  61 
  62     private static class InvalidAssumption extends Assumption {
  63     }
  64 
  65     private static class InvalidVMConstant implements VMConstant {
  66 
  67         public boolean isDefaultForKind() {
  68             return false;
  69         }
  70 
  71         public String toValueString() {
  72             return null;
  73         }
  74     }
  75 
  76     private static class InvalidReference extends Reference {
  77 
  78         @Override
  79         public int hashCode() {
  80             return 0;
  81         }
  82 
  83         @Override
  84         public boolean equals(Object obj) {
  85             return false;
  86         }
  87     }
  88 
  89     @Test(expected = JVMCIError.class)
  90     public void testInvalidAssumption() {
  91         installEmptyCode(new Site[0], new Assumption[]{new InvalidAssumption()}, new Comment[0], 16, new DataPatch[0], null);
  92     }
  93 
  94     @Test(expected = JVMCIError.class)
  95     public void testInvalidAlignment() {
  96         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 7, new DataPatch[0], null);
  97     }
  98 
  99     @Test(expected = NullPointerException.class)
 100     public void testNullDataPatchInDataSection() {
 101         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{null}, null);
 102     }
 103 
 104     @Test(expected = NullPointerException.class)
 105     public void testNullReferenceInDataSection() {
 106         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{new DataPatch(0, null)}, null);
 107     }
 108 
 109     @Test(expected = JVMCIError.class)
 110     public void testInvalidDataSectionReference() {
 111         DataSectionReference ref = new DataSectionReference();
 112         ref.setOffset(0);
 113         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{new DataPatch(0, ref)}, null);
 114     }
 115 
 116     @Test(expected = JVMCIError.class)
 117     public void testInvalidNarrowMethodInDataSection() {
 118         HotSpotConstant c = (HotSpotConstant) dummyMethod.getEncoding();
 119         ConstantReference ref = new ConstantReference((VMConstant) c.compress());
 120         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{new DataPatch(0, ref)}, null);
 121     }
 122 
 123     @Test(expected = NullPointerException.class)
 124     public void testNullConstantInDataSection() {
 125         ConstantReference ref = new ConstantReference(null);
 126         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{new DataPatch(0, ref)}, null);
 127     }
 128 
 129     @Test(expected = JVMCIError.class)
 130     public void testInvalidConstantInDataSection() {
 131         ConstantReference ref = new ConstantReference(new InvalidVMConstant());
 132         installEmptyCode(new Site[0], new Assumption[0], new Comment[0], 16, new DataPatch[]{new DataPatch(0, ref)}, null);
 133     }
 134 
 135     @Test(expected = NullPointerException.class)
 136     public void testNullReferenceInCode() {
 137         installEmptyCode(new Site[]{new DataPatch(0, null)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 138     }
 139 
 140     @Test(expected = NullPointerException.class)
 141     public void testNullConstantInCode() {
 142         ConstantReference ref = new ConstantReference(null);
 143         installEmptyCode(new Site[]{new DataPatch(0, ref)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 144     }
 145 
 146     @Test(expected = JVMCIError.class)
 147     public void testInvalidConstantInCode() {
 148         ConstantReference ref = new ConstantReference(new InvalidVMConstant());
 149         installEmptyCode(new Site[]{new DataPatch(0, ref)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 150     }
 151 
 152     @Test(expected = JVMCIError.class)
 153     public void testInvalidReference() {
 154         InvalidReference ref = new InvalidReference();
 155         installEmptyCode(new Site[]{new DataPatch(0, ref)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 156     }
 157 
 158     @Test(expected = JVMCIError.class)
 159     public void testOutOfBoundsDataSectionReference() {
 160         DataSectionReference ref = new DataSectionReference();
 161         ref.setOffset(0x1000);
 162         installEmptyCode(new Site[]{new DataPatch(0, ref)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 163     }
 164 
 165     @Test(expected = JVMCIError.class)
 166     public void testInvalidMark() {
 167         installEmptyCode(new Site[]{new Mark(0, new Object())}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 168     }
 169 
 170     @Test(expected = JVMCIError.class)
 171     public void testInvalidMarkInt() {
 172         installEmptyCode(new Site[]{new Mark(0, -1)}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 173     }
 174 
 175     @Test(expected = NullPointerException.class)
 176     public void testNullSite() {
 177         installEmptyCode(new Site[]{null}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 178     }
 179 
 180     @Test(expected = JVMCIError.class)
 181     public void testInfopointMissingDebugInfo() {
 182         Infopoint info = new Infopoint(0, null, InfopointReason.METHOD_START);
 183         installEmptyCode(new Site[]{info}, new Assumption[0], new Comment[0], 16, new DataPatch[0], null);
 184     }
 185 
 186     @Test(expected = JVMCIError.class)
 187     public void testSafepointMissingDebugInfo() {
 188         Infopoint info = new Infopoint(0, null, InfopointReason.SAFEPOINT);
 189         StackSlot deoptRescueSlot = StackSlot.get(null, 0, true);
 190         installEmptyCode(new Site[]{info}, new Assumption[0], new Comment[0], 16, new DataPatch[0], deoptRescueSlot);
 191     }
 192 
 193     @Test(expected = JVMCIError.class)
 194     public void testInvalidDeoptRescueSlot() {
 195         StackSlot deoptRescueSlot = StackSlot.get(null, -1, false);
 196         installEmptyCode(new Site[]{}, new Assumption[0], new Comment[0], 16, new DataPatch[0], deoptRescueSlot);
 197     }
 198 }