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

JDK-8273914: Indy string concat changes order of operations #5844

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

@cushon
Copy link
Contributor

@cushon cushon commented Oct 6, 2021

This change makes string concatenation call toString on the arguments eagerly, to preserve the correct evaluation order of the arguments and the calls to toString.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Integration blocker

 ⚠️ The change requires a CSR request to be approved.

Issue

  • JDK-8273914: Indy string concat changes order of operations

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5844/head:pull/5844
$ git checkout pull/5844

Update a local copy of the PR:
$ git checkout pull/5844
$ git pull https://git.openjdk.java.net/jdk pull/5844/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 5844

View PR using the GUI difftool:
$ git pr show -t 5844

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5844.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Oct 6, 2021

👋 Welcome back cushon! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

Loading

@openjdk openjdk bot added the rfr label Oct 6, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Oct 6, 2021

@cushon The following labels will be automatically applied to this pull request:

  • compiler
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

Loading

@mlbridge
Copy link

@mlbridge mlbridge bot commented Oct 6, 2021

Loading

* Returns true if the argument should be converted to a string eagerly, to preserve
* possible side-effects.
*/
protected boolean shouldConvertToStringEagerly(Type argType) {
Copy link
Member

@forax forax Oct 6, 2021

Choose a reason for hiding this comment

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

For me the implementation should be neither a primitive type nor a final class of java.lang. I think that at least the wrappers should not be converted eagerly

Loading

Copy link
Contributor Author

@cushon cushon Oct 6, 2021

Choose a reason for hiding this comment

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

@forax can you expand on the suggestion here?

The current implementation is not eagerly converting boxes for primitives types, which wrappers should not be converted eagerly?

Also note that one of the motivating examples was StringBuilder, which is a final class in java.lang. It's not just about toString() not having side-effects, it's also about insulting the operands from each others's side effects (e.g. myStringBuilder.append("foo") + myStringBuilder.append("bar"))

Loading

Copy link
Member

@forax forax Oct 7, 2021

Choose a reason for hiding this comment

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

The current implementation is not eagerly converting boxes for primitives types, which wrappers should not be converted eagerly?

I was just thinking that not calling Boolean or Double.toString() explicitly was Ok

myStringBuilder.append("foo") + myStringBuilder.append("bar")

Aaaah, so only primitives + their boxes should not be evaluated eagerly.

Loading

@jddarcy
Copy link
Member

@jddarcy jddarcy commented Oct 6, 2021

/csr needed

Loading

@jddarcy
Copy link
Member

@jddarcy jddarcy commented Oct 6, 2021

Please file a CSR for this issues to discuss the behavioral changes, even if to fix a bug.

Loading

@openjdk openjdk bot added the csr label Oct 6, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Oct 6, 2021

@jddarcy has indicated that a compatibility and specification (CSR) request is needed for this pull request.
@cushon please create a CSR request for issue JDK-8273914. This pull request cannot be integrated until the CSR request is approved.

Loading

@cushon cushon changed the title JDK-8273914: Indy string concat changes order of operations. JDK-8273914: Indy string concat changes order of operations Oct 6, 2021
@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 6, 2021

Loading

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks,
David

Loading

Copy link
Contributor

@shipilev shipilev left a comment

Thank you for fixing this. It looks nominally fine, but I would defer to javac experts to approve this. Minor testing suggestions below.

Loading

* @test
* @bug 8273914
* @summary Indy string concat changes order of operations
*
Copy link
Contributor

@shipilev shipilev Oct 7, 2021

Choose a reason for hiding this comment

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

Here and later: please add the test block with -XDstringConcat=inline here as well, so that test would verify that every javac strategy produces the same result?

Loading

Copy link
Contributor Author

@cushon cushon Oct 7, 2021

Choose a reason for hiding this comment

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

Done, thanks

Loading

Copy link
Contributor Author

@cushon cushon Oct 7, 2021

Choose a reason for hiding this comment

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

(The WellKnownTypes test still only exercises the indy strategies, because it's testing logic that only exists in those strategies.)

Loading

Copy link
Contributor

@shipilev shipilev Oct 12, 2021

Choose a reason for hiding this comment

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

But still, we probably want to confirm that inline strategy yields the same answer in WellKnownTypes? That's my thinking why to test inline: that is a baseline.

Loading

Copy link
Contributor Author

@cushon cushon Oct 12, 2021

Choose a reason for hiding this comment

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

I agree in general about using inline as a baseline, but WellKnownTypes doesn't currently check the output of string concatenation, it checks the signature of the invokedynamic it find in the test case.

What do you have in mind for inline here? Should I update the test to assert on the output of string concatenation (and tolerate not being able to find an invokedynamic, which could make the coverage for the indy strategies more fragile)? Or add a different test with similar inputs, and check the output of string concatenation on primitives and boxes?

I think it might make sense to rely on other tests to ensure inline and the indy strategies produce the same results, and use WellKnownTypes as a test just for shouldConvertToStringEagerly, which is only used by the indy strategies.

Loading

Copy link
Contributor

@shipilev shipilev Oct 14, 2021

Choose a reason for hiding this comment

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

Ah, I missed that WellKnownTypes does not verify concat output. Yes, that resolves my concern, we don't need to handle inline there. Maybe rename WellKnownTypes to WellKnownTypeSignatures or something?

But yes, I think adding another test that verifies the "special" input types produce consistent results across all strategies would be useful.

Loading

Copy link
Contributor Author

@cushon cushon Oct 14, 2021

Choose a reason for hiding this comment

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

Sure, thanks--I renamed WellKnownTypes to WellKnownTypeSignatures, and added a new WellKnownTypes that tests the actual concat behaviour on primitives for all of the strategies.

Loading

Copy link
Contributor

@shipilev shipilev Oct 15, 2021

Choose a reason for hiding this comment

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

Thank you, the last change resolves my comments.

Loading

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 7, 2021

@dholmes-ora

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks, good question:

That test is for JDK-8174954, which checks for an expected IllegalAccessError when the parameter type of an invokedynamic is inaccessible. It's assuming that given "" + param, javac generates an invokedynamic that uses the specific type of param. This change make javac eagerly convert param to a String before passing it to the invokedynamic call, which avoids the accessibility issue the test is trying to exercise.

Using jasm preserves the existing bytecode (including the signature of the invokedynamic) that the test is expecting.

Loading

@mlbridge
Copy link

@mlbridge mlbridge bot commented Oct 8, 2021

Mailing list message from David Holmes on hotspot-runtime-dev:

On 8/10/2021 6:02 am, Liam Miller-Cushon wrote:

On Thu, 7 Oct 2021 01:20:04 GMT, David Holmes <dholmes at openjdk.org> wrote:

Liam Miller-Cushon has updated the pull request incrementally with two additional commits since the last revision:

- Also test evaluation order for 'inline'
- Remove sharpestAccessible logic, since types are now passed as strings

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks,
David

@dholmes-ora

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks, good question:

That test is for [JDK-8174954](https://bugs.openjdk.java.net/browse/JDK-8174954), which checks for an expected `IllegalAccessError` when the parameter type of an invokedynamic is inaccessible. It's assuming that given `"" + param`, javac generates an invokedynamic that uses the specific type of `param`. This change make javac eagerly convert `param` to a `String` before passing it to the `invokedynamic` call, which avoids the accessibility issue the test is trying to exercise.

Using `jasm` preserves the existing bytecode (including the signature of the `invokedynamic`) that the test is expecting.

Okay, can you please add a comment to the jasm file that explains that.
It is useful to know what Java code a jasm file represents and where/why
it differs from how that Java code would compile.

Thanks,
David
-----

Loading

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 8, 2021

Okay, can you please add a comment to the jasm file that explains that.

Done

Loading

@shipilev
Copy link
Contributor

@shipilev shipilev commented Oct 12, 2021

@lahodaj, want to review the compiler change?

Loading

@lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Oct 12, 2021

How about code like:

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

Loading

@shipilev
Copy link
Contributor

@shipilev shipilev commented Oct 12, 2021

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

Tried it, still not correct:

$ cat Concat.java 
public class Concat {
	public static void main(String... args) {
	        StringBuilder builder2 = new StringBuilder("foo");
	        Object oo = builder2;
        	oo += "" + builder2.append("bar");
		System.out.println(oo);
	}
}

$ build/linux-x86_64-server-fastdebug/images/jdk/bin/javac Concat.java
$ build/linux-x86_64-server-fastdebug/images/jdk/bin/java Concat
foobarfoobar

I believe if (shouldConvertToStringEagerly(argType)) branch should be handled for first argument as well, i.e. code should be shaped as:

if (!first || generateFirstArg) {
    genExpr(arg, arg.type).load();
}
if (shouldConvertToStringEagerly(argType)) {
    gen.callMethod(pos, syms.stringType, names.valueOf, List.of(syms.objectType), true);
    argType = syms.stringType;
}
dynamicArgs.add(argType);

This produces the correct result:

$ build/linux-x86_64-server-fastdebug/images/jdk/bin/java Concat
foofoobar

@cushon, could you do this change and add a relevant test case?

Loading

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 12, 2021

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

Tried it, still not correct:

I believe if (shouldConvertToStringEagerly(argType)) branch should be handled for first argument as well, i.e. code should be shaped as:

@cushon, could you do this change and add a relevant test case?

Fixed, thanks!

Loading

and add more functional test coverage for string concat of well-known
types, covering all strategies.
Copy link
Contributor

@shipilev shipilev left a comment

Current change passes tier{1,2,3,4} for me. Still, I think compiler folks have to approve the javac change. @lahodaj, could you do it, or ask somebody else?

Loading

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 18, 2021

@shipilev this is also blocked on the CSR, I think the next step is for it to be 'reviewed by at least one engineer familiar with that technology area', is that something you'd be able to help with? https://bugs.openjdk.java.net/browse/JDK-8274863

Loading

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Nov 15, 2021

@cushon This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

Loading

@cushon
Copy link
Contributor Author

@cushon cushon commented Nov 15, 2021

This pull request has been inactive for more than 4 weeks

The CSR is still in progress

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
6 participants