Skip to content

Commit 6a83392

Browse files
committed
8323682: C2: guard check is not generated in Arrays.copyOfRange intrinsic when allocation is eliminated by EA
Backport-of: 92f5c0be8e3b47343b54a26940df691faaf49b23
1 parent d13f75c commit 6a83392

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

src/hotspot/share/opto/library_call.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,12 +4272,16 @@ bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
42724272
length = _gvn.transform(new SubINode(end, start));
42734273
}
42744274

4275-
// Bail out if length is negative.
4275+
// Bail out if length is negative (i.e., if start > end).
42764276
// Without this the new_array would throw
42774277
// NegativeArraySizeException but IllegalArgumentException is what
42784278
// should be thrown
42794279
generate_negative_guard(length, bailout, &length);
42804280

4281+
// Bail out if start is larger than the original length
4282+
Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
4283+
generate_negative_guard(orig_tail, bailout, &orig_tail);
4284+
42814285
if (bailout->req() > 1) {
42824286
PreserveJVMState pjvms(this);
42834287
set_control(_gvn.transform(bailout));
@@ -4287,8 +4291,7 @@ bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
42874291

42884292
if (!stopped()) {
42894293
// How many elements will we copy from the original?
4290-
// The answer is MinI(orig_length - start, length).
4291-
Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
4294+
// The answer is MinI(orig_tail, length).
42924295
Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
42934296

42944297
// Generate a direct call to the right arraycopy function(s).
@@ -4336,7 +4339,7 @@ bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
43364339
if (!stopped()) {
43374340
newcopy = new_array(klass_node, length, 0); // no arguments to push
43384341

4339-
ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, false,
4342+
ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, true,
43404343
load_object_klass(original), klass_node);
43414344
if (!is_copyOfRange) {
43424345
ac->set_copyof(validated);

src/hotspot/share/opto/macroArrayCopy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, 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
@@ -1266,7 +1266,7 @@ void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
12661266
generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
12671267
adr_type, T_OBJECT,
12681268
src, src_offset, dest, dest_offset, length,
1269-
true, !ac->is_copyofrange());
1269+
true, ac->has_negative_length_guard());
12701270

12711271
return;
12721272
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2024, 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+
* @bug 8323682
27+
* @summary Test that the appropriate guards are generated for the copyOfRange
28+
* intrinsic, even if the result of the array copy is not used.
29+
*
30+
* @run main/othervm -XX:-TieredCompilation
31+
* -XX:CompileCommand=compileonly,compiler.arraycopy.TestArrayCopyOfRangeGuards::test
32+
* -Xbatch
33+
* compiler.arraycopy.TestArrayCopyOfRangeGuards
34+
*/
35+
36+
package compiler.arraycopy;
37+
38+
import java.util.Arrays;
39+
40+
public class TestArrayCopyOfRangeGuards {
41+
static int counter = 0;
42+
43+
public static void main(String[] args) {
44+
Object[] array = new Object[10];
45+
for (int i = 0; i < 50_000; i++) {
46+
test(array);
47+
}
48+
if (counter != 50_000) {
49+
throw new RuntimeException("Test failed");
50+
}
51+
}
52+
53+
static void test(Object[] array) {
54+
try {
55+
Arrays.copyOfRange(array, 15, 20, Object[].class);
56+
} catch (ArrayIndexOutOfBoundsException e) {
57+
// Expected
58+
counter++;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)