From 82433dfe91c484038413ddf96803a3bc6dd685b7 Mon Sep 17 00:00:00 2001 From: Felix Berlakovich Date: Thu, 6 Nov 2025 17:19:51 +0100 Subject: [PATCH] Fix DebugContext handling in ProfileReplaySupport --- .../hotspot/test/ProfileReplayTest.java | 33 +++++++++++++++++-- .../hotspot/ProfileReplaySupport.java | 5 +-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/ProfileReplayTest.java b/compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/ProfileReplayTest.java index d2d8d8c85b9b..0a845b085fb2 100644 --- a/compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/ProfileReplayTest.java +++ b/compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/ProfileReplayTest.java @@ -25,17 +25,21 @@ package jdk.graal.compiler.hotspot.test; import java.io.IOException; +import java.nio.file.AccessDeniedException; + +import org.junit.Assert; +import org.junit.Test; import jdk.graal.compiler.api.directives.GraalDirectives; import jdk.graal.compiler.core.test.GraalCompilerTest; +import jdk.graal.compiler.debug.DebugCloseable; +import jdk.graal.compiler.debug.DebugContext; import jdk.graal.compiler.debug.DebugOptions; import jdk.graal.compiler.hotspot.CompilationTask; import jdk.graal.compiler.hotspot.HotSpotGraalCompiler; import jdk.graal.compiler.hotspot.ProfileReplaySupport; +import jdk.graal.compiler.nodes.spi.StableProfileProvider; import jdk.graal.compiler.options.OptionValues; -import org.junit.Assert; -import org.junit.Test; - import jdk.vm.ci.code.InstalledCode; import jdk.vm.ci.hotspot.HotSpotCompilationRequest; import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult; @@ -116,6 +120,29 @@ public void testProfileReplay() throws IOException { } } + @Test + @SuppressWarnings("try") + public void testFailingProfileReplay() throws IOException { + final ResolvedJavaMethod method = getResolvedJavaMethod("foo"); + try (TemporaryDirectory temp = new TemporaryDirectory("ProfileReplayTest")) { + OptionValues overrides = new OptionValues(getInitialOptions(), DebugOptions.DumpPath, temp.toString(), ProfileReplaySupport.Options.SaveProfiles, true); + try (DebugContext debug = getDebugContext(overrides)) { + + // make sure the ProfileReplay fails when trying to save the profiles + temp.path.toFile().setWritable(false); + ProfileReplaySupport profileReplay = ProfileReplaySupport.profileReplayPrologue(debug, 0, method, new StableProfileProvider(), null); + + try (DebugContext.Scope scope = debug.scope("TestScope"); DebugCloseable closable = debug.disableIntercept()) { + /* + * we haven't compiled anything, we just want ProfileReplay to save the profiles + * (and fail) + */ + Assert.assertThrows(AccessDeniedException.class, () -> profileReplay.profileReplayEpilogue(debug, null, null, new StableProfileProvider(), getCompilationId(method), 0, method)); + } + } + } + } + private static void runInitialCompilation(ResolvedJavaMethod m, OptionValues rootOptions, HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalCompiler compiler) { HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) m, -1, 0L); OptionValues opt = new OptionValues(rootOptions, ProfileReplaySupport.Options.StrictProfiles, true, ProfileReplaySupport.Options.SaveProfiles, true); diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/ProfileReplaySupport.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/ProfileReplaySupport.java index 7af15f94b762..8e199210c28b 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/ProfileReplaySupport.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/ProfileReplaySupport.java @@ -226,13 +226,14 @@ public String formatLamdaName(ResolvedJavaMethod m) { * {@link Options#WarnAboutGraphSignatureMismatch}). If {@link Options#SaveProfiles} is set, the * method additionally saves the previously collected profiles to the given profile path. */ + @SuppressWarnings("try") public void profileReplayEpilogue(DebugContext debug, CompilationResult result, StructuredGraph graph, StableProfileProvider profileProvider, CompilationIdentifier compilationId, int entryBCI, ResolvedJavaMethod method) { if ((SaveProfiles.getValue(debug.getOptions()) || LoadProfiles.getValue(debug.getOptions()) != null) && profileFilter.matches(method)) { String codeSignature = null; String graphSignature = null; if (result != null) { - try { + try (DebugContext.Scope scope = debug.scope("ProfileReplay")) { codeSignature = result.getCodeSignature(); assert graph != null; String s = getCanonicalGraphString(graph); @@ -252,7 +253,7 @@ public void profileReplayEpilogue(DebugContext debug, CompilationResult result, } } if (SaveProfiles.getValue(debug.getOptions())) { - try { + try (DebugContext.Scope scope = debug.scope("ProfileReplay")) { EconomicMap map = EconomicMap.create(); map.put("identifier", compilationId.toString()); map.put("method", method.format("%H.%n(%P)%R"));