Skip to content

Commit 662f3bd

Browse files
parttimenerdGoeLin
authored andcommitted
8317920: JDWP-agent sends broken exception event with onthrow option
Reviewed-by: phh
1 parent 68807aa commit 662f3bd

File tree

4 files changed

+255
-20
lines changed

4 files changed

+255
-20
lines changed

src/jdk.jdwp.agent/share/native/libjdwp/debugInit.c

+27-17
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void JNICALL cbEarlyVMDeath(jvmtiEnv*, JNIEnv *);
103103
static void JNICALL cbEarlyException(jvmtiEnv*, JNIEnv *,
104104
jthread, jmethodID, jlocation, jobject, jmethodID, jlocation);
105105

106-
static void initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei);
106+
static void initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei, EventInfo *opt_info);
107107
static jboolean parseOptions(char *str);
108108

109109
/*
@@ -439,7 +439,7 @@ cbEarlyVMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread)
439439
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at VM_INIT time");
440440
}
441441
if (initOnStartup)
442-
initialize(env, thread, EI_VM_INIT);
442+
initialize(env, thread, EI_VM_INIT, NULL);
443443
vmInitialized = JNI_TRUE;
444444
LOG_MISC(("END cbEarlyVMInit"));
445445
}
@@ -492,6 +492,16 @@ cbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env,
492492
LOG_MISC(("VM is not initialized yet"));
493493
return;
494494
}
495+
EventInfo info;
496+
info.ei = EI_EXCEPTION;
497+
info.thread = thread;
498+
info.clazz = getMethodClass(jvmti_env, method);
499+
info.method = method;
500+
info.location = location;
501+
info.object = exception;
502+
info.u.exception.catch_clazz = getMethodClass(jvmti_env, catch_method);
503+
info.u.exception.catch_method = catch_method;
504+
info.u.exception.catch_location = catch_location;
495505

496506
/*
497507
* We want to preserve any current exception that might get wiped
@@ -506,24 +516,22 @@ cbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env,
506516
if (initOnUncaught && catch_method == NULL) {
507517

508518
LOG_MISC(("Initializing on uncaught exception"));
509-
initialize(env, thread, EI_EXCEPTION);
519+
initialize(env, thread, EI_EXCEPTION, &info);
510520

511521
} else if (initOnException != NULL) {
512522

513-
jclass clazz;
514-
515-
/* Get class of exception thrown */
516-
clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, exception);
517-
if ( clazz != NULL ) {
523+
jclass exception_clazz = JNI_FUNC_PTR(env, GetObjectClass)(env, exception);
524+
/* check class of exception thrown */
525+
if ( exception_clazz != NULL ) {
518526
char *signature = NULL;
519527
/* initing on throw, check */
520-
error = classSignature(clazz, &signature, NULL);
528+
error = classSignature(exception_clazz, &signature, NULL);
521529
LOG_MISC(("Checking specific exception: looking for %s, got %s",
522530
initOnException, signature));
523531
if ( (error==JVMTI_ERROR_NONE) &&
524532
(strcmp(signature, initOnException) == 0)) {
525533
LOG_MISC(("Initializing on specific exception"));
526-
initialize(env, thread, EI_EXCEPTION);
534+
initialize(env, thread, EI_EXCEPTION, &info);
527535
} else {
528536
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */
529537
}
@@ -664,9 +672,11 @@ jniFatalError(JNIEnv *env, const char *msg, jvmtiError error, int exit_code)
664672

665673
/*
666674
* Initialize debugger back end modules
675+
*
676+
* @param opt_info optional event info to use, might be null
667677
*/
668678
static void
669-
initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei)
679+
initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei, EventInfo *opt_info)
670680
{
671681
jvmtiError error;
672682
EnumerateArg arg;
@@ -754,13 +764,13 @@ initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei)
754764
* can get in the queue (from other not-yet-suspended threads)
755765
* before this one does. (Also need to handle allocation error below?)
756766
*/
757-
EventInfo info;
758767
struct bag *initEventBag;
759-
LOG_MISC(("triggering_ei != EI_VM_INIT"));
768+
LOG_MISC(("triggering_ei == EI_EXCEPTION"));
769+
JDI_ASSERT(triggering_ei == EI_EXCEPTION);
770+
JDI_ASSERT(opt_info != NULL);
760771
initEventBag = eventHelper_createEventBag();
761-
(void)memset(&info,0,sizeof(info));
762-
info.ei = triggering_ei;
763-
eventHelper_recordEvent(&info, 0, suspendPolicy, initEventBag);
772+
threadControl_onEventHandlerEntry(currentSessionID, opt_info->ei, thread, NULL);
773+
eventHelper_recordEvent(opt_info, 0, suspendPolicy, initEventBag);
764774
(void)eventHelper_reportEvents(currentSessionID, initEventBag);
765775
bagDestroyBag(initEventBag);
766776
}
@@ -1395,7 +1405,7 @@ JNIEXPORT char const* JNICALL debugInit_startDebuggingViaCommand(JNIEnv* env, jt
13951405
if (!startedViaJcmd) {
13961406
startedViaJcmd = JNI_TRUE;
13971407
is_first_start = JNI_TRUE;
1398-
initialize(env, thread, EI_VM_INIT);
1408+
initialize(env, thread, EI_VM_INIT, NULL);
13991409
}
14001410

14011411
bagEnumerateOver(transports, getFirstTransport, &spec);
+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2023 SAP SE. 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 com.sun.jdi.Bootstrap;
25+
import com.sun.jdi.VirtualMachine;
26+
import com.sun.jdi.connect.AttachingConnector;
27+
import com.sun.jdi.connect.Connector;
28+
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
29+
import com.sun.jdi.event.EventIterator;
30+
import com.sun.jdi.event.EventQueue;
31+
import com.sun.jdi.event.EventSet;
32+
import com.sun.jdi.event.Event;
33+
import com.sun.jdi.event.ExceptionEvent;
34+
import lib.jdb.Debuggee;
35+
36+
import java.io.IOException;
37+
import java.net.ServerSocket;
38+
import java.util.Iterator;
39+
import java.util.List;
40+
import java.util.Map;
41+
42+
/*
43+
* @test
44+
* @bug 8317920
45+
* @summary Tests for JDWP agent to send valid exception event with onthrow option
46+
* @library /test/lib
47+
*
48+
* @build ThrowCaughtException JdwpOnThrowTest
49+
* @run main/othervm JdwpOnThrowTest
50+
*/
51+
public class JdwpOnThrowTest {
52+
53+
private static long TIMEOUT = 10000;
54+
55+
private static String ATTACH_CONNECTOR = "com.sun.jdi.SocketAttach";
56+
// cache socket attaching connector
57+
private static AttachingConnector attachingConnector;
58+
59+
public static void main(String[] args) throws Exception {
60+
int port = findFreePort();
61+
try (Debuggee debuggee = Debuggee.launcher("ThrowCaughtException").setAddress("localhost:" + port)
62+
.enableOnThrow("Ex", "Start").setSuspended(true).launch()) {
63+
VirtualMachine vm = null;
64+
try {
65+
vm = attach("localhost", "" + port);
66+
EventQueue queue = vm.eventQueue();
67+
log("Waiting for exception event");
68+
long start = System.currentTimeMillis();
69+
while (start + TIMEOUT > System.currentTimeMillis()) {
70+
EventSet eventSet = queue.remove(TIMEOUT);
71+
EventIterator eventIterator = eventSet.eventIterator();
72+
while(eventIterator.hasNext() && start + TIMEOUT > System.currentTimeMillis()) {
73+
Event event = eventIterator.next();
74+
if (event instanceof ExceptionEvent) {
75+
ExceptionEvent ex = (ExceptionEvent)event;
76+
verifyExceptionEvent(ex);
77+
log("Received exception event: " + event);
78+
vm.dispose();
79+
return;
80+
}
81+
log("Received event: " + event);
82+
}
83+
}
84+
throw new RuntimeException("ERROR: failed to receive exception event");
85+
} catch (IOException ex) {
86+
throw new RuntimeException("ERROR: failed to attach", ex);
87+
}
88+
}
89+
}
90+
91+
private static void verifyExceptionEvent(ExceptionEvent ex) throws Exception {
92+
if (ex.exception() == null) {
93+
throw new RuntimeException("Exception is null");
94+
}
95+
if (ex.exception().type() == null) {
96+
throw new RuntimeException("Exception type is null");
97+
}
98+
if (ex.exception().referenceType() == null) {
99+
throw new RuntimeException("Exception reference type is null");
100+
}
101+
if (ex.catchLocation() == null) {
102+
throw new RuntimeException("Exception catch location is null");
103+
}
104+
if (!ex.location().equals(ex.thread().frame(0).location())) {
105+
throw new RuntimeException(
106+
String.format("Throw location %s and location of first frame %s are not equal",
107+
ex.location(), ex.thread().frame(0).location()));
108+
}
109+
if (!ex.exception().type().name().equals("Ex")) {
110+
throw new RuntimeException("Exception has wrong type: " + ex.exception().type().name());
111+
}
112+
}
113+
114+
private static int findFreePort() {
115+
try (ServerSocket socket = new ServerSocket(0)) {
116+
return socket.getLocalPort();
117+
} catch (IOException e) {
118+
throw new RuntimeException(e);
119+
}
120+
}
121+
122+
private static VirtualMachine attach(String address, String port) throws IOException {
123+
if (attachingConnector == null) {
124+
attachingConnector = (AttachingConnector)getConnector(ATTACH_CONNECTOR);
125+
}
126+
Map<String, Connector.Argument> args = attachingConnector.defaultArguments();
127+
setConnectorArg(args, "hostname", address);
128+
setConnectorArg(args, "port", port);
129+
try {
130+
return attachingConnector.attach(args);
131+
} catch (IllegalConnectorArgumentsException e) {
132+
// unexpected.. wrap in RuntimeException
133+
throw new RuntimeException(e);
134+
}
135+
}
136+
137+
private static Connector getConnector(String name) {
138+
for (Connector connector : Bootstrap.virtualMachineManager().allConnectors()) {
139+
if (connector.name().equalsIgnoreCase(name)) {
140+
return connector;
141+
}
142+
}
143+
throw new IllegalArgumentException("Connector " + name + " not found");
144+
}
145+
146+
private static void setConnectorArg(Map<String, Connector.Argument> args, String name, String value) {
147+
Connector.Argument arg = args.get(name);
148+
if (arg == null) {
149+
throw new IllegalArgumentException("Argument " + name + " is not defined");
150+
}
151+
arg.setValue(value);
152+
}
153+
154+
private static void log(Object o) {
155+
System.out.println(String.valueOf(o));
156+
}
157+
158+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023 SAP SE. 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+
public class ThrowCaughtException {
25+
public static void main(String args[]) throws Exception {
26+
try {
27+
System.out.println("Start");
28+
throw new Ex();
29+
} catch (Exception e) {
30+
System.out.println(e);
31+
}
32+
}
33+
}
34+
35+
class Ex extends RuntimeException {
36+
}

test/jdk/com/sun/jdi/lib/jdb/Debuggee.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public static class Launcher {
6868
private String transport = "dt_socket";
6969
private String address = null;
7070
private boolean suspended = true;
71+
private String onthrow = "";
72+
private boolean waitForPortPrint = true;
73+
private String expectedOutputBeforeThrow = "";
7174

7275
private Launcher(String mainClass) {
7376
this.mainClass = mainClass;
@@ -96,29 +99,51 @@ public Launcher setSuspended(boolean value) {
9699
return this;
97100
}
98101

102+
// required to pass non null port with address and emit string before the throw
103+
public Launcher enableOnThrow(String value, String expectedOutputBeforeThrow) {
104+
this.onthrow = value;
105+
this.waitForPortPrint = false;
106+
this.expectedOutputBeforeThrow = expectedOutputBeforeThrow;
107+
return this;
108+
}
109+
99110
public ProcessBuilder prepare() {
100111
List<String> debuggeeArgs = new LinkedList<>();
112+
String onthrowArgs = onthrow.isEmpty() ? "" : ",onthrow=" + onthrow + ",launch=exit";
101113
debuggeeArgs.add("-agentlib:jdwp=transport=" + transport
102114
+ (address == null ? "" : ",address=" + address)
103-
+ ",server=y,suspend=" + (suspended ? "y" : "n"));
115+
+ ",server=y,suspend=" + (suspended ? "y" : "n")
116+
+ onthrowArgs);
104117
debuggeeArgs.addAll(options);
105118
debuggeeArgs.add(mainClass);
106119
return ProcessTools.createTestJvm(debuggeeArgs);
107120
}
108121

109122
public Debuggee launch(String name) {
110-
return new Debuggee(prepare(), name);
123+
return new Debuggee(prepare(), name, waitForPortPrint, expectedOutputBeforeThrow);
111124
}
112125
public Debuggee launch() {
113126
return launch("debuggee");
114127
}
115128
}
116129

117130
// starts the process, waits for "Listening for transport" output and detects transport/address
118-
private Debuggee(ProcessBuilder pb, String name) {
131+
private Debuggee(ProcessBuilder pb, String name, boolean waitForPortPrint, String expectedOutputBeforeThrow) {
119132
// debuggeeListen[0] - transport, debuggeeListen[1] - address
120133
String[] debuggeeListen = new String[2];
121134
Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
135+
if (!waitForPortPrint) {
136+
try {
137+
p = ProcessTools.startProcess(name, pb, s -> {output.add(s);}, s -> {
138+
return s.equals(expectedOutputBeforeThrow);
139+
}, 30, TimeUnit.SECONDS);
140+
} catch (IOException | InterruptedException | TimeoutException ex) {
141+
throw new RuntimeException("failed to launch debuggee", ex);
142+
}
143+
transport = null;
144+
address = null;
145+
return;
146+
}
122147
try {
123148
p = ProcessTools.startProcess(name, pb,
124149
s -> output.add(s), // output consumer
@@ -167,10 +192,16 @@ public String getOutput() {
167192
}
168193

169194
String getTransport() {
195+
if (transport == null) {
196+
throw new IllegalStateException("transport is not available");
197+
}
170198
return transport;
171199
}
172200

173201
public String getAddress() {
202+
if (address == null) {
203+
throw new IllegalStateException("address is not available");
204+
}
174205
return address;
175206
}
176207

0 commit comments

Comments
 (0)