Skip to content

8328874: Class::forName0 should validate the class name length early #26802

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

hgqxjj
Copy link
Member

@hgqxjj hgqxjj commented Aug 15, 2025

Validate class name length immediately after GetStringUTFLength() in Class.forName0. This prevents potential issues caused by overly long class names before they reach later code that would reject them, throwing ClassNotFoundException early.


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-8328874: Class::forName0 should validate the class name length early (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26802

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

Using diff file

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

Using Webrev

Link to Webrev Comment

Validate class name length immediately after GetStringUTFLength() in Class.forName0. This prevents potential issues caused by overly long class names before they reach later code that would reject them, throwing ClassNotFoundException early.
@bridgekeeper
Copy link

bridgekeeper bot commented Aug 15, 2025

👋 Welcome back ghan! 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 15, 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 Aug 15, 2025

@hgqxjj 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.

@openjdk openjdk bot added core-libs [email protected] rfr Pull request is ready for review labels Aug 15, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 15, 2025

@liach
Copy link
Member

liach commented Aug 15, 2025

We currently have a trend of moving argument validations and checks to pure Java code, to minimize downcall into the VM (whose code cannot be optimized by compilers). Even if we keep checks in the VM, I guess jvm.cpp might be a better place than Class.c.

@dholmes-ora
Copy link
Member

We currently have a trend of moving argument validations and checks to pure Java code, to minimize downcall into the VM (whose code cannot be optimized by compilers). Even if we keep checks in the VM, I guess jvm.cpp might be a better place than Class.c.

No, that would defeat the purpose of creating this request in the first place. It was the calls within Class.c that originally choked on the excessive length. We want to avoid operating on these excessively large strings, and so need to bail out early on the Java/libjava side.

@openjdk openjdk bot removed the rfr Pull request is ready for review label Aug 19, 2025
fix whitespace errors
@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 19, 2025
correct length of class name
@hgqxjj
Copy link
Member Author

hgqxjj commented Aug 19, 2025

@liach @dholmes-ora Thank you both for the detailed feedback and explanation.
I've moved the check to the Java side. Please have a look when you get a chance.

@liach
Copy link
Member

liach commented Aug 19, 2025

There is a jdk.internal.util.ModifiedUtf::utfLen that you can use instead of writing your own function.

@rose00
Copy link
Contributor

rose00 commented Aug 19, 2025

Surely that loop already exists, and even if it not, it should live in a place where it is more widely useful and easier to optimize.

(Widely useful? Yes, because asking the length of a string in modified-utf8, and checking whether it fits inside the constant pool limit, is not just for Class::forName.)

(Easier to optimize? If we have the loop in a common place, maintainers will probably ensure that it has the right performance characteristics. A one-off loop is less likely to attract improvements in the future.)

A quick search reveals a new friend, jdk.internal.util.ModifiedUtf::utfLen. It seems to me you should use that rather than writing your own loop. Look at other code (under java.* packages) that uses it for a model. You can pass zero for the funny extra parameter.

Perhaps not for this PR, we might add a method somewhere (requires discussion) that calls jdk.internal.util.ModifiedUtf::utfLen and also the prefix-searcher (non-zero-ascii only?). But if we just have a hand-rolled one-off loop, it is much harder to make such improvements. If we use API points instead of manual loops, its much easier for maintainers to make improvements.

Also, speaking of improvements, if such a method is framed as a constant pool validity check (isValidConstantPoolString) then an O(1) check is possible: Just ask if the string's body is less than (1<<16)/2 for ASCII-only strings, and less than (1<<16)/3 for other strings, and there's no need to search the string body. But this only works for an API point that "knows" what the maximum size is ahead of time. The classfile spinner logic could use this, not just for class names, but for all CONSTANT_Utf8 entries in the CP. That work would NOT be for this PR, but rather for the authors of the classfile API to think about.

@rose00
Copy link
Contributor

rose00 commented Aug 19, 2025

(Chen beat me to the discovery!)

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.

It is up to core-libs folk but to me that seems far too heavyweight to have on the Java side, and we are then going to repeat some of it in the C code.

updates the class name length validation logic on the Java side
@hgqxjj
Copy link
Member Author

hgqxjj commented Aug 20, 2025

@liach @dholmes-ora @rose00 @RogerRiggs Thanks for all the valuable feedback! I’ve updated the changes accordingly—please take another look when you have time.

@RogerRiggs
Copy link
Contributor

What tests validate the behavior is the same both with and without the shortcut? Tnx.

@hgqxjj
Copy link
Member Author

hgqxjj commented Aug 23, 2025

@RogerRiggs I added a test function. Please take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

7 participants