Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8261022

Fix incorrect result of Math.abs() with char type

    XMLWordPrintable

Details

    • 13
    • b09
    • generic
    • generic
    • Verified

    Backports

      Description

        Math.abs() with char type may return incorrect result after C2 superword optimization. It can be reproduced by below Java code and commands.

        public class Bug {
            private static int SIZE = 60000;
            private static char[] a = new char[SIZE];
            private static char[] b = new char[SIZE];

            public static void main(String[] args) {
                for (int i = 0; i < SIZE; i++) {
                    a[i] = b[i] = (char) i;
                }
                for (int i = 0; i < SIZE; i++) {
                    a[i] = (char) Math.abs(a[i]);
                }
                for (int i = 0; i < SIZE; i++) {
                    if (a[i] != b[i]) {
                        throw new RuntimeException("Broken!");
                    }
                }
                System.out.println("OK");
            }
        }

        // $ java -Xint Bug
        // OK

        // $ java -Xcomp -XX:-TieredCompilation Bug
        // Exception in thread "main" java.lang.RuntimeException: Broken!
        // at Bug.main(Bug.java:15)

        This may cause loss of data (although in few cases) so I would change the priority to P2.

        Affect versions: 13, 14, 15, 16, 17 (both x86 and AArch64) and 11u (x86-only)

        Attachments

          Issue Links

            Activity

              People

                pli Pengfei Li
                pli Pengfei Li
                Votes:
                0 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: