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