Skip to content

8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB #21324

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

Closed
wants to merge 3 commits into from

Conversation

kimbarrett
Copy link

@kimbarrett kimbarrett commented Oct 3, 2024

Please review this change to TypeRawPtr::add_offset to prevent a compiler from
inferring things based on prior pointer arithmetic not invoking UB. As noted in
the bug report, clang is actually doing this.

To accomplish this, changed to integral arithmetic. Also added over/underflow
checks.

Also made a couple of minor touchups. Replaced an implicit conversion to bool
with an explicit compare to nullptr (per style guide). Removed a no longer
needed dummy return after a (now) noreturn function.

Testing: mach5 tier1-7
That testing was with calls to "fatal" for the over/underflow cases and the
sum==0 case. There were no hits. I'm not sure how to construct a test that
would hit those.


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-8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21324

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 3, 2024

👋 Welcome back kbarrett! 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
Copy link

openjdk bot commented Oct 3, 2024

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

8341178: TypeRawPtr::add_offset may be "miscompiled" due to UB

Reviewed-by: dlong, kvn

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

  • a45abf1: 8341860: ProblemList applications/ctw/modules/java_base_2.java on linux-x64
  • 593c27e: 8341535: sun/awt/font/TestDevTransform.java fails with RuntimeException: Different rendering
  • 3180aaa: 8341832: Incorrect continuation address of synthetic SIGSEGV for APX in product builds
  • 3ab519f: 8341424: GHA: Collect hs_errs from build time failures
  • a24525b: 8339320: Optimize ClassFile Utf8EntryImpl#inflate
  • fcc9c8d: 8341854: Incorrect clearing of ZF in fast_unlock_lightweight on x86
  • 38c1d65: 8337980: Javac allows invocation of an inherited instance method from a static method
  • 950e3a7: 8341625: Improve ZipFile validation of the END header
  • e704c05: 8340547: Starting many threads can delay safepoints
  • c30ad01: 8325495: C2: implement optimization for series of Add of unique value
  • ... and 89 more: https://git.openjdk.org/jdk/compare/7d524d7e378430afb3a262e8fe544bd1be22748c...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.

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

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 3, 2024
@openjdk
Copy link

openjdk bot commented Oct 3, 2024

@kimbarrett 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 Oct 3, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 3, 2024

Webrevs

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

Looks reasonable. Just one nit comment.

@@ -3133,7 +3133,7 @@ const TypeRawPtr *TypeRawPtr::make( enum PTR ptr ) {
}

const TypeRawPtr *TypeRawPtr::make( address bits ) {
assert( bits, "Use TypePtr for null" );
assert( bits != nullptr, "Use TypePtr for null" );
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, remove spaces after open and before close ().

Copy link
Author

Choose a reason for hiding this comment

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

I'm not fond of those spaces, but they follow the style used throughout this file.

Copy link
Author

Choose a reason for hiding this comment

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

Although it looks like only 1/3 of the asserts in this file have extra whitespace, including the one being touched
here. So sure, I can remove the extraneous whitespace from this function, since touching it anyway.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

Good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 4, 2024
@vnkozlov
Copy link
Contributor

vnkozlov commented Oct 4, 2024

Side note: please enable GHA testing for your repo.

@TobiHartmann
Copy link
Member

What about using intptr_t for TypeRawPtr::_bits instead?

@kimbarrett
Copy link
Author

What about using intptr_t for TypeRawPtr::_bits instead?

That has more fannout, into code I'm not familiar with. The proposed change fixes the immediate "miscompilation".
A change of the type could be done as a further enhancement, if that makes sense to do. I'd rather leave that to
someone from the compiler team. If that approach is what's wanted to fix the immediate problem, then I'm going
to want to hand this issue off. Also, uintptr_t might be more appropriate than intptr_t.

@@ -3223,14 +3223,22 @@ const TypePtr* TypeRawPtr::add_offset(intptr_t offset) const {
case TypePtr::NotNull:
return this;
case TypePtr::Null:
return make( (address)offset );
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this assert that _bits == 0? Looking at the code, however, I can't find anywhere that we actually create a TypeRawPtr with TypePtr::Null. We could probably remove this case and let it fall through to the default ShouldNotReachHere().

Copy link
Author

Choose a reason for hiding this comment

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

Initialization of TypePtr::NULL_PTR here:

TypePtr::NULL_PTR= TypePtr::make(AnyPtr, TypePtr::Null, 0);

Copy link
Member

Choose a reason for hiding this comment

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

I saw that too, but it creates a TypePtr, not a TypeRawPtr.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, you are right. And TypeRawPtr::make asserts the PTR is neither Constant nor Null. Which makes
both switch cases under modification here supposedly unreachable. That would explain why I never hit
either after running lots of tests. All of the change proposed here can be eliminated, and instead change
both cases to fall through to the default ShouldNotReachHere(). (And that would be another way to
remove the -Wzero-as-null-pointer-constant warning that was how I got here in the first place. :) )

Copy link
Member

Choose a reason for hiding this comment

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

There's TypeRawPtr::make(enum PTR ptr) which doesn't allow Constant or Null, but we are using TypeRawPtr::make(address bits) here.
We may need to keep the Constant case. I wouldn't be surprised if there was a way to trigger that path using Unsafe.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, keeping it makes sense. I've removed the TypePtr::Null case, allowing that one to default to
ShuoldNotReachHere().

@TobiHartmann
Copy link
Member

What about using intptr_t for TypeRawPtr::_bits instead?

That has more fannout, into code I'm not familiar with. The proposed change fixes the immediate "miscompilation". A change of the type could be done as a further enhancement, if that makes sense to do. I'd rather leave that to someone from the compiler team. If that approach is what's wanted to fix the immediate problem, then I'm going to want to hand this issue off. Also, uintptr_t might be more appropriate than intptr_t.

Okay, that's fine with me.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 9, 2024
Copy link
Member

@dean-long dean-long 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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 9, 2024
@kimbarrett
Copy link
Author

Thanks for reviews, @vnkozlov and @dean-long

@kimbarrett
Copy link
Author

/integrate

@openjdk
Copy link

openjdk bot commented Oct 11, 2024

Going to push as commit 0a57fe1.
Since your change was applied there have been 134 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 Oct 11, 2024
@openjdk openjdk bot closed this Oct 11, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 11, 2024
@openjdk
Copy link

openjdk bot commented Oct 11, 2024

@kimbarrett Pushed as commit 0a57fe1.

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

@kimbarrett kimbarrett deleted the fix-add-offset branch October 11, 2024 21:12
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
Development

Successfully merging this pull request may close these issues.

4 participants