1 /*
   2  * Copyright (c) 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 /*
  26  * @test
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library /testlibrary /test/lib /compiler/jvmci/jdk.vm.ci.hotspot.test/src
  29  * @modules jdk.vm.ci/jdk.vm.ci.meta
  30  *          jdk.vm.ci/jdk.vm.ci.common
  31  *          jdk.vm.ci/jdk.vm.ci.runtime
  32  *          jdk.vm.ci/jdk.vm.ci.hotspot
  33  * @run testng/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  34  *      jdk.vm.ci.hotspot.test.MemoryAccessProviderTest
  35  */
  36 
  37 package jdk.vm.ci.hotspot.test;
  38 
  39 import jdk.vm.ci.meta.Constant;
  40 import org.testng.annotations.Test;
  41 import org.testng.Assert;
  42 import jdk.vm.ci.meta.JavaConstant;
  43 import jdk.vm.ci.meta.JavaKind;
  44 import jdk.vm.ci.meta.MemoryAccessProvider;
  45 import jdk.vm.ci.runtime.JVMCI;
  46 
  47 public class MemoryAccessProviderTest {
  48     private static final MemoryAccessProvider PROVIDER = JVMCI.getRuntime()
  49             .getHostJVMCIBackend()
  50             .getConstantReflection()
  51             .getMemoryAccessProvider();
  52 
  53     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class)
  54     public void testPositiveReadUnsafeConstant(JavaKind kind, JavaConstant base,
  55             Long offset, Object expected, int bitsCount /* not used */) {
  56         Assert.assertEquals(PROVIDER.readUnsafeConstant(kind, base, offset),
  57                 expected, "Failed to read constant");
  58     }
  59 
  60     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class,
  61             expectedExceptions = {IllegalArgumentException.class})
  62     public void testReadUnsafeConstantNullBase(JavaKind kind,
  63             JavaConstant base, Long offset, Object expected, int bitsCount /* not used */) {
  64         PROVIDER.readUnsafeConstant(kind, null, offset);
  65     }
  66 
  67     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class,
  68             expectedExceptions = {IllegalArgumentException.class})
  69     public void testNegativeReadUnsafeConstantNullKind(JavaKind kind, JavaConstant base,
  70             Long offset, Object expected /* not used */, int bitsCount /* not used */) {
  71         Assert.assertNull(PROVIDER.readUnsafeConstant(null, base, offset), "Expected null return");
  72     }
  73 
  74     @Test(dataProvider = "negative", dataProviderClass = MemoryAccessProviderData.class,
  75             expectedExceptions = {IllegalArgumentException.class})
  76     public void testNegativeReadUnsafeConstant(JavaKind kind, JavaConstant base) {
  77         PROVIDER.readUnsafeConstant(kind, base, 0L);
  78     }
  79 
  80     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class)
  81     public void testPositiveReadPrimitiveConstant(JavaKind kind, Constant base,
  82             Long offset, Object expected, int bitsCount) {
  83         Assert.assertEquals(PROVIDER.readPrimitiveConstant(kind, base, offset,
  84                 bitsCount), expected, "Failed to read constant");
  85     }
  86 
  87     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class,
  88             expectedExceptions = {IllegalArgumentException.class})
  89     public void testReadPrimitiveConstantNullBase(JavaKind kind,
  90             Constant base, Long offset, Object expected, int bitsCount) {
  91         Assert.assertNull(PROVIDER.readPrimitiveConstant(kind, null, offset, bitsCount),
  92                 "Unexpected value for null base");
  93     }
  94 
  95     @Test(dataProvider = "negative", dataProviderClass = MemoryAccessProviderData.class,
  96             expectedExceptions = {IllegalArgumentException.class})
  97     public void testNegativeReadPrimitiveConstant(JavaKind kind, Constant base) {
  98         PROVIDER.readPrimitiveConstant(kind, base, 0L, kind == null ? 0 : kind.getBitCount());
  99     }
 100 
 101     @Test(dataProvider = "positiveObject", dataProviderClass = MemoryAccessProviderData.class,
 102             expectedExceptions = {IllegalArgumentException.class})
 103     public void testObjectReadPrimitiveConstant(JavaKind kind, Constant base,
 104             Long offset /* not used */, Object expected /* not used */, int bitsCount) {
 105         PROVIDER.readPrimitiveConstant(kind, base, 0L, bitsCount);
 106     }
 107 
 108     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class,
 109             expectedExceptions = {IllegalArgumentException.class})
 110     public void testReadPrimitiveConstantLessBits(JavaKind kind, Constant base,
 111             Long offset, Object expected /* not used */, int bitsCount /* not used */) {
 112         PROVIDER.readPrimitiveConstant(kind, base, offset, bitsCount - 1);
 113     }
 114 
 115     @Test(dataProvider = "positiveObject", dataProviderClass = MemoryAccessProviderData.class)
 116     public void testPositiveReadObjectConstant(JavaKind kind /* not used */, Constant base,
 117             Long offset, Object expected, int bitsCount /* not used */) {
 118         Assert.assertEquals(PROVIDER.readObjectConstant(base, offset), expected,
 119                 "Unexpected result");
 120     }
 121 
 122     @Test(dataProvider = "positiveObject", dataProviderClass = MemoryAccessProviderData.class)
 123     public void testNegativeReadObjectConstantNullBase(JavaKind kind /* not used */,
 124             Constant base, Long offset, Object expected /* not used */, int bitsCount /* not used */) {
 125         Assert.assertNull(PROVIDER.readObjectConstant(null, offset), "Unexpected return");
 126     }
 127 
 128     @Test(dataProvider = "positiveObject", dataProviderClass = MemoryAccessProviderData.class)
 129     public void testNegativeReadObjectConstantWrongOffset(JavaKind kind /* not used */,
 130             Constant base, Long offset, Object expected /* not used */, int bitsCount /* not used */) {
 131         Assert.assertNull(PROVIDER.readObjectConstant(base, offset + 1), "Expected null");
 132     }
 133 
 134     @Test(dataProvider = "positivePrimitive", dataProviderClass = MemoryAccessProviderData.class)
 135     public void testNegativeReadObjectConstantPrimitiveBase(JavaKind kind /* not used*/,
 136             Constant base, Long offset, Object expected /* not used */, int bitsCount /* not used */) {
 137         Assert.assertNull(PROVIDER.readObjectConstant(base, offset), "Expected null");
 138     }
 139 }