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