|
| 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.graal.pointsto.meta; |
| 26 | + |
| 27 | +import java.lang.reflect.Executable; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | +import java.util.function.BiConsumer; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess; |
| 35 | + |
| 36 | +import com.oracle.graal.pointsto.PointsToAnalysis; |
| 37 | + |
| 38 | +public abstract class AnalysisElement { |
| 39 | + |
| 40 | + /** |
| 41 | + * Contains reachability handlers that are notified when the element is marked as reachable. |
| 42 | + * Each handler is notified only once, and then it is removed from the set. |
| 43 | + */ |
| 44 | + private final Set<ElementReachableNotification> elementReachableNotifications = ConcurrentHashMap.newKeySet(); |
| 45 | + |
| 46 | + public void registerReachabilityNotification(ElementReachableNotification notification) { |
| 47 | + elementReachableNotifications.add(notification); |
| 48 | + } |
| 49 | + |
| 50 | + protected void notifyReachabilityCallbacks(AnalysisUniverse universe) { |
| 51 | + elementReachableNotifications.forEach(c -> c.notifyCallback(universe, this)); |
| 52 | + elementReachableNotifications.removeIf(ElementReachableNotification::isNotified); |
| 53 | + } |
| 54 | + |
| 55 | + public abstract boolean isReachable(); |
| 56 | + |
| 57 | + protected abstract void onReachable(); |
| 58 | + |
| 59 | + /** Return true if reachability handlers should be executed for this element. */ |
| 60 | + public boolean isTriggered() { |
| 61 | + return isReachable(); |
| 62 | + } |
| 63 | + |
| 64 | + public static final class ElementReachableNotification { |
| 65 | + |
| 66 | + private final Consumer<DuringAnalysisAccess> callback; |
| 67 | + private final AtomicBoolean notified = new AtomicBoolean(); |
| 68 | + |
| 69 | + public ElementReachableNotification(Consumer<DuringAnalysisAccess> callback) { |
| 70 | + this.callback = callback; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isNotified() { |
| 74 | + return notified.get(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Notify the callback exactly once. Note that this callback can be shared by multiple |
| 79 | + * triggers, the one that triggers it is passed into triggeredElement for debugging. |
| 80 | + */ |
| 81 | + public void notifyCallback(AnalysisUniverse universe, AnalysisElement triggeredElement) { |
| 82 | + assert triggeredElement.isTriggered(); |
| 83 | + if (!notified.getAndSet(true)) { |
| 84 | + execute(universe, () -> callback.accept(universe.getConcurrentAnalysisAccess())); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static final class SubtypeReachableNotification { |
| 90 | + private final BiConsumer<DuringAnalysisAccess, Class<?>> callback; |
| 91 | + private final Set<AnalysisType> seenSubtypes = ConcurrentHashMap.newKeySet(); |
| 92 | + |
| 93 | + public SubtypeReachableNotification(BiConsumer<DuringAnalysisAccess, Class<?>> callback) { |
| 94 | + this.callback = callback; |
| 95 | + } |
| 96 | + |
| 97 | + /** Notify the callback exactly once for each reachable subtype. */ |
| 98 | + public void notifyCallback(AnalysisUniverse universe, AnalysisType reachableSubtype) { |
| 99 | + assert reachableSubtype.isReachable(); |
| 100 | + if (seenSubtypes.add(reachableSubtype)) { |
| 101 | + execute(universe, () -> callback.accept(universe.getConcurrentAnalysisAccess(), reachableSubtype.getJavaClass())); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static final class MethodOverrideReachableNotification { |
| 107 | + private final BiConsumer<DuringAnalysisAccess, Executable> callback; |
| 108 | + private final Set<AnalysisMethod> seenOverride = ConcurrentHashMap.newKeySet(); |
| 109 | + |
| 110 | + public MethodOverrideReachableNotification(BiConsumer<DuringAnalysisAccess, Executable> callback) { |
| 111 | + this.callback = callback; |
| 112 | + } |
| 113 | + |
| 114 | + /** Notify the callback exactly once for each reachable method override. */ |
| 115 | + public void notifyCallback(AnalysisUniverse universe, AnalysisMethod reachableOverride) { |
| 116 | + assert reachableOverride.isReachable(); |
| 117 | + if (seenOverride.add(reachableOverride)) { |
| 118 | + execute(universe, () -> callback.accept(universe.getConcurrentAnalysisAccess(), reachableOverride.getJavaMethod())); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void execute(AnalysisUniverse universe, Runnable task) { |
| 124 | + /* |
| 125 | + * Post the tasks to the analysis executor. This ensures that even for elements registered |
| 126 | + * as reachable early, before the analysis is started, the reachability callbacks are run |
| 127 | + * during the analysis. |
| 128 | + */ |
| 129 | + ((PointsToAnalysis) universe.getBigbang()).postTask(task); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments