|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 2026, 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.heap; |
| 26 | + |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | + |
| 30 | +import org.graalvm.nativeimage.ImageSingletons; |
| 31 | + |
| 32 | +import com.oracle.graal.pointsto.ObjectScanner.OtherReason; |
| 33 | +import com.oracle.graal.pointsto.ObjectScanner.ScanReason; |
| 34 | +import com.oracle.svm.core.BuildPhaseProvider; |
| 35 | +import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport; |
| 36 | +import com.oracle.svm.core.util.ImageHeapList.HostedImageHeapList; |
| 37 | +import com.oracle.svm.core.util.ImageHeapMap.HostedImageHeapMap; |
| 38 | +import com.oracle.svm.core.util.LayeredHostedImageHeapMapCollector; |
| 39 | +import com.oracle.svm.core.util.LayeredImageHeapMap; |
| 40 | +import com.oracle.svm.hosted.FeatureImpl.DuringAnalysisAccessImpl; |
| 41 | +import com.oracle.svm.shared.singletons.traits.BuiltinTraits.BuildtimeAccessOnly; |
| 42 | +import com.oracle.svm.shared.singletons.traits.BuiltinTraits.NoLayeredCallbacks; |
| 43 | +import com.oracle.svm.shared.singletons.traits.SingletonTraits; |
| 44 | +import com.oracle.svm.shared.util.VMError; |
| 45 | + |
| 46 | +/** |
| 47 | + * Build-time state holder for hosted image-heap collections that need to be rewritten to their |
| 48 | + * runtime counterparts and tracked for later rescans. The feature installs this support object as |
| 49 | + * the owner of the object replacer so all collection state captured during setup, analysis, and |
| 50 | + * image writing stays in one place. |
| 51 | + */ |
| 52 | +@SingletonTraits(access = BuildtimeAccessOnly.class, layeredCallbacks = NoLayeredCallbacks.class) |
| 53 | +final class ImageHeapCollectionSupport { |
| 54 | + private final Set<HostedImageHeapMap<?, ?>> allMaps = ConcurrentHashMap.newKeySet(); |
| 55 | + private final Set<HostedImageHeapList<?>> allLists = ConcurrentHashMap.newKeySet(); |
| 56 | + private final ScanReason scanReason = new OtherReason("Manual rescan triggered from " + ImageHeapCollectionSupport.class); |
| 57 | + |
| 58 | + static ImageHeapCollectionSupport singleton() { |
| 59 | + return ImageSingletons.lookup(ImageHeapCollectionSupport.class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Rewrites hosted image-heap collections to their runtime storage objects and records the |
| 64 | + * hosted collections that need later update propagation. During analysis, first reachability of |
| 65 | + * a collection registers it for subsequent update/rescan handling; after analysis, encountering |
| 66 | + * a collection that was never captured is treated as an invariant violation. |
| 67 | + */ |
| 68 | + Object replaceHostedWithRuntime(Object obj) { |
| 69 | + if (obj instanceof HostedImageHeapMap) { |
| 70 | + HostedImageHeapMap<?, ?> hostedImageHeapMap = (HostedImageHeapMap<?, ?>) obj; |
| 71 | + if (BuildPhaseProvider.isAnalysisFinished()) { |
| 72 | + VMError.guarantee(allMaps.contains(hostedImageHeapMap), "ImageHeapMap reachable after analysis that was not seen during analysis"); |
| 73 | + } else { |
| 74 | + allMaps.add(hostedImageHeapMap); |
| 75 | + if (hostedImageHeapMap.getRuntimeMap() instanceof LayeredImageHeapMap<Object, Object> layeredImageHeapMap) { |
| 76 | + LayeredHostedImageHeapMapCollector.singleton().registerReachableHostedImageHeapMap(layeredImageHeapMap); |
| 77 | + } |
| 78 | + } |
| 79 | + return hostedImageHeapMap.getRuntimeMap(); |
| 80 | + } else if (obj instanceof HostedImageHeapList<?> hostedImageHeapList) { |
| 81 | + if (BuildPhaseProvider.isAnalysisFinished()) { |
| 82 | + VMError.guarantee(allLists.contains(hostedImageHeapList), "ImageHeapList reachable after analysis that was not seen during analysis"); |
| 83 | + } else { |
| 84 | + allLists.add(hostedImageHeapList); |
| 85 | + } |
| 86 | + return hostedImageHeapList.getRuntimeList(); |
| 87 | + } |
| 88 | + return obj; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Makes sure that the content of all modified |
| 93 | + * {@link com.oracle.svm.core.util.ImageHeapMap.HostedImageHeapMap}s and |
| 94 | + * {@link com.oracle.svm.core.util.ImageHeapList.HostedImageHeapList}s is properly propagated to |
| 95 | + * their runtime counterparts. As both the number of these collections and their individual |
| 96 | + * sizes are theoretically unbounded, this method uses <i>parallel streams</i> to divide the |
| 97 | + * load across all cores. |
| 98 | + * <p> |
| 99 | + * The process is split into two stages. First, the content of each modified collection is |
| 100 | + * propagated from the hosted to the runtime version. Then, the modified runtime collections are |
| 101 | + * rescanned. The split prevents concurrent modifications of the hosted collections during the |
| 102 | + * execution of this method, as they may be updated indirectly during the heap scanning. |
| 103 | + */ |
| 104 | + void updateAndRescanCollections(DuringAnalysisAccessImpl access) { |
| 105 | + if (ImageLayerBuildingSupport.buildingExtensionLayer()) { |
| 106 | + allMaps.addAll(LayeredHostedImageHeapMapCollector.singleton().getPreviousLayerReachableMaps()); |
| 107 | + } |
| 108 | + Set<Object> objectsToRescan = ConcurrentHashMap.newKeySet(); |
| 109 | + allMaps.parallelStream().forEach(hostedImageHeapMap -> { |
| 110 | + if (hostedImageHeapMap.needsUpdate()) { |
| 111 | + hostedImageHeapMap.update(); |
| 112 | + objectsToRescan.add(hostedImageHeapMap.getCurrentLayerMap()); |
| 113 | + } |
| 114 | + }); |
| 115 | + allLists.parallelStream().forEach(hostedImageHeapList -> { |
| 116 | + if (hostedImageHeapList.needsUpdate()) { |
| 117 | + hostedImageHeapList.update(); |
| 118 | + objectsToRescan.add(hostedImageHeapList.getRuntimeList()); |
| 119 | + } |
| 120 | + }); |
| 121 | + if (!objectsToRescan.isEmpty()) { |
| 122 | + objectsToRescan.parallelStream().forEach(obj -> access.rescanObject(obj, scanReason)); |
| 123 | + access.requireAnalysisIteration(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + boolean needsUpdate() { |
| 128 | + for (var hostedImageHeapMap : allMaps) { |
| 129 | + if (hostedImageHeapMap.needsUpdate()) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + for (var hostedImageHeapList : allLists) { |
| 134 | + if (hostedImageHeapList.needsUpdate()) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + void verifyCollectionsAreStableAfterAnalysis() { |
| 142 | + for (var hostedImageHeapMap : allMaps) { |
| 143 | + if (hostedImageHeapMap.needsUpdate()) { |
| 144 | + throw VMError.shouldNotReachHere("ImageHeapMap modified after static analysis:%n%s%n%s", |
| 145 | + hostedImageHeapMap, hostedImageHeapMap.getCurrentLayerMap()); |
| 146 | + } |
| 147 | + } |
| 148 | + for (var hostedImageHeapList : allLists) { |
| 149 | + if (hostedImageHeapList.needsUpdate()) { |
| 150 | + throw VMError.shouldNotReachHere("ImageHeapList modified after static analysis:%n%s%n%s", |
| 151 | + hostedImageHeapList, hostedImageHeapList.getRuntimeList()); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments