-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8344168: Change Unsafe base offset from int to long #22095
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
Conversation
|
👋 Welcome back swen! A progress list of the required criteria for merging this PR into |
|
@wenshao 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: 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 13 new commits pushed to the
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 |
|
/label remove hotspot nio |
Webrevs
|
|
@wenshao The |
dholmes-ora
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this I'm not at all convinced this is the right thing to do. The ARRAY_BYTE_BASE_OFFSET is a small value - it is an int.
I understand there is concern about integer arithmetic overflow, but I'm not convinced this is where it needs to be addressed.
| int alignLength | ||
| long alignLength | ||
| = (8 - ((Unsafe.ARRAY_BYTE_BASE_OFFSET + off) & 0x7)) & 0x7; | ||
| for (int alignEnd = off + alignLength; off < alignEnd; off++) { | ||
| for (long alignEnd = off + alignLength; off < alignEnd; off++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think casting the (now) long expression back to int makes more sense here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alignLength is a very small value, which is an int, but alignEnd needs to be a long. Here, changing alignLength to a long can avoid alignEnd overflow.
|
I think you should convert all array index scales to long too. They are susceptible to the same overflow problem (actually more susceptible as they involve in integer multiplications) |
|
I'm concerned that such a change could have unintended consequences, and perhaps deprecating them and providing new long constants would be a better option. |
|
@Glavo This change only happens to jdk.internal.misc.Unsafe. The values in sun.misc.Unsafe are untouched, and they are already deprecated for removal. |
The only places in JDK where there is a risk of offset overflow using Unsafe index scale are jdk.incubator.vector.XXXVector, and they are all explicitly converted to long type. Can we not change it yet? |
|
I am currently busy with a few things in ClassFile API. I can soon come back onto this and might send PRs to you to migrate risky use of array index scales too. |
|
Doesn't this break any apps that use these offsets? Aren't these fields part of the public API of Unsafe, so changing them requires a CSR? |
This is the encapsulated JDK‑internal |
dean-long
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ExE-Boss , are you saying the CSR doesn't apply to jdk.internal.*? The CSR FAQ says:
"Changes to public and exported APIs in jdk.*packages."
Whether or not it needs a CSR, I think there are apps outside the JDK that will break because of this change.
jdk.internal.** is JDK internal, none of these packages are exported. It's okay to make changes in any release, any builds. Nothing outside of the JDK should be depending on internal APIs like this. |
|
It's been a week, can anyone help me complete this PR? |
Netty, for example, has code to access jdk.internal.misc.Unsafe through reflection. Should the CSR FAQ be updated to remove references to If later on someone decides to change these fields again, maybe to something like short, char, or byte, that better represents the possible value range, then we get the overflow problem again. How do we guard against that? Do we have regression tests for the overflow problem? There are other idioms we could use if casting with (long) is considered ugly. For example: |
|
Explicit conversion to long is not ugly, but it is easy to be overlooked and cause bugs. Experienced JDK Committers can also make mistakes. PR #22027 fixes many problems, which proves this. The improvement of this PR is to avoid this type of mistake. |
The |
The "Design principles" section of JEP 200 may be help. Standard modules, as in java.* modules, only export standard API packages. Non-standard may export non-standard APIs. The JDK has several jdk.* modules that export non-standard APIs in the com.sun.* and jdk.* name spaces. |
|
JDK 25 has been launched, please help to continue reviewing this PR |
|
@wenshao 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 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! |
|
Bringing this to attention again; by using the long type, this patch avoids potentially erroneous integer arithmetic used in memory address calculations. |
liach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tier 1-3 passes on Oracle's CI.
|
/integrate |
|
Going to push as commit fdfb68c.
Your commit was automatically rebased without conflicts. |
|
I'm curious why |
|
Re dougxc: This migration is specific to the Java language. I am not so sure about the C++ counterparts, especially that C++ has unsigned types that can complicate things. Another motivation of the Java change is that unsafe is widely used in the Java codebase so upgrading the type can potentially make future usages safer. For example, this already revealed a misuse of the array base offset in the benchmarks in #23393. |
|
Thanks for the clarification @liach . This means we only need to focus on the Java code in https://bugs.openjdk.org/browse/JDK-8349743 (cc @mur47x111). |
The type of the Unsafe base offset constant is int, which may cause overflow when adding int offsets, such as 8343925 (PR #22012). 8343984 (PR #22027) fixes most of the offset overflows in JDK, but ArraysSupport and CRC32C are still unfixed.
@liach proposed the idea of changing the Unsafe base offset to long, which is a complete solution to the Unsafe offset overflow. After discussing with @liach, I submitted this PR to implement @liach's idea.
Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/22095/head:pull/22095$ git checkout pull/22095Update a local copy of the PR:
$ git checkout pull/22095$ git pull https://git.openjdk.org/jdk.git pull/22095/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 22095View PR using the GUI difftool:
$ git pr show -t 22095Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/22095.diff
Using Webrev
Link to Webrev Comment