|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2024, 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.core.layeredimagesingleton; |
| 26 | + |
| 27 | +import java.lang.reflect.Array; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | + |
| 30 | +import org.graalvm.nativeimage.hosted.Feature; |
| 31 | + |
| 32 | +import com.oracle.svm.core.ParsingReason; |
| 33 | +import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; |
| 34 | +import com.oracle.svm.core.feature.InternalFeature; |
| 35 | +import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport; |
| 36 | +import com.oracle.svm.core.util.VMError; |
| 37 | + |
| 38 | +import jdk.graal.compiler.nodes.ConstantNode; |
| 39 | +import jdk.graal.compiler.nodes.ValueNode; |
| 40 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
| 41 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
| 42 | +import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugin; |
| 43 | +import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugins; |
| 44 | +import jdk.graal.compiler.phases.util.Providers; |
| 45 | +import jdk.vm.ci.meta.JavaKind; |
| 46 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 47 | + |
| 48 | +/** |
| 49 | + * Adds support for layered image singleton features within traditional builds. |
| 50 | + */ |
| 51 | +@AutomaticallyRegisteredFeature |
| 52 | +public class NonLayeredImageSingletonFeature implements InternalFeature, FeatureSingleton { |
| 53 | + |
| 54 | + ConcurrentHashMap<Class<?>, Object> multiLayeredArrays = new ConcurrentHashMap<>(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean isInConfiguration(Feature.IsInConfigurationAccess access) { |
| 58 | + return !ImageLayerBuildingSupport.buildingImageLayer(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void registerInvocationPlugins(Providers providers, GraphBuilderConfiguration.Plugins plugins, ParsingReason reason) { |
| 63 | + InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins.getInvocationPlugins(), MultiLayeredImageSingleton.class); |
| 64 | + r.register(new InvocationPlugin.RequiredInvocationPlugin("getAllLayers", Class.class) { |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode classNode) { |
| 68 | + Class<?> key = b.getSnippetReflection().asObject(Class.class, classNode.asJavaConstant()); |
| 69 | + |
| 70 | + Object singleton = LayeredImageSingletonSupport.singleton().runtimeLookup(key); |
| 71 | + boolean conditions = singleton.getClass().equals(key) && |
| 72 | + singleton instanceof MultiLayeredImageSingleton multiLayerSingleton && |
| 73 | + multiLayerSingleton.getImageBuilderFlags().contains(LayeredImageSingletonBuilderFlags.RUNTIME_ACCESS); |
| 74 | + VMError.guarantee(conditions, "Illegal singleton %s", singleton); |
| 75 | + |
| 76 | + var multiLayeredArray = multiLayeredArrays.computeIfAbsent(key, k -> { |
| 77 | + var result = Array.newInstance(k, 1); |
| 78 | + Array.set(result, 0, singleton); |
| 79 | + return result; |
| 80 | + }); |
| 81 | + |
| 82 | + b.addPush(JavaKind.Object, ConstantNode.forConstant(b.getSnippetReflection().forObject(multiLayeredArray), b.getMetaAccess())); |
| 83 | + return true; |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
0 commit comments