1 /*
  2  * Copyright (c) 1998, 2021, 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 "jvm.h"
 27 #include "code/codeBlob.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "code/icBuffer.hpp"
 30 #include "code/relocInfo.hpp"
 31 #include "code/vtableStubs.hpp"
 32 #include "compiler/disassembler.hpp"
 33 #include "compiler/oopMap.hpp"
 34 #include "interpreter/bytecode.hpp"
 35 #include "interpreter/interpreter.hpp"
 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/interfaceSupport.inline.hpp"
 44 #include "runtime/javaFrameAnchor.hpp"
 45 #include "runtime/jniHandles.hpp"
 46 #include "runtime/mutexLocker.hpp"
 47 #include "runtime/safepoint.hpp"
 48 #include "runtime/sharedRuntime.hpp"
 49 #include "runtime/stubCodeGenerator.hpp"
 50 #include "runtime/stubRoutines.hpp"
 51 #include "runtime/vframe.hpp"
 52 #include "services/memoryService.hpp"
 53 #include "utilities/align.hpp"
 54 #ifdef COMPILER1
 55 #include "c1/c1_Runtime1.hpp"
 56 #endif
 57 
 58 const char* CodeBlob::compiler_name() const {
 59   return compilertype2name(_type);
 60 }
 61 
 62 unsigned int CodeBlob::align_code_offset(int offset) {
 63   // align the size to CodeEntryAlignment
 64   int header_size = (int)CodeHeap::header_size();
 65   return align_up(offset + header_size, CodeEntryAlignment) - header_size;
 66 }
 67 
 68 
 69 // This must be consistent with the CodeBlob constructor's layout actions.
 70 unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) {
 71   unsigned int size = header_size;
 72   size += align_up(cb->total_relocation_size(), oopSize);
 73   // align the size to CodeEntryAlignment
 74   size = align_code_offset(size);
 75   size += align_up(cb->total_content_size(), oopSize);
 76   size += align_up(cb->total_oop_size(), oopSize);
 77   size += align_up(cb->total_metadata_size(), oopSize);
 78   return size;
 79 }
 80 
 81 CodeBlob::CodeBlob(const char* name, CompilerType type, const CodeBlobLayout& layout, int frame_complete_offset, int frame_size, ImmutableOopMapSet* oop_maps, bool caller_must_gc_arguments, bool compiled) :
 82   _type(type),
 83   _size(layout.size()),
 84   _header_size(layout.header_size()),
 85   _frame_complete_offset(frame_complete_offset),
 86   _data_offset(layout.data_offset()),
 87   _frame_size(frame_size),
 88   _code_begin(layout.code_begin()),
 89   _code_end(layout.code_end()),
 90   _content_begin(layout.content_begin()),
 91   _data_end(layout.data_end()),
 92   _relocation_begin(layout.relocation_begin()),
 93   _relocation_end(layout.relocation_end()),
 94   _is_compiled(compiled),
 95   _oop_maps(oop_maps),
 96   _caller_must_gc_arguments(caller_must_gc_arguments),
 97   _name(name)
 98 {
 99   assert(is_aligned(layout.size(),            oopSize), "unaligned size");
100   assert(is_aligned(layout.header_size(),     oopSize), "unaligned size");
101   assert(is_aligned(layout.relocation_size(), oopSize), "unaligned size");
102   assert(layout.code_end() == layout.content_end(), "must be the same - see code_end()");
103 #ifdef COMPILER1
104   // probably wrong for tiered
105   assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
106 #endif // COMPILER1
107   S390_ONLY(_ctable_offset = 0;) // avoid uninitialized fields
108 }
109 
110 CodeBlob::CodeBlob(const char* name, CompilerType type, const CodeBlobLayout& layout, CodeBuffer* cb /*UNUSED*/, int frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments, bool compiled) :
111   _type(type),
112   _size(layout.size()),
113   _header_size(layout.header_size()),
114   _frame_complete_offset(frame_complete_offset),
115   _data_offset(layout.data_offset()),
116   _frame_size(frame_size),
117   _code_begin(layout.code_begin()),
118   _code_end(layout.code_end()),
119   _content_begin(layout.content_begin()),
120   _data_end(layout.data_end()),
121   _relocation_begin(layout.relocation_begin()),
122   _relocation_end(layout.relocation_end()),
123   _is_compiled(compiled),
124   _caller_must_gc_arguments(caller_must_gc_arguments),
125   _name(name)
126 {
127   assert(is_aligned(_size,        oopSize), "unaligned size");
128   assert(is_aligned(_header_size, oopSize), "unaligned size");
129   assert(_data_offset <= _size, "codeBlob is too small");
130   assert(layout.code_end() == layout.content_end(), "must be the same - see code_end()");
131 
132   set_oop_maps(oop_maps);
133 #ifdef COMPILER1
134   // probably wrong for tiered
135   assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
136 #endif // COMPILER1
137   S390_ONLY(_ctable_offset = 0;) // avoid uninitialized fields
138 }
139 
140 
141 // Creates a simple CodeBlob. Sets up the size of the different regions.
142 RuntimeBlob::RuntimeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size)
143   : CodeBlob(name, compiler_none, CodeBlobLayout((address) this, size, header_size, locs_size, size), frame_complete, 0, NULL, false /* caller_must_gc_arguments */)
144 {
145   assert(is_aligned(locs_size, oopSize), "unaligned size");
146 }
147 
148 
149 // Creates a RuntimeBlob from a CodeBuffer
150 // and copy code and relocation info.
151 RuntimeBlob::RuntimeBlob(
152   const char* name,
153   CodeBuffer* cb,
154   int         header_size,
155   int         size,
156   int         frame_complete,
157   int         frame_size,
158   OopMapSet*  oop_maps,
159   bool        caller_must_gc_arguments
160 ) : CodeBlob(name, compiler_none, CodeBlobLayout((address) this, size, header_size, cb), cb, frame_complete, frame_size, oop_maps, caller_must_gc_arguments) {
161   cb->copy_code_and_locs_to(this);
162 }
163 
164 void CodeBlob::flush() {
165   FREE_C_HEAP_ARRAY(unsigned char, _oop_maps);
166   _oop_maps = NULL;
167   NOT_PRODUCT(_asm_remarks.clear());
168   NOT_PRODUCT(_dbg_strings.clear());
169 }
170 
171 void CodeBlob::set_oop_maps(OopMapSet* p) {
172   // Danger Will Robinson! This method allocates a big
173   // chunk of memory, its your job to free it.
174   if (p != NULL) {
175     _oop_maps = ImmutableOopMapSet::build_from(p);
176   } else {
177     _oop_maps = NULL;
178   }
179 }
180 
181 
182 void RuntimeBlob::trace_new_stub(RuntimeBlob* stub, const char* name1, const char* name2) {
183   // Do not hold the CodeCache lock during name formatting.
184   assert(!CodeCache_lock->owned_by_self(), "release CodeCache before registering the stub");
185 
186   if (stub != NULL) {
187     char stub_id[256];
188     assert(strlen(name1) + strlen(name2) < sizeof(stub_id), "");
189     jio_snprintf(stub_id, sizeof(stub_id), "%s%s", name1, name2);
190     if (PrintStubCode) {
191       ttyLocker ttyl;
192       tty->print_cr("- - - [BEGIN] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
193       tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, (intptr_t) stub);
194       Disassembler::decode(stub->code_begin(), stub->code_end(), tty
195                            NOT_PRODUCT(COMMA &stub->asm_remarks()));
196       if ((stub->oop_maps() != NULL) && AbstractDisassembler::show_structs()) {
197         tty->print_cr("- - - [OOP MAPS]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
198         stub->oop_maps()->print();
199       }
200       tty->print_cr("- - - [END] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
201       tty->cr();
202     }
203     Forte::register_stub(stub_id, stub->code_begin(), stub->code_end());
204 
205     if (JvmtiExport::should_post_dynamic_code_generated()) {
206       const char* stub_name = name2;
207       if (name2[0] == '\0')  stub_name = name1;
208       JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end());
209     }
210   }
211 
212   // Track memory usage statistic after releasing CodeCache_lock
213   MemoryService::track_code_cache_memory_usage();
214 }
215 
216 const ImmutableOopMap* CodeBlob::oop_map_for_return_address(address return_address) const {
217   assert(_oop_maps != NULL, "nope");
218   return _oop_maps->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin());
219 }
220 
221 void CodeBlob::print_code() {
222   ResourceMark m;
223   Disassembler::decode(this, tty);
224 }
225 
226 //----------------------------------------------------------------------------------------------------
227 // Implementation of BufferBlob
228 
229 
230 BufferBlob::BufferBlob(const char* name, int size)
231 : RuntimeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0)
232 {}
233 
234 BufferBlob* BufferBlob::create(const char* name, int buffer_size) {
235   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
236 
237   BufferBlob* blob = NULL;
238   unsigned int size = sizeof(BufferBlob);
239   // align the size to CodeEntryAlignment
240   size = CodeBlob::align_code_offset(size);
241   size += align_up(buffer_size, oopSize);
242   assert(name != NULL, "must provide a name");
243   {
244     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
245     blob = new (size) BufferBlob(name, size);
246   }
247   // Track memory usage statistic after releasing CodeCache_lock
248   MemoryService::track_code_cache_memory_usage();
249 
250   return blob;
251 }
252 
253 
254 BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb)
255   : RuntimeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL)
256 {}
257 
258 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
259   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
260 
261   BufferBlob* blob = NULL;
262   unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob));
263   assert(name != NULL, "must provide a name");
264   {
265     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
266     blob = new (size) BufferBlob(name, size, cb);
267   }
268   // Track memory usage statistic after releasing CodeCache_lock
269   MemoryService::track_code_cache_memory_usage();
270 
271   return blob;
272 }
273 
274 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
275   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
276 }
277 
278 void BufferBlob::free(BufferBlob *blob) {
279   assert(blob != NULL, "caller must check for NULL");
280   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
281   blob->flush();
282   {
283     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
284     CodeCache::free((RuntimeBlob*)blob);
285   }
286   // Track memory usage statistic after releasing CodeCache_lock
287   MemoryService::track_code_cache_memory_usage();
288 }
289 
290 
291 //----------------------------------------------------------------------------------------------------
292 // Implementation of AdapterBlob
293 
294 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) :
295   BufferBlob("I2C/C2I adapters", size, cb) {
296   CodeCache::commit(this);
297 }
298 
299 AdapterBlob* AdapterBlob::create(CodeBuffer* cb) {
300   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
301 
302   AdapterBlob* blob = NULL;
303   unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
304   {
305     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
306     blob = new (size) AdapterBlob(size, cb);
307   }
308   // Track memory usage statistic after releasing CodeCache_lock
309   MemoryService::track_code_cache_memory_usage();
310 
311   return blob;
312 }
313 
314 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
315   // Handling of allocation failure stops compilation and prints a bunch of
316   // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
317   // can be locked, and then re-locking the CodeCache_lock. That is not safe in
318   // this context as we hold the CompiledICLocker. So we just don't handle code
319   // cache exhaustion here; we leave that for a later allocation that does not
320   // hold the CompiledICLocker.
321   return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
322 }
323 
324 VtableBlob::VtableBlob(const char* name, int size) :
325   BufferBlob(name, size) {
326 }
327 
328 VtableBlob* VtableBlob::create(const char* name, int buffer_size) {
329   assert(JavaThread::current()->thread_state() == _thread_in_vm, "called with the wrong state");
330 
331   VtableBlob* blob = NULL;
332   unsigned int size = sizeof(VtableBlob);
333   // align the size to CodeEntryAlignment
334   size = align_code_offset(size);
335   size += align_up(buffer_size, oopSize);
336   assert(name != NULL, "must provide a name");
337   {
338     if (!CodeCache_lock->try_lock()) {
339       // If we can't take the CodeCache_lock, then this is a bad time to perform the ongoing
340       // IC transition to megamorphic, for which this stub will be needed. It is better to
341       // bail out the transition, and wait for a more opportune moment. Not only is it not
342       // worth waiting for the lock blockingly for the megamorphic transition, it might
343       // also result in a deadlock to blockingly wait, when concurrent class unloading is
344       // performed. At this point in time, the CompiledICLocker is taken, so we are not
345       // allowed to blockingly wait for the CodeCache_lock, as these two locks are otherwise
346       // consistently taken in the opposite order. Bailing out results in an IC transition to
347       // the clean state instead, which will cause subsequent calls to retry the transitioning
348       // eventually.
349       return NULL;
350     }
351     blob = new (size) VtableBlob(name, size);
352     CodeCache_lock->unlock();
353   }
354   // Track memory usage statistic after releasing CodeCache_lock
355   MemoryService::track_code_cache_memory_usage();
356 
357   return blob;
358 }
359 
360 //----------------------------------------------------------------------------------------------------
361 // Implementation of MethodHandlesAdapterBlob
362 
363 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
364   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
365 
366   MethodHandlesAdapterBlob* blob = NULL;
367   unsigned int size = sizeof(MethodHandlesAdapterBlob);
368   // align the size to CodeEntryAlignment
369   size = CodeBlob::align_code_offset(size);
370   size += align_up(buffer_size, oopSize);
371   {
372     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
373     blob = new (size) MethodHandlesAdapterBlob(size);
374     if (blob == NULL) {
375       vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
376     }
377   }
378   // Track memory usage statistic after releasing CodeCache_lock
379   MemoryService::track_code_cache_memory_usage();
380 
381   return blob;
382 }
383 
384 //----------------------------------------------------------------------------------------------------
385 // Implementation of RuntimeStub
386 
387 RuntimeStub::RuntimeStub(
388   const char* name,
389   CodeBuffer* cb,
390   int         size,
391   int         frame_complete,
392   int         frame_size,
393   OopMapSet*  oop_maps,
394   bool        caller_must_gc_arguments
395 )
396 : RuntimeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
397 {
398 }
399 
400 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
401                                            CodeBuffer* cb,
402                                            int frame_complete,
403                                            int frame_size,
404                                            OopMapSet* oop_maps,
405                                            bool caller_must_gc_arguments)
406 {
407   RuntimeStub* stub = NULL;
408   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
409   {
410     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
411     unsigned int size = CodeBlob::allocation_size(cb, sizeof(RuntimeStub));
412     stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
413   }
414 
415   trace_new_stub(stub, "RuntimeStub - ", stub_name);
416 
417   return stub;
418 }
419 
420 
421 void* RuntimeStub::operator new(size_t s, unsigned size) throw() {
422   void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
423   if (!p) fatal("Initial size of CodeCache is too small");
424   return p;
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", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps)
448 {
449   _unpack_offset           = unpack_offset;
450   _unpack_with_exception   = unpack_with_exception_offset;
451   _unpack_with_reexecution = unpack_with_reexecution_offset;
452 #ifdef COMPILER1
453   _unpack_with_exception_in_tls   = -1;
454 #endif
455 }
456 
457 
458 DeoptimizationBlob* DeoptimizationBlob::create(
459   CodeBuffer* cb,
460   OopMapSet*  oop_maps,
461   int        unpack_offset,
462   int        unpack_with_exception_offset,
463   int        unpack_with_reexecution_offset,
464   int        frame_size)
465 {
466   DeoptimizationBlob* blob = NULL;
467   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
468   {
469     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
470     unsigned int size = CodeBlob::allocation_size(cb, sizeof(DeoptimizationBlob));
471     blob = new (size) DeoptimizationBlob(cb,
472                                          size,
473                                          oop_maps,
474                                          unpack_offset,
475                                          unpack_with_exception_offset,
476                                          unpack_with_reexecution_offset,
477                                          frame_size);
478   }
479 
480   trace_new_stub(blob, "DeoptimizationBlob");
481 
482   return blob;
483 }
484 
485 
486 //----------------------------------------------------------------------------------------------------
487 // Implementation of UncommonTrapBlob
488 
489 #ifdef COMPILER2
490 UncommonTrapBlob::UncommonTrapBlob(
491   CodeBuffer* cb,
492   int         size,
493   OopMapSet*  oop_maps,
494   int         frame_size
495 )
496 : SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps)
497 {}
498 
499 
500 UncommonTrapBlob* UncommonTrapBlob::create(
501   CodeBuffer* cb,
502   OopMapSet*  oop_maps,
503   int        frame_size)
504 {
505   UncommonTrapBlob* blob = NULL;
506   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
507   {
508     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
509     unsigned int size = CodeBlob::allocation_size(cb, sizeof(UncommonTrapBlob));
510     blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
511   }
512 
513   trace_new_stub(blob, "UncommonTrapBlob");
514 
515   return blob;
516 }
517 
518 
519 #endif // COMPILER2
520 
521 
522 //----------------------------------------------------------------------------------------------------
523 // Implementation of ExceptionBlob
524 
525 #ifdef COMPILER2
526 ExceptionBlob::ExceptionBlob(
527   CodeBuffer* cb,
528   int         size,
529   OopMapSet*  oop_maps,
530   int         frame_size
531 )
532 : SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps)
533 {}
534 
535 
536 ExceptionBlob* ExceptionBlob::create(
537   CodeBuffer* cb,
538   OopMapSet*  oop_maps,
539   int         frame_size)
540 {
541   ExceptionBlob* blob = NULL;
542   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
543   {
544     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
545     unsigned int size = CodeBlob::allocation_size(cb, sizeof(ExceptionBlob));
546     blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
547   }
548 
549   trace_new_stub(blob, "ExceptionBlob");
550 
551   return blob;
552 }
553 
554 
555 #endif // COMPILER2
556 
557 
558 //----------------------------------------------------------------------------------------------------
559 // Implementation of SafepointBlob
560 
561 SafepointBlob::SafepointBlob(
562   CodeBuffer* cb,
563   int         size,
564   OopMapSet*  oop_maps,
565   int         frame_size
566 )
567 : SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps)
568 {}
569 
570 
571 SafepointBlob* SafepointBlob::create(
572   CodeBuffer* cb,
573   OopMapSet*  oop_maps,
574   int         frame_size)
575 {
576   SafepointBlob* blob = NULL;
577   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
578   {
579     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
580     unsigned int size = CodeBlob::allocation_size(cb, sizeof(SafepointBlob));
581     blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
582   }
583 
584   trace_new_stub(blob, "SafepointBlob");
585 
586   return blob;
587 }
588 
589 
590 //----------------------------------------------------------------------------------------------------
591 // Verification and printing
592 
593 void CodeBlob::print_on(outputStream* st) const {
594   st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", p2i(this));
595   st->print_cr("Framesize: %d", _frame_size);
596 }
597 
598 void CodeBlob::print() const { print_on(tty); }
599 
600 void CodeBlob::print_value_on(outputStream* st) const {
601   st->print_cr("[CodeBlob]");
602 }
603 
604 void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const {
605   if (is_buffer_blob()) {
606     // the interpreter is generated into a buffer blob
607     InterpreterCodelet* i = Interpreter::codelet_containing(addr);
608     if (i != NULL) {
609       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", p2i(addr), (int)(addr - i->code_begin()));
610       i->print_on(st);
611       return;
612     }
613     if (Interpreter::contains(addr)) {
614       st->print_cr(INTPTR_FORMAT " is pointing into interpreter code"
615                    " (not bytecode specific)", p2i(addr));
616       return;
617     }
618     //
619     if (AdapterHandlerLibrary::contains(this)) {
620       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", p2i(addr), (int)(addr - code_begin()));
621       AdapterHandlerLibrary::print_handler_on(st, this);
622     }
623     // the stubroutines are generated into a buffer blob
624     StubCodeDesc* d = StubCodeDesc::desc_for(addr);
625     if (d != NULL) {
626       st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", p2i(addr), (int)(addr - d->begin()));
627       d->print_on(st);
628       st->cr();
629       return;
630     }
631     if (StubRoutines::contains(addr)) {
632       st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) stub routine", p2i(addr));
633       return;
634     }
635     // the InlineCacheBuffer is using stubs generated into a buffer blob
636     if (InlineCacheBuffer::contains(addr)) {
637       st->print_cr(INTPTR_FORMAT " is pointing into InlineCacheBuffer", p2i(addr));
638       return;
639     }
640     VtableStub* v = VtableStubs::stub_containing(addr);
641     if (v != NULL) {
642       st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", p2i(addr), (int)(addr - v->entry_point()));
643       v->print_on(st);
644       st->cr();
645       return;
646     }
647   }
648   if (is_nmethod()) {
649     nmethod* nm = (nmethod*)this;
650     ResourceMark rm;
651     st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT,
652               p2i(addr), (int)(addr - nm->entry_point()), p2i(nm));
653     if (verbose) {
654       st->print(" for ");
655       nm->method()->print_value_on(st);
656     }
657     st->cr();
658     if (verbose && st == tty) {
659       // verbose is only ever true when called from findpc in debug.cpp
660       nm->print_nmethod(true);
661     } else {
662       nm->print(st);
663     }
664     return;
665   }
666   st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", p2i(addr), (int)(addr - code_begin()));
667   print_on(st);
668 }
669 
670 void RuntimeBlob::verify() {
671   ShouldNotReachHere();
672 }
673 
674 void BufferBlob::verify() {
675   // unimplemented
676 }
677 
678 void BufferBlob::print_on(outputStream* st) const {
679   RuntimeBlob::print_on(st);
680   print_value_on(st);
681 }
682 
683 void BufferBlob::print_value_on(outputStream* st) const {
684   st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
685 }
686 
687 void RuntimeStub::verify() {
688   // unimplemented
689 }
690 
691 void RuntimeStub::print_on(outputStream* st) const {
692   ttyLocker ttyl;
693   RuntimeBlob::print_on(st);
694   st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this));
695   st->print_cr("%s", name());
696   Disassembler::decode((RuntimeBlob*)this, st);
697 }
698 
699 void RuntimeStub::print_value_on(outputStream* st) const {
700   st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name());
701 }
702 
703 void SingletonBlob::verify() {
704   // unimplemented
705 }
706 
707 void SingletonBlob::print_on(outputStream* st) const {
708   ttyLocker ttyl;
709   RuntimeBlob::print_on(st);
710   st->print_cr("%s", name());
711   Disassembler::decode((RuntimeBlob*)this, st);
712 }
713 
714 void SingletonBlob::print_value_on(outputStream* st) const {
715   st->print_cr("%s", name());
716 }
717 
718 void DeoptimizationBlob::print_value_on(outputStream* st) const {
719   st->print_cr("Deoptimization (frame not available)");
720 }
721 
722 // Implementation of OptimizedEntryBlob
723 
724 OptimizedEntryBlob::OptimizedEntryBlob(const char* name, int size, CodeBuffer* cb, intptr_t exception_handler_offset,
725                                        jobject receiver, ByteSize frame_data_offset) :
726   BufferBlob(name, size, cb),
727   _exception_handler_offset(exception_handler_offset),
728   _receiver(receiver),
729   _frame_data_offset(frame_data_offset) {
730   CodeCache::commit(this);
731 }
732 
733 OptimizedEntryBlob* OptimizedEntryBlob::create(const char* name, CodeBuffer* cb, intptr_t exception_handler_offset,
734                                                jobject receiver, ByteSize frame_data_offset) {
735   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
736 
737   OptimizedEntryBlob* blob = nullptr;
738   unsigned int size = CodeBlob::allocation_size(cb, sizeof(OptimizedEntryBlob));
739   {
740     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
741     blob = new (size) OptimizedEntryBlob(name, size, cb, exception_handler_offset, receiver, frame_data_offset);
742   }
743   // Track memory usage statistic after releasing CodeCache_lock
744   MemoryService::track_code_cache_memory_usage();
745 
746   return blob;
747 }
748 
749 void OptimizedEntryBlob::oops_do(OopClosure* f, const frame& frame) {
750   frame_data_for_frame(frame)->old_handles->oops_do(f);
751 }
752 
753 JavaFrameAnchor* OptimizedEntryBlob::jfa_for_frame(const frame& frame) const {
754   return &frame_data_for_frame(frame)->jfa;
755 }