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

8303737: C2: Load can bypass subtype check that enforces it's from the right object type #15595

Closed
wants to merge 9 commits into from

Conversation

rwestrel
Copy link
Contributor

@rwestrel rwestrel commented Sep 6, 2023

This patch has 3 test cases:

TestAddPChainMismatchedBase.java
TestAddPChainMismatchedBase2.java

Both fail with a "Base pointers must match" assert

TestLoadBypassesClassCast.java

fails with: assert(Universe::is_in_heap(result)) failed: object not in heap

All failures are related to 8139771 (Eliminating CastPP nodes at Phis
when they all come from a unique input may cause crash). The problem
there was that if a Phi merges say:

(Phi (CastPP in) in)

it was transformed to in. Doing that would cause the control
dependency from the Phi on the null check that guards the CastPP
to be dropped. As demonstrated by the test case of 8139771, a load
could then float above the null check that should have guaranteed the
object being read from was non null.

The fix for 8139771 was to transform:

(Phi (CastPP in) in)

to:

(CastPP in)

That CastPP inherits the type of the Phi and is flagged as a
special kind of cast node so it's not eliminated if the CastPP
doesn't narrow the type of its input. As a result some depending load
cannot float above the cast.

Because that was found to be too conservative, code was added to try
to eliminate cast node added as replacement of a Phi by looking for
a dominating cast with the same input and a narrower type that the
cast. The idea was that if there was some dominating null check then
we would not need the cast. As TestLoadBypassesClassCast.java shows,
this is flawed.

TestLoadBypassesClassCast.java demonstrates that what used to happen
with a null check (a load that bypasses a null check) can still happen
with a type check: in the case of the test, the load of objectField
from o happens before o is checked to be of type A (the holder
of the field). When o is not of type A, the result of the load is
never used by compiled code but if the gc runs while the result of the
load is live (as it does in the test case), gc code sees something
that's not a valid oop and crashes.

In the test case, the same logic is duplicated in 2 branches of
ifs. There are 2 loads of objectField, one in each copy of the
logic. After transformation, the objectField loads lose their
dependency on type checks for A and common above a safepoint causing
a load from something that may not be a A to be live across a
safepoint.

Now looking at one copy of the logic:

  • right above the objectField load, a Phi (that merges the 2
    banches of if (flag) { and has type non null Object) is tested to
    be a subtype of A.

  • the subtype checks is split throught Phi but the CheckCastPP
    stays at the merge point.

  • The Phi is replaced by a CastPP to non null Object.

  • The CastPP is replaced by a dominating CastPP to non null that's
    at the beginning of the method.

  • The if (flag) goes away and the CheckCastPP becomes control
    dependent on the range check for the array load that's above. That
    range check is replaced by a dominating range check, itself at the
    beginning of the method. Now the CheckCastPP is above any subtype
    check. The load can float as high as the CheckCastPP.

The flaw here I think is the logic from 8139771 that assumes a cast
node can be replaced by any cast with a narrower or equal type. In
that case, the cast replaces a Phi that merges types not null A
and not null Object which as a result has type non null Object. It
would be fine to replace the cast with a dominating cast that's
narrower that the type of each input to the Phi (so in this case non
null A and non null Object). That's not the case for the cast of
the null check. The problem is when the Phi is transformed to a
cast, the types of its inputs are lost. What I propose in the fix is
to attach those types to the cast and change the logic that decides
whether a cast is redundant (whether because of the type of its input
or because of a dominating cast) so it goes over the list of attached
types.

How does that relate to the other 2 test cases that fail because in a
chain of Addp nodes, not all nodes have the same base (some casted,
some not)? What I found looking at those test cases is that the graph
contained some casts created as a result of the PhiNode::Ideal that
were "obviously" useless but that are conservatively kept. Those casts
were involved in a chain of transformations that cause the assert. The
new logic does a better job of removing the casts and make the
failures go away. So the change doesn't directly fix it but, at the
very least, it makes it less likely and I'm not sure there's more work
needed at this point unless we see that failure again.


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-8303737: C2: Load can bypass subtype check that enforces it's from the right object type (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15595

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 6, 2023

👋 Welcome back roland! 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 Sep 6, 2023
@openjdk
Copy link

openjdk bot commented Sep 6, 2023

@rwestrel The following label will be automatically applied to this pull request:

  • hotspot-compiler

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

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Sep 6, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 6, 2023

Webrevs

Copy link
Member

@chhagedorn chhagedorn left a comment

Choose a reason for hiding this comment

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

Nice analysis! Took me some time to grasp it but after thinking about it some more after our offline discussion, I think it's a good way to fix this problem.

Apart from some small code style comments, it looks good to me!

// Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
// Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
// pointer comparison is enough to tell if 2 list of types are the same or not)
const TypeTuple* PhiNode::collect_types(PhaseGVN* phase, const Node* r, const Type* phi_type) const {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to rename r -> region.


bool higher_equal_types(PhaseGVN* phase, const Node* other) const;

int nb_extra_types() const {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could rename this to extra_types_count() to make it more clear.

src/hotspot/share/opto/castnode.hpp Show resolved Hide resolved
ConstraintCastNode(Node *n, const Type *t, DependencyType dependency)
: TypeNode(t,2), _dependency(dependency) {
ConstraintCastNode(Node* n, const Type* t, ConstraintCastNode::DependencyType dependency,
const TypeTuple* types)
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to rename types to extra_types to match the field name. Same in the other make* methods.

if (!TypeNode::cmp(n)) {
return false;
}
if (((ConstraintCastNode&) n)._dependency != _dependency) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you can put ((ConstraintCastNode&) n)._dependency into a variable as you are using it a couple of times.

@@ -2133,10 +2133,12 @@ Node *PhiNode::Ideal(PhaseGVN *phase, bool can_reshape) {
// Add casts to carry the control dependency of the Phi that is
// going away
Node* cast = nullptr;
const TypeTuple* extra_types = collect_types(phase, r, phi_type);
Copy link
Member

Choose a reason for hiding this comment

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

Completely optional but you could just directly use bottom_type() and in(0) inside collect_types instead of passing it with r and phi_type.

@openjdk
Copy link

openjdk bot commented Sep 14, 2023

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

8303737: C2: Load can bypass subtype check that enforces it's from the right object type

Reviewed-by: chagedorn, thartmann

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 81 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 added the ready Pull request is ready to be integrated label Sep 14, 2023
rwestrel and others added 3 commits September 15, 2023 10:46
…ClassCast.java

Co-authored-by: Andrey Turbanov <turbanoff@gmail.com>
Co-authored-by: Christian Hagedorn <christian.hagedorn@oracle.com>
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 15, 2023
@openjdk openjdk bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 15, 2023
Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

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

That looks good to me. Correctness and performance testing also passed.

Copy link
Member

@chhagedorn chhagedorn left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the updates!

@rwestrel
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Sep 26, 2023

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

  • 9e6cb62: 8316851: Add @sealedGraph to Executable
  • 3fe6e0f: 8308479: [s390x] Implement alternative fast-locking scheme
  • e2e8e8e: 8312136: Modify runtime/ErrorHandling/TestDwarf.java to split dwarf and decoder testing
  • 0dce4c1: 8313220: Remove Windows specific workaround in LCMS.c for _snprintf
  • e5f05b5: 8312191: ColorConvertOp.filter for the default destination is too slow
  • be9cc73: 8315871: Opensource five more Swing regression tests
  • b65f4f7: 8313403: Remove unused 'mask' field from JFormattedTextField
  • e3201d1: 8310631: test/jdk/sun/nio/cs/TestCharsetMapping.java is spuriously passing
  • 9291b46: 8313804: JDWP support for -Djava.net.preferIPv6Addresses=system
  • afa4833: 8271268: Fix Javadoc links for Stream.mapMulti
  • ... and 82 more: https://git.openjdk.org/jdk/compare/62c0a1b9ac6462233f3ee06af470be9844e9e226...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 26, 2023
@openjdk openjdk bot closed this Sep 26, 2023
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Sep 26, 2023
@openjdk openjdk bot removed the rfr Pull request is ready for review label Sep 26, 2023
@openjdk
Copy link

openjdk bot commented Sep 26, 2023

@rwestrel Pushed as commit 52983ed.

💡 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
hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
4 participants