Skip to content

Commit 6e60332

Browse files
committed
8269795: C2: Out of bounds array load floats above its range check in loop peeling resulting in SEGV
Reviewed-by: roland Backport-of: 040c02b
1 parent 69697b3 commit 6e60332

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

src/hotspot/share/opto/loopTransform.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -381,35 +381,39 @@ bool IdealLoopTree::policy_peeling( PhaseIdealLoop *phase ) const {
381381
// If we got the effect of peeling, either by actually peeling or by making
382382
// a pre-loop which must execute at least once, we can remove all
383383
// loop-invariant dominated tests in the main body.
384-
void PhaseIdealLoop::peeled_dom_test_elim( IdealLoopTree *loop, Node_List &old_new ) {
384+
void PhaseIdealLoop::peeled_dom_test_elim(IdealLoopTree* loop, Node_List& old_new) {
385385
bool progress = true;
386-
while( progress ) {
387-
progress = false; // Reset for next iteration
388-
Node *prev = loop->_head->in(LoopNode::LoopBackControl);//loop->tail();
389-
Node *test = prev->in(0);
390-
while( test != loop->_head ) { // Scan till run off top of loop
391-
386+
while (progress) {
387+
progress = false; // Reset for next iteration
388+
Node* prev = loop->_head->in(LoopNode::LoopBackControl); // loop->tail();
389+
Node* test = prev->in(0);
390+
while (test != loop->_head) { // Scan till run off top of loop
392391
int p_op = prev->Opcode();
393-
if( (p_op == Op_IfFalse || p_op == Op_IfTrue) &&
394-
test->is_If() && // Test?
395-
!test->in(1)->is_Con() && // And not already obvious?
396-
// Condition is not a member of this loop?
397-
!loop->is_member(get_loop(get_ctrl(test->in(1))))){
392+
assert(test != NULL, "test cannot be NULL");
393+
Node* test_cond = NULL;
394+
if ((p_op == Op_IfFalse || p_op == Op_IfTrue) && test->is_If()) {
395+
test_cond = test->in(1);
396+
}
397+
if (test_cond != NULL && // Test?
398+
!test_cond->is_Con() && // And not already obvious?
399+
// And condition is not a member of this loop?
400+
!loop->is_member(get_loop(get_ctrl(test_cond)))) {
398401
// Walk loop body looking for instances of this test
399-
for( uint i = 0; i < loop->_body.size(); i++ ) {
400-
Node *n = loop->_body.at(i);
401-
if( n->is_If() && n->in(1) == test->in(1) /*&& n != loop->tail()->in(0)*/ ) {
402+
for (uint i = 0; i < loop->_body.size(); i++) {
403+
Node* n = loop->_body.at(i);
404+
// Check against cached test condition because dominated_by()
405+
// replaces the test condition with a constant.
406+
if (n->is_If() && n->in(1) == test_cond) {
402407
// IfNode was dominated by version in peeled loop body
403408
progress = true;
404-
dominated_by( old_new[prev->_idx], n );
409+
dominated_by(old_new[prev->_idx], n);
405410
}
406411
}
407412
}
408413
prev = test;
409414
test = idom(test);
410415
} // End of scan tests in loop
411-
412-
} // End of while( progress )
416+
} // End of while (progress)
413417
}
414418

415419
//------------------------------do_peeling-------------------------------------
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* @test
26+
* @key stress
27+
* @requires vm.compiler2.enabled
28+
* @bug 8269795
29+
* @summary PhaseIdealLoop::peeled_dom_test_elim wrongly moves a non-dominated test out of a loop together with control dependent data nodes.
30+
* This results in a crash due to an out of bounds read of an array.
31+
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -Xcomp -XX:-TieredCompilation -XX:+StressGCM
32+
* -XX:CompileCommand=compileonly,compiler.loopopts.TestPeelingRemoveDominatedTest compiler.loopopts.TestPeelingRemoveDominatedTest
33+
*/
34+
35+
package compiler.loopopts;
36+
37+
public class TestPeelingRemoveDominatedTest {
38+
public static int N = 400;
39+
static boolean bFld = true;
40+
static int iArrFld[] = new int[N];
41+
42+
public static void main(String[] strArr) {
43+
TestPeelingRemoveDominatedTest _instance = new TestPeelingRemoveDominatedTest();
44+
for (int i = 0; i < 10; i++) {
45+
_instance.mainTest();
46+
}
47+
}
48+
49+
public void mainTest() {
50+
vMeth();
51+
}
52+
53+
54+
static void vMeth() {
55+
iArrFld[1] = 2;
56+
int i6 = 2;
57+
while (--i6 > 0) {
58+
try {
59+
int i3 = (iArrFld[i6 - 1] / 56);
60+
iArrFld[1] = (-139 % i3);
61+
} catch (ArithmeticException a_e) {
62+
}
63+
if (bFld) {
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)