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