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