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