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!

@merykitty
Copy link
Member Author

Can I have this PR reviewed, please?

@jatin-bhateja
Copy link
Member

jatin-bhateja commented May 16, 2025

Hi @merykitty ,

Just to complement your documentation update, I have created test scenarios for various verification errors around implicit larval objects, i.e. the ones created using new operator but are still pending initialization, explicit larvals generated using Unsafe.makePrivateBuffer should adhere to the same semantics.

Implicit larval objects verification.docx

Overall, your changes look good to me; finer refinements can be done in due course, but specs are much clear now, thank you so much!

@@ -381,8 +422,30 @@ public native Object[] newSpecialArray(Class<?> componentType,

/**
* Returns an object instance with a private buffered value whose layout
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Returns an object instance with a private buffered value whose layout
* Returns a value type instance with a private buffered value whose layout

* and contents is exactly the given value instance. The return object
* is in the larval state that can be updated using the unsafe put operation.
* and contents is exactly the given value instance. The return object is
* in the larval state that can be updated using the unsafe put operation.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* in the larval state that can be updated using the unsafe put operation.
* in the larval state that can be modified through an unsafe put operation.

Comment on lines +429 to +430
* the creation of a value object without the conventional Java procedure
* and bypassing the bytecode verifier regarding larval objects. As a
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* the creation of a value object without the conventional Java procedure
* and bypassing the bytecode verifier regarding larval objects. As a
* the creation of a value object without the conventional Java procedure
* and bypassing usual verification applicable to @{code new} bytecode. As a

* local variable.
* <li>The return object can only be used as the first argument passed to
* {@code Unsafe::putXXX} or to {@link #finishPrivateBuffer(Object)}, any
* other usage, such as loading from or returning it, is illegal. The only
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* other usage, such as loading from or returning it, is illegal. The only
* other usage, such as reading any of its fields or returning it, is illegal. The only

* simultaneously 0 and 1.
* <li>Impossible code execution. E.g. the branches of an {@code if} are
* both executed or both not executed.
* <li>Wiping out the hard drive.
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 these are just for alerting people; this is not that valuable to create a huge list to specifically highlight. I think what we should highlight instead is the best practices.

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 13, 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!

@liach
Copy link
Member

liach commented Jun 13, 2025

Should we change these long paragraphs to a design document, as suggested in openjdk/jdk#24777 (review)?

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.

4 participants