RFR: 8340620: Fix -Wzero-as-null-pointer-constant warnings for CompressedOops

Kim Barrett kbarrett at openjdk.org
Wed Sep 25 20:58:37 UTC 2024


On Wed, 25 Sep 2024 11:23:05 GMT, Quan Anh Mai <qamai at openjdk.org> wrote:

>> +1. Although I would expect any sane compiler to fold it, maybe it is still not optimized with something like `-O0`. Or maybe just move these asserts to `CompressedOops::initialize`, so whatever happens, happens once.
>
> This cannot be used in `static_assert` because `reinterpret_cast` is not allowed here.
> 
> I believe [`reinterpret_cast<uintptr_t>(nullptr)` will always return 0](https://en.cppreference.com/w/cpp/language/reinterpret_cast). You may need to do it the other way around.

Correct that static_assert can't be used, because of the reinterpret_cast.

I considered putting the assert in CompressedOops::set_base, with comments to
connect encode/decode with that assertion, but prefer putting the assert near
the code that is actually relying on the property. Even with -O0 gcc seems to
constant fold the expression and (pretty much?) compile away the assertion.

A null pointer value is not guaranteed to have a zero representation.  The
conversion of a literal 0 to a null pointer value is syntactic sugar.  The
bit pattern of the result might be something else.  There's some good
discussion of this here:
https://stackoverflow.com/questions/2761360/could-i-ever-want-to-access-the-address-zero

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/21172#discussion_r1775982734


More information about the hotspot-dev mailing list