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 -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI compiler.jvmci.errors.TestInvalidCompilationResult
  29  */
  30 
  31 package compiler.jvmci.errors;
  32 
  33 import static jdk.vm.ci.code.CompilationResult.ConstantReference;
  34 import static jdk.vm.ci.code.CompilationResult.DataPatch;
  35 import static jdk.vm.ci.code.CompilationResult.DataSectionReference;
  36 import static jdk.vm.ci.code.CompilationResult.Infopoint;
  37 import static jdk.vm.ci.code.CompilationResult.Reference;
  38 import static jdk.vm.ci.code.DataSection.Data;
  39 import static jdk.vm.ci.code.DataSection.DataBuilder;
  40 import static jdk.vm.ci.meta.Assumptions.Assumption;
  41 
  42 import jdk.vm.ci.code.CompilationResult;
  43 import jdk.vm.ci.code.InfopointReason;
  44 import jdk.vm.ci.common.JVMCIError;
  45 import jdk.vm.ci.hotspot.HotSpotConstant;
  46 import jdk.vm.ci.meta.ResolvedJavaType;
  47 import jdk.vm.ci.meta.VMConstant;
  48 
  49 import org.junit.Test;
  50 
  51 /**
  52  * Tests for errors in the code installer.
  53  */
  54 public class TestInvalidCompilationResult extends CodeInstallerTest {
  55 
  56     private static class InvalidAssumption extends Assumption {
  57     }
  58 
  59     private static class InvalidVMConstant implements VMConstant {
  60 
  61         public boolean isDefaultForKind() {
  62             return false;
  63         }
  64 
  65         public String toValueString() {
  66             return null;
  67         }
  68     }
  69 
  70     private static class InvalidReference extends Reference {
  71 
  72         @Override
  73         public int hashCode() {
  74             return 0;
  75         }
  76 
  77         @Override
  78         public boolean equals(Object obj) {
  79             return false;
  80         }
  81     }
  82 
  83     @Test(expected = JVMCIError.class)
  84     public void testInvalidAssumption() {
  85         CompilationResult result = new CompilationResult();
  86         result.setAssumptions(new Assumption[]{new InvalidAssumption()});
  87         installCode(result);
  88     }
  89 
  90     @Test(expected = JVMCIError.class)
  91     public void testInvalidAlignment() {
  92         CompilationResult result = new CompilationResult();
  93         result.getDataSection().insertData(new Data(7, 1, DataBuilder.zero(1)));
  94         installCode(result);
  95     }
  96 
  97     @Test(expected = NullPointerException.class)
  98     public void testNullDataPatchInDataSection() {
  99         CompilationResult result = new CompilationResult();
 100         Data data = new Data(1, 1, (buffer, patch) -> {
 101             patch.accept(null);
 102             buffer.put((byte) 0);
 103         });
 104         result.getDataSection().insertData(data);
 105         installCode(result);
 106     }
 107 
 108     @Test(expected = NullPointerException.class)
 109     public void testNullReferenceInDataSection() {
 110         CompilationResult result = new CompilationResult();
 111         Data data = new Data(1, 1, (buffer, patch) -> {
 112             patch.accept(new DataPatch(buffer.position(), null));
 113             buffer.put((byte) 0);
 114         });
 115         result.getDataSection().insertData(data);
 116         installCode(result);
 117     }
 118 
 119     @Test(expected = JVMCIError.class)
 120     public void testInvalidDataSectionReference() {
 121         CompilationResult result = new CompilationResult();
 122         DataSectionReference ref = result.getDataSection().insertData(new Data(1, 1, DataBuilder.zero(1)));
 123         Data data = new Data(1, 1, (buffer, patch) -> {
 124             patch.accept(new DataPatch(buffer.position(), ref));
 125             buffer.put((byte) 0);
 126         });
 127         result.getDataSection().insertData(data);
 128         installCode(result);
 129     }
 130 
 131     @Test(expected = JVMCIError.class)
 132     public void testInvalidNarrowMethodInDataSection() {
 133         CompilationResult result = new CompilationResult();
 134         HotSpotConstant c = (HotSpotConstant) dummyMethod.getEncoding();
 135         Data data = new Data(4, 4, (buffer, patch) -> {
 136             patch.accept(new DataPatch(buffer.position(), new ConstantReference((VMConstant) c.compress())));
 137             buffer.putInt(0);
 138         });
 139         result.getDataSection().insertData(data);
 140         installCode(result);
 141     }
 142 
 143     @Test(expected = NullPointerException.class)
 144     public void testNullConstantInDataSection() {
 145         CompilationResult result = new CompilationResult();
 146         Data data = new Data(1, 1, (buffer, patch) -> {
 147             patch.accept(new DataPatch(buffer.position(), new ConstantReference(null)));
 148         });
 149         result.getDataSection().insertData(data);
 150         installCode(result);
 151     }
 152 
 153     @Test(expected = JVMCIError.class)
 154     public void testInvalidConstantInDataSection() {
 155         CompilationResult result = new CompilationResult();
 156         Data data = new Data(1, 1, (buffer, patch) -> {
 157             patch.accept(new DataPatch(buffer.position(), new ConstantReference(new InvalidVMConstant())));
 158         });
 159         result.getDataSection().insertData(data);
 160         installCode(result);
 161     }
 162 
 163     @Test(expected = NullPointerException.class)
 164     public void testNullReferenceInCode() {
 165         CompilationResult result = new CompilationResult();
 166         result.recordDataPatch(0, null);
 167         installCode(result);
 168     }
 169 
 170     @Test(expected = NullPointerException.class)
 171     public void testNullConstantInCode() {
 172         CompilationResult result = new CompilationResult();
 173         result.recordDataPatch(0, new ConstantReference(null));
 174         installCode(result);
 175     }
 176 
 177     @Test(expected = JVMCIError.class)
 178     public void testInvalidConstantInCode() {
 179         CompilationResult result = new CompilationResult();
 180         result.recordDataPatch(0, new ConstantReference(new InvalidVMConstant()));
 181         installCode(result);
 182     }
 183 
 184     @Test(expected = JVMCIError.class)
 185     public void testInvalidReference() {
 186         CompilationResult result = new CompilationResult();
 187         result.recordDataPatch(0, new InvalidReference());
 188         installCode(result);
 189     }
 190 
 191     @Test(expected = JVMCIError.class)
 192     public void testOutOfBoundsDataSectionReference() {
 193         CompilationResult result = new CompilationResult();
 194         DataSectionReference ref = new DataSectionReference();
 195         ref.setOffset(0x1000);
 196         result.recordDataPatch(0, ref);
 197         installCode(result);
 198     }
 199 
 200     @Test(expected = JVMCIError.class)
 201     public void testInvalidMark() {
 202         CompilationResult result = new CompilationResult();
 203         result.recordMark(0, new Object());
 204         installCode(result);
 205     }
 206 
 207     @Test(expected = JVMCIError.class)
 208     public void testInvalidMarkInt() {
 209         CompilationResult result = new CompilationResult();
 210         result.recordMark(0, -1);
 211         installCode(result);
 212     }
 213 
 214     @Test(expected = NullPointerException.class)
 215     public void testNullInfopoint() {
 216         CompilationResult result = new CompilationResult();
 217         result.addInfopoint(null);
 218         installCode(result);
 219     }
 220 
 221     @Test(expected = JVMCIError.class)
 222     public void testUnknownInfopointReason() {
 223         CompilationResult result = new CompilationResult();
 224         result.addInfopoint(new Infopoint(0, null, InfopointReason.UNKNOWN));
 225         installCode(result);
 226     }
 227 
 228     @Test(expected = JVMCIError.class)
 229     public void testInfopointMissingDebugInfo() {
 230         CompilationResult result = new CompilationResult();
 231         result.addInfopoint(new Infopoint(0, null, InfopointReason.METHOD_START));
 232         installCode(result);
 233     }
 234 
 235     @Test(expected = JVMCIError.class)
 236     public void testSafepointMissingDebugInfo() {
 237         CompilationResult result = new CompilationResult();
 238         result.addInfopoint(new Infopoint(0, null, InfopointReason.SAFEPOINT));
 239         installCode(result);
 240     }
 241 }