Skip to content

Commit b77c5b3

Browse files
authored
Merge pull request #158 from pdbain-ibm/attach_self
Disable late attach to self by default
2 parents f2542b1 + 609fe3e commit b77c5b3

File tree

11 files changed

+368
-41
lines changed

11 files changed

+368
-41
lines changed

jcl/src/java.base/share/classes/com/ibm/oti/util/ExternalMessages-MasterIndex.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,4 @@ K0644="Caller-sensitive method called StackWalker.getCallerClass()"
12621262
#java.lang.ClassLoader
12631263
K0645="The given class loader name can't be empty."
12641264

1265+
K0646="Late attach connection to self disabled. Set jdk.attach.allowAttachSelf=true"

jcl/src/java.base/share/classes/com/ibm/tools/attach/target/AttachHandler.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.ibm.tools.attach.target;
33

44
/*******************************************************************************
5-
* Copyright (c) 2009, 2015 IBM Corp. and others
5+
* Copyright (c) 2009, 2017 IBM Corp. and others
66
*
77
* This program and the accompanying materials are made available under
88
* the terms of the Eclipse Public License 2.0 which accompanies this
@@ -29,6 +29,8 @@
2929
import java.util.Properties;
3030
import java.util.Vector;
3131

32+
import com.ibm.oti.vm.VM;
33+
3234
/**
3335
* This class handles incoming attachment requests from other VMs. Must be
3436
* public because it is used by java.lang.System
@@ -50,7 +52,7 @@ public class AttachHandler extends Thread {
5052
/**
5153
* Time delay before we give up trying to terminate the wait loop.
5254
*/
53-
static final long shutdownTimeoutMs = Integer.getInteger("com.ibm.tools.attach.shutdown_timeout", 10000); //$NON-NLS-1$
55+
static final long shutdownTimeoutMs = Integer.getInteger("com.ibm.tools.attach.shutdown_timeout", 10000).longValue(); //$NON-NLS-1$
5456
private enum AttachStateValues {
5557
ATTACH_UNINITIALIZED, ATTACH_TERMINATED, ATTACH_STARTING, ATTACH_INITIALIZED
5658
}
@@ -60,7 +62,7 @@ private enum AttachStateValues {
6062
*/
6163
static AttachHandler mainHandler = new AttachHandler();
6264
static volatile Thread currentAttachThread = mainHandler; /* Join on this when shutting down */
63-
private Vector<Attachment> attachments = new Vector<Attachment>();
65+
private Vector<Attachment> attachments = new Vector<>();
6466
private static String vmId = ""; /* ID of the currently running VM *///$NON-NLS-1$
6567
/**
6668
* Human-friendly name for VM
@@ -70,13 +72,19 @@ private enum AttachStateValues {
7072
private static boolean waitingForSemaphore = false;
7173

7274
private static final class AttachStateSync {
73-
};
75+
/**
76+
* Empty class for synchronization objects.
77+
*/
78+
}
7479

7580
private static AttachStateSync stateSync = new AttachStateSync();
7681

7782
private static int notificationCount;
7883

7984
private static final class syncObject {
85+
/**
86+
* Empty class for synchronization objects.
87+
*/
8088
}
8189

8290
private static boolean doCancelNotify;
@@ -102,10 +110,24 @@ private static final class syncObject {
102110
private static String nameProperty;
103111
private static String pidProperty;
104112
private static int numberOfTargets;
113+
/**
114+
* As of Java 9, a VM cannot attach to itself unless explicitly enabled.
115+
* Grab the setting before the application has a chance to change it,
116+
* but parse it lazily because we rarely need the value.
117+
*/
118+
public final static String allowAttachSelf =
119+
VM.getVMLangAccess().internalGetProperties().getProperty("jdk.attach.allowAttachSelf", //$NON-NLS-1$
120+
/*[IF Sidecar19-SE]*/
121+
"false" //$NON-NLS-1$
122+
/*[ELSE]
123+
"true" //$NON-NLS-1$
124+
/*[ENDIF]*/
125+
);
105126

106127
/* only the attach handler thread uses syncFileLock */
107128
/* [PR Jazz 30075] Make syncFileLock an instance variable since it is accessed only by the attachHandler singleton. */
108-
private FileLock syncFileLock;
129+
private FileLock syncFileLock;
130+
private PrintStream logStream;
109131

110132
/**
111133
* Keep the constructor private
@@ -127,20 +149,19 @@ static void initializeAttachAPI() {
127149
setAttachState(AttachStateValues.ATTACH_TERMINATED);
128150
return;
129151
}
130-
131152
/*
132153
* set default behaviour:
133154
* Java 6 R24 and later: disabled by default on z/OS, enabled on all other platforms
134155
* Java 5: disabled by default on all platforms
135156
*/
136157
/*[PR Jazz 59196 LIR: Disable attach API by default on z/OS (31972)]*/
137-
boolean enableAttach = true;
138-
String osName = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("os.name"); //$NON-NLS-1$
139-
if ((null != osName) && (osName.equalsIgnoreCase("z/OS"))) { //$NON-NLS-1$
140-
enableAttach = false;
141-
}
142-
String enableAttachProp = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("com.ibm.tools.attach.enable"); //$NON-NLS-1$
158+
boolean enableAttach = true;
159+
String osName = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("os.name"); //$NON-NLS-1$
160+
if ((null != osName) && (osName.equalsIgnoreCase("z/OS"))) { //$NON-NLS-1$
161+
enableAttach = false;
162+
}
143163
/* the system property overrides the default */
164+
String enableAttachProp = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("com.ibm.tools.attach.enable"); //$NON-NLS-1$
144165
if (null != enableAttachProp) {
145166
if (enableAttachProp.equalsIgnoreCase("no")) { //$NON-NLS-1$
146167
enableAttach = false;
@@ -281,7 +302,8 @@ private boolean initialize() throws IOException {
281302
if ((null == IPC.logStream) && (null != loggingProperty)
282303
&& loggingProperty.equalsIgnoreCase("yes")) { //$NON-NLS-1$
283304
File logFile = new File(logName + pidProperty + ".log"); //$NON-NLS-1$
284-
IPC.setLogStream(new PrintStream(logFile));
305+
logStream = new PrintStream(logFile);
306+
IPC.setLogStream(logStream);
285307
IPC.loggingEnabled = true;
286308
IPC.setDefaultVmId(pidProperty);
287309
IPC.logMessage("AttachHandler initialize"); //$NON-NLS-1$
@@ -438,7 +460,7 @@ private Attachment waitForNotification(boolean retry) throws IOException {
438460
return at;
439461
}
440462

441-
private Attachment checkReplyAndCreateAttachment() throws IOException {
463+
private static Attachment checkReplyAndCreateAttachment() throws IOException {
442464
Attachment at = mainHandler.connectToAttacher();
443465
/*[PR Jazz 41720 - Recreate notification directory if it is deleted. ]*/
444466
if (!TargetDirectory.ensureMyAdvertisementExists(getVmId())) {
@@ -512,6 +534,9 @@ protected boolean terminate(boolean wakeHandler) {
512534
case ATTACH_STARTING: break; /*/* may be anywhere in the initialization code. Do the full shutdown */
513535
case ATTACH_INITIALIZED: break; /* go through the full shutdown */
514536
case ATTACH_TERMINATED: return false; /* already started terminating */
537+
default:
538+
IPC.logMessage("Unrecognized synchronization state "+stateSync.toString()); //$NON-NLS-1$
539+
break;
515540
}
516541
}
517542
currentAttachThread.interrupt(); /* do this after we change the attachState */
@@ -752,7 +777,7 @@ public static boolean waitForAttachApiInitialization() {
752777
--waitCycles;
753778
try {
754779
/* assignments to stateSync cause a notifyAll on stateSync */
755-
stateSync.wait((long) 100000); /* timeout value */
780+
stateSync.wait(100000); /* timeout value */
756781
currentState = getAttachState();
757782
switch (currentState) {
758783
case ATTACH_STARTING:
@@ -967,6 +992,9 @@ static void setFactoryException(Exception factoryException) {
967992
AttachHandler.factoryException = factoryException;
968993
}
969994

995+
/**
996+
* @return ignoreNotification sync object
997+
*/
970998
public Object getIgnoreNotification() {
971999
return ignoreNotification;
9721000
}

jcl/src/jdk.attach/share/classes/com/ibm/tools/attach/attacher/OpenJ9VirtualMachine.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import com.sun.tools.attach.AgentInitializationException;
5151
import com.sun.tools.attach.AgentLoadException;
5252

53+
import static com.ibm.oti.util.Msg.getString;
54+
5355
/**
5456
* Handles the initiator end of an attachment to a target VM
5557
*
@@ -96,7 +98,7 @@ final class OpenJ9VirtualMachine extends VirtualMachine implements Response {
9698
super(provider, id);
9799
if ((null == id) || (null == provider)) {
98100
/*[MSG "K0554", "Virtual machine ID or display name is null"]*/
99-
throw new NullPointerException(com.ibm.oti.util.Msg.getString("K0554")); //$NON-NLS-1$
101+
throw new NullPointerException(getString("K0554")); //$NON-NLS-1$
100102
}
101103
new IPC();
102104
this.targetId = id;
@@ -113,7 +115,7 @@ final class OpenJ9VirtualMachine extends VirtualMachine implements Response {
113115
void attachTarget() throws IOException, AttachNotSupportedException {
114116
if (null == descriptor) {
115117
/*[MSG "K0531", "target not found"]*/
116-
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0531")); //$NON-NLS-1$
118+
throw new AttachNotSupportedException(getString("K0531")); //$NON-NLS-1$
117119
}
118120
AttachNotSupportedException lastException = null;
119121
/*[PR CMVC 182802 ]*/
@@ -187,7 +189,7 @@ public synchronized void detach() throws IOException {
187189
public Properties getAgentProperties() throws IOException {
188190
if (!targetAttached) {
189191
/*[MSG "K0544", "Target not attached"]*/
190-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
192+
throw new IOException(getString("K0544")); //$NON-NLS-1$
191193
}
192194
Properties props = getTargetProperties(false);
193195

@@ -198,7 +200,7 @@ public Properties getAgentProperties() throws IOException {
198200
public Properties getSystemProperties() throws IOException {
199201
if (!targetAttached) {
200202
/*[MSG "K0544", "Target not attached"]*/
201-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
203+
throw new IOException(getString("K0544")); //$NON-NLS-1$
202204
}
203205
Properties props = getTargetProperties(true);
204206
return props;
@@ -225,7 +227,7 @@ public synchronized void loadAgent(String agent, String options)
225227

226228
if (!targetAttached) {
227229
/*[MSG "K0544", "Target not attached"]*/
228-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
230+
throw new IOException(getString("K0544")); //$NON-NLS-1$
229231
}
230232
AttachmentConnection.streamSend(commandStream, (createLoadAgent(agent, options)));
231233
String response = AttachmentConnection.streamReceiveString(responseStream);
@@ -239,7 +241,7 @@ public synchronized void loadAgentLibrary(String agentLibrary,
239241

240242
if (!targetAttached) {
241243
/*[MSG "K0544", "Target not attached"]*/
242-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
244+
throw new IOException(getString("K0544")); //$NON-NLS-1$
243245
}
244246
AttachmentConnection.streamSend(commandStream, createLoadAgentLibrary(
245247
agentLibrary, options, false));
@@ -253,11 +255,11 @@ public synchronized void loadAgentPath(String agentPath, String options)
253255
IOException {
254256
if (null == agentPath) {
255257
/*[MSG "K0577", "loadAgentPath: null agent path"]*/
256-
throw new AgentLoadException(com.ibm.oti.util.Msg.getString("K0577")); //$NON-NLS-1$
258+
throw new AgentLoadException(getString("K0577")); //$NON-NLS-1$
257259
}
258260
if (!targetAttached) {
259261
/*[MSG "K0544", "Target not attached"]*/
260-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
262+
throw new IOException(getString("K0544")); //$NON-NLS-1$
261263
}
262264
AttachmentConnection.streamSend(commandStream, createLoadAgentLibrary(agentPath,
263265
options, true));
@@ -318,7 +320,7 @@ private static boolean parseResponse(String response) throws IOException,
318320
}
319321
if (response.contains(EXCEPTION_IOEXCEPTION)) {
320322
/*[MSG "K0576","IOException from target: {0}"]*/
321-
throw new IOException(com.ibm.oti.util.Msg.getString("K0576", trimmedResponse)); //$NON-NLS-1$
323+
throw new IOException(getString("K0576", trimmedResponse)); //$NON-NLS-1$
322324
} else if (response.contains(EXCEPTION_AGENT_INITIALIZATION_EXCEPTION)) {
323325
Integer status = getStatusValue(trimmedResponse);
324326
if (null == status) {
@@ -330,13 +332,13 @@ private static boolean parseResponse(String response) throws IOException,
330332
throw new AgentLoadException(trimmedResponse);
331333
} else if (response.contains(EXCEPTION_IOEXCEPTION)) {
332334
/*[MSG "K0576","IOException from target: {0}"]*/
333-
throw new IOException(com.ibm.oti.util.Msg.getString("K0576", trimmedResponse)); //$NON-NLS-1$
335+
throw new IOException(getString("K0576", trimmedResponse)); //$NON-NLS-1$
334336
} else if (response.contains(EXCEPTION_ILLEGAL_ARGUMENT_EXCEPTION)) {
335337
/*[MSG "K05de","IllegalArgumentException from target: {0}"]*/
336-
throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString("K05de", trimmedResponse)); //$NON-NLS-1$
338+
throw new IllegalArgumentException(getString("K05de", trimmedResponse)); //$NON-NLS-1$
337339
} else if (response.contains(EXCEPTION_ATTACH_OPERATION_FAILED_EXCEPTION)) {
338340
/*[MSG "k05dc","AttachOperationFailedException from target: {0}"]*/
339-
throw new AttachOperationFailedException(com.ibm.oti.util.Msg.getString("k05dc", trimmedResponse)); //$NON-NLS-1$
341+
throw new AttachOperationFailedException(getString("k05dc", trimmedResponse)); //$NON-NLS-1$
340342
}
341343
return false;
342344
} else if (response.startsWith(ACK) || response.startsWith(ATTACH_RESULT)) {
@@ -397,18 +399,24 @@ private void tryAttachTarget(int timeout) throws IOException,
397399
* to attach
398400
*/
399401
/*[MSG "K0457", "Target no longer available"]*/
400-
AttachNotSupportedException exc = new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0457")); //$NON-NLS-1$
402+
AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0457")); //$NON-NLS-1$
401403
exc.initCause(e);
402404
throw exc;
403405
}
404406

405407
if (descriptor.id().equals(AttachHandler.getVmId())) {
408+
String allowAttachSelf_Value = AttachHandler.allowAttachSelf;
409+
boolean selfAttachAllowed = "".equals(allowAttachSelf_Value) || Boolean.parseBoolean(allowAttachSelf_Value); //$NON-NLS-1$
410+
if (!selfAttachAllowed) {
411+
/*[MSG "K0646", "Late attach connection to self disabled. Set jdk.attach.allowAttachSelf=true"]*/
412+
throw new IOException(getString("K0646")); //$NON-NLS-1$
413+
}
406414
/* I am connecting to myself: bypass the notification and launch the attachment thread directly */
407415
if (AttachHandler.isAttachApiInitialized()) {
408416
AttachHandler.getMainHandler().connectToAttacher();
409417
} else {
410418
/*[MSG "K0558", "Attach API initialization failed"]*/
411-
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0558")); //$NON-NLS-1$
419+
throw new AttachNotSupportedException(getString("K0558")); //$NON-NLS-1$
412420
}
413421
} else {
414422
lockAllAttachNotificationSyncFiles(vmds);
@@ -417,7 +425,7 @@ private void tryAttachTarget(int timeout) throws IOException,
417425
/*[MSG "K0532", "status={0}"]*/
418426
if ((IPC.JNI_OK != status)
419427
&& (CommonDirectory.J9PORT_INFO_SHSEM_OPENED_STALE != status)) {
420-
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0532", status)); //$NON-NLS-1$
428+
throw new AttachNotSupportedException(getString("K0532", status)); //$NON-NLS-1$
421429
}
422430
}
423431

@@ -429,7 +437,7 @@ private void tryAttachTarget(int timeout) throws IOException,
429437
targetServer.close();
430438
IPC.logMessage("attachTarget SocketTimeoutException on " + portNumber + " to " + targetId); //$NON-NLS-1$ //$NON-NLS-2$
431439
/*[MSG "K0539","acknowledgement timeout from {0} on port {1}"]*/
432-
AttachNotSupportedException exc = new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0539", targetId, portNumber)); //$NON-NLS-1$
440+
AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0539", targetId, portNumber)); //$NON-NLS-1$
433441
exc.initCause(e);
434442
throw exc;
435443
}
@@ -443,7 +451,7 @@ private void tryAttachTarget(int timeout) throws IOException,
443451
String response = AttachmentConnection.streamReceiveString(responseStream, ATTACH_CONNECTED_MESSAGE_LENGTH_LIMIT);
444452
/*[MSG "K0533", "key error: {0}"]*/
445453
if (!response.contains(' ' + key + ' ')) {
446-
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0533", response)); //$NON-NLS-1$
454+
throw new AttachNotSupportedException(getString("K0533", response)); //$NON-NLS-1$
447455
}
448456
IPC.logMessage("attachTarget connected on ", portNumber.toString()); //$NON-NLS-1$
449457
targetAttached = true;
@@ -493,7 +501,7 @@ public void startManagementAgent(Properties agentProperties)
493501
throws IOException, IllegalArgumentException, AttachOperationFailedException {
494502
if (!targetAttached) {
495503
/*[MSG "K0544", "Target not attached"]*/
496-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
504+
throw new IOException(getString("K0544")); //$NON-NLS-1$
497505
} else if (null == agentProperties) {
498506
throw new NullPointerException();
499507
}
@@ -516,7 +524,7 @@ public void startManagementAgent(Properties agentProperties)
516524
public String startLocalManagementAgent() throws IOException {
517525
if (!targetAttached) {
518526
/*[MSG "K0544", "Target not attached"]*/
519-
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
527+
throw new IOException(getString("K0544")); //$NON-NLS-1$
520528
}
521529
AttachmentConnection.streamSend(commandStream, Command.START_LOCAL_MANAGEMENT_AGENT);
522530
String response = AttachmentConnection.streamReceiveString(responseStream);
@@ -525,14 +533,14 @@ public String startLocalManagementAgent() throws IOException {
525533
try {
526534
if (!parseResponse(response)) {
527535
/*[MSG "k05dd", "unrecognized response: {0}"]*/
528-
throw new IOException(com.ibm.oti.util.Msg.getString("k05dd", response)); //$NON-NLS-1$
536+
throw new IOException(getString("k05dd", response)); //$NON-NLS-1$
529537
} else if (response.startsWith(ACK)) { /* this came from a legacy VM with dummy start*Agent()s */
530538
result = response.substring(ACK.length());
531539
} else if (response.startsWith(ATTACH_RESULT)) {
532540
result = response.substring(ATTACH_RESULT.length());
533541
} else {
534542
/*[MSG "k05dd", "unrecognized response: {0}"]*/
535-
throw new IOException(com.ibm.oti.util.Msg.getString("k05dd", response)); //$NON-NLS-1$
543+
throw new IOException(getString("k05dd", response)); //$NON-NLS-1$
536544
}
537545
} catch (AgentLoadException | IllegalArgumentException | AgentInitializationException e) {
538546
IPC.logMessage("Unexpected exception " + e + " in startLocalManagementAgent"); //$NON-NLS-1$//$NON-NLS-2$

test/Java8andUp/playlist.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED \
191191
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
192192
-Dcom.ibm.tools.attach.enable=yes \
193+
-Djdk.attach.allowAttachSelf=true \
193194
-Dcom.ibm.tools.attach.timeout=15000 \
194195
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
195196
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames AttachAPISanity \
@@ -276,6 +277,7 @@
276277
--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED \
277278
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
278279
-Dcom.ibm.tools.attach.enable=yes \
280+
-Djdk.attach.allowAttachSelf=true \
279281
-Dcom.ibm.tools.attach.timeout=15000 \
280282
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
281283
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestAttachAPIEnabling \
@@ -365,6 +367,7 @@
365367
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
366368
-Dcom.ibm.tools.attach.enable=yes \
367369
-Dcom.ibm.tools.attach.timeout=15000 \
370+
-Djdk.attach.allowAttachSelf=true \
368371
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
369372
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestSunAttachClasses \
370373
-groups $(TEST_GROUP) \

0 commit comments

Comments
 (0)