RFR: 8361300: Document exceptions for Unsafe offset methods [v4]

John R Rose jrose at openjdk.org
Wed Jul 30 17:38:57 UTC 2025


On Thu, 3 Jul 2025 18:41:27 GMT, Chen Liang <liach at openjdk.org> wrote:

>> Unsafe throws IAE for misusing static vs instance fields, and it's revealed that AtomicXxxFieldUpdaters are using this mechanism to reject static fields. This is not a good practice, but we can at least document this so we don't accidentally introduce problems.
>
> Chen Liang has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Test to verify observed internal unsafe behaviors

Changes requested by jrose (Reviewer).

src/hotspot/share/prims/unsafe.cpp line 496:

> 494:     Symbol *name = fs.name();
> 495:     if (name->equals(utf_name)) {
> 496:       offset = fs.offset();

While you are here, if you keep this check, rename this particular function `find_field_offset` to `find_nonstatic_field_offset` (not the other one that takes the "must be static" flag).

The logic, as written, is difficult to understand because of the behavioral difference between the two overloading.

(I note that "instance field" is also a term of art in our code base, but "nonstatic_field" is more common.)

Alternatively, and probably better, take out this particular check (restoring equal access to static and non-static) and just rely on the checks in Java code.  Such checks are almost always better (easier to reason about by humans and JITs) than checks in C++ code.

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

PR Review: https://git.openjdk.org/jdk/pull/25945#pullrequestreview-3072687621
PR Review Comment: https://git.openjdk.org/jdk/pull/25945#discussion_r2243419037


More information about the core-libs-dev mailing list