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