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, int header_size)
251   : RuntimeBlob(name, kind, cb, size, header_size, 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, sizeof(BufferBlob));
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 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments)
280   : RuntimeBlob(name, kind, cb, size, sizeof(BufferBlob), frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
281 {}
282 
283 
284 //----------------------------------------------------------------------------------------------------
285 // Implementation of AdapterBlob
286 
287 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) :
288   BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments) {
289   CodeCache::commit(this);
290 }
291 
292 AdapterBlob* AdapterBlob::create(CodeBuffer* cb, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) {
293   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
294 
295   CodeCache::gc_on_allocation();
296 
297   AdapterBlob* blob = nullptr;
298   unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
299   {
300     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
301     blob = new (size) AdapterBlob(size, cb, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
302   }
303   // Track memory usage statistic after releasing CodeCache_lock
304   MemoryService::track_code_cache_memory_usage();
305 
306   return blob;
307 }
308 
309 //----------------------------------------------------------------------------------------------------
310 // Implementation of VtableBlob
311 
312 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
313   // Handling of allocation failure stops compilation and prints a bunch of
314   // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
315   // can be locked, and then re-locking the CodeCache_lock. That is not safe in
316   // this context as we hold the CompiledICLocker. So we just don't handle code
317   // cache exhaustion here; we leave that for a later allocation that does not
318   // hold the CompiledICLocker.
319   return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
320 }
321 
322 VtableBlob::VtableBlob(const char* name, int size) :
323   BufferBlob(name, CodeBlobKind::Vtable, size) {
324 }
325 
326 VtableBlob* VtableBlob::create(const char* name, int buffer_size) {
327   assert(JavaThread::current()->thread_state() == _thread_in_vm, "called with the wrong state");
328 
329   VtableBlob* blob = nullptr;
330   unsigned int size = sizeof(VtableBlob);
331   // align the size to CodeEntryAlignment
332   size = align_code_offset(size);
333   size += align_up(buffer_size, oopSize);
334   assert(name != nullptr, "must provide a name");
335   {
336     if (!CodeCache_lock->try_lock()) {
337       // If we can't take the CodeCache_lock, then this is a bad time to perform the ongoing
338       // IC transition to megamorphic, for which this stub will be needed. It is better to
339       // bail out the transition, and wait for a more opportune moment. Not only is it not
340       // worth waiting for the lock blockingly for the megamorphic transition, it might
341       // also result in a deadlock to blockingly wait, when concurrent class unloading is
342       // performed. At this point in time, the CompiledICLocker is taken, so we are not
343       // allowed to blockingly wait for the CodeCache_lock, as these two locks are otherwise
344       // consistently taken in the opposite order. Bailing out results in an IC transition to
345       // the clean state instead, which will cause subsequent calls to retry the transitioning
346       // eventually.
347       return nullptr;
348     }
349     blob = new (size) VtableBlob(name, size);
350     CodeCache_lock->unlock();
351   }
352   // Track memory usage statistic after releasing CodeCache_lock
353   MemoryService::track_code_cache_memory_usage();
354 
355   return blob;
356 }
357 
358 //----------------------------------------------------------------------------------------------------
359 // Implementation of MethodHandlesAdapterBlob
360 
361 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
362   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
363 
364   MethodHandlesAdapterBlob* blob = nullptr;
365   unsigned int size = sizeof(MethodHandlesAdapterBlob);
366   // align the size to CodeEntryAlignment
367   size = CodeBlob::align_code_offset(size);
368   size += align_up(buffer_size, oopSize);
369   {
370     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
371     blob = new (size) MethodHandlesAdapterBlob(size);
372     if (blob == nullptr) {
373       vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
374     }
375   }
376   // Track memory usage statistic after releasing CodeCache_lock
377   MemoryService::track_code_cache_memory_usage();
378 
379   return blob;
380 }
381 
382 //----------------------------------------------------------------------------------------------------
383 // Implementation of BufferedInlineTypeBlob
384 BufferedInlineTypeBlob::BufferedInlineTypeBlob(int size, CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) :
385   BufferBlob("buffered inline type", CodeBlobKind::BufferedInlineType, cb, size, sizeof(BufferedInlineTypeBlob)),
386   _pack_fields_off(pack_fields_off),
387   _pack_fields_jobject_off(pack_fields_jobject_off),
388   _unpack_fields_off(unpack_fields_off) {
389   CodeCache::commit(this);
390 }
391 
392 BufferedInlineTypeBlob* BufferedInlineTypeBlob::create(CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) {
393   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
394 
395   BufferedInlineTypeBlob* blob = nullptr;
396   unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferedInlineTypeBlob));
397   {
398     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
399     blob = new (size) BufferedInlineTypeBlob(size, cb, pack_fields_off, pack_fields_jobject_off, unpack_fields_off);
400   }
401   // Track memory usage statistic after releasing CodeCache_lock
402   MemoryService::track_code_cache_memory_usage();
403 
404   return blob;
405 }
406 
407 //----------------------------------------------------------------------------------------------------
408 // Implementation of RuntimeStub
409 
410 RuntimeStub::RuntimeStub(
411   const char* name,
412   CodeBuffer* cb,
413   int         size,
414   int16_t     frame_complete,
415   int         frame_size,
416   OopMapSet*  oop_maps,
417   bool        caller_must_gc_arguments
418 )
419 : RuntimeBlob(name, CodeBlobKind::Runtime_Stub, cb, size, sizeof(RuntimeStub),
420               frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
421 {
422 }
423 
424 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
425                                            CodeBuffer* cb,
426                                            int16_t frame_complete,
427                                            int frame_size,
428                                            OopMapSet* oop_maps,
429                                            bool caller_must_gc_arguments,
430                                            bool alloc_fail_is_fatal)
431 {
432   RuntimeStub* stub = nullptr;
433   unsigned int size = CodeBlob::allocation_size(cb, sizeof(RuntimeStub));
434   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
435   {
436     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
437     stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
438     if (stub == nullptr) {
439       if (!alloc_fail_is_fatal) {
440         return nullptr;
441       }
442       fatal("Initial size of CodeCache is too small");
443     }
444   }
445 
446   trace_new_stub(stub, "RuntimeStub - ", stub_name);
447 
448   return stub;
449 }
450 
451 
452 void* RuntimeStub::operator new(size_t s, unsigned size) throw() {
453   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
454 }
455 
456 // operator new shared by all singletons:
457 void* SingletonBlob::operator new(size_t s, unsigned size) throw() {
458   void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
459   if (!p) fatal("Initial size of CodeCache is too small");
460   return p;
461 }
462 
463 
464 //----------------------------------------------------------------------------------------------------
465 // Implementation of DeoptimizationBlob
466 
467 DeoptimizationBlob::DeoptimizationBlob(
468   CodeBuffer* cb,
469   int         size,
470   OopMapSet*  oop_maps,
471   int         unpack_offset,
472   int         unpack_with_exception_offset,
473   int         unpack_with_reexecution_offset,
474   int         frame_size
475 )
476 : SingletonBlob("DeoptimizationBlob", CodeBlobKind::Deoptimization, cb,
477                 size, sizeof(DeoptimizationBlob), frame_size, oop_maps)
478 {
479   _unpack_offset           = unpack_offset;
480   _unpack_with_exception   = unpack_with_exception_offset;
481   _unpack_with_reexecution = unpack_with_reexecution_offset;
482 #ifdef COMPILER1
483   _unpack_with_exception_in_tls   = -1;
484 #endif
485 }
486 
487 
488 DeoptimizationBlob* DeoptimizationBlob::create(
489   CodeBuffer* cb,
490   OopMapSet*  oop_maps,
491   int        unpack_offset,
492   int        unpack_with_exception_offset,
493   int        unpack_with_reexecution_offset,
494   int        frame_size)
495 {
496   DeoptimizationBlob* blob = nullptr;
497   unsigned int size = CodeBlob::allocation_size(cb, sizeof(DeoptimizationBlob));
498   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
499   {
500     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
501     blob = new (size) DeoptimizationBlob(cb,
502                                          size,
503                                          oop_maps,
504                                          unpack_offset,
505                                          unpack_with_exception_offset,
506                                          unpack_with_reexecution_offset,
507                                          frame_size);
508   }
509 
510   trace_new_stub(blob, "DeoptimizationBlob");
511 
512   return blob;
513 }
514 
515 
516 //----------------------------------------------------------------------------------------------------
517 // Implementation of UncommonTrapBlob
518 
519 #ifdef COMPILER2
520 UncommonTrapBlob::UncommonTrapBlob(
521   CodeBuffer* cb,
522   int         size,
523   OopMapSet*  oop_maps,
524   int         frame_size
525 )
526 : SingletonBlob("UncommonTrapBlob", CodeBlobKind::Uncommon_Trap, cb,
527                 size, sizeof(UncommonTrapBlob), frame_size, oop_maps)
528 {}
529 
530 
531 UncommonTrapBlob* UncommonTrapBlob::create(
532   CodeBuffer* cb,
533   OopMapSet*  oop_maps,
534   int        frame_size)
535 {
536   UncommonTrapBlob* blob = nullptr;
537   unsigned int size = CodeBlob::allocation_size(cb, sizeof(UncommonTrapBlob));
538   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
539   {
540     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
541     blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
542   }
543 
544   trace_new_stub(blob, "UncommonTrapBlob");
545 
546   return blob;
547 }
548 
549 
550 #endif // COMPILER2
551 
552 
553 //----------------------------------------------------------------------------------------------------
554 // Implementation of ExceptionBlob
555 
556 #ifdef COMPILER2
557 ExceptionBlob::ExceptionBlob(
558   CodeBuffer* cb,
559   int         size,
560   OopMapSet*  oop_maps,
561   int         frame_size
562 )
563 : SingletonBlob("ExceptionBlob", CodeBlobKind::Exception, cb,
564                 size, sizeof(ExceptionBlob), frame_size, oop_maps)
565 {}
566 
567 
568 ExceptionBlob* ExceptionBlob::create(
569   CodeBuffer* cb,
570   OopMapSet*  oop_maps,
571   int         frame_size)
572 {
573   ExceptionBlob* blob = nullptr;
574   unsigned int size = CodeBlob::allocation_size(cb, sizeof(ExceptionBlob));
575   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
576   {
577     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
578     blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
579   }
580 
581   trace_new_stub(blob, "ExceptionBlob");
582 
583   return blob;
584 }
585 
586 
587 #endif // COMPILER2
588 
589 
590 //----------------------------------------------------------------------------------------------------
591 // Implementation of SafepointBlob
592 
593 SafepointBlob::SafepointBlob(
594   CodeBuffer* cb,
595   int         size,
596   OopMapSet*  oop_maps,
597   int         frame_size
598 )
599 : SingletonBlob("SafepointBlob", CodeBlobKind::Safepoint, cb,
600                 size, sizeof(SafepointBlob), frame_size, oop_maps)
601 {}
602 
603 
604 SafepointBlob* SafepointBlob::create(
605   CodeBuffer* cb,
606   OopMapSet*  oop_maps,
607   int         frame_size)
608 {
609   SafepointBlob* blob = nullptr;
610   unsigned int size = CodeBlob::allocation_size(cb, sizeof(SafepointBlob));
611   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
612   {
613     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
614     blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
615   }
616 
617   trace_new_stub(blob, "SafepointBlob");
618 
619   return blob;
620 }
621 
622 //----------------------------------------------------------------------------------------------------
623 // Implementation of UpcallStub
624 
625 UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset) :
626   RuntimeBlob(name, CodeBlobKind::Upcall, cb, size, sizeof(UpcallStub),
627               CodeOffsets::frame_never_safe, 0 /* no frame size */,
628               /* oop maps = */ nullptr, /* caller must gc arguments = */ false),
629   _receiver(receiver),
630   _frame_data_offset(frame_data_offset)
631 {
632   CodeCache::commit(this);
633 }
634 
635 void* UpcallStub::operator new(size_t s, unsigned size) throw() {
636   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
637 }
638 
639 UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset) {
640   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
641 
642   UpcallStub* blob = nullptr;
643   unsigned int size = CodeBlob::allocation_size(cb, sizeof(UpcallStub));
644   {
645     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
646     blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset);
647   }
648   if (blob == nullptr) {
649     return nullptr; // caller must handle this
650   }
651 
652   // Track memory usage statistic after releasing CodeCache_lock
653   MemoryService::track_code_cache_memory_usage();
654 
655   trace_new_stub(blob, "UpcallStub - ", name);
656 
657   return blob;
658 }
659 
660 void UpcallStub::oops_do(OopClosure* f, const frame& frame) {
661   frame_data_for_frame(frame)->old_handles->oops_do(f);
662 }
663 
664 JavaFrameAnchor* UpcallStub::jfa_for_frame(const frame& frame) const {
665   return &frame_data_for_frame(frame)->jfa;
666 }
667 
668 void UpcallStub::free(UpcallStub* blob) {
669   assert(blob != nullptr, "caller must check for nullptr");
670   JNIHandles::destroy_global(blob->receiver());
671   RuntimeBlob::free(blob);
672 }
673 
674 //----------------------------------------------------------------------------------------------------
675 // Verification and printing
676 
677 void CodeBlob::print_on(outputStream* st) const {
678   st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", p2i(this));
679   st->print_cr("Framesize: %d", _frame_size);
680 }
681 
682 void CodeBlob::print() const { print_on(tty); }
683 
684 void CodeBlob::print_value_on(outputStream* st) const {
685   st->print_cr("[CodeBlob]");
686 }
687 
688 void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const {
689   if (is_buffer_blob()) {
690     // the interpreter is generated into a buffer blob
691     InterpreterCodelet* i = Interpreter::codelet_containing(addr);
692     if (i != nullptr) {
693       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", p2i(addr), (int)(addr - i->code_begin()));
694       i->print_on(st);
695       return;
696     }
697     if (Interpreter::contains(addr)) {
698       st->print_cr(INTPTR_FORMAT " is pointing into interpreter code"
699                    " (not bytecode specific)", p2i(addr));
700       return;
701     }
702     //
703     if (AdapterHandlerLibrary::contains(this)) {
704       st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", p2i(addr), (int)(addr - code_begin()));
705       AdapterHandlerLibrary::print_handler_on(st, this);
706     }
707     // the stubroutines are generated into a buffer blob
708     StubCodeDesc* d = StubCodeDesc::desc_for(addr);
709     if (d != nullptr) {
710       st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", p2i(addr), (int)(addr - d->begin()));
711       d->print_on(st);
712       st->cr();
713       return;
714     }
715     if (StubRoutines::contains(addr)) {
716       st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) stub routine", p2i(addr));
717       return;
718     }
719     VtableStub* v = VtableStubs::stub_containing(addr);
720     if (v != nullptr) {
721       st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", p2i(addr), (int)(addr - v->entry_point()));
722       v->print_on(st);
723       st->cr();
724       return;
725     }
726   }
727   if (is_nmethod()) {
728     nmethod* nm = (nmethod*)this;
729     ResourceMark rm;
730     st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT,
731               p2i(addr), (int)(addr - nm->entry_point()), p2i(nm));
732     if (verbose) {
733       st->print(" for ");
734       nm->method()->print_value_on(st);
735     }
736     st->cr();
737     if (verbose && st == tty) {
738       // verbose is only ever true when called from findpc in debug.cpp
739       nm->print_nmethod(true);
740     } else {
741       nm->print(st);
742     }
743     return;
744   }
745   st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", p2i(addr), (int)(addr - code_begin()));
746   print_on(st);
747 }
748 
749 void BufferBlob::verify() {
750   // unimplemented
751 }
752 
753 void BufferBlob::print_on(outputStream* st) const {
754   RuntimeBlob::print_on(st);
755   print_value_on(st);
756 }
757 
758 void BufferBlob::print_value_on(outputStream* st) const {
759   st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
760 }
761 
762 void RuntimeStub::verify() {
763   // unimplemented
764 }
765 
766 void RuntimeStub::print_on(outputStream* st) const {
767   ttyLocker ttyl;
768   RuntimeBlob::print_on(st);
769   st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this));
770   st->print_cr("%s", name());
771   Disassembler::decode((RuntimeBlob*)this, st);
772 }
773 
774 void RuntimeStub::print_value_on(outputStream* st) const {
775   st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name());
776 }
777 
778 void SingletonBlob::verify() {
779   // unimplemented
780 }
781 
782 void SingletonBlob::print_on(outputStream* st) const {
783   ttyLocker ttyl;
784   RuntimeBlob::print_on(st);
785   st->print_cr("%s", name());
786   Disassembler::decode((RuntimeBlob*)this, st);
787 }
788 
789 void SingletonBlob::print_value_on(outputStream* st) const {
790   st->print_cr("%s", name());
791 }
792 
793 void DeoptimizationBlob::print_value_on(outputStream* st) const {
794   st->print_cr("Deoptimization (frame not available)");
795 }
796 
797 void UpcallStub::verify() {
798   // unimplemented
799 }
800 
801 void UpcallStub::print_on(outputStream* st) const {
802   RuntimeBlob::print_on(st);
803   print_value_on(st);
804   st->print_cr("Frame data offset: %d", (int) _frame_data_offset);
805   oop recv = JNIHandles::resolve(_receiver);
806   st->print("Receiver MH=");
807   recv->print_on(st);
808   Disassembler::decode((RuntimeBlob*)this, st);
809 }
810 
811 void UpcallStub::print_value_on(outputStream* st) const {
812   st->print_cr("UpcallStub (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
813 }