-
Couldn't load subscription status.
- Fork 6.1k
8359472: JVM crashes when attaching a dynamic agent before JVMTI_PHASE_LIVE #27766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+167
−0
Closed
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
b7166c7
fix and test
fandreuz 29f1a25
nl
fandreuz b821ba3
cc
fandreuz 001e2f6
macro
fandreuz 96e2340
fix check
fandreuz cdd5a22
mv
fandreuz e3df527
nn
fandreuz 941f9a6
mv
fandreuz 49722e1
summary
fandreuz 84f168b
Update src/hotspot/share/prims/jvmtiAgentList.cpp
fandreuz 2e21bc3
nn
fandreuz e9646e6
errno
fandreuz cbbbf8a
ops
fandreuz b53c2da
debug
fandreuz 2a7ab98
replace
fandreuz 7870ca7
revert
fandreuz 1363424
check jcmd exist
fandreuz bc998af
wip
fandreuz e0b879b
revert
fandreuz 4269dee
quote
fandreuz 8bbedbc
backslash
fandreuz 84fa5ac
debug
fandreuz 5b98640
revert
fandreuz 4dddb5a
mv to attach
fandreuz 4fa7d72
simplfiy
fandreuz 4fd6e07
cc
fandreuz 1af09f8
c++
fandreuz 93667b8
msg
fandreuz 2e628c6
nullptr
fandreuz 30b2cf9
fix tool call
fandreuz bd4b98a
PidJcmdExecutor. unused import. cc
fandreuz 9d51f08
rephrase
fandreuz 206fd08
close
fandreuz a401f0a
indent
fandreuz 925f9fc
jvmti errors
fandreuz 60d6fdf
empty stderr
fandreuz ba3dc02
cc
fandreuz 2059679
return value
fandreuz 462f982
Merge branch 'master' into JDK-8359472
fandreuz 1dafec3
rename
fandreuz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
test/hotspot/jtreg/serviceability/attach/EarlyDynamicLoad/EarlyDynamicLoad.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| import com.sun.tools.attach.VirtualMachine; | ||
| import com.sun.tools.attach.AgentLoadException; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.AfterAll; | ||
|
|
||
| import java.io.File; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.concurrent.TimeUnit; | ||
| import jdk.test.lib.dcmd.PidJcmdExecutor; | ||
| import jdk.test.lib.process.OutputAnalyzer; | ||
| import jdk.test.lib.process.ProcessTools; | ||
| import jdk.test.lib.Utils; | ||
|
|
||
| /* | ||
| * @test EarlyDynamicLoad | ||
| * @summary Test that dynamic attach fails gracefully when the JVM is not in live phase. | ||
| * @requires vm.jvmti | ||
| * @library /test/lib | ||
| * @run junit EarlyDynamicLoad | ||
| */ | ||
| public class EarlyDynamicLoad { | ||
| private static final String EXPECTED_MESSAGE = "Dynamic agent loading is only permitted in the live phase"; | ||
|
|
||
| private static Process child; | ||
|
|
||
| @BeforeAll | ||
| static void startAndWaitChild() throws Exception { | ||
| child = ProcessTools.createTestJavaProcessBuilder( | ||
| "-XX:+StartAttachListener", | ||
| "-agentpath:" + Utils.TEST_NATIVE_PATH + File.separator + System.mapLibraryName("EarlyDynamicLoad"), | ||
| "--version").start(); | ||
|
|
||
| // Wait until the process enters VMStartCallback | ||
| try (InputStream is = child.getInputStream()) { | ||
| is.read(); | ||
| } | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopChild() throws Exception { | ||
| try (OutputStream os = child.getOutputStream()) { | ||
| os.write(0); | ||
| } | ||
|
|
||
| if (!child.waitFor(5, TimeUnit.SECONDS)) { | ||
| child.destroyForcibly(); | ||
| throw new AssertionError("Timed out while waiting child process to complete"); | ||
| } | ||
|
|
||
| OutputAnalyzer analyzer = new OutputAnalyzer(child); | ||
| analyzer.shouldHaveExitValue(0); | ||
| analyzer.stderrShouldBeEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void virtualMachine() throws Exception { | ||
| try { | ||
| VirtualMachine vm = VirtualMachine.attach(String.valueOf(child.pid())); | ||
| vm.loadAgent("some.jar"); | ||
| vm.detach(); | ||
| throw new AssertionError("Should have failed with AgentLoadException"); | ||
| } catch(AgentLoadException exception) { | ||
| if (!exception.getMessage().contains(EXPECTED_MESSAGE)) { | ||
| throw new AssertionError("Unexpected error message", exception); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void jcmd() throws Exception { | ||
| PidJcmdExecutor executor = new PidJcmdExecutor(String.valueOf(child.pid())); | ||
| OutputAnalyzer out = executor.execute("JVMTI.agent_load some.jar"); | ||
|
|
||
| out.shouldHaveExitValue(0); | ||
| out.stdoutShouldContain(EXPECTED_MESSAGE); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
test/hotspot/jtreg/serviceability/attach/EarlyDynamicLoad/libEarlyDynamicLoad.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| #include <jvmti.h> | ||
| #include <cstdio> | ||
| #include <cstring> | ||
|
|
||
| extern "C" { | ||
|
|
||
| static void JNICALL VMStartCallback(jvmtiEnv* jvmti, JNIEnv* env) { | ||
| putchar('1'); | ||
| fflush(stdout); | ||
| getchar(); | ||
| } | ||
|
|
||
| JNIEXPORT int Agent_OnLoad(JavaVM* vm, char* options, void* reserved) { | ||
| jvmtiEnv* jvmti; | ||
| if (vm->GetEnv((void**) &jvmti, JVMTI_VERSION_1_0) != JVMTI_ERROR_NONE) { | ||
| fprintf(stderr, "JVMTI error occurred during GetEnv\n"); | ||
| return JNI_ERR; | ||
| } | ||
|
|
||
| jvmtiEventCallbacks callbacks; | ||
| memset(&callbacks, 0, sizeof(callbacks)); | ||
| callbacks.VMStart = VMStartCallback; | ||
|
|
||
| if (jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)) != JVMTI_ERROR_NONE) { | ||
| fprintf(stderr, "JVMTI error occurred during SetEventCallbacks\n"); | ||
| return JNI_ERR; | ||
| } | ||
| if (jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_START, nullptr) != JVMTI_ERROR_NONE) { | ||
| fprintf(stderr, "JVMTI error occurred during SetEventNotificationMode\n"); | ||
| return JNI_ERR; | ||
| } | ||
fandreuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return JNI_OK; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.