JDK-8239787 : AArch64: String.indexOf may incorrectly handle empty strings.
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 11,14,15
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: aarch64
  • Submitted: 2020-02-21
  • Updated: 2022-02-24
  • Resolved: 2020-03-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 14 JDK 15
11.0.8Fixed 13.0.3Fixed 14.0.2Fixed 15 b13Fixed
Related Reports
Relates :  
Description
On AArch64, the String.indexOf() may produce incorrect results for empty
strings due to absence of the argument length check in
MacroAssembler::string_indexof_char().

Following test demonstrates the problem:

public class Test1 {
  public static void main(String ... str) {

    System.out.println("Begin");

    for (int i = 0 ; i < 100000; i ++) {
      String emptyString = "";
      for(int c=0; c<0xFFFF; c++) {
        int dot = emptyString.indexOf((char)c, -1);
        if (dot != -1) {
          System.out.println("indexOf returned index " + dot);
        }
      }
    }

    System.out.println("End");
  }
}

Run:
java -XX:-CompactStrings Test1


Suggested fix:

diff --git a/src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
b/src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
--- a/src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
+++ b/src/hotspot/cpu/aarch64/macroAssembler_aarch64.cpp
@@ -4860,6 +4860,8 @@
   Register ch1 = rscratch1;
   Register result_tmp = rscratch2;
 
+  cbz(cnt1, NOMATCH);
+
   cmp(cnt1, (u1)4);
   br(LT, DO1_SHORT);

Comments
It seems it was introduced by JDK-8157708. I checked current 8u-aarch64 does not break on the original test.
21-09-2020

Correction for 11u: the fix doesn't apply cleanly. RFR: https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2020-March/002798.html
16-03-2020

adding request on behalf of Alexey Bakhtin (alexey@azul.com) Fix request (14/13/11): The fix applies cleanly.
06-03-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/a3a462ce27cd User: bae Date: 2020-03-04 10:34:58 +0000
04-03-2020

Fix review: https://mail.openjdk.java.net/pipermail/jdk-dev/2020-February/003937.html
28-02-2020