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 // Virtual methods are not allowed in code blobs to simplify caching compiled code.
58 // Check all "leaf" subclasses of CodeBlob class.
59
60 static_assert(!std::is_polymorphic<nmethod>::value, "no virtual methods are allowed in nmethod");
61 static_assert(!std::is_polymorphic<AdapterBlob>::value, "no virtual methods are allowed in code blobs");
62 static_assert(!std::is_polymorphic<VtableBlob>::value, "no virtual methods are allowed in code blobs");
63 static_assert(!std::is_polymorphic<MethodHandlesAdapterBlob>::value, "no virtual methods are allowed in code blobs");
64 static_assert(!std::is_polymorphic<RuntimeStub>::value, "no virtual methods are allowed in code blobs");
65 static_assert(!std::is_polymorphic<DeoptimizationBlob>::value, "no virtual methods are allowed in code blobs");
66 static_assert(!std::is_polymorphic<SafepointBlob>::value, "no virtual methods are allowed in code blobs");
67 static_assert(!std::is_polymorphic<UpcallStub>::value, "no virtual methods are allowed in code blobs");
68 #ifdef COMPILER2
69 static_assert(!std::is_polymorphic<ExceptionBlob>::value, "no virtual methods are allowed in code blobs");
70 static_assert(!std::is_polymorphic<UncommonTrapBlob>::value, "no virtual methods are allowed in code blobs");
71 #endif
72
73 // Add proxy vtables.
74 // We need only few for now - they are used only from prints.
75 const nmethod::Vptr nmethod::_vpntr;
76 const BufferBlob::Vptr BufferBlob::_vpntr;
77 const RuntimeStub::Vptr RuntimeStub::_vpntr;
78 const SingletonBlob::Vptr SingletonBlob::_vpntr;
79 const DeoptimizationBlob::Vptr DeoptimizationBlob::_vpntr;
80 #ifdef COMPILER2
81 const ExceptionBlob::Vptr ExceptionBlob::_vpntr;
82 #endif // COMPILER2
83 const UpcallStub::Vptr UpcallStub::_vpntr;
84
85 const CodeBlob::Vptr* CodeBlob::vptr(CodeBlobKind kind) {
86 constexpr const CodeBlob::Vptr* array[(size_t)CodeBlobKind::Number_Of_Kinds] = {
87 nullptr/* None */,
88 &nmethod::_vpntr,
89 &BufferBlob::_vpntr,
90 &AdapterBlob::_vpntr,
91 &VtableBlob::_vpntr,
92 &MethodHandlesAdapterBlob::_vpntr,
93 &RuntimeStub::_vpntr,
94 &DeoptimizationBlob::_vpntr,
95 &SafepointBlob::_vpntr,
96 #ifdef COMPILER2
97 &ExceptionBlob::_vpntr,
98 &UncommonTrapBlob::_vpntr,
99 #endif
100 &UpcallStub::_vpntr
101 };
102
103 return array[(size_t)kind];
104 }
105
106 const CodeBlob::Vptr* CodeBlob::vptr() const {
107 return vptr(_kind);
108 }
109
110 unsigned int CodeBlob::align_code_offset(int offset) {
111 // align the size to CodeEntryAlignment
112 int header_size = (int)CodeHeap::header_size();
409 // Track memory usage statistic after releasing CodeCache_lock
410 MemoryService::track_code_cache_memory_usage();
411
412 return blob;
413 }
414
415
416 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size)
417 : RuntimeBlob(name, kind, cb, size, header_size, CodeOffsets::frame_never_safe, 0, nullptr)
418 {}
419
420 // Used by gtest
421 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
422 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
423
424 BufferBlob* blob = nullptr;
425 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob));
426 assert(name != nullptr, "must provide a name");
427 {
428 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
429 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, cb, size);
430 }
431 // Track memory usage statistic after releasing CodeCache_lock
432 MemoryService::track_code_cache_memory_usage();
433
434 return blob;
435 }
436
437 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
438 return CodeCache::allocate(size, CodeBlobType::NonNMethod);
439 }
440
441 void BufferBlob::free(BufferBlob *blob) {
442 RuntimeBlob::free(blob);
443 }
444
445
446 //----------------------------------------------------------------------------------------------------
447 // Implementation of AdapterBlob
448
449 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT]) :
450 BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size, sizeof(AdapterBlob)) {
451 assert(entry_offset[I2C] == 0, "sanity check");
452 #ifdef ASSERT
453 for (int i = 1; i < AdapterBlob::ENTRY_COUNT; i++) {
454 // The entry is within the adapter blob or unset.
455 int offset = entry_offset[i];
456 assert((offset > 0 && offset < cb->insts()->size()) ||
457 (i >= C2I_No_Clinit_Check && offset == -1),
458 "invalid entry offset[%d] = 0x%x", i, offset);
459 }
460 #endif // ASSERT
461 _c2i_offset = entry_offset[C2I];
462 _c2i_unverified_offset = entry_offset[C2I_Unverified];
463 _c2i_no_clinit_check_offset = entry_offset[C2I_No_Clinit_Check];
464 CodeCache::commit(this);
465 }
466
467 AdapterBlob* AdapterBlob::create(CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT]) {
468 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
469
470 CodeCache::gc_on_allocation();
471
472 AdapterBlob* blob = nullptr;
473 unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
474 {
475 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
476 blob = new (size) AdapterBlob(size, cb, entry_offset);
477 }
478 // Track memory usage statistic after releasing CodeCache_lock
479 MemoryService::track_code_cache_memory_usage();
480
481 return blob;
482 }
483
484 //----------------------------------------------------------------------------------------------------
485 // Implementation of VtableBlob
486
487 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
488 // Handling of allocation failure stops compilation and prints a bunch of
489 // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
490 // can be locked, and then re-locking the CodeCache_lock. That is not safe in
491 // this context as we hold the CompiledICLocker. So we just don't handle code
492 // cache exhaustion here; we leave that for a later allocation that does not
493 // hold the CompiledICLocker.
494 return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
495 }
496
539 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
540
541 MethodHandlesAdapterBlob* blob = nullptr;
542 unsigned int size = sizeof(MethodHandlesAdapterBlob);
543 // align the size to CodeEntryAlignment
544 size = CodeBlob::align_code_offset(size);
545 size += align_up(buffer_size, oopSize);
546 {
547 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
548 blob = new (size) MethodHandlesAdapterBlob(size);
549 if (blob == nullptr) {
550 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
551 }
552 }
553 // Track memory usage statistic after releasing CodeCache_lock
554 MemoryService::track_code_cache_memory_usage();
555
556 return blob;
557 }
558
559 //----------------------------------------------------------------------------------------------------
560 // Implementation of RuntimeStub
561
562 RuntimeStub::RuntimeStub(
563 const char* name,
564 CodeBuffer* cb,
565 int size,
566 int16_t frame_complete,
567 int frame_size,
568 OopMapSet* oop_maps,
569 bool caller_must_gc_arguments
570 )
571 : RuntimeBlob(name, CodeBlobKind::RuntimeStub, cb, size, sizeof(RuntimeStub),
572 frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
573 {
574 }
575
576 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
577 CodeBuffer* cb,
578 int16_t frame_complete,
|
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 // Virtual methods are not allowed in code blobs to simplify caching compiled code.
58 // Check all "leaf" subclasses of CodeBlob class.
59
60 static_assert(!std::is_polymorphic<nmethod>::value, "no virtual methods are allowed in nmethod");
61 static_assert(!std::is_polymorphic<AdapterBlob>::value, "no virtual methods are allowed in code blobs");
62 static_assert(!std::is_polymorphic<VtableBlob>::value, "no virtual methods are allowed in code blobs");
63 static_assert(!std::is_polymorphic<MethodHandlesAdapterBlob>::value, "no virtual methods are allowed in code blobs");
64 static_assert(!std::is_polymorphic<RuntimeStub>::value, "no virtual methods are allowed in code blobs");
65 static_assert(!std::is_polymorphic<BufferedInlineTypeBlob>::value, "no virtual methods are allowed in code blobs");
66 static_assert(!std::is_polymorphic<DeoptimizationBlob>::value, "no virtual methods are allowed in code blobs");
67 static_assert(!std::is_polymorphic<SafepointBlob>::value, "no virtual methods are allowed in code blobs");
68 static_assert(!std::is_polymorphic<UpcallStub>::value, "no virtual methods are allowed in code blobs");
69 #ifdef COMPILER2
70 static_assert(!std::is_polymorphic<ExceptionBlob>::value, "no virtual methods are allowed in code blobs");
71 static_assert(!std::is_polymorphic<UncommonTrapBlob>::value, "no virtual methods are allowed in code blobs");
72 #endif
73
74 // Add proxy vtables.
75 // We need only few for now - they are used only from prints.
76 const nmethod::Vptr nmethod::_vpntr;
77 const BufferBlob::Vptr BufferBlob::_vpntr;
78 const RuntimeStub::Vptr RuntimeStub::_vpntr;
79 const SingletonBlob::Vptr SingletonBlob::_vpntr;
80 const DeoptimizationBlob::Vptr DeoptimizationBlob::_vpntr;
81 #ifdef COMPILER2
82 const ExceptionBlob::Vptr ExceptionBlob::_vpntr;
83 #endif // COMPILER2
84 const UpcallStub::Vptr UpcallStub::_vpntr;
85
86 const CodeBlob::Vptr* CodeBlob::vptr(CodeBlobKind kind) {
87 constexpr const CodeBlob::Vptr* array[(size_t)CodeBlobKind::Number_Of_Kinds] = {
88 nullptr/* None */,
89 &nmethod::_vpntr,
90 &BufferBlob::_vpntr,
91 &AdapterBlob::_vpntr,
92 &VtableBlob::_vpntr,
93 &MethodHandlesAdapterBlob::_vpntr,
94 &BufferedInlineTypeBlob::_vpntr,
95 &RuntimeStub::_vpntr,
96 &DeoptimizationBlob::_vpntr,
97 &SafepointBlob::_vpntr,
98 #ifdef COMPILER2
99 &ExceptionBlob::_vpntr,
100 &UncommonTrapBlob::_vpntr,
101 #endif
102 &UpcallStub::_vpntr
103 };
104
105 return array[(size_t)kind];
106 }
107
108 const CodeBlob::Vptr* CodeBlob::vptr() const {
109 return vptr(_kind);
110 }
111
112 unsigned int CodeBlob::align_code_offset(int offset) {
113 // align the size to CodeEntryAlignment
114 int header_size = (int)CodeHeap::header_size();
411 // Track memory usage statistic after releasing CodeCache_lock
412 MemoryService::track_code_cache_memory_usage();
413
414 return blob;
415 }
416
417
418 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size)
419 : RuntimeBlob(name, kind, cb, size, header_size, CodeOffsets::frame_never_safe, 0, nullptr)
420 {}
421
422 // Used by gtest
423 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
424 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
425
426 BufferBlob* blob = nullptr;
427 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob));
428 assert(name != nullptr, "must provide a name");
429 {
430 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
431 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, cb, size, sizeof(BufferBlob));
432 }
433 // Track memory usage statistic after releasing CodeCache_lock
434 MemoryService::track_code_cache_memory_usage();
435
436 return blob;
437 }
438
439 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
440 return CodeCache::allocate(size, CodeBlobType::NonNMethod);
441 }
442
443 void BufferBlob::free(BufferBlob *blob) {
444 RuntimeBlob::free(blob);
445 }
446
447 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments)
448 : RuntimeBlob(name, kind, cb, size, header_size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
449 {}
450
451
452 //----------------------------------------------------------------------------------------------------
453 // Implementation of AdapterBlob
454
455 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT], int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) :
456 BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size, sizeof(AdapterBlob), frame_complete, frame_size, oop_maps, caller_must_gc_arguments) {
457 #ifdef ASSERT
458 assert(entry_offset[I2C] == 0, "sanity check");
459 for (int i = 1; i < AdapterBlob::ENTRY_COUNT; i++) {
460 // The entry is within the adapter blob or unset.
461 int offset = entry_offset[i];
462 assert((offset > 0 && offset < cb->insts()->size()) ||
463 (i >= C2I_No_Clinit_Check && offset == -1),
464 "invalid entry offset[%d] = 0x%x", i, offset);
465 }
466 #endif // ASSERT
467 _c2i_offset = entry_offset[C2I];
468 _c2i_inline_offset = entry_offset[C2I_Inline];
469 _c2i_inline_ro_offset = entry_offset[C2I_Inline_RO];
470 _c2i_unverified_offset = entry_offset[C2I_Unverified];
471 _c2i_unverified_inline_offset = entry_offset[C2I_Unverified_Inline];
472 _c2i_no_clinit_check_offset = entry_offset[C2I_No_Clinit_Check];
473 CodeCache::commit(this);
474 }
475
476 AdapterBlob* AdapterBlob::create(CodeBuffer* cb, int entry_offset[AdapterBlob::ENTRY_COUNT], int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments) {
477 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
478
479 CodeCache::gc_on_allocation();
480
481 AdapterBlob* blob = nullptr;
482 unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob));
483 {
484 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
485 blob = new (size) AdapterBlob(size, cb, entry_offset, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
486 }
487 // Track memory usage statistic after releasing CodeCache_lock
488 MemoryService::track_code_cache_memory_usage();
489
490 return blob;
491 }
492
493 //----------------------------------------------------------------------------------------------------
494 // Implementation of VtableBlob
495
496 void* VtableBlob::operator new(size_t s, unsigned size) throw() {
497 // Handling of allocation failure stops compilation and prints a bunch of
498 // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock
499 // can be locked, and then re-locking the CodeCache_lock. That is not safe in
500 // this context as we hold the CompiledICLocker. So we just don't handle code
501 // cache exhaustion here; we leave that for a later allocation that does not
502 // hold the CompiledICLocker.
503 return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */);
504 }
505
548 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
549
550 MethodHandlesAdapterBlob* blob = nullptr;
551 unsigned int size = sizeof(MethodHandlesAdapterBlob);
552 // align the size to CodeEntryAlignment
553 size = CodeBlob::align_code_offset(size);
554 size += align_up(buffer_size, oopSize);
555 {
556 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
557 blob = new (size) MethodHandlesAdapterBlob(size);
558 if (blob == nullptr) {
559 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob");
560 }
561 }
562 // Track memory usage statistic after releasing CodeCache_lock
563 MemoryService::track_code_cache_memory_usage();
564
565 return blob;
566 }
567
568 //----------------------------------------------------------------------------------------------------
569 // Implementation of BufferedInlineTypeBlob
570 BufferedInlineTypeBlob::BufferedInlineTypeBlob(int size, CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) :
571 BufferBlob("buffered inline type", CodeBlobKind::BufferedInlineType, cb, size, sizeof(BufferedInlineTypeBlob)),
572 _pack_fields_off(pack_fields_off),
573 _pack_fields_jobject_off(pack_fields_jobject_off),
574 _unpack_fields_off(unpack_fields_off) {
575 CodeCache::commit(this);
576 }
577
578 BufferedInlineTypeBlob* BufferedInlineTypeBlob::create(CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off) {
579 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock
580
581 BufferedInlineTypeBlob* blob = nullptr;
582 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferedInlineTypeBlob));
583 {
584 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
585 blob = new (size) BufferedInlineTypeBlob(size, cb, pack_fields_off, pack_fields_jobject_off, unpack_fields_off);
586 }
587 // Track memory usage statistic after releasing CodeCache_lock
588 MemoryService::track_code_cache_memory_usage();
589
590 return blob;
591 }
592
593 //----------------------------------------------------------------------------------------------------
594 // Implementation of RuntimeStub
595
596 RuntimeStub::RuntimeStub(
597 const char* name,
598 CodeBuffer* cb,
599 int size,
600 int16_t frame_complete,
601 int frame_size,
602 OopMapSet* oop_maps,
603 bool caller_must_gc_arguments
604 )
605 : RuntimeBlob(name, CodeBlobKind::RuntimeStub, cb, size, sizeof(RuntimeStub),
606 frame_complete, frame_size, oop_maps, caller_must_gc_arguments)
607 {
608 }
609
610 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
611 CodeBuffer* cb,
612 int16_t frame_complete,
|