RFR: JDK-8300080: offset_of for GCC/Clang exhibits undefined behavior and is not always a compile-time constant [v4]

Kim Barrett kbarrett at openjdk.org
Sun Jan 22 20:44:05 UTC 2023


On Fri, 13 Jan 2023 16:06:44 GMT, Justin King <jcking at openjdk.org> wrote:

>> The implementation of `offset_of` for GCC/Clang only deals with types are aligned to 16 bytes or less, if they are more, such as `zCollectedHeap` the behavior is undefined. UBSan also suggests that `offset_of` is not always a compile time constant, as the stack trace came from the dynamic loader during library loading. This patch changes `offset_of` to use `offsetof` and disables the warning `invalid-offsetof` for the JVM.
>
> Justin King has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Move attribute on lambda to correct location
>   
>   Signed-off-by: Justin King <jcking at google.com>

Looks good.

src/hotspot/share/utilities/globalDefinitions_gcc.hpp line 142:

> 140: 
> 141: // GCC/Clang warns about applying offsetof() to types that are not standard layout or calculating
> 142: // offset directly when base address is NULL. Technically using offsetof() on non-standard layout is

suggest s/Technically using/Using/

src/hotspot/share/utilities/globalDefinitions_gcc.hpp line 152:

> 150:   char* c = (char*)(void*)&dummyObj->field;             \
> 151:   return (size_t)(c - space);                           \
> 152: }())

The xlc version of this macro is the same as the old (before JDK-8294902) one from here, with a
comment saying it is the same as the gcc version.  Clearly that's no longer true.  Either that one
should also be updated or the comment updated.  That's probably something for the aix-ppc port
maintainers to decide.  There ought to be a followup bug report for it though.

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

Marked as reviewed by kbarrett (Reviewer).

PR: https://git.openjdk.org/jdk/pull/11978


More information about the hotspot-dev mailing list