-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8337683: Fix -Wconversion problem with arrayOop.hpp #20431
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 coleenp! A progress list of the required criteria for merging this PR into |
@coleenp 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 63 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 |
Webrevs
|
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.
This doesn't seem to be enough to fix -Wconversion for this file. It just pushes the problem down to line 142.
I ran your patch with -Wconversion -ferror-limit=20000 and searched for arrayOop.hpp and it gives me:
src/hotspot/share/oops/arrayOop.hpp:71:17: error: implicit conversion changes signedness: 'int' to 'unsigned long' [-Werror,-Wsign-conversion]
size_t hs = length_offset_in_bytes() + sizeof(int);
^~~~~~~~~~~~~~~~~~~~~~~~ ~
src/hotspot/share/oops/arrayOop.hpp:91:17: error: implicit conversion changes signedness: 'int' to 'size_t' (aka 'unsigned long') [-Werror,-Wsign-conversion]
size_t hs = header_size_in_bytes();
~~ ^~~~~~~~~~~~~~~~~~~~~~
src/hotspot/share/oops/arrayOop.hpp:142:43: error: implicit conversion changes signedness: 'int' to 'unsigned long' [-Werror,-Wsign-conversion]
align_down((SIZE_MAX/HeapWordSize - hdr_size_in_words), MinObjAlignment);
~ ^~~~~~~~~~~~~~~~~
src/hotspot/share/oops/arrayOop.hpp:144:53: error: implicit conversion changes signedness: 'int' to 'unsigned long' [-Werror,-Wsign-conversion]
HeapWordSize * max_element_words_per_size_t / type2aelembytes(type);
~ ^~~~~~~~~~~~~~~~~~~~~
I run with this:
It doesn't check sign change, which all of the files have a million of these errors. Only size change. At some point, I thought that fixing all the multitudes of -Wsign-conversion errors is an impossible task. |
If I add in the -Wsign-conversion flags, there's a lot more lines that give an error (everywhere). I can close this as WNF if just fixing the -Wconversion error isn't helpful. @stefank |
These GHA compilers are happy with this change now, please re-review. |
return (((x & UINT16_C(0x00ff)) << 8) | ((x & UINT16_C(0xff00)) >> 8)); | ||
return checked_cast<uint16_t>(((x & UINT16_C(0x00ff)) << 8) | ((x & UINT16_C(0xff00)) >> 8)); |
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.
What is the type of the expression without the cast?
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.
The type of the expression is int
.
https://en.cppreference.com/w/cpp/language/implicit_conversion
In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable.
size_t hs = length_offset_in_bytes() + sizeof(int); | ||
int hs = length_offset_in_bytes() + (int)sizeof(int); | ||
#ifdef ASSERT | ||
// make sure it isn't called before UseCompressedOops is initialized. | ||
static size_t arrayoopdesc_hs = 0; | ||
static int arrayoopdesc_hs = 0; |
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.
Why not just do a checked_cast<int>
on the return statement? (or even a range assert and a static cast?)
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.
Because restricting the types is better than checked_cast. The return type of length_offset_in_bytes() is int.
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.
Also I think the current checked_cast still has problems with signed <--> unsigned.
Another technique to use an even more restricted type. For example, if these offset_in_bytes() functions returned uint16_t, then the value can be widened to either int
or size_t
without a cast.
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.
Yes that would be a change that we could make that would help, and these offset_in_bytes functions should return an unsigned type since they're never negative.
return (((x & UINT16_C(0x00ff)) << 8) | ((x & UINT16_C(0xff00)) >> 8)); | ||
return checked_cast<uint16_t>(((x & UINT16_C(0x00ff)) << 8) | ((x & UINT16_C(0xff00)) >> 8)); |
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.
The type of the expression is int
.
https://en.cppreference.com/w/cpp/language/implicit_conversion
In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable.
@@ -1118,7 +1119,7 @@ inline T Atomic::CmpxchgByteUsingInt::operator()(T volatile* dest, | |||
uint8_t canon_compare_value = compare_value; | |||
volatile uint32_t* aligned_dest | |||
= reinterpret_cast<volatile uint32_t*>(align_down(dest, sizeof(uint32_t))); | |||
size_t offset = pointer_delta(dest, aligned_dest, 1); | |||
uint32_t offset = checked_cast<uint32_t>(pointer_delta(dest, aligned_dest, 1)); |
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.
If this works for all compilers, then that's great. I was a bit concerned that the code in the statement below would cause a warning:
(sizeof(uint32_t) - 1 - offset)
given that sizeof
returns a size_t
and 1
an int
, but I guess the compilers are smart enough to figure out that they all fit within a uint32_t?
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.
gcc doesn't complain about this with -Wsign-conversion, or at least as included with arrayOop.hpp, which was the only goal of this change was to fix the new -Wconversion warning with arrayOop.hpp.
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.
Thanks for reviewing Stefan and suggestions for byteswap.hpp and atomic.hpp.
@@ -1118,7 +1119,7 @@ inline T Atomic::CmpxchgByteUsingInt::operator()(T volatile* dest, | |||
uint8_t canon_compare_value = compare_value; | |||
volatile uint32_t* aligned_dest | |||
= reinterpret_cast<volatile uint32_t*>(align_down(dest, sizeof(uint32_t))); | |||
size_t offset = pointer_delta(dest, aligned_dest, 1); | |||
uint32_t offset = checked_cast<uint32_t>(pointer_delta(dest, aligned_dest, 1)); |
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.
gcc doesn't complain about this with -Wsign-conversion, or at least as included with arrayOop.hpp, which was the only goal of this change was to fix the new -Wconversion warning with arrayOop.hpp.
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.
Ok.
Thanks for reviewing Stefan and David. |
Going to push as commit 9695f09.
Your commit was automatically rebased without conflicts. |
Since base_offset_in_bytes and HeapWordSize are int, there's no loss of conversion in making these variables int. This seems trivial.
Tested with tier1 on linux and windows.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/20431/head:pull/20431
$ git checkout pull/20431
Update a local copy of the PR:
$ git checkout pull/20431
$ git pull https://git.openjdk.org/jdk.git pull/20431/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 20431
View PR using the GUI difftool:
$ git pr show -t 20431
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20431.diff
Webrev
Link to Webrev Comment