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.TestInvalidOopMap
  29  */
  30 
  31 package compiler.jvmci.errors;
  32 
  33 import static jdk.vm.ci.code.CompilationResult.Infopoint;
  34 
  35 import jdk.vm.ci.code.BytecodePosition;
  36 import jdk.vm.ci.code.CompilationResult;
  37 import jdk.vm.ci.code.DebugInfo;
  38 import jdk.vm.ci.code.InfopointReason;
  39 import jdk.vm.ci.code.Location;
  40 import jdk.vm.ci.code.ReferenceMap;
  41 import jdk.vm.ci.code.Register;
  42 import jdk.vm.ci.hotspot.HotSpotReferenceMap;
  43 import jdk.vm.ci.hotspot.HotSpotVMConfig;
  44 import jdk.vm.ci.meta.JavaKind;
  45 import jdk.vm.ci.meta.LIRKind;
  46 import jdk.vm.ci.meta.PlatformKind;
  47 import jdk.vm.ci.common.JVMCIError;
  48 
  49 import org.junit.Test;
  50 
  51 /**
  52  * Tests for errors in oop maps.
  53  */
  54 public class TestInvalidOopMap extends CodeInstallerTest {
  55 
  56     private static class InvalidReferenceMap extends ReferenceMap {
  57     }
  58 
  59     private void test(ReferenceMap refMap) {
  60         BytecodePosition pos = new BytecodePosition(null, dummyMethod, 0);
  61         DebugInfo info = new DebugInfo(pos);
  62         info.setReferenceMap(refMap);
  63 
  64         CompilationResult result = new CompilationResult();
  65         result.addInfopoint(new Infopoint(0, info, InfopointReason.SAFEPOINT));
  66         installCode(result);
  67     }
  68 
  69     @Test(expected = NullPointerException.class)
  70     public void testMissingReferenceMap() {
  71         test(null);
  72     }
  73 
  74     @Test(expected = JVMCIError.class)
  75     public void testInvalidReferenceMap() {
  76         test(new InvalidReferenceMap());
  77     }
  78 
  79     @Test(expected = NullPointerException.class)
  80     public void testNullOops() {
  81         test(new HotSpotReferenceMap(null, new Location[0], new int[0], 8));
  82     }
  83 
  84     @Test(expected = NullPointerException.class)
  85     public void testNullBase() {
  86         test(new HotSpotReferenceMap(new Location[0], null, new int[0], 8));
  87     }
  88 
  89     @Test(expected = NullPointerException.class)
  90     public void testNullSize() {
  91         test(new HotSpotReferenceMap(new Location[0], new Location[0], null, 8));
  92     }
  93 
  94     @Test(expected = JVMCIError.class)
  95     public void testInvalidLength() {
  96         test(new HotSpotReferenceMap(new Location[1], new Location[2], new int[3], 8));
  97     }
  98 
  99     @Test(expected = JVMCIError.class)
 100     public void testInvalidShortOop() {
 101         PlatformKind kind = arch.getPlatformKind(JavaKind.Short);
 102         Register reg = getRegister(kind, 0);
 103 
 104         Location[] oops = new Location[]{Location.register(reg)};
 105         Location[] base = new Location[]{null};
 106         int[] size = new int[]{kind.getSizeInBytes()};
 107 
 108         test(new HotSpotReferenceMap(oops, base, size, 8));
 109     }
 110 
 111     @Test(expected = JVMCIError.class)
 112     public void testInvalidNarrowDerivedOop() {
 113         if (!HotSpotVMConfig.config().useCompressedOops) {
 114             throw new JVMCIError("skipping test");
 115         }
 116 
 117         PlatformKind kind = arch.getPlatformKind(JavaKind.Int);
 118         Register reg = getRegister(kind, 0);
 119         Register baseReg = getRegister(arch.getPlatformKind(JavaKind.Object), 1);
 120 
 121         Location[] oops = new Location[]{Location.register(reg)};
 122         Location[] base = new Location[]{Location.register(baseReg)};
 123         int[] size = new int[]{kind.getSizeInBytes()};
 124 
 125         test(new HotSpotReferenceMap(oops, base, size, 8));
 126     }
 127 }