Skip to content
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

Disable late attach to self by default #158

Merged
merged 1 commit into from
Nov 21, 2017

Conversation

pdbain-ibm
Copy link
Contributor

@pdbain-ibm pdbain-ibm commented Sep 27, 2017

This addresses openj9 issue #126
fixes #126

Prevent a JVM from late attaching to itself unless the system property
"jdk.attach.allowAttachSelf" is set either to "true" or an empty string, per
the reference implementation. Enforce this in Java 9 only. Update existing
tests to enable this feature by default so they will continue to run. Add new
Java 9 tests to exercise this feature.

Signed-off-by: Peter Bain [email protected]

@pdbain-ibm pdbain-ibm changed the title Disable late attach to self by default WIP Disable late attach to self by default Sep 27, 2017
@pdbain-ibm pdbain-ibm changed the title WIP Disable late attach to self by default Disable late attach to self by default Sep 27, 2017
@pdbain-ibm
Copy link
Contributor Author

@DanHeidinga do you have any comments further to those in pull request #18?

* but parse it lazily because we rarely need the value.
*/
public final static String allowAttachSelf =
com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties().getProperty("jdk.attach.allowAttachSelf"); //$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.

Is there a reason not to import com.ibm.oti.vm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Historical reasons. Fixed.
Also cleaned up warnings on Java 5-era code.

* @param vmArgs VM arguments
* @param appArgs Application arguments
*/
public TargetManager(String cmdName, String targetId, String displayName,
Copy link
Member

Choose a reason for hiding this comment

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

For new code, please don't wrap at ~80 chars. The code is way more readable with longer lines then with arbitrary breaks to limit line length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code was cloned from the old Target manager, but fixed anyway.

* This test must be invoked via testng. Running the main method will return only the status of the attach API.
*
*/
@SuppressWarnings({"nls","boxing"})
Copy link
Member

Choose a reason for hiding this comment

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

Please address the NLS and boxing warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Missed that one.

/**
* Reads any available lines of text from a stream without blocking,
* except to find the terminating newline.
* This assumes the producer emits complete lines
Copy link
Member

Choose a reason for hiding this comment

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

ready() returns true as soon as a read() operation won't block - i.e.: a single character is available. This comment makes assumptions that aren't guaranteed by the Class libraries.

The usual way to do this is with separate threads that read from the buffered reader and append lines to some shared data structure.

Is there something I'm missing here?

Copy link
Member

Choose a reason for hiding this comment

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

Discussed deskside with Peter. This isn't new code per se, its moving old code to a new location. I'm willing to live it, especially as it's functioned for the past ~8 years.

We should take a hard look at some of this code when the Java 8 vs 9 vs next test code is rationalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For new code, I agree with the suggestion. However, as discussed, this is cloned legacy code which we have been running for ~9 years. Rewriting it now would add risk to this changeset.

I would be happy to re-do this code (and the Java8AndUp TargetManager) if you would open an issue.

@pdbain-ibm pdbain-ibm force-pushed the attach_self branch 3 times, most recently from 8920243 to f490329 Compare September 29, 2017 15:15
@pdbain-ibm
Copy link
Contributor Author

Changeset updated.

@pdbain-ibm
Copy link
Contributor Author

Are any further changes required?

@DanHeidinga
Copy link
Member

I'm at JavaOne this week and likely won't get a chance to finish the review until next week. I have no objections to one of the other committers completing the review / merge.

@pdbain-ibm
Copy link
Contributor Author

Are any further changes required?

@pdbain-ibm
Copy link
Contributor Author

@DanHeidinga Do you have any other comments on this pull request?

@@ -50,7 +52,7 @@
/**
* 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.

@@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2015, 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

As this is new code the start date should be 2017

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -0,0 +1,103 @@
/*******************************************************************************
* Copyright (c) 2001, 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

As this is new code the start date should be 2017.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -0,0 +1,30 @@
/* Copyright (c) 2015, 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

As this is new code the start date should 2017.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -0,0 +1,30 @@
/* Copyright (c) 2015, 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

As this is new code the start date should 2017.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -0,0 +1,412 @@
/*******************************************************************************
* Copyright (c) 2001, 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

As this is new code the start date should be 2017.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code is also Distributed under one or more Secondary Licenses,
Copy link
Member

Choose a reason for hiding this comment

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

These new files all have the incorrect license.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Let me know if I missed any.

logger.debug("javaExec="+javaExec);

/* These are the VM options to the parent process, which we wish to pass on to the child. */
String sideCar = System.getProperty("java.sidecar");
Copy link
Member

@pshipton pshipton Nov 9, 2017

Choose a reason for hiding this comment

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

"java.sidecar" is obsolete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this is set by the playlist to indicate VM settings required by child processes (see test/Java8andUp/playlist.xml). Should I use a different system property?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I though it was a reference to something old and obsolete.

@@ -102,10 +110,20 @@
private static String nameProperty;
private static String pidProperty;
private static int numberOfTargets;
/*[IF Sidecar19-SE]*/
Copy link
Member

Choose a reason for hiding this comment

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

Might be useful to add this code to Java 8 as well, but making the default true instead of false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done

@pdbain-ibm pdbain-ibm force-pushed the attach_self branch 2 times, most recently from d59dfed to db3c6cb Compare November 10, 2017 14:10
@@ -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.

/*******************************************************************************
* Copyright (c) 2017 IBM Corp. and others
*
* This program and the accompanying materials are made available under
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 all the licenses to be the lastest version.

Copy link
Member

Choose a reason for hiding this comment

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

It appears the middle part of the licenses was updated, but not the ending part.

@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corp. and others
Copy link
Member

Choose a reason for hiding this comment

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

Please use 2017, 2017 in order to pass the copyright check.

@pshipton
Copy link
Member

@llxia any concerns with the test code?

@pdbain-ibm
Copy link
Contributor Author

@llxia or @sophia-guo Further to @pshipton 's comment above, is there a way I can share utility classes between the Java8AndUp and Java9AndUp projects? That would simplify this PR considerably. Thanks

@pdbain-ibm
Copy link
Contributor Author

Copyrights and NLS message updated.

@pdbain-ibm
Copy link
Contributor Author

Further to discussion with @smlambert et al, I will create a test utilities directory (under s separate pull request) to hold common code. When that is complete, this pull request will be updated to use utilities in the common directory.

@pdbain-ibm
Copy link
Contributor Author

Pull request updated:

  • removed the duplicate TargetManager and used the common copy in TestUtilities
  • Renamed the test class and changed its package for compatibility with the Java 8 test code.
    Otherwise it's the same.
    @llxia could you please review the test code?

Thanks

@smlambert smlambert self-requested a review November 17, 2017 13:54
public static void main(String[] args) {
try {
String myId = VmIdGetter.getVmId();
System.err.println("myId="+myId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please avoid stray system.out / system.err calls in test code and use logger instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The SelfAttacher is actually a child process. It uses stderr to communicate its process ID (and hence VM ID) to the target manager. I will add a comment to that effect.

e.printStackTrace();
System.exit(ATTACH_ERROR_CODE);
}
System.exit(ATTACH_SELF_API_SUCCEEDED_CODE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not think the System.exit calls are appropriate above, assert is likely what you should be doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use the exit code to indicate the result of the late attach attempt to the parent test process, i.e.

			int status = target.waitFor();
			assertEquals(status, ATTACH_SELF_API_SUCCEEDED_CODE, "wrong exit status");
Again, will add a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

I should have stepped back and viewed the entire suite of attach API tests to see. Comments will be useful thanks.

}

@Test
public void testAllowAttachSelfDisnable() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Disable vs Disnable in method name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Thanks for catching that.

Arrays.asList(new String[] {ATTACH_ENABLE_PROPERTY, ATTACH_SELF_ENABLE_PROPERTY+"=false"}), null);
try {
int status = target.waitFor();
assertEquals(status, ATTACH_SELF_IOEXCEPTION_CODE, "wrong exit status");
Copy link
Contributor

Choose a reason for hiding this comment

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

An opportunity for a better error message that wrong exit status (include exit status in the message would aid triage).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

assertEquals() already does that, e.g.
wrong exit status expected [2] but found [1]

*******************************************************************************/
package org.openj9.test.attachAPI;
/* This class is separated from the main test because we need two
* copies to accommodate old and new versions of ProcessHandle
Copy link
Contributor

Choose a reason for hiding this comment

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

@pshipton src_current is for supporting old class library version, which I thought we were trying to move from, instead of build up more test/support for. Any guidance on this, why we keep adding more test material into this effort?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs to work on the vmfarm builds, which are stuck on an old (incompatible) class library.

Copy link
Member

Choose a reason for hiding this comment

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

This is adding a new test to IBM internal Java 9 b148 builds, correct? I agree its not necessary to add new testing. Existing testing either needs to be fixed or removed to keep the builds green. The b148 builds are obsolete and will be deleted once the equivalent testing is running on OpenJ9.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed with the FV team, the tooling doesn't allow us to add the test only for the "latest" (i.e. post b148) JCL, so we will need to maintain 2 copies of some classes (as is done with the StackWalker tests) until we can kill b148.

public static void main(String[] args) {
try {
String myId = VmIdGetter.getVmId();
System.err.println("myId="+myId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we should logger here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, but no, see above.

return myId;
}

}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should put this file in test/Java9andUp/src to avoid duplication in both ssrc_latset and src_current.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly no. the ProcessHandle API changed from getPid() to pid();

@pdbain-ibm
Copy link
Contributor Author

Testing changes now. Stay tuned...

@pdbain-ibm pdbain-ibm force-pushed the attach_self branch 5 times, most recently from adbcb58 to 5f3b64e Compare November 17, 2017 20:52
This addresses openj9 issue eclipse-openj9#126

Prevent a JVM from late attaching to itself unless the system property
"jdk.attach.allowAttachSelf" is set either to "true" or an empty string, per
the reference implementation. Enforce this in Java 9 only. Update existing
tests to enable this feature by default so they will continue to run. Add new
Java 9 tests to exercise this feature.

Signed-off-by: Peter Bain <[email protected]>
@pdbain-ibm
Copy link
Contributor Author

Personal build passed. @smlambert any more concerns?
Thanks

@smlambert
Copy link
Contributor

Jenkins test extended

@pdbain
Copy link

pdbain commented Nov 21, 2017

failed tests due to known unrelated issue: org.openj9.test.vmArguments.VmArgumentTests.testCrNocr
#479

@smlambert smlambert merged commit b77c5b3 into eclipse-openj9:master Nov 21, 2017
tajila pushed a commit to tajila/openj9 that referenced this pull request Jun 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attach API: prevent Java VM from attaching to the current VM
6 participants