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