Skip to content

8353236: [lworld] Better documentation for Valhalla Unsafe intrinsics #1425

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

Open
wants to merge 3 commits into
base: lworld
Choose a base branch
from

Conversation

merykitty
Copy link
Member

@merykitty merykitty commented Apr 9, 2025

Hi,

This patch clarifies the meaning of undefined behavior when working with Unsafe and specifies the requirements when working with Unsafe::makePrivateBuffer and Unsafe::finishPrivateBuffer.

Please take a look and leave your suggestions, thanks a lot.


Progress

  • Change must not contain extraneous whitespace

Issue

  • JDK-8353236: [lworld] Better documentation for Valhalla Unsafe intrinsics (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 1425

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/valhalla/pull/1425.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 9, 2025

👋 Welcome back qamai! A progress list of the required criteria for merging this PR into lworld 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 Apr 9, 2025

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

8353236: [lworld] Better documentation for Valhalla Unsafe intrinsics

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 85 new commits pushed to the lworld 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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@mlbridge
Copy link

mlbridge bot commented Apr 9, 2025

Webrevs

* with illegal arguments, undefined behavior may time travel. That is, if a
* control path may eventually reach an invocation of an {@code Unsafe} method
* with illegal arguments, the symptoms of undefined behavior may be present
* even before the invocation of the {@code Unsafe} method.
Copy link
Member

Choose a reason for hiding this comment

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

I think you can talk in a more JIT-oriented manner, that while Unsafe is mostly set up to be the hooks between core libraries and hotspot runtime, hotspot compiler may interpret the input values and perform aggressive optimizations with contracts that cannot be explicitly expressed by Java program semantics.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, not only the JIT, but the runtime may exhibit unpredictable behaviors in the presence of UB, too.

Comment on lines 93 to 94
* By default, usage of all methods in this class exhibits undefined behavior,
* unless otherwise explicitly specified.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The "by default" and "unless otherwise specified" seems to be redundant.

Suggested change
* By default, usage of all methods in this class exhibits undefined behavior,
* unless otherwise explicitly specified.
* Unless otherwise explicitly specified, all methods in this class can exhibit undefined behavior.
* .

Copy link
Member Author

Choose a reason for hiding this comment

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

I decide to just remove it as each method specifies its behaviours already.

Comment on lines +441 to +442
* value of this method. The object must also be not assigned to another
* local variable.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this restriction? What assumption is violated if there is a second assignment?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is none, but it would be easier to specify the restriction on Unsafe::finishPrivateBuffer as the only variable referring to the larval object is the one passed into that method. Otherwise, we must say that all variables referring to the object must not be used. This, however, sounds like a runtime constraint while what we really mean is a compile-time constraint. As a result, I think this restriction is harmless but make our life easier.

* {@code Unsafe::putXXX} or to {@link #finishPrivateBuffer(Object)}, any
* other usage, such as loading from or returning it, is illegal. The only
* exception is the implicit check cast inserted by the compiler on the
* return value of this method. Explicit check casts are not allowed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"not allowed"? But there is no enforcement; unpredictable behavior yes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not allowed here in the sense that failure to do so will result in undefined behavior.

* exception is the implicit check cast inserted by the compiler on the
* return value of this method. Explicit check casts are not allowed.
* </ul>
* Illegal usage of this method exhibits undefined behavior even if the
Copy link
Collaborator

Choose a reason for hiding this comment

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

may or can; but not always.

Copy link
Member Author

Choose a reason for hiding this comment

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

Illegal usage ALWAYS exhibits undefined behavior, the uncertainty here is that whether this undefined behavior manifests itself as an unexpected operation.

Comment on lines +471 to +472
* Illegal usage of this method exhibits undefined behavior even if the
* illegal statements are never actually reached at runtime.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another can/may exhibit case; improve the phrase everywhere.

* {@link #makePrivateBuffer(Object)}.
* <li>After the invocation of this method, the variable that holds the
* argument passed into this method must not be used.
* </ul>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would add the exhortation that every field of the value object must have a value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unsafe::makePrivateBuffer takes a non-larval value object so all of its fields always have a value.

@bridgekeeper
Copy link

bridgekeeper bot commented May 8, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants