Skip to content

Commit 4b15349

Browse files
author
Serguei Spitsyn
committed
8304438: jcmd JVMTI.agent_load should obey EnableDynamicAgentLoading
Reviewed-by: cjplummer, alanb, amenkov
1 parent b3c9d67 commit 4b15349

File tree

4 files changed

+147
-4
lines changed

4 files changed

+147
-4
lines changed

src/hotspot/share/prims/jvmtiAgent.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ extern "C" {
488488
// Loading the agent by invoking Agent_OnAttach.
489489
// This function is called before the agent is added to JvmtiAgentList.
490490
static bool invoke_Agent_OnAttach(JvmtiAgent* agent, outputStream* st) {
491+
if (!EnableDynamicAgentLoading) {
492+
st->print_cr("Dynamic agent loading is not enabled. "
493+
"Use -XX:+EnableDynamicAgentLoading to launch target VM.");
494+
return false;
495+
}
491496
DEBUG_ONLY(assert_preload(agent);)
492497
assert(agent->is_dynamic(), "invariant");
493498
assert(st != nullptr, "invariant");

src/hotspot/share/services/attachListener.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,6 @@ static void attach_listener_thread_entry(JavaThread* thread, TRAPS) {
402402
// handle special detachall operation
403403
if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {
404404
AttachListener::detachall();
405-
} else if (!EnableDynamicAgentLoading && strcmp(op->name(), "load") == 0) {
406-
st.print("Dynamic agent loading is not enabled. "
407-
"Use -XX:+EnableDynamicAgentLoading to launch target VM.");
408-
res = JNI_ERR;
409405
} else {
410406
// find the function to dispatch too
411407
AttachOperationFunctionInfo* info = nullptr;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* A no-op Java agent.
26+
*/
27+
public class JavaAgent {
28+
public static void agentmain(String args) {
29+
}
30+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2023, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import static jdk.test.lib.Asserts.*;
25+
26+
import java.io.IOException;
27+
import java.lang.management.ManagementFactory;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.jar.Attributes;
32+
import java.util.jar.Manifest;
33+
import java.util.List;
34+
35+
import com.sun.management.HotSpotDiagnosticMXBean;
36+
import com.sun.management.VMOption;
37+
import jdk.test.lib.process.OutputAnalyzer;
38+
import jdk.test.lib.Utils;
39+
import jdk.test.lib.util.JarUtils;
40+
41+
/*
42+
* @test id=default
43+
* @bug 8304438
44+
* @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (by default)
45+
* @modules jdk.attach jdk.jcmd
46+
* @library /test/lib
47+
* @build JavaAgent
48+
* @run main/othervm TestJcmdNoAgentLoad
49+
*/
50+
51+
/*
52+
* @test id=disabled
53+
* @bug 8304438
54+
* @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (disabled)
55+
* @modules jdk.attach jdk.jcmd
56+
* @library /test/lib
57+
* @build JavaAgent
58+
* @run main/othervm -XX:-EnableDynamicAgentLoading TestJcmdNoAgentLoad
59+
*/
60+
61+
/*
62+
* @test id=enabled
63+
* @bug 8304438
64+
* @summary JVMTI.agent_load should obey EnableDynamicAgentLoading (enabled)
65+
* @modules jdk.attach jdk.jcmd
66+
* @library /test/lib
67+
* @build JavaAgent
68+
* @run main/othervm -XX:+EnableDynamicAgentLoading TestJcmdNoAgentLoad
69+
*/
70+
71+
72+
public class TestJcmdNoAgentLoad {
73+
private static final String PTRN = "Dynamic agent loading is not enabled";
74+
private static boolean dynamicLoadingEnabled = true;
75+
private static final String TEST_CLASSES = System.getProperty("test.classes");
76+
private static String javaAgent;
77+
78+
static {
79+
// get VM option EnableDynamicAgentLoading value
80+
HotSpotDiagnosticMXBean bean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
81+
VMOption dynamicLoadingEnabledOpt = bean.getVMOption("EnableDynamicAgentLoading");
82+
dynamicLoadingEnabled = dynamicLoadingEnabledOpt.getValue().equals("true");
83+
}
84+
85+
public static void main(String[] args) throws Exception {
86+
setup();
87+
testNoAgentLoad(new String[] { "JVMTI.agent_load", javaAgent });
88+
}
89+
90+
private static void setup() throws Exception {
91+
// create JAR file with Java agent
92+
Manifest man = new Manifest();
93+
Attributes attrs = man.getMainAttributes();
94+
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
95+
attrs.put(new Attributes.Name("Agent-Class"), "JavaAgent");
96+
Path jarfile = Path.of("javaagent.jar");
97+
Path classes = Path.of(TEST_CLASSES);
98+
JarUtils.createJarFile(jarfile, man, classes, Path.of("JavaAgent.class"));
99+
javaAgent = jarfile.toString();
100+
}
101+
102+
private static void testNoAgentLoad(String... jcmdArgs) throws Exception {
103+
OutputAnalyzer output = JcmdBase.jcmd(jcmdArgs);
104+
105+
output.shouldHaveExitValue(0);
106+
if (dynamicLoadingEnabled) {
107+
output.shouldNotContain(PTRN);
108+
} else {
109+
output.shouldContain(PTRN);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)