|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2022, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.hosted.classinitialization; |
| 26 | + |
| 27 | +import java.lang.reflect.Proxy; |
| 28 | + |
| 29 | +import org.graalvm.compiler.java.LambdaUtils; |
| 30 | + |
| 31 | +import com.oracle.graal.pointsto.meta.AnalysisMetaAccess; |
| 32 | +import com.oracle.graal.pointsto.meta.AnalysisUniverse; |
| 33 | +import com.oracle.svm.core.util.UserError; |
| 34 | +import com.oracle.svm.core.util.VMError; |
| 35 | +import com.oracle.svm.hosted.ImageClassLoader; |
| 36 | + |
| 37 | +import jdk.internal.misc.Unsafe; |
| 38 | +import jdk.vm.ci.meta.MetaAccessProvider; |
| 39 | + |
| 40 | +class AllowAllHostedUsagesClassInitializationSupport extends ClassInitializationSupport { |
| 41 | + |
| 42 | + AllowAllHostedUsagesClassInitializationSupport(MetaAccessProvider metaAccess, ImageClassLoader loader) { |
| 43 | + super(metaAccess, loader); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void initializeAtRunTime(Class<?> clazz, String reason) { |
| 48 | + UserError.guarantee(!configurationSealed, "The class initialization configuration can be changed only before the phase analysis."); |
| 49 | + classInitializationConfiguration.insert(clazz.getTypeName(), InitKind.RUN_TIME, reason, true); |
| 50 | + setSubclassesAsRunTime(clazz); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void rerunInitialization(Class<?> clazz, String reason) { |
| 55 | + UserError.guarantee(!configurationSealed, "The class initialization configuration can be changed only before the phase analysis."); |
| 56 | + classInitializationConfiguration.insert(clazz.getTypeName(), InitKind.RERUN, reason, true); |
| 57 | + |
| 58 | + try { |
| 59 | + Unsafe.getUnsafe().ensureClassInitialized(clazz); |
| 60 | + } catch (Throwable ex) { |
| 61 | + throw UserError.abort(ex, "Class initialization failed for %s. The class is requested for re-running (reason: %s)", clazz.getTypeName(), reason); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + String reasonForClass(Class<?> clazz) { |
| 67 | + InitKind initKind = classInitKinds.get(clazz); |
| 68 | + String reason = classInitializationConfiguration.lookupReason(clazz.getTypeName()); |
| 69 | + if (initKind.isRunTime()) { |
| 70 | + return "classes are initialized at run time by default"; |
| 71 | + } else if (reason != null) { |
| 72 | + return reason; |
| 73 | + } else { |
| 74 | + throw VMError.shouldNotReachHere("Must be either proven or specified"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void setSubclassesAsRunTime(Class<?> clazz) { |
| 79 | + if (clazz.isInterface() && !metaAccess.lookupJavaType(clazz).declaresDefaultMethods()) { |
| 80 | + /* |
| 81 | + * An interface that does not declare a default method is independent from a class |
| 82 | + * initialization point of view, i.e., it is not initialized when a class implementing |
| 83 | + * that interface is initialized. |
| 84 | + */ |
| 85 | + return; |
| 86 | + } |
| 87 | + loader.findSubclasses(clazz, false).stream() |
| 88 | + .filter(c -> !c.equals(clazz)) |
| 89 | + .filter(c -> !(c.isInterface() && !metaAccess.lookupJavaType(c).declaresDefaultMethods())) |
| 90 | + .forEach(c -> classInitializationConfiguration.insert(c.getTypeName(), InitKind.RUN_TIME, "subtype of " + clazz.getTypeName(), true)); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void forceInitializeHosted(Class<?> clazz, String reason, boolean allowInitializationErrors) { |
| 95 | + if (clazz == null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + classInitializationConfiguration.insert(clazz.getTypeName(), InitKind.BUILD_TIME, reason, true); |
| 99 | + InitKind initKind = ensureClassInitialized(clazz, allowInitializationErrors); |
| 100 | + classInitKinds.put(clazz, initKind); |
| 101 | + |
| 102 | + forceInitializeHosted(clazz.getSuperclass(), "super type of " + clazz.getTypeName(), allowInitializationErrors); |
| 103 | + forceInitializeInterfaces(clazz.getInterfaces(), "super type of " + clazz.getTypeName()); |
| 104 | + } |
| 105 | + |
| 106 | + private void forceInitializeInterfaces(Class<?>[] interfaces, String reason) { |
| 107 | + for (Class<?> iface : interfaces) { |
| 108 | + if (metaAccess.lookupJavaType(iface).declaresDefaultMethods()) { |
| 109 | + classInitializationConfiguration.insert(iface.getTypeName(), InitKind.BUILD_TIME, reason, true); |
| 110 | + |
| 111 | + ensureClassInitialized(iface, false); |
| 112 | + classInitKinds.put(iface, InitKind.BUILD_TIME); |
| 113 | + } |
| 114 | + forceInitializeInterfaces(iface.getInterfaces(), "super type of " + iface.getTypeName()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + boolean checkDelayedInitialization() { |
| 120 | + /* Nothing to check, all classes are allowed to be initialized in the image builder VM. */ |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + InitKind computeInitKindAndMaybeInitializeClass(Class<?> clazz) { |
| 126 | + return computeInitKindAndMaybeInitializeClass(clazz, true); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Computes the class initialization kind of the provided class, all superclasses, and all |
| 131 | + * interfaces that the provided class depends on (i.e., interfaces implemented by the provided |
| 132 | + * class that declare default methods). |
| 133 | + * |
| 134 | + * Also defines class initialization based on a policy of the subclass. |
| 135 | + */ |
| 136 | + InitKind computeInitKindAndMaybeInitializeClass(Class<?> clazz, boolean memoize) { |
| 137 | + InitKind existing = classInitKinds.get(clazz); |
| 138 | + if (existing != null) { |
| 139 | + return existing; |
| 140 | + } |
| 141 | + |
| 142 | + if (clazz.isPrimitive()) { |
| 143 | + forceInitializeHosted(clazz, "primitive types are initialized at build time", false); |
| 144 | + return InitKind.BUILD_TIME; |
| 145 | + } |
| 146 | + |
| 147 | + if (clazz.isArray()) { |
| 148 | + forceInitializeHosted(clazz, "arrays are initialized at build time", false); |
| 149 | + return InitKind.BUILD_TIME; |
| 150 | + } |
| 151 | + |
| 152 | + if (clazz.getTypeName().contains("$$StringConcat")) { |
| 153 | + forceInitializeHosted(clazz, "string concatenation classes are initialized at build time", false); |
| 154 | + return InitKind.BUILD_TIME; |
| 155 | + } |
| 156 | + |
| 157 | + InitKind specifiedInitKind = specifiedInitKindFor(clazz); |
| 158 | + InitKind clazzResult = specifiedInitKind != null ? specifiedInitKind : InitKind.RUN_TIME; |
| 159 | + |
| 160 | + InitKind superResult = InitKind.BUILD_TIME; |
| 161 | + if (clazz.getSuperclass() != null) { |
| 162 | + superResult = superResult.max(computeInitKindAndMaybeInitializeClass(clazz.getSuperclass(), memoize)); |
| 163 | + } |
| 164 | + superResult = superResult.max(processInterfaces(clazz, memoize)); |
| 165 | + |
| 166 | + if (superResult == InitKind.BUILD_TIME && (Proxy.isProxyClass(clazz) || LambdaUtils.isLambdaType(metaAccess.lookupJavaType(clazz)))) { |
| 167 | + forceInitializeHosted(clazz, "proxy/lambda classes with interfaces initialized at build time are also initialized at build time", false); |
| 168 | + return InitKind.BUILD_TIME; |
| 169 | + } |
| 170 | + |
| 171 | + InitKind result = superResult.max(clazzResult); |
| 172 | + |
| 173 | + if (memoize) { |
| 174 | + if (!result.isRunTime()) { |
| 175 | + result = result.max(ensureClassInitialized(clazz, false)); |
| 176 | + } |
| 177 | + |
| 178 | + /* |
| 179 | + * Unfortunately, the computation of canInitializeWithoutSideEffects is not completely |
| 180 | + * deterministic: Consider a class A whose class initializer depends on class B. Assume |
| 181 | + * class B has no other dependencies and can therefore be initialized at build time. |
| 182 | + * When class A is analyzed after class B has been initialized, it can also be |
| 183 | + * initialized at build time. But when class A is analyzed before class B has been |
| 184 | + * initialized, it cannot. Since two threads can analyze class A at the same time (there |
| 185 | + * is no per-class locking) and another thread can initialize B at the same time, we can |
| 186 | + * have a conflicting initialization status. In that case, BUILD_TIME must win over |
| 187 | + * RUN_TIME because one thread has already initialized class A. |
| 188 | + */ |
| 189 | + result = classInitKinds.merge(clazz, result, InitKind::min); |
| 190 | + } |
| 191 | + return result; |
| 192 | + } |
| 193 | + |
| 194 | + private InitKind processInterfaces(Class<?> clazz, boolean memoizeEager) { |
| 195 | + /* |
| 196 | + * Note that we do not call computeInitKindForClass(clazz) on purpose: if clazz is the root |
| 197 | + * class or an interface declaring default methods, then |
| 198 | + * computeInitKindAndMaybeInitializeClass() already calls computeInitKindForClass. If the |
| 199 | + * interface does not declare default methods, than we must not take the InitKind of that |
| 200 | + * interface into account, because interfaces without default methods are independent from a |
| 201 | + * class initialization point of view. |
| 202 | + */ |
| 203 | + InitKind result = InitKind.BUILD_TIME; |
| 204 | + |
| 205 | + for (Class<?> iface : clazz.getInterfaces()) { |
| 206 | + if (metaAccess.lookupJavaType(iface).declaresDefaultMethods()) { |
| 207 | + /* |
| 208 | + * An interface that declares default methods is initialized when a class |
| 209 | + * implementing it is initialized. So we need to inherit the InitKind from such an |
| 210 | + * interface. |
| 211 | + */ |
| 212 | + result = result.max(computeInitKindAndMaybeInitializeClass(iface, memoizeEager)); |
| 213 | + } else { |
| 214 | + /* |
| 215 | + * An interface that does not declare default methods is independent from a class |
| 216 | + * that implements it, i.e., the interface can still be uninitialized even when the |
| 217 | + * class is initialized. |
| 218 | + */ |
| 219 | + result = result.max(processInterfaces(iface, memoizeEager)); |
| 220 | + } |
| 221 | + } |
| 222 | + return result; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + void doLateInitialization(AnalysisUniverse aUniverse, AnalysisMetaAccess aMetaAccess) { |
| 227 | + /* Nothing for now. */ |
| 228 | + } |
| 229 | +} |
0 commit comments