RFR: JDK-8301491: C2: java.lang.StringUTF16::indexOfChar intrinsic called with negative character argument
Damon Fenacci
duke at openjdk.org
Thu Feb 16 08:03:28 UTC 2023
The `java.lang.StringUTF16::indexOfChar` is supposed to return -1 for characters with value `< 0`. Its intrinsic methods don't always do so.
https://github.com/openjdk/jdk/blob/96c50a3486e3b6cdce7f8fb409d015b289770811/src/java.base/share/classes/java/lang/StringUTF16.java#L535
The intrinsic methods expect the `int` character being passed to be `>= 0`. Unfortunately this is not enforced in the Java part (`indexOf` only checks for the upper bound):
https://github.com/openjdk/jdk/blob/96c50a3486e3b6cdce7f8fb409d015b289770811/src/java.base/share/classes/java/lang/StringUTF16.java#L430
The intrinsic methods assume that only the lower 16 bits are used (0 <= `ch` <= 0xFFFF) and either don't care about the upper 16 bits or implicitly mask them, e.g. for aarch64:
https://github.com/openjdk/jdk/blob/96c50a3486e3b6cdce7f8fb409d015b289770811/src/hotspot/cpu/aarch64/c2_MacroAssembler_aarch64.cpp#L502-L503 or avx:
https://github.com/openjdk/jdk/blob/96c50a3486e3b6cdce7f8fb409d015b289770811/src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp#L3039
On the other hand, the Java method `indexOfCharUnsafe` makes this check implicitly by comparing a `char` with an `int`;
https://github.com/openjdk/jdk/blob/96c50a3486e3b6cdce7f8fb409d015b289770811/src/java.base/share/classes/java/lang/StringUTF16.java#L542-L544
As there doesn't seem to be a good reason to call `indexOfChar` with `ch < 0`, it seems reasonable to add a check in `LibraryCallKit::inline_string_indexOfChar` that triggers a deoptimization if it fails.
-------------
Commit messages:
- JDK-8301491: remove unneeded spec trap limit flag
- JDK-8301491: rename int character variable and postpone check for stopped. Increase trap limit and add warmup and non-constant variant to test.
- JDK-8301491: C2: java.lang.StringUTF16::indexOfChar instrinsic produces wrong result
Changes: https://git.openjdk.org/jdk/pull/12538/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=12538&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8301491
Stats: 85 lines in 2 files changed: 83 ins; 0 del; 2 mod
Patch: https://git.openjdk.org/jdk/pull/12538.diff
Fetch: git fetch https://git.openjdk.org/jdk pull/12538/head:pull/12538
PR: https://git.openjdk.org/jdk/pull/12538
More information about the hotspot-compiler-dev
mailing list