Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestOutOfBoundsArrayLoad.java
41149 views
/*1* Copyright (c) 2021 SAP SE. 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* @requires vm.gc.Serial27* @bug 826229528* @library /test/lib /29* @summary Out of bounds array load on clone source crashes GC which30* interpretes the loaded value as oop. A small heap is configured to31* get a lot of GCs.32*33* @comment C2 generates the out of bounds load with serial, parallel and34* shenandoah gc but not with g1 and z gc. For simplicity serial gc is35* configured.36*37* @build sun.hotspot.WhiteBox38* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox39* @run main/othervm -XX:+UseSerialGC -Xmx128m40* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.41* -XX:-BackgroundCompilation42* -XX:CompileCommand=dontinline,*::*_dontinline43* compiler.arraycopy.TestOutOfBoundsArrayLoad44*/4546package compiler.arraycopy;4748import compiler.whitebox.CompilerWhiteBoxTest;4950public class TestOutOfBoundsArrayLoad {5152public static Object escape1;53public static Object escape2;5455public static void main(String[] args_ignored) {56try {57Object[] arrNotEmpty = {null, null, null, null, null, };5859// Warm-up60for (int i = CompilerWhiteBoxTest.THRESHOLD; i > 0; i--) {61testMethod_dontinline(arrNotEmpty);62}63// Call testmethod with empty array often enough to trigger GC.64// GC is assumed to crash.65for (int i = 20_000_000; i > 0; i--) {66// Trick for ParallelGC: empty[4] will be loaded in the testmethod67// (out of bounds!) and interpreted as oop (or68// narrowOop). PSScavenge::should_scavenge() will skip the loaded69// value if it is before the young generation. So before calling the70// test method we allocate the empty array and an array of -1 values71// right behind it. So empty[4] will likely result in72// 0xffffffffffffffff Which is not before the young generation.73Object[] empty = new Object[0];74long[] l = new long[4];75l[0] = -1L; l[1] = -1L; l[2] = -1L; l[3] = -1L;76escape2 = l;77testMethod_dontinline(empty);78}79} catch (Throwable t) {80t.printStackTrace();81System.exit(1);82}83}8485public static void testMethod_dontinline(Object[] src) throws Exception {86Object[] clone = src.clone();87// Load L below is executed speculatively at this point from src without range check.88// The result is put into the OopMap of the allocation in the next line.89// If src.length is 0 then the loaded value is no heap reference and GC crashes.90escape1 = new Object();91if (src.length > 4) {92escape2 = clone[4]; // Load L93}94}95}969798