Skip to content

Add a diagnostic option that would force System.identityHashCode to produce the same value for all the objects #17309

@vlsi

Description

@vlsi

Typically the values produced by identityHashCode and the default implementation of Object#hashCode are distinct.
Sometimes developers unintentionally assume they are unique. For instance, sometimes they use object.hashCode() as unique keys in maps.

I suggest adding an option that would force OpenJ9 to produce the same value for all the default hashCode values. It would make it easier to surface bugs when developers unintentionally assumed the hashcodes are distinct.

Java language specification allows identityHashCode to produce duplicate results, so there's no harm in that regard.

I do not suggest such a mode to be used in production as it might slow down the runtime.

Hotspot JIT has such "force all default hashcodes to be the same" for a long time with -XX:+UnlockExperimentalVMOptions -XX:hashCode=2.

Sample issues that would be hard to reproduce without "force the same identityHashCode":

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions