Skip to content

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

Closed
wants to merge 5 commits into from

Conversation

coleenp
Copy link
Contributor

@coleenp coleenp commented Aug 1, 2024

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

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8337683: Fix -Wconversion problem with arrayOop.hpp (Enhancement - P4)

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

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 1, 2024

👋 Welcome back coleenp! 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
Copy link

openjdk bot commented Aug 1, 2024

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

8337683: Fix -Wconversion problem with arrayOop.hpp

Reviewed-by: stefank, dholmes

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 master branch:

  • 4b740d8: 8225209: jdk/jfr/event/compiler/TestCodeSweeper.java fails
  • ddbd7a7: 8337320: Update ProblemList.txt with tests known to fail on XWayland
  • 12c553f: 8329471: Remove GTK2
  • 088871c: 8335267: [XWayland] move screencast tokens from .awt to .java folder
  • 3bc4a1a: 8233068: HIDPI: Linux: AWT Checkbox check mark is unscaled
  • a9460a6: 8337982: Remove dead undef assrt0n
  • fa18359: 8335981: ProblemList runtime/Thread/TestAlwaysPreTouchStacks.java for MacOS
  • 1846a65: 8337205: Typo in Stack vs Deque Method table in Deque specification
  • 16df9c3: 8337971: Problem list several jvmci tests on linux-riscv64 until JDK-8331704 is fixed
  • 9b11bd7: 8337826: Improve logging in OCSPTimeout and SimpleOCSPResponder to help diagnose JDK-8309754
  • ... and 53 more: https://git.openjdk.org/jdk/compare/f1fa64b6b67f2eef916b8d92eb522ccb19035e12...master

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 master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 1, 2024
@openjdk
Copy link

openjdk bot commented Aug 1, 2024

@coleenp 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 Aug 1, 2024
@mlbridge
Copy link

mlbridge bot commented Aug 1, 2024

Webrevs

Copy link
Member

@stefank stefank left a 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);
                                                  ~ ^~~~~~~~~~~~~~~~~~~~~

@coleenp
Copy link
Contributor Author

coleenp commented Aug 2, 2024

I run with this:

diff --git a/make/autoconf/flags-cflags.m4 b/make/autoconf/flags-cflags.m4
index cf7f4534c89..131e9ece31c 100644
--- a/make/autoconf/flags-cflags.m4
+++ b/make/autoconf/flags-cflags.m4
@@ -186,12 +186,12 @@ AC_DEFUN([FLAGS_SETUP_WARNINGS],
     gcc)
       DISABLE_WARNING_PREFIX="-Wno-"
       BUILD_CC_DISABLE_WARNING_PREFIX="-Wno-"
-      CFLAGS_WARNINGS_ARE_ERRORS="-Werror"
+      CFLAGS_WARNINGS_ARE_ERRORS=""
 
       # Additional warnings that are not activated by -Wall and -Wextra
       WARNINGS_ENABLE_ADDITIONAL="-Wpointer-arith -Wsign-compare \
           -Wunused-function -Wundef -Wunused-value -Wreturn-type \
-          -Wtrampolines"
+          -Wtrampolines -Wconversion"
       WARNINGS_ENABLE_ADDITIONAL_CXX="-Woverloaded-virtual -Wreorder"
       WARNINGS_ENABLE_ALL_CFLAGS="-Wall -Wextra -Wformat=2 $WARNINGS_ENABLE_ADDITIONAL"
       WARNINGS_ENABLE_ALL_CXXFLAGS="$WARNINGS_ENABLE_ALL_CFLAGS 
$WARNINGS_ENABLE_ADDITIONAL_CXX"

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.

@coleenp
Copy link
Contributor Author

coleenp commented Aug 5, 2024

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

@coleenp
Copy link
Contributor Author

coleenp commented Aug 7, 2024

These GHA compilers are happy with this change now, please re-review.

Comment on lines -66 to +67
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));
Copy link
Member

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?

Copy link
Member

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.

Comment on lines -71 to +74
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;
Copy link
Member

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?)

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Comment on lines -66 to +67
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));
Copy link
Member

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));
Copy link
Member

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?

Copy link
Contributor Author

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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 8, 2024
Copy link
Contributor Author

@coleenp coleenp left a 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));
Copy link
Contributor Author

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.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Ok.

@coleenp
Copy link
Contributor Author

coleenp commented Aug 8, 2024

Thanks for reviewing Stefan and David.
/integrate

@openjdk
Copy link

openjdk bot commented Aug 8, 2024

Going to push as commit 9695f09.
Since your change was applied there have been 65 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 8, 2024
@openjdk openjdk bot closed this Aug 8, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 8, 2024
@openjdk
Copy link

openjdk bot commented Aug 8, 2024

@coleenp Pushed as commit 9695f09.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@coleenp coleenp deleted the arrayoop branch August 8, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants