Path: blob/master/test/hotspot/jtreg/compiler/loopopts/TestEliminateNullCheckWithSplitIf.java
64478 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @key stress randomness27* @bug 827561028* @summary Null check for field access of object floats above null check resulting in a segfault.29* @requires vm.compiler2.enabled30* @run main/othervm -Xbatch -XX:CompileCommand=compileonly,compiler.loopopts.TestEliminateNullCheckWithSplitIf::test31* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:StressSeed=42 compiler.loopopts.TestEliminateNullCheckWithSplitIf32* @run main/othervm -Xbatch -XX:CompileCommand=compileonly,compiler.loopopts.TestEliminateNullCheckWithSplitIf::test33* -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressIGVN compiler.loopopts.TestEliminateNullCheckWithSplitIf34*/3536package compiler.loopopts;3738public class TestEliminateNullCheckWithSplitIf {39public static int[] iArrFld = new int[20];40public static int[] iArrFld2 = new int[20];41public static int iFld = 10;42public static MyClass obj;4344public static void main(String[] strArr) {45for (int i = 0; i < 10000; i++) {46obj = (i % 100 == 0 ? null : new MyClass());47test();48}49}5051// The field access obj.iFld requires a null check NC3 and adds a not-null CastPP node on the succeeded projection.52// In the first IGVN after parsing, the null check NC3 can be subsumed by the explicit null check NC2.53// (done in IfNode::simple_subsuming()). The Bool node of NC2 is also shared with the same null check NC1 earlier.54// However, C2 cannot remove the null check NC2, yet, because the IR in between the two checks are too complex55// (IfNode::search_identical() fails).56// Now, loopopts are applied:57// (1) First, the split if optimization is done. It recognizes that NC1 and NC2 are back to back null checks and removes58// the null check NC2 by splitting it through the region R which is removed afterwards. In this process, control dependent59// data nodes on the out projections of NC2 end up at the new regions R1/R2 created for each projection for R. They get60// the last nodes of the if and else block as input. For this example, R1 is a control input to the CastPP node which61// will merge both true projections.62// (2) Later in loop opts, the loop L is transformed into normal code and y will become a constant 1.63// After loopopts, another round of IGVN is done:64// (These steps also depend on the order in which they are applied in order to trigger the bug)65// (1) The region R is removed because one path is dead (a result of the split if optimization).66// (2) The new If node added by the above split if optimization is also folded. This rewires the CastPP node to67// the last control node in the If block which is the true projection of range check RC2. Up until now, the CastPP68// is still after the null check NC1.69// (3) The range check RC2 is removed because the range check RC1 already covers this range (see RangeCheck::Ideal()).70// All data nodes which are control dependent on RC2 will be rewired to the dominating range check RC1, including71// the non-null CastPP node - which now has a control input above the null check NC1. This also means that the field72// load obj.iFld now has the same early control as the CastPP (CastPP -> AddP -> LoadI). Using StressGCM can73// now schedule the obj.iFld load before the null check NC1 because the early control allows it which leads to a74// segmentation fault if obj is null.75public static void test() {76int x = iArrFld[17]; // Emits range check RC177if (obj != null) { // Null check NC178int y = 0;79for (int i = 0; i < 1; i++) { // Loop L80y++;81}82// Use additional loop to keep the rangecheck for iArrFld[y] in before loopopts.83// y will become constant 1 but only once the loop above is removed in loopopts.84x = iArrFld[y]; // Emits range check RC285} else {86x = iArrFld2[18];87}88// Region R merging the if and else paths above.89if (obj != null) { // Null check NC290x = iArrFld2[obj.iFld]; // Emits Null check NC3 for obj.iFld91}92}93}9495class MyClass {96int iFld;97}9899100101102103104