Skip to content

Document IAE for Unsafe offset methods #25945

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

liach
Copy link
Member

@liach liach commented Jun 24, 2025

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.


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

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25945/head:pull/25945
$ git checkout pull/25945

Update a local copy of the PR:
$ git checkout pull/25945
$ git pull https://git.openjdk.org/jdk.git pull/25945/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25945

View PR using the GUI difftool:
$ git pr show -t 25945

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25945.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 24, 2025

👋 Welcome back liach! 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 Jun 24, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jun 24, 2025

@liach The following label will be automatically applied to this pull request:

  • core-libs

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.

Comment on lines +1069 to +1070
* @throws NullPointerException if the field is {@code null}
* @throws IllegalArgumentException if the field is static

Choose a reason for hiding this comment

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

Maybe these checks could be done in Java like:

// implicit null check:
if ((f.getModifiers() & ACC_STATIC) != 0) {
	throw new IllegalArgumentException("Field is static");
}

and for the staticField(Offset/Base) methods:

// implicit null check:
if ((f.getModifiers() & ACC_STATIC) == 0) {
	throw new IllegalArgumentException("Field is not static");
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't think anyone is willing to change code here...

Copy link
Member Author

Choose a reason for hiding this comment

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

That said, the only places that depend on this IAE behavior is the AtomicXxxFieldUpdater classes. All other sites pass trusted fields into these methods, of course besides the sun.misc.Unsafe users which definitely need this IAE. Still I personally prefer adding these checks to atomic field updaters instead of here to reduce risky dependencies.

@ExE-Boss
Copy link

ExE-Boss commented Jun 26, 2025

I noticed that the native implementation of Unsafe​::array⁠(BaseOffset​/IndexScale) refers to the non‑existent class java.lang.InvalidClassException instead of java.lang.IllegalArgumentException (which results in java.lang.NoClassDefFoundError being thrown):

static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) {
assert(clazz != nullptr, "clazz must not be null");
oop mirror = JNIHandles::resolve_non_null(clazz);
Klass* k = java_lang_Class::as_Klass(mirror);
if (k == nullptr || !k->is_array_klass()) {
THROW(vmSymbols::java_lang_InvalidClassException());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants