1 /*
2 * Copyright (c) 2012, 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 #include "classfile/javaClasses.inline.hpp"
25 #include "classfile/symbolTable.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmClasses.hpp"
28 #include "compiler/compileBroker.hpp"
29 #include "gc/shared/collectedHeap.hpp"
30 #include "gc/shared/memAllocator.hpp"
31 #include "gc/shared/oopStorage.inline.hpp"
32 #include "jvmci/jniAccessMark.inline.hpp"
33 #include "jvmci/jvmciCodeInstaller.hpp"
34 #include "jvmci/jvmciCompilerToVM.hpp"
35 #include "jvmci/jvmciRuntime.hpp"
36 #include "jvmci/metadataHandles.hpp"
37 #include "logging/log.hpp"
38 #include "logging/logStream.hpp"
39 #include "memory/oopFactory.hpp"
40 #include "memory/universe.hpp"
41 #include "oops/constantPool.inline.hpp"
42 #include "oops/klass.inline.hpp"
43 #include "oops/method.inline.hpp"
44 #include "oops/objArrayKlass.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "oops/typeArrayOop.inline.hpp"
47 #include "prims/jvmtiExport.hpp"
48 #include "prims/methodHandles.hpp"
49 #include "runtime/arguments.hpp"
50 #include "runtime/atomic.hpp"
51 #include "runtime/deoptimization.hpp"
52 #include "runtime/fieldDescriptor.inline.hpp"
53 #include "runtime/frame.inline.hpp"
54 #include "runtime/java.hpp"
55 #include "runtime/jniHandles.inline.hpp"
56 #include "runtime/mutex.hpp"
57 #include "runtime/reflection.hpp"
58 #include "runtime/sharedRuntime.hpp"
59 #include "runtime/synchronizer.hpp"
60 #if INCLUDE_G1GC
61 #include "gc/g1/g1BarrierSetRuntime.hpp"
62 #endif // INCLUDE_G1GC
63
64 // Simple helper to see if the caller of a runtime stub which
65 // entered the VM has been deoptimized
66
67 static bool caller_is_deopted() {
68 JavaThread* thread = JavaThread::current();
69 RegisterMap reg_map(thread,
70 RegisterMap::UpdateMap::skip,
71 RegisterMap::ProcessFrames::include,
72 RegisterMap::WalkContinuation::skip);
73 frame runtime_frame = thread->last_frame();
74 frame caller_frame = runtime_frame.sender(®_map);
75 assert(caller_frame.is_compiled_frame(), "must be compiled");
76 return caller_frame.is_deoptimized_frame();
77 }
78
79 // Stress deoptimization
80 static void deopt_caller() {
81 if ( !caller_is_deopted()) {
82 JavaThread* thread = JavaThread::current();
83 RegisterMap reg_map(thread,
84 RegisterMap::UpdateMap::skip,
85 RegisterMap::ProcessFrames::include,
86 RegisterMap::WalkContinuation::skip);
87 frame runtime_frame = thread->last_frame();
88 frame caller_frame = runtime_frame.sender(®_map);
89 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint);
90 assert(caller_is_deopted(), "Must be deoptimized");
91 }
92 }
93
94 // Manages a scope for a JVMCI runtime call that attempts a heap allocation.
95 // If there is a pending OutOfMemoryError upon closing the scope and the runtime
96 // call is of the variety where allocation failure returns null without an
97 // exception, the following action is taken:
98 // 1. The pending OutOfMemoryError is cleared
99 // 2. null is written to JavaThread::_vm_result_oop
100 class RetryableAllocationMark {
101 private:
102 InternalOOMEMark _iom;
103 public:
104 RetryableAllocationMark(JavaThread* thread) : _iom(thread) {}
105 ~RetryableAllocationMark() {
106 JavaThread* THREAD = _iom.thread(); // For exception macros.
107 if (THREAD != nullptr) {
108 if (HAS_PENDING_EXCEPTION) {
109 oop ex = PENDING_EXCEPTION;
110 THREAD->set_vm_result_oop(nullptr);
111 if (ex->is_a(vmClasses::OutOfMemoryError_klass())) {
112 CLEAR_PENDING_EXCEPTION;
113 }
114 }
115 }
116 }
117 };
118
119 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_or_null(JavaThread* current, Klass* klass))
120 JRT_BLOCK;
121 assert(klass->is_klass(), "not a class");
122 Handle holder(current, klass->klass_holder()); // keep the klass alive
123 InstanceKlass* h = InstanceKlass::cast(klass);
124 {
125 RetryableAllocationMark ram(current);
126 h->check_valid_for_instantiation(true, CHECK);
127 if (!h->is_initialized()) {
128 // Cannot re-execute class initialization without side effects
129 // so return without attempting the initialization
130 current->set_vm_result_oop(nullptr);
131 return;
132 }
133 // allocate instance and return via TLS
134 oop obj = h->allocate_instance(CHECK);
135 current->set_vm_result_oop(obj);
136 }
137 JRT_BLOCK_END;
138 SharedRuntime::on_slowpath_allocation_exit(current);
139 JRT_END
140
141 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array_or_null(JavaThread* current, Klass* array_klass, jint length))
142 JRT_BLOCK;
143 // Note: no handle for klass needed since they are not used
144 // anymore after new_objArray() and no GC can happen before.
145 // (This may have to change if this code changes!)
146 assert(array_klass->is_klass(), "not a class");
147 oop obj;
148 if (array_klass->is_typeArray_klass()) {
149 BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type();
150 RetryableAllocationMark ram(current);
151 obj = oopFactory::new_typeArray(elt_type, length, CHECK);
152 } else {
153 Handle holder(current, array_klass->klass_holder()); // keep the klass alive
154 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
155 RetryableAllocationMark ram(current);
156 obj = oopFactory::new_objArray(elem_klass, length, CHECK);
157 }
158 // This is pretty rare but this runtime patch is stressful to deoptimization
159 // if we deoptimize here so force a deopt to stress the path.
160 if (DeoptimizeALot) {
161 static int deopts = 0;
162 if (deopts++ % 2 == 0) {
163 // Drop the allocation
164 obj = nullptr;
165 } else {
166 deopt_caller();
167 }
168 }
169 current->set_vm_result_oop(obj);
170 JRT_BLOCK_END;
171 SharedRuntime::on_slowpath_allocation_exit(current);
172 JRT_END
173
174 JRT_ENTRY(void, JVMCIRuntime::new_multi_array_or_null(JavaThread* current, Klass* klass, int rank, jint* dims))
175 assert(klass->is_klass(), "not a class");
176 assert(rank >= 1, "rank must be nonzero");
177 Handle holder(current, klass->klass_holder()); // keep the klass alive
178 RetryableAllocationMark ram(current);
179 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
180 current->set_vm_result_oop(obj);
181 JRT_END
182
183 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array_or_null(JavaThread* current, oopDesc* element_mirror, jint length))
184 RetryableAllocationMark ram(current);
185 oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK);
186 current->set_vm_result_oop(obj);
187 JRT_END
188
189 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance_or_null(JavaThread* current, oopDesc* type_mirror))
190 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror));
191
192 if (klass == nullptr) {
193 ResourceMark rm(current);
194 THROW(vmSymbols::java_lang_InstantiationException());
195 }
196 RetryableAllocationMark ram(current);
197
198 // Create new instance (the receiver)
199 klass->check_valid_for_instantiation(false, CHECK);
200
201 if (!klass->is_initialized()) {
202 // Cannot re-execute class initialization without side effects
203 // so return without attempting the initialization
204 current->set_vm_result_oop(nullptr);
205 return;
206 }
207
208 oop obj = klass->allocate_instance(CHECK);
209 current->set_vm_result_oop(obj);
210 JRT_END
211
212 extern void vm_exit(int code);
213
214 // Enter this method from compiled code handler below. This is where we transition
215 // to VM mode. This is done as a helper routine so that the method called directly
216 // from compiled code does not have to transition to VM. This allows the entry
217 // method to see if the nmethod that we have just looked up a handler for has
218 // been deoptimized while we were in the vm. This simplifies the assembly code
219 // cpu directories.
220 //
221 // We are entering here from exception stub (via the entry method below)
222 // If there is a compiled exception handler in this method, we will continue there;
223 // otherwise we will unwind the stack and continue at the caller of top frame method
224 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
225 // control the area where we can allow a safepoint. After we exit the safepoint area we can
226 // check to see if the handler we are going to return is now in a nmethod that has
227 // been deoptimized. If that is the case we return the deopt blob
228 // unpack_with_exception entry instead. This makes life for the exception blob easier
229 // because making that same check and diverting is painful from assembly language.
230 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
231 // Reset method handle flag.
232 current->set_is_method_handle_return(false);
233
234 Handle exception(current, ex);
235
236 // The frame we rethrow the exception to might not have been processed by the GC yet.
237 // The stack watermark barrier takes care of detecting that and ensuring the frame
238 // has updated oops.
239 StackWatermarkSet::after_unwind(current);
240
241 nm = CodeCache::find_nmethod(pc);
242 assert(nm != nullptr, "did not find nmethod");
243 // Adjust the pc as needed/
244 if (nm->is_deopt_pc(pc)) {
245 RegisterMap map(current,
246 RegisterMap::UpdateMap::skip,
247 RegisterMap::ProcessFrames::include,
248 RegisterMap::WalkContinuation::skip);
249 frame exception_frame = current->last_frame().sender(&map);
250 // if the frame isn't deopted then pc must not correspond to the caller of last_frame
251 assert(exception_frame.is_deoptimized_frame(), "must be deopted");
252 pc = exception_frame.pc();
253 }
254 assert(exception.not_null(), "null exceptions should be handled by throw_exception");
255 assert(oopDesc::is_oop(exception()), "just checking");
256 // Check that exception is a subclass of Throwable
257 assert(exception->is_a(vmClasses::Throwable_klass()),
258 "Exception not subclass of Throwable");
259
260 // debugging support
261 // tracing
262 if (log_is_enabled(Info, exceptions)) {
263 ResourceMark rm;
264 stringStream tempst;
265 assert(nm->method() != nullptr, "Unexpected null method()");
266 tempst.print("JVMCI compiled method <%s>\n"
267 " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT,
268 nm->method()->print_value_string(), p2i(pc), p2i(current));
269 Exceptions::log_exception(exception, tempst.as_string());
270 }
271 // for AbortVMOnException flag
272 Exceptions::debug_check_abort(exception);
273
274 // Check the stack guard pages and re-enable them if necessary and there is
275 // enough space on the stack to do so. Use fast exceptions only if the guard
276 // pages are enabled.
277 bool guard_pages_enabled = current->stack_overflow_state()->reguard_stack_if_needed();
278
279 if (JvmtiExport::can_post_on_exceptions()) {
280 // To ensure correct notification of exception catches and throws
281 // we have to deoptimize here. If we attempted to notify the
282 // catches and throws during this exception lookup it's possible
283 // we could deoptimize on the way out of the VM and end back in
284 // the interpreter at the throw site. This would result in double
285 // notifications since the interpreter would also notify about
286 // these same catches and throws as it unwound the frame.
287
288 RegisterMap reg_map(current,
289 RegisterMap::UpdateMap::include,
290 RegisterMap::ProcessFrames::include,
291 RegisterMap::WalkContinuation::skip);
292 frame stub_frame = current->last_frame();
293 frame caller_frame = stub_frame.sender(®_map);
294
295 // We don't really want to deoptimize the nmethod itself since we
296 // can actually continue in the exception handler ourselves but I
297 // don't see an easy way to have the desired effect.
298 Deoptimization::deoptimize_frame(current, caller_frame.id(), Deoptimization::Reason_constraint);
299 assert(caller_is_deopted(), "Must be deoptimized");
300
301 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
302 }
303
304 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
305 if (guard_pages_enabled) {
306 address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
307 if (fast_continuation != nullptr) {
308 // Set flag if return address is a method handle call site.
309 current->set_is_method_handle_return(nm->is_method_handle_return(pc));
310 return fast_continuation;
311 }
312 }
313
314 // If the stack guard pages are enabled, check whether there is a handler in
315 // the current method. Otherwise (guard pages disabled), force an unwind and
316 // skip the exception cache update (i.e., just leave continuation==nullptr).
317 address continuation = nullptr;
318 if (guard_pages_enabled) {
319
320 // New exception handling mechanism can support inlined methods
321 // with exception handlers since the mappings are from PC to PC
322
323 // Clear out the exception oop and pc since looking up an
324 // exception handler can cause class loading, which might throw an
325 // exception and those fields are expected to be clear during
326 // normal bytecode execution.
327 current->clear_exception_oop_and_pc();
328
329 bool recursive_exception = false;
330 continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false, recursive_exception);
331 // If an exception was thrown during exception dispatch, the exception oop may have changed
332 current->set_exception_oop(exception());
333 current->set_exception_pc(pc);
334
335 // The exception cache is used only for non-implicit exceptions
336 // Update the exception cache only when another exception did
337 // occur during the computation of the compiled exception handler
338 // (e.g., when loading the class of the catch type).
339 // Checking for exception oop equality is not
340 // sufficient because some exceptions are pre-allocated and reused.
341 if (continuation != nullptr && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) {
342 nm->add_handler_for_exception_and_pc(exception, pc, continuation);
343 }
344 }
345
346 // Set flag if return address is a method handle call site.
347 current->set_is_method_handle_return(nm->is_method_handle_return(pc));
348
349 if (log_is_enabled(Info, exceptions)) {
350 ResourceMark rm;
351 log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT
352 " for exception thrown at PC " PTR_FORMAT,
353 p2i(current), p2i(continuation), p2i(pc));
354 }
355
356 return continuation;
357 JRT_END
358
359 // Enter this method from compiled code only if there is a Java exception handler
360 // in the method handling the exception.
361 // We are entering here from exception stub. We don't do a normal VM transition here.
362 // We do it in a helper. This is so we can check to see if the nmethod we have just
363 // searched for an exception handler has been deoptimized in the meantime.
364 address JVMCIRuntime::exception_handler_for_pc(JavaThread* current) {
365 oop exception = current->exception_oop();
366 address pc = current->exception_pc();
367 // Still in Java mode
368 DEBUG_ONLY(NoHandleMark nhm);
369 nmethod* nm = nullptr;
370 address continuation = nullptr;
371 {
372 // Enter VM mode by calling the helper
373 ResetNoHandleMark rnhm;
374 continuation = exception_handler_for_pc_helper(current, exception, pc, nm);
375 }
376 // Back in JAVA, use no oops DON'T safepoint
377
378 // Now check to see if the compiled method we were called from is now deoptimized.
379 // If so we must return to the deopt blob and deoptimize the nmethod
380 if (nm != nullptr && caller_is_deopted()) {
381 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
382 }
383
384 assert(continuation != nullptr, "no handler found");
385 return continuation;
386 }
387
388 JRT_BLOCK_ENTRY(void, JVMCIRuntime::monitorenter(JavaThread* current, oopDesc* obj, BasicLock* lock))
389 SharedRuntime::monitor_enter_helper(obj, lock, current);
390 JRT_END
391
392 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* current, oopDesc* obj, BasicLock* lock))
393 assert(current == JavaThread::current(), "pre-condition");
394 assert(current->last_Java_sp(), "last_Java_sp must be set");
395 assert(oopDesc::is_oop(obj), "invalid lock object pointer dected");
396 SharedRuntime::monitor_exit_helper(obj, lock, current);
397 JRT_END
398
399 // Object.notify() fast path, caller does slow path
400 JRT_LEAF(jboolean, JVMCIRuntime::object_notify(JavaThread* current, oopDesc* obj))
401 assert(current == JavaThread::current(), "pre-condition");
402
403 // Very few notify/notifyAll operations find any threads on the waitset, so
404 // the dominant fast-path is to simply return.
405 // Relatedly, it's critical that notify/notifyAll be fast in order to
406 // reduce lock hold times.
407 if (!SafepointSynchronize::is_synchronizing()) {
408 if (ObjectSynchronizer::quick_notify(obj, current, false)) {
409 return true;
410 }
411 }
412 return false; // caller must perform slow path
413
414 JRT_END
415
416 // Object.notifyAll() fast path, caller does slow path
417 JRT_LEAF(jboolean, JVMCIRuntime::object_notifyAll(JavaThread* current, oopDesc* obj))
418 assert(current == JavaThread::current(), "pre-condition");
419
420 if (!SafepointSynchronize::is_synchronizing() ) {
421 if (ObjectSynchronizer::quick_notify(obj, current, true)) {
422 return true;
423 }
424 }
425 return false; // caller must perform slow path
426
427 JRT_END
428
429 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* current, const char* exception, const char* message))
430 JRT_BLOCK;
431 TempNewSymbol symbol = SymbolTable::new_symbol(exception);
432 SharedRuntime::throw_and_post_jvmti_exception(current, symbol, message);
433 JRT_BLOCK_END;
434 return caller_is_deopted();
435 JRT_END
436
437 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* current, const char* exception, Klass* klass))
438 JRT_BLOCK;
439 ResourceMark rm(current);
440 TempNewSymbol symbol = SymbolTable::new_symbol(exception);
441 SharedRuntime::throw_and_post_jvmti_exception(current, symbol, klass->external_name());
442 JRT_BLOCK_END;
443 return caller_is_deopted();
444 JRT_END
445
446 JRT_BLOCK_ENTRY(int, JVMCIRuntime::throw_class_cast_exception(JavaThread* current, const char* exception, Klass* caster_klass, Klass* target_klass))
447 JRT_BLOCK;
448 ResourceMark rm(current);
449 const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass);
450 TempNewSymbol symbol = SymbolTable::new_symbol(exception);
451 SharedRuntime::throw_and_post_jvmti_exception(current, symbol, message);
452 JRT_BLOCK_END;
453 return caller_is_deopted();
454 JRT_END
455
456 class ArgumentPusher : public SignatureIterator {
457 protected:
458 JavaCallArguments* _jca;
459 jlong _argument;
460 bool _pushed;
461
462 jlong next_arg() {
463 guarantee(!_pushed, "one argument");
464 _pushed = true;
465 return _argument;
466 }
467
468 float next_float() {
469 guarantee(!_pushed, "one argument");
470 _pushed = true;
471 jvalue v;
472 v.i = (jint) _argument;
473 return v.f;
474 }
475
476 double next_double() {
477 guarantee(!_pushed, "one argument");
478 _pushed = true;
479 jvalue v;
480 v.j = _argument;
481 return v.d;
482 }
483
484 Handle next_object() {
485 guarantee(!_pushed, "one argument");
486 _pushed = true;
487 return Handle(Thread::current(), cast_to_oop(_argument));
488 }
489
490 public:
491 ArgumentPusher(Symbol* signature, JavaCallArguments* jca, jlong argument) : SignatureIterator(signature) {
492 this->_return_type = T_ILLEGAL;
493 _jca = jca;
494 _argument = argument;
495 _pushed = false;
496 do_parameters_on(this);
497 }
498
499 void do_type(BasicType type) {
500 switch (type) {
501 case T_OBJECT:
502 case T_ARRAY: _jca->push_oop(next_object()); break;
503 case T_BOOLEAN: _jca->push_int((jboolean) next_arg()); break;
504 case T_CHAR: _jca->push_int((jchar) next_arg()); break;
505 case T_SHORT: _jca->push_int((jint) next_arg()); break;
506 case T_BYTE: _jca->push_int((jbyte) next_arg()); break;
507 case T_INT: _jca->push_int((jint) next_arg()); break;
508 case T_LONG: _jca->push_long((jlong) next_arg()); break;
509 case T_FLOAT: _jca->push_float(next_float()); break;
510 case T_DOUBLE: _jca->push_double(next_double()); break;
511 default: fatal("Unexpected type %s", type2name(type));
512 }
513 }
514 };
515
516
517 JRT_ENTRY(jlong, JVMCIRuntime::invoke_static_method_one_arg(JavaThread* current, Method* method, jlong argument))
518 ResourceMark rm;
519 HandleMark hm(current);
520
521 methodHandle mh(current, method);
522 if (mh->size_of_parameters() > 1 && !mh->is_static()) {
523 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Invoked method must be static and take at most one argument");
524 }
525
526 Symbol* signature = mh->signature();
527 JavaCallArguments jca(mh->size_of_parameters());
528 ArgumentPusher jap(signature, &jca, argument);
529 BasicType return_type = jap.return_type();
530 JavaValue result(return_type);
531 JavaCalls::call(&result, mh, &jca, CHECK_0);
532
533 if (return_type == T_VOID) {
534 return 0;
535 } else if (return_type == T_OBJECT || return_type == T_ARRAY) {
536 current->set_vm_result_oop(result.get_oop());
537 return 0;
538 } else {
539 jvalue *value = (jvalue *) result.get_value_addr();
540 // Narrow the value down if required (Important on big endian machines)
541 switch (return_type) {
542 case T_BOOLEAN:
543 return (jboolean) value->i;
544 case T_BYTE:
545 return (jbyte) value->i;
546 case T_CHAR:
547 return (jchar) value->i;
548 case T_SHORT:
549 return (jshort) value->i;
550 case T_INT:
551 case T_FLOAT:
552 return value->i;
553 case T_LONG:
554 case T_DOUBLE:
555 return value->j;
556 default:
557 fatal("Unexpected type %s", type2name(return_type));
558 return 0;
559 }
560 }
561 JRT_END
562
563 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline))
564 ttyLocker ttyl;
565
566 if (obj == nullptr) {
567 tty->print("null");
568 } else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) {
569 if (oopDesc::is_oop_or_null(obj, true)) {
570 char buf[O_BUFLEN];
571 tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj));
572 } else {
573 tty->print(INTPTR_FORMAT, p2i(obj));
574 }
575 } else {
576 ResourceMark rm;
577 assert(obj != nullptr && java_lang_String::is_instance(obj), "must be");
578 char *buf = java_lang_String::as_utf8_string(obj);
579 tty->print_raw(buf);
580 }
581 if (newline) {
582 tty->cr();
583 }
584 JRT_END
585
586 #if INCLUDE_G1GC
587
588 void JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj) {
589 G1BarrierSetRuntime::write_ref_field_pre_entry(obj, thread);
590 }
591
592 void JVMCIRuntime::write_barrier_post(JavaThread* thread, volatile CardValue* card_addr) {
593 G1BarrierSetRuntime::write_ref_field_post_entry(card_addr, thread);
594 }
595
596 #endif // INCLUDE_G1GC
597
598 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child))
599 bool ret = true;
600 if(!Universe::heap()->is_in(parent)) {
601 tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent));
602 parent->print();
603 ret=false;
604 }
605 if(!Universe::heap()->is_in(child)) {
606 tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child));
607 child->print();
608 ret=false;
609 }
610 return (jint)ret;
611 JRT_END
612
613 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* current, jlong where, jlong format, jlong value))
614 ResourceMark rm(current);
615 const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where;
616 char *detail_msg = nullptr;
617 if (format != 0L) {
618 const char* buf = (char*) (address) format;
619 size_t detail_msg_length = strlen(buf) * 2;
620 detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length);
621 jio_snprintf(detail_msg, detail_msg_length, buf, value);
622 }
623 report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg);
624 JRT_END
625
626 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread))
627 oop exception = thread->exception_oop();
628 assert(exception != nullptr, "npe");
629 thread->set_exception_oop(nullptr);
630 thread->set_exception_pc(nullptr);
631 return exception;
632 JRT_END
633
634 PRAGMA_DIAG_PUSH
635 PRAGMA_FORMAT_NONLITERAL_IGNORED
636 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3))
637 ResourceMark rm;
638 tty->print(format, v1, v2, v3);
639 JRT_END
640 PRAGMA_DIAG_POP
641
642 static void decipher(jlong v, bool ignoreZero) {
643 if (v != 0 || !ignoreZero) {
644 void* p = (void *)(address) v;
645 CodeBlob* cb = CodeCache::find_blob(p);
646 if (cb) {
647 if (cb->is_nmethod()) {
648 char buf[O_BUFLEN];
649 tty->print("%s [" INTPTR_FORMAT "+" JLONG_FORMAT "]", cb->as_nmethod()->method()->name_and_sig_as_C_string(buf, O_BUFLEN), p2i(cb->code_begin()), (jlong)((address)v - cb->code_begin()));
650 return;
651 }
652 cb->print_value_on(tty);
653 return;
654 }
655 if (Universe::heap()->is_in(p)) {
656 oop obj = cast_to_oop(p);
657 obj->print_value_on(tty);
658 return;
659 }
660 tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v);
661 }
662 }
663
664 PRAGMA_DIAG_PUSH
665 PRAGMA_FORMAT_NONLITERAL_IGNORED
666 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3))
667 ResourceMark rm;
668 const char *buf = (const char*) (address) format;
669 if (vmError) {
670 if (buf != nullptr) {
671 fatal(buf, v1, v2, v3);
672 } else {
673 fatal("<anonymous error>");
674 }
675 } else if (buf != nullptr) {
676 tty->print(buf, v1, v2, v3);
677 } else {
678 assert(v2 == 0, "v2 != 0");
679 assert(v3 == 0, "v3 != 0");
680 decipher(v1, false);
681 }
682 JRT_END
683 PRAGMA_DIAG_POP
684
685 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline))
686 union {
687 jlong l;
688 jdouble d;
689 jfloat f;
690 } uu;
691 uu.l = value;
692 switch (typeChar) {
693 case 'Z': tty->print(value == 0 ? "false" : "true"); break;
694 case 'B': tty->print("%d", (jbyte) value); break;
695 case 'C': tty->print("%c", (jchar) value); break;
696 case 'S': tty->print("%d", (jshort) value); break;
697 case 'I': tty->print("%d", (jint) value); break;
698 case 'F': tty->print("%f", uu.f); break;
699 case 'J': tty->print(JLONG_FORMAT, value); break;
700 case 'D': tty->print("%lf", uu.d); break;
701 default: assert(false, "unknown typeChar"); break;
702 }
703 if (newline) {
704 tty->cr();
705 }
706 JRT_END
707
708 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* current, oopDesc* obj))
709 return (jint) obj->identity_hash();
710 JRT_END
711
712 JRT_ENTRY(jint, JVMCIRuntime::test_deoptimize_call_int(JavaThread* current, int value))
713 deopt_caller();
714 return (jint) value;
715 JRT_END
716
717
718 // Implementation of JVMCI.initializeRuntime()
719 // When called from libjvmci, `libjvmciOrHotspotEnv` is a libjvmci env so use JVM_ENTRY_NO_ENV.
720 JVM_ENTRY_NO_ENV(jobject, JVM_GetJVMCIRuntime(JNIEnv *libjvmciOrHotspotEnv, jclass c))
721 JVMCIENV_FROM_JNI(thread, libjvmciOrHotspotEnv);
722 if (!EnableJVMCI) {
723 JVMCI_THROW_MSG_NULL(InternalError, JVMCI_NOT_ENABLED_ERROR_MESSAGE);
724 }
725 JVMCIENV->runtime()->initialize_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
726 JVMCIObject runtime = JVMCIENV->runtime()->get_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL);
727 return JVMCIENV->get_jobject(runtime);
728 JVM_END
729
730 // Implementation of Services.readSystemPropertiesInfo(int[] offsets)
731 // When called from libjvmci, `env` is a libjvmci env so use JVM_ENTRY_NO_ENV.
732 JVM_ENTRY_NO_ENV(jlong, JVM_ReadSystemPropertiesInfo(JNIEnv *env, jclass c, jintArray offsets_handle))
733 JVMCIENV_FROM_JNI(thread, env);
734 if (!EnableJVMCI) {
735 JVMCI_THROW_MSG_0(InternalError, JVMCI_NOT_ENABLED_ERROR_MESSAGE);
736 }
737 JVMCIPrimitiveArray offsets = JVMCIENV->wrap(offsets_handle);
738 JVMCIENV->put_int_at(offsets, 0, SystemProperty::next_offset_in_bytes());
739 JVMCIENV->put_int_at(offsets, 1, SystemProperty::key_offset_in_bytes());
740 JVMCIENV->put_int_at(offsets, 2, PathString::value_offset_in_bytes());
741
742 return (jlong) Arguments::system_properties();
743 JVM_END
744
745
746 void JVMCINMethodData::initialize(int nmethod_mirror_index,
747 int nmethod_entry_patch_offset,
748 const char* nmethod_mirror_name,
749 FailedSpeculation** failed_speculations)
750 {
751 _failed_speculations = failed_speculations;
752 _nmethod_mirror_index = nmethod_mirror_index;
753 guarantee(nmethod_entry_patch_offset != -1, "missing entry barrier");
754 _nmethod_entry_patch_offset = nmethod_entry_patch_offset;
755 if (nmethod_mirror_name != nullptr) {
756 _has_name = true;
757 char* dest = (char*) name();
758 strcpy(dest, nmethod_mirror_name);
759 } else {
760 _has_name = false;
761 }
762 }
763
764 void JVMCINMethodData::copy(JVMCINMethodData* data) {
765 initialize(data->_nmethod_mirror_index, data->_nmethod_entry_patch_offset, data->name(), data->_failed_speculations);
766 }
767
768 void JVMCINMethodData::add_failed_speculation(nmethod* nm, jlong speculation) {
769 jlong index = speculation >> JVMCINMethodData::SPECULATION_LENGTH_BITS;
770 guarantee(index >= 0 && index <= max_jint, "Encoded JVMCI speculation index is not a positive Java int: " INTPTR_FORMAT, index);
771 int length = speculation & JVMCINMethodData::SPECULATION_LENGTH_MASK;
772 if (index + length > (uint) nm->speculations_size()) {
773 fatal(INTPTR_FORMAT "[index: " JLONG_FORMAT ", length: %d out of bounds wrt encoded speculations of length %u", speculation, index, length, nm->speculations_size());
774 }
775 address data = nm->speculations_begin() + index;
776 FailedSpeculation::add_failed_speculation(nm, _failed_speculations, data, length);
777 }
778
779 oop JVMCINMethodData::get_nmethod_mirror(nmethod* nm) {
780 if (_nmethod_mirror_index == -1) {
781 return nullptr;
782 }
783 return nm->oop_at(_nmethod_mirror_index);
784 }
785
786 void JVMCINMethodData::set_nmethod_mirror(nmethod* nm, oop new_mirror) {
787 guarantee(_nmethod_mirror_index != -1, "cannot set JVMCI mirror for nmethod");
788 oop* addr = nm->oop_addr_at(_nmethod_mirror_index);
789 guarantee(new_mirror != nullptr, "use clear_nmethod_mirror to clear the mirror");
790 guarantee(*addr == nullptr, "cannot overwrite non-null mirror");
791
792 *addr = new_mirror;
793
794 // Since we've patched some oops in the nmethod,
795 // (re)register it with the heap.
796 MutexLocker ml(CodeCache_lock, Mutex::_no_safepoint_check_flag);
797 Universe::heap()->register_nmethod(nm);
798 }
799
800 void JVMCINMethodData::invalidate_nmethod_mirror(nmethod* nm, nmethod::InvalidationReason invalidation_reason) {
801 oop nmethod_mirror = get_nmethod_mirror(nm);
802 if (nmethod_mirror == nullptr) {
803 return;
804 }
805
806 // Update the values in the mirror if it still refers to nm.
807 // We cannot use JVMCIObject to wrap the mirror as this is called
808 // during GC, forbidding the creation of JNIHandles.
809 JVMCIEnv* jvmciEnv = nullptr;
810 nmethod* current = (nmethod*) HotSpotJVMCI::InstalledCode::address(jvmciEnv, nmethod_mirror);
811 if (nm == current) {
812 if (nm->is_unloading()) {
813 // Break the link from the mirror to nm such that
814 // future invocations via the mirror will result in
815 // an InvalidInstalledCodeException.
816 HotSpotJVMCI::InstalledCode::set_address(jvmciEnv, nmethod_mirror, 0);
817 HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
818 HotSpotJVMCI::HotSpotInstalledCode::set_codeStart(jvmciEnv, nmethod_mirror, 0);
819 if (HotSpotJVMCI::HotSpotNmethod::invalidationReason(jvmciEnv, nmethod_mirror) ==
820 static_cast<int>(nmethod::InvalidationReason::NOT_INVALIDATED)) {
821 HotSpotJVMCI::HotSpotNmethod::set_invalidationReason(jvmciEnv, nmethod_mirror, static_cast<int>(invalidation_reason));
822 }
823 } else if (nm->is_not_entrant()) {
824 // Zero the entry point so any new invocation will fail but keep
825 // the address link around that so that existing activations can
826 // be deoptimized via the mirror (i.e. JVMCIEnv::invalidate_installed_code).
827 HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0);
828 HotSpotJVMCI::HotSpotInstalledCode::set_codeStart(jvmciEnv, nmethod_mirror, 0);
829 if (HotSpotJVMCI::HotSpotNmethod::invalidationReason(jvmciEnv, nmethod_mirror) ==
830 static_cast<int>(nmethod::InvalidationReason::NOT_INVALIDATED)) {
831 HotSpotJVMCI::HotSpotNmethod::set_invalidationReason(jvmciEnv, nmethod_mirror, static_cast<int>(invalidation_reason));
832 }
833 }
834 }
835
836 if (_nmethod_mirror_index != -1 && nm->is_unloading()) {
837 // Drop the reference to the nmethod mirror object but don't clear the actual oop reference. Otherwise
838 // it would appear that the nmethod didn't need to be unloaded in the first place.
839 _nmethod_mirror_index = -1;
840 }
841 }
842
843 // Handles to objects in the Hotspot heap.
844 static OopStorage* object_handles() {
845 return Universe::vm_global();
846 }
847
848 jlong JVMCIRuntime::make_oop_handle(const Handle& obj) {
849 assert(!Universe::heap()->is_stw_gc_active(), "can't extend the root set during GC pause");
850 assert(oopDesc::is_oop(obj()), "not an oop");
851
852 oop* ptr = OopHandle(object_handles(), obj()).ptr_raw();
853 MutexLocker ml(_lock);
854 _oop_handles.append(ptr);
855 return reinterpret_cast<jlong>(ptr);
856 }
857
858 #ifdef ASSERT
859 bool JVMCIRuntime::is_oop_handle(jlong handle) {
860 const oop* ptr = (oop*) handle;
861 return object_handles()->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
862 }
863 #endif
864
865 int JVMCIRuntime::release_and_clear_oop_handles() {
866 guarantee(_num_attached_threads == cannot_be_attached, "only call during JVMCI runtime shutdown");
867 int released = release_cleared_oop_handles();
868 if (_oop_handles.length() != 0) {
869 for (int i = 0; i < _oop_handles.length(); i++) {
870 oop* oop_ptr = _oop_handles.at(i);
871 guarantee(oop_ptr != nullptr, "release_cleared_oop_handles left null entry in _oop_handles");
872 guarantee(NativeAccess<>::oop_load(oop_ptr) != nullptr, "unexpected cleared handle");
873 // Satisfy OopHandles::release precondition that all
874 // handles being released are null.
875 NativeAccess<>::oop_store(oop_ptr, (oop) nullptr);
876 }
877
878 // Do the bulk release
879 object_handles()->release(_oop_handles.adr_at(0), _oop_handles.length());
880 released += _oop_handles.length();
881 }
882 _oop_handles.clear();
883 return released;
884 }
885
886 static bool is_referent_non_null(oop* handle) {
887 return handle != nullptr && NativeAccess<>::oop_load(handle) != nullptr;
888 }
889
890 // Swaps the elements in `array` at index `a` and index `b`
891 static void swap(GrowableArray<oop*>* array, int a, int b) {
892 oop* tmp = array->at(a);
893 array->at_put(a, array->at(b));
894 array->at_put(b, tmp);
895 }
896
897 int JVMCIRuntime::release_cleared_oop_handles() {
898 // Despite this lock, it's possible for another thread
899 // to clear a handle's referent concurrently (e.g., a thread
900 // executing IndirectHotSpotObjectConstantImpl.clear()).
901 // This is benign - it means there can still be cleared
902 // handles in _oop_handles when this method returns.
903 MutexLocker ml(_lock);
904
905 int next = 0;
906 if (_oop_handles.length() != 0) {
907 // Key for _oop_handles contents in example below:
908 // H: handle with non-null referent
909 // h: handle with clear (i.e., null) referent
910 // -: null entry
911
912 // Shuffle all handles with non-null referents to the front of the list
913 // Example: Before: 0HHh-Hh-
914 // After: HHHh--h-
915 for (int i = 0; i < _oop_handles.length(); i++) {
916 oop* handle = _oop_handles.at(i);
917 if (is_referent_non_null(handle)) {
918 if (i != next && !is_referent_non_null(_oop_handles.at(next))) {
919 // Swap elements at index `next` and `i`
920 swap(&_oop_handles, next, i);
921 }
922 next++;
923 }
924 }
925
926 // `next` is now the index of the first null handle or handle with a null referent
927 int num_alive = next;
928
929 // Shuffle all null handles to the end of the list
930 // Example: Before: HHHh--h-
931 // After: HHHhh---
932 // num_alive: 3
933 for (int i = next; i < _oop_handles.length(); i++) {
934 oop* handle = _oop_handles.at(i);
935 if (handle != nullptr) {
936 if (i != next && _oop_handles.at(next) == nullptr) {
937 // Swap elements at index `next` and `i`
938 swap(&_oop_handles, next, i);
939 }
940 next++;
941 }
942 }
943 if (next != num_alive) {
944 int to_release = next - num_alive;
945
946 // `next` is now the index of the first null handle
947 // Example: to_release: 2
948
949 // Bulk release the handles with a null referent
950 object_handles()->release(_oop_handles.adr_at(num_alive), to_release);
951
952 // Truncate oop handles to only those with a non-null referent
953 JVMCI_event_2("compacted oop handles in JVMCI runtime %d from %d to %d", _id, _oop_handles.length(), num_alive);
954 _oop_handles.trunc_to(num_alive);
955 // Example: HHH
956
957 return to_release;
958 }
959 }
960 return 0;
961 }
962
963 jmetadata JVMCIRuntime::allocate_handle(const methodHandle& handle) {
964 MutexLocker ml(_lock);
965 return _metadata_handles->allocate_handle(handle);
966 }
967
968 jmetadata JVMCIRuntime::allocate_handle(const constantPoolHandle& handle) {
969 MutexLocker ml(_lock);
970 return _metadata_handles->allocate_handle(handle);
971 }
972
973 void JVMCIRuntime::release_handle(jmetadata handle) {
974 MutexLocker ml(_lock);
975 _metadata_handles->chain_free_list(handle);
976 }
977
978 // Function for redirecting shared library JavaVM output to tty
979 static void _log(const char* buf, size_t count) {
980 tty->write((char*) buf, count);
981 }
982
983 // Function for redirecting shared library JavaVM fatal error data to a log file.
984 // The log file is opened on first call to this function.
985 static void _fatal_log(const char* buf, size_t count) {
986 JVMCI::fatal_log(buf, count);
987 }
988
989 // Function for shared library JavaVM to flush tty
990 static void _flush_log() {
991 tty->flush();
992 }
993
994 // Function for shared library JavaVM to exit HotSpot on a fatal error
995 static void _fatal() {
996 Thread* thread = Thread::current_or_null_safe();
997 if (thread != nullptr && thread->is_Java_thread()) {
998 JavaThread* jthread = JavaThread::cast(thread);
999 JVMCIRuntime* runtime = jthread->libjvmci_runtime();
1000 if (runtime != nullptr) {
1001 int javaVM_id = runtime->get_shared_library_javavm_id();
1002 fatal("Fatal error in JVMCI shared library JavaVM[%d] owned by JVMCI runtime %d", javaVM_id, runtime->id());
1003 }
1004 }
1005 intx current_thread_id = os::current_thread_id();
1006 fatal("thread %zd: Fatal error in JVMCI shared library", current_thread_id);
1007 }
1008
1009 JVMCIRuntime::JVMCIRuntime(JVMCIRuntime* next, int id, bool for_compile_broker) :
1010 _init_state(uninitialized),
1011 _shared_library_javavm(nullptr),
1012 _shared_library_javavm_id(0),
1013 _id(id),
1014 _next(next),
1015 _metadata_handles(new MetadataHandles()),
1016 _oop_handles(100, mtJVMCI),
1017 _num_attached_threads(0),
1018 _for_compile_broker(for_compile_broker)
1019 {
1020 if (id == -1) {
1021 _lock = JVMCIRuntime_lock;
1022 } else {
1023 stringStream lock_name;
1024 lock_name.print("%s@%d", JVMCIRuntime_lock->name(), id);
1025 Mutex::Rank lock_rank = DEBUG_ONLY(JVMCIRuntime_lock->rank()) NOT_DEBUG(Mutex::safepoint);
1026 _lock = new PaddedMonitor(lock_rank, lock_name.as_string(/*c_heap*/true));
1027 }
1028 JVMCI_event_1("created new %s JVMCI runtime %d (" PTR_FORMAT ")",
1029 id == -1 ? "Java" : for_compile_broker ? "CompileBroker" : "Compiler", id, p2i(this));
1030 }
1031
1032 JVMCIRuntime* JVMCIRuntime::select_runtime_in_shutdown(JavaThread* thread) {
1033 assert(JVMCI_lock->owner() == thread, "must be");
1034 // When shutting down, use the first available runtime.
1035 for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1036 if (runtime->_num_attached_threads != cannot_be_attached) {
1037 runtime->pre_attach_thread(thread);
1038 JVMCI_event_1("using pre-existing JVMCI runtime %d in shutdown", runtime->id());
1039 return runtime;
1040 }
1041 }
1042 // Lazily initialize JVMCI::_shutdown_compiler_runtime. Safe to
1043 // do here since JVMCI_lock is locked.
1044 if (JVMCI::_shutdown_compiler_runtime == nullptr) {
1045 JVMCI::_shutdown_compiler_runtime = new JVMCIRuntime(nullptr, -2, true);
1046 }
1047 JVMCIRuntime* runtime = JVMCI::_shutdown_compiler_runtime;
1048 JVMCI_event_1("using reserved shutdown JVMCI runtime %d", runtime->id());
1049 return runtime;
1050 }
1051
1052 JVMCIRuntime* JVMCIRuntime::select_runtime(JavaThread* thread, JVMCIRuntime* skip, int* count) {
1053 assert(JVMCI_lock->owner() == thread, "must be");
1054 bool for_compile_broker = thread->is_Compiler_thread();
1055 for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1056 if (count != nullptr) {
1057 (*count)++;
1058 }
1059 if (for_compile_broker == runtime->_for_compile_broker) {
1060 int count = runtime->_num_attached_threads;
1061 if (count == cannot_be_attached || runtime == skip) {
1062 // Cannot attach to rt
1063 continue;
1064 }
1065 // If selecting for repacking, ignore a runtime without an existing JavaVM
1066 if (skip != nullptr && !runtime->has_shared_library_javavm()) {
1067 continue;
1068 }
1069
1070 // Select first runtime with sufficient capacity
1071 if (count < (int) JVMCIThreadsPerNativeLibraryRuntime) {
1072 runtime->pre_attach_thread(thread);
1073 return runtime;
1074 }
1075 }
1076 }
1077 return nullptr;
1078 }
1079
1080 JVMCIRuntime* JVMCIRuntime::select_or_create_runtime(JavaThread* thread) {
1081 assert(JVMCI_lock->owner() == thread, "must be");
1082 int id = 0;
1083 JVMCIRuntime* runtime;
1084 if (JVMCI::using_singleton_shared_library_runtime()) {
1085 runtime = JVMCI::_compiler_runtimes;
1086 guarantee(runtime != nullptr, "must be");
1087 while (runtime->_num_attached_threads == cannot_be_attached) {
1088 // Since there is only a singleton JVMCIRuntime, we
1089 // need to wait for it to be available for attaching.
1090 JVMCI_lock->wait();
1091 }
1092 runtime->pre_attach_thread(thread);
1093 } else {
1094 runtime = select_runtime(thread, nullptr, &id);
1095 }
1096 if (runtime == nullptr) {
1097 runtime = new JVMCIRuntime(JVMCI::_compiler_runtimes, id, thread->is_Compiler_thread());
1098 JVMCI::_compiler_runtimes = runtime;
1099 runtime->pre_attach_thread(thread);
1100 }
1101 return runtime;
1102 }
1103
1104 JVMCIRuntime* JVMCIRuntime::for_thread(JavaThread* thread) {
1105 assert(thread->libjvmci_runtime() == nullptr, "must be");
1106 // Find the runtime with fewest attached threads
1107 JVMCIRuntime* runtime = nullptr;
1108 {
1109 MutexLocker locker(JVMCI_lock);
1110 runtime = JVMCI::in_shutdown() ? select_runtime_in_shutdown(thread) : select_or_create_runtime(thread);
1111 }
1112 runtime->attach_thread(thread);
1113 return runtime;
1114 }
1115
1116 const char* JVMCIRuntime::attach_shared_library_thread(JavaThread* thread, JavaVM* javaVM) {
1117 MutexLocker locker(JVMCI_lock);
1118 for (JVMCIRuntime* runtime = JVMCI::_compiler_runtimes; runtime != nullptr; runtime = runtime->_next) {
1119 if (runtime->_shared_library_javavm == javaVM) {
1120 if (runtime->_num_attached_threads == cannot_be_attached) {
1121 return "Cannot attach to JVMCI runtime that is shutting down";
1122 }
1123 runtime->pre_attach_thread(thread);
1124 runtime->attach_thread(thread);
1125 return nullptr;
1126 }
1127 }
1128 return "Cannot find JVMCI runtime";
1129 }
1130
1131 void JVMCIRuntime::pre_attach_thread(JavaThread* thread) {
1132 assert(JVMCI_lock->owner() == thread, "must be");
1133 _num_attached_threads++;
1134 }
1135
1136 void JVMCIRuntime::attach_thread(JavaThread* thread) {
1137 assert(thread->libjvmci_runtime() == nullptr, "must be");
1138 thread->set_libjvmci_runtime(this);
1139 guarantee(this == JVMCI::_shutdown_compiler_runtime ||
1140 _num_attached_threads > 0,
1141 "missing reservation in JVMCI runtime %d: _num_attached_threads=%d", _id, _num_attached_threads);
1142 JVMCI_event_1("attached to JVMCI runtime %d%s", _id, JVMCI::in_shutdown() ? " [in JVMCI shutdown]" : "");
1143 }
1144
1145 void JVMCIRuntime::repack(JavaThread* thread) {
1146 JVMCIRuntime* new_runtime = nullptr;
1147 {
1148 MutexLocker locker(JVMCI_lock);
1149 if (JVMCI::using_singleton_shared_library_runtime() || _num_attached_threads != 1 || JVMCI::in_shutdown()) {
1150 return;
1151 }
1152 new_runtime = select_runtime(thread, this, nullptr);
1153 }
1154 if (new_runtime != nullptr) {
1155 JVMCI_event_1("Moving thread from JVMCI runtime %d to JVMCI runtime %d (%d attached)", _id, new_runtime->_id, new_runtime->_num_attached_threads - 1);
1156 detach_thread(thread, "moving thread to another JVMCI runtime");
1157 new_runtime->attach_thread(thread);
1158 }
1159 }
1160
1161 bool JVMCIRuntime::detach_thread(JavaThread* thread, const char* reason, bool can_destroy_javavm) {
1162 if (this == JVMCI::_shutdown_compiler_runtime || JVMCI::in_shutdown()) {
1163 // Do minimal work when shutting down JVMCI
1164 thread->set_libjvmci_runtime(nullptr);
1165 return false;
1166 }
1167 bool should_shutdown;
1168 bool destroyed_javavm = false;
1169 {
1170 MutexLocker locker(JVMCI_lock);
1171 _num_attached_threads--;
1172 JVMCI_event_1("detaching from JVMCI runtime %d: %s (%d other threads still attached)", _id, reason, _num_attached_threads);
1173 should_shutdown = _num_attached_threads == 0 && !JVMCI::in_shutdown();
1174 if (should_shutdown && !can_destroy_javavm) {
1175 // If it's not possible to destroy the JavaVM on this thread then the VM must
1176 // not be shutdown. This can happen when a shared library thread is the last
1177 // thread to detach from a shared library JavaVM (e.g. GraalServiceThread).
1178 JVMCI_event_1("Cancelled shut down of JVMCI runtime %d", _id);
1179 should_shutdown = false;
1180 }
1181 if (should_shutdown) {
1182 // Prevent other threads from attaching to this runtime
1183 // while it is shutting down and destroying its JavaVM
1184 _num_attached_threads = cannot_be_attached;
1185 }
1186 }
1187 if (should_shutdown) {
1188 // Release the JavaVM resources associated with this
1189 // runtime once there are no threads attached to it.
1190 shutdown();
1191 if (can_destroy_javavm) {
1192 destroyed_javavm = destroy_shared_library_javavm();
1193 if (destroyed_javavm) {
1194 // Can release all handles now that there's no code executing
1195 // that could be using them. Handles for the Java JVMCI runtime
1196 // are never released as we cannot guarantee all compiler threads
1197 // using it have been stopped.
1198 int released = release_and_clear_oop_handles();
1199 JVMCI_event_1("releasing handles for JVMCI runtime %d: oop handles=%d, metadata handles={total=%d, live=%d, blocks=%d}",
1200 _id,
1201 released,
1202 _metadata_handles->num_handles(),
1203 _metadata_handles->num_handles() - _metadata_handles->num_free_handles(),
1204 _metadata_handles->num_blocks());
1205
1206 // No need to acquire _lock since this is the only thread accessing this runtime
1207 _metadata_handles->clear();
1208 }
1209 }
1210 // Allow other threads to attach to this runtime now
1211 MutexLocker locker(JVMCI_lock);
1212 _num_attached_threads = 0;
1213 if (JVMCI::using_singleton_shared_library_runtime()) {
1214 // Notify any thread waiting to attach to the
1215 // singleton JVMCIRuntime
1216 JVMCI_lock->notify();
1217 }
1218 }
1219 thread->set_libjvmci_runtime(nullptr);
1220 JVMCI_event_1("detached from JVMCI runtime %d", _id);
1221 return destroyed_javavm;
1222 }
1223
1224 JNIEnv* JVMCIRuntime::init_shared_library_javavm(int* create_JavaVM_err, const char** err_msg) {
1225 MutexLocker locker(_lock);
1226 JavaVM* javaVM = _shared_library_javavm;
1227 if (javaVM == nullptr) {
1228 #ifdef ASSERT
1229 const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceEnomemOnLibjvmciInit");
1230 if (val != nullptr && strcmp(val, "true") == 0) {
1231 *create_JavaVM_err = JNI_ENOMEM;
1232 return nullptr;
1233 }
1234 #endif
1235
1236 char* sl_path;
1237 void* sl_handle = JVMCI::get_shared_library(sl_path, true);
1238
1239 jint (*JNI_CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
1240 typedef jint (*JNI_CreateJavaVM_t)(JavaVM **pvm, void **penv, void *args);
1241
1242 JNI_CreateJavaVM = CAST_TO_FN_PTR(JNI_CreateJavaVM_t, os::dll_lookup(sl_handle, "JNI_CreateJavaVM"));
1243 if (JNI_CreateJavaVM == nullptr) {
1244 fatal("Unable to find JNI_CreateJavaVM in %s", sl_path);
1245 }
1246
1247 ResourceMark rm;
1248 JavaVMInitArgs vm_args;
1249 vm_args.version = JNI_VERSION_1_2;
1250 vm_args.ignoreUnrecognized = JNI_TRUE;
1251 JavaVMOption options[6];
1252 jlong javaVM_id = 0;
1253
1254 // Protocol: JVMCI shared library JavaVM should support a non-standard "_javavm_id"
1255 // option whose extraInfo info field is a pointer to which a unique id for the
1256 // JavaVM should be written.
1257 options[0].optionString = (char*) "_javavm_id";
1258 options[0].extraInfo = &javaVM_id;
1259
1260 options[1].optionString = (char*) "_log";
1261 options[1].extraInfo = (void*) _log;
1262 options[2].optionString = (char*) "_flush_log";
1263 options[2].extraInfo = (void*) _flush_log;
1264 options[3].optionString = (char*) "_fatal";
1265 options[3].extraInfo = (void*) _fatal;
1266 options[4].optionString = (char*) "_fatal_log";
1267 options[4].extraInfo = (void*) _fatal_log;
1268 options[5].optionString = (char*) "_createvm_errorstr";
1269 options[5].extraInfo = (void*) err_msg;
1270
1271 vm_args.version = JNI_VERSION_1_2;
1272 vm_args.options = options;
1273 vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
1274
1275 JNIEnv* env = nullptr;
1276 int result = (*JNI_CreateJavaVM)(&javaVM, (void**) &env, &vm_args);
1277 if (result == JNI_OK) {
1278 guarantee(env != nullptr, "missing env");
1279 _shared_library_javavm_id = javaVM_id;
1280 _shared_library_javavm = javaVM;
1281 JVMCI_event_1("created JavaVM[%ld]@" PTR_FORMAT " for JVMCI runtime %d", javaVM_id, p2i(javaVM), _id);
1282 return env;
1283 } else {
1284 *create_JavaVM_err = result;
1285 }
1286 }
1287 return nullptr;
1288 }
1289
1290 void JVMCIRuntime::init_JavaVM_info(jlongArray info, JVMCI_TRAPS) {
1291 if (info != nullptr) {
1292 typeArrayOop info_oop = (typeArrayOop) JNIHandles::resolve(info);
1293 if (info_oop->length() < 4) {
1294 JVMCI_THROW_MSG(ArrayIndexOutOfBoundsException, err_msg("%d < 4", info_oop->length()));
1295 }
1296 JavaVM* javaVM = _shared_library_javavm;
1297 info_oop->long_at_put(0, (jlong) (address) javaVM);
1298 info_oop->long_at_put(1, (jlong) (address) javaVM->functions->reserved0);
1299 info_oop->long_at_put(2, (jlong) (address) javaVM->functions->reserved1);
1300 info_oop->long_at_put(3, (jlong) (address) javaVM->functions->reserved2);
1301 }
1302 }
1303
1304 #define JAVAVM_CALL_BLOCK \
1305 guarantee(thread != nullptr && _shared_library_javavm != nullptr, "npe"); \
1306 ThreadToNativeFromVM ttnfv(thread); \
1307 JavaVM* javavm = _shared_library_javavm;
1308
1309 jint JVMCIRuntime::AttachCurrentThread(JavaThread* thread, void **penv, void *args) {
1310 JAVAVM_CALL_BLOCK
1311 return javavm->AttachCurrentThread(penv, args);
1312 }
1313
1314 jint JVMCIRuntime::AttachCurrentThreadAsDaemon(JavaThread* thread, void **penv, void *args) {
1315 JAVAVM_CALL_BLOCK
1316 return javavm->AttachCurrentThreadAsDaemon(penv, args);
1317 }
1318
1319 jint JVMCIRuntime::DetachCurrentThread(JavaThread* thread) {
1320 JAVAVM_CALL_BLOCK
1321 return javavm->DetachCurrentThread();
1322 }
1323
1324 jint JVMCIRuntime::GetEnv(JavaThread* thread, void **penv, jint version) {
1325 JAVAVM_CALL_BLOCK
1326 return javavm->GetEnv(penv, version);
1327 }
1328 #undef JAVAVM_CALL_BLOCK \
1329
1330 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
1331 if (is_HotSpotJVMCIRuntime_initialized()) {
1332 if (JVMCIENV->is_hotspot() && UseJVMCINativeLibrary) {
1333 JVMCI_THROW_MSG(InternalError, "JVMCI has already been enabled in the JVMCI shared library");
1334 }
1335 }
1336
1337 initialize(JVMCI_CHECK);
1338
1339 // This should only be called in the context of the JVMCI class being initialized
1340 JVMCIObject result = JVMCIENV->call_HotSpotJVMCIRuntime_runtime(JVMCI_CHECK);
1341 result = JVMCIENV->make_global(result);
1342
1343 OrderAccess::storestore(); // Ensure handle is fully constructed before publishing
1344 _HotSpotJVMCIRuntime_instance = result;
1345
1346 JVMCI::_is_initialized = true;
1347 }
1348
1349 JVMCIRuntime::InitState JVMCIRuntime::_shared_library_javavm_refs_init_state = JVMCIRuntime::uninitialized;
1350 JVMCIRuntime::InitState JVMCIRuntime::_hotspot_javavm_refs_init_state = JVMCIRuntime::uninitialized;
1351
1352 class JavaVMRefsInitialization: public StackObj {
1353 JVMCIRuntime::InitState *_state;
1354 int _id;
1355 public:
1356 JavaVMRefsInitialization(JVMCIRuntime::InitState *state, int id) {
1357 _state = state;
1358 _id = id;
1359 // All classes, methods and fields in the JVMCI shared library
1360 // are in the read-only part of the image. As such, these
1361 // values (and any global handle derived from them via NewGlobalRef)
1362 // are the same for all JavaVM instances created in the
1363 // shared library which means they only need to be initialized
1364 // once. In non-product mode, we check this invariant.
1365 // See com.oracle.svm.jni.JNIImageHeapHandles.
1366 // The same is true for Klass* and field offsets in HotSpotJVMCI.
1367 if (*state == JVMCIRuntime::uninitialized DEBUG_ONLY( || true)) {
1368 *state = JVMCIRuntime::being_initialized;
1369 JVMCI_event_1("initializing JavaVM references in JVMCI runtime %d", id);
1370 } else {
1371 while (*state != JVMCIRuntime::fully_initialized) {
1372 JVMCI_event_1("waiting for JavaVM references initialization in JVMCI runtime %d", id);
1373 JVMCI_lock->wait();
1374 }
1375 JVMCI_event_1("done waiting for JavaVM references initialization in JVMCI runtime %d", id);
1376 }
1377 }
1378
1379 ~JavaVMRefsInitialization() {
1380 if (*_state == JVMCIRuntime::being_initialized) {
1381 *_state = JVMCIRuntime::fully_initialized;
1382 JVMCI_event_1("initialized JavaVM references in JVMCI runtime %d", _id);
1383 JVMCI_lock->notify_all();
1384 }
1385 }
1386
1387 bool should_init() {
1388 return *_state == JVMCIRuntime::being_initialized;
1389 }
1390 };
1391
1392 void JVMCIRuntime::initialize(JVMCI_TRAPS) {
1393 // Check first without _lock
1394 if (_init_state == fully_initialized) {
1395 return;
1396 }
1397
1398 JavaThread* THREAD = JavaThread::current();
1399
1400 MutexLocker locker(_lock);
1401 // Check again under _lock
1402 if (_init_state == fully_initialized) {
1403 return;
1404 }
1405
1406 while (_init_state == being_initialized) {
1407 JVMCI_event_1("waiting for initialization of JVMCI runtime %d", _id);
1408 _lock->wait();
1409 if (_init_state == fully_initialized) {
1410 JVMCI_event_1("done waiting for initialization of JVMCI runtime %d", _id);
1411 return;
1412 }
1413 }
1414
1415 JVMCI_event_1("initializing JVMCI runtime %d", _id);
1416 _init_state = being_initialized;
1417
1418 {
1419 MutexUnlocker unlock(_lock);
1420
1421 HandleMark hm(THREAD);
1422 ResourceMark rm(THREAD);
1423 {
1424 MutexLocker lock_jvmci(JVMCI_lock);
1425 if (JVMCIENV->is_hotspot()) {
1426 JavaVMRefsInitialization initialization(&_hotspot_javavm_refs_init_state, _id);
1427 if (initialization.should_init()) {
1428 MutexUnlocker unlock_jvmci(JVMCI_lock);
1429 HotSpotJVMCI::compute_offsets(CHECK_EXIT);
1430 }
1431 } else {
1432 JavaVMRefsInitialization initialization(&_shared_library_javavm_refs_init_state, _id);
1433 if (initialization.should_init()) {
1434 MutexUnlocker unlock_jvmci(JVMCI_lock);
1435 JNIAccessMark jni(JVMCIENV, THREAD);
1436
1437 JNIJVMCI::initialize_ids(jni.env());
1438 if (jni()->ExceptionCheck()) {
1439 jni()->ExceptionDescribe();
1440 fatal("JNI exception during init");
1441 }
1442 // _lock is re-locked at this point
1443 }
1444 }
1445 }
1446
1447 if (!JVMCIENV->is_hotspot()) {
1448 JNIAccessMark jni(JVMCIENV, THREAD);
1449 JNIJVMCI::register_natives(jni.env());
1450 }
1451 create_jvmci_primitive_type(T_BOOLEAN, JVMCI_CHECK_EXIT_((void)0));
1452 create_jvmci_primitive_type(T_BYTE, JVMCI_CHECK_EXIT_((void)0));
1453 create_jvmci_primitive_type(T_CHAR, JVMCI_CHECK_EXIT_((void)0));
1454 create_jvmci_primitive_type(T_SHORT, JVMCI_CHECK_EXIT_((void)0));
1455 create_jvmci_primitive_type(T_INT, JVMCI_CHECK_EXIT_((void)0));
1456 create_jvmci_primitive_type(T_LONG, JVMCI_CHECK_EXIT_((void)0));
1457 create_jvmci_primitive_type(T_FLOAT, JVMCI_CHECK_EXIT_((void)0));
1458 create_jvmci_primitive_type(T_DOUBLE, JVMCI_CHECK_EXIT_((void)0));
1459 create_jvmci_primitive_type(T_VOID, JVMCI_CHECK_EXIT_((void)0));
1460
1461 DEBUG_ONLY(CodeInstaller::verify_bci_constants(JVMCIENV);)
1462 }
1463
1464 _init_state = fully_initialized;
1465 JVMCI_event_1("initialized JVMCI runtime %d", _id);
1466 _lock->notify_all();
1467 }
1468
1469 JVMCIObject JVMCIRuntime::create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS) {
1470 JavaThread* THREAD = JavaThread::current(); // For exception macros.
1471 // These primitive types are long lived and are created before the runtime is fully set up
1472 // so skip registering them for scanning.
1473 JVMCIObject mirror = JVMCIENV->get_object_constant(java_lang_Class::primitive_mirror(type), false, true);
1474 if (JVMCIENV->is_hotspot()) {
1475 JavaValue result(T_OBJECT);
1476 JavaCallArguments args;
1477 args.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(mirror)));
1478 args.push_int(type2char(type));
1479 JavaCalls::call_static(&result, HotSpotJVMCI::HotSpotResolvedPrimitiveType::klass(), vmSymbols::fromMetaspace_name(), vmSymbols::primitive_fromMetaspace_signature(), &args, CHECK_(JVMCIObject()));
1480
1481 return JVMCIENV->wrap(JNIHandles::make_local(result.get_oop()));
1482 } else {
1483 JNIAccessMark jni(JVMCIENV);
1484 jobject result = jni()->CallStaticObjectMethod(JNIJVMCI::HotSpotResolvedPrimitiveType::clazz(),
1485 JNIJVMCI::HotSpotResolvedPrimitiveType_fromMetaspace_method(),
1486 mirror.as_jobject(), type2char(type));
1487 if (jni()->ExceptionCheck()) {
1488 return JVMCIObject();
1489 }
1490 return JVMCIENV->wrap(result);
1491 }
1492 }
1493
1494 void JVMCIRuntime::initialize_JVMCI(JVMCI_TRAPS) {
1495 if (!is_HotSpotJVMCIRuntime_initialized()) {
1496 initialize(JVMCI_CHECK);
1497 JVMCIENV->call_JVMCI_getRuntime(JVMCI_CHECK);
1498 guarantee(_HotSpotJVMCIRuntime_instance.is_non_null(), "NPE in JVMCI runtime %d", _id);
1499 }
1500 }
1501
1502 JVMCIObject JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_TRAPS) {
1503 initialize(JVMCI_CHECK_(JVMCIObject()));
1504 initialize_JVMCI(JVMCI_CHECK_(JVMCIObject()));
1505 return _HotSpotJVMCIRuntime_instance;
1506 }
1507
1508 // Implementation of CompilerToVM.registerNatives()
1509 // When called from libjvmci, `libjvmciOrHotspotEnv` is a libjvmci env so use JVM_ENTRY_NO_ENV.
1510 JVM_ENTRY_NO_ENV(void, JVM_RegisterJVMCINatives(JNIEnv *libjvmciOrHotspotEnv, jclass c2vmClass))
1511 JVMCIENV_FROM_JNI(thread, libjvmciOrHotspotEnv);
1512
1513 if (!EnableJVMCI) {
1514 JVMCI_THROW_MSG(InternalError, JVMCI_NOT_ENABLED_ERROR_MESSAGE);
1515 }
1516
1517 JVMCIENV->runtime()->initialize(JVMCIENV);
1518
1519 {
1520 ResourceMark rm(thread);
1521 HandleMark hm(thread);
1522 ThreadToNativeFromVM trans(thread);
1523
1524 // Ensure _non_oop_bits is initialized
1525 Universe::non_oop_word();
1526 JNIEnv *env = libjvmciOrHotspotEnv;
1527 if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count())) {
1528 if (!env->ExceptionCheck()) {
1529 for (int i = 0; i < CompilerToVM::methods_count(); i++) {
1530 if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods + i, 1)) {
1531 guarantee(false, "Error registering JNI method %s%s", CompilerToVM::methods[i].name, CompilerToVM::methods[i].signature);
1532 break;
1533 }
1534 }
1535 } else {
1536 env->ExceptionDescribe();
1537 }
1538 guarantee(false, "Failed registering CompilerToVM native methods");
1539 }
1540 }
1541 JVM_END
1542
1543
1544 void JVMCIRuntime::shutdown() {
1545 if (_HotSpotJVMCIRuntime_instance.is_non_null()) {
1546 JVMCI_event_1("shutting down HotSpotJVMCIRuntime for JVMCI runtime %d", _id);
1547 JVMCIEnv __stack_jvmci_env__(JavaThread::current(), _HotSpotJVMCIRuntime_instance.is_hotspot(),__FILE__, __LINE__);
1548 JVMCIEnv* JVMCIENV = &__stack_jvmci_env__;
1549 if (JVMCIENV->init_error() == JNI_OK) {
1550 JVMCIENV->call_HotSpotJVMCIRuntime_shutdown(_HotSpotJVMCIRuntime_instance);
1551 } else {
1552 JVMCI_event_1("Error in JVMCIEnv for shutdown (err: %d)", JVMCIENV->init_error());
1553 }
1554 if (_num_attached_threads == cannot_be_attached) {
1555 // Only when no other threads are attached to this runtime
1556 // is it safe to reset these fields.
1557 _HotSpotJVMCIRuntime_instance = JVMCIObject();
1558 _init_state = uninitialized;
1559 JVMCI_event_1("shut down JVMCI runtime %d", _id);
1560 }
1561 }
1562 }
1563
1564 bool JVMCIRuntime::destroy_shared_library_javavm() {
1565 guarantee(_num_attached_threads == cannot_be_attached,
1566 "cannot destroy JavaVM for JVMCI runtime %d with %d attached threads", _id, _num_attached_threads);
1567 JavaVM* javaVM;
1568 jlong javaVM_id = _shared_library_javavm_id;
1569 {
1570 // Exactly one thread can destroy the JavaVM
1571 // and release the handle to it.
1572 MutexLocker only_one(_lock);
1573 javaVM = _shared_library_javavm;
1574 if (javaVM != nullptr) {
1575 _shared_library_javavm = nullptr;
1576 _shared_library_javavm_id = 0;
1577 }
1578 }
1579 if (javaVM != nullptr) {
1580 int result;
1581 {
1582 // Must transition into native before calling into libjvmci
1583 ThreadToNativeFromVM ttnfv(JavaThread::current());
1584 result = javaVM->DestroyJavaVM();
1585 }
1586 if (result == JNI_OK) {
1587 JVMCI_event_1("destroyed JavaVM[" JLONG_FORMAT "]@" PTR_FORMAT " for JVMCI runtime %d", javaVM_id, p2i(javaVM), _id);
1588 } else {
1589 warning("Non-zero result (%d) when calling JNI_DestroyJavaVM on JavaVM[" JLONG_FORMAT "]@" PTR_FORMAT, result, javaVM_id, p2i(javaVM));
1590 }
1591 return true;
1592 }
1593 return false;
1594 }
1595
1596 void JVMCIRuntime::bootstrap_finished(TRAPS) {
1597 if (_HotSpotJVMCIRuntime_instance.is_non_null()) {
1598 JVMCIENV_FROM_THREAD(THREAD);
1599 JVMCIENV->check_init(CHECK);
1600 JVMCIENV->call_HotSpotJVMCIRuntime_bootstrapFinished(_HotSpotJVMCIRuntime_instance, JVMCIENV);
1601 }
1602 }
1603
1604 void JVMCIRuntime::describe_pending_hotspot_exception(JavaThread* THREAD) {
1605 if (HAS_PENDING_EXCEPTION) {
1606 Handle exception(THREAD, PENDING_EXCEPTION);
1607 CLEAR_PENDING_EXCEPTION;
1608 java_lang_Throwable::print_stack_trace(exception, tty);
1609
1610 // Clear and ignore any exceptions raised during printing
1611 CLEAR_PENDING_EXCEPTION;
1612 }
1613 }
1614
1615
1616 void JVMCIRuntime::fatal_exception(JVMCIEnv* JVMCIENV, const char* message) {
1617 JavaThread* THREAD = JavaThread::current(); // For exception macros.
1618
1619 static volatile int report_error = 0;
1620 if (!report_error && Atomic::cmpxchg(&report_error, 0, 1) == 0) {
1621 // Only report an error once
1622 tty->print_raw_cr(message);
1623 if (JVMCIENV != nullptr) {
1624 JVMCIENV->describe_pending_exception(tty);
1625 } else {
1626 describe_pending_hotspot_exception(THREAD);
1627 }
1628 } else {
1629 // Allow error reporting thread time to print the stack trace.
1630 THREAD->sleep(200);
1631 }
1632 fatal("Fatal JVMCI exception (see JVMCI Events for stack trace): %s", message);
1633 }
1634
1635 // ------------------------------------------------------------------
1636 // Note: the logic of this method should mirror the logic of
1637 // constantPoolOopDesc::verify_constant_pool_resolve.
1638 bool JVMCIRuntime::check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass) {
1639 if (accessing_klass->is_objArray_klass()) {
1640 accessing_klass = ObjArrayKlass::cast(accessing_klass)->bottom_klass();
1641 }
1642 if (!accessing_klass->is_instance_klass()) {
1643 return true;
1644 }
1645
1646 if (resolved_klass->is_objArray_klass()) {
1647 // Find the element klass, if this is an array.
1648 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass();
1649 }
1650 if (resolved_klass->is_instance_klass()) {
1651 Reflection::VerifyClassAccessResults result =
1652 Reflection::verify_class_access(accessing_klass, InstanceKlass::cast(resolved_klass), true);
1653 return result == Reflection::ACCESS_OK;
1654 }
1655 return true;
1656 }
1657
1658 // ------------------------------------------------------------------
1659 Klass* JVMCIRuntime::get_klass_by_name_impl(Klass*& accessing_klass,
1660 const constantPoolHandle& cpool,
1661 Symbol* sym,
1662 bool require_local) {
1663 JVMCI_EXCEPTION_CONTEXT;
1664
1665 // Now we need to check the SystemDictionary
1666 if (sym->char_at(0) == JVM_SIGNATURE_CLASS &&
1667 sym->char_at(sym->utf8_length()-1) == JVM_SIGNATURE_ENDCLASS) {
1668 // This is a name from a signature. Strip off the trimmings.
1669 // Call recursive to keep scope of strippedsym.
1670 TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1,
1671 sym->utf8_length()-2);
1672 return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local);
1673 }
1674
1675 Handle loader;
1676 if (accessing_klass != nullptr) {
1677 loader = Handle(THREAD, accessing_klass->class_loader());
1678 }
1679
1680 Klass* found_klass = require_local ?
1681 SystemDictionary::find_instance_or_array_klass(THREAD, sym, loader) :
1682 SystemDictionary::find_constrained_instance_or_array_klass(THREAD, sym, loader);
1683
1684 // If we fail to find an array klass, look again for its element type.
1685 // The element type may be available either locally or via constraints.
1686 // In either case, if we can find the element type in the system dictionary,
1687 // we must build an array type around it. The CI requires array klasses
1688 // to be loaded if their element klasses are loaded, except when memory
1689 // is exhausted.
1690 if (sym->char_at(0) == JVM_SIGNATURE_ARRAY &&
1691 (sym->char_at(1) == JVM_SIGNATURE_ARRAY || sym->char_at(1) == JVM_SIGNATURE_CLASS)) {
1692 // We have an unloaded array.
1693 // Build it on the fly if the element class exists.
1694 TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1,
1695 sym->utf8_length()-1);
1696
1697 // Get element Klass recursively.
1698 Klass* elem_klass =
1699 get_klass_by_name_impl(accessing_klass,
1700 cpool,
1701 elem_sym,
1702 require_local);
1703 if (elem_klass != nullptr) {
1704 // Now make an array for it
1705 return elem_klass->array_klass(THREAD);
1706 }
1707 }
1708
1709 if (found_klass == nullptr && !cpool.is_null() && cpool->has_preresolution()) {
1710 // Look inside the constant pool for pre-resolved class entries.
1711 for (int i = cpool->length() - 1; i >= 1; i--) {
1712 if (cpool->tag_at(i).is_klass()) {
1713 Klass* kls = cpool->resolved_klass_at(i);
1714 if (kls->name() == sym) {
1715 return kls;
1716 }
1717 }
1718 }
1719 }
1720
1721 return found_klass;
1722 }
1723
1724 // ------------------------------------------------------------------
1725 Klass* JVMCIRuntime::get_klass_by_name(Klass* accessing_klass,
1726 Symbol* klass_name,
1727 bool require_local) {
1728 ResourceMark rm;
1729 constantPoolHandle cpool;
1730 return get_klass_by_name_impl(accessing_klass,
1731 cpool,
1732 klass_name,
1733 require_local);
1734 }
1735
1736 // ------------------------------------------------------------------
1737 // Implementation of get_klass_by_index.
1738 Klass* JVMCIRuntime::get_klass_by_index_impl(const constantPoolHandle& cpool,
1739 int index,
1740 bool& is_accessible,
1741 Klass* accessor) {
1742 JVMCI_EXCEPTION_CONTEXT;
1743 Klass* klass = ConstantPool::klass_at_if_loaded(cpool, index);
1744 Symbol* klass_name = nullptr;
1745 if (klass == nullptr) {
1746 klass_name = cpool->klass_name_at(index);
1747 }
1748
1749 if (klass == nullptr) {
1750 // Not found in constant pool. Use the name to do the lookup.
1751 Klass* k = get_klass_by_name_impl(accessor,
1752 cpool,
1753 klass_name,
1754 false);
1755 // Calculate accessibility the hard way.
1756 if (k == nullptr) {
1757 is_accessible = false;
1758 } else if (k->class_loader() != accessor->class_loader() &&
1759 get_klass_by_name_impl(accessor, cpool, k->name(), true) == nullptr) {
1760 // Loaded only remotely. Not linked yet.
1761 is_accessible = false;
1762 } else {
1763 // Linked locally, and we must also check public/private, etc.
1764 is_accessible = check_klass_accessibility(accessor, k);
1765 }
1766 if (!is_accessible) {
1767 return nullptr;
1768 }
1769 return k;
1770 }
1771
1772 // It is known to be accessible, since it was found in the constant pool.
1773 is_accessible = true;
1774 return klass;
1775 }
1776
1777 // ------------------------------------------------------------------
1778 // Get a klass from the constant pool.
1779 Klass* JVMCIRuntime::get_klass_by_index(const constantPoolHandle& cpool,
1780 int index,
1781 bool& is_accessible,
1782 Klass* accessor) {
1783 ResourceMark rm;
1784 Klass* result = get_klass_by_index_impl(cpool, index, is_accessible, accessor);
1785 return result;
1786 }
1787
1788 // ------------------------------------------------------------------
1789 // Perform an appropriate method lookup based on accessor, holder,
1790 // name, signature, and bytecode.
1791 Method* JVMCIRuntime::lookup_method(InstanceKlass* accessor,
1792 Klass* holder,
1793 Symbol* name,
1794 Symbol* sig,
1795 Bytecodes::Code bc,
1796 constantTag tag) {
1797 // Accessibility checks are performed in JVMCIEnv::get_method_by_index_impl().
1798 assert(check_klass_accessibility(accessor, holder), "holder not accessible");
1799
1800 LinkInfo link_info(holder, name, sig, accessor,
1801 LinkInfo::AccessCheck::required,
1802 LinkInfo::LoaderConstraintCheck::required,
1803 tag);
1804 switch (bc) {
1805 case Bytecodes::_invokestatic:
1806 return LinkResolver::resolve_static_call_or_null(link_info);
1807 case Bytecodes::_invokespecial:
1808 return LinkResolver::resolve_special_call_or_null(link_info);
1809 case Bytecodes::_invokeinterface:
1810 return LinkResolver::linktime_resolve_interface_method_or_null(link_info);
1811 case Bytecodes::_invokevirtual:
1812 return LinkResolver::linktime_resolve_virtual_method_or_null(link_info);
1813 default:
1814 fatal("Unhandled bytecode: %s", Bytecodes::name(bc));
1815 return nullptr; // silence compiler warnings
1816 }
1817 }
1818
1819
1820 // ------------------------------------------------------------------
1821 Method* JVMCIRuntime::get_method_by_index_impl(const constantPoolHandle& cpool,
1822 int index, Bytecodes::Code bc,
1823 InstanceKlass* accessor) {
1824 if (bc == Bytecodes::_invokedynamic) {
1825 if (cpool->resolved_indy_entry_at(index)->is_resolved()) {
1826 return cpool->resolved_indy_entry_at(index)->method();
1827 }
1828
1829 return nullptr;
1830 }
1831
1832 int holder_index = cpool->klass_ref_index_at(index, bc);
1833 bool holder_is_accessible;
1834 Klass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor);
1835
1836 // Get the method's name and signature.
1837 Symbol* name_sym = cpool->name_ref_at(index, bc);
1838 Symbol* sig_sym = cpool->signature_ref_at(index, bc);
1839
1840 if (cpool->has_preresolution()
1841 || ((holder == vmClasses::MethodHandle_klass() || holder == vmClasses::VarHandle_klass()) &&
1842 MethodHandles::is_signature_polymorphic_name(holder, name_sym))) {
1843 // Short-circuit lookups for JSR 292-related call sites.
1844 // That is, do not rely only on name-based lookups, because they may fail
1845 // if the names are not resolvable in the boot class loader (7056328).
1846 switch (bc) {
1847 case Bytecodes::_invokevirtual:
1848 case Bytecodes::_invokeinterface:
1849 case Bytecodes::_invokespecial:
1850 case Bytecodes::_invokestatic:
1851 {
1852 Method* m = ConstantPool::method_at_if_loaded(cpool, index);
1853 if (m != nullptr) {
1854 return m;
1855 }
1856 }
1857 break;
1858 default:
1859 break;
1860 }
1861 }
1862
1863 if (holder_is_accessible) { // Our declared holder is loaded.
1864 constantTag tag = cpool->tag_ref_at(index, bc);
1865 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
1866 if (m != nullptr) {
1867 // We found the method.
1868 return m;
1869 }
1870 }
1871
1872 // Either the declared holder was not loaded, or the method could
1873 // not be found.
1874
1875 return nullptr;
1876 }
1877
1878 // ------------------------------------------------------------------
1879 InstanceKlass* JVMCIRuntime::get_instance_klass_for_declared_method_holder(Klass* method_holder) {
1880 // For the case of <array>.clone(), the method holder can be an ArrayKlass*
1881 // instead of an InstanceKlass*. For that case simply pretend that the
1882 // declared holder is Object.clone since that's where the call will bottom out.
1883 if (method_holder->is_instance_klass()) {
1884 return InstanceKlass::cast(method_holder);
1885 } else if (method_holder->is_array_klass()) {
1886 return vmClasses::Object_klass();
1887 } else {
1888 ShouldNotReachHere();
1889 }
1890 return nullptr;
1891 }
1892
1893
1894 // ------------------------------------------------------------------
1895 Method* JVMCIRuntime::get_method_by_index(const constantPoolHandle& cpool,
1896 int index, Bytecodes::Code bc,
1897 InstanceKlass* accessor) {
1898 ResourceMark rm;
1899 return get_method_by_index_impl(cpool, index, bc, accessor);
1900 }
1901
1902 // ------------------------------------------------------------------
1903 // Check for changes to the system dictionary during compilation
1904 // class loads, evolution, breakpoints
1905 JVMCI::CodeInstallResult JVMCIRuntime::validate_compile_task_dependencies(Dependencies* dependencies,
1906 JVMCICompileState* compile_state,
1907 char** failure_detail,
1908 bool& failing_dep_is_call_site)
1909 {
1910 failing_dep_is_call_site = false;
1911 // If JVMTI capabilities were enabled during compile, the compilation is invalidated.
1912 if (compile_state != nullptr && compile_state->jvmti_state_changed()) {
1913 *failure_detail = (char*) "Jvmti state change during compilation invalidated dependencies";
1914 return JVMCI::dependencies_failed;
1915 }
1916
1917 CompileTask* task = compile_state == nullptr ? nullptr : compile_state->task();
1918 Dependencies::DepType result = dependencies->validate_dependencies(task, failure_detail);
1919
1920 if (result == Dependencies::end_marker) {
1921 return JVMCI::ok;
1922 }
1923 if (result == Dependencies::call_site_target_value) {
1924 failing_dep_is_call_site = true;
1925 }
1926 return JVMCI::dependencies_failed;
1927 }
1928
1929 // Called after an upcall to `function` while compiling `method`.
1930 // If an exception occurred, it is cleared, the compilation state
1931 // is updated with the failure and this method returns true.
1932 // Otherwise, it returns false.
1933 static bool after_compiler_upcall(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, const char* function) {
1934 if (JVMCIENV->has_pending_exception()) {
1935 ResourceMark rm;
1936 bool reason_on_C_heap = true;
1937 const char* pending_string = nullptr;
1938 const char* pending_stack_trace = nullptr;
1939 JVMCIENV->pending_exception_as_string(&pending_string, &pending_stack_trace);
1940 if (pending_string == nullptr) pending_string = "null";
1941 // Using stringStream instead of err_msg to avoid truncation
1942 stringStream st;
1943 st.print("uncaught exception in %s [%s]", function, pending_string);
1944 const char* failure_reason = os::strdup(st.freeze(), mtJVMCI);
1945 if (failure_reason == nullptr) {
1946 failure_reason = "uncaught exception";
1947 reason_on_C_heap = false;
1948 }
1949 JVMCI_event_1("%s", failure_reason);
1950 Log(jit, compilation) log;
1951 if (log.is_info()) {
1952 log.info("%s while compiling %s", failure_reason, method->name_and_sig_as_C_string());
1953 if (pending_stack_trace != nullptr) {
1954 LogStream ls(log.info());
1955 ls.print_raw_cr(pending_stack_trace);
1956 }
1957 }
1958 JVMCICompileState* compile_state = JVMCIENV->compile_state();
1959 compile_state->set_failure(true, failure_reason, reason_on_C_heap);
1960 compiler->on_upcall(failure_reason, compile_state);
1961 return true;
1962 }
1963 return false;
1964 }
1965
1966 void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, int entry_bci) {
1967 JVMCI_EXCEPTION_CONTEXT
1968
1969 JVMCICompileState* compile_state = JVMCIENV->compile_state();
1970
1971 bool is_osr = entry_bci != InvocationEntryBci;
1972 if (compiler->is_bootstrapping() && is_osr) {
1973 // no OSR compilations during bootstrap - the compiler is just too slow at this point,
1974 // and we know that there are no endless loops
1975 compile_state->set_failure(true, "No OSR during bootstrap");
1976 return;
1977 }
1978 if (JVMCI::in_shutdown()) {
1979 if (UseJVMCINativeLibrary) {
1980 JVMCIRuntime *runtime = JVMCI::compiler_runtime(thread, false);
1981 if (runtime != nullptr) {
1982 runtime->detach_thread(thread, "JVMCI shutdown pre-empted compilation");
1983 }
1984 }
1985 compile_state->set_failure(false, "Avoiding compilation during shutdown");
1986 return;
1987 }
1988
1989 HandleMark hm(thread);
1990 JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
1991 if (after_compiler_upcall(JVMCIENV, compiler, method, "get_HotSpotJVMCIRuntime")) {
1992 return;
1993 }
1994 JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(method, JVMCIENV);
1995 if (after_compiler_upcall(JVMCIENV, compiler, method, "get_jvmci_method")) {
1996 return;
1997 }
1998
1999 JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci,
2000 (jlong) compile_state, compile_state->task()->compile_id());
2001 #ifdef ASSERT
2002 if (JVMCIENV->has_pending_exception()) {
2003 const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.compileMethodExceptionIsFatal");
2004 if (val != nullptr && strcmp(val, "true") == 0) {
2005 fatal_exception(JVMCIENV, "testing JVMCI fatal exception handling");
2006 }
2007 }
2008 #endif
2009
2010 if (after_compiler_upcall(JVMCIENV, compiler, method, "call_HotSpotJVMCIRuntime_compileMethod")) {
2011 return;
2012 }
2013 compiler->on_upcall(nullptr);
2014 guarantee(result_object.is_non_null(), "call_HotSpotJVMCIRuntime_compileMethod returned null");
2015 JVMCIObject failure_message = JVMCIENV->get_HotSpotCompilationRequestResult_failureMessage(result_object);
2016 if (failure_message.is_non_null()) {
2017 // Copy failure reason into resource memory first ...
2018 const char* failure_reason = JVMCIENV->as_utf8_string(failure_message);
2019 // ... and then into the C heap.
2020 failure_reason = os::strdup(failure_reason, mtJVMCI);
2021 bool retryable = JVMCIENV->get_HotSpotCompilationRequestResult_retry(result_object) != 0;
2022 compile_state->set_failure(retryable, failure_reason, true);
2023 } else {
2024 if (!compile_state->task()->is_success()) {
2025 compile_state->set_failure(true, "no nmethod produced");
2026 } else {
2027 compile_state->task()->set_num_inlined_bytecodes(JVMCIENV->get_HotSpotCompilationRequestResult_inlinedBytecodes(result_object));
2028 compiler->inc_methods_compiled();
2029 }
2030 }
2031 if (compiler->is_bootstrapping()) {
2032 compiler->set_bootstrap_compilation_request_handled();
2033 }
2034 }
2035
2036 bool JVMCIRuntime::is_gc_supported(JVMCIEnv* JVMCIENV, CollectedHeap::Name name) {
2037 JVMCI_EXCEPTION_CONTEXT
2038
2039 JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
2040 if (JVMCIENV->has_pending_exception()) {
2041 fatal_exception(JVMCIENV, "Exception during HotSpotJVMCIRuntime initialization");
2042 }
2043 return JVMCIENV->call_HotSpotJVMCIRuntime_isGCSupported(receiver, (int) name);
2044 }
2045
2046 bool JVMCIRuntime::is_intrinsic_supported(JVMCIEnv* JVMCIENV, jint id) {
2047 JVMCI_EXCEPTION_CONTEXT
2048
2049 JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV);
2050 if (JVMCIENV->has_pending_exception()) {
2051 fatal_exception(JVMCIENV, "Exception during HotSpotJVMCIRuntime initialization");
2052 }
2053 return JVMCIENV->call_HotSpotJVMCIRuntime_isIntrinsicSupported(receiver, id);
2054 }
2055
2056 // ------------------------------------------------------------------
2057 JVMCI::CodeInstallResult JVMCIRuntime::register_method(JVMCIEnv* JVMCIENV,
2058 const methodHandle& method,
2059 nmethod*& nm,
2060 int entry_bci,
2061 CodeOffsets* offsets,
2062 int orig_pc_offset,
2063 CodeBuffer* code_buffer,
2064 int frame_words,
2065 OopMapSet* oop_map_set,
2066 ExceptionHandlerTable* handler_table,
2067 ImplicitExceptionTable* implicit_exception_table,
2068 AbstractCompiler* compiler,
2069 DebugInformationRecorder* debug_info,
2070 Dependencies* dependencies,
2071 int compile_id,
2072 bool has_monitors,
2073 bool has_unsafe_access,
2074 bool has_scoped_access,
2075 bool has_wide_vector,
2076 JVMCIObject compiled_code,
2077 JVMCIObject nmethod_mirror,
2078 FailedSpeculation** failed_speculations,
2079 char* speculations,
2080 int speculations_len,
2081 int nmethod_entry_patch_offset) {
2082 JVMCI_EXCEPTION_CONTEXT;
2083 CompLevel comp_level = CompLevel_full_optimization;
2084 char* failure_detail = nullptr;
2085
2086 bool install_default = JVMCIENV->get_HotSpotNmethod_isDefault(nmethod_mirror) != 0;
2087 assert(JVMCIENV->isa_HotSpotNmethod(nmethod_mirror), "must be");
2088 JVMCIObject name = JVMCIENV->get_InstalledCode_name(nmethod_mirror);
2089 const char* nmethod_mirror_name = name.is_null() ? nullptr : JVMCIENV->as_utf8_string(name);
2090 int nmethod_mirror_index;
2091 if (!install_default) {
2092 // Reserve or initialize mirror slot in the oops table.
2093 OopRecorder* oop_recorder = debug_info->oop_recorder();
2094 nmethod_mirror_index = oop_recorder->allocate_oop_index(nmethod_mirror.is_hotspot() ? nmethod_mirror.as_jobject() : nullptr);
2095 } else {
2096 // A default HotSpotNmethod mirror is never tracked by the nmethod
2097 nmethod_mirror_index = -1;
2098 }
2099
2100 JVMCI::CodeInstallResult result(JVMCI::ok);
2101
2102 // We require method counters to store some method state (max compilation levels) required by the compilation policy.
2103 if (method->get_method_counters(THREAD) == nullptr) {
2104 result = JVMCI::cache_full;
2105 failure_detail = (char*) "can't create method counters";
2106 }
2107
2108 if (result == JVMCI::ok) {
2109 // Check if memory should be freed before allocation
2110 CodeCache::gc_on_allocation();
2111
2112 // To prevent compile queue updates.
2113 MutexLocker locker(THREAD, MethodCompileQueue_lock);
2114
2115 // Prevent InstanceKlass::add_to_hierarchy from running
2116 // and invalidating our dependencies until we install this method.
2117 MutexLocker ml(Compile_lock);
2118
2119 // Encode the dependencies now, so we can check them right away.
2120 dependencies->encode_content_bytes();
2121
2122 // Record the dependencies for the current compile in the log
2123 if (LogCompilation) {
2124 for (Dependencies::DepStream deps(dependencies); deps.next(); ) {
2125 deps.log_dependency();
2126 }
2127 }
2128
2129 // Check for {class loads, evolution, breakpoints} during compilation
2130 JVMCICompileState* compile_state = JVMCIENV->compile_state();
2131 bool failing_dep_is_call_site;
2132 result = validate_compile_task_dependencies(dependencies, compile_state, &failure_detail, failing_dep_is_call_site);
2133 if (result != JVMCI::ok) {
2134 // While not a true deoptimization, it is a preemptive decompile.
2135 MethodData* mdp = method()->method_data();
2136 if (mdp != nullptr && !failing_dep_is_call_site) {
2137 mdp->inc_decompile_count();
2138 #ifdef ASSERT
2139 if (mdp->decompile_count() > (uint)PerMethodRecompilationCutoff) {
2140 ResourceMark m;
2141 tty->print_cr("WARN: endless recompilation of %s. Method was set to not compilable.", method()->name_and_sig_as_C_string());
2142 }
2143 #endif
2144 }
2145
2146 // All buffers in the CodeBuffer are allocated in the CodeCache.
2147 // If the code buffer is created on each compile attempt
2148 // as in C2, then it must be freed.
2149 //code_buffer->free_blob();
2150 } else {
2151 JVMCINMethodData* data = JVMCINMethodData::create(nmethod_mirror_index,
2152 nmethod_entry_patch_offset,
2153 nmethod_mirror_name,
2154 failed_speculations);
2155 nm = nmethod::new_nmethod(method,
2156 compile_id,
2157 entry_bci,
2158 offsets,
2159 orig_pc_offset,
2160 debug_info, dependencies, code_buffer,
2161 frame_words, oop_map_set,
2162 handler_table, implicit_exception_table,
2163 compiler, comp_level,
2164 speculations, speculations_len, data);
2165
2166
2167 // Free codeBlobs
2168 if (nm == nullptr) {
2169 // The CodeCache is full. Print out warning and disable compilation.
2170 {
2171 MutexUnlocker ml(Compile_lock);
2172 MutexUnlocker locker(MethodCompileQueue_lock);
2173 CompileBroker::handle_full_code_cache(CodeCache::get_code_blob_type(comp_level));
2174 }
2175 result = JVMCI::cache_full;
2176 } else {
2177 nm->set_has_unsafe_access(has_unsafe_access);
2178 nm->set_has_wide_vectors(has_wide_vector);
2179 nm->set_has_monitors(has_monitors);
2180 nm->set_has_scoped_access(has_scoped_access);
2181
2182 JVMCINMethodData* data = nm->jvmci_nmethod_data();
2183 assert(data != nullptr, "must be");
2184 if (install_default) {
2185 assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == nullptr, "must be");
2186 if (entry_bci == InvocationEntryBci) {
2187 // If there is an old version we're done with it
2188 nmethod* old = method->code();
2189 if (TraceMethodReplacement && old != nullptr) {
2190 ResourceMark rm;
2191 char *method_name = method->name_and_sig_as_C_string();
2192 tty->print_cr("Replacing method %s", method_name);
2193 }
2194 if (old != nullptr) {
2195 old->make_not_entrant(nmethod::InvalidationReason::JVMCI_REPLACED_WITH_NEW_CODE);
2196 }
2197
2198 LogTarget(Info, nmethod, install) lt;
2199 if (lt.is_enabled()) {
2200 ResourceMark rm;
2201 char *method_name = method->name_and_sig_as_C_string();
2202 lt.print("Installing method (%d) %s [entry point: %p]",
2203 comp_level, method_name, nm->entry_point());
2204 }
2205 // Allow the code to be executed
2206 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2207 if (nm->make_in_use()) {
2208 method->set_code(method, nm);
2209 } else {
2210 result = JVMCI::nmethod_reclaimed;
2211 }
2212 } else {
2213 LogTarget(Info, nmethod, install) lt;
2214 if (lt.is_enabled()) {
2215 ResourceMark rm;
2216 char *method_name = method->name_and_sig_as_C_string();
2217 lt.print("Installing osr method (%d) %s @ %d",
2218 comp_level, method_name, entry_bci);
2219 }
2220 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2221 if (nm->make_in_use()) {
2222 InstanceKlass::cast(method->method_holder())->add_osr_nmethod(nm);
2223 } else {
2224 result = JVMCI::nmethod_reclaimed;
2225 }
2226 }
2227 } else {
2228 assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == HotSpotJVMCI::resolve(nmethod_mirror), "must be");
2229 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
2230 if (!nm->make_in_use()) {
2231 result = JVMCI::nmethod_reclaimed;
2232 }
2233 }
2234 }
2235 }
2236 }
2237
2238 // String creation must be done outside lock
2239 if (failure_detail != nullptr) {
2240 // A failure to allocate the string is silently ignored.
2241 JVMCIObject message = JVMCIENV->create_string(failure_detail, JVMCIENV);
2242 JVMCIENV->set_HotSpotCompiledNmethod_installationFailureMessage(compiled_code, message);
2243 }
2244
2245 if (result == JVMCI::ok) {
2246 JVMCICompileState* state = JVMCIENV->compile_state();
2247 if (state != nullptr) {
2248 // Compilation succeeded, post what we know about it
2249 nm->post_compiled_method(state->task());
2250 }
2251 }
2252
2253 return result;
2254 }
2255
2256 void JVMCIRuntime::post_compile(JavaThread* thread) {
2257 if (UseJVMCINativeLibrary && JVMCI::one_shared_library_javavm_per_compilation()) {
2258 if (thread->libjvmci_runtime() != nullptr) {
2259 detach_thread(thread, "single use JavaVM");
2260 } else {
2261 // JVMCI shutdown may have already detached the thread
2262 }
2263 }
2264 }