Skip to content

Commit 8f4ebd8

Browse files
committed
8317920: JDWP-agent sends broken exception event with onthrow option
Reviewed-by: clanger, cjplummer
1 parent cd25d1a commit 8f4ebd8

File tree

4 files changed

+258
-21
lines changed

4 files changed

+258
-21
lines changed

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

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

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

108108
/*
@@ -391,7 +391,7 @@ cbEarlyVMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread)
391391
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at VM_INIT time");
392392
}
393393
if (initOnStartup)
394-
initialize(env, thread, EI_VM_INIT);
394+
initialize(env, thread, EI_VM_INIT, NULL);
395395
vmInitialized = JNI_TRUE;
396396
LOG_MISC(("END cbEarlyVMInit"));
397397
}
@@ -444,6 +444,19 @@ cbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env,
444444
LOG_MISC(("VM is not initialized yet"));
445445
return;
446446
}
447+
EventInfo info;
448+
info.ei = EI_EXCEPTION;
449+
info.thread = thread;
450+
info.clazz = getMethodClass(jvmti_env, method);
451+
info.method = method;
452+
info.location = location;
453+
info.object = exception;
454+
if (gdata->vthreadsSupported) {
455+
info.is_vthread = isVThread(thread);
456+
}
457+
info.u.exception.catch_clazz = getMethodClass(jvmti_env, catch_method);
458+
info.u.exception.catch_method = catch_method;
459+
info.u.exception.catch_location = catch_location;
447460

448461
/*
449462
* We want to preserve any current exception that might get wiped
@@ -458,24 +471,22 @@ cbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env,
458471
if (initOnUncaught && catch_method == NULL) {
459472

460473
LOG_MISC(("Initializing on uncaught exception"));
461-
initialize(env, thread, EI_EXCEPTION);
474+
initialize(env, thread, EI_EXCEPTION, &info);
462475

463476
} else if (initOnException != NULL) {
464477

465-
jclass clazz;
466-
467-
/* Get class of exception thrown */
468-
clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, exception);
469-
if ( clazz != NULL ) {
478+
jclass exception_clazz = JNI_FUNC_PTR(env, GetObjectClass)(env, exception);
479+
/* check class of exception thrown */
480+
if ( exception_clazz != NULL ) {
470481
char *signature = NULL;
471482
/* initing on throw, check */
472-
error = classSignature(clazz, &signature, NULL);
483+
error = classSignature(exception_clazz, &signature, NULL);
473484
LOG_MISC(("Checking specific exception: looking for %s, got %s",
474485
initOnException, signature));
475486
if ( (error==JVMTI_ERROR_NONE) &&
476487
(strcmp(signature, initOnException) == 0)) {
477488
LOG_MISC(("Initializing on specific exception"));
478-
initialize(env, thread, EI_EXCEPTION);
489+
initialize(env, thread, EI_EXCEPTION, &info);
479490
} else {
480491
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */
481492
}
@@ -616,9 +627,11 @@ jniFatalError(JNIEnv *env, const char *msg, jvmtiError error, int exit_code)
616627

617628
/*
618629
* Initialize debugger back end modules
630+
*
631+
* @param opt_info optional event info to use, might be null
619632
*/
620633
static void
621-
initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei)
634+
initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei, EventInfo *opt_info)
622635
{
623636
jvmtiError error;
624637
EnumerateArg arg;
@@ -706,13 +719,13 @@ initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei)
706719
* can get in the queue (from other not-yet-suspended threads)
707720
* before this one does. (Also need to handle allocation error below?)
708721
*/
709-
EventInfo info;
710722
struct bag *initEventBag;
711-
LOG_MISC(("triggering_ei != EI_VM_INIT"));
723+
LOG_MISC(("triggering_ei == EI_EXCEPTION"));
724+
JDI_ASSERT(triggering_ei == EI_EXCEPTION);
725+
JDI_ASSERT(opt_info != NULL);
712726
initEventBag = eventHelper_createEventBag();
713-
(void)memset(&info,0,sizeof(info));
714-
info.ei = triggering_ei;
715-
eventHelper_recordEvent(&info, 0, suspendPolicy, initEventBag);
727+
threadControl_onEventHandlerEntry(currentSessionID, opt_info, NULL);
728+
eventHelper_recordEvent(opt_info, 0, suspendPolicy, initEventBag);
716729
(void)eventHelper_reportEvents(currentSessionID, initEventBag);
717730
bagDestroyBag(initEventBag);
718731
}
@@ -1368,7 +1381,7 @@ JNIEXPORT char const* JNICALL debugInit_startDebuggingViaCommand(JNIEnv* env, jt
13681381
if (!startedViaJcmd) {
13691382
startedViaJcmd = JNI_TRUE;
13701383
is_first_start = JNI_TRUE;
1371-
initialize(env, thread, EI_VM_INIT);
1384+
initialize(env, thread, EI_VM_INIT, NULL);
13721385
}
13731386

13741387
bagEnumerateOver(transports, getFirstTransport, &spec);
+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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 ex) {
75+
verifyExceptionEvent(ex);
76+
log("Received exception event: " + event);
77+
vm.dispose();
78+
return;
79+
}
80+
log("Received event: " + event);
81+
}
82+
}
83+
throw new RuntimeException("ERROR: failed to receive exception event");
84+
} catch (IOException ex) {
85+
throw new RuntimeException("ERROR: failed to attach", ex);
86+
}
87+
}
88+
}
89+
90+
private static void verifyExceptionEvent(ExceptionEvent ex) throws Exception {
91+
if (ex.exception() == null) {
92+
throw new RuntimeException("Exception is null");
93+
}
94+
if (ex.exception().type() == null) {
95+
throw new RuntimeException("Exception type is null");
96+
}
97+
if (ex.exception().referenceType() == null) {
98+
throw new RuntimeException("Exception reference type is null");
99+
}
100+
if (ex.catchLocation() == null) {
101+
throw new RuntimeException("Exception catch location is null");
102+
}
103+
if (!ex.location().equals(ex.thread().frame(0).location())) {
104+
throw new RuntimeException(
105+
String.format("Throw location %s and location of first frame %s are not equal",
106+
ex.location(), ex.thread().frame(0).location()));
107+
}
108+
if (!ex.exception().type().name().equals("Ex")) {
109+
throw new RuntimeException("Exception has wrong type: " + ex.exception().type().name());
110+
}
111+
}
112+
113+
private static int findFreePort() {
114+
try (ServerSocket socket = new ServerSocket(0)) {
115+
return socket.getLocalPort();
116+
} catch (IOException e) {
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
121+
private static VirtualMachine attach(String address, String port) throws IOException {
122+
if (attachingConnector == null) {
123+
attachingConnector = (AttachingConnector)getConnector(ATTACH_CONNECTOR);
124+
}
125+
Map<String, Connector.Argument> args = attachingConnector.defaultArguments();
126+
setConnectorArg(args, "hostname", address);
127+
setConnectorArg(args, "port", port);
128+
try {
129+
return attachingConnector.attach(args);
130+
} catch (IllegalConnectorArgumentsException e) {
131+
// unexpected.. wrap in RuntimeException
132+
throw new RuntimeException(e);
133+
}
134+
}
135+
136+
private static Connector getConnector(String name) {
137+
for (Connector connector : Bootstrap.virtualMachineManager().allConnectors()) {
138+
if (connector.name().equalsIgnoreCase(name)) {
139+
return connector;
140+
}
141+
}
142+
throw new IllegalArgumentException("Connector " + name + " not found");
143+
}
144+
145+
private static void setConnectorArg(Map<String, Connector.Argument> args, String name, String value) {
146+
Connector.Argument arg = args.get(name);
147+
if (arg == null) {
148+
throw new IllegalArgumentException("Argument " + name + " is not defined");
149+
}
150+
arg.setValue(value);
151+
}
152+
153+
private static void log(Object o) {
154+
System.out.println(String.valueOf(o));
155+
}
156+
157+
}
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

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -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;
@@ -100,30 +103,52 @@ public Launcher setSuspended(boolean value) {
100103
return this;
101104
}
102105

106+
// required to pass non null port with address and emit string before the throw
107+
public Launcher enableOnThrow(String value, String expectedOutputBeforeThrow) {
108+
this.onthrow = value;
109+
this.waitForPortPrint = false;
110+
this.expectedOutputBeforeThrow = expectedOutputBeforeThrow;
111+
return this;
112+
}
113+
103114
public ProcessBuilder prepare() {
104115
List<String> debuggeeArgs = new LinkedList<>();
105116
if (vmOptions != null) {
106117
debuggeeArgs.add(vmOptions);
107118
}
119+
String onthrowArgs = onthrow.isEmpty() ? "" : ",onthrow=" + onthrow + ",launch=exit";
108120
debuggeeArgs.add("-agentlib:jdwp=transport=" + transport
109121
+ (address == null ? "" : ",address=" + address)
110-
+ ",server=y,suspend=" + (suspended ? "y" : "n"));
122+
+ ",server=y,suspend=" + (suspended ? "y" : "n")
123+
+ onthrowArgs);
111124
debuggeeArgs.addAll(options);
112125
debuggeeArgs.add(mainClass);
113126
return ProcessTools.createTestJvm(debuggeeArgs);
114127
}
115128

116129
public Debuggee launch(String name) {
117-
return new Debuggee(prepare(), name);
130+
return new Debuggee(prepare(), name, waitForPortPrint, expectedOutputBeforeThrow);
118131
}
119132
public Debuggee launch() {
120133
return launch("debuggee");
121134
}
122135
}
123136

124137
// starts the process, waits for "Listening for transport" output and detects transport/address
125-
private Debuggee(ProcessBuilder pb, String name) {
138+
private Debuggee(ProcessBuilder pb, String name, boolean waitForPortPrint, String expectedOutputBeforeThrow) {
126139
JDWP.ListenAddress[] listenAddress = new JDWP.ListenAddress[1];
140+
if (!waitForPortPrint) {
141+
try {
142+
p = ProcessTools.startProcess(name, pb, s -> {output.add(s);}, s -> {
143+
return s.equals(expectedOutputBeforeThrow);
144+
}, 30, TimeUnit.SECONDS);
145+
} catch (IOException | InterruptedException | TimeoutException ex) {
146+
throw new RuntimeException("failed to launch debuggee", ex);
147+
}
148+
transport = null;
149+
address = null;
150+
return;
151+
}
127152
try {
128153
p = ProcessTools.startProcess(name, pb,
129154
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)