1 /*
   2  * Copyright (c) 2012, 2019, 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 vm.jvmci
  27  * @library ../../../../../
  28  * @modules jdk.internal.vm.ci/jdk.vm.ci.meta
  29  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  30  *          java.base/jdk.internal.misc
  31  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:-UseJVMCICompiler jdk.vm.ci.runtime.test.TestMetaAccessProvider
  32  */
  33 
  34 package jdk.vm.ci.runtime.test;
  35 
  36 import jdk.vm.ci.meta.DeoptimizationAction;
  37 import jdk.vm.ci.meta.DeoptimizationReason;
  38 import jdk.vm.ci.meta.JavaConstant;
  39 import jdk.vm.ci.meta.JavaKind;
  40 import jdk.vm.ci.meta.MetaAccessProvider;
  41 import jdk.vm.ci.meta.ResolvedJavaField;
  42 import jdk.vm.ci.meta.ResolvedJavaMethod;
  43 import jdk.vm.ci.meta.ResolvedJavaType;
  44 import jdk.vm.ci.meta.Signature;
  45 import org.junit.Test;
  46 
  47 import java.lang.reflect.Field;
  48 import java.lang.reflect.Method;
  49 
  50 import static jdk.vm.ci.meta.MetaUtil.toInternalName;
  51 import static org.junit.Assert.assertEquals;
  52 import static org.junit.Assert.assertNotNull;
  53 import static org.junit.Assert.assertNull;
  54 import static org.junit.Assert.assertTrue;
  55 
  56 /**
  57  * Tests for {@link MetaAccessProvider}.
  58  */
  59 public class TestMetaAccessProvider extends TypeUniverse {
  60     private static final DeoptimizationAction DEOPT_ACTION = DeoptimizationAction.InvalidateRecompile;
  61     private static final DeoptimizationReason DEOPT_REASON = DeoptimizationReason.Aliasing;
  62     private static final int INT_23BITS_SET = 0x7FFFFF;
  63     private static final int[] DEBUG_IDS = new int[]{0, 1, 42, INT_23BITS_SET};
  64     private static final int[] VALID_ENCODED_VALUES = new int[]{
  65                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[0]).asInt(),
  66                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[1]).asInt(),
  67                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[2]).asInt(),
  68                     metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[3]).asInt()
  69     };
  70 
  71     @Test
  72     public void lookupJavaTypeTest() {
  73         for (Class<?> c : classes) {
  74             ResolvedJavaType type = metaAccess.lookupJavaType(c);
  75             assertNotNull(c.toString(), type);
  76             assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
  77             assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
  78             assertEquals(c.toString(), c.getName(), type.toClassName());
  79             if (!type.isArray()) {
  80                 assertEquals(c.toString(), c.getName(), type.toJavaName());
  81             }
  82         }
  83     }
  84 
  85     @Test(expected = IllegalArgumentException.class)
  86     public void lookupJavaTypeNegativeTest() {
  87         metaAccess.lookupJavaType((Class<?>) null);
  88     }
  89 
  90     @Test
  91     public void lookupJavaTypesTest() {
  92         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
  93         int counter = 0;
  94         for (Class<?> aClass : classes) {
  95             assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
  96             counter++;
  97         }
  98     }
  99 
 100     @Test(expected = NullPointerException.class)
 101     public void lookupJavaTypesNegative1Test() {
 102         assertNull("Expected null", metaAccess.lookupJavaTypes(null));
 103     }
 104 
 105     @Test(expected = IllegalArgumentException.class)
 106     public void lookupJavaTypesNegative2Test() {
 107         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
 108         for (ResolvedJavaType aType : result) {
 109             assertNull("Expected null javaType", aType);
 110         }
 111         result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
 112         assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
 113         assertEquals("Result is not String.class", result[0].getClass(), String.class);
 114     }
 115 
 116     @Test
 117     public void lookupJavaMethodTest() {
 118         for (Class<?> c : classes) {
 119             for (Method reflect : c.getDeclaredMethods()) {
 120                 ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect);
 121                 assertNotNull(method);
 122                 assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 123             }
 124         }
 125     }
 126 
 127     @Test(expected = NullPointerException.class)
 128     public void lookupJavaMethodNegativeTest() {
 129         metaAccess.lookupJavaMethod(null);
 130     }
 131 
 132     @Test
 133     public void lookupJavaFieldTest() {
 134         for (Class<?> c : classes) {
 135             for (Field reflect : c.getDeclaredFields()) {
 136                 ResolvedJavaField field = metaAccess.lookupJavaField(reflect);
 137                 assertNotNull(field);
 138                 assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 139             }
 140         }
 141     }
 142 
 143     @Test
 144     public void lookupJavaTypeConstantTest() {
 145         for (ConstantValue cv : constants()) {
 146             JavaConstant c = cv.value;
 147             if (c.getJavaKind() == JavaKind.Object && !c.isNull()) {
 148                 Object o = cv.boxed;
 149                 ResolvedJavaType type = metaAccess.lookupJavaType(c);
 150                 assertNotNull(type);
 151                 assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass())));
 152             } else {
 153                 assertEquals(metaAccess.lookupJavaType(c), null);
 154             }
 155         }
 156     }
 157 
 158     @Test(expected = NullPointerException.class)
 159     public void lookupJavaTypeConstantNegativeTest() {
 160         metaAccess.lookupJavaType((JavaConstant) null);
 161     }
 162 
 163     @Test
 164     public void getMemorySizeTest() {
 165         for (ConstantValue cv : constants()) {
 166             JavaConstant c = cv.value;
 167             long memSize = metaAccess.getMemorySize(c);
 168             if (c.isNull()) {
 169                 assertEquals("Expected size = 0 for null", memSize, 0L);
 170             } else {
 171                 assertTrue("Expected size != 0 for " + cv, memSize != 0L);
 172             }
 173         }
 174     }
 175 
 176     @Test(expected = NullPointerException.class)
 177     public void getMemorySizeNegativeTest() {
 178         metaAccess.getMemorySize(null);
 179     }
 180 
 181     @Test
 182     public void parseMethodDescriptorTest() {
 183         for (String retType : new String[]{"V", "Z", "Ljava/lang/String;"}) {
 184             for (String paramTypes : new String[]{"", "B",
 185                             "Ljava/lang/String;", "JLjava/lang/String;",
 186                             "Ljava/lang/String;F",
 187                             "[Ljava/lang/String;ZBCDFIJLS[ILjava/lang/Object;"}) {
 188                 String signature = "(" + paramTypes + ")" + retType;
 189                 Signature result = metaAccess.parseMethodDescriptor(signature);
 190                 assertEquals("Expected signatures to be equal", result.toMethodDescriptor(), signature);
 191             }
 192         }
 193     }
 194 
 195     @Test(expected = NullPointerException.class)
 196     public void parseMethodDescriptorNegativeNullTest() {
 197         metaAccess.parseMethodDescriptor(null);
 198     }
 199 
 200     @Test(expected = NullPointerException.class)
 201     public void encodeDeoptActionAndReasonNegative1Test() {
 202         metaAccess.encodeDeoptActionAndReason(null, DeoptimizationReason.Aliasing, 0);
 203 
 204     }
 205 
 206     @Test(expected = NullPointerException.class)
 207     public void encodeDeoptActionAndReasonNegative2Test() {
 208         metaAccess.encodeDeoptActionAndReason(DeoptimizationAction.InvalidateRecompile, null, 0);
 209     }
 210 
 211     @Test
 212     public void decodeDeoptReasonTest() {
 213         for (int encoded : VALID_ENCODED_VALUES) {
 214             JavaConstant value = JavaConstant.forInt(encoded);
 215             DeoptimizationReason reason = metaAccess.decodeDeoptReason(value);
 216             assertEquals("Expected equal reasons", reason, DEOPT_REASON);
 217         }
 218     }
 219 
 220     @Test
 221     public void decodeDeoptReasonNegative1Test() {
 222         int encoded = 42;
 223         JavaConstant value = JavaConstant.forInt(encoded);
 224         metaAccess.decodeDeoptReason(value);
 225     }
 226 
 227     @Test(expected = NullPointerException.class)
 228     public void decodeDeoptReasonNegative2Test() {
 229         metaAccess.decodeDeoptReason(null);
 230     }
 231 
 232     @Test
 233     public void decodeDeoptActionTest() {
 234         for (int encoded : VALID_ENCODED_VALUES) {
 235             JavaConstant value = JavaConstant.forInt(encoded);
 236             DeoptimizationAction action = metaAccess.decodeDeoptAction(value);
 237             assertEquals("Expected equal actions", action, DEOPT_ACTION);
 238         }
 239     }
 240 
 241     @Test
 242     public void decodeDeoptActionNegative1Test() {
 243         int encoded = 123456789;
 244         JavaConstant value = JavaConstant.forInt(encoded);
 245         metaAccess.decodeDeoptAction(value);
 246     }
 247 
 248     @Test(expected = NullPointerException.class)
 249     public void decodeDeoptActionNegative2Test() {
 250         metaAccess.decodeDeoptAction(null);
 251     }
 252 
 253     @Test
 254     public void decodeDebugIdTest() {
 255         for (int i = 0; i < VALID_ENCODED_VALUES.length; i++) {
 256             JavaConstant value = JavaConstant.forInt(VALID_ENCODED_VALUES[i]);
 257             assertEquals("Unexpected debugId", metaAccess.decodeDebugId(value), DEBUG_IDS[i]);
 258         }
 259     }
 260 
 261     @Test
 262     public void parseSignatureTest() {
 263         for (String badSig : new String[]{"", "()", "(", "()Vextra", "()E", "(E)", "(Ljava.lang.Object;)V"}) {
 264             try {
 265                 metaAccess.parseMethodDescriptor(badSig);
 266                 throw new AssertionError("Expected signature to be invalid: " + badSig);
 267             } catch (IllegalArgumentException e) {
 268             }
 269         }
 270     }
 271 }