1 /*
   2  * Copyright (c) 2011, 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 package jdk.vm.ci.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale;
  27 
  28 import java.lang.reflect.Array;
  29 
  30 import jdk.vm.ci.meta.Constant;
  31 import jdk.vm.ci.meta.ConstantReflectionProvider;
  32 import jdk.vm.ci.meta.JavaConstant;
  33 import jdk.vm.ci.meta.JavaKind;
  34 import jdk.vm.ci.meta.JavaType;
  35 import jdk.vm.ci.meta.MemoryAccessProvider;
  36 import jdk.vm.ci.meta.MethodHandleAccessProvider;
  37 import jdk.vm.ci.meta.ResolvedJavaField;
  38 import jdk.vm.ci.meta.ResolvedJavaType;
  39 
  40 /**
  41  * HotSpot implementation of {@link ConstantReflectionProvider}.
  42  */
  43 public class HotSpotConstantReflectionProvider implements ConstantReflectionProvider, HotSpotProxified {
  44 
  45     /**
  46      * Determines whether to treat {@code final} fields with default values as constant.
  47      */
  48     private static final boolean TrustFinalDefaultFields = HotSpotJVMCIRuntime.getBooleanProperty("TrustFinalDefaultFields", true);
  49 
  50     protected final HotSpotJVMCIRuntimeProvider runtime;
  51     protected final HotSpotMethodHandleAccessProvider methodHandleAccess;
  52     protected final HotSpotMemoryAccessProviderImpl memoryAccess;
  53 
  54     public HotSpotConstantReflectionProvider(HotSpotJVMCIRuntimeProvider runtime) {
  55         this.runtime = runtime;
  56         this.methodHandleAccess = new HotSpotMethodHandleAccessProvider(this);
  57         this.memoryAccess = new HotSpotMemoryAccessProviderImpl(runtime);
  58     }
  59 
  60     public MethodHandleAccessProvider getMethodHandleAccess() {
  61         return methodHandleAccess;
  62     }
  63 
  64     @Override
  65     public MemoryAccessProvider getMemoryAccessProvider() {
  66         return memoryAccess;
  67     }
  68 
  69     @Override
  70     public Boolean constantEquals(Constant x, Constant y) {
  71         if (x == y) {
  72             return true;
  73         } else if (x instanceof HotSpotObjectConstantImpl) {
  74             return y instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) x).object() == ((HotSpotObjectConstantImpl) y).object();
  75         } else {
  76             return x.equals(y);
  77         }
  78     }
  79 
  80     @Override
  81     public Integer readArrayLength(JavaConstant array) {
  82         if (array.getJavaKind() != JavaKind.Object || array.isNull()) {
  83             return null;
  84         }
  85 
  86         Object arrayObject = ((HotSpotObjectConstantImpl) array).object();
  87         if (!arrayObject.getClass().isArray()) {
  88             return null;
  89         }
  90         return Array.getLength(arrayObject);
  91     }
  92 
  93     public JavaConstant readConstantArrayElement(JavaConstant array, int index) {
  94         if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) {
  95             JavaConstant element = readArrayElement(array, index);
  96             if (element != null && (((HotSpotObjectConstantImpl) array).isDefaultStable() || !element.isDefaultForKind())) {
  97                 return element;
  98             }
  99         }
 100         return null;
 101     }
 102 
 103     /**
 104      * Try to convert {@code offset} into an an index into {@code array}.
 105      *
 106      * @return the computed index or -1 if the offset isn't within the array
 107      */
 108     private int indexForOffset(JavaConstant array, long offset) {
 109         if (array.getJavaKind() != JavaKind.Object || array.isNull()) {
 110             return -1;
 111         }
 112         Class<?> componentType = ((HotSpotObjectConstantImpl) array).object().getClass().getComponentType();
 113         JavaKind kind = runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType(componentType).getJavaKind();
 114         int arraybase = getArrayBaseOffset(kind);
 115         int scale = getArrayIndexScale(kind);
 116         if (offset < arraybase) {
 117             return -1;
 118         }
 119         long index = offset - arraybase;
 120         if (index % scale != 0) {
 121             return -1;
 122         }
 123         long result = index / scale;
 124         if (result >= Integer.MAX_VALUE) {
 125             return -1;
 126         }
 127         return (int) result;
 128     }
 129 
 130     public JavaConstant readConstantArrayElementForOffset(JavaConstant array, long offset) {
 131         if (array instanceof HotSpotObjectConstantImpl && ((HotSpotObjectConstantImpl) array).getStableDimension() > 0) {
 132             return readConstantArrayElement(array, indexForOffset(array, offset));
 133         }
 134         return null;
 135     }
 136 
 137     @Override
 138     public JavaConstant readArrayElement(JavaConstant array, int index) {
 139         if (array.getJavaKind() != JavaKind.Object || array.isNull()) {
 140             return null;
 141         }
 142         Object a = ((HotSpotObjectConstantImpl) array).object();
 143 
 144         if (index < 0 || index >= Array.getLength(a)) {
 145             return null;
 146         }
 147 
 148         if (a instanceof Object[]) {
 149             Object element = ((Object[]) a)[index];
 150             if (((HotSpotObjectConstantImpl) array).getStableDimension() > 1) {
 151                 return HotSpotObjectConstantImpl.forStableArray(element, ((HotSpotObjectConstantImpl) array).getStableDimension() - 1, ((HotSpotObjectConstantImpl) array).isDefaultStable());
 152             } else {
 153                 return HotSpotObjectConstantImpl.forObject(element);
 154             }
 155         } else {
 156             return JavaConstant.forBoxedPrimitive(Array.get(a, index));
 157         }
 158     }
 159 
 160     /**
 161      * Check if the constant is a boxed value that is guaranteed to be cached by the platform.
 162      * Otherwise the generated code might be the only reference to the boxed value and since object
 163      * references from nmethods are weak this can cause GC problems.
 164      *
 165      * @param source
 166      * @return true if the box is cached
 167      */
 168     private static boolean isBoxCached(JavaConstant source) {
 169         switch (source.getJavaKind()) {
 170             case Boolean:
 171                 return true;
 172             case Char:
 173                 return source.asInt() <= 127;
 174             case Byte:
 175             case Short:
 176             case Int:
 177                 return source.asInt() >= -128 && source.asInt() <= 127;
 178             case Long:
 179                 return source.asLong() >= -128 && source.asLong() <= 127;
 180             case Float:
 181             case Double:
 182                 return false;
 183             default:
 184                 throw new IllegalArgumentException("unexpected kind " + source.getJavaKind());
 185         }
 186     }
 187 
 188     @Override
 189     public JavaConstant boxPrimitive(JavaConstant source) {
 190         if (!source.getJavaKind().isPrimitive() || !isBoxCached(source)) {
 191             return null;
 192         }
 193         return HotSpotObjectConstantImpl.forObject(source.asBoxedPrimitive());
 194     }
 195 
 196     @Override
 197     public JavaConstant unboxPrimitive(JavaConstant source) {
 198         if (!source.getJavaKind().isObject()) {
 199             return null;
 200         }
 201         if (source.isNull()) {
 202             return null;
 203         }
 204         return JavaConstant.forBoxedPrimitive(((HotSpotObjectConstantImpl) source).object());
 205     }
 206 
 207     public JavaConstant forString(String value) {
 208         return HotSpotObjectConstantImpl.forObject(value);
 209     }
 210 
 211     public JavaConstant forObject(Object value) {
 212         return HotSpotObjectConstantImpl.forObject(value);
 213     }
 214 
 215     @Override
 216     public ResolvedJavaType asJavaType(Constant constant) {
 217         if (constant instanceof HotSpotObjectConstant) {
 218             Object obj = ((HotSpotObjectConstantImpl) constant).object();
 219             if (obj instanceof Class) {
 220                 return runtime.getHostJVMCIBackend().getMetaAccess().lookupJavaType((Class<?>) obj);
 221             }
 222         }
 223         if (constant instanceof HotSpotMetaspaceConstant) {
 224             MetaspaceWrapperObject obj = HotSpotMetaspaceConstantImpl.getMetaspaceObject(constant);
 225             if (obj instanceof HotSpotResolvedObjectTypeImpl) {
 226                 return (ResolvedJavaType) obj;
 227             }
 228         }
 229         return null;
 230     }
 231 
 232     private static final String SystemClassName = "Ljava/lang/System;";
 233 
 234     /**
 235      * Determines if a static field is constant for the purpose of
 236      * {@link #readConstantFieldValue(ResolvedJavaField, JavaConstant)}.
 237      */
 238     protected boolean isStaticFieldConstant(HotSpotResolvedJavaField staticField) {
 239         if (staticField.isFinal() || (staticField.isStable() && runtime.getConfig().foldStableValues)) {
 240             ResolvedJavaType holder = staticField.getDeclaringClass();
 241             if (holder.isInitialized() && !holder.getName().equals(SystemClassName)) {
 242                 return true;
 243             }
 244         }
 245         return false;
 246     }
 247 
 248     /**
 249      * Determines if a value read from a {@code final} instance field is considered constant. The
 250      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 251      * not the {@link JavaConstant#isDefaultForKind default value} for its kind or if
 252      * {@link #TrustFinalDefaultFields} is true.
 253      *
 254      * @param value a value read from a {@code final} instance field
 255      * @param receiverClass the {@link Object#getClass() class} of object from which the
 256      *            {@code value} was read
 257      */
 258     protected boolean isFinalInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 259         return !value.isDefaultForKind() || TrustFinalDefaultFields;
 260     }
 261 
 262     /**
 263      * Determines if a value read from a {@link Stable} instance field is considered constant. The
 264      * implementation in {@link HotSpotConstantReflectionProvider} returns true if {@code value} is
 265      * not the {@link JavaConstant#isDefaultForKind default value} for its kind.
 266      *
 267      * @param value a value read from a {@link Stable} field
 268      * @param receiverClass the {@link Object#getClass() class} of object from which the
 269      *            {@code value} was read
 270      */
 271     protected boolean isStableInstanceFieldValueConstant(JavaConstant value, Class<?> receiverClass) {
 272         return !value.isDefaultForKind();
 273     }
 274 
 275     public JavaConstant readConstantFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 276         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 277 
 278         if (hotspotField.isStatic()) {
 279             if (isStaticFieldConstant(hotspotField)) {
 280                 JavaConstant value = readFieldValue(field, receiver);
 281                 if (hotspotField.isFinal() || !value.isDefaultForKind()) {
 282                     return value;
 283                 }
 284             }
 285         } else {
 286             /*
 287              * for non-static final fields, we must assume that they are only initialized if they
 288              * have a non-default value.
 289              */
 290             Object object = receiver.isNull() ? null : ((HotSpotObjectConstantImpl) receiver).object();
 291 
 292             // Canonicalization may attempt to process an unsafe read before
 293             // processing a guard (e.g. a null check or a type check) for this read
 294             // so we need to check the object being read
 295             if (object != null) {
 296                 if (hotspotField.isFinal()) {
 297                     if (hotspotField.isInObject(object)) {
 298                         JavaConstant value = readFieldValue(field, receiver);
 299                         if (isFinalInstanceFieldValueConstant(value, object.getClass())) {
 300                             return value;
 301                         }
 302                     }
 303                 } else if (hotspotField.isStable() && runtime.getConfig().foldStableValues) {
 304                     if (hotspotField.isInObject(object)) {
 305                         JavaConstant value = readFieldValue(field, receiver);
 306                         if (isStableInstanceFieldValueConstant(value, object.getClass())) {
 307                             return value;
 308                         }
 309                     }
 310                 }
 311             }
 312         }
 313         return null;
 314     }
 315 
 316     public JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 317         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 318         if (!hotspotField.isStable()) {
 319             return readNonStableFieldValue(field, receiver);
 320         } else if (runtime.getConfig().foldStableValues) {
 321             return readStableFieldValue(field, receiver, hotspotField.isDefaultStable());
 322         } else {
 323             return null;
 324         }
 325     }
 326 
 327     private JavaConstant readNonStableFieldValue(ResolvedJavaField field, JavaConstant receiver) {
 328         HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field;
 329         if (hotspotField.isStatic()) {
 330             HotSpotResolvedJavaType holder = (HotSpotResolvedJavaType) hotspotField.getDeclaringClass();
 331             if (holder.isInitialized()) {
 332                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), HotSpotObjectConstantImpl.forObject(holder.mirror()), hotspotField.offset());
 333             }
 334         } else {
 335             if (receiver.isNonNull() && hotspotField.isInObject(((HotSpotObjectConstantImpl) receiver).object())) {
 336                 return memoryAccess.readUnsafeConstant(hotspotField.getJavaKind(), receiver, hotspotField.offset());
 337             }
 338         }
 339         return null;
 340     }
 341 
 342     public JavaConstant readStableFieldValue(ResolvedJavaField field, JavaConstant receiver, boolean isDefaultStable) {
 343         JavaConstant fieldValue = readNonStableFieldValue(field, receiver);
 344         if (fieldValue.isNonNull()) {
 345             JavaType declaredType = field.getType();
 346             if (declaredType.getComponentType() != null) {
 347                 int stableDimension = getArrayDimension(declaredType);
 348                 return HotSpotObjectConstantImpl.forStableArray(((HotSpotObjectConstantImpl) fieldValue).object(), stableDimension, isDefaultStable);
 349             }
 350         }
 351         return fieldValue;
 352     }
 353 
 354     private static int getArrayDimension(JavaType type) {
 355         int dimensions = 0;
 356         JavaType componentType = type;
 357         while ((componentType = componentType.getComponentType()) != null) {
 358             dimensions++;
 359         }
 360         return dimensions;
 361     }
 362 }