1 /*
2 * Copyright (c) 1997, 2026, 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 "classfile/javaClasses.inline.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmClasses.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "code/codeCache.hpp"
31 #include "compiler/compilationPolicy.hpp"
32 #include "compiler/compileBroker.hpp"
33 #include "compiler/disassembler.hpp"
34 #include "gc/shared/barrierSetNMethod.hpp"
35 #include "gc/shared/collectedHeap.hpp"
36 #include "interpreter/bytecodeTracer.hpp"
37 #include "interpreter/interpreter.hpp"
38 #include "interpreter/interpreterRuntime.hpp"
39 #include "interpreter/linkResolver.hpp"
40 #include "interpreter/templateTable.hpp"
41 #include "jvm_io.h"
42 #include "logging/log.hpp"
43 #include "memory/oopFactory.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "oops/constantPool.inline.hpp"
47 #include "oops/cpCache.inline.hpp"
48 #include "oops/flatArrayKlass.hpp"
49 #include "oops/flatArrayOop.inline.hpp"
50 #include "oops/inlineKlass.inline.hpp"
51 #include "oops/instanceKlass.inline.hpp"
52 #include "oops/klass.inline.hpp"
53 #include "oops/method.inline.hpp"
54 #include "oops/methodData.hpp"
55 #include "oops/objArrayKlass.hpp"
56 #include "oops/objArrayOop.inline.hpp"
57 #include "oops/oop.inline.hpp"
58 #include "oops/symbol.hpp"
59 #include "prims/jvmtiExport.hpp"
60 #include "prims/methodHandles.hpp"
61 #include "prims/nativeLookup.hpp"
62 #include "runtime/atomicAccess.hpp"
63 #include "runtime/continuation.hpp"
64 #include "runtime/deoptimization.hpp"
65 #include "runtime/fieldDescriptor.inline.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/handles.inline.hpp"
68 #include "runtime/icache.hpp"
69 #include "runtime/interfaceSupport.inline.hpp"
70 #include "runtime/java.hpp"
71 #include "runtime/javaCalls.hpp"
72 #include "runtime/jfieldIDWorkaround.hpp"
73 #include "runtime/osThread.hpp"
74 #include "runtime/sharedRuntime.hpp"
75 #include "runtime/stackWatermarkSet.hpp"
76 #include "runtime/stubRoutines.hpp"
77 #include "runtime/synchronizer.hpp"
78 #include "utilities/align.hpp"
79 #include "utilities/checkedCast.hpp"
80 #include "utilities/copy.hpp"
81 #include "utilities/events.hpp"
82 #include "utilities/globalDefinitions.hpp"
83 #if INCLUDE_JFR
84 #include "jfr/jfr.inline.hpp"
85 #endif
86
87 // Helper class to access current interpreter state
88 class LastFrameAccessor : public StackObj {
89 frame _last_frame;
90 public:
91 LastFrameAccessor(JavaThread* current) {
92 assert(current == Thread::current(), "sanity");
93 _last_frame = current->last_frame();
94 }
95 bool is_interpreted_frame() const { return _last_frame.is_interpreted_frame(); }
96 Method* method() const { return _last_frame.interpreter_frame_method(); }
97 address bcp() const { return _last_frame.interpreter_frame_bcp(); }
98 int bci() const { return _last_frame.interpreter_frame_bci(); }
99 address mdp() const { return _last_frame.interpreter_frame_mdp(); }
100
101 void set_bcp(address bcp) { _last_frame.interpreter_frame_set_bcp(bcp); }
102 void set_mdp(address dp) { _last_frame.interpreter_frame_set_mdp(dp); }
103
104 // pass method to avoid calling unsafe bcp_to_method (partial fix 4926272)
105 Bytecodes::Code code() const { return Bytecodes::code_at(method(), bcp()); }
106
107 Bytecode bytecode() const { return Bytecode(method(), bcp()); }
108 int get_index_u1(Bytecodes::Code bc) const { return bytecode().get_index_u1(bc); }
109 int get_index_u2(Bytecodes::Code bc) const { return bytecode().get_index_u2(bc); }
110 int get_index_u4(Bytecodes::Code bc) const { return bytecode().get_index_u4(bc); }
111 int number_of_dimensions() const { return bcp()[3]; }
112
113 oop callee_receiver(Symbol* signature) {
114 return _last_frame.interpreter_callee_receiver(signature);
115 }
116 BasicObjectLock* monitor_begin() const {
117 return _last_frame.interpreter_frame_monitor_begin();
118 }
119 BasicObjectLock* monitor_end() const {
120 return _last_frame.interpreter_frame_monitor_end();
121 }
122 BasicObjectLock* next_monitor(BasicObjectLock* current) const {
123 return _last_frame.next_monitor_in_interpreter_frame(current);
124 }
125
126 frame& get_frame() { return _last_frame; }
127 };
128
129 //------------------------------------------------------------------------------------------------------------------------
130 // State accessors
131
132 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread* current) {
133 LastFrameAccessor last_frame(current);
134 last_frame.set_bcp(bcp);
135 if (ProfileInterpreter) {
136 // ProfileTraps uses MDOs independently of ProfileInterpreter.
137 // That is why we must check both ProfileInterpreter and mdo != nullptr.
138 MethodData* mdo = last_frame.method()->method_data();
139 if (mdo != nullptr) {
140 NEEDS_CLEANUP;
141 last_frame.set_mdp(mdo->bci_to_dp(last_frame.bci()));
142 }
143 }
144 }
145
146 //------------------------------------------------------------------------------------------------------------------------
147 // Constants
148
149
150 JRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* current, bool wide))
151 // access constant pool
152 LastFrameAccessor last_frame(current);
153 ConstantPool* pool = last_frame.method()->constants();
154 int cp_index = wide ? last_frame.get_index_u2(Bytecodes::_ldc_w) : last_frame.get_index_u1(Bytecodes::_ldc);
155 constantTag tag = pool->tag_at(cp_index);
156
157 assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
158 Klass* klass = pool->klass_at(cp_index, CHECK);
159 oop java_class = klass->java_mirror();
160 current->set_vm_result_oop(java_class);
161 JRT_END
162
163 JRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* current, Bytecodes::Code bytecode)) {
164 assert(bytecode == Bytecodes::_ldc ||
165 bytecode == Bytecodes::_ldc_w ||
166 bytecode == Bytecodes::_ldc2_w ||
167 bytecode == Bytecodes::_fast_aldc ||
168 bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
169 ResourceMark rm(current);
170 const bool is_fast_aldc = (bytecode == Bytecodes::_fast_aldc ||
171 bytecode == Bytecodes::_fast_aldc_w);
172 LastFrameAccessor last_frame(current);
173 methodHandle m (current, last_frame.method());
174 Bytecode_loadconstant ldc(m, last_frame.bci());
175
176 // Double-check the size. (Condy can have any type.)
177 BasicType type = ldc.result_type();
178 switch (type2size[type]) {
179 case 2: guarantee(bytecode == Bytecodes::_ldc2_w, ""); break;
180 case 1: guarantee(bytecode != Bytecodes::_ldc2_w, ""); break;
181 default: ShouldNotReachHere();
182 }
183
184 // Resolve the constant. This does not do unboxing.
185 // But it does replace Universe::the_null_sentinel by null.
186 oop result = ldc.resolve_constant(CHECK);
187 assert(result != nullptr || is_fast_aldc, "null result only valid for fast_aldc");
188
189 #ifdef ASSERT
190 {
191 // The bytecode wrappers aren't GC-safe so construct a new one
192 Bytecode_loadconstant ldc2(m, last_frame.bci());
193 int rindex = ldc2.cache_index();
194 if (rindex < 0)
195 rindex = m->constants()->cp_to_object_index(ldc2.pool_index());
196 if (rindex >= 0) {
197 oop coop = m->constants()->resolved_reference_at(rindex);
198 oop roop = (result == nullptr ? Universe::the_null_sentinel() : result);
199 assert(roop == coop, "expected result for assembly code");
200 }
201 }
202 #endif
203 current->set_vm_result_oop(result);
204 if (!is_fast_aldc) {
205 // Tell the interpreter how to unbox the primitive.
206 guarantee(java_lang_boxing_object::is_instance(result, type), "");
207 int offset = java_lang_boxing_object::value_offset(type);
208 intptr_t flags = ((as_TosState(type) << ConstantPoolCache::tos_state_shift)
209 | (offset & ConstantPoolCache::field_index_mask));
210 current->set_vm_result_metadata((Metadata*)flags);
211 }
212 }
213 JRT_END
214
215
216 //------------------------------------------------------------------------------------------------------------------------
217 // Allocation
218
219 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
220 Klass* k = pool->klass_at(index, CHECK);
221 InstanceKlass* klass = InstanceKlass::cast(k);
222
223 // Make sure we are not instantiating an abstract klass
224 klass->check_valid_for_instantiation(true, CHECK);
225
226 // Make sure klass is initialized
227 klass->initialize_preemptable(CHECK_AND_CLEAR_PREEMPTED);
228
229 oop obj = klass->allocate_instance(CHECK);
230 current->set_vm_result_oop(obj);
231 JRT_END
232
233 JRT_BLOCK_ENTRY(void, InterpreterRuntime::read_flat_field(JavaThread* current, oopDesc* obj, ResolvedFieldEntry* entry))
234 assert(oopDesc::is_oop(obj), "Sanity check");
235
236 InstanceKlass* holder = InstanceKlass::cast(entry->field_holder());
237 assert(entry->field_holder()->field_is_flat(entry->field_index()), "Sanity check");
238
239 InlineLayoutInfo* layout_info = holder->inline_layout_info_adr(entry->field_index());
240 InlineKlass* field_klass = layout_info->klass();
241 const LayoutKind lk = layout_info->kind();
242 const int offset = entry->field_offset();
243
244 // If the field is nullable and is marked null, return early.
245 if (LayoutKindHelper::is_nullable_flat(lk) &&
246 field_klass->is_payload_marked_as_null(cast_from_oop<address>(obj) + offset)) {
247 current->set_vm_result_oop(nullptr);
248 return;
249 }
250
251 #ifdef ASSERT
252 fieldDescriptor fd;
253 bool found = holder->find_field_from_offset(offset, false, &fd);
254 assert(found, "Field not found");
255 assert(fd.is_flat(), "Field must be flat");
256 #endif // ASSERT
257
258 JRT_BLOCK
259 oop res = field_klass->read_payload_from_addr(obj, (size_t)offset, lk, CHECK);
260 current->set_vm_result_oop(res);
261 JRT_BLOCK_END
262 JRT_END
263
264 JRT_ENTRY(void, InterpreterRuntime::write_flat_field(JavaThread* current, oopDesc* obj, oopDesc* value, ResolvedFieldEntry* entry))
265 assert(oopDesc::is_oop(obj), "Sanity check");
266 Handle obj_h(THREAD, obj);
267 assert(value == nullptr || oopDesc::is_oop(value), "Sanity check");
268 Handle val_h(THREAD, value);
269
270 InstanceKlass* holder = entry->field_holder();
271 InlineLayoutInfo* li = holder->inline_layout_info_adr(entry->field_index());
272 InlineKlass* vk = li->klass();
273 vk->write_value_to_addr(val_h(), ((char*)(oopDesc*)obj_h()) + entry->field_offset(), li->kind(), CHECK);
274 JRT_END
275
276 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
277 oop obj = oopFactory::new_typeArray(type, size, CHECK);
278 current->set_vm_result_oop(obj);
279 JRT_END
280
281
282 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
283 Klass* klass = pool->klass_at(index, CHECK);
284 arrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
285 current->set_vm_result_oop(obj);
286 JRT_END
287
288 JRT_ENTRY(void, InterpreterRuntime::flat_array_load(JavaThread* current, arrayOopDesc* array, int index))
289 assert(array->is_flatArray(), "Must be");
290 flatArrayOop farray = (flatArrayOop)array;
291 oop res = farray->obj_at(index, CHECK);
292 current->set_vm_result_oop(res);
293 JRT_END
294
295 JRT_ENTRY(void, InterpreterRuntime::flat_array_store(JavaThread* current, oopDesc* val, arrayOopDesc* array, int index))
296 assert(array->is_flatArray(), "Must be");
297 flatArrayOop farray = (flatArrayOop)array;
298 farray->obj_at_put(index, val, CHECK);
299 JRT_END
300
301 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
302 // We may want to pass in more arguments - could make this slightly faster
303 LastFrameAccessor last_frame(current);
304 ConstantPool* constants = last_frame.method()->constants();
305 int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
306 Klass* klass = constants->klass_at(i, CHECK);
307 int nof_dims = last_frame.number_of_dimensions();
308 assert(klass->is_klass(), "not a class");
309 assert(nof_dims >= 1, "multianewarray rank must be nonzero");
310
311 // We must create an array of jints to pass to multi_allocate.
312 ResourceMark rm(current);
313 const int small_dims = 10;
314 jint dim_array[small_dims];
315 jint *dims = &dim_array[0];
316 if (nof_dims > small_dims) {
317 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
318 }
319 for (int index = 0; index < nof_dims; index++) {
320 // offset from first_size_address is addressed as local[index]
321 int n = Interpreter::local_offset_in_bytes(index)/jintSize;
322 dims[index] = first_size_address[n];
323 }
324 oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
325 current->set_vm_result_oop(obj);
326 JRT_END
327
328
329 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
330 assert(oopDesc::is_oop(obj), "must be a valid oop");
331 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
332 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
333 JRT_END
334
335 JRT_ENTRY(jboolean, InterpreterRuntime::is_substitutable(JavaThread* current, oopDesc* aobj, oopDesc* bobj))
336 assert(oopDesc::is_oop(aobj) && oopDesc::is_oop(bobj), "must be valid oops");
337
338 Handle ha(THREAD, aobj);
339 Handle hb(THREAD, bobj);
340 JavaValue result(T_BOOLEAN);
341 JavaCallArguments args;
342 args.push_oop(ha);
343 args.push_oop(hb);
344 methodHandle method(current, Universe::is_substitutable_method());
345 method->method_holder()->initialize(CHECK_false); // Ensure class ValueObjectMethods is initialized
346 JavaCalls::call(&result, method, &args, THREAD);
347 if (HAS_PENDING_EXCEPTION) {
348 // Something really bad happened because isSubstitutable() should not throw exceptions
349 // If it is an error, just let it propagate
350 // If it is an exception, wrap it into an InternalError
351 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
352 Handle e(THREAD, PENDING_EXCEPTION);
353 CLEAR_PENDING_EXCEPTION;
354 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
355 }
356 }
357 return result.get_jboolean();
358 JRT_END
359
360 // Quicken instance-of and check-cast bytecodes
361 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
362 // Force resolving; quicken the bytecode
363 LastFrameAccessor last_frame(current);
364 int which = last_frame.get_index_u2(Bytecodes::_checkcast);
365 ConstantPool* cpool = last_frame.method()->constants();
366 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
367 // program we might have seen an unquick'd bytecode in the interpreter but have another
368 // thread quicken the bytecode before we get here.
369 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
370 Klass* klass = cpool->klass_at(which, CHECK);
371 current->set_vm_result_metadata(klass);
372 JRT_END
373
374
375 //------------------------------------------------------------------------------------------------------------------------
376 // Exceptions
377
378 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,
379 const methodHandle& trap_method, int trap_bci) {
380 if (trap_method.not_null()) {
381 MethodData* trap_mdo = trap_method->method_data();
382 if (trap_mdo == nullptr) {
383 ExceptionMark em(current);
384 JavaThread* THREAD = current; // For exception macros.
385 Method::build_profiling_method_data(trap_method, THREAD);
386 if (HAS_PENDING_EXCEPTION) {
387 // Only metaspace OOM is expected. No Java code executed.
388 assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())),
389 "we expect only an OOM error here");
390 CLEAR_PENDING_EXCEPTION;
391 }
392 trap_mdo = trap_method->method_data();
393 // and fall through...
394 }
395 if (trap_mdo != nullptr) {
396 // Update per-method count of trap events. The interpreter
397 // is updating the MDO to simulate the effect of compiler traps.
398 Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
399 }
400 }
401 }
402
403 // Assume the compiler is (or will be) interested in this event.
404 // If necessary, create an MDO to hold the information, and record it.
405 void InterpreterRuntime::note_trap(JavaThread* current, int reason) {
406 assert(ProfileTraps, "call me only if profiling");
407 LastFrameAccessor last_frame(current);
408 methodHandle trap_method(current, last_frame.method());
409 int trap_bci = trap_method->bci_from(last_frame.bcp());
410 note_trap_inner(current, reason, trap_method, trap_bci);
411 }
412
413 static Handle get_preinitialized_exception(Klass* k, TRAPS) {
414 // get klass
415 InstanceKlass* klass = InstanceKlass::cast(k);
416 assert(klass->is_initialized(),
417 "this klass should have been initialized during VM initialization");
418 // create instance - do not call constructor since we may have no
419 // (java) stack space left (should assert constructor is empty)
420 Handle exception;
421 oop exception_oop = klass->allocate_instance(CHECK_(exception));
422 exception = Handle(THREAD, exception_oop);
423 if (StackTraceInThrowable) {
424 java_lang_Throwable::fill_in_stack_trace(exception);
425 }
426 return exception;
427 }
428
429 // Special handling for stack overflow: since we don't have any (java) stack
430 // space left we use the pre-allocated & pre-initialized StackOverflowError
431 // klass to create an stack overflow error instance. We do not call its
432 // constructor for the same reason (it is empty, anyway).
433 JRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* current))
434 Handle exception = get_preinitialized_exception(
435 vmClasses::StackOverflowError_klass(),
436 CHECK);
437 // Increment counter for hs_err file reporting
438 AtomicAccess::inc(&Exceptions::_stack_overflow_errors);
439 // Remove the ScopedValue bindings in case we got a StackOverflowError
440 // while we were trying to manipulate ScopedValue bindings.
441 current->clear_scopedValueBindings();
442 THROW_HANDLE(exception);
443 JRT_END
444
445 JRT_ENTRY(void, InterpreterRuntime::throw_delayed_StackOverflowError(JavaThread* current))
446 Handle exception = get_preinitialized_exception(
447 vmClasses::StackOverflowError_klass(),
448 CHECK);
449 java_lang_Throwable::set_message(exception(),
450 Universe::delayed_stack_overflow_error_message());
451 // Increment counter for hs_err file reporting
452 AtomicAccess::inc(&Exceptions::_stack_overflow_errors);
453 // Remove the ScopedValue bindings in case we got a StackOverflowError
454 // while we were trying to manipulate ScopedValue bindings.
455 current->clear_scopedValueBindings();
456 THROW_HANDLE(exception);
457 JRT_END
458
459 JRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* current, char* name, char* message))
460 // lookup exception klass
461 TempNewSymbol s = SymbolTable::new_symbol(name);
462 if (ProfileTraps) {
463 if (s == vmSymbols::java_lang_ArithmeticException()) {
464 note_trap(current, Deoptimization::Reason_div0_check);
465 } else if (s == vmSymbols::java_lang_NullPointerException()) {
466 note_trap(current, Deoptimization::Reason_null_check);
467 }
468 }
469 // create exception
470 Handle exception = Exceptions::new_exception(current, s, message);
471 current->set_vm_result_oop(exception());
472 JRT_END
473
474
475 JRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* current, char* name, oopDesc* obj))
476 // Produce the error message first because note_trap can safepoint
477 ResourceMark rm(current);
478 const char* klass_name = obj->klass()->external_name();
479 // lookup exception klass
480 TempNewSymbol s = SymbolTable::new_symbol(name);
481 if (ProfileTraps) {
482 if (s == vmSymbols::java_lang_ArrayStoreException()) {
483 note_trap(current, Deoptimization::Reason_array_check);
484 } else {
485 note_trap(current, Deoptimization::Reason_class_check);
486 }
487 }
488 // create exception, with klass name as detail message
489 Handle exception = Exceptions::new_exception(current, s, klass_name);
490 current->set_vm_result_oop(exception());
491 JRT_END
492
493 JRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* current, arrayOopDesc* a, jint index))
494 // Produce the error message first because note_trap can safepoint
495 ResourceMark rm(current);
496 stringStream ss;
497 ss.print("Index %d out of bounds for length %d", index, a->length());
498
499 if (ProfileTraps) {
500 note_trap(current, Deoptimization::Reason_range_check);
501 }
502
503 THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
504 JRT_END
505
506 JRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
507 JavaThread* current, oopDesc* obj))
508
509 // Produce the error message first because note_trap can safepoint
510 ResourceMark rm(current);
511 char* message = SharedRuntime::generate_class_cast_message(
512 current, obj->klass());
513
514 if (ProfileTraps) {
515 note_trap(current, Deoptimization::Reason_class_check);
516 }
517
518 // create exception
519 THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
520 JRT_END
521
522 // exception_handler_for_exception(...) returns the continuation address,
523 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
524 // The exception oop is returned to make sure it is preserved over GC (it
525 // is only on the stack if the exception was thrown explicitly via athrow).
526 // During this operation, the expression stack contains the values for the
527 // bci where the exception happened. If the exception was propagated back
528 // from a call, the expression stack contains the values for the bci at the
529 // invoke w/o arguments (i.e., as if one were inside the call).
530 // Note that the implementation of this method assumes it's only called when an exception has actually occured
531 JRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* current, oopDesc* exception))
532 // We get here after we have unwound from a callee throwing an exception
533 // into the interpreter. Any deferred stack processing is notified of
534 // the event via the StackWatermarkSet.
535 StackWatermarkSet::after_unwind(current);
536
537 LastFrameAccessor last_frame(current);
538 Handle h_exception(current, exception);
539 methodHandle h_method (current, last_frame.method());
540 constantPoolHandle h_constants(current, h_method->constants());
541 bool should_repeat;
542 int handler_bci;
543 int current_bci = last_frame.bci();
544
545 if (current->frames_to_pop_failed_realloc() > 0) {
546 // Allocation of scalar replaced object used in this frame
547 // failed. Unconditionally pop the frame.
548 current->dec_frames_to_pop_failed_realloc();
549 current->set_vm_result_oop(h_exception());
550 // If the method is synchronized we already unlocked the monitor
551 // during deoptimization so the interpreter needs to skip it when
552 // the frame is popped.
553 current->set_do_not_unlock_if_synchronized(true);
554 return Interpreter::remove_activation_entry();
555 }
556
557 // Need to do this check first since when _do_not_unlock_if_synchronized
558 // is set, we don't want to trigger any classloading which may make calls
559 // into java, or surprisingly find a matching exception handler for bci 0
560 // since at this moment the method hasn't been "officially" entered yet.
561 if (current->do_not_unlock_if_synchronized()) {
562 ResourceMark rm;
563 assert(current_bci == 0, "bci isn't zero for do_not_unlock_if_synchronized");
564 current->set_vm_result_oop(exception);
565 return Interpreter::remove_activation_entry();
566 }
567
568 do {
569 should_repeat = false;
570
571 // assertions
572 assert(h_exception.not_null(), "null exceptions should be handled by athrow");
573 // Check that exception is a subclass of Throwable.
574 assert(h_exception->is_a(vmClasses::Throwable_klass()),
575 "Exception not subclass of Throwable");
576
577 // tracing
578 if (log_is_enabled(Info, exceptions)) {
579 ResourceMark rm(current);
580 stringStream tempst;
581 tempst.print("interpreter method <%s>\n"
582 " at bci %d for thread " INTPTR_FORMAT " (%s)",
583 h_method->print_value_string(), current_bci, p2i(current), current->name());
584 Exceptions::log_exception(h_exception, tempst.as_string());
585 }
586 if (log_is_enabled(Info, exceptions, stacktrace)) {
587 Exceptions::log_exception_stacktrace(h_exception, h_method, current_bci);
588 }
589
590 // Don't go paging in something which won't be used.
591 // else if (extable->length() == 0) {
592 // // disabled for now - interpreter is not using shortcut yet
593 // // (shortcut is not to call runtime if we have no exception handlers)
594 // // warning("performance bug: should not call runtime if method has no exception handlers");
595 // }
596 // for AbortVMOnException flag
597 Exceptions::debug_check_abort(h_exception);
598
599 // exception handler lookup
600 Klass* klass = h_exception->klass();
601 handler_bci = Method::fast_exception_handler_bci_for(h_method, klass, current_bci, THREAD);
602 if (HAS_PENDING_EXCEPTION) {
603 // We threw an exception while trying to find the exception handler.
604 // Transfer the new exception to the exception handle which will
605 // be set into thread local storage, and do another lookup for an
606 // exception handler for this exception, this time starting at the
607 // BCI of the exception handler which caused the exception to be
608 // thrown (bug 4307310).
609 h_exception = Handle(THREAD, PENDING_EXCEPTION);
610 CLEAR_PENDING_EXCEPTION;
611 if (handler_bci >= 0) {
612 current_bci = handler_bci;
613 should_repeat = true;
614 }
615 }
616 } while (should_repeat == true);
617
618 #if INCLUDE_JVMCI
619 if (EnableJVMCI && h_method->method_data() != nullptr) {
620 ResourceMark rm(current);
621 MethodData* mdo = h_method->method_data();
622
623 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
624 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
625
626 ProfileData* pdata = mdo->allocate_bci_to_data(current_bci, nullptr);
627 if (pdata != nullptr && pdata->is_BitData()) {
628 BitData* bit_data = (BitData*) pdata;
629 bit_data->set_exception_seen();
630 }
631 }
632 #endif
633
634 // notify JVMTI of an exception throw; JVMTI will detect if this is a first
635 // time throw or a stack unwinding throw and accordingly notify the debugger
636 if (JvmtiExport::can_post_on_exceptions()) {
637 JvmtiExport::post_exception_throw(current, h_method(), last_frame.bcp(), h_exception());
638 }
639
640 address continuation = nullptr;
641 address handler_pc = nullptr;
642 if (handler_bci < 0 || !current->stack_overflow_state()->reguard_stack((address) &continuation)) {
643 // Forward exception to callee (leaving bci/bcp untouched) because (a) no
644 // handler in this method, or (b) after a stack overflow there is not yet
645 // enough stack space available to reprotect the stack.
646 continuation = Interpreter::remove_activation_entry();
647 #if COMPILER2_OR_JVMCI
648 // Count this for compilation purposes
649 h_method->interpreter_throwout_increment(THREAD);
650 #endif
651 } else {
652 // handler in this method => change bci/bcp to handler bci/bcp and continue there
653 handler_pc = h_method->code_base() + handler_bci;
654 h_method->set_exception_handler_entered(handler_bci); // profiling
655 #ifndef ZERO
656 set_bcp_and_mdp(handler_pc, current);
657 continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
658 #else
659 continuation = (address)(intptr_t) handler_bci;
660 #endif
661 }
662
663 // notify debugger of an exception catch
664 // (this is good for exceptions caught in native methods as well)
665 if (JvmtiExport::can_post_on_exceptions()) {
666 JvmtiExport::notice_unwind_due_to_exception(current, h_method(), handler_pc, h_exception(), (handler_pc != nullptr));
667 }
668
669 current->set_vm_result_oop(h_exception());
670 return continuation;
671 JRT_END
672
673
674 JRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* current))
675 assert(current->has_pending_exception(), "must only be called if there's an exception pending");
676 // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
677 JRT_END
678
679
680 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* current))
681 THROW(vmSymbols::java_lang_AbstractMethodError());
682 JRT_END
683
684 // This method is called from the "abstract_entry" of the interpreter.
685 // At that point, the arguments have already been removed from the stack
686 // and therefore we don't have the receiver object at our fingertips. (Though,
687 // on some platforms the receiver still resides in a register...). Thus,
688 // we have no choice but print an error message not containing the receiver
689 // type.
690 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
691 Method* missingMethod))
692 ResourceMark rm(current);
693 assert(missingMethod != nullptr, "sanity");
694 methodHandle m(current, missingMethod);
695 LinkResolver::throw_abstract_method_error(m, THREAD);
696 JRT_END
697
698 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
699 Klass* recvKlass,
700 Method* missingMethod))
701 ResourceMark rm(current);
702 methodHandle mh = methodHandle(current, missingMethod);
703 LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
704 JRT_END
705
706 JRT_ENTRY(void, InterpreterRuntime::throw_InstantiationError(JavaThread* current))
707 THROW(vmSymbols::java_lang_InstantiationError());
708 JRT_END
709
710
711 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
712 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
713 JRT_END
714
715 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
716 Klass* recvKlass,
717 Klass* interfaceKlass))
718 ResourceMark rm(current);
719 char buf[1000];
720 buf[0] = '\0';
721 jio_snprintf(buf, sizeof(buf),
722 "Class %s does not implement the requested interface %s",
723 recvKlass ? recvKlass->external_name() : "nullptr",
724 interfaceKlass ? interfaceKlass->external_name() : "nullptr");
725 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
726 JRT_END
727
728 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
729 THROW(vmSymbols::java_lang_NullPointerException());
730 JRT_END
731
732 //------------------------------------------------------------------------------------------------------------------------
733 // Fields
734 //
735
736 void InterpreterRuntime::resolve_get_put(Bytecodes::Code bytecode, TRAPS) {
737 JavaThread* current = THREAD;
738 LastFrameAccessor last_frame(current);
739 constantPoolHandle pool(current, last_frame.method()->constants());
740 methodHandle m(current, last_frame.method());
741
742 resolve_get_put(bytecode, last_frame.get_index_u2(bytecode), m, pool, ClassInitMode::init_preemptable, THREAD);
743 }
744
745 void InterpreterRuntime::resolve_get_put(Bytecodes::Code bytecode, int field_index,
746 methodHandle& m,
747 constantPoolHandle& pool,
748 ClassInitMode init_mode, TRAPS) {
749 fieldDescriptor info;
750 bool is_put = (bytecode == Bytecodes::_putfield || bytecode == Bytecodes::_nofast_putfield ||
751 bytecode == Bytecodes::_putstatic);
752 bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
753
754 {
755 JvmtiHideSingleStepping jhss(THREAD);
756 LinkResolver::resolve_field_access(info, pool, field_index, m, bytecode, init_mode, CHECK);
757 } // end JvmtiHideSingleStepping
758
759 // check if link resolution caused cpCache to be updated
760 if (pool->resolved_field_entry_at(field_index)->is_resolved(bytecode)) return;
761
762 // compute auxiliary field attributes
763 TosState state = as_TosState(info.field_type());
764
765 // Resolution of put instructions on final fields is delayed. That is required so that
766 // exceptions are thrown at the correct place (when the instruction is actually invoked).
767 // If we do not resolve an instruction in the current pass, leaving the put_code
768 // set to zero will cause the next put instruction to the same field to reresolve.
769
770 // Resolution of put instructions to final instance fields with invalid updates (i.e.,
771 // to final instance fields with updates originating from a method different than <init>)
772 // is inhibited. A putfield instruction targeting an instance final field must throw
773 // an IllegalAccessError if the instruction is not in an instance
774 // initializer method <init>. If resolution were not inhibited, a putfield
775 // in an initializer method could be resolved in the initializer. Subsequent
776 // putfield instructions to the same field would then use cached information.
777 // As a result, those instructions would not pass through the VM. That is,
778 // checks in resolve_field_access() would not be executed for those instructions
779 // and the required IllegalAccessError would not be thrown.
780 //
781 // Also, we need to delay resolving getstatic and putstatic instructions until the
782 // class is initialized. This is required so that access to the static
783 // field will call the initialization function every time until the class
784 // is completely initialized ala. in 2.17.5 in JVM Specification.
785 InstanceKlass* klass = info.field_holder();
786 bool uninitialized_static = is_static && !klass->is_initialized();
787 bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
788 info.has_initialized_final_update();
789 bool strict_static_final = info.is_strict() && info.is_static() && info.is_final();
790 assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
791
792 Bytecodes::Code get_code = (Bytecodes::Code)0;
793 Bytecodes::Code put_code = (Bytecodes::Code)0;
794 if (uninitialized_static && (info.is_strict_static_unset() || strict_static_final)) {
795 // During <clinit>, closely track the state of strict statics.
796 // 1. if we are reading an uninitialized strict static, throw
797 // 2. if we are writing one, clear the "unset" flag
798 //
799 // Note: If we were handling an attempted write of a null to a
800 // null-restricted strict static, we would NOT clear the "unset"
801 // flag.
802 assert(klass->is_being_initialized(), "else should have thrown");
803 assert(klass->is_reentrant_initialization(THREAD),
804 "<clinit> must be running in current thread");
805 klass->notify_strict_static_access(info.index(), is_put, CHECK);
806 assert(!info.is_strict_static_unset(), "after initialization, no unset flags");
807 } else if (!uninitialized_static || VM_Version::supports_fast_class_init_checks()) {
808 get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
809 if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
810 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
811 }
812 }
813
814 ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
815 entry->set_flags(info.access_flags().is_volatile(),
816 info.access_flags().is_final(),
817 info.is_flat(),
818 info.is_null_free_inline_type(),
819 info.has_null_marker());
820
821 entry->fill_in(info.field_holder(), info.offset(),
822 checked_cast<u2>(info.index()), checked_cast<u1>(state),
823 static_cast<u1>(get_code), static_cast<u1>(put_code));
824 }
825
826
827 //------------------------------------------------------------------------------------------------------------------------
828 // Synchronization
829 //
830 // The interpreter's synchronization code is factored out so that it can
831 // be shared by method invocation and synchronized blocks.
832 //%note synchronization_3
833
834 //%note monitor_1
835 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
836 #ifdef ASSERT
837 current->last_frame().interpreter_frame_verify_monitor(elem);
838 #endif
839 Handle h_obj(current, elem->obj());
840 assert(Universe::heap()->is_in_or_null(h_obj()),
841 "must be null or an object");
842 ObjectSynchronizer::enter(h_obj, elem->lock(), current);
843 assert(Universe::heap()->is_in_or_null(elem->obj()),
844 "must be null or an object");
845 #ifdef ASSERT
846 if (!current->preempting()) current->last_frame().interpreter_frame_verify_monitor(elem);
847 #endif
848 JRT_END
849
850 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
851 oop obj = elem->obj();
852 assert(Universe::heap()->is_in(obj), "must be an object");
853 // The object could become unlocked through a JNI call, which we have no other checks for.
854 // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
855 if (obj->is_unlocked()) {
856 if (CheckJNICalls) {
857 fatal("Object has been unlocked by JNI");
858 }
859 return;
860 }
861 ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
862 // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
863 // again at method exit or in the case of an exception.
864 elem->set_obj(nullptr);
865 JRT_END
866
867 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
868 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
869 JRT_END
870
871 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
872 // Returns an illegal exception to install into the current thread. The
873 // pending_exception flag is cleared so normal exception handling does not
874 // trigger. Any current installed exception will be overwritten. This
875 // method will be called during an exception unwind.
876
877 assert(!HAS_PENDING_EXCEPTION, "no pending exception");
878 Handle exception(current, current->vm_result_oop());
879 assert(exception() != nullptr, "vm result should be set");
880 current->set_vm_result_oop(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
881 exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
882 current->set_vm_result_oop(exception());
883 JRT_END
884
885 JRT_ENTRY(void, InterpreterRuntime::throw_identity_exception(JavaThread* current, oopDesc* obj))
886 Klass* klass = cast_to_oop(obj)->klass();
887 ResourceMark rm(THREAD);
888 const char* desc = "Cannot synchronize on an instance of value class ";
889 const char* className = klass->external_name();
890 size_t msglen = strlen(desc) + strlen(className) + 1;
891 char* message = NEW_RESOURCE_ARRAY(char, msglen);
892 if (nullptr == message) {
893 // Out of memory: can't create detailed error message
894 THROW_MSG(vmSymbols::java_lang_IdentityException(), className);
895 } else {
896 jio_snprintf(message, msglen, "%s%s", desc, className);
897 THROW_MSG(vmSymbols::java_lang_IdentityException(), message);
898 }
899 JRT_END
900
901 //------------------------------------------------------------------------------------------------------------------------
902 // Invokes
903
904 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
905 return method->orig_bytecode_at(method->bci_from(bcp));
906 JRT_END
907
908 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
909 method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
910 JRT_END
911
912 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
913 JvmtiExport::post_raw_breakpoint(current, method, bcp);
914 JRT_END
915
916 void InterpreterRuntime::resolve_invoke(Bytecodes::Code bytecode, TRAPS) {
917 JavaThread* current = THREAD;
918 LastFrameAccessor last_frame(current);
919 // extract receiver from the outgoing argument list if necessary
920 Handle receiver(current, nullptr);
921 if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface ||
922 bytecode == Bytecodes::_invokespecial) {
923 ResourceMark rm(current);
924 methodHandle m (current, last_frame.method());
925 Bytecode_invoke call(m, last_frame.bci());
926 Symbol* signature = call.signature();
927 receiver = Handle(current, last_frame.callee_receiver(signature));
928
929 assert(Universe::heap()->is_in_or_null(receiver()),
930 "sanity check");
931 assert(receiver.is_null() ||
932 !Universe::heap()->is_in(receiver->klass()),
933 "sanity check");
934 }
935
936 // resolve method
937 CallInfo info;
938 constantPoolHandle pool(current, last_frame.method()->constants());
939
940 methodHandle resolved_method;
941
942 int method_index = last_frame.get_index_u2(bytecode);
943 {
944 JvmtiHideSingleStepping jhss(current);
945 LinkResolver::resolve_invoke(info, receiver, pool,
946 method_index, bytecode,
947 ClassInitMode::init_preemptable, THREAD);
948
949 if (HAS_PENDING_EXCEPTION) {
950 if (ProfileTraps && PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_NullPointerException()) {
951 // Preserve the original exception across the call to note_trap()
952 PreserveExceptionMark pm(current);
953 // Recording the trap will help the compiler to potentially recognize this exception as "hot"
954 note_trap(current, Deoptimization::Reason_null_check);
955 }
956 return;
957 }
958
959 resolved_method = methodHandle(current, info.resolved_method());
960 } // end JvmtiHideSingleStepping
961
962 update_invoke_cp_cache_entry(info, bytecode, resolved_method, pool, method_index);
963 }
964
965 void InterpreterRuntime::update_invoke_cp_cache_entry(CallInfo& info, Bytecodes::Code bytecode,
966 methodHandle& resolved_method,
967 constantPoolHandle& pool,
968 int method_index) {
969 // Don't allow safepoints until the method is cached.
970 NoSafepointVerifier nsv;
971
972 // check if link resolution caused cpCache to be updated
973 ConstantPoolCache* cache = pool->cache();
974 if (cache->resolved_method_entry_at(method_index)->is_resolved(bytecode)) return;
975
976 #ifdef ASSERT
977 if (bytecode == Bytecodes::_invokeinterface) {
978 if (resolved_method->method_holder() == vmClasses::Object_klass()) {
979 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
980 // (see also CallInfo::set_interface for details)
981 assert(info.call_kind() == CallInfo::vtable_call ||
982 info.call_kind() == CallInfo::direct_call, "");
983 assert(resolved_method->is_final() || info.has_vtable_index(),
984 "should have been set already");
985 } else if (!resolved_method->has_itable_index()) {
986 // Resolved something like CharSequence.toString. Use vtable not itable.
987 assert(info.call_kind() != CallInfo::itable_call, "");
988 } else {
989 // Setup itable entry
990 assert(info.call_kind() == CallInfo::itable_call, "");
991 int index = resolved_method->itable_index();
992 assert(info.itable_index() == index, "");
993 }
994 } else if (bytecode == Bytecodes::_invokespecial) {
995 assert(info.call_kind() == CallInfo::direct_call, "must be direct call");
996 } else {
997 assert(info.call_kind() == CallInfo::direct_call ||
998 info.call_kind() == CallInfo::vtable_call, "");
999 }
1000 #endif
1001 // Get sender and only set cpCache entry to resolved if it is not an
1002 // interface. The receiver for invokespecial calls within interface
1003 // methods must be checked for every call.
1004 InstanceKlass* sender = pool->pool_holder();
1005
1006 switch (info.call_kind()) {
1007 case CallInfo::direct_call:
1008 cache->set_direct_call(bytecode, method_index, resolved_method, sender->is_interface());
1009 break;
1010 case CallInfo::vtable_call:
1011 cache->set_vtable_call(bytecode, method_index, resolved_method, info.vtable_index());
1012 break;
1013 case CallInfo::itable_call:
1014 cache->set_itable_call(
1015 bytecode,
1016 method_index,
1017 info.resolved_klass(),
1018 resolved_method,
1019 info.itable_index());
1020 break;
1021 default: ShouldNotReachHere();
1022 }
1023 }
1024
1025 void InterpreterRuntime::cds_resolve_invoke(Bytecodes::Code bytecode, int method_index,
1026 constantPoolHandle& pool, TRAPS) {
1027 LinkInfo link_info(pool, method_index, bytecode, CHECK);
1028
1029 if (!link_info.resolved_klass()->is_instance_klass() || InstanceKlass::cast(link_info.resolved_klass())->is_linked()) {
1030 CallInfo call_info;
1031 switch (bytecode) {
1032 case Bytecodes::_invokevirtual: LinkResolver::cds_resolve_virtual_call (call_info, link_info, CHECK); break;
1033 case Bytecodes::_invokeinterface: LinkResolver::cds_resolve_interface_call(call_info, link_info, CHECK); break;
1034 case Bytecodes::_invokestatic: LinkResolver::cds_resolve_static_call (call_info, link_info, CHECK); break;
1035 case Bytecodes::_invokespecial: LinkResolver::cds_resolve_special_call (call_info, link_info, CHECK); break;
1036
1037 default: fatal("Unimplemented: %s", Bytecodes::name(bytecode));
1038 }
1039 methodHandle resolved_method(THREAD, call_info.resolved_method());
1040 guarantee(resolved_method->method_holder()->is_linked(), "");
1041 update_invoke_cp_cache_entry(call_info, bytecode, resolved_method, pool, method_index);
1042 } else {
1043 // FIXME: why a shared class is not linked yet?
1044 // Can't link it here since there are no guarantees it'll be prelinked on the next run.
1045 ResourceMark rm;
1046 InstanceKlass* resolved_iklass = InstanceKlass::cast(link_info.resolved_klass());
1047 log_info(aot, resolve)("Not resolved: class not linked: %s %s %s",
1048 resolved_iklass->in_aot_cache() ? "in_aot_cache" : "",
1049 resolved_iklass->init_state_name(),
1050 resolved_iklass->external_name());
1051 }
1052 }
1053
1054 // First time execution: Resolve symbols, create a permanent MethodType object.
1055 void InterpreterRuntime::resolve_invokehandle(TRAPS) {
1056 JavaThread* current = THREAD;
1057 const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
1058 LastFrameAccessor last_frame(current);
1059
1060 // resolve method
1061 CallInfo info;
1062 constantPoolHandle pool(current, last_frame.method()->constants());
1063 int method_index = last_frame.get_index_u2(bytecode);
1064 {
1065 JvmtiHideSingleStepping jhss(current);
1066 JavaThread* THREAD = current; // For exception macros.
1067 LinkResolver::resolve_invoke(info, Handle(), pool,
1068 method_index, bytecode,
1069 CHECK);
1070 } // end JvmtiHideSingleStepping
1071
1072 pool->cache()->set_method_handle(method_index, info);
1073 }
1074
1075 void InterpreterRuntime::cds_resolve_invokehandle(int raw_index,
1076 constantPoolHandle& pool, TRAPS) {
1077 const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
1078 CallInfo info;
1079 LinkResolver::resolve_invoke(info, Handle(), pool, raw_index, bytecode, CHECK);
1080
1081 pool->cache()->set_method_handle(raw_index, info);
1082 }
1083
1084 // First time execution: Resolve symbols, create a permanent CallSite object.
1085 void InterpreterRuntime::resolve_invokedynamic(TRAPS) {
1086 JavaThread* current = THREAD;
1087 LastFrameAccessor last_frame(current);
1088 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
1089
1090 // resolve method
1091 CallInfo info;
1092 constantPoolHandle pool(current, last_frame.method()->constants());
1093 int index = last_frame.get_index_u4(bytecode);
1094 {
1095 JvmtiHideSingleStepping jhss(current);
1096 JavaThread* THREAD = current; // For exception macros.
1097 LinkResolver::resolve_invoke(info, Handle(), pool,
1098 index, bytecode, CHECK);
1099 } // end JvmtiHideSingleStepping
1100
1101 pool->cache()->set_dynamic_call(info, index);
1102 }
1103
1104 void InterpreterRuntime::cds_resolve_invokedynamic(int raw_index,
1105 constantPoolHandle& pool, TRAPS) {
1106 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
1107 CallInfo info;
1108 LinkResolver::resolve_invoke(info, Handle(), pool, raw_index, bytecode, CHECK);
1109 pool->cache()->set_dynamic_call(info, raw_index);
1110 }
1111
1112 // This function is the interface to the assembly code. It returns the resolved
1113 // cpCache entry. This doesn't safepoint, but the helper routines safepoint.
1114 // This function will check for redefinition!
1115 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
1116 switch (bytecode) {
1117 case Bytecodes::_getstatic:
1118 case Bytecodes::_putstatic:
1119 case Bytecodes::_getfield:
1120 case Bytecodes::_putfield:
1121 resolve_get_put(bytecode, CHECK_AND_CLEAR_PREEMPTED);
1122 break;
1123 case Bytecodes::_invokevirtual:
1124 case Bytecodes::_invokespecial:
1125 case Bytecodes::_invokestatic:
1126 case Bytecodes::_invokeinterface:
1127 resolve_invoke(bytecode, CHECK_AND_CLEAR_PREEMPTED);
1128 break;
1129 case Bytecodes::_invokehandle:
1130 resolve_invokehandle(THREAD);
1131 break;
1132 case Bytecodes::_invokedynamic:
1133 resolve_invokedynamic(THREAD);
1134 break;
1135 default:
1136 fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
1137 break;
1138 }
1139 }
1140 JRT_END
1141
1142 //------------------------------------------------------------------------------------------------------------------------
1143 // Miscellaneous
1144
1145
1146 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* current, address branch_bcp) {
1147 // Enable WXWrite: the function is called directly by interpreter.
1148 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1149
1150 // frequency_counter_overflow_inner can throw async exception.
1151 nmethod* nm = frequency_counter_overflow_inner(current, branch_bcp);
1152 assert(branch_bcp != nullptr || nm == nullptr, "always returns null for non OSR requests");
1153 if (branch_bcp != nullptr && nm != nullptr) {
1154 // This was a successful request for an OSR nmethod. Because
1155 // frequency_counter_overflow_inner ends with a safepoint check,
1156 // nm could have been unloaded so look it up again. It's unsafe
1157 // to examine nm directly since it might have been freed and used
1158 // for something else.
1159 LastFrameAccessor last_frame(current);
1160 Method* method = last_frame.method();
1161 int bci = method->bci_from(last_frame.bcp());
1162 nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
1163 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1164 if (nm != nullptr) {
1165 // in case the transition passed a safepoint we need to barrier this again
1166 if (!bs_nm->nmethod_osr_entry_barrier(nm)) {
1167 nm = nullptr;
1168 }
1169 }
1170 }
1171 if (nm != nullptr && current->is_interp_only_mode()) {
1172 // Normally we never get an nm if is_interp_only_mode() is true, because
1173 // policy()->event has a check for this and won't compile the method when
1174 // true. However, it's possible for is_interp_only_mode() to become true
1175 // during the compilation. We don't want to return the nm in that case
1176 // because we want to continue to execute interpreted.
1177 nm = nullptr;
1178 }
1179 #ifndef PRODUCT
1180 if (TraceOnStackReplacement) {
1181 if (nm != nullptr) {
1182 tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", p2i(nm->osr_entry()));
1183 nm->print();
1184 }
1185 }
1186 #endif
1187 return nm;
1188 }
1189
1190 JRT_ENTRY(nmethod*,
1191 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* current, address branch_bcp))
1192 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
1193 // flag, in case this method triggers classloading which will call into Java.
1194 UnlockFlagSaver fs(current);
1195
1196 LastFrameAccessor last_frame(current);
1197 assert(last_frame.is_interpreted_frame(), "must come from interpreter");
1198 methodHandle method(current, last_frame.method());
1199 const int branch_bci = branch_bcp != nullptr ? method->bci_from(branch_bcp) : InvocationEntryBci;
1200 const int bci = branch_bcp != nullptr ? method->bci_from(last_frame.bcp()) : InvocationEntryBci;
1201
1202 nmethod* osr_nm = CompilationPolicy::event(method, method, branch_bci, bci, CompLevel_none, nullptr, CHECK_NULL);
1203
1204 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1205 if (osr_nm != nullptr) {
1206 if (!bs_nm->nmethod_osr_entry_barrier(osr_nm)) {
1207 osr_nm = nullptr;
1208 }
1209 }
1210 return osr_nm;
1211 JRT_END
1212
1213 JRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
1214 assert(ProfileInterpreter, "must be profiling interpreter");
1215 int bci = method->bci_from(cur_bcp);
1216 MethodData* mdo = method->method_data();
1217 if (mdo == nullptr) return 0;
1218 return mdo->bci_to_di(bci);
1219 JRT_END
1220
1221 #ifdef ASSERT
1222 JRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
1223 assert(ProfileInterpreter, "must be profiling interpreter");
1224
1225 MethodData* mdo = method->method_data();
1226 assert(mdo != nullptr, "must not be null");
1227
1228 int bci = method->bci_from(bcp);
1229
1230 address mdp2 = mdo->bci_to_dp(bci);
1231 if (mdp != mdp2) {
1232 ResourceMark rm;
1233 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci);
1234 int current_di = mdo->dp_to_di(mdp);
1235 int expected_di = mdo->dp_to_di(mdp2);
1236 tty->print_cr(" actual di %d expected di %d", current_di, expected_di);
1237 int expected_approx_bci = mdo->data_at(expected_di)->bci();
1238 int approx_bci = -1;
1239 if (current_di >= 0) {
1240 approx_bci = mdo->data_at(current_di)->bci();
1241 }
1242 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci);
1243 mdo->print_on(tty);
1244 method->print_codes();
1245 }
1246 assert(mdp == mdp2, "wrong mdp");
1247 JRT_END
1248 #endif // ASSERT
1249
1250 JRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* current, int return_bci))
1251 assert(ProfileInterpreter, "must be profiling interpreter");
1252 ResourceMark rm(current);
1253 LastFrameAccessor last_frame(current);
1254 assert(last_frame.is_interpreted_frame(), "must come from interpreter");
1255 MethodData* h_mdo = last_frame.method()->method_data();
1256
1257 // Grab a lock to ensure atomic access to setting the return bci and
1258 // the displacement. This can block and GC, invalidating all naked oops.
1259 MutexLocker ml(RetData_lock);
1260
1261 // ProfileData is essentially a wrapper around a derived oop, so we
1262 // need to take the lock before making any ProfileData structures.
1263 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(last_frame.mdp()));
1264 guarantee(data != nullptr, "profile data must be valid");
1265 RetData* rdata = data->as_RetData();
1266 address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
1267 last_frame.set_mdp(new_mdp);
1268 JRT_END
1269
1270 JRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* current, Method* m))
1271 return Method::build_method_counters(current, m);
1272 JRT_END
1273
1274
1275 JRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* current))
1276 // We used to need an explicit preserve_arguments here for invoke bytecodes. However,
1277 // stack traversal automatically takes care of preserving arguments for invoke, so
1278 // this is no longer needed.
1279
1280 // JRT_END does an implicit safepoint check, hence we are guaranteed to block
1281 // if this is called during a safepoint
1282
1283 if (JvmtiExport::should_post_single_step()) {
1284 // This function is called by the interpreter when single stepping. Such single
1285 // stepping could unwind a frame. Then, it is important that we process any frames
1286 // that we might return into.
1287 StackWatermarkSet::before_unwind(current);
1288
1289 // We are called during regular safepoints and when the VM is
1290 // single stepping. If any thread is marked for single stepping,
1291 // then we may have JVMTI work to do.
1292 LastFrameAccessor last_frame(current);
1293 JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1294 }
1295 JRT_END
1296
1297 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1298 assert(current == JavaThread::current(), "pre-condition");
1299 JFR_ONLY(Jfr::check_and_process_sample_request(current);)
1300 // This function is called by the interpreter when the return poll found a reason
1301 // to call the VM. The reason could be that we are returning into a not yet safe
1302 // to access frame. We handle that below.
1303 // Note that this path does not check for single stepping, because we do not want
1304 // to single step when unwinding frames for an exception being thrown. Instead,
1305 // such single stepping code will use the safepoint table, which will use the
1306 // InterpreterRuntime::at_safepoint callback.
1307 StackWatermarkSet::before_unwind(current);
1308 JRT_END
1309
1310 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1311 ResolvedFieldEntry *entry))
1312
1313 assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1314 // check the access_flags for the field in the klass
1315
1316 InstanceKlass* ik = entry->field_holder();
1317 int index = entry->field_index();
1318 if (!ik->field_status(index).is_access_watched()) return;
1319
1320 bool is_static = (obj == nullptr);
1321 bool is_flat = entry->is_flat();
1322 HandleMark hm(current);
1323
1324 Handle h_obj;
1325 if (!is_static) {
1326 // non-static field accessors have an object, but we need a handle
1327 h_obj = Handle(current, obj);
1328 }
1329 InstanceKlass* field_holder = entry->field_holder(); // HERE
1330 jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static, is_flat);
1331 LastFrameAccessor last_frame(current);
1332 JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1333 JRT_END
1334
1335 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1336 ResolvedFieldEntry *entry, jvalue *value))
1337
1338 assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1339 InstanceKlass* ik = entry->field_holder();
1340
1341 // check the access_flags for the field in the klass
1342 int index = entry->field_index();
1343 // bail out if field modifications are not watched
1344 if (!ik->field_status(index).is_modification_watched()) return;
1345
1346 char sig_type = '\0';
1347
1348 switch((TosState)entry->tos_state()) {
1349 case btos: sig_type = JVM_SIGNATURE_BYTE; break;
1350 case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1351 case ctos: sig_type = JVM_SIGNATURE_CHAR; break;
1352 case stos: sig_type = JVM_SIGNATURE_SHORT; break;
1353 case itos: sig_type = JVM_SIGNATURE_INT; break;
1354 case ftos: sig_type = JVM_SIGNATURE_FLOAT; break;
1355 case atos: sig_type = JVM_SIGNATURE_CLASS; break;
1356 case ltos: sig_type = JVM_SIGNATURE_LONG; break;
1357 case dtos: sig_type = JVM_SIGNATURE_DOUBLE; break;
1358 default: ShouldNotReachHere(); return;
1359 }
1360
1361 bool is_static = (obj == nullptr);
1362 bool is_flat = entry->is_flat();
1363
1364 HandleMark hm(current);
1365 jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static, is_flat);
1366 jvalue fvalue;
1367 #ifdef _LP64
1368 fvalue = *value;
1369 #else
1370 // Long/double values are stored unaligned and also noncontiguously with
1371 // tagged stacks. We can't just do a simple assignment even in the non-
1372 // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1373 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1374 // We assume that the two halves of longs/doubles are stored in interpreter
1375 // stack slots in platform-endian order.
1376 jlong_accessor u;
1377 jint* newval = (jint*)value;
1378 u.words[0] = newval[0];
1379 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1380 fvalue.j = u.long_value;
1381 #endif // _LP64
1382
1383 Handle h_obj;
1384 if (!is_static) {
1385 // non-static field accessors have an object, but we need a handle
1386 h_obj = Handle(current, obj);
1387 }
1388
1389 LastFrameAccessor last_frame(current);
1390 JvmtiExport::post_raw_field_modification(current, last_frame.method(), last_frame.bcp(), ik, h_obj,
1391 fid, sig_type, &fvalue);
1392 JRT_END
1393
1394 JRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread* current))
1395 LastFrameAccessor last_frame(current);
1396 JvmtiExport::post_method_entry(current, last_frame.method(), last_frame.get_frame());
1397 JRT_END
1398
1399
1400 // This is a JRT_BLOCK_ENTRY because we have to stash away the return oop
1401 // before transitioning to VM, and restore it after transitioning back
1402 // to Java. The return oop at the top-of-stack, is not walked by the GC.
1403 JRT_BLOCK_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread* current))
1404 LastFrameAccessor last_frame(current);
1405 JvmtiExport::post_method_exit(current, last_frame.method(), last_frame.get_frame());
1406 JRT_END
1407
1408 JRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1409 {
1410 return (Interpreter::contains(Continuation::get_top_return_pc_post_barrier(JavaThread::current(), pc)) ? 1 : 0);
1411 }
1412 JRT_END
1413
1414
1415 // Implementation of SignatureHandlerLibrary
1416
1417 #ifndef SHARING_FAST_NATIVE_FINGERPRINTS
1418 // Dummy definition (else normalization method is defined in CPU
1419 // dependent code)
1420 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
1421 return fingerprint;
1422 }
1423 #endif
1424
1425 address SignatureHandlerLibrary::set_handler_blob() {
1426 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1427 if (handler_blob == nullptr) {
1428 return nullptr;
1429 }
1430 address handler = handler_blob->code_begin();
1431 _handler_blob = handler_blob;
1432 _handler = handler;
1433 return handler;
1434 }
1435
1436 void SignatureHandlerLibrary::initialize() {
1437 if (_fingerprints != nullptr) {
1438 return;
1439 }
1440 if (set_handler_blob() == nullptr) {
1441 vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1442 }
1443
1444 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1445 SignatureHandlerLibrary::buffer_size);
1446 _buffer = bb->code_begin();
1447
1448 _fingerprints = new (mtCode) GrowableArray<uint64_t>(32, mtCode);
1449 _handlers = new (mtCode) GrowableArray<address>(32, mtCode);
1450 }
1451
1452 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1453 address handler = _handler;
1454 int insts_size = buffer->pure_insts_size();
1455 if (handler + insts_size > _handler_blob->code_end()) {
1456 // get a new handler blob
1457 handler = set_handler_blob();
1458 }
1459 if (handler != nullptr) {
1460 memcpy(handler, buffer->insts_begin(), insts_size);
1461 pd_set_handler(handler);
1462 ICache::invalidate_range(handler, insts_size);
1463 _handler = handler + insts_size;
1464 }
1465 return handler;
1466 }
1467
1468 void SignatureHandlerLibrary::add(const methodHandle& method) {
1469 if (method->signature_handler() == nullptr) {
1470 // use slow signature handler if we can't do better
1471 int handler_index = -1;
1472 // check if we can use customized (fast) signature handler
1473 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::fp_max_size_of_parameters) {
1474 // use customized signature handler
1475 MutexLocker mu(SignatureHandlerLibrary_lock);
1476 // make sure data structure is initialized
1477 initialize();
1478 // lookup method signature's fingerprint
1479 uint64_t fingerprint = Fingerprinter(method).fingerprint();
1480 // allow CPU dependent code to optimize the fingerprints for the fast handler
1481 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1482 handler_index = _fingerprints->find(fingerprint);
1483 // create handler if necessary
1484 if (handler_index < 0) {
1485 ResourceMark rm;
1486 ptrdiff_t align_offset = align_up(_buffer, CodeEntryAlignment) - (address)_buffer;
1487 CodeBuffer buffer((address)(_buffer + align_offset),
1488 checked_cast<int>(SignatureHandlerLibrary::buffer_size - align_offset));
1489 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1490 // copy into code heap
1491 address handler = set_handler(&buffer);
1492 if (handler == nullptr) {
1493 // use slow signature handler (without memorizing it in the fingerprints)
1494 } else {
1495 // debugging support
1496 if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) {
1497 ttyLocker ttyl;
1498 tty->cr();
1499 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1500 _handlers->length(),
1501 (method->is_static() ? "static" : "receiver"),
1502 method->name_and_sig_as_C_string(),
1503 fingerprint,
1504 buffer.insts_size());
1505 if (buffer.insts_size() > 0) {
1506 Disassembler::decode(handler, handler + buffer.insts_size(), tty
1507 NOT_PRODUCT(COMMA &buffer.asm_remarks()));
1508 }
1509 #ifndef PRODUCT
1510 address rh_begin = Interpreter::result_handler(method()->result_type());
1511 if (CodeCache::contains(rh_begin)) {
1512 // else it might be special platform dependent values
1513 tty->print_cr(" --- associated result handler ---");
1514 address rh_end = rh_begin;
1515 while (*(int*)rh_end != 0) {
1516 rh_end += sizeof(int);
1517 }
1518 Disassembler::decode(rh_begin, rh_end);
1519 } else {
1520 tty->print_cr(" associated result handler: " PTR_FORMAT, p2i(rh_begin));
1521 }
1522 #endif
1523 }
1524 // add handler to library
1525 _fingerprints->append(fingerprint);
1526 _handlers->append(handler);
1527 // set handler index
1528 assert(_fingerprints->length() == _handlers->length(), "sanity check");
1529 handler_index = _fingerprints->length() - 1;
1530 }
1531 }
1532 // Set handler under SignatureHandlerLibrary_lock
1533 if (handler_index < 0) {
1534 // use generic signature handler
1535 method->set_signature_handler(Interpreter::slow_signature_handler());
1536 } else {
1537 // set handler
1538 method->set_signature_handler(_handlers->at(handler_index));
1539 }
1540 } else {
1541 DEBUG_ONLY(JavaThread::current()->check_possible_safepoint());
1542 // use generic signature handler
1543 method->set_signature_handler(Interpreter::slow_signature_handler());
1544 }
1545 }
1546 #ifdef ASSERT
1547 int handler_index = -1;
1548 int fingerprint_index = -2;
1549 {
1550 // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1551 // in any way if accessed from multiple threads. To avoid races with another
1552 // thread which may change the arrays in the above, mutex protected block, we
1553 // have to protect this read access here with the same mutex as well!
1554 MutexLocker mu(SignatureHandlerLibrary_lock);
1555 if (_handlers != nullptr) {
1556 handler_index = _handlers->find(method->signature_handler());
1557 uint64_t fingerprint = Fingerprinter(method).fingerprint();
1558 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1559 fingerprint_index = _fingerprints->find(fingerprint);
1560 }
1561 }
1562 assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1563 handler_index == fingerprint_index, "sanity check");
1564 #endif // ASSERT
1565 }
1566
1567 BufferBlob* SignatureHandlerLibrary::_handler_blob = nullptr;
1568 address SignatureHandlerLibrary::_handler = nullptr;
1569 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = nullptr;
1570 GrowableArray<address>* SignatureHandlerLibrary::_handlers = nullptr;
1571 address SignatureHandlerLibrary::_buffer = nullptr;
1572
1573
1574 JRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* current, Method* method))
1575 methodHandle m(current, method);
1576 assert(m->is_native(), "sanity check");
1577 // lookup native function entry point if it doesn't exist
1578 if (!m->has_native_function()) {
1579 NativeLookup::lookup(m, CHECK);
1580 }
1581 // make sure signature handler is installed
1582 SignatureHandlerLibrary::add(m);
1583 // The interpreter entry point checks the signature handler first,
1584 // before trying to fetch the native entry point and klass mirror.
1585 // We must set the signature handler last, so that multiple processors
1586 // preparing the same method will be sure to see non-null entry & mirror.
1587 JRT_END
1588
1589 #if defined(AMD64) || defined(ARM)
1590 JRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* current, void* src_address, void* dest_address))
1591 assert(current == JavaThread::current(), "pre-condition");
1592 if (src_address == dest_address) {
1593 return;
1594 }
1595 ResourceMark rm;
1596 LastFrameAccessor last_frame(current);
1597 assert(last_frame.is_interpreted_frame(), "");
1598 jint bci = last_frame.bci();
1599 methodHandle mh(current, last_frame.method());
1600 Bytecode_invoke invoke(mh, bci);
1601 ArgumentSizeComputer asc(invoke.signature());
1602 int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1603 Copy::conjoint_jbytes(src_address, dest_address,
1604 size_of_arguments * Interpreter::stackElementSize);
1605 JRT_END
1606 #endif
1607
1608 #if INCLUDE_JVMTI
1609 // This is a support of the JVMTI PopFrame interface.
1610 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1611 // and return it as a vm_result_oop so that it can be reloaded in the list of invokestatic parameters.
1612 // The member_name argument is a saved reference (in local#0) to the member_name.
1613 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle.
1614 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated.
1615 JRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* current, address member_name,
1616 Method* method, address bcp))
1617 Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1618 if (code != Bytecodes::_invokestatic) {
1619 return;
1620 }
1621 ConstantPool* cpool = method->constants();
1622 int cp_index = Bytes::get_native_u2(bcp + 1);
1623 Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index, code));
1624 Symbol* mname = cpool->name_ref_at(cp_index, code);
1625
1626 if (MethodHandles::has_member_arg(cname, mname)) {
1627 oop member_name_oop = cast_to_oop(member_name);
1628 if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) {
1629 // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated.
1630 member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop);
1631 }
1632 current->set_vm_result_oop(member_name_oop);
1633 } else {
1634 current->set_vm_result_oop(nullptr);
1635 }
1636 JRT_END
1637 #endif // INCLUDE_JVMTI
1638
1639 #ifndef PRODUCT
1640 // This must be a JRT_LEAF function because the interpreter must save registers on x86 to
1641 // call this, which changes rsp and makes the interpreter's expression stack not walkable.
1642 // The generated code still uses call_VM because that will set up the frame pointer for
1643 // bcp and method.
1644 JRT_LEAF(intptr_t, InterpreterRuntime::trace_bytecode(JavaThread* current, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2))
1645 assert(current == JavaThread::current(), "pre-condition");
1646 LastFrameAccessor last_frame(current);
1647 assert(last_frame.is_interpreted_frame(), "must be an interpreted frame");
1648 methodHandle mh(current, last_frame.method());
1649 BytecodeTracer::trace_interpreter(mh, last_frame.bcp(), tos, tos2);
1650 return preserve_this_value;
1651 JRT_END
1652 #endif // !PRODUCT
1653
1654 #ifdef ASSERT
1655 bool InterpreterRuntime::is_preemptable_call(address entry_point) {
1656 return entry_point == CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter) ||
1657 entry_point == CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache) ||
1658 entry_point == CAST_FROM_FN_PTR(address, InterpreterRuntime::_new);
1659 }
1660 #endif // ASSERT