1 /*
  2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.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 "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 
 58 unsigned int CodeBlob::align_code_offset(int offset) {
 59   // align the size to CodeEntryAlignment
 60   int header_size = (int)CodeHeap::header_size();
 61   return align_up(offset + header_size, CodeEntryAlignment) - header_size;
 62 }
 63 
 64 // This must be consistent with the CodeBlob constructor's layout actions.
 65 unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) {
 66   unsigned int size = header_size;
 67   size += align_up(cb->total_relocation_size(), oopSize);
 68   // align the size to CodeEntryAlignment
 69   size = align_code_offset(size);
 70   size += align_up(cb->total_content_size(), oopSize);
 71   size += align_up(cb->total_oop_size(), oopSize);
 72   size += align_up(cb->total_metadata_size(), oopSize);
 73   return size;
 74 }
 75 
 76 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size,
 77                    int16_t frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) :
 78   _oop_maps(nullptr), // will be set by set_oop_maps() call
 79   _name(name),
 80   _size(size),
 81   _relocation_size(align_up(cb->total_relocation_size(), oopSize)),
 82   _content_offset(CodeBlob::align_code_offset(header_size + _relocation_size)),
 83   _code_offset(_content_offset + cb->total_offset_of(cb->insts())),
 84   _data_offset(_content_offset + align_up(cb->total_content_size(), oopSize)),
 85   _frame_size(frame_size),
 86   S390_ONLY(_ctable_offset(0) COMMA)
 87   _header_size(header_size),
 88   _frame_complete_offset(frame_complete_offset),
 89   _kind(kind),
 90   _caller_must_gc_arguments(caller_must_gc_arguments)
 91 {
 92   assert(is_aligned(_size,            oopSize), "unaligned size");
 93   assert(is_aligned(header_size,      oopSize), "unaligned size");
 94   assert(is_aligned(_relocation_size, oopSize), "unaligned size");
 95   assert(_data_offset <= _size, "codeBlob is too small: %d > %d", _data_offset, _size);
 96   assert(code_end() == content_end(), "must be the same - see code_end()");
 97 #ifdef COMPILER1
 98   // probably wrong for tiered
 99   assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
100 #endif // COMPILER1
101 
102   set_oop_maps(oop_maps);
103 }
104 
105 // Simple CodeBlob used for simple BufferBlob.
106 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size) :
107   _oop_maps(nullptr),
108   _name(name),
109   _size(size),
110   _relocation_size(0),
111   _content_offset(CodeBlob::align_code_offset(header_size)),
112   _code_offset(_content_offset),
113   _data_offset(size),
114   _frame_size(0),
115   S390_ONLY(_ctable_offset(0) COMMA)
116   _header_size(header_size),
117   _frame_complete_offset(CodeOffsets::frame_never_safe),
118   _kind(kind),
119   _caller_must_gc_arguments(false)
120 {
121   assert(is_aligned(size,            oopSize), "unaligned size");
122   assert(is_aligned(header_size,     oopSize), "unaligned size");
123 }
124 
125 void CodeBlob::purge() {
126   if (_oop_maps != nullptr) {
127     delete _oop_maps;
128     _oop_maps = nullptr;
129   }
130   NOT_PRODUCT(_asm_remarks.clear());
131   NOT_PRODUCT(_dbg_strings.clear());
132 }
133 
134 void CodeBlob::set_oop_maps(OopMapSet* p) {
135   // Danger Will Robinson! This method allocates a big
136   // chunk of memory, its your job to free it.
137   if (p != nullptr) {
138     _oop_maps = ImmutableOopMapSet::build_from(p);
139   } else {
140     _oop_maps = nullptr;
141   }
142 }
143 
144 const ImmutableOopMap* CodeBlob::oop_map_for_return_address(address return_address) const {
145   assert(_oop_maps != nullptr, "nope");
146   return _oop_maps->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin());
147 }
148 
149 void CodeBlob::print_code_on(outputStream* st) {
150   ResourceMark m;
151   Disassembler::decode(this, st);
152 }
153 
154 //-----------------------------------------------------------------------------------------
155 // Creates a RuntimeBlob from a CodeBuffer and copy code and relocation info.
156 
157 RuntimeBlob::RuntimeBlob(
158   const char* name,
159   CodeBlobKind kind,
160   CodeBuffer* cb,
161   int         size,
162   uint16_t    header_size,
163   int16_t     frame_complete,
164   int         frame_size,
165   OopMapSet*  oop_maps,
166   bool        caller_must_gc_arguments)
167   : CodeBlob(name, kind, cb, size, header_size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
168 {
169   cb->copy_code_and_locs_to(this);
170 }
171 
172 void RuntimeBlob::free(RuntimeBlob* blob) {
173   assert(blob != nullptr, "caller must check for nullptr");
174   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
175   blob->purge();
176   {
177     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
178     CodeCache::free(blob);
179   }
180   // Track memory usage statistic after releasing CodeCache_lock
181   MemoryService::track_code_cache_memory_usage();
182 }
183 
184 void RuntimeBlob::trace_new_stub(RuntimeBlob* stub, const char* name1, const char* name2) {
185   // Do not hold the CodeCache lock during name formatting.
186   assert(!CodeCache_lock->owned_by_self(), "release CodeCache before registering the stub");
187 
188   if (stub != nullptr && (PrintStubCode ||
189                        Forte::is_enabled() ||
190                        JvmtiExport::should_post_dynamic_code_generated())) {
191     char stub_id[256];
192     assert(strlen(name1) + strlen(name2) < sizeof(stub_id), "");
193     jio_snprintf(stub_id, sizeof(stub_id), "%s%s", name1, name2);
194     if (PrintStubCode) {
195       ttyLocker ttyl;
196       tty->print_cr("- - - [BEGIN] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
197       tty->print_cr("Decoding %s " PTR_FORMAT " [" PTR_FORMAT ", " PTR_FORMAT "] (%d bytes)",
198                     stub_id, p2i(stub), p2i(stub->code_begin()), p2i(stub->code_end()), stub->code_size());
199       Disassembler::decode(stub->code_begin(), stub->code_end(), tty
200                            NOT_PRODUCT(COMMA &stub->asm_remarks()));
201       if ((stub->oop_maps() != nullptr) && AbstractDisassembler::show_structs()) {
202         tty->print_cr("- - - [OOP MAPS]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
203         stub->oop_maps()->print();
204       }
205       tty->print_cr("- - - [END] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
206       tty->cr();
207     }
208     if (Forte::is_enabled()) {
209       Forte::register_stub(stub_id, stub->code_begin(), stub->code_end());
210     }
211 
212     if (JvmtiExport::should_post_dynamic_code_generated()) {
213       const char* stub_name = name2;
214       if (name2[0] == '\0')  stub_name = name1;
215       JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end());
216     }
217   }
218 
219   // Track memory usage statistic after releasing CodeCache_lock
220   MemoryService::track_code_cache_memory_usage();
221 }
222 
223 //----------------------------------------------------------------------------------------------------
224 // Implementation of BufferBlob
225 
226 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, int size)
227 : RuntimeBlob(name, kind, size, sizeof(BufferBlob))
228 {}
229 
230 BufferBlob* BufferBlob::create(const char* name, uint buffer_size) {
231   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
232 
233   BufferBlob* blob = nullptr;
234   unsigned int size = sizeof(BufferBlob);
235   // align the size to CodeEntryAlignment
236   size = CodeBlob::align_code_offset(size);
237   size += align_up(buffer_size, oopSize);
238   assert(name != nullptr, "must provide a name");
239   {
240     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
241     blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, size);
242   }
243   // Track memory usage statistic after releasing CodeCache_lock
244   MemoryService::track_code_cache_memory_usage();
245 
246   return blob;
247 }
248 
249 
250 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size)
251   : RuntimeBlob(name, kind, cb, size, sizeof(BufferBlob), CodeOffsets::frame_never_safe, 0, nullptr)
252 {}
253 
254 // Used by gtest
255 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
256   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
257 
258   BufferBlob* blob = nullptr;
259   unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob));
260   assert(name != nullptr, "must provide a name");
261   {
262     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
263     blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, cb, size);
264   }
265   // Track memory usage statistic after releasing CodeCache_lock
266   MemoryService::track_code_cache_memory_usage();
267 
268   return blob;
269 }
270 
271 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
272   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
273 }
274 
275 void BufferBlob::free(BufferBlob *blob) {
276   RuntimeBlob::free(blob);
277 }
278 
279 
280 //----------------------------------------------------------------------------------------------------
281 // Implementation of AdapterBlob
282 
283 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) :
284   BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size) {
285   CodeCache::commit(this);
286 }
287 
288 AdapterBlob* AdapterBlob::create(CodeBuffer* cb) {
289   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
290 
291   CodeCache::gc_on_allocation();
292 
293   AdapterBlob* blob = nullptr;
294   unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
295   {
296     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
297     blob = new (size) AdapterBlob(size, cb);
298   }
299   // Track memory usage statistic after releasing CodeCache_lock
300   MemoryService::track_code_cache_memory_usage();
301 
302   return blob;
303 }
304 
305 //----------------------------------------------------------------------------------------------------
306 // Implementation of VtableBlob
307 
308 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
309   // Handling of allocation failure stops compilation and prints a bunch of
310   // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
311   // can be locked, and then re-locking the CodeCache_lock. That is not safe in
312   // this context as we hold the CompiledICLocker. So we just don't handle code
313   // cache exhaustion here; we leave that for a later allocation that does not
314   // hold the CompiledICLocker.
315   return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
316 }
317 
318 VtableBlob::VtableBlob(const char* name, int size) :
319   BufferBlob(name, CodeBlobKind::Vtable, size) {
320 }
321 
322 VtableBlob* VtableBlob::create(const char* name, int buffer_size) {
323   assert(JavaThread::current()->thread_state() == _thread_in_vm, "called with the wrong state");
324 
325   VtableBlob* blob = nullptr;
326   unsigned int size = sizeof(VtableBlob);
327   // align the size to CodeEntryAlignment
328   size = align_code_offset(size);
329   size += align_up(buffer_size, oopSize);
330   assert(name != nullptr, "must provide a name");
331   {
332     if (!CodeCache_lock->try_lock()) {
333       // If we can't take the CodeCache_lock, then this is a bad time to perform the ongoing
334       // IC transition to megamorphic, for which this stub will be needed. It is better to
335       // bail out the transition, and wait for a more opportune moment. Not only is it not
336       // worth waiting for the lock blockingly for the megamorphic transition, it might
337       // also result in a deadlock to blockingly wait, when concurrent class unloading is
338       // performed. At this point in time, the CompiledICLocker is taken, so we are not
339       // allowed to blockingly wait for the CodeCache_lock, as these two locks are otherwise
340       // consistently taken in the opposite order. Bailing out results in an IC transition to
341       // the clean state instead, which will cause subsequent calls to retry the transitioning
342       // eventually.
343       return nullptr;
344     }
345     blob = new (size) VtableBlob(name, size);
346     CodeCache_lock->unlock();
347   }
348   // Track memory usage statistic after releasing CodeCache_lock
349   MemoryService::track_code_cache_memory_usage();
350 
351   return blob;
352 }
353 
354 //----------------------------------------------------------------------------------------------------
355 // Implementation of MethodHandlesAdapterBlob
356 
357 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
358   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
359 
360   MethodHandlesAdapterBlob* blob = nullptr;
361   unsigned int size = sizeof(MethodHandlesAdapterBlob);
362   // align the size to CodeEntryAlignment
363   size = CodeBlob::align_code_offset(size);
364   size += align_up(buffer_size, oopSize);
365   {
366     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
367     blob = new (size) MethodHandlesAdapterBlob(size);
368     if (blob == nullptr) {
369       vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
370     }
371   }
372   // Track memory usage statistic after releasing CodeCache_lock
373   MemoryService::track_code_cache_memory_usage();
374 
375   return blob;
376 }
377 
378 //----------------------------------------------------------------------------------------------------
379 // Implementation of RuntimeStub
380 
381 RuntimeStub::RuntimeStub(
382   const char* name,
383   CodeBuffer* cb,
384   int         size,
385   int16_t     frame_complete,
386   int         frame_size,
387   OopMapSet*  oop_maps,
388   bool        caller_must_gc_arguments
389 )
390 : RuntimeBlob(name, CodeBlobKind::Runtime_Stub, cb, size, sizeof(RuntimeStub),
391               frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
392 {
393 }
394 
395 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
396                                            CodeBuffer* cb,
397                                            int16_t frame_complete,
398                                            int frame_size,
399                                            OopMapSet* oop_maps,
400                                            bool caller_must_gc_arguments,
401                                            bool alloc_fail_is_fatal)
402 {
403   RuntimeStub* stub = nullptr;
404   unsigned int size = CodeBlob::allocation_size(cb, sizeof(RuntimeStub));
405   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
406   {
407     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
408     stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
409     if (stub == nullptr) {
410       if (!alloc_fail_is_fatal) {
411         return nullptr;
412       }
413       fatal("Initial size of CodeCache is too small");
414     }
415   }
416 
417   trace_new_stub(stub, "RuntimeStub - ", stub_name);
418 
419   return stub;
420 }
421 
422 
423 void* RuntimeStub::operator new(size_t s, unsigned size) throw() {
424   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
425 }
426 
427 // operator new shared by all singletons:
428 void* SingletonBlob::operator new(size_t s, unsigned size) throw() {
429   void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
430   if (!p) fatal("Initial size of CodeCache is too small");
431   return p;
432 }
433 
434 
435 //----------------------------------------------------------------------------------------------------
436 // Implementation of DeoptimizationBlob
437 
438 DeoptimizationBlob::DeoptimizationBlob(
439   CodeBuffer* cb,
440   int         size,
441   OopMapSet*  oop_maps,
442   int         unpack_offset,
443   int         unpack_with_exception_offset,
444   int         unpack_with_reexecution_offset,
445   int         frame_size
446 )
447 : SingletonBlob("DeoptimizationBlob", CodeBlobKind::Deoptimization, cb,
448                 size, sizeof(DeoptimizationBlob), frame_size, oop_maps)
449 {
450   _unpack_offset           = unpack_offset;
451   _unpack_with_exception   = unpack_with_exception_offset;
452   _unpack_with_reexecution = unpack_with_reexecution_offset;
453 #ifdef COMPILER1
454   _unpack_with_exception_in_tls   = -1;
455 #endif
456 }
457 
458 
459 DeoptimizationBlob* DeoptimizationBlob::create(
460   CodeBuffer* cb,
461   OopMapSet*  oop_maps,
462   int        unpack_offset,
463   int        unpack_with_exception_offset,
464   int        unpack_with_reexecution_offset,
465   int        frame_size)
466 {
467   DeoptimizationBlob* blob = nullptr;
468   unsigned int size = CodeBlob::allocation_size(cb, sizeof(DeoptimizationBlob));
469   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
470   {
471     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
472     blob = new (size) DeoptimizationBlob(cb,
473                                          size,
474                                          oop_maps,
475                                          unpack_offset,
476                                          unpack_with_exception_offset,
477                                          unpack_with_reexecution_offset,
478                                          frame_size);
479   }
480 
481   trace_new_stub(blob, "DeoptimizationBlob");
482 
483   return blob;
484 }
485 
486 
487 //----------------------------------------------------------------------------------------------------
488 // Implementation of UncommonTrapBlob
489 
490 #ifdef COMPILER2
491 UncommonTrapBlob::UncommonTrapBlob(
492   CodeBuffer* cb,
493   int         size,
494   OopMapSet*  oop_maps,
495   int         frame_size
496 )
497 : SingletonBlob("UncommonTrapBlob", CodeBlobKind::Uncommon_Trap, cb,
498                 size, sizeof(UncommonTrapBlob), frame_size, oop_maps)
499 {}
500 
501 
502 UncommonTrapBlob* UncommonTrapBlob::create(
503   CodeBuffer* cb,
504   OopMapSet*  oop_maps,
505   int        frame_size)
506 {
507   UncommonTrapBlob* blob = nullptr;
508   unsigned int size = CodeBlob::allocation_size(cb, sizeof(UncommonTrapBlob));
509   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
510   {
511     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
512     blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
513   }
514 
515   trace_new_stub(blob, "UncommonTrapBlob");
516 
517   return blob;
518 }
519 
520 
521 #endif // COMPILER2
522 
523 
524 //----------------------------------------------------------------------------------------------------
525 // Implementation of ExceptionBlob
526 
527 #ifdef COMPILER2
528 ExceptionBlob::ExceptionBlob(
529   CodeBuffer* cb,
530   int         size,
531   OopMapSet*  oop_maps,
532   int         frame_size
533 )
534 : SingletonBlob("ExceptionBlob", CodeBlobKind::Exception, cb,
535                 size, sizeof(ExceptionBlob), frame_size, oop_maps)
536 {}
537 
538 
539 ExceptionBlob* ExceptionBlob::create(
540   CodeBuffer* cb,
541   OopMapSet*  oop_maps,
542   int         frame_size)
543 {
544   ExceptionBlob* blob = nullptr;
545   unsigned int size = CodeBlob::allocation_size(cb, sizeof(ExceptionBlob));
546   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
547   {
548     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
549     blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
550   }
551 
552   trace_new_stub(blob, "ExceptionBlob");
553 
554   return blob;
555 }
556 
557 
558 #endif // COMPILER2
559 
560 
561 //----------------------------------------------------------------------------------------------------
562 // Implementation of SafepointBlob
563 
564 SafepointBlob::SafepointBlob(
565   CodeBuffer* cb,
566   int         size,
567   OopMapSet*  oop_maps,
568   int         frame_size
569 )
570 : SingletonBlob("SafepointBlob", CodeBlobKind::Safepoint, cb,
571                 size, sizeof(SafepointBlob), frame_size, oop_maps)
572 {}
573 
574 
575 SafepointBlob* SafepointBlob::create(
576   CodeBuffer* cb,
577   OopMapSet*  oop_maps,
578   int         frame_size)
579 {
580   SafepointBlob* blob = nullptr;
581   unsigned int size = CodeBlob::allocation_size(cb, sizeof(SafepointBlob));
582   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
583   {
584     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
585     blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
586   }
587 
588   trace_new_stub(blob, "SafepointBlob");
589 
590   return blob;
591 }
592 
593 //----------------------------------------------------------------------------------------------------
594 // Implementation of UpcallStub
595 
596 UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset) :
597   RuntimeBlob(name, CodeBlobKind::Upcall, cb, size, sizeof(UpcallStub),
598               CodeOffsets::frame_never_safe, 0 /* no frame size */,
599               /* oop maps = */ nullptr, /* caller must gc arguments = */ false),
600   _receiver(receiver),
601   _frame_data_offset(frame_data_offset)
602 {
603   CodeCache::commit(this);
604 }
605 
606 void* UpcallStub::operator new(size_t s, unsigned size) throw() {
607   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
608 }
609 
610 UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset) {
611   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
612 
613   UpcallStub* blob = nullptr;
614   unsigned int size = CodeBlob::allocation_size(cb, sizeof(UpcallStub));
615   {
616     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
617     blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset);
618   }
619   if (blob == nullptr) {
620     return nullptr; // caller must handle this
621   }
622 
623   // Track memory usage statistic after releasing CodeCache_lock
624   MemoryService::track_code_cache_memory_usage();
625 
626   trace_new_stub(blob, "UpcallStub - ", name);
627 
628   return blob;
629 }
630 
631 void UpcallStub::oops_do(OopClosure* f, const frame& frame) {
632   frame_data_for_frame(frame)->old_handles->oops_do(f);
633 }
634 
635 JavaFrameAnchor* UpcallStub::jfa_for_frame(const frame& frame) const {
636   return &frame_data_for_frame(frame)->jfa;
637 }
638 
639 void UpcallStub::free(UpcallStub* blob) {
640   assert(blob != nullptr, "caller must check for nullptr");
641   JNIHandles::destroy_global(blob->receiver());
642   RuntimeBlob::free(blob);
643 }
644 
645 //----------------------------------------------------------------------------------------------------
646 // Verification and printing
647 
648 void CodeBlob::print_on(outputStream* st) const {
649   st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", p2i(this));
650   st->print_cr("Framesize: %d", _frame_size);
651 }
652 
653 void CodeBlob::print() const { print_on(tty); }
654 
655 void CodeBlob::print_value_on(outputStream* st) const {
656   st->print_cr("[CodeBlob]");
657 }
658 
659 void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const {
660   if (is_buffer_blob()) {
661     // the interpreter is generated into a buffer blob
662     InterpreterCodelet* i = Interpreter::codelet_containing(addr);
663     if (i != nullptr) {
664       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", p2i(addr), (int)(addr - i->code_begin()));
665       i->print_on(st);
666       return;
667     }
668     if (Interpreter::contains(addr)) {
669       st->print_cr(INTPTR_FORMAT " is pointing into interpreter code"
670                    " (not bytecode specific)", p2i(addr));
671       return;
672     }
673     //
674     if (AdapterHandlerLibrary::contains(this)) {
675       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", p2i(addr), (int)(addr - code_begin()));
676       AdapterHandlerLibrary::print_handler_on(st, this);
677     }
678     // the stubroutines are generated into a buffer blob
679     StubCodeDesc* d = StubCodeDesc::desc_for(addr);
680     if (d != nullptr) {
681       st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", p2i(addr), (int)(addr - d->begin()));
682       d->print_on(st);
683       st->cr();
684       return;
685     }
686     if (StubRoutines::contains(addr)) {
687       st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) stub routine", p2i(addr));
688       return;
689     }
690     VtableStub* v = VtableStubs::stub_containing(addr);
691     if (v != nullptr) {
692       st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", p2i(addr), (int)(addr - v->entry_point()));
693       v->print_on(st);
694       st->cr();
695       return;
696     }
697   }
698   if (is_nmethod()) {
699     nmethod* nm = (nmethod*)this;
700     ResourceMark rm;
701     st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT,
702               p2i(addr), (int)(addr - nm->entry_point()), p2i(nm));
703     if (verbose) {
704       st->print(" for ");
705       nm->method()->print_value_on(st);
706     }
707     st->cr();
708     if (verbose && st == tty) {
709       // verbose is only ever true when called from findpc in debug.cpp
710       nm->print_nmethod(true);
711     } else {
712       nm->print(st);
713     }
714     return;
715   }
716   st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", p2i(addr), (int)(addr - code_begin()));
717   print_on(st);
718 }
719 
720 void BufferBlob::verify() {
721   // unimplemented
722 }
723 
724 void BufferBlob::print_on(outputStream* st) const {
725   RuntimeBlob::print_on(st);
726   print_value_on(st);
727 }
728 
729 void BufferBlob::print_value_on(outputStream* st) const {
730   st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
731 }
732 
733 void RuntimeStub::verify() {
734   // unimplemented
735 }
736 
737 void RuntimeStub::print_on(outputStream* st) const {
738   ttyLocker ttyl;
739   RuntimeBlob::print_on(st);
740   st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this));
741   st->print_cr("%s", name());
742   Disassembler::decode((RuntimeBlob*)this, st);
743 }
744 
745 void RuntimeStub::print_value_on(outputStream* st) const {
746   st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name());
747 }
748 
749 void SingletonBlob::verify() {
750   // unimplemented
751 }
752 
753 void SingletonBlob::print_on(outputStream* st) const {
754   ttyLocker ttyl;
755   RuntimeBlob::print_on(st);
756   st->print_cr("%s", name());
757   Disassembler::decode((RuntimeBlob*)this, st);
758 }
759 
760 void SingletonBlob::print_value_on(outputStream* st) const {
761   st->print_cr("%s", name());
762 }
763 
764 void DeoptimizationBlob::print_value_on(outputStream* st) const {
765   st->print_cr("Deoptimization (frame not available)");
766 }
767 
768 void UpcallStub::verify() {
769   // unimplemented
770 }
771 
772 void UpcallStub::print_on(outputStream* st) const {
773   RuntimeBlob::print_on(st);
774   print_value_on(st);
775   st->print_cr("Frame data offset: %d", (int) _frame_data_offset);
776   oop recv = JNIHandles::resolve(_receiver);
777   st->print("Receiver MH=");
778   recv->print_on(st);
779   Disassembler::decode((RuntimeBlob*)this, st);
780 }
781 
782 void UpcallStub::print_value_on(outputStream* st) const {
783   st->print_cr("UpcallStub (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
784 }