Skip to content

8156534: Check if range checks can be moved into Java wrapper for intrinsics #25998

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 2 commits into
base: master
Choose a base branch
from

Conversation

vy
Copy link
Contributor

@vy vy commented Jun 26, 2025

Warning

This is a draft PR to collect feedback on implementing JDK-8156534 only for java.lang.StringCodec::countPositives.

Moves String-related intrinsic checks from C++ to Java – see the ticket for details on motivation.

Implementation notes

The goal of this work is to, for java.lang.String* classes,

  1. Move @IntrinsicCandidate-annotated public methods1 (in Java code) to private ones, and wrap them with a public "front door" method
  2. Since we moved the @IntrinsicCandidate annotation to a new method, intrinsic mappings – i.e., associated do_intrinsic() calls in vmIntrinsics.hpp – need to be updated too
  3. Add range and null checks to the newly created public front door method
  4. Place all range and null checks in the intrinsic code (add if missing!) behind a VerifyIntrinsicChecks flag

Following preliminary work needs to be carried out as well:

  1. Add a new VerifyIntrinsicChecks VM flag
  2. Update generate_string_range_check to produce a HaltNode

1 @IntrinsicCandidate-annotated constructors are not subject to this change, since they are a special case.

Verification

I've tested the implementation as follows:

  1. Created the following test program:
public class StrIntri {
    public static void main(String[] args) {
        Exception lastException = null;
        for (int i = 0; i < 1_000_000; i++) {
            try {
                jdk.internal.access.SharedSecrets.getJavaLangAccess().countPositives(new byte[1,2,3], 2, 5);
            } catch (Exception exception) {
                lastException = exception;
            }
        }
        if (lastException != null) {
            lastException.printStackTrace();
        } else {
            System.out.println("completed");
        }
    }
}
  1. Compiled the JDK and run the test:
$ CONF=linux-x64-slowdebug make jdk
$ ~/jdk/build/linux-x64-slowdebug/jdk/bin/java -XX:+VerifyIntrinsicChecks --add-exports java.base/jdk.internal.access=ALL-UNNAMED StrIntri.java
java.lang.IndexOutOfBoundsException: Range [2, 2 + 5) out of bounds for length 3

Received IOOBE as expected.

  1. Removed all checks in StringCodec.java, and re-compiled the JDK
  2. Set the countPositives(...) arguments in the program to (null, 1, 1), run it, and observed the VM crash with unexpected null in intrinsic.
  3. Set the countPositives(...) arguments in the program to (new byte[]{1,2,3}, 2, 5), run it, and observed the VM crash with unexpected guard failure in intrinsic.

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-8156534: Check if range checks can be moved into Java wrapper for intrinsics (Enhancement - P2)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25998

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 26, 2025

👋 Welcome back vyazici! 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 26, 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 26, 2025

@vy The following labels will be automatically applied to this pull request:

  • core-libs
  • graal
  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

public static int countPositives(byte[] ba, int off, int len) {
Objects.requireNonNull(ba, "ba");
Objects.checkFromIndexSize(off, len, ba.length);
Copy link
Member

Choose a reason for hiding this comment

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

I recall core libraries intentionally avoided this because of performance problems. Is it possible for us to say trust the len argument to be non-negative? That allows us to simplify this to Objects.checkIndex(off, ba.length - len). See this usage in perf-sensitive FFM API:

void checkBounds(long offset, long length) {

Copy link
Member

@TobiHartmann TobiHartmann Jun 27, 2025

Choose a reason for hiding this comment

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

But the original code already checks for len >= 0, right? See LibraryCallKit::inline_countPositives -> generate_string_range_check -> // Offset and count must not be negative

This PR is about moving the range checks from the intrinsics into the Java wrappers. Removing range checks is out of the scope and should be carefully evaluated on a case-by-case basis separately.

Copy link
Member

Choose a reason for hiding this comment

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

My point is this is a performance-sensitive API. We are using a known-slow check method checkFromIndexSize which may introduce a performance regression.

Choose a reason for hiding this comment

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

Maybe use jdk.internal.util.Preconditions directly instead?

Suggested change
Objects.checkFromIndexSize(off, len, ba.length);
Preconditions.checkFromIndexSize(off, len, ba.length, null);

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.

4 participants