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