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

8311971: SA's ConstantPool.java uses incorrect computation to read long value in the constant pool #14855

Closed

Conversation

ashu-mehra
Copy link
Contributor

@ashu-mehra ashu-mehra commented Jul 12, 2023

Please review this fix to correctly read a long value in the runtime constant pool. Details are mentioned in the issue [0].
As an example, before this fix the long value generated by SA's dumpclass for java.lang.String.serialVersionUID was:

  private static final long serialVersionUID;
    descriptor: J
    flags: (0x001a) ACC_PRIVATE, ACC_STATIC, ACC_FINAL
    ConstantValue: long 2050732866l

After this fix value of java.lang.String.serialVersionUID is:

  private static final long serialVersionUID;
    descriptor: J
    flags: (0x001a) ACC_PRIVATE, ACC_STATIC, ACC_FINAL
    ConstantValue: long -6849794470754667710l

Correct value as obtained from original java.lang.String is -6849794470754667710l.

Testing: tests under serviceability/sa and sun/tools/jhsdb are passing.

[0] https://bugs.openjdk.org/browse/JDK-8311971


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8311971: SA's ConstantPool.java uses incorrect computation to read long value in the constant pool (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/14855/head:pull/14855
$ git checkout pull/14855

Update a local copy of the PR:
$ git checkout pull/14855
$ git pull https://git.openjdk.org/jdk.git pull/14855/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 14855

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/14855.diff

Webrev

Link to Webrev Comment

…ng value in the constant pool

Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 12, 2023

👋 Welcome back ashu-mehra! 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.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 12, 2023
@openjdk
Copy link

openjdk bot commented Jul 12, 2023

@ashu-mehra The following labels will be automatically applied to this pull request:

  • hotspot-gc
  • serviceability

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.

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org hotspot-gc hotspot-gc-dev@openjdk.org labels Jul 12, 2023
@mlbridge
Copy link

mlbridge bot commented Jul 12, 2023

Webrevs

@ashu-mehra
Copy link
Contributor Author

/label remove hotspot-gc

@openjdk openjdk bot removed the hotspot-gc hotspot-gc-dev@openjdk.org label Jul 12, 2023
@openjdk
Copy link

openjdk bot commented Jul 12, 2023

@ashu-mehra
The hotspot-gc label was successfully removed.

Copy link
Contributor

@plummercj plummercj left a comment

Choose a reason for hiding this comment

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

Please make sure all serviceability/sa and sun/tools/jhsdb tests are run.

@plummercj
Copy link
Contributor

/reviewers 2

@openjdk
Copy link

openjdk bot commented Jul 12, 2023

@ashu-mehra This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8311971: SA's ConstantPool.java uses incorrect computation to read long value in the constant pool

Reviewed-by: cjplummer, dholmes, stuefe

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 58 new commits pushed to the master branch:

  • 1fc726a: 8312163: Crash in dominance check when compiling unnamed patterns
  • b20dc1e: 8310629: java/security/cert/CertPathValidator/OCSP/OCSPTimeout.java fails with RuntimeException Server not ready
  • 4b9ec82: 8310355: Move the stub test from initialize_final_stubs() to test/hotspot/gtest
  • 8ec136e: 8312072: Deprecate for removal the -Xnoagent option
  • fbe51e3: 8312127: FileDescriptor.sync should temporarily increase parallelism
  • 201e3bc: 8291065: Creating a VarHandle for a static field triggers class initialization
  • a53345a: 8287061: Support for rematerializing scalar replaced objects participating in allocation merges
  • 3236ba0: 8312189: ProblemList serviceability/jvmti/vthread/VThreadTLSTest/VThreadTLSTest.java#id1
  • 5cc71f8: 8312089: Simplify and modernize equals, hashCode, and compareTo in java.nio and implementation code
  • 6a09992: 8311968: Clarify Three-letter time zone IDs in java.util.TimeZone
  • ... and 48 more: https://git.openjdk.org/jdk/compare/119cc495fc6c18a29b7484d294c31ad1d478791c...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@plummercj, @dholmes-ora, @tstuefe) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 12, 2023
@openjdk
Copy link

openjdk bot commented Jul 12, 2023

@plummercj
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jul 12, 2023
@ashu-mehra
Copy link
Contributor Author

All tests under serviceability/sa and sun/tools/jhsdb are passing.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

This raises a few questions for me.

First, what is it about constructing the long from two ints that is incorrect?

Second, the fact we have VM.buildLongFromIntsPD suggests this is the intended way to do things. Why do we also have Address.getJLongAt()? Do we not actually need VM.buildLongFromIntsPD? Is its other use in the code in ./jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/StackValueCollection.java also incorrect?

And third, how can it be that we seemingly have no test for this???

@ashu-mehra
Copy link
Contributor Author

First, what is it about constructing the long from two ints that is incorrect?

The incorrect part is fetching the ints from index and index+1. This doesn't work for 64-bit platforms because the single entry in runtime constant pool is large enough to store the long value, so the entry at index+1 is not used and is invalid.

In ClassFileParser::parse_constant_pool_entries():

  case JVM_CONSTANT_Long: {
    // some code
    const u8 bytes = cfs->get_u8_fast();
    cp->long_at_put(index, bytes);
    index++;   // Skip entry following eigth-byte constant, see JVM book p. 98

The existing code that uses VM.buildLongFromIntsPD() would have worked if each constant pool entry is of 4 bytes. So 32-bit systems it wouldn't be a problem.
From my understanding getJLongAt() should work for both 32 and 64 bit systems.

the fact we have VM.buildLongFromIntsPD suggests this is the intended way to do things. Why do we also have Address.getJLongAt()? Do we not actually need VM.buildLongFromIntsPD?

It seems both would produce the same result i.e. getJLongAt(0x1000) == VM.buildLongFromIntsPD(getIntAt(0x1004), getIntAt(0x1000));

Is its other use in the code in ./jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/StackValueCollection.java also incorrect?

Not sure about that. Looking at the code for StackValueCollection.longAt() it would depend on whether any value is stored in slot+1. If the slots in the stack behave the same as the runtime constant pool, then it might be incorrect.
Also there are no users of StackValueCollection.longAt() method, so its a dead code as of now.

how can it be that we seemingly have no test for this???

As per my knowledge we have very basic tests that just verify that the classfile generated by SA is parseable by javap. There is not test that checks for equality of each individual component of the classfile.
I have a test [0] that can be a good proxy for ensuring the generated classfiles are in a use-able shape because this test has helped me in uncovering quite a few issues including this one and another one in handling of invokedynamic bytecodes which is being fixed here #14852 (although it was reported through another channel).

[0] https://bugs.openjdk.org/browse/JDK-8311101

@tstuefe
Copy link
Member

tstuefe commented Jul 14, 2023

@dholmes-ora Could this code precede the 64-bit version of java? It would certainly make sense for 32-bit when a 64-bit value needs to be cobbled together by reading two slots. The function precedes the initial OpenJDK load, so I cannot check.

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

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

Patch is correct. Tested manually, works and fixes both long constants and double constants.

AFAICT the problem only shows up for values > 32-bit, at least on little endian machines, that may have been the reason it had not been discovered before. Still, odd, for such a long time. We really should have better tests for that.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 14, 2023
@dholmes-ora
Copy link
Member

@tstuefe I don't think this pre-dates 64-bit as the change was made in August 2002. The irony here is that the new code in the PR is what we originally had in August 2002 until we had to fix a "jlong unaligned access" issue! At that point I think someone simply got confused about how classfile parsing needs to interpret the CONSTANT_Long_info when filling out the constant pool entry, versus reading back that existing CP entry.

As noted above this will only fail on little-endian when the constant is larger than 32-bits, and if the unused slot had a non-zero value. Anyway still extremely surprising that this error has never been noticed. I have to wonder when/where this code actually gets used in practice?

I think we need a follow-up RFE to get rid of buildLongFromIntsPD and any other dead code.

Thanks.

@ashu-mehra
Copy link
Contributor Author

I think we need a follow-up RFE to get rid of buildLongFromIntsPD and any other dead code.

okay.

@dholmes-ora can you please approve it if there are no other concerns.

@tstuefe
Copy link
Member

tstuefe commented Jul 17, 2023

@tstuefe I don't think this pre-dates 64-bit as the change was made in August 2002. The irony here is that the new code in the PR is what we originally had in August 2002 until we had to fix a "jlong unaligned access" issue! At that point I think someone simply got confused about how classfile parsing needs to interpret the CONSTANT_Long_info when filling out the constant pool entry, versus reading back that existing CP entry.

That sounds likely. Pity we don't have the history prior to opensourcing the JDK.

Thanks, Thomas

@dholmes-ora
Copy link
Member

Approved, but please file that follow up issue.

@ashu-mehra
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jul 18, 2023
@openjdk
Copy link

openjdk bot commented Jul 18, 2023

@ashu-mehra
Your change (at version 9fd8efc) is now ready to be sponsored by a Committer.

@ashu-mehra
Copy link
Contributor Author

Approved, but please file that follow up issue.

Follow up issue for removing the dead code - https://bugs.openjdk.org/browse/JDK-8312232

@tstuefe
Copy link
Member

tstuefe commented Jul 19, 2023

/sponsor

@openjdk
Copy link

openjdk bot commented Jul 19, 2023

Going to push as commit c119037.
Since your change was applied there have been 85 commits pushed to the master branch:

  • 028068a: 8312166: (dc) DatagramChannel's socket adaptor does not release carrier thread when blocking in receive
  • e7adbdb: 8311923: TestIRMatching.java fails on RISC-V
  • c6ab9c2: 8308103: Massive (up to ~30x) increase in C2 compilation time since JDK 17
  • d33e8e6: 8312200: Fix Parse::catch_call_exceptions memory leak
  • f677793: 8312190: Fix c++11-narrowing warnings in hotspot code
  • 82612e2: 8312329: Minimal build failure after JDK-8311541
  • 702fea8: 8312147: Dynamic Exception Specification warnings are no longer required after JDK-8311380
  • e5ecbff: 8312203: Improve specification of Array.newInstance
  • c2f421b: 8311541: JavaThread::print_jni_stack doesn't support native stacks on all platforms
  • e31df3a: 6211126: ICC_ColorSpace.toCIEXYZ(float[]): NPE is not specified
  • ... and 75 more: https://git.openjdk.org/jdk/compare/119cc495fc6c18a29b7484d294c31ad1d478791c...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jul 19, 2023
@openjdk openjdk bot closed this Jul 19, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jul 19, 2023
@openjdk
Copy link

openjdk bot commented Jul 19, 2023

@tstuefe @ashu-mehra Pushed as commit c119037.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@ashu-mehra ashu-mehra deleted the 8311971-incorrect-long-value branch July 24, 2023 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated serviceability serviceability-dev@openjdk.org
4 participants