Skip to content

Commit 2b81fae

Browse files
committed
8303022: "assert(allocates2(pc)) failed: not in CodeBuffer memory" When linking downcall handle
Reviewed-by: kvn, vlivanov
1 parent be08a25 commit 2b81fae

File tree

8 files changed

+102
-21
lines changed

8 files changed

+102
-21
lines changed

src/hotspot/cpu/aarch64/downcallLinker_aarch64.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2019, Arm Limited. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -90,7 +90,8 @@ class DowncallStubGenerator : public StubCodeGenerator {
9090
}
9191
};
9292

93-
static const int native_invoker_code_size = 1024;
93+
static const int native_invoker_code_base_size = 256;
94+
static const int native_invoker_size_per_arg = 8;
9495

9596
RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
9697
int num_args,
@@ -100,8 +101,9 @@ RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
100101
const GrowableArray<VMStorage>& output_registers,
101102
bool needs_return_buffer,
102103
int captured_state_mask) {
103-
int locs_size = 64;
104-
CodeBuffer code("nep_invoker_blob", native_invoker_code_size, locs_size);
104+
int code_size = native_invoker_code_base_size + (num_args * native_invoker_size_per_arg);
105+
int locs_size = 1; // must be non-zero
106+
CodeBuffer code("nep_invoker_blob", code_size, locs_size);
105107
DowncallStubGenerator g(&code, signature, num_args, ret_bt, abi,
106108
input_registers, output_registers,
107109
needs_return_buffer, captured_state_mask);

src/hotspot/cpu/aarch64/upcallLinker_aarch64.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2019, 2022, Arm Limited. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -114,6 +114,9 @@ static void restore_callee_saved_registers(MacroAssembler* _masm, const ABIDescr
114114
__ block_comment("} restore_callee_saved_regs ");
115115
}
116116

117+
static const int upcall_stub_code_base_size = 1024;
118+
static const int upcall_stub_size_per_arg = 16;
119+
117120
address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
118121
BasicType* in_sig_bt, int total_in_args,
119122
BasicType* out_sig_bt, int total_out_args,
@@ -123,7 +126,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
123126
ResourceMark rm;
124127
const ABIDescriptor abi = ForeignGlobals::parse_abi_descriptor(jabi);
125128
const CallRegs call_regs = ForeignGlobals::parse_call_regs(jconv);
126-
CodeBuffer buffer("upcall_stub", /* code_size = */ 2048, /* locs_size = */ 1024);
129+
int code_size = upcall_stub_code_base_size + (total_in_args * upcall_stub_size_per_arg);
130+
CodeBuffer buffer("upcall_stub", code_size, /* locs_size = */ 1);
127131

128132
Register shuffle_reg = r19;
129133
JavaCallingConvention out_conv;
@@ -325,6 +329,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
325329
const char* name = "upcall_stub";
326330
#endif // PRODUCT
327331

332+
buffer.log_section_sizes(name);
333+
328334
UpcallStub* blob
329335
= UpcallStub::create(name,
330336
&buffer,

src/hotspot/cpu/riscv/downcallLinker_riscv.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -91,7 +91,8 @@ class DowncallStubGenerator : public StubCodeGenerator {
9191
}
9292
};
9393

94-
static const int native_invoker_code_size = 1024;
94+
static const int native_invoker_code_base_size = 256;
95+
static const int native_invoker_size_per_arg = 8;
9596

9697
RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
9798
int num_args,
@@ -101,8 +102,9 @@ RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
101102
const GrowableArray<VMStorage>& output_registers,
102103
bool needs_return_buffer,
103104
int captured_state_mask) {
104-
int locs_size = 64;
105-
CodeBuffer code("nep_invoker_blob", native_invoker_code_size, locs_size);
105+
int code_size = native_invoker_code_base_size + (num_args * native_invoker_size_per_arg);
106+
int locs_size = 1; // must be non-zero
107+
CodeBuffer code("nep_invoker_blob", code_size, locs_size);
106108
DowncallStubGenerator g(&code, signature, num_args, ret_bt, abi,
107109
input_registers, output_registers,
108110
needs_return_buffer, captured_state_mask);

src/hotspot/cpu/riscv/upcallLinker_riscv.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -114,6 +114,9 @@ static void restore_callee_saved_registers(MacroAssembler* _masm, const ABIDescr
114114
__ block_comment("} restore_callee_saved_regs ");
115115
}
116116

117+
static const int upcall_stub_code_base_size = 2048;
118+
static const int upcall_stub_size_per_arg = 16;
119+
117120
address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
118121
BasicType* in_sig_bt, int total_in_args,
119122
BasicType* out_sig_bt, int total_out_args,
@@ -124,7 +127,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
124127
ResourceMark rm;
125128
const ABIDescriptor abi = ForeignGlobals::parse_abi_descriptor(jabi);
126129
const CallRegs call_regs = ForeignGlobals::parse_call_regs(jconv);
127-
CodeBuffer buffer("upcall_stub", /* code_size = */ 2048, /* locs_size = */ 1024);
130+
int code_size = upcall_stub_code_base_size + (total_in_args * upcall_stub_size_per_arg);
131+
CodeBuffer buffer("upcall_stub", code_size, /* locs_size = */ 1);
128132

129133
Register shuffle_reg = x9;
130134
JavaCallingConvention out_conv;
@@ -343,6 +347,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
343347
const char* name = "upcall_stub";
344348
#endif // PRODUCT
345349

350+
buffer.log_section_sizes(name);
351+
346352
UpcallStub* blob
347353
= UpcallStub::create(name,
348354
&buffer,

src/hotspot/cpu/x86/downcallLinker_x86_64.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, 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
@@ -89,7 +89,8 @@ class DowncallStubGenerator : public StubCodeGenerator {
8989
}
9090
};
9191

92-
static const int native_invoker_code_size = 1024;
92+
static const int native_invoker_code_base_size = 512;
93+
static const int native_invoker_size_per_arg = 8;
9394

9495
RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
9596
int num_args,
@@ -99,8 +100,9 @@ RuntimeStub* DowncallLinker::make_downcall_stub(BasicType* signature,
99100
const GrowableArray<VMStorage>& output_registers,
100101
bool needs_return_buffer,
101102
int captured_state_mask) {
102-
int locs_size = 64;
103-
CodeBuffer code("nep_invoker_blob", native_invoker_code_size, locs_size);
103+
int code_size = native_invoker_code_base_size + (num_args * native_invoker_size_per_arg);
104+
int locs_size = 1; // can not be zero
105+
CodeBuffer code("nep_invoker_blob", code_size, locs_size);
104106
DowncallStubGenerator g(&code, signature, num_args, ret_bt, abi,
105107
input_registers, output_registers,
106108
needs_return_buffer, captured_state_mask);

src/hotspot/cpu/x86/upcallLinker_x86_64.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, 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
@@ -165,6 +165,9 @@ static void restore_callee_saved_registers(MacroAssembler* _masm, const ABIDescr
165165
__ block_comment("} restore_callee_saved_regs ");
166166
}
167167

168+
static const int upcall_stub_code_base_size = 2048;
169+
static const int upcall_stub_size_per_arg = 16;
170+
168171
address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
169172
BasicType* in_sig_bt, int total_in_args,
170173
BasicType* out_sig_bt, int total_out_args,
@@ -173,7 +176,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
173176
bool needs_return_buffer, int ret_buf_size) {
174177
const ABIDescriptor abi = ForeignGlobals::parse_abi_descriptor(jabi);
175178
const CallRegs call_regs = ForeignGlobals::parse_call_regs(jconv);
176-
CodeBuffer buffer("upcall_stub", /* code_size = */ 2048, /* locs_size = */ 1024);
179+
int code_size = upcall_stub_code_base_size + (total_in_args * upcall_stub_size_per_arg);
180+
CodeBuffer buffer("upcall_stub", code_size, /* locs_size = */ 1);
177181

178182
VMStorage shuffle_reg = as_VMStorage(rbx);
179183
JavaCallingConvention out_conv;
@@ -386,6 +390,8 @@ address UpcallLinker::make_upcall_stub(jobject receiver, Method* entry,
386390
const char* name = "upcall_stub";
387391
#endif // PRODUCT
388392

393+
buffer.log_section_sizes(name);
394+
389395
UpcallStub* blob
390396
= UpcallStub::create(name,
391397
&buffer,

src/hotspot/share/asm/codeBuffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,12 +998,12 @@ void CodeBuffer::log_section_sizes(const char* name) {
998998
if (xtty != NULL) {
999999
ttyLocker ttyl;
10001000
// log info about buffer usage
1001-
xtty->print_cr("<blob name='%s' size='%d'>", name, _total_size);
1001+
xtty->print_cr("<blob name='%s' total_size='%d'>", name, _total_size);
10021002
for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
10031003
CodeSection* sect = code_section(n);
10041004
if (!sect->is_allocated() || sect->is_empty()) continue;
1005-
xtty->print_cr("<sect index='%d' size='" SIZE_FORMAT "' free='" SIZE_FORMAT "'/>",
1006-
n, sect->limit() - sect->start(), sect->limit() - sect->end());
1005+
xtty->print_cr("<sect index='%d' capacity='%d' size='%d' remaining='%d'/>",
1006+
n, sect->capacity(), sect->size(), sect->remaining());
10071007
}
10081008
xtty->print_cr("</blob>");
10091009
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2023, 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+
* @enablePreview
27+
* @library ../
28+
* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64" | os.arch == "riscv64"
29+
* @modules java.base/jdk.internal.foreign
30+
* @run testng/othervm --enable-native-access=ALL-UNNAMED TestLargeStub
31+
*/
32+
33+
import org.testng.annotations.Test;
34+
35+
import java.lang.foreign.FunctionDescriptor;
36+
import java.lang.foreign.Linker;
37+
import java.lang.foreign.MemoryLayout;
38+
import java.util.stream.Stream;
39+
40+
public class TestLargeStub extends NativeTestHelper {
41+
@Test
42+
public void testDowncall() {
43+
// Link a handle with a large number of arguments, to try and overflow the code buffer
44+
Linker.nativeLinker().downcallHandle(
45+
FunctionDescriptor.of(C_LONG_LONG,
46+
Stream.generate(() -> C_DOUBLE).limit(50).toArray(MemoryLayout[]::new)),
47+
Linker.Option.captureCallState("errno"));
48+
}
49+
50+
@Test
51+
public void testUpcall() {
52+
// Link a handle with a large number of arguments, to try and overflow the code buffer
53+
Linker.nativeLinker().downcallHandle(
54+
FunctionDescriptor.of(C_LONG_LONG,
55+
Stream.generate(() -> C_DOUBLE).limit(50).toArray(MemoryLayout[]::new)));
56+
}
57+
}

0 commit comments

Comments
 (0)