Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
/ jdk23u Public archive

Commit caf28d4

Browse files
Matias Saavedra Silvacoleenp
Matias Saavedra Silva
authored andcommitted
8338924: C1: assert(0 <= i && i < _len) failed: illegal index 5 for length 5
8335664: Parsing jsr broken: assert(bci>= 0 && bci < c->method()->code_size()) failed: index out of bounds Reviewed-by: coleenp Backport-of: 1353601
1 parent 54bb851 commit caf28d4

File tree

6 files changed

+138
-10
lines changed

6 files changed

+138
-10
lines changed

src/hotspot/share/c1/c1_GraphBuilder.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,11 @@ void GraphBuilder::jsr(int dest) {
13891389
// If the bytecodes are strange (jumping out of a jsr block) then we
13901390
// might end up trying to re-parse a block containing a jsr which
13911391
// has already been activated. Watch for this case and bail out.
1392+
if (next_bci() >= method()->code_size()) {
1393+
// This can happen if the subroutine does not terminate with a ret,
1394+
// effectively turning the jsr into a goto.
1395+
BAILOUT("too-complicated jsr/ret structure");
1396+
}
13921397
for (ScopeData* cur_scope_data = scope_data();
13931398
cur_scope_data != nullptr && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope();
13941399
cur_scope_data = cur_scope_data->parent()) {
@@ -3731,6 +3736,9 @@ bool GraphBuilder::try_inline_intrinsics(ciMethod* callee, bool ignore_return) {
37313736
bool GraphBuilder::try_inline_jsr(int jsr_dest_bci) {
37323737
// Introduce a new callee continuation point - all Ret instructions
37333738
// will be replaced with Gotos to this point.
3739+
if (next_bci() >= method()->code_size()) {
3740+
return false;
3741+
}
37343742
BlockBegin* cont = block_at(next_bci());
37353743
assert(cont != nullptr, "continuation must exist (BlockListBuilder starts a new block after a jsr");
37363744

src/hotspot/share/compiler/methodLiveness.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 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
@@ -222,6 +222,9 @@ void MethodLiveness::init_basic_blocks() {
222222
dest = _block_map->at(bytes.get_dest());
223223
assert(dest != nullptr, "branch destination must start a block.");
224224
dest->add_normal_predecessor(current_block);
225+
if (bci + Bytecodes::length_for(code) >= method_len) {
226+
break;
227+
}
225228
BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
226229
assert(jsrExit != nullptr, "jsr return bci must start a block.");
227230
jsr_exit_list->append(jsrExit);
@@ -232,6 +235,9 @@ void MethodLiveness::init_basic_blocks() {
232235
dest = _block_map->at(bytes.get_far_dest());
233236
assert(dest != nullptr, "branch destination must start a block.");
234237
dest->add_normal_predecessor(current_block);
238+
if (bci + Bytecodes::length_for(code) >= method_len) {
239+
break;
240+
}
235241
BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
236242
assert(jsrExit != nullptr, "jsr return bci must start a block.");
237243
jsr_exit_list->append(jsrExit);

src/hotspot/share/oops/generateOopMap.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 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
@@ -436,12 +436,12 @@ void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
436436
/* We will also mark successors of jsr's as basic block headers. */
437437
switch (bytecode) {
438438
case Bytecodes::_jsr:
439-
assert(!fellThrough, "should not happen");
440-
bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), nullptr);
441-
break;
442439
case Bytecodes::_jsr_w:
443440
assert(!fellThrough, "should not happen");
444-
bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), nullptr);
441+
// If this is the last bytecode, there is no successor to mark
442+
if (bci + Bytecodes::length_for(bytecode) < method()->code_size()) {
443+
bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), nullptr);
444+
}
445445
break;
446446
default:
447447
break;
@@ -502,7 +502,10 @@ void GenerateOopMap::mark_reachable_code() {
502502
case Bytecodes::_jsr:
503503
case Bytecodes::_jsr_w:
504504
assert(!fell_through, "should not happen");
505-
reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
505+
// If this is the last bytecode, there is no successor to mark
506+
if (bci + Bytecodes::length_for(bytecode) < method()->code_size()) {
507+
reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
508+
}
506509
break;
507510
default:
508511
break;
@@ -586,9 +589,6 @@ bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *
586589
case Bytecodes::_jsr:
587590
assert(bcs->is_wide()==false, "sanity check");
588591
(*jmpFct)(this, bcs->dest(), data);
589-
590-
591-
592592
break;
593593
case Bytecodes::_jsr_w:
594594
(*jmpFct)(this, bcs->dest_w(), data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
super public class LastJsr
26+
{
27+
public static Method test:"()V"
28+
stack 100 locals 100
29+
{
30+
return;
31+
LABEL:
32+
nop;
33+
jsr LABEL; // bci=2. Compute bci + length(jsr) -> bci = 5 accessed, out of bounds.
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
super public class LastJsrReachable
26+
{
27+
public static Method test:"()V"
28+
stack 100 locals 100
29+
{
30+
goto LB2;
31+
LABEL:
32+
return;
33+
LB2:
34+
nop;
35+
jsr LABEL;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 8335664 8338924
27+
* @summary Ensure a program that ends with a JSR does not crash
28+
* @library /test/lib
29+
* @compile LastJsr.jasm
30+
* @compile LastJsrReachable.jasm
31+
* @run main/othervm -Xbatch LastJsrTest
32+
*/
33+
34+
public class LastJsrTest {
35+
public static void main(String[] args) {
36+
for (int i = 0; i < 1000; ++i) {
37+
LastJsr.test();
38+
LastJsrReachable.test();
39+
}
40+
System.out.println("PASSED");
41+
}
42+
}

0 commit comments

Comments
 (0)