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

8279143: Undefined behaviours in globalDefinitions.hpp #6930

Closed
wants to merge 10 commits into from

Conversation

merykitty
Copy link
Member

@merykitty merykitty commented Dec 23, 2021

Hi,

This patch replaces undefined behaviours in globalDefinitions.hpp by proper well-defined ones.

Thank you very much.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8279143: Undefined behaviours in globalDefinitions.hpp

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6930/head:pull/6930
$ git checkout pull/6930

Update a local copy of the PR:
$ git checkout pull/6930
$ git pull https://git.openjdk.java.net/jdk pull/6930/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6930

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6930.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 23, 2021

👋 Welcome back merykitty! 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 Dec 23, 2021
@openjdk
Copy link

openjdk bot commented Dec 23, 2021

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

  • hotspot

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 hotspot-dev@openjdk.org label Dec 23, 2021
@mlbridge
Copy link

mlbridge bot commented Dec 23, 2021

Webrevs

@merykitty
Copy link
Member Author

/label hotspot-runtime

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Dec 23, 2021
@openjdk
Copy link

openjdk bot commented Dec 23, 2021

@merykitty
The hotspot-runtime label was successfully added.

@merykitty
Copy link
Member Author

/cc hotspot-runtime

@openjdk
Copy link

openjdk bot commented Dec 23, 2021

@merykitty The hotspot-runtime label was already applied.

static_assert(std::is_default_constructible<T>() &&
std::is_trivially_copy_assignable<T>(), "implementation limits");
T to;
memcpy(&to, &from, sizeof(T));

Choose a reason for hiding this comment

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

During the review of JDK-8145096 it was found that some compilers produce
wretched code for these kinds of memcpy uses, even at fairly high optimization
levels. (I don't know if we still care about those compilers. Unfortunately I
don't remember which ones they were, other than gcc/clang/VS all being good.)

While using the so-called "union trick" is technically undefined behavior, it
is a technique that is known to be widely and well supported and produces good
code, at least for the cases where it is being used in HotSpot. In some cases,
such as gcc (and I think Visual Studio, though can't find a reference right
now), this behavior is documented.

Rather than adding a partial bit_cast (or moving it from elsewhere), we should
be using our existing PrimitiveConversions::cast
(metaprogramming/primitiveConversions.hpp). That has the small difficulty of a
circular include dependency with globalDefintions.hpp. That can be fixed by
moving the various jfoo_cast functions elsewhere (either to
primitiveConversions.hpp or to a new file; I might prefer the latter (along
with the Translate<jfloat/jdouble> specializations in primitiveConversions),
moving these relatively infrequently used utilities to their own dedicated
location). That also reduces the content of globalDefinitions.hpp, which IMO
is too much of a random dumping ground.

Copy link
Contributor

Choose a reason for hiding this comment

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

From replies to what must by not be a GCC FAQ, type punning through a union like this is explicitly permitted:

inline jlong jlong_cast (jdouble x) {
  DoubleLongConv u;
  u.x = x;
  return u.l;
}

But this isn't:

inline jlong jlong_cast (jdouble x) {
  return ((DoubleLongConv*)&x)->l;
}

Not that it matters to HotSpot, because we use -fno-strict-aliasing.

@merykitty merykitty marked this pull request as draft January 7, 2022 05:02
@openjdk openjdk bot removed the rfr Pull request is ready for review label Jan 7, 2022
@bridgekeeper
Copy link

bridgekeeper bot commented Feb 2, 2022

@merykitty This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 2, 2022

@merykitty This pull request has been inactive for more than 16 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Mar 2, 2022
@merykitty merykitty deleted the undefinedBehaviour branch April 27, 2023 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org
3 participants