1 /*
2 * Copyright (c) 1998, 2026, 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 #include "code/aotCodeCache.hpp"
26 #include "code/codeBlob.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/relocInfo.hpp"
29 #include "code/vtableStubs.hpp"
30 #include "compiler/disassembler.hpp"
31 #include "compiler/oopMap.hpp"
32 #include "cppstdlib/type_traits.hpp"
33 #include "interpreter/bytecode.hpp"
34 #include "interpreter/interpreter.hpp"
35 #include "jvm.h"
36 #include "memory/allocation.inline.hpp"
37 #include "memory/heap.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "prims/forte.hpp"
41 #include "prims/jvmtiExport.hpp"
42 #include "runtime/handles.inline.hpp"
43 #include "runtime/icache.hpp"
44 #include "runtime/interfaceSupport.inline.hpp"
45 #include "runtime/javaFrameAnchor.hpp"
46 #include "runtime/jniHandles.inline.hpp"
47 #include "runtime/mutexLocker.hpp"
48 #include "runtime/safepoint.hpp"
49 #include "runtime/sharedRuntime.hpp"
50 #include "runtime/stubCodeGenerator.hpp"
51 #include "runtime/stubRoutines.hpp"
52 #include "runtime/vframe.hpp"
53 #include "services/memoryService.hpp"
54 #include "utilities/align.hpp"
55 #ifdef COMPILER1
56 #include "c1/c1_Runtime1.hpp"
57 #endif
58
59 // Virtual methods are not allowed in code blobs to simplify caching compiled code.
60 // Check all "leaf" subclasses of CodeBlob class.
61
62 static_assert(!std::is_polymorphic<nmethod>::value, "no virtual methods are allowed in nmethod");
63 static_assert(!std::is_polymorphic<AdapterBlob>::value, "no virtual methods are allowed in code blobs");
64 static_assert(!std::is_polymorphic<VtableBlob>::value, "no virtual methods are allowed in code blobs");
65 static_assert(!std::is_polymorphic<MethodHandlesAdapterBlob>::value, "no virtual methods are allowed in code blobs");
66 static_assert(!std::is_polymorphic<RuntimeStub>::value, "no virtual methods are allowed in code blobs");
67 static_assert(!std::is_polymorphic<BufferedInlineTypeBlob>::value, "no virtual methods are allowed in code blobs");
68 static_assert(!std::is_polymorphic<DeoptimizationBlob>::value, "no virtual methods are allowed in code blobs");
69 static_assert(!std::is_polymorphic<SafepointBlob>::value, "no virtual methods are allowed in code blobs");
70 static_assert(!std::is_polymorphic<UpcallStub>::value, "no virtual methods are allowed in code blobs");
71 #ifdef COMPILER2
72 static_assert(!std::is_polymorphic<ExceptionBlob>::value, "no virtual methods are allowed in code blobs");
73 static_assert(!std::is_polymorphic<UncommonTrapBlob>::value, "no virtual methods are allowed in code blobs");
74 #endif
75
76 // Add proxy vtables.
77 // We need only few for now - they are used only from prints.
78 const nmethod::Vptr nmethod::_vpntr;
79 const BufferBlob::Vptr BufferBlob::_vpntr;
80 const RuntimeStub::Vptr RuntimeStub::_vpntr;
81 const SingletonBlob::Vptr SingletonBlob::_vpntr;
82 const DeoptimizationBlob::Vptr DeoptimizationBlob::_vpntr;
83 const SafepointBlob::Vptr SafepointBlob::_vpntr;
84 #ifdef COMPILER2
85 const ExceptionBlob::Vptr ExceptionBlob::_vpntr;
86 const UncommonTrapBlob::Vptr UncommonTrapBlob::_vpntr;
87 #endif // COMPILER2
88 const UpcallStub::Vptr UpcallStub::_vpntr;
89
90 const CodeBlob::Vptr* CodeBlob::vptr(CodeBlobKind kind) {
91 constexpr const CodeBlob::Vptr* array[(size_t)CodeBlobKind::Number_Of_Kinds] = {
92 nullptr/* None */,
93 &nmethod::_vpntr,
94 &BufferBlob::_vpntr,
95 &AdapterBlob::_vpntr,
96 &VtableBlob::_vpntr,
97 &MethodHandlesAdapterBlob::_vpntr,
98 &BufferedInlineTypeBlob::_vpntr,
99 &RuntimeStub::_vpntr,
100 &DeoptimizationBlob::_vpntr,
101 &SafepointBlob::_vpntr,
102 #ifdef COMPILER2
103 &ExceptionBlob::_vpntr,
104 &UncommonTrapBlob::_vpntr,
105 #endif
106 &UpcallStub::_vpntr
107 };
108
109 return array[(size_t)kind];
110 }
111
112 const CodeBlob::Vptr* CodeBlob::vptr() const {
113 return vptr(_kind);
114 }
115
116 unsigned int CodeBlob::align_code_offset(int offset) {
117 // align the size to CodeEntryAlignment
118 int header_size = (int)CodeHeap::header_size();
119 return align_up(offset + header_size, CodeEntryAlignment) - header_size;
120 }
121
122 // This must be consistent with the CodeBlob constructor's layout actions.
123 unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) {
124 // align the size to CodeEntryAlignment
125 unsigned int size = align_code_offset(header_size);
126 size += align_up(cb->total_content_size(), oopSize);
127 size += align_up(cb->total_oop_size(), oopSize);
128 return size;
129 }
130
131 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size,
132 int16_t frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments,
133 int mutable_data_size) :
134 _oop_maps(nullptr), // will be set by set_oop_maps() call
135 _name(name),
136 _mutable_data(header_begin() + size), // default value is blob_end()
137 _size(size),
138 _relocation_size(align_up(cb->total_relocation_size(), oopSize)),
139 _content_offset(CodeBlob::align_code_offset(header_size)),
140 _code_offset(_content_offset + cb->total_offset_of(cb->insts())),
141 _data_offset(_content_offset + align_up(cb->total_content_size(), oopSize)),
142 _frame_size(frame_size),
143 _mutable_data_size(mutable_data_size),
144 S390_ONLY(_ctable_offset(0) COMMA)
145 _header_size(header_size),
146 _frame_complete_offset(frame_complete_offset),
147 _kind(kind),
148 _caller_must_gc_arguments(caller_must_gc_arguments)
149 {
150 assert(is_aligned(_size, oopSize), "unaligned size");
151 assert(is_aligned(header_size, oopSize), "unaligned size");
152 assert(is_aligned(_relocation_size, oopSize), "unaligned size");
153 assert(_data_offset <= _size, "codeBlob is too small: %d > %d", _data_offset, _size);
154 assert(is_nmethod() || (cb->total_oop_size() + cb->total_metadata_size() == 0), "must be nmethod");
155 assert(code_end() == content_end(), "must be the same - see code_end()");
156 #ifdef COMPILER1
157 // probably wrong for tiered
158 assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
159 #endif // COMPILER1
160
161 if (_mutable_data_size > 0) {
162 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
163 if (_mutable_data == nullptr) {
164 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data");
165 }
166 } else {
167 // We need unique and valid not null address
168 assert(_mutable_data == blob_end(), "sanity");
169 }
170
171 set_oop_maps(oop_maps);
172 }
173
174 // Simple CodeBlob used for simple BufferBlob.
175 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size) :
176 _oop_maps(nullptr),
177 _name(name),
178 _mutable_data(header_begin() + size), // default value is blob_end()
179 _size(size),
180 _relocation_size(0),
181 _content_offset(CodeBlob::align_code_offset(header_size)),
182 _code_offset(_content_offset),
183 _data_offset(size),
184 _frame_size(0),
185 _mutable_data_size(0),
186 S390_ONLY(_ctable_offset(0) COMMA)
187 _header_size(header_size),
188 _frame_complete_offset(CodeOffsets::frame_never_safe),
189 _kind(kind),
190 _caller_must_gc_arguments(false)
191 {
192 assert(is_aligned(size, oopSize), "unaligned size");
193 assert(is_aligned(header_size, oopSize), "unaligned size");
194 assert(_mutable_data == blob_end(), "sanity");
195 }
196
197 void CodeBlob::purge() {
198 assert(_mutable_data != nullptr, "should never be null");
199 if (_mutable_data != blob_end()) {
200 os::free(_mutable_data);
201 _mutable_data = blob_end(); // Valid not null address
202 _mutable_data_size = 0;
203 _relocation_size = 0;
204 }
205 if (_oop_maps != nullptr) {
206 delete _oop_maps;
207 _oop_maps = nullptr;
208 }
209 NOT_PRODUCT(_asm_remarks.clear());
210 NOT_PRODUCT(_dbg_strings.clear());
211 }
212
213 void CodeBlob::set_oop_maps(OopMapSet* p) {
214 // Danger Will Robinson! This method allocates a big
215 // chunk of memory, its your job to free it.
216 if (p != nullptr) {
217 _oop_maps = ImmutableOopMapSet::build_from(p);
218 } else {
219 _oop_maps = nullptr;
220 }
221 }
222
223 const ImmutableOopMap* CodeBlob::oop_map_for_return_address(address return_address) const {
224 assert(_oop_maps != nullptr, "nope");
225 return _oop_maps->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin());
226 }
227
228 void CodeBlob::print_code_on(outputStream* st) {
229 ResourceMark m;
230 Disassembler::decode(this, st);
231 }
232
233 #if INCLUDE_CDS
234 void CodeBlob::restore_mutable_data(address reloc_data) {
235 // Relocation data is now stored as part of the mutable data area; allocate it before copy relocations
236 if (_mutable_data_size > 0) {
237 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
238 if (_mutable_data == nullptr) {
239 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data");
240 }
241 } else {
242 _mutable_data = blob_end(); // default value
243 }
244 if (_relocation_size > 0) {
245 assert(_mutable_data_size > 0, "relocation is part of mutable data section");
246 memcpy((address)relocation_begin(), reloc_data, relocation_size());
247 }
248 }
249
250 void CodeBlob::prepare_for_archiving_impl() {
251 set_name(nullptr);
252 _oop_maps = nullptr;
253 _mutable_data = nullptr;
254 #ifndef PRODUCT
255 asm_remarks().clear();
256 dbg_strings().clear();
257 #endif /* PRODUCT */
258 }
259
260 void CodeBlob::prepare_for_archiving() {
261 vptr(_kind)->prepare_for_archiving(this);
262 }
263
264 void CodeBlob::archive_blob(CodeBlob* blob, address archive_buffer) {
265 blob->copy_to(archive_buffer);
266 CodeBlob* archived_blob = (CodeBlob*)archive_buffer;
267 archived_blob->prepare_for_archiving();
268 }
269
270 void CodeBlob::post_restore_impl() {
271 // Track memory usage statistic after releasing CodeCache_lock
272 MemoryService::track_code_cache_memory_usage();
273 }
274
275 void CodeBlob::post_restore() {
276 vptr(_kind)->post_restore(this);
277 }
278
279 CodeBlob* CodeBlob::restore(address code_cache_buffer, AOTCodeReader* reader)
280 {
281 copy_to(code_cache_buffer);
282 CodeBlob* code_blob = (CodeBlob*)code_cache_buffer;
283 reader->restore(code_blob);
284 return code_blob;
285 }
286
287 CodeBlob* CodeBlob::create(CodeBlob* archived_blob, AOTCodeReader* reader)
288 {
289 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
290
291 CodeCache::gc_on_allocation();
292
293 CodeBlob* blob = nullptr;
294 unsigned int size = archived_blob->size();
295 {
296 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
297 address code_cache_buffer = (address)CodeCache::allocate(size, CodeBlobType::NonNMethod);
298 if (code_cache_buffer != nullptr) {
299 blob = archived_blob->restore(code_cache_buffer, reader);
300 assert(blob != nullptr, "sanity check");
301
302 // Flush the code block
303 ICache::invalidate_range(blob->code_begin(), blob->code_size());
304 CodeCache::commit(blob); // Count adapters
305 }
306 }
307 if (blob != nullptr) {
308 blob->post_restore();
309 }
310 return blob;
311 }
312
313 #endif // INCLUDE_CDS
314
315 //-----------------------------------------------------------------------------------------
316 // Creates a RuntimeBlob from a CodeBuffer and copy code and relocation info.
317
318 RuntimeBlob::RuntimeBlob(
319 const char* name,
320 CodeBlobKind kind,
321 CodeBuffer* cb,
322 int size,
323 uint16_t header_size,
324 int16_t frame_complete,
325 int frame_size,
326 OopMapSet* oop_maps,
327 bool caller_must_gc_arguments)
328 : CodeBlob(name, kind, cb, size, header_size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments,
329 align_up(cb->total_relocation_size(), oopSize))
330 {
331 if (code_size() == 0) {
332 // Nothing to copy
333 return;
334 }
335
336 cb->copy_code_and_locs_to(this);
337
338 // Flush generated code
339 ICache::invalidate_range(code_begin(), code_size());
340 }
341
342 void RuntimeBlob::free(RuntimeBlob* blob) {
343 assert(blob != nullptr, "caller must check for nullptr");
344 MACOS_AARCH64_ONLY(os::thread_wx_enable_write());
345 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
346 blob->purge();
347 {
348 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
349 CodeCache::free(blob);
350 }
351 // Track memory usage statistic after releasing CodeCache_lock
352 MemoryService::track_code_cache_memory_usage();
353 }
354
355 void RuntimeBlob::trace_new_stub(RuntimeBlob* stub, const char* name1, const char* name2) {
356 // Do not hold the CodeCache lock during name formatting.
357 assert(!CodeCache_lock->owned_by_self(), "release CodeCache before registering the stub");
358
359 if (stub != nullptr && (PrintStubCode ||
360 Forte::is_enabled() ||
361 JvmtiExport::should_post_dynamic_code_generated())) {
362 ResourceMark rm;
363 const size_t name1_len = strlen(name1);
364 const size_t name2_len = strlen(name2);
365 const size_t stub_id_size = name1_len + name2_len + 1;
366 char* stub_id = NEW_RESOURCE_ARRAY(char, stub_id_size);
367 jio_snprintf(stub_id, stub_id_size, "%s%s", name1, name2);
368 if (PrintStubCode) {
369 ttyLocker ttyl;
370 tty->print_cr("- - - [BEGIN] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
371 tty->print_cr("Decoding %s " PTR_FORMAT " [" PTR_FORMAT ", " PTR_FORMAT "] (%d bytes)",
372 stub_id, p2i(stub), p2i(stub->code_begin()), p2i(stub->code_end()), stub->code_size());
373 Disassembler::decode(stub->code_begin(), stub->code_end(), tty
374 NOT_PRODUCT(COMMA &stub->asm_remarks()));
375 if ((stub->oop_maps() != nullptr) && AbstractDisassembler::show_structs()) {
376 tty->print_cr("- - - [OOP MAPS]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
377 stub->oop_maps()->print();
378 }
379 tty->print_cr("- - - [END] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
380 tty->cr();
381 }
382 if (Forte::is_enabled()) {
383 Forte::register_stub(stub_id, stub->code_begin(), stub->code_end());
384 }
385
386 if (JvmtiExport::should_post_dynamic_code_generated()) {
387 const char* stub_name = name2;
388 if (name2[0] == '\0') stub_name = name1;
389 JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end());
390 }
391 }
392
393 // Track memory usage statistic after releasing CodeCache_lock
394 MemoryService::track_code_cache_memory_usage();
395 }
396
397 //----------------------------------------------------------------------------------------------------
398 // Implementation of BufferBlob
399
400 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size)
401 : RuntimeBlob(name, kind, size, header_size)
402 {}
403
404 BufferBlob* BufferBlob::create(const char* name, uint buffer_size) {
405 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
406
407 BufferBlob* blob = nullptr;
408 unsigned int size = sizeof(BufferBlob);
409 // align the size to CodeEntryAlignment
410 size = CodeBlob::align_code_offset(size);
411 size += align_up(buffer_size, oopSize);
412 assert(name != nullptr, "must provide a name");
413 {
414 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
415 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, size);
416 }
417 // Track memory usage statistic after releasing CodeCache_lock
418 MemoryService::track_code_cache_memory_usage();
419
420 return blob;
421 }
422
423
424 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size)
425 : RuntimeBlob(name, kind, cb, size, header_size, CodeOffsets::frame_never_safe, 0, nullptr)
426 {}
427
428 // Used by gtest
429 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
430 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
431
432 BufferBlob* blob = nullptr;
433 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob));
434 assert(name != nullptr, "must provide a name");
435 {
436 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
437 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, cb, size, sizeof(BufferBlob));
438 }
439 // Track memory usage statistic after releasing CodeCache_lock
440 MemoryService::track_code_cache_memory_usage();
441
442 return blob;
443 }
444
445 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
446 return CodeCache::allocate(size, CodeBlobType::NonNMethod);
447 }
448
449 void BufferBlob::free(BufferBlob *blob) {
450 RuntimeBlob::free(blob);
451 }
452
453 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments)
454 : RuntimeBlob(name, kind, cb, size, header_size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
455 {}
456
457
458 //----------------------------------------------------------------------------------------------------
459 // Implementation of AdapterBlob
460
461 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT], int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) :
462 BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size, sizeof(AdapterBlob), frame_complete, frame_size, oop_maps, caller_must_gc_arguments) {
463 #ifdef ASSERT
464 assert(entry_offset[I2C] == 0, "sanity check");
465 for (int i = 1; i < AdapterBlob::ENTRY_COUNT; i++) {
466 // The entry is within the adapter blob or unset.
467 int offset = entry_offset[i];
468 assert((offset > 0 && offset < cb->insts()->size()) ||
469 (i >= C2I_No_Clinit_Check && offset == -1),
470 "invalid entry offset[%d] = 0x%x", i, offset);
471 }
472 #endif // ASSERT
473 _c2i_offset = entry_offset[C2I];
474 _c2i_inline_offset = entry_offset[C2I_Inline];
475 _c2i_inline_ro_offset = entry_offset[C2I_Inline_RO];
476 _c2i_unverified_offset = entry_offset[C2I_Unverified];
477 _c2i_unverified_inline_offset = entry_offset[C2I_Unverified_Inline];
478 _c2i_no_clinit_check_offset = entry_offset[C2I_No_Clinit_Check];
479 CodeCache::commit(this);
480 }
481
482 AdapterBlob* AdapterBlob::create(CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT], int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) {
483 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
484
485 CodeCache::gc_on_allocation();
486
487 AdapterBlob* blob = nullptr;
488 unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
489 {
490 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
491 blob = new (size) AdapterBlob(size, cb, entry_offset, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
492 }
493 // Track memory usage statistic after releasing CodeCache_lock
494 MemoryService::track_code_cache_memory_usage();
495
496 return blob;
497 }
498
499 //----------------------------------------------------------------------------------------------------
500 // Implementation of VtableBlob
501
502 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
503 // Handling of allocation failure stops compilation and prints a bunch of
504 // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
505 // can be locked, and then re-locking the CodeCache_lock. That is not safe in
506 // this context as we hold the CompiledICLocker. So we just don't handle code
507 // cache exhaustion here; we leave that for a later allocation that does not
508 // hold the CompiledICLocker.
509 return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
510 }
511
512 VtableBlob::VtableBlob(const char* name, int size) :
513 BufferBlob(name, CodeBlobKind::Vtable, size) {
514 }
515
516 VtableBlob* VtableBlob::create(const char* name, int buffer_size) {
517 assert(JavaThread::current()->thread_state() == _thread_in_vm, "called with the wrong state");
518
519 VtableBlob* blob = nullptr;
520 unsigned int size = sizeof(VtableBlob);
521 // align the size to CodeEntryAlignment
522 size = align_code_offset(size);
523 size += align_up(buffer_size, oopSize);
524 assert(name != nullptr, "must provide a name");
525 {
526 if (!CodeCache_lock->try_lock()) {
527 // If we can't take the CodeCache_lock, then this is a bad time to perform the ongoing
528 // IC transition to megamorphic, for which this stub will be needed. It is better to
529 // bail out the transition, and wait for a more opportune moment. Not only is it not
530 // worth waiting for the lock blockingly for the megamorphic transition, it might
531 // also result in a deadlock to blockingly wait, when concurrent class unloading is
532 // performed. At this point in time, the CompiledICLocker is taken, so we are not
533 // allowed to blockingly wait for the CodeCache_lock, as these two locks are otherwise
534 // consistently taken in the opposite order. Bailing out results in an IC transition to
535 // the clean state instead, which will cause subsequent calls to retry the transitioning
536 // eventually.
537 return nullptr;
538 }
539
540 MACOS_AARCH64_ONLY(os::thread_wx_enable_write());
541 blob = new (size) VtableBlob(name, size);
542 CodeCache_lock->unlock();
543 }
544 // Track memory usage statistic after releasing CodeCache_lock
545 MemoryService::track_code_cache_memory_usage();
546
547 return blob;
548 }
549
550 //----------------------------------------------------------------------------------------------------
551 // Implementation of MethodHandlesAdapterBlob
552
553 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
554 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
555
556 MethodHandlesAdapterBlob* blob = nullptr;
557 unsigned int size = sizeof(MethodHandlesAdapterBlob);
558 // align the size to CodeEntryAlignment
559 size = CodeBlob::align_code_offset(size);
560 size += align_up(buffer_size, oopSize);
561 {
562 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
563 blob = new (size) MethodHandlesAdapterBlob(size);
564 if (blob == nullptr) {
565 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
566 }
567 }
568 // Track memory usage statistic after releasing CodeCache_lock
569 MemoryService::track_code_cache_memory_usage();
570
571 return blob;
572 }
573
574 //----------------------------------------------------------------------------------------------------
575 // Implementation of BufferedInlineTypeBlob
576 BufferedInlineTypeBlob::BufferedInlineTypeBlob(int size, CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) :
577 BufferBlob("buffered inline type", CodeBlobKind::BufferedInlineType, cb, size, sizeof(BufferedInlineTypeBlob)),
578 _pack_fields_off(pack_fields_off),
579 _pack_fields_jobject_off(pack_fields_jobject_off),
580 _unpack_fields_off(unpack_fields_off) {
581 CodeCache::commit(this);
582 }
583
584 BufferedInlineTypeBlob* BufferedInlineTypeBlob::create(CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) {
585 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
586
587 BufferedInlineTypeBlob* blob = nullptr;
588 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferedInlineTypeBlob));
589 {
590 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
591 blob = new (size) BufferedInlineTypeBlob(size, cb, pack_fields_off, pack_fields_jobject_off, unpack_fields_off);
592 }
593 // Track memory usage statistic after releasing CodeCache_lock
594 MemoryService::track_code_cache_memory_usage();
595
596 return blob;
597 }
598
599 //----------------------------------------------------------------------------------------------------
600 // Implementation of RuntimeStub
601
602 RuntimeStub::RuntimeStub(
603 const char* name,
604 CodeBuffer* cb,
605 int size,
606 int16_t frame_complete,
607 int frame_size,
608 OopMapSet* oop_maps,
609 bool caller_must_gc_arguments
610 )
611 : RuntimeBlob(name, CodeBlobKind::RuntimeStub, cb, size, sizeof(RuntimeStub),
612 frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
613 {
614 }
615
616 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
617 CodeBuffer* cb,
618 int16_t frame_complete,
619 int frame_size,
620 OopMapSet* oop_maps,
621 bool caller_must_gc_arguments,
622 bool alloc_fail_is_fatal)
623 {
624 RuntimeStub* stub = nullptr;
625 unsigned int size = CodeBlob::allocation_size(cb, sizeof(RuntimeStub));
626 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
627 {
628 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
629 stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
630 if (stub == nullptr) {
631 if (!alloc_fail_is_fatal) {
632 return nullptr;
633 }
634 fatal("Initial size of CodeCache is too small");
635 }
636 }
637
638 trace_new_stub(stub, "RuntimeStub - ", stub_name);
639
640 return stub;
641 }
642
643
644 void* RuntimeStub::operator new(size_t s, unsigned size) throw() {
645 return CodeCache::allocate(size, CodeBlobType::NonNMethod);
646 }
647
648 // operator new shared by all singletons:
649 void* SingletonBlob::operator new(size_t s, unsigned size, bool alloc_fail_is_fatal) throw() {
650 void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
651 if (alloc_fail_is_fatal && !p) fatal("Initial size of CodeCache is too small");
652 return p;
653 }
654
655
656 //----------------------------------------------------------------------------------------------------
657 // Implementation of DeoptimizationBlob
658
659 DeoptimizationBlob::DeoptimizationBlob(
660 CodeBuffer* cb,
661 int size,
662 OopMapSet* oop_maps,
663 int unpack_offset,
664 int unpack_with_exception_offset,
665 int unpack_with_reexecution_offset,
666 int frame_size
667 )
668 : SingletonBlob("DeoptimizationBlob", CodeBlobKind::Deoptimization, cb,
669 size, sizeof(DeoptimizationBlob), frame_size, oop_maps)
670 {
671 _unpack_offset = unpack_offset;
672 _unpack_with_exception = unpack_with_exception_offset;
673 _unpack_with_reexecution = unpack_with_reexecution_offset;
674 #ifdef COMPILER1
675 _unpack_with_exception_in_tls = -1;
676 #endif
677 }
678
679
680 DeoptimizationBlob* DeoptimizationBlob::create(
681 CodeBuffer* cb,
682 OopMapSet* oop_maps,
683 int unpack_offset,
684 int unpack_with_exception_offset,
685 int unpack_with_reexecution_offset,
686 int frame_size)
687 {
688 DeoptimizationBlob* blob = nullptr;
689 unsigned int size = CodeBlob::allocation_size(cb, sizeof(DeoptimizationBlob));
690 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
691 {
692 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
693 blob = new (size) DeoptimizationBlob(cb,
694 size,
695 oop_maps,
696 unpack_offset,
697 unpack_with_exception_offset,
698 unpack_with_reexecution_offset,
699 frame_size);
700 }
701
702 trace_new_stub(blob, "DeoptimizationBlob");
703
704 return blob;
705 }
706
707 #ifdef COMPILER2
708
709 //----------------------------------------------------------------------------------------------------
710 // Implementation of UncommonTrapBlob
711
712 UncommonTrapBlob::UncommonTrapBlob(
713 CodeBuffer* cb,
714 int size,
715 OopMapSet* oop_maps,
716 int frame_size
717 )
718 : SingletonBlob("UncommonTrapBlob", CodeBlobKind::UncommonTrap, cb,
719 size, sizeof(UncommonTrapBlob), frame_size, oop_maps)
720 {}
721
722
723 UncommonTrapBlob* UncommonTrapBlob::create(
724 CodeBuffer* cb,
725 OopMapSet* oop_maps,
726 int frame_size)
727 {
728 UncommonTrapBlob* blob = nullptr;
729 unsigned int size = CodeBlob::allocation_size(cb, sizeof(UncommonTrapBlob));
730 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
731 {
732 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
733 blob = new (size, false) UncommonTrapBlob(cb, size, oop_maps, frame_size);
734 }
735
736 trace_new_stub(blob, "UncommonTrapBlob");
737
738 return blob;
739 }
740
741 //----------------------------------------------------------------------------------------------------
742 // Implementation of ExceptionBlob
743
744 ExceptionBlob::ExceptionBlob(
745 CodeBuffer* cb,
746 int size,
747 OopMapSet* oop_maps,
748 int frame_size
749 )
750 : SingletonBlob("ExceptionBlob", CodeBlobKind::Exception, cb,
751 size, sizeof(ExceptionBlob), frame_size, oop_maps)
752 {}
753
754
755 ExceptionBlob* ExceptionBlob::create(
756 CodeBuffer* cb,
757 OopMapSet* oop_maps,
758 int frame_size)
759 {
760 ExceptionBlob* blob = nullptr;
761 unsigned int size = CodeBlob::allocation_size(cb, sizeof(ExceptionBlob));
762 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
763 {
764 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
765 blob = new (size, false) ExceptionBlob(cb, size, oop_maps, frame_size);
766 }
767
768 trace_new_stub(blob, "ExceptionBlob");
769
770 return blob;
771 }
772
773 #endif // COMPILER2
774
775 //----------------------------------------------------------------------------------------------------
776 // Implementation of SafepointBlob
777
778 SafepointBlob::SafepointBlob(
779 CodeBuffer* cb,
780 int size,
781 OopMapSet* oop_maps,
782 int frame_size
783 )
784 : SingletonBlob(cb->name(), CodeBlobKind::Safepoint, cb,
785 size, sizeof(SafepointBlob), frame_size, oop_maps)
786 {}
787
788
789 SafepointBlob* SafepointBlob::create(
790 CodeBuffer* cb,
791 OopMapSet* oop_maps,
792 int frame_size)
793 {
794 SafepointBlob* blob = nullptr;
795 unsigned int size = CodeBlob::allocation_size(cb, sizeof(SafepointBlob));
796 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
797 {
798 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
799 blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
800 }
801
802 trace_new_stub(blob, "SafepointBlob - ", blob->name());
803
804 return blob;
805 }
806
807 //----------------------------------------------------------------------------------------------------
808 // Implementation of UpcallStub
809
810 UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset) :
811 RuntimeBlob(name, CodeBlobKind::Upcall, cb, size, sizeof(UpcallStub),
812 CodeOffsets::frame_never_safe, 0 /* no frame size */,
813 /* oop maps = */ nullptr, /* caller must gc arguments = */ false),
814 _receiver(receiver),
815 _frame_data_offset(frame_data_offset)
816 {
817 CodeCache::commit(this);
818 }
819
820 void* UpcallStub::operator new(size_t s, unsigned size) throw() {
821 return CodeCache::allocate(size, CodeBlobType::NonNMethod);
822 }
823
824 UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset) {
825 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
826
827 UpcallStub* blob = nullptr;
828 unsigned int size = CodeBlob::allocation_size(cb, sizeof(UpcallStub));
829 {
830 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
831 blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset);
832 }
833 if (blob == nullptr) {
834 return nullptr; // caller must handle this
835 }
836
837 // Track memory usage statistic after releasing CodeCache_lock
838 MemoryService::track_code_cache_memory_usage();
839
840 trace_new_stub(blob, "UpcallStub - ", name);
841
842 return blob;
843 }
844
845 void UpcallStub::oops_do(OopClosure* f, const frame& frame) {
846 frame_data_for_frame(frame)->old_handles->oops_do(f);
847 }
848
849 JavaFrameAnchor* UpcallStub::jfa_for_frame(const frame& frame) const {
850 return &frame_data_for_frame(frame)->jfa;
851 }
852
853 void UpcallStub::free(UpcallStub* blob) {
854 assert(blob != nullptr, "caller must check for nullptr");
855 JNIHandles::destroy_global(blob->receiver());
856 RuntimeBlob::free(blob);
857 }
858
859 //----------------------------------------------------------------------------------------------------
860 // Verification and printing
861
862 void CodeBlob::verify() {
863 if (is_nmethod()) {
864 as_nmethod()->verify();
865 }
866 }
867
868 void CodeBlob::print_on(outputStream* st) const {
869 vptr()->print_on(this, st);
870 }
871
872 void CodeBlob::print() const { print_on(tty); }
873
874 void CodeBlob::print_value_on(outputStream* st) const {
875 vptr()->print_value_on(this, st);
876 }
877
878 void CodeBlob::print_on_impl(outputStream* st) const {
879 st->print_cr("[CodeBlob kind:%d (" INTPTR_FORMAT ")]", (int)_kind, p2i(this));
880 st->print_cr("Framesize: %d", _frame_size);
881 }
882
883 void CodeBlob::print_value_on_impl(outputStream* st) const {
884 st->print_cr("[CodeBlob]");
885 }
886
887 void CodeBlob::print_block_comment(outputStream* stream, address block_begin) const {
888 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
889 if (is_nmethod()) {
890 as_nmethod()->print_nmethod_labels(stream, block_begin);
891 }
892 #endif
893
894 #ifndef PRODUCT
895 ptrdiff_t offset = block_begin - code_begin();
896 assert(offset >= 0, "Expecting non-negative offset!");
897 _asm_remarks.print(uint(offset), stream);
898 #endif
899 }
900
901 void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const {
902 if (is_buffer_blob() || is_adapter_blob() || is_vtable_blob() || is_method_handles_adapter_blob()) {
903 // the interpreter is generated into a buffer blob
904 InterpreterCodelet* i = Interpreter::codelet_containing(addr);
905 if (i != nullptr) {
906 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", p2i(addr), (int)(addr - i->code_begin()));
907 i->print_on(st);
908 return;
909 }
910 if (Interpreter::contains(addr)) {
911 st->print_cr(INTPTR_FORMAT " is pointing into interpreter code"
912 " (not bytecode specific)", p2i(addr));
913 return;
914 }
915 //
916 if (is_adapter_blob()) {
917 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", p2i(addr), (int)(addr - code_begin()));
918 AdapterHandlerLibrary::print_handler_on(st, this);
919 return;
920 }
921 // the stubroutines are generated into a buffer blob
922 StubCodeDesc* d = StubCodeDesc::desc_for(addr);
923 if (d != nullptr) {
924 st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", p2i(addr), (int)(addr - d->begin()));
925 d->print_on(st);
926 st->cr();
927 return;
928 }
929 if (StubRoutines::contains(addr)) {
930 st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) stub routine", p2i(addr));
931 return;
932 }
933 VtableStub* v = VtableStubs::stub_containing(addr);
934 if (v != nullptr) {
935 st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", p2i(addr), (int)(addr - v->entry_point()));
936 v->print_on(st);
937 st->cr();
938 return;
939 }
940 }
941 if (is_nmethod()) {
942 nmethod* nm = as_nmethod();
943 ResourceMark rm;
944 st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT,
945 p2i(addr), (int)(addr - nm->entry_point()), p2i(nm));
946 if (verbose) {
947 st->print(" for ");
948 nm->method()->print_value_on(st);
949 }
950 st->cr();
951 if (verbose && st == tty) {
952 // verbose is only ever true when called from findpc in debug.cpp
953 nm->print_nmethod(true);
954 } else {
955 nm->print_on(st);
956 nm->print_code_snippet(st, addr);
957 }
958 return;
959 }
960 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", p2i(addr), (int)(addr - code_begin()));
961 print_on(st);
962 }
963
964 void BufferBlob::print_on_impl(outputStream* st) const {
965 RuntimeBlob::print_on_impl(st);
966 print_value_on_impl(st);
967 }
968
969 void BufferBlob::print_value_on_impl(outputStream* st) const {
970 st->print_cr("BufferBlob (" INTPTR_FORMAT ") used for %s", p2i(this), name());
971 }
972
973 void RuntimeStub::print_on_impl(outputStream* st) const {
974 ttyLocker ttyl;
975 RuntimeBlob::print_on_impl(st);
976 st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this));
977 st->print_cr("%s", name());
978 Disassembler::decode((CodeBlob*)this, st);
979 }
980
981 void RuntimeStub::print_value_on_impl(outputStream* st) const {
982 st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name());
983 }
984
985 void SingletonBlob::print_on_impl(outputStream* st) const {
986 ttyLocker ttyl;
987 RuntimeBlob::print_on_impl(st);
988 st->print_cr("%s", name());
989 Disassembler::decode((CodeBlob*)this, st);
990 }
991
992 void SingletonBlob::print_value_on_impl(outputStream* st) const {
993 st->print_cr("%s", name());
994 }
995
996 void DeoptimizationBlob::print_value_on_impl(outputStream* st) const {
997 st->print_cr("Deoptimization (frame not available)");
998 }
999
1000 void UpcallStub::print_on_impl(outputStream* st) const {
1001 RuntimeBlob::print_on_impl(st);
1002 print_value_on_impl(st);
1003 st->print_cr("Frame data offset: %d", (int) _frame_data_offset);
1004 oop recv = JNIHandles::resolve(_receiver);
1005 st->print("Receiver MH=");
1006 recv->print_on(st);
1007 Disassembler::decode((CodeBlob*)this, st);
1008 }
1009
1010 void UpcallStub::print_value_on_impl(outputStream* st) const {
1011 st->print_cr("UpcallStub (" INTPTR_FORMAT ") used for %s", p2i(this), name());
1012 }