Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit c1fb521

Browse files
committed
8259227: C2 crashes with SIGFPE due to a division that floats above its zero check
Reviewed-by: kvn, thartmann
1 parent 484e23b commit c1fb521

File tree

7 files changed

+109
-26
lines changed

7 files changed

+109
-26
lines changed

src/hotspot/share/opto/ifnode.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,14 +1477,16 @@ Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) {
14771477
// Loop ends when projection has no more uses.
14781478
for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
14791479
Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
1480-
if( !s->depends_only_on_test() ) {
1480+
if (s->depends_only_on_test() && igvn->no_dependent_zero_check(s)) {
1481+
// For control producers.
1482+
// Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check.
1483+
igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1484+
} else {
14811485
// Find the control input matching this def-use edge.
14821486
// For Regions it may not be in slot 0.
14831487
uint l;
1484-
for( l = 0; s->in(l) != ifp; l++ ) { }
1488+
for (l = 0; s->in(l) != ifp; l++) { }
14851489
igvn->replace_input_of(s, l, ctrl_target);
1486-
} else { // Else, for control producers,
1487-
igvn->replace_input_of(s, 0, data_target); // Move child to data-target
14881490
}
14891491
} // End for each child of a projection
14901492

src/hotspot/share/opto/loopnode.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,6 @@ class PhaseIdealLoop : public PhaseTransform {
14511451
// Mark an IfNode as being dominated by a prior test,
14521452
// without actually altering the CFG (and hence IDOM info).
14531453
void dominated_by( Node *prevdom, Node *iff, bool flip = false, bool exclude_loop_predicate = false );
1454-
bool no_dependent_zero_check(Node* n) const;
14551454

14561455
// Split Node 'n' through merge point
14571456
Node *split_thru_region( Node *n, Node *region );

src/hotspot/share/opto/loopopts.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip, bool exc
283283
for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
284284
Node* cd = dp->fast_out(i); // Control-dependent node
285285
// Do not rewire Div and Mod nodes which could have a zero divisor to avoid skipping their zero check.
286-
if (cd->depends_only_on_test() && no_dependent_zero_check(cd)) {
286+
if (cd->depends_only_on_test() && _igvn.no_dependent_zero_check(cd)) {
287287
assert(cd->in(0) == dp, "");
288288
_igvn.replace_input_of(cd, 0, prevdom);
289289
set_early_ctrl(cd, false);
@@ -302,25 +302,6 @@ void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip, bool exc
302302
}
303303
}
304304

305-
// Check if the type of a divisor of a Div or Mod node includes zero.
306-
bool PhaseIdealLoop::no_dependent_zero_check(Node* n) const {
307-
switch (n->Opcode()) {
308-
case Op_DivI:
309-
case Op_ModI: {
310-
// Type of divisor includes 0?
311-
const TypeInt* type_divisor = _igvn.type(n->in(2))->is_int();
312-
return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
313-
}
314-
case Op_DivL:
315-
case Op_ModL: {
316-
// Type of divisor includes 0?
317-
const TypeLong* type_divisor = _igvn.type(n->in(2))->is_long();
318-
return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
319-
}
320-
}
321-
return true;
322-
}
323-
324305
//------------------------------has_local_phi_input----------------------------
325306
// Return TRUE if 'n' has Phi inputs from its local block and no other
326307
// block-local inputs (all non-local-phi inputs come from earlier blocks)

src/hotspot/share/opto/phaseX.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,25 @@ void PhaseIterGVN::remove_speculative_types() {
16591659
_table.check_no_speculative_types();
16601660
}
16611661

1662+
// Check if the type of a divisor of a Div or Mod node includes zero.
1663+
bool PhaseIterGVN::no_dependent_zero_check(Node* n) const {
1664+
switch (n->Opcode()) {
1665+
case Op_DivI:
1666+
case Op_ModI: {
1667+
// Type of divisor includes 0?
1668+
const TypeInt* type_divisor = type(n->in(2))->is_int();
1669+
return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
1670+
}
1671+
case Op_DivL:
1672+
case Op_ModL: {
1673+
// Type of divisor includes 0?
1674+
const TypeLong* type_divisor = type(n->in(2))->is_long();
1675+
return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
1676+
}
1677+
}
1678+
return true;
1679+
}
1680+
16621681
//=============================================================================
16631682
#ifndef PRODUCT
16641683
uint PhaseCCP::_total_invokes = 0;

src/hotspot/share/opto/phaseX.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class PhaseIterGVN : public PhaseGVN {
546546
}
547547

548548
bool is_dominator(Node *d, Node *n) { return is_dominator_helper(d, n, false); }
549+
bool no_dependent_zero_check(Node* n) const;
549550

550551
#ifndef PRODUCT
551552
protected:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
/*
26+
* @test
27+
* @key stress randomness
28+
* @bug 8259227
29+
* @summary Verify that zero check is executed before division/modulo operation.
30+
* @requires vm.compiler2.enabled
31+
* @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=compiler/loopopts/TestDivZeroDominatedBy::test
32+
* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:StressSeed=917280111 compiler.loopopts.TestDivZeroDominatedBy
33+
* @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=compiler/loopopts/TestDivZeroDominatedBy::test
34+
* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM compiler.loopopts.TestDivZeroDominatedBy
35+
*/
36+
37+
package compiler.loopopts;
38+
39+
public class TestDivZeroDominatedBy {
40+
41+
public static int iFld = 1;
42+
public static int iFld1 = 2;
43+
public static int iFld2 = 3;
44+
public static int iArrFld[] = new int[10];
45+
public static double dFld = 1.0;
46+
47+
public static void test() {
48+
int x = 1;
49+
int y = 2;
50+
int z = 3;
51+
52+
iFld = y;
53+
iArrFld[5] += iFld1;
54+
int i = 1;
55+
do {
56+
for (int j = 0; j < 10; j++) {
57+
iFld2 += iFld2;
58+
iFld1 = iFld2;
59+
int k = 1;
60+
do {
61+
iArrFld[k] = y;
62+
z = iFld2;
63+
dFld = x;
64+
try {
65+
y = iArrFld[k];
66+
iArrFld[8] = 5;
67+
iFld = (100 / z);
68+
} catch (ArithmeticException a_e) {}
69+
} while (++k < 2);
70+
}
71+
} while (++i < 10);
72+
}
73+
74+
public static void main(String[] strArr) {
75+
test();
76+
}
77+
}
78+

test/hotspot/jtreg/compiler/loopopts/TestDivZeroWithSplitIf.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,11 +24,14 @@
2424

2525
/*
2626
* @test
27+
* @key stress randomness
2728
* @bug 8257822
2829
* @summary Verify that zero check is executed before division/modulo operation.
2930
* @requires vm.compiler2.enabled
3031
* @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=compiler/loopopts/TestDivZeroWithSplitIf::test
3132
* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:StressSeed=873732072 compiler.loopopts.TestDivZeroWithSplitIf
33+
* @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=compiler/loopopts/TestDivZeroWithSplitIf::test
34+
* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM compiler.loopopts.TestDivZeroWithSplitIf
3235
*/
3336

3437
package compiler.loopopts;

0 commit comments

Comments
 (0)