1 /* 2 * Copyright (c) 1998, 2025, 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 "code/codeBlob.hpp" 26 #include "code/codeCache.hpp" 27 #include "code/relocInfo.hpp" 28 #include "code/vtableStubs.hpp" 29 #include "compiler/disassembler.hpp" 30 #include "compiler/oopMap.hpp" 31 #include "interpreter/bytecode.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "jvm.h" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/heap.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "prims/forte.hpp" 39 #include "prims/jvmtiExport.hpp" 40 #include "runtime/handles.inline.hpp" 41 #include "runtime/interfaceSupport.inline.hpp" 42 #include "runtime/javaFrameAnchor.hpp" 43 #include "runtime/jniHandles.inline.hpp" 44 #include "runtime/mutexLocker.hpp" 45 #include "runtime/safepoint.hpp" 46 #include "runtime/sharedRuntime.hpp" 47 #include "runtime/stubCodeGenerator.hpp" 48 #include "runtime/stubRoutines.hpp" 49 #include "runtime/vframe.hpp" 50 #include "services/memoryService.hpp" 51 #include "utilities/align.hpp" 52 #ifdef COMPILER1 53 #include "c1/c1_Runtime1.hpp" 54 #endif 55 56 #include <type_traits> 57 58 // Virtual methods are not allowed in code blobs to simplify caching compiled code. 59 // Check all "leaf" subclasses of CodeBlob class. 60 61 static_assert(!std::is_polymorphic<nmethod>::value, "no virtual methods are allowed in nmethod"); 62 static_assert(!std::is_polymorphic<AdapterBlob>::value, "no virtual methods are allowed in code blobs"); 63 static_assert(!std::is_polymorphic<VtableBlob>::value, "no virtual methods are allowed in code blobs"); 64 static_assert(!std::is_polymorphic<MethodHandlesAdapterBlob>::value, "no virtual methods are allowed in code blobs"); 65 static_assert(!std::is_polymorphic<RuntimeStub>::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 &RuntimeStub::_vpntr, 95 &DeoptimizationBlob::_vpntr, 96 &SafepointBlob::_vpntr, 97 #ifdef COMPILER2 98 &ExceptionBlob::_vpntr, 99 &UncommonTrapBlob::_vpntr, 100 #endif 101 &UpcallStub::_vpntr 102 }; 103 104 return array[(size_t)kind]; 105 } 106 107 const CodeBlob::Vptr* CodeBlob::vptr() const { 108 return vptr(_kind); 109 } 110 111 unsigned int CodeBlob::align_code_offset(int offset) { 112 // align the size to CodeEntryAlignment 113 int header_size = (int)CodeHeap::header_size(); 114 return align_up(offset + header_size, CodeEntryAlignment) - header_size; 115 } 116 117 // This must be consistent with the CodeBlob constructor's layout actions. 118 unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) { 119 // align the size to CodeEntryAlignment 120 unsigned int size = align_code_offset(header_size); 121 size += align_up(cb->total_content_size(), oopSize); 122 size += align_up(cb->total_oop_size(), oopSize); 123 return size; 124 } 125 126 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size, 127 int16_t frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments, 128 int mutable_data_size) : 129 _oop_maps(nullptr), // will be set by set_oop_maps() call 130 _name(name), 131 _mutable_data(header_begin() + size), // default value is blob_end() 132 _size(size), 133 _relocation_size(align_up(cb->total_relocation_size(), oopSize)), 134 _content_offset(CodeBlob::align_code_offset(header_size)), 135 _code_offset(_content_offset + cb->total_offset_of(cb->insts())), 136 _data_offset(_content_offset + align_up(cb->total_content_size(), oopSize)), 137 _frame_size(frame_size), 138 _mutable_data_size(mutable_data_size), 139 S390_ONLY(_ctable_offset(0) COMMA) 140 _header_size(header_size), 141 _frame_complete_offset(frame_complete_offset), 142 _kind(kind), 143 _caller_must_gc_arguments(caller_must_gc_arguments) 144 { 145 assert(is_aligned(_size, oopSize), "unaligned size"); 146 assert(is_aligned(header_size, oopSize), "unaligned size"); 147 assert(is_aligned(_relocation_size, oopSize), "unaligned size"); 148 assert(_data_offset <= _size, "codeBlob is too small: %d > %d", _data_offset, _size); 149 assert(is_nmethod() || (cb->total_oop_size() + cb->total_metadata_size() == 0), "must be nmethod"); 150 assert(code_end() == content_end(), "must be the same - see code_end()"); 151 #ifdef COMPILER1 152 // probably wrong for tiered 153 assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs"); 154 #endif // COMPILER1 155 156 if (_mutable_data_size > 0) { 157 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode); 158 if (_mutable_data == nullptr) { 159 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data"); 160 } 161 } else { 162 // We need unique and valid not null address 163 assert(_mutable_data == blob_end(), "sanity"); 164 } 165 166 set_oop_maps(oop_maps); 167 } 168 169 // Simple CodeBlob used for simple BufferBlob. 170 CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size) : 171 _oop_maps(nullptr), 172 _name(name), 173 _mutable_data(header_begin() + size), // default value is blob_end() 174 _size(size), 175 _relocation_size(0), 176 _content_offset(CodeBlob::align_code_offset(header_size)), 177 _code_offset(_content_offset), 178 _data_offset(size), 179 _frame_size(0), 180 _mutable_data_size(0), 181 S390_ONLY(_ctable_offset(0) COMMA) 182 _header_size(header_size), 183 _frame_complete_offset(CodeOffsets::frame_never_safe), 184 _kind(kind), 185 _caller_must_gc_arguments(false) 186 { 187 assert(is_aligned(size, oopSize), "unaligned size"); 188 assert(is_aligned(header_size, oopSize), "unaligned size"); 189 assert(_mutable_data == blob_end(), "sanity"); 190 } 191 192 void CodeBlob::restore_mutable_data(address reloc_data) { 193 // Relocation data is now stored as part of the mutable data area; allocate it before copy relocations 194 if (_mutable_data_size > 0) { 195 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode); 196 if (_mutable_data == nullptr) { 197 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data"); 198 } 199 } else { 200 _mutable_data = blob_end(); // default value 201 } 202 if (_relocation_size > 0) { 203 assert(_mutable_data_size > 0, "relocation is part of mutable data section"); 204 memcpy((address)relocation_begin(), reloc_data, relocation_size()); 205 } 206 } 207 208 void CodeBlob::purge() { 209 assert(_mutable_data != nullptr, "should never be null"); 210 if (_mutable_data != blob_end()) { 211 os::free(_mutable_data); 212 _mutable_data = blob_end(); // Valid not null address 213 } 214 if (_oop_maps != nullptr && !AOTCodeCache::is_address_in_aot_cache((address)_oop_maps)) { 215 delete _oop_maps; 216 _oop_maps = nullptr; 217 } 218 NOT_PRODUCT(_asm_remarks.clear()); 219 NOT_PRODUCT(_dbg_strings.clear()); 220 } 221 222 void CodeBlob::set_oop_maps(OopMapSet* p) { 223 // Danger Will Robinson! This method allocates a big 224 // chunk of memory, its your job to free it. 225 if (p != nullptr) { 226 _oop_maps = ImmutableOopMapSet::build_from(p); 227 } else { 228 _oop_maps = nullptr; 229 } 230 } 231 232 const ImmutableOopMap* CodeBlob::oop_map_for_return_address(address return_address) const { 233 assert(_oop_maps != nullptr, "nope"); 234 return _oop_maps->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin()); 235 } 236 237 void CodeBlob::print_code_on(outputStream* st) { 238 ResourceMark m; 239 Disassembler::decode(this, st); 240 } 241 242 void CodeBlob::prepare_for_archiving_impl() { 243 set_name(nullptr); 244 _oop_maps = nullptr; 245 _mutable_data = nullptr; 246 #ifndef PRODUCT 247 asm_remarks().clear_ref(); 248 dbg_strings().clear_ref(); 249 #endif /* PRODUCT */ 250 } 251 252 void CodeBlob::prepare_for_archiving() { 253 vptr(_kind)->prepare_for_archiving(this); 254 } 255 256 void CodeBlob::archive_blob(CodeBlob* blob, address archive_buffer) { 257 blob->copy_to(archive_buffer); 258 CodeBlob* archived_blob = (CodeBlob*)archive_buffer; 259 archived_blob->prepare_for_archiving(); 260 } 261 262 void CodeBlob::post_restore_impl() { 263 // Track memory usage statistic after releasing CodeCache_lock 264 MemoryService::track_code_cache_memory_usage(); 265 } 266 267 void CodeBlob::post_restore() { 268 vptr(_kind)->post_restore(this); 269 } 270 271 CodeBlob* CodeBlob::restore(address code_cache_buffer, 272 const char* name, 273 address archived_reloc_data, 274 ImmutableOopMapSet* archived_oop_maps) 275 { 276 copy_to(code_cache_buffer); 277 CodeBlob* code_blob = (CodeBlob*)code_cache_buffer; 278 code_blob->set_name(name); 279 code_blob->restore_mutable_data(archived_reloc_data); 280 code_blob->set_oop_maps(archived_oop_maps); 281 return code_blob; 282 } 283 284 CodeBlob* CodeBlob::create(CodeBlob* archived_blob, 285 const char* name, 286 address archived_reloc_data, 287 ImmutableOopMapSet* archived_oop_maps 288 ) 289 { 290 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 291 292 CodeCache::gc_on_allocation(); 293 294 CodeBlob* blob = nullptr; 295 unsigned int size = archived_blob->size(); 296 { 297 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 298 address code_cache_buffer = (address)CodeCache::allocate(size, CodeBlobType::NonNMethod); 299 if (code_cache_buffer != nullptr) { 300 blob = archived_blob->restore(code_cache_buffer, 301 name, 302 archived_reloc_data, 303 archived_oop_maps); 304 305 assert(blob != nullptr, "sanity check"); 306 // Flush the code block 307 ICache::invalidate_range(blob->code_begin(), blob->code_size()); 308 CodeCache::commit(blob); // Count adapters 309 } 310 } 311 if (blob != nullptr) { 312 blob->post_restore(); 313 } 314 return blob; 315 } 316 317 //----------------------------------------------------------------------------------------- 318 // Creates a RuntimeBlob from a CodeBuffer and copy code and relocation info. 319 320 RuntimeBlob::RuntimeBlob( 321 const char* name, 322 CodeBlobKind kind, 323 CodeBuffer* cb, 324 int size, 325 uint16_t header_size, 326 int16_t frame_complete, 327 int frame_size, 328 OopMapSet* oop_maps, 329 bool caller_must_gc_arguments) 330 : CodeBlob(name, kind, cb, size, header_size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments, 331 align_up(cb->total_relocation_size(), oopSize)) 332 { 333 cb->copy_code_and_locs_to(this); 334 } 335 336 void RuntimeBlob::free(RuntimeBlob* blob) { 337 assert(blob != nullptr, "caller must check for nullptr"); 338 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 339 blob->purge(); 340 { 341 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 342 CodeCache::free(blob); 343 } 344 // Track memory usage statistic after releasing CodeCache_lock 345 MemoryService::track_code_cache_memory_usage(); 346 } 347 348 void RuntimeBlob::trace_new_stub(RuntimeBlob* stub, const char* name1, const char* name2) { 349 // Do not hold the CodeCache lock during name formatting. 350 assert(!CodeCache_lock->owned_by_self(), "release CodeCache before registering the stub"); 351 352 if (stub != nullptr && (PrintStubCode || 353 Forte::is_enabled() || 354 JvmtiExport::should_post_dynamic_code_generated())) { 355 char stub_id[256]; 356 assert(strlen(name1) + strlen(name2) < sizeof(stub_id), ""); 357 jio_snprintf(stub_id, sizeof(stub_id), "%s%s", name1, name2); 358 if (PrintStubCode) { 359 ttyLocker ttyl; 360 tty->print_cr("- - - [BEGIN] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); 361 tty->print_cr("Decoding %s " PTR_FORMAT " [" PTR_FORMAT ", " PTR_FORMAT "] (%d bytes)", 362 stub_id, p2i(stub), p2i(stub->code_begin()), p2i(stub->code_end()), stub->code_size()); 363 Disassembler::decode(stub->code_begin(), stub->code_end(), tty 364 NOT_PRODUCT(COMMA &stub->asm_remarks())); 365 if ((stub->oop_maps() != nullptr) && AbstractDisassembler::show_structs()) { 366 tty->print_cr("- - - [OOP MAPS]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); 367 stub->oop_maps()->print(); 368 } 369 tty->print_cr("- - - [END] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); 370 tty->cr(); 371 } 372 if (Forte::is_enabled()) { 373 Forte::register_stub(stub_id, stub->code_begin(), stub->code_end()); 374 } 375 376 if (JvmtiExport::should_post_dynamic_code_generated()) { 377 const char* stub_name = name2; 378 if (name2[0] == '\0') stub_name = name1; 379 JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end()); 380 } 381 } 382 383 // Track memory usage statistic after releasing CodeCache_lock 384 MemoryService::track_code_cache_memory_usage(); 385 } 386 387 //---------------------------------------------------------------------------------------------------- 388 // Implementation of BufferBlob 389 390 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, int size) 391 : RuntimeBlob(name, kind, size, sizeof(BufferBlob)) 392 {} 393 394 BufferBlob* BufferBlob::create(const char* name, uint buffer_size) { 395 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 396 397 BufferBlob* blob = nullptr; 398 unsigned int size = sizeof(BufferBlob); 399 // align the size to CodeEntryAlignment 400 size = CodeBlob::align_code_offset(size); 401 size += align_up(buffer_size, oopSize); 402 assert(name != nullptr, "must provide a name"); 403 { 404 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 405 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, size); 406 } 407 // Track memory usage statistic after releasing CodeCache_lock 408 MemoryService::track_code_cache_memory_usage(); 409 410 return blob; 411 } 412 413 414 BufferBlob::BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size) 415 : RuntimeBlob(name, kind, cb, size, sizeof(BufferBlob), CodeOffsets::frame_never_safe, 0, nullptr) 416 {} 417 418 // Used by gtest 419 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) { 420 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 421 422 BufferBlob* blob = nullptr; 423 unsigned int size = CodeBlob::allocation_size(cb, sizeof(BufferBlob)); 424 assert(name != nullptr, "must provide a name"); 425 { 426 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 427 blob = new (size) BufferBlob(name, CodeBlobKind::Buffer, cb, size); 428 } 429 // Track memory usage statistic after releasing CodeCache_lock 430 MemoryService::track_code_cache_memory_usage(); 431 432 return blob; 433 } 434 435 void* BufferBlob::operator new(size_t s, unsigned size) throw() { 436 return CodeCache::allocate(size, CodeBlobType::NonNMethod); 437 } 438 439 void BufferBlob::free(BufferBlob *blob) { 440 RuntimeBlob::free(blob); 441 } 442 443 444 //---------------------------------------------------------------------------------------------------- 445 // Implementation of AdapterBlob 446 447 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) : 448 BufferBlob("I2C/C2I adapters", CodeBlobKind::Adapter, cb, size) { 449 CodeCache::commit(this); 450 } 451 452 AdapterBlob* AdapterBlob::create(CodeBuffer* cb) { 453 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 454 455 CodeCache::gc_on_allocation(); 456 457 AdapterBlob* blob = nullptr; 458 unsigned int size = CodeBlob::allocation_size(cb, sizeof(AdapterBlob)); 459 { 460 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 461 blob = new (size) AdapterBlob(size, cb); 462 } 463 // Track memory usage statistic after releasing CodeCache_lock 464 MemoryService::track_code_cache_memory_usage(); 465 466 return blob; 467 } 468 469 //---------------------------------------------------------------------------------------------------- 470 // Implementation of VtableBlob 471 472 void* VtableBlob::operator new(size_t s, unsigned size) throw() { 473 // Handling of allocation failure stops compilation and prints a bunch of 474 // stuff, which requires unlocking the CodeCache_lock, so that the Compile_lock 475 // can be locked, and then re-locking the CodeCache_lock. That is not safe in 476 // this context as we hold the CompiledICLocker. So we just don't handle code 477 // cache exhaustion here; we leave that for a later allocation that does not 478 // hold the CompiledICLocker. 479 return CodeCache::allocate(size, CodeBlobType::NonNMethod, false /* handle_alloc_failure */); 480 } 481 482 VtableBlob::VtableBlob(const char* name, int size) : 483 BufferBlob(name, CodeBlobKind::Vtable, size) { 484 } 485 486 VtableBlob* VtableBlob::create(const char* name, int buffer_size) { 487 assert(JavaThread::current()->thread_state() == _thread_in_vm, "called with the wrong state"); 488 489 VtableBlob* blob = nullptr; 490 unsigned int size = sizeof(VtableBlob); 491 // align the size to CodeEntryAlignment 492 size = align_code_offset(size); 493 size += align_up(buffer_size, oopSize); 494 assert(name != nullptr, "must provide a name"); 495 { 496 if (!CodeCache_lock->try_lock()) { 497 // If we can't take the CodeCache_lock, then this is a bad time to perform the ongoing 498 // IC transition to megamorphic, for which this stub will be needed. It is better to 499 // bail out the transition, and wait for a more opportune moment. Not only is it not 500 // worth waiting for the lock blockingly for the megamorphic transition, it might 501 // also result in a deadlock to blockingly wait, when concurrent class unloading is 502 // performed. At this point in time, the CompiledICLocker is taken, so we are not 503 // allowed to blockingly wait for the CodeCache_lock, as these two locks are otherwise 504 // consistently taken in the opposite order. Bailing out results in an IC transition to 505 // the clean state instead, which will cause subsequent calls to retry the transitioning 506 // eventually. 507 return nullptr; 508 } 509 blob = new (size) VtableBlob(name, size); 510 CodeCache_lock->unlock(); 511 } 512 // Track memory usage statistic after releasing CodeCache_lock 513 MemoryService::track_code_cache_memory_usage(); 514 515 return blob; 516 } 517 518 //---------------------------------------------------------------------------------------------------- 519 // Implementation of MethodHandlesAdapterBlob 520 521 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) { 522 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 523 524 MethodHandlesAdapterBlob* blob = nullptr; 525 unsigned int size = sizeof(MethodHandlesAdapterBlob); 526 // align the size to CodeEntryAlignment 527 size = CodeBlob::align_code_offset(size); 528 size += align_up(buffer_size, oopSize); 529 { 530 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 531 blob = new (size) MethodHandlesAdapterBlob(size); 532 if (blob == nullptr) { 533 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for method handle adapter blob"); 534 } 535 } 536 // Track memory usage statistic after releasing CodeCache_lock 537 MemoryService::track_code_cache_memory_usage(); 538 539 return blob; 540 } 541 542 //---------------------------------------------------------------------------------------------------- 543 // Implementation of RuntimeStub 544 545 RuntimeStub::RuntimeStub( 546 const char* name, 547 CodeBuffer* cb, 548 int size, 549 int16_t frame_complete, 550 int frame_size, 551 OopMapSet* oop_maps, 552 bool caller_must_gc_arguments 553 ) 554 : RuntimeBlob(name, CodeBlobKind::RuntimeStub, cb, size, sizeof(RuntimeStub), 555 frame_complete, frame_size, oop_maps, caller_must_gc_arguments) 556 { 557 } 558 559 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name, 560 CodeBuffer* cb, 561 int16_t frame_complete, 562 int frame_size, 563 OopMapSet* oop_maps, 564 bool caller_must_gc_arguments, 565 bool alloc_fail_is_fatal) 566 { 567 RuntimeStub* stub = nullptr; 568 unsigned int size = CodeBlob::allocation_size(cb, sizeof(RuntimeStub)); 569 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 570 { 571 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 572 stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments); 573 if (stub == nullptr) { 574 if (!alloc_fail_is_fatal) { 575 return nullptr; 576 } 577 fatal("Initial size of CodeCache is too small"); 578 } 579 } 580 581 trace_new_stub(stub, "RuntimeStub - ", stub_name); 582 583 return stub; 584 } 585 586 587 void* RuntimeStub::operator new(size_t s, unsigned size) throw() { 588 return CodeCache::allocate(size, CodeBlobType::NonNMethod); 589 } 590 591 // operator new shared by all singletons: 592 void* SingletonBlob::operator new(size_t s, unsigned size, bool alloc_fail_is_fatal) throw() { 593 void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod); 594 if (alloc_fail_is_fatal && !p) fatal("Initial size of CodeCache is too small"); 595 return p; 596 } 597 598 599 //---------------------------------------------------------------------------------------------------- 600 // Implementation of DeoptimizationBlob 601 602 DeoptimizationBlob::DeoptimizationBlob( 603 CodeBuffer* cb, 604 int size, 605 OopMapSet* oop_maps, 606 int unpack_offset, 607 int unpack_with_exception_offset, 608 int unpack_with_reexecution_offset, 609 int frame_size 610 ) 611 : SingletonBlob("DeoptimizationBlob", CodeBlobKind::Deoptimization, cb, 612 size, sizeof(DeoptimizationBlob), frame_size, oop_maps) 613 { 614 _unpack_offset = unpack_offset; 615 _unpack_with_exception = unpack_with_exception_offset; 616 _unpack_with_reexecution = unpack_with_reexecution_offset; 617 #ifdef COMPILER1 618 _unpack_with_exception_in_tls = -1; 619 #endif 620 } 621 622 623 DeoptimizationBlob* DeoptimizationBlob::create( 624 CodeBuffer* cb, 625 OopMapSet* oop_maps, 626 int unpack_offset, 627 int unpack_with_exception_offset, 628 int unpack_with_reexecution_offset, 629 int frame_size) 630 { 631 DeoptimizationBlob* blob = nullptr; 632 unsigned int size = CodeBlob::allocation_size(cb, sizeof(DeoptimizationBlob)); 633 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 634 { 635 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 636 blob = new (size) DeoptimizationBlob(cb, 637 size, 638 oop_maps, 639 unpack_offset, 640 unpack_with_exception_offset, 641 unpack_with_reexecution_offset, 642 frame_size); 643 } 644 645 trace_new_stub(blob, "DeoptimizationBlob"); 646 647 return blob; 648 } 649 650 #ifdef COMPILER2 651 652 //---------------------------------------------------------------------------------------------------- 653 // Implementation of UncommonTrapBlob 654 655 UncommonTrapBlob::UncommonTrapBlob( 656 CodeBuffer* cb, 657 int size, 658 OopMapSet* oop_maps, 659 int frame_size 660 ) 661 : SingletonBlob("UncommonTrapBlob", CodeBlobKind::UncommonTrap, cb, 662 size, sizeof(UncommonTrapBlob), frame_size, oop_maps) 663 {} 664 665 666 UncommonTrapBlob* UncommonTrapBlob::create( 667 CodeBuffer* cb, 668 OopMapSet* oop_maps, 669 int frame_size) 670 { 671 UncommonTrapBlob* blob = nullptr; 672 unsigned int size = CodeBlob::allocation_size(cb, sizeof(UncommonTrapBlob)); 673 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 674 { 675 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 676 blob = new (size, false) UncommonTrapBlob(cb, size, oop_maps, frame_size); 677 } 678 679 trace_new_stub(blob, "UncommonTrapBlob"); 680 681 return blob; 682 } 683 684 //---------------------------------------------------------------------------------------------------- 685 // Implementation of ExceptionBlob 686 687 ExceptionBlob::ExceptionBlob( 688 CodeBuffer* cb, 689 int size, 690 OopMapSet* oop_maps, 691 int frame_size 692 ) 693 : SingletonBlob("ExceptionBlob", CodeBlobKind::Exception, cb, 694 size, sizeof(ExceptionBlob), frame_size, oop_maps) 695 {} 696 697 698 ExceptionBlob* ExceptionBlob::create( 699 CodeBuffer* cb, 700 OopMapSet* oop_maps, 701 int frame_size) 702 { 703 ExceptionBlob* blob = nullptr; 704 unsigned int size = CodeBlob::allocation_size(cb, sizeof(ExceptionBlob)); 705 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 706 { 707 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 708 blob = new (size, false) ExceptionBlob(cb, size, oop_maps, frame_size); 709 } 710 711 trace_new_stub(blob, "ExceptionBlob"); 712 713 return blob; 714 } 715 716 #endif // COMPILER2 717 718 //---------------------------------------------------------------------------------------------------- 719 // Implementation of SafepointBlob 720 721 SafepointBlob::SafepointBlob( 722 CodeBuffer* cb, 723 int size, 724 OopMapSet* oop_maps, 725 int frame_size 726 ) 727 : SingletonBlob("SafepointBlob", CodeBlobKind::Safepoint, cb, 728 size, sizeof(SafepointBlob), frame_size, oop_maps) 729 {} 730 731 732 SafepointBlob* SafepointBlob::create( 733 CodeBuffer* cb, 734 OopMapSet* oop_maps, 735 int frame_size) 736 { 737 SafepointBlob* blob = nullptr; 738 unsigned int size = CodeBlob::allocation_size(cb, sizeof(SafepointBlob)); 739 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 740 { 741 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 742 blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size); 743 } 744 745 trace_new_stub(blob, "SafepointBlob"); 746 747 return blob; 748 } 749 750 //---------------------------------------------------------------------------------------------------- 751 // Implementation of UpcallStub 752 753 UpcallStub::UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset) : 754 RuntimeBlob(name, CodeBlobKind::Upcall, cb, size, sizeof(UpcallStub), 755 CodeOffsets::frame_never_safe, 0 /* no frame size */, 756 /* oop maps = */ nullptr, /* caller must gc arguments = */ false), 757 _receiver(receiver), 758 _frame_data_offset(frame_data_offset) 759 { 760 CodeCache::commit(this); 761 } 762 763 void* UpcallStub::operator new(size_t s, unsigned size) throw() { 764 return CodeCache::allocate(size, CodeBlobType::NonNMethod); 765 } 766 767 UpcallStub* UpcallStub::create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset) { 768 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 769 770 UpcallStub* blob = nullptr; 771 unsigned int size = CodeBlob::allocation_size(cb, sizeof(UpcallStub)); 772 { 773 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 774 blob = new (size) UpcallStub(name, cb, size, receiver, frame_data_offset); 775 } 776 if (blob == nullptr) { 777 return nullptr; // caller must handle this 778 } 779 780 // Track memory usage statistic after releasing CodeCache_lock 781 MemoryService::track_code_cache_memory_usage(); 782 783 trace_new_stub(blob, "UpcallStub - ", name); 784 785 return blob; 786 } 787 788 void UpcallStub::oops_do(OopClosure* f, const frame& frame) { 789 frame_data_for_frame(frame)->old_handles->oops_do(f); 790 } 791 792 JavaFrameAnchor* UpcallStub::jfa_for_frame(const frame& frame) const { 793 return &frame_data_for_frame(frame)->jfa; 794 } 795 796 void UpcallStub::free(UpcallStub* blob) { 797 assert(blob != nullptr, "caller must check for nullptr"); 798 JNIHandles::destroy_global(blob->receiver()); 799 RuntimeBlob::free(blob); 800 } 801 802 //---------------------------------------------------------------------------------------------------- 803 // Verification and printing 804 805 void CodeBlob::verify() { 806 if (is_nmethod()) { 807 as_nmethod()->verify(); 808 } 809 } 810 811 void CodeBlob::print_on(outputStream* st) const { 812 vptr()->print_on(this, st); 813 } 814 815 void CodeBlob::print() const { print_on(tty); } 816 817 void CodeBlob::print_value_on(outputStream* st) const { 818 vptr()->print_value_on(this, st); 819 } 820 821 void CodeBlob::print_on_impl(outputStream* st) const { 822 st->print_cr("[CodeBlob kind:%d (" INTPTR_FORMAT ")]", (int)_kind, p2i(this)); 823 st->print_cr("Framesize: %d", _frame_size); 824 } 825 826 void CodeBlob::print_value_on_impl(outputStream* st) const { 827 st->print_cr("[CodeBlob]"); 828 } 829 830 void CodeBlob::print_block_comment(outputStream* stream, address block_begin) const { 831 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY) 832 if (is_nmethod()) { 833 as_nmethod()->print_nmethod_labels(stream, block_begin); 834 } 835 #endif 836 837 #ifndef PRODUCT 838 ptrdiff_t offset = block_begin - code_begin(); 839 assert(offset >= 0, "Expecting non-negative offset!"); 840 _asm_remarks.print(uint(offset), stream); 841 #endif 842 } 843 844 void CodeBlob::dump_for_addr(address addr, outputStream* st, bool verbose) const { 845 if (is_buffer_blob() || is_adapter_blob() || is_vtable_blob() || is_method_handles_adapter_blob()) { 846 // the interpreter is generated into a buffer blob 847 InterpreterCodelet* i = Interpreter::codelet_containing(addr); 848 if (i != nullptr) { 849 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", p2i(addr), (int)(addr - i->code_begin())); 850 i->print_on(st); 851 return; 852 } 853 if (Interpreter::contains(addr)) { 854 st->print_cr(INTPTR_FORMAT " is pointing into interpreter code" 855 " (not bytecode specific)", p2i(addr)); 856 return; 857 } 858 // 859 if (AdapterHandlerLibrary::contains(this)) { 860 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", p2i(addr), (int)(addr - code_begin())); 861 AdapterHandlerLibrary::print_handler_on(st, this); 862 } 863 // the stubroutines are generated into a buffer blob 864 StubCodeDesc* d = StubCodeDesc::desc_for(addr); 865 if (d != nullptr) { 866 st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", p2i(addr), (int)(addr - d->begin())); 867 d->print_on(st); 868 st->cr(); 869 return; 870 } 871 if (StubRoutines::contains(addr)) { 872 st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) stub routine", p2i(addr)); 873 return; 874 } 875 VtableStub* v = VtableStubs::stub_containing(addr); 876 if (v != nullptr) { 877 st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", p2i(addr), (int)(addr - v->entry_point())); 878 v->print_on(st); 879 st->cr(); 880 return; 881 } 882 } 883 if (is_nmethod()) { 884 nmethod* nm = (nmethod*)this; 885 ResourceMark rm; 886 st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT, 887 p2i(addr), (int)(addr - nm->entry_point()), p2i(nm)); 888 if (verbose) { 889 st->print(" for "); 890 nm->method()->print_value_on(st); 891 } 892 st->cr(); 893 if (verbose && st == tty) { 894 // verbose is only ever true when called from findpc in debug.cpp 895 nm->print_nmethod(true); 896 } else { 897 nm->print_on(st); 898 } 899 return; 900 } 901 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", p2i(addr), (int)(addr - code_begin())); 902 print_on(st); 903 } 904 905 void BufferBlob::print_on_impl(outputStream* st) const { 906 RuntimeBlob::print_on_impl(st); 907 print_value_on_impl(st); 908 } 909 910 void BufferBlob::print_value_on_impl(outputStream* st) const { 911 st->print_cr("BufferBlob (" INTPTR_FORMAT ") used for %s", p2i(this), name()); 912 } 913 914 void RuntimeStub::print_on_impl(outputStream* st) const { 915 ttyLocker ttyl; 916 RuntimeBlob::print_on_impl(st); 917 st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this)); 918 st->print_cr("%s", name()); 919 Disassembler::decode((RuntimeBlob*)this, st); 920 } 921 922 void RuntimeStub::print_value_on_impl(outputStream* st) const { 923 st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name()); 924 } 925 926 void SingletonBlob::print_on_impl(outputStream* st) const { 927 ttyLocker ttyl; 928 RuntimeBlob::print_on_impl(st); 929 st->print_cr("%s", name()); 930 Disassembler::decode((RuntimeBlob*)this, st); 931 } 932 933 void SingletonBlob::print_value_on_impl(outputStream* st) const { 934 st->print_cr("%s", name()); 935 } 936 937 void DeoptimizationBlob::print_value_on_impl(outputStream* st) const { 938 st->print_cr("Deoptimization (frame not available)"); 939 } 940 941 void UpcallStub::print_on_impl(outputStream* st) const { 942 RuntimeBlob::print_on_impl(st); 943 print_value_on_impl(st); 944 st->print_cr("Frame data offset: %d", (int) _frame_data_offset); 945 oop recv = JNIHandles::resolve(_receiver); 946 st->print("Receiver MH="); 947 recv->print_on(st); 948 Disassembler::decode((RuntimeBlob*)this, st); 949 } 950 951 void UpcallStub::print_value_on_impl(outputStream* st) const { 952 st->print_cr("UpcallStub (" INTPTR_FORMAT ") used for %s", p2i(this), name()); 953 }