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