|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2025, 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.imagelayer; |
| 26 | + |
| 27 | +import com.oracle.graal.pointsto.meta.AnalysisMetaAccess; |
| 28 | +import org.graalvm.nativeimage.hosted.Feature; |
| 29 | + |
| 30 | +import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; |
| 31 | +import com.oracle.svm.core.feature.InternalFeature; |
| 32 | +import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport; |
| 33 | +import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl; |
| 34 | +import com.oracle.svm.util.ReflectionUtil; |
| 35 | + |
| 36 | +import jdk.internal.misc.Unsafe; |
| 37 | + |
| 38 | +@AutomaticallyRegisteredFeature |
| 39 | +public class InitialLayerFeature implements InternalFeature { |
| 40 | + @Override |
| 41 | + public boolean isInConfiguration(Feature.IsInConfigurationAccess access) { |
| 42 | + return ImageLayerBuildingSupport.buildingInitialLayer(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeAnalysis(BeforeAnalysisAccess a) { |
| 47 | + BeforeAnalysisAccessImpl access = (BeforeAnalysisAccessImpl) a; |
| 48 | + |
| 49 | + /* |
| 50 | + * Make sure that critical VM components are included in the base layer by registering |
| 51 | + * runtime APIs as entry points. Although the types below are part of java.base, so they |
| 52 | + * would anyway be included in every base layer created with module=java.base, this ensures |
| 53 | + * that the base layer is usable regardless of the class inclusion policy. |
| 54 | + */ |
| 55 | + String pinReason = "base layer entry point included unconditionally"; |
| 56 | + AnalysisMetaAccess metaAccess = access.getMetaAccess(); |
| 57 | + metaAccess.lookupJavaMethod(ReflectionUtil.lookupMethod(Unsafe.class, "getUnsafe")).setPinnedToInitialLayer(pinReason); |
| 58 | + metaAccess.lookupJavaMethod(ReflectionUtil.lookupMethod(Unsafe.class, "allocateInstance", Class.class)).setPinnedToInitialLayer(pinReason); |
| 59 | + metaAccess.lookupJavaMethod(ReflectionUtil.lookupMethod(Runtime.class, "getRuntime")).setPinnedToInitialLayer(pinReason); |
| 60 | + metaAccess.lookupJavaMethod(ReflectionUtil.lookupMethod(Runtime.class, "gc")).setPinnedToInitialLayer(pinReason); |
| 61 | + metaAccess.lookupJavaMethod(ReflectionUtil.lookupMethod(Class.class, "getResource", String.class)).setPinnedToInitialLayer(pinReason); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments