JDK-8261022 : Fix incorrect result of Math.abs() with char type
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 11.0.10,13,14,15,16,17
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2021-02-03
  • Updated: 2021-11-05
  • Resolved: 2021-02-07
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 15 JDK 16 JDK 17
11.0.11Fixed 13.0.7Fixed 15.0.4Fixed 16.0.1Fixed 17 b09Fixed
Related Reports
Relates :  
Relates :  
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)

Comments
Fix request (15u) Requesting backport to 15u for parity with 11u. The patch applies cleanly, but requires an adjustment to be compiled. Tested with tier1; new test fails without the patch, passes with it. RFR: http://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-May/006237.html
18-05-2021

Fix request (13u) Requesting backport to 13u for parity with 11u. The patch applies cleanly, but requires some modification due to absence of VectorNode::is_shift_opcode in 13u (JDK-8257625 is not in 13u), replaced by similar VectorNode::is_shift. Tested with tier1; new test fails without the patch, passes with it. RFR: http://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-March/005360.html
18-03-2021

I'll handle this for you in both, 11u and 16u.
17-02-2021

Review thread for jdk11u: https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-February/004943.html Can anyone help move forward the backporting process (for both 11u and 16u)? I don't have the commit right of the jdk-update repos.
15-02-2021

Hi [~pli], coud you please post your 11u patch for review on jdk-updates-dev? Cheers, Christoph
08-02-2021

Fix Request (for jdk11u) This issue causes vectorized abs generate incorrect result when the argument has char type. Root cause is that the vector abs operation is not specially handled in computing vector element types after we enabled that in JDK-8222074 in jdk13. As JDK-8222074 was backported to jdk11u, jdk11u is also affected. The patch to fix this (https://github.com/openjdk/jdk/commit/7a2db858) was merged in jdk17. This patch does not apply to jdk11u cleanly, as VectorNode::is_shift_opcode() is not defined in jdk11u. I have modified the patch a little bit to fit this difference. Updated patch is uploaded at http://cr.openjdk.java.net/~pli/rfr/8261022/for11u.patch Tested treg hotspot::tier1 and the newly added case. No failure after the modified patch.
08-02-2021

Fix Request (for jdk16u) This issue causes vectorized abs generate incorrect result when the argument has char type. Root cause is that the vector abs operation is not specially handled in computing vector element types after we enabled that in JDK-8222074 in jdk13. The patch to fix this (https://github.com/openjdk/jdk/commit/7a2db858) was merged in jdk17. This patch applies to jdk16u cleanly. Jtreg hotspot::tier1 and the newly added case in the fix patch pass after the backport.
08-02-2021

Changeset: 7a2db858 Author: Pengfei Li <pli@openjdk.org> Date: 2021-02-07 01:15:24 +0000 URL: https://git.openjdk.java.net/jdk/commit/7a2db858
07-02-2021

It is late for JDK 16 (P1 only fixes) and it is not new regression. I suggest to push it into jdk 16u (update) repo and backport into 11u.
05-02-2021

Right, JDK-8222074 was backported to jdk-11u. That's why this is also reproduceable in 11u (x86).
05-02-2021

Looks like this was introduced by JDK-8222074.
05-02-2021

Thanks for the update!
05-02-2021

Yes, I'm working on a patch to fix and will post it to Github soon
05-02-2021

We need more information to properly triage this. Is this a C2 issue?
04-02-2021