From 777cb827c6ec1a6dff803ad29fa3057c8b646f13 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Tue, 7 Nov 2023 11:13:31 +0200 Subject: [PATCH] Use synchronized data structures for reachability handlers registration Prevent data races in reachability handlers registration when using `-H:-RunReachabilityHandlersConcurrently`. Closes https://github.com/oracle/graal/issues/5868 --- .../oracle/svm/hosted/ReachabilityHandlerFeature.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ReachabilityHandlerFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ReachabilityHandlerFeature.java index 21a5ee637191..e32b859e0c19 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ReachabilityHandlerFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ReachabilityHandlerFeature.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -44,6 +45,7 @@ import com.oracle.svm.core.SubstrateOptions; import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; import com.oracle.svm.core.feature.InternalFeature; +import com.oracle.svm.core.util.ConcurrentIdentityHashMap; import com.oracle.svm.core.util.UserError; import com.oracle.svm.core.util.VMError; import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl; @@ -52,8 +54,8 @@ @AutomaticallyRegisteredFeature public class ReachabilityHandlerFeature extends ReachabilityHandler implements InternalFeature { - private final IdentityHashMap> activeHandlers = new IdentityHashMap<>(); - private final IdentityHashMap>> triggeredHandlers = new IdentityHashMap<>(); + private final Map> activeHandlers = new ConcurrentIdentityHashMap<>(); + private final Map>> triggeredHandlers = new ConcurrentIdentityHashMap<>(); public static ReachabilityHandlerFeature singleton() { return ImageSingletons.lookup(ReachabilityHandlerFeature.class); @@ -88,7 +90,7 @@ private void registerReachabilityHandler(BeforeAnalysisAccess a, Object callback BeforeAnalysisAccessImpl access = (BeforeAnalysisAccessImpl) a; AnalysisMetaAccess metaAccess = access.getMetaAccess(); - Set triggerSet = activeHandlers.computeIfAbsent(callback, c -> new HashSet<>()); + var triggerSet = activeHandlers.computeIfAbsent(callback, c -> ConcurrentHashMap.newKeySet()); for (Object trigger : triggers) { if (trigger instanceof Class) { @@ -119,7 +121,7 @@ public void duringAnalysis(DuringAnalysisAccess a) { Set triggers = activeHandlers.get(callback); if (callback instanceof Consumer) { if (isTriggered(access, triggers)) { - triggeredHandlers.put(callback, null); + triggeredHandlers.put(callback, Map.of()); toExactCallback(callback).accept(access); completedCallbacks.add(callback); }