1 /*
   2  * Copyright (c) 2012, 2020, 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     private static boolean isHiddenClass(Class<?> cls) {
  72         if (cls.isHiddenClass()) {
  73             return true;
  74         }
  75 
  76         // Check array of hidden type.
  77         while (cls.getComponentType() != null) {
  78             cls = cls.getComponentType();
  79         }
  80         if (cls.isHiddenClass()) {
  81             return true;
  82         }
  83         return false;
  84     }
  85 
  86     private static boolean isUnsafeAnonymous(ResolvedJavaType type) {
  87         return type.getHostClass() != null;
  88     }
  89 
  90     @Test
  91     public void lookupJavaTypeTest() {
  92         for (Class<?> c : classes) {
  93             ResolvedJavaType type = metaAccess.lookupJavaType(c);
  94             assertNotNull(c.toString(), type);
  95             if (!isHiddenClass(c) && !isUnsafeAnonymous(type)) {
  96                 assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
  97                 assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
  98                 assertEquals(c.toString(), c.getName(), type.toClassName());
  99                 if (!type.isArray()) {
 100                     assertEquals(c.toString(), c.getName(), type.toJavaName());
 101                 }
 102             }
 103         }
 104     }
 105 
 106     @Test(expected = IllegalArgumentException.class)
 107     public void lookupJavaTypeNegativeTest() {
 108         metaAccess.lookupJavaType((Class<?>) null);
 109     }
 110 
 111     @Test
 112     public void lookupJavaTypesTest() {
 113         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
 114         int counter = 0;
 115         for (Class<?> aClass : classes) {
 116             if (!isHiddenClass(aClass) && !isUnsafeAnonymous(result[counter])) {
 117                 assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
 118             }
 119             counter++;
 120          }
 121     }
 122 
 123     @Test(expected = NullPointerException.class)
 124     public void lookupJavaTypesNegative1Test() {
 125         assertNull("Expected null", metaAccess.lookupJavaTypes(null));
 126     }
 127 
 128     @Test(expected = IllegalArgumentException.class)
 129     public void lookupJavaTypesNegative2Test() {
 130         ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
 131         for (ResolvedJavaType aType : result) {
 132             assertNull("Expected null javaType", aType);
 133         }
 134         result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
 135         assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
 136         assertEquals("Result is not String.class", result[0].getClass(), String.class);
 137     }
 138 
 139     @Test
 140     public void lookupJavaMethodTest() {
 141         for (Class<?> c : classes) {
 142             for (Method reflect : c.getDeclaredMethods()) {
 143                 ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect);
 144                 assertNotNull(method);
 145                 assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 146             }
 147         }
 148     }
 149 
 150     @Test(expected = NullPointerException.class)
 151     public void lookupJavaMethodNegativeTest() {
 152         metaAccess.lookupJavaMethod(null);
 153     }
 154 
 155     @Test
 156     public void lookupJavaFieldTest() {
 157         for (Class<?> c : classes) {
 158             for (Field reflect : c.getDeclaredFields()) {
 159                 ResolvedJavaField field = metaAccess.lookupJavaField(reflect);
 160                 assertNotNull(field);
 161                 assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
 162             }
 163         }
 164     }
 165 
 166     @Test
 167     public void lookupJavaTypeConstantTest() {
 168         for (ConstantValue cv : constants()) {
 169             JavaConstant c = cv.value;
 170             if (c.getJavaKind() == JavaKind.Object && !c.isNull()) {
 171                 Object o = cv.boxed;
 172                 ResolvedJavaType type = metaAccess.lookupJavaType(c);
 173                 assertNotNull(type);
 174                 assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass())));
 175             } else {
 176                 assertEquals(metaAccess.lookupJavaType(c), null);
 177             }
 178         }
 179     }
 180 
 181     @Test(expected = NullPointerException.class)
 182     public void lookupJavaTypeConstantNegativeTest() {
 183         metaAccess.lookupJavaType((JavaConstant) null);
 184     }
 185 
 186     @Test
 187     public void getMemorySizeTest() {
 188         for (ConstantValue cv : constants()) {
 189             JavaConstant c = cv.value;
 190             long memSize = metaAccess.getMemorySize(c);
 191             if (c.isNull()) {
 192                 assertEquals("Expected size = 0 for null", memSize, 0L);
 193             } else {
 194                 assertTrue("Expected size != 0 for " + cv, memSize != 0L);
 195             }
 196         }
 197     }
 198 
 199     @Test(expected = NullPointerException.class)
 200     public void getMemorySizeNegativeTest() {
 201         metaAccess.getMemorySize(null);
 202     }
 203 
 204     @Test
 205     public void parseMethodDescriptorTest() {
 206         for (String retType : new String[]{"V", "Z", "Ljava/lang/String;"}) {
 207             for (String paramTypes : new String[]{"", "B",
 208                             "Ljava/lang/String;", "JLjava/lang/String;",
 209                             "Ljava/lang/String;F",
 210                             "[Ljava/lang/String;ZBCDFIJLS[ILjava/lang/Object;"}) {
 211                 String signature = "(" + paramTypes + ")" + retType;
 212                 Signature result = metaAccess.parseMethodDescriptor(signature);
 213                 assertEquals("Expected signatures to be equal", result.toMethodDescriptor(), signature);
 214             }
 215         }
 216     }
 217 
 218     @Test(expected = NullPointerException.class)
 219     public void parseMethodDescriptorNegativeNullTest() {
 220         metaAccess.parseMethodDescriptor(null);
 221     }
 222 
 223     @Test(expected = NullPointerException.class)
 224     public void encodeDeoptActionAndReasonNegative1Test() {
 225         metaAccess.encodeDeoptActionAndReason(null, DeoptimizationReason.Aliasing, 0);
 226 
 227     }
 228 
 229     @Test(expected = NullPointerException.class)
 230     public void encodeDeoptActionAndReasonNegative2Test() {
 231         metaAccess.encodeDeoptActionAndReason(DeoptimizationAction.InvalidateRecompile, null, 0);
 232     }
 233 
 234     @Test
 235     public void decodeDeoptReasonTest() {
 236         for (int encoded : VALID_ENCODED_VALUES) {
 237             JavaConstant value = JavaConstant.forInt(encoded);
 238             DeoptimizationReason reason = metaAccess.decodeDeoptReason(value);
 239             assertEquals("Expected equal reasons", reason, DEOPT_REASON);
 240         }
 241     }
 242 
 243     @Test
 244     public void decodeDeoptReasonNegative1Test() {
 245         int encoded = 42;
 246         JavaConstant value = JavaConstant.forInt(encoded);
 247         metaAccess.decodeDeoptReason(value);
 248     }
 249 
 250     @Test(expected = NullPointerException.class)
 251     public void decodeDeoptReasonNegative2Test() {
 252         metaAccess.decodeDeoptReason(null);
 253     }
 254 
 255     @Test
 256     public void decodeDeoptActionTest() {
 257         for (int encoded : VALID_ENCODED_VALUES) {
 258             JavaConstant value = JavaConstant.forInt(encoded);
 259             DeoptimizationAction action = metaAccess.decodeDeoptAction(value);
 260             assertEquals("Expected equal actions", action, DEOPT_ACTION);
 261         }
 262     }
 263 
 264     @Test
 265     public void decodeDeoptActionNegative1Test() {
 266         int encoded = 123456789;
 267         JavaConstant value = JavaConstant.forInt(encoded);
 268         metaAccess.decodeDeoptAction(value);
 269     }
 270 
 271     @Test(expected = NullPointerException.class)
 272     public void decodeDeoptActionNegative2Test() {
 273         metaAccess.decodeDeoptAction(null);
 274     }
 275 
 276     @Test
 277     public void decodeDebugIdTest() {
 278         for (int i = 0; i < VALID_ENCODED_VALUES.length; i++) {
 279             JavaConstant value = JavaConstant.forInt(VALID_ENCODED_VALUES[i]);
 280             assertEquals("Unexpected debugId", metaAccess.decodeDebugId(value), DEBUG_IDS[i]);
 281         }
 282     }
 283 
 284     @Test
 285     public void parseSignatureTest() {
 286         for (String badSig : new String[]{"", "()", "(", "()Vextra", "()E", "(E)", "(Ljava.lang.Object;)V"}) {
 287             try {
 288                 metaAccess.parseMethodDescriptor(badSig);
 289                 throw new AssertionError("Expected signature to be invalid: " + badSig);
 290             } catch (IllegalArgumentException e) {
 291             }
 292         }
 293     }
 294 }