8191915: JCK tests produce incorrect results with C2
authorrbackman
Thu, 18 Jan 2018 19:21:11 +0100
changeset 48809 a81c930a8838
parent 48808 2b0b7f222800
child 48810 1f7ebe9dd5b2
8191915: JCK tests produce incorrect results with C2 Reviewed-by: thartmann, vlivanov, goetz
src/hotspot/share/opto/mathexactnode.cpp
src/hotspot/share/opto/mathexactnode.hpp
test/hotspot/gtest/opto/test_mathexact.cpp
test/hotspot/jtreg/compiler/intrinsics/mathexact/LongMulOverflowTest.java
--- a/src/hotspot/share/opto/mathexactnode.cpp	Tue Jan 23 14:27:10 2018 -0500
+++ b/src/hotspot/share/opto/mathexactnode.cpp	Thu Jan 18 19:21:11 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -117,23 +117,33 @@
   return SubHelper<OverflowSubLNode>::will_overflow(v1, v2);
 }
 
-bool OverflowMulLNode::will_overflow(jlong val1, jlong val2) const {
-    jlong result = val1 * val2;
-    jlong ax = (val1 < 0 ? -val1 : val1);
-    jlong ay = (val2 < 0 ? -val2 : val2);
+bool OverflowMulLNode::is_overflow(jlong val1, jlong val2) {
+    // x * { 0, 1 } will never overflow. Even for x = min_jlong
+    if (val1 == 0 || val2 == 0 || val1 == 1 || val2 == 1) {
+      return false;
+    }
 
-    bool overflow = false;
-    if ((ax | ay) & CONST64(0xFFFFFFFF00000000)) {
-      // potential overflow if any bit in upper 32 bits are set
-      if ((val1 == min_jlong && val2 == -1) || (val2 == min_jlong && val1 == -1)) {
-        // -1 * Long.MIN_VALUE will overflow
-        overflow = true;
-      } else if (val2 != 0 && (result / val2 != val1)) {
-        overflow = true;
-      }
+    // x * min_jlong for x not in { 0, 1 } overflows
+    // even -1 as -1 * min_jlong is an overflow
+    if (val1 == min_jlong || val2 == min_jlong) {
+      return true;
     }
 
-    return overflow;
+    // if (x * y) / y == x there is no overflow
+    //
+    // the multiplication here is done as unsigned to avoid undefined behaviour which
+    // can be used by the compiler to assume that the check further down (result / val2 != val1)
+    // is always false and breaks the overflow check
+    julong v1 = (julong) val1;
+    julong v2 = (julong) val2;
+    julong tmp = v1 * v2;
+    jlong result = (jlong) tmp;
+
+    if (result / val2 != val1) {
+      return true;
+    }
+
+    return false;
 }
 
 bool OverflowAddINode::can_overflow(const Type* t1, const Type* t2) const {
--- a/src/hotspot/share/opto/mathexactnode.hpp	Tue Jan 23 14:27:10 2018 -0500
+++ b/src/hotspot/share/opto/mathexactnode.hpp	Thu Jan 18 19:21:11 2018 +0100
@@ -129,8 +129,10 @@
   OverflowMulLNode(Node* in1, Node* in2) : OverflowLNode(in1, in2) {}
   virtual int Opcode() const;
 
-  virtual bool will_overflow(jlong v1, jlong v2) const;
+  virtual bool will_overflow(jlong v1, jlong v2) const { return is_overflow(v1, v2); }
   virtual bool can_overflow(const Type* t1, const Type* t2) const;
+
+  static bool is_overflow(jlong v1, jlong v2);
 };
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/gtest/opto/test_mathexact.cpp	Thu Jan 18 19:21:11 2018 +0100
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+#include "precompiled.hpp"
+#include "opto/mulnode.hpp"
+#include "opto/mathexactnode.hpp"
+#include "unittest.hpp"
+
+TEST_VM(opto, mathexact) {
+  ASSERT_FALSE(OverflowMulLNode::is_overflow(1, 1));
+  ASSERT_FALSE(OverflowMulLNode::is_overflow(1, min_jlong));
+  ASSERT_TRUE(OverflowMulLNode::is_overflow(-1, min_jlong));
+  ASSERT_FALSE(OverflowMulLNode::is_overflow(-1, max_jlong));
+  ASSERT_TRUE(OverflowMulLNode::is_overflow(max_jlong / 2 + 1, 2));
+  ASSERT_FALSE(OverflowMulLNode::is_overflow(min_jlong, 0));
+  ASSERT_FALSE(OverflowMulLNode::is_overflow(min_jlong + 1, -1));
+  ASSERT_TRUE(OverflowMulLNode::is_overflow(min_jlong + 1, 2));
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/intrinsics/mathexact/LongMulOverflowTest.java	Thu Jan 18 19:21:11 2018 +0100
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8191915
+ * @summary Regression test for multiplyExact intrinsic
+ * @library /test/lib /
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ *
+ * @run main/othervm -Xcomp -XX:-TieredCompilation compiler.intrinsics.mathexact.LongMulOverflowTest
+ */
+
+package compiler.intrinsics.mathexact;
+
+public class LongMulOverflowTest {
+    public static void main(String[] args) {
+        LongMulOverflowTest test = new LongMulOverflowTest();
+        for (int i = 0; i < 10; ++i) {
+            try {
+                test.runTest();
+                throw new RuntimeException("Error, runTest() did not overflow!");
+            } catch (ArithmeticException e) {
+                // success
+            }
+
+            try {
+                test.runTestOverflow();
+                throw new RuntimeException("Error, runTestOverflow() did not overflow!");
+            } catch (ArithmeticException e) {
+                // success
+            }
+        }
+    }
+
+    public void runTest() {
+        java.lang.Math.multiplyExact(Long.MIN_VALUE, 7);
+    }
+
+    public void runTestOverflow() {
+      java.lang.Math.multiplyExact((Long.MAX_VALUE / 2) + 1, 2);
+    }
+}