Skip to content

Disable late attach to self by default #158

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

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1262,3 +1262,4 @@ K0644="Caller-sensitive method called StackWalker.getCallerClass()"
#java.lang.ClassLoader
K0645="The given class loader name can't be empty."

K0646="Late attach connection to self disabled. Set jdk.attach.allowAttachSelf=true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the preprocessor definition to match.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.ibm.tools.attach.target;

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

import com.ibm.oti.vm.VM;

/**
* This class handles incoming attachment requests from other VMs. Must be
* public because it is used by java.lang.System
Expand All @@ -50,7 +52,7 @@ public class AttachHandler extends Thread {
/**
* Time delay before we give up trying to terminate the wait loop.
*/
static final long shutdownTimeoutMs = Integer.getInteger("com.ibm.tools.attach.shutdown_timeout", 10000); //$NON-NLS-1$
static final long shutdownTimeoutMs = Integer.getInteger("com.ibm.tools.attach.shutdown_timeout", 10000).longValue(); //$NON-NLS-1$
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the point of this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It avoids a warning about auto-unboxing.

private enum AttachStateValues {
ATTACH_UNINITIALIZED, ATTACH_TERMINATED, ATTACH_STARTING, ATTACH_INITIALIZED
}
Expand All @@ -60,7 +62,7 @@ private enum AttachStateValues {
*/
static AttachHandler mainHandler = new AttachHandler();
static volatile Thread currentAttachThread = mainHandler; /* Join on this when shutting down */
private Vector<Attachment> attachments = new Vector<Attachment>();
private Vector<Attachment> attachments = new Vector<>();
private static String vmId = ""; /* ID of the currently running VM *///$NON-NLS-1$
/**
* Human-friendly name for VM
Expand All @@ -70,13 +72,19 @@ private enum AttachStateValues {
private static boolean waitingForSemaphore = false;

private static final class AttachStateSync {
};
/**
* Empty class for synchronization objects.
*/
}

private static AttachStateSync stateSync = new AttachStateSync();

private static int notificationCount;

private static final class syncObject {
/**
* Empty class for synchronization objects.
*/
}

private static boolean doCancelNotify;
Expand All @@ -102,10 +110,24 @@ private static final class syncObject {
private static String nameProperty;
private static String pidProperty;
private static int numberOfTargets;
/**
* As of Java 9, a VM cannot attach to itself unless explicitly enabled.
* Grab the setting before the application has a chance to change it,
* but parse it lazily because we rarely need the value.
*/
public final static String allowAttachSelf =
VM.getVMLangAccess().internalGetProperties().getProperty("jdk.attach.allowAttachSelf", //$NON-NLS-1$
/*[IF Sidecar19-SE]*/
"false" //$NON-NLS-1$
/*[ELSE]
"true" //$NON-NLS-1$
/*[ENDIF]*/
);

/* only the attach handler thread uses syncFileLock */
/* [PR Jazz 30075] Make syncFileLock an instance variable since it is accessed only by the attachHandler singleton. */
private FileLock syncFileLock;
private FileLock syncFileLock;
private PrintStream logStream;

/**
* Keep the constructor private
Expand All @@ -127,20 +149,19 @@ static void initializeAttachAPI() {
setAttachState(AttachStateValues.ATTACH_TERMINATED);
return;
}

/*
* set default behaviour:
* Java 6 R24 and later: disabled by default on z/OS, enabled on all other platforms
* Java 5: disabled by default on all platforms
*/
/*[PR Jazz 59196 LIR: Disable attach API by default on z/OS (31972)]*/
boolean enableAttach = true;
String osName = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("os.name"); //$NON-NLS-1$
if ((null != osName) && (osName.equalsIgnoreCase("z/OS"))) { //$NON-NLS-1$
enableAttach = false;
}
String enableAttachProp = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("com.ibm.tools.attach.enable"); //$NON-NLS-1$
boolean enableAttach = true;
String osName = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("os.name"); //$NON-NLS-1$
if ((null != osName) && (osName.equalsIgnoreCase("z/OS"))) { //$NON-NLS-1$
enableAttach = false;
}
/* the system property overrides the default */
String enableAttachProp = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("com.ibm.tools.attach.enable"); //$NON-NLS-1$
if (null != enableAttachProp) {
if (enableAttachProp.equalsIgnoreCase("no")) { //$NON-NLS-1$
enableAttach = false;
Expand Down Expand Up @@ -281,7 +302,8 @@ private boolean initialize() throws IOException {
if ((null == IPC.logStream) && (null != loggingProperty)
&& loggingProperty.equalsIgnoreCase("yes")) { //$NON-NLS-1$
File logFile = new File(logName + pidProperty + ".log"); //$NON-NLS-1$
IPC.setLogStream(new PrintStream(logFile));
logStream = new PrintStream(logFile);
IPC.setLogStream(logStream);
IPC.loggingEnabled = true;
IPC.setDefaultVmId(pidProperty);
IPC.logMessage("AttachHandler initialize"); //$NON-NLS-1$
Expand Down Expand Up @@ -438,7 +460,7 @@ private Attachment waitForNotification(boolean retry) throws IOException {
return at;
}

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

/**
* @return ignoreNotification sync object
*/
public Object getIgnoreNotification() {
return ignoreNotification;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;

import static com.ibm.oti.util.Msg.getString;

/**
* Handles the initiator end of an attachment to a target VM
*
Expand Down Expand Up @@ -96,7 +98,7 @@ final class OpenJ9VirtualMachine extends VirtualMachine implements Response {
super(provider, id);
if ((null == id) || (null == provider)) {
/*[MSG "K0554", "Virtual machine ID or display name is null"]*/
throw new NullPointerException(com.ibm.oti.util.Msg.getString("K0554")); //$NON-NLS-1$
throw new NullPointerException(getString("K0554")); //$NON-NLS-1$
}
new IPC();
this.targetId = id;
Expand All @@ -113,7 +115,7 @@ final class OpenJ9VirtualMachine extends VirtualMachine implements Response {
void attachTarget() throws IOException, AttachNotSupportedException {
if (null == descriptor) {
/*[MSG "K0531", "target not found"]*/
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0531")); //$NON-NLS-1$
throw new AttachNotSupportedException(getString("K0531")); //$NON-NLS-1$
}
AttachNotSupportedException lastException = null;
/*[PR CMVC 182802 ]*/
Expand Down Expand Up @@ -187,7 +189,7 @@ public synchronized void detach() throws IOException {
public Properties getAgentProperties() throws IOException {
if (!targetAttached) {
/*[MSG "K0544", "Target not attached"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
throw new IOException(getString("K0544")); //$NON-NLS-1$
}
Properties props = getTargetProperties(false);

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

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

if (!targetAttached) {
/*[MSG "K0544", "Target not attached"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
throw new IOException(getString("K0544")); //$NON-NLS-1$
}
AttachmentConnection.streamSend(commandStream, createLoadAgentLibrary(
agentLibrary, options, false));
Expand All @@ -253,11 +255,11 @@ public synchronized void loadAgentPath(String agentPath, String options)
IOException {
if (null == agentPath) {
/*[MSG "K0577", "loadAgentPath: null agent path"]*/
throw new AgentLoadException(com.ibm.oti.util.Msg.getString("K0577")); //$NON-NLS-1$
throw new AgentLoadException(getString("K0577")); //$NON-NLS-1$
}
if (!targetAttached) {
/*[MSG "K0544", "Target not attached"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
throw new IOException(getString("K0544")); //$NON-NLS-1$
}
AttachmentConnection.streamSend(commandStream, createLoadAgentLibrary(agentPath,
options, true));
Expand Down Expand Up @@ -318,7 +320,7 @@ private static boolean parseResponse(String response) throws IOException,
}
if (response.contains(EXCEPTION_IOEXCEPTION)) {
/*[MSG "K0576","IOException from target: {0}"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0576", trimmedResponse)); //$NON-NLS-1$
throw new IOException(getString("K0576", trimmedResponse)); //$NON-NLS-1$
} else if (response.contains(EXCEPTION_AGENT_INITIALIZATION_EXCEPTION)) {
Integer status = getStatusValue(trimmedResponse);
if (null == status) {
Expand All @@ -330,13 +332,13 @@ private static boolean parseResponse(String response) throws IOException,
throw new AgentLoadException(trimmedResponse);
} else if (response.contains(EXCEPTION_IOEXCEPTION)) {
/*[MSG "K0576","IOException from target: {0}"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0576", trimmedResponse)); //$NON-NLS-1$
throw new IOException(getString("K0576", trimmedResponse)); //$NON-NLS-1$
} else if (response.contains(EXCEPTION_ILLEGAL_ARGUMENT_EXCEPTION)) {
/*[MSG "K05de","IllegalArgumentException from target: {0}"]*/
throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString("K05de", trimmedResponse)); //$NON-NLS-1$
throw new IllegalArgumentException(getString("K05de", trimmedResponse)); //$NON-NLS-1$
} else if (response.contains(EXCEPTION_ATTACH_OPERATION_FAILED_EXCEPTION)) {
/*[MSG "k05dc","AttachOperationFailedException from target: {0}"]*/
throw new AttachOperationFailedException(com.ibm.oti.util.Msg.getString("k05dc", trimmedResponse)); //$NON-NLS-1$
throw new AttachOperationFailedException(getString("k05dc", trimmedResponse)); //$NON-NLS-1$
}
return false;
} else if (response.startsWith(ACK) || response.startsWith(ATTACH_RESULT)) {
Expand Down Expand Up @@ -397,18 +399,24 @@ private void tryAttachTarget(int timeout) throws IOException,
* to attach
*/
/*[MSG "K0457", "Target no longer available"]*/
AttachNotSupportedException exc = new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0457")); //$NON-NLS-1$
AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0457")); //$NON-NLS-1$
exc.initCause(e);
throw exc;
}

if (descriptor.id().equals(AttachHandler.getVmId())) {
String allowAttachSelf_Value = AttachHandler.allowAttachSelf;
boolean selfAttachAllowed = "".equals(allowAttachSelf_Value) || Boolean.parseBoolean(allowAttachSelf_Value); //$NON-NLS-1$
if (!selfAttachAllowed) {
/*[MSG "K0646", "Late attach connection to self disabled. Set jdk.attach.allowAttachSelf=true"]*/
throw new IOException(getString("K0646")); //$NON-NLS-1$
}
/* I am connecting to myself: bypass the notification and launch the attachment thread directly */
if (AttachHandler.isAttachApiInitialized()) {
AttachHandler.getMainHandler().connectToAttacher();
} else {
/*[MSG "K0558", "Attach API initialization failed"]*/
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0558")); //$NON-NLS-1$
throw new AttachNotSupportedException(getString("K0558")); //$NON-NLS-1$
}
} else {
lockAllAttachNotificationSyncFiles(vmds);
Expand All @@ -417,7 +425,7 @@ private void tryAttachTarget(int timeout) throws IOException,
/*[MSG "K0532", "status={0}"]*/
if ((IPC.JNI_OK != status)
&& (CommonDirectory.J9PORT_INFO_SHSEM_OPENED_STALE != status)) {
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0532", status)); //$NON-NLS-1$
throw new AttachNotSupportedException(getString("K0532", status)); //$NON-NLS-1$
}
}

Expand All @@ -429,7 +437,7 @@ private void tryAttachTarget(int timeout) throws IOException,
targetServer.close();
IPC.logMessage("attachTarget SocketTimeoutException on " + portNumber + " to " + targetId); //$NON-NLS-1$ //$NON-NLS-2$
/*[MSG "K0539","acknowledgement timeout from {0} on port {1}"]*/
AttachNotSupportedException exc = new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0539", targetId, portNumber)); //$NON-NLS-1$
AttachNotSupportedException exc = new AttachNotSupportedException(getString("K0539", targetId, portNumber)); //$NON-NLS-1$
exc.initCause(e);
throw exc;
}
Expand All @@ -443,7 +451,7 @@ private void tryAttachTarget(int timeout) throws IOException,
String response = AttachmentConnection.streamReceiveString(responseStream, ATTACH_CONNECTED_MESSAGE_LENGTH_LIMIT);
/*[MSG "K0533", "key error: {0}"]*/
if (!response.contains(' ' + key + ' ')) {
throw new AttachNotSupportedException(com.ibm.oti.util.Msg.getString("K0533", response)); //$NON-NLS-1$
throw new AttachNotSupportedException(getString("K0533", response)); //$NON-NLS-1$
}
IPC.logMessage("attachTarget connected on ", portNumber.toString()); //$NON-NLS-1$
targetAttached = true;
Expand Down Expand Up @@ -493,7 +501,7 @@ public void startManagementAgent(Properties agentProperties)
throws IOException, IllegalArgumentException, AttachOperationFailedException {
if (!targetAttached) {
/*[MSG "K0544", "Target not attached"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
throw new IOException(getString("K0544")); //$NON-NLS-1$
} else if (null == agentProperties) {
throw new NullPointerException();
}
Expand All @@ -516,7 +524,7 @@ public void startManagementAgent(Properties agentProperties)
public String startLocalManagementAgent() throws IOException {
if (!targetAttached) {
/*[MSG "K0544", "Target not attached"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("K0544")); //$NON-NLS-1$
throw new IOException(getString("K0544")); //$NON-NLS-1$
}
AttachmentConnection.streamSend(commandStream, Command.START_LOCAL_MANAGEMENT_AGENT);
String response = AttachmentConnection.streamReceiveString(responseStream);
Expand All @@ -525,14 +533,14 @@ public String startLocalManagementAgent() throws IOException {
try {
if (!parseResponse(response)) {
/*[MSG "k05dd", "unrecognized response: {0}"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("k05dd", response)); //$NON-NLS-1$
throw new IOException(getString("k05dd", response)); //$NON-NLS-1$
} else if (response.startsWith(ACK)) { /* this came from a legacy VM with dummy start*Agent()s */
result = response.substring(ACK.length());
} else if (response.startsWith(ATTACH_RESULT)) {
result = response.substring(ATTACH_RESULT.length());
} else {
/*[MSG "k05dd", "unrecognized response: {0}"]*/
throw new IOException(com.ibm.oti.util.Msg.getString("k05dd", response)); //$NON-NLS-1$
throw new IOException(getString("k05dd", response)); //$NON-NLS-1$
}
} catch (AgentLoadException | IllegalArgumentException | AgentInitializationException e) {
IPC.logMessage("Unexpected exception " + e + " in startLocalManagementAgent"); //$NON-NLS-1$//$NON-NLS-2$
Expand Down
3 changes: 3 additions & 0 deletions test/Java8andUp/playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED \
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
-Dcom.ibm.tools.attach.enable=yes \
-Djdk.attach.allowAttachSelf=true \
-Dcom.ibm.tools.attach.timeout=15000 \
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames AttachAPISanity \
Expand Down Expand Up @@ -276,6 +277,7 @@
--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED \
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
-Dcom.ibm.tools.attach.enable=yes \
-Djdk.attach.allowAttachSelf=true \
-Dcom.ibm.tools.attach.timeout=15000 \
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestAttachAPIEnabling \
Expand Down Expand Up @@ -365,6 +367,7 @@
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
-Dcom.ibm.tools.attach.enable=yes \
-Dcom.ibm.tools.attach.timeout=15000 \
-Djdk.attach.allowAttachSelf=true \
-Djava.sidecar="--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED" \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestSunAttachClasses \
-groups $(TEST_GROUP) \
Expand Down
Loading