Skip to content

8358624: ImmutableDescriptor violates equals/hashCode contract after deserialization - #25758

Closed
kevinjwalls wants to merge 7 commits into
openjdk:masterfrom
kevinjwalls:8358624_ImmutableDescriptor_hashcode
Closed

kevinjwalls wants to merge 7 commits into
openjdk:masterfrom
kevinjwalls:8358624_ImmutableDescriptor_hashcode

Conversation

@kevinjwalls

@kevinjwalls kevinjwalls commented Jun 11, 2025 •

Copy link
Copy Markdown
Contributor

Hashcode needs to be reset to -1 to force its recalculation on next call, after deserialization.

The change in the readResolve() method is the fix for this problem. While here I added similar lines in other methods that may update fields (although these are noted as not supported, as they change a class supposedly "immutable").

Added a test. There is a test for serialization for this class, but I found it clearer to add the test for this specific recently discovered issue in its own test file.


Progress

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

Issue

  • JDK-8358624: ImmutableDescriptor violates equals/hashCode contract after deserialization (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25758

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Jun 11, 2025

Copy link
Copy Markdown

👋 Welcome back kevinw! 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 commented Jun 11, 2025 •

Copy link
Copy Markdown

@kevinjwalls 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:

8358624: ImmutableDescriptor violates equals/hashCode contract after deserialization

Reviewed-by: cjplummer, sspitsyn

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 107 new commits pushed to the master branch:

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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk

openjdk Bot commented Jun 11, 2025

Copy link
Copy Markdown

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

  • jmx
  • 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 jmx jmx-dev@openjdk.org labels Jun 11, 2025
@kevinjwalls
kevinjwalls marked this pull request as ready for review June 11, 2025 15:17
@openjdk openjdk Bot added the rfr Pull request is ready for review label Jun 11, 2025
@mlbridge

mlbridge Bot commented Jun 11, 2025 •

Copy link
Copy Markdown

Webrevs

@plummercj

Copy link
Copy Markdown
Contributor

I'm not much of a serialization expert, so just asking for some clarification here. I assume when de-serialized, the initial hashcode is always 0. Why it is 0 and not by default -1. Seems this would be an issue with any de-serialized type and all would need to explicitly set to -1 as you have done in this case.

@kevinjwalls

Copy link
Copy Markdown
Contributor Author

I'm not much of a serialization expert, so just asking for some clarification here. I assume when de-serialized, the initial hashcode is always 0. Why it is 0 and not by default -1. Seems this would be an issue with any de-serialized type and all would need to explicitly set to -1 as you have done in this case.

As a transient field the hashCode int is not restored by serialization. But why zero, and not -1 as per the field declaration?

The -1 is set by a constructor in ImmutableDescriptor.

ObjectInputStream:
"Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL). No-arg constructors are invoked for the non-serializable classes and then the fields of the serializable classes are restored from the stream starting with the serializable class closest to java.lang.object and finishing with the object's most specific class."

I think that is the explanation: it's all zero'd, and the constructor that would set the -1 doesn't get called.

I see another example (src/java.management/share/classes/javax/management/openmbean/ArrayType.java) where we implement a hashCode member as "private transient Integer myHashCode = null; " There we check if that is null, which causes re-calculating of the hashCode on demand after deserialization. That gets default behaviour of recalculating, but uses a Java object.

This class used a primitive field, I didn't want to change much in this obscure old class.

@plummercj plummercj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Jun 25, 2025
@kevinjwalls

Copy link
Copy Markdown
Contributor Author

Thanks Serguei and Chris for reviewing!

@kevinjwalls

Copy link
Copy Markdown
Contributor Author

/integrate

@openjdk

openjdk Bot commented Jun 27, 2025

Copy link
Copy Markdown

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Jun 27, 2025
@openjdk openjdk Bot closed this Jun 27, 2025
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 27, 2025
@openjdk

openjdk Bot commented Jun 27, 2025

Copy link
Copy Markdown

@kevinjwalls Pushed as commit 12196ba.

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

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 jmx jmx-dev@openjdk.org serviceability serviceability-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

3 participants