-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8361842: Move input validation checks to Java for java.lang.StringCoding 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
Conversation
👋 Welcome back vyazici! A progress list of the required criteria for merging this PR into |
@vy This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 263 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
…r our cases This reverts commit 196fc5d.
* </p> | ||
* | ||
* @param sa the source byte array containing characters encoded in UTF-16 | ||
* @param sp the index of the <em>byte (not character!)</em> from the source array to start reading from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the byte (not character!)
emphasis here and below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is incorrect.
This is the index of a character (two bytes).
As it is used in encodeISOArray0()
, it is incremented by 1 and passed to StringUTF16.getChar()
, where it is multiplied by 2 to obtain the real byte[]
index.
* {@linkplain Preconditions#checkFromIndexSize(int, int, int, BiFunction) out of bounds} | ||
*/ | ||
static int encodeISOArray(byte[] sa, int sp, byte[] da, int dp, int len) { | ||
checkFromIndexSize(sp, len << 1, requireNonNull(sa, "sa").length, AIOOBE_FORMATTER); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sa
contains 2-byte char
s, and sp
points to an index of this inflated array. Though, len
denotes the codepoint count, hence the len << 1
while checking sp
and len
bounds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reference of sa.length
is likely wrong also, as it is the source length in bytes but for the index check should be checking the source length in chars.
It might be worth trying to find or create a test for the accidental incorrect interpretation of length in bytes vs chars..
Webrevs
|
I disagree with a small part of the statement of goals:
As formulated above, this is a violation of DRY and if embraced the wrong way will lead to code that is harder to review and prove bug-free. Performing 100% accurate range/null/validation checks is deeply impractical for an assembly-based or IR-based intrinsic. It’s too hard to verify by code review, and coverage testing is suspect. We must frankly put all the weight of verification on Java code, including Java bytecode intrinsic behaviors. Java code is high-level and can be read mostly as a declarative spec, if clearly written (as straight-line code, then the intrinsic call). Also, such simple Java code shapes (and their underlying bytecodes) are tested many orders of magnitude more than any given intrinsic. I see two bits of evidence that you agree with me on this: 1. The intrinsic-local validation (IR or assembly) is allowed to Halt instead of throw, and 2. the intrinsic-local validation is optional, turned on only by a stress test mode. This tells me that the extra optional testing is also not required to be 100%. Thus, I think the above goal would be better stated this way:
I think I'm agreeing with you on the material points. It is important to summarize our intentions accurately at the top, for those readers that are reading only the top as a summary. |
@rose00, thanks so much for the feedback. I agree with your remarks and get your points on "Always validate all input at the intrinsic" is a violation of DRY and an impractical goal. I incorporated your suggestions as follows:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for looking into this Volkan!
I left a couple of minor comments.
I also noticed that you haven't yet added the benchmark results to the description: do you want to run them again after the reviews?
@dafedafe, thanks so much for the review! I've implemented the changes you requested, and shared some benchmark figures in the associated ticket. I am still actively working on evaluating the performance impact. |
Those who are touching to these methods should well be aware of the details elaborated in the `@apiNote`, no need to put it on a display.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done some testing on linux-amd64 and verified that on microbenchmarks that exercise for example StringCoding.hasNegatives
(a front door of one of the intrinsics this PR changes) the generated assembly is identical under ideal conditions. Spurious regressions seen in some setups could be inlining related: moving from a simple range check emitted by the intrinsic to a call to Preconditions.checkFromIndexSize
may push us over some inlining threshold in some cases. I'll try to get my hands on a linux-aarch64 machine to do some diagnostic runs on.
An idea for future investigation could be to make Preconditions.checkFromIndexSize
an intrinsic similar to Preconditions.checkIndex
- to help the compiler do the right thing with more ease and perhaps slightly faster.
If the Java code is good enough, then the precondition method can simply be marked |
Some more parts to the precondition intrinsic story, FTR: Apart from inlining, one of the goals of the intrinsic preconditions is to allow them to more reliably interact with elimination of range checks. The JVM knows its own intrinsic checks in the If the precondition check in question works OK for these use cases, it can be marked force-inline, but if there is also evidence that it would unlock more loop optimizations, then it should be made an intrinsic (or else built on top of another intrinsics, with force-inline). |
Another comment on the precondition in question: It does not appear to be inside a loop, but rather a precursor to a bulk operation (which searches the sign bits of a byte array slice). It's hard to imagine the JIT doing a better job with that as an intrinsic, since it probably won't be RCE-ed within an enclosing hot loop. So, yes, force-inline it. Volkan found a pre-existing RFE about that precondition check, and I added a lengthy comment to it, FTR: https://bugs.openjdk.org/browse/JDK-8361837#comment-14809088 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is lovely work. I've left a few suggestions which you may wish to take action on.
JDK-8361842, addressed by this PR, is the first step in a series of similar improvements under the JDK-8156534 umbrella issue. I wanted to get this one in perfect shape to serve as a guide for the subsequent PRs, hence the meticulous effort. Thanks so much to everyone helped with reviewing this work. 🙇 😍 I've verified that /integrate |
@vy This pull request has not yet been marked as ready for integration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good in principle; didn't check in the details for compiler code, which I don't necessarily understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good.
/integrate |
Going to push as commit 655dc51.
Your commit was automatically rebased without conflicts. |
Validate input in
java.lang.StringCoding
intrinsic Java wrappers, improve their documentation, enhance the checks in the associated IR or assembly code, and adapt them to cause VM crash on invalid input.Implementation notes
The goal of the associated umbrella issue JDK-8156534 is to, for
java.lang.String*
classes,@IntrinsicCandidate
-annotatedpublic
methods1 (in Java code) toprivate
ones, and wrap them with apublic
"front door" method@IntrinsicCandidate
annotation to a new method, intrinsic mappings – i.e., associateddo_intrinsic()
calls invmIntrinsics.hpp
– need to be updated tooVerifyIntrinsicChecks
VM flagFollowing preliminary work needs to be carried out as well:
VerifyIntrinsicChecks
VM flaggenerate_string_range_check
to produce aHaltNode
. That is, crash the VM ifVerifyIntrinsicChecks
is set and a Java wrapper fails to spot an invalid input.1
@IntrinsicCandidate
-annotated constructors are not subject to this change, since they are a special case.Functional and performance tests
tier1
(which includestest/hotspot/jtreg/compiler/intrinsics/string
) passes on several platforms. Further tiers will be executed after integrating reviewer feedback.Performance impact is still actively monitored using
test/micro/org/openjdk/bench/java/lang/String{En,De}code.java
, among other tests. If you have suggestions on benchmarks, please share in the comments.Verification of the VM crash
I've tested the VM crash scenario as follows:
Received
AIOOBE
as expected.StringCodec.java
, and re-compiled the JDKcountPositives(...)
arguments in the program to(null, 1, 1)
, run it, and observed the VM crash withunexpected null in intrinsic
.countPositives(...)
arguments in the program to(new byte[]{1,2,3}, 2, 5)
, run it, and observed the VM crash withunexpected guard failure in intrinsic
.Progress
Issue
Reviewers
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
Using Webrev
Link to Webrev Comment