1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/javaClasses.inline.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "classfile/vmClasses.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "code/codeCache.hpp"
30 #include "compiler/compilationPolicy.hpp"
31 #include "compiler/compileBroker.hpp"
32 #include "compiler/disassembler.hpp"
33 #include "gc/shared/barrierSetNMethod.hpp"
34 #include "gc/shared/collectedHeap.hpp"
35 #include "interpreter/bytecodeTracer.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "interpreter/interpreterRuntime.hpp"
38 #include "interpreter/linkResolver.hpp"
39 #include "interpreter/templateTable.hpp"
40 #include "jvm_io.h"
41 #include "logging/log.hpp"
42 #include "memory/oopFactory.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "oops/constantPool.inline.hpp"
46 #include "oops/cpCache.inline.hpp"
47 #include "oops/instanceKlass.inline.hpp"
48 #include "oops/klass.inline.hpp"
49 #include "oops/method.inline.hpp"
50 #include "oops/methodData.hpp"
51 #include "oops/objArrayKlass.hpp"
52 #include "oops/objArrayOop.inline.hpp"
53 #include "oops/oop.inline.hpp"
54 #include "oops/symbol.hpp"
55 #include "prims/jvmtiExport.hpp"
56 #include "prims/methodHandles.hpp"
57 #include "prims/nativeLookup.hpp"
58 #include "runtime/atomicAccess.hpp"
59 #include "runtime/continuation.hpp"
60 #include "runtime/deoptimization.hpp"
61 #include "runtime/fieldDescriptor.inline.hpp"
62 #include "runtime/frame.inline.hpp"
63 #include "runtime/handles.inline.hpp"
64 #include "runtime/icache.hpp"
65 #include "runtime/interfaceSupport.inline.hpp"
66 #include "runtime/java.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/jfieldIDWorkaround.hpp"
69 #include "runtime/osThread.hpp"
70 #include "runtime/sharedRuntime.hpp"
71 #include "runtime/stackWatermarkSet.hpp"
72 #include "runtime/stubRoutines.hpp"
73 #include "runtime/synchronizer.hpp"
74 #include "utilities/align.hpp"
75 #include "utilities/checkedCast.hpp"
76 #include "utilities/copy.hpp"
77 #include "utilities/events.hpp"
78 #if INCLUDE_JFR
79 #include "jfr/jfr.inline.hpp"
80 #endif
81
82 // Helper class to access current interpreter state
83 class LastFrameAccessor : public StackObj {
84 frame _last_frame;
85 public:
86 LastFrameAccessor(JavaThread* current) {
87 assert(current == Thread::current(), "sanity");
88 _last_frame = current->last_frame();
89 }
90 bool is_interpreted_frame() const { return _last_frame.is_interpreted_frame(); }
91 Method* method() const { return _last_frame.interpreter_frame_method(); }
92 address bcp() const { return _last_frame.interpreter_frame_bcp(); }
93 int bci() const { return _last_frame.interpreter_frame_bci(); }
94 address mdp() const { return _last_frame.interpreter_frame_mdp(); }
95
96 void set_bcp(address bcp) { _last_frame.interpreter_frame_set_bcp(bcp); }
97 void set_mdp(address dp) { _last_frame.interpreter_frame_set_mdp(dp); }
208 JRT_END
209
210
211 //------------------------------------------------------------------------------------------------------------------------
212 // Allocation
213
214 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
215 Klass* k = pool->klass_at(index, CHECK);
216 InstanceKlass* klass = InstanceKlass::cast(k);
217
218 // Make sure we are not instantiating an abstract klass
219 klass->check_valid_for_instantiation(true, CHECK);
220
221 // Make sure klass is initialized
222 klass->initialize_preemptable(CHECK_AND_CLEAR_PREEMPTED);
223
224 oop obj = klass->allocate_instance(CHECK);
225 current->set_vm_result_oop(obj);
226 JRT_END
227
228
229 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
230 oop obj = oopFactory::new_typeArray(type, size, CHECK);
231 current->set_vm_result_oop(obj);
232 JRT_END
233
234
235 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
236 Klass* klass = pool->klass_at(index, CHECK);
237 objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
238 current->set_vm_result_oop(obj);
239 JRT_END
240
241
242 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
243 // We may want to pass in more arguments - could make this slightly faster
244 LastFrameAccessor last_frame(current);
245 ConstantPool* constants = last_frame.method()->constants();
246 int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
247 Klass* klass = constants->klass_at(i, CHECK);
248 int nof_dims = last_frame.number_of_dimensions();
249 assert(klass->is_klass(), "not a class");
250 assert(nof_dims >= 1, "multianewarray rank must be nonzero");
251
252 // We must create an array of jints to pass to multi_allocate.
253 ResourceMark rm(current);
254 const int small_dims = 10;
255 jint dim_array[small_dims];
256 jint *dims = &dim_array[0];
257 if (nof_dims > small_dims) {
258 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
259 }
260 for (int index = 0; index < nof_dims; index++) {
261 // offset from first_size_address is addressed as local[index]
262 int n = Interpreter::local_offset_in_bytes(index)/jintSize;
263 dims[index] = first_size_address[n];
264 }
265 oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
266 current->set_vm_result_oop(obj);
267 JRT_END
268
269
270 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
271 assert(oopDesc::is_oop(obj), "must be a valid oop");
272 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
273 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
274 JRT_END
275
276
277 // Quicken instance-of and check-cast bytecodes
278 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
279 // Force resolving; quicken the bytecode
280 LastFrameAccessor last_frame(current);
281 int which = last_frame.get_index_u2(Bytecodes::_checkcast);
282 ConstantPool* cpool = last_frame.method()->constants();
283 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
284 // program we might have seen an unquick'd bytecode in the interpreter but have another
285 // thread quicken the bytecode before we get here.
286 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
287 Klass* klass = cpool->klass_at(which, CHECK);
288 current->set_vm_result_metadata(klass);
289 JRT_END
290
291
292 //------------------------------------------------------------------------------------------------------------------------
293 // Exceptions
294
295 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,
603 // and therefore we don't have the receiver object at our fingertips. (Though,
604 // on some platforms the receiver still resides in a register...). Thus,
605 // we have no choice but print an error message not containing the receiver
606 // type.
607 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
608 Method* missingMethod))
609 ResourceMark rm(current);
610 assert(missingMethod != nullptr, "sanity");
611 methodHandle m(current, missingMethod);
612 LinkResolver::throw_abstract_method_error(m, THREAD);
613 JRT_END
614
615 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
616 Klass* recvKlass,
617 Method* missingMethod))
618 ResourceMark rm(current);
619 methodHandle mh = methodHandle(current, missingMethod);
620 LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
621 JRT_END
622
623
624 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
625 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
626 JRT_END
627
628 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
629 Klass* recvKlass,
630 Klass* interfaceKlass))
631 ResourceMark rm(current);
632 char buf[1000];
633 buf[0] = '\0';
634 jio_snprintf(buf, sizeof(buf),
635 "Class %s does not implement the requested interface %s",
636 recvKlass ? recvKlass->external_name() : "nullptr",
637 interfaceKlass ? interfaceKlass->external_name() : "nullptr");
638 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
639 JRT_END
640
641 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
642 THROW(vmSymbols::java_lang_NullPointerException());
682
683 // Resolution of put instructions to final instance fields with invalid updates (i.e.,
684 // to final instance fields with updates originating from a method different than <init>)
685 // is inhibited. A putfield instruction targeting an instance final field must throw
686 // an IllegalAccessError if the instruction is not in an instance
687 // initializer method <init>. If resolution were not inhibited, a putfield
688 // in an initializer method could be resolved in the initializer. Subsequent
689 // putfield instructions to the same field would then use cached information.
690 // As a result, those instructions would not pass through the VM. That is,
691 // checks in resolve_field_access() would not be executed for those instructions
692 // and the required IllegalAccessError would not be thrown.
693 //
694 // Also, we need to delay resolving getstatic and putstatic instructions until the
695 // class is initialized. This is required so that access to the static
696 // field will call the initialization function every time until the class
697 // is completely initialized ala. in 2.17.5 in JVM Specification.
698 InstanceKlass* klass = info.field_holder();
699 bool uninitialized_static = is_static && !klass->is_initialized();
700 bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
701 info.has_initialized_final_update();
702 assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
703
704 Bytecodes::Code get_code = (Bytecodes::Code)0;
705 Bytecodes::Code put_code = (Bytecodes::Code)0;
706 if (!uninitialized_static || VM_Version::supports_fast_class_init_checks()) {
707 get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
708 if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
709 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
710 }
711 }
712
713 ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
714 entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile());
715 entry->fill_in(info.field_holder(), info.offset(),
716 checked_cast<u2>(info.index()), checked_cast<u1>(state),
717 static_cast<u1>(get_code), static_cast<u1>(put_code));
718 }
719
720
721 //------------------------------------------------------------------------------------------------------------------------
722 // Synchronization
723 //
724 // The interpreter's synchronization code is factored out so that it can
725 // be shared by method invocation and synchronized blocks.
726 //%note synchronization_3
727
728 //%note monitor_1
729 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
730 #ifdef ASSERT
731 current->last_frame().interpreter_frame_verify_monitor(elem);
732 #endif
733 Handle h_obj(current, elem->obj());
734 assert(Universe::heap()->is_in_or_null(h_obj()),
741 #endif
742 JRT_END
743
744 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
745 oop obj = elem->obj();
746 assert(Universe::heap()->is_in(obj), "must be an object");
747 // The object could become unlocked through a JNI call, which we have no other checks for.
748 // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
749 if (obj->is_unlocked()) {
750 if (CheckJNICalls) {
751 fatal("Object has been unlocked by JNI");
752 }
753 return;
754 }
755 ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
756 // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
757 // again at method exit or in the case of an exception.
758 elem->set_obj(nullptr);
759 JRT_END
760
761
762 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
763 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
764 JRT_END
765
766
767 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
768 // Returns an illegal exception to install into the current thread. The
769 // pending_exception flag is cleared so normal exception handling does not
770 // trigger. Any current installed exception will be overwritten. This
771 // method will be called during an exception unwind.
772
773 assert(!HAS_PENDING_EXCEPTION, "no pending exception");
774 Handle exception(current, current->vm_result_oop());
775 assert(exception() != nullptr, "vm result should be set");
776 current->set_vm_result_oop(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
777 exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
778 current->set_vm_result_oop(exception());
779 JRT_END
780
781
782 //------------------------------------------------------------------------------------------------------------------------
783 // Invokes
784
785 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
786 return method->orig_bytecode_at(method->bci_from(bcp));
787 JRT_END
788
789 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
790 method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
791 JRT_END
792
793 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
794 JvmtiExport::post_raw_breakpoint(current, method, bcp);
795 JRT_END
796
797 void InterpreterRuntime::resolve_invoke(Bytecodes::Code bytecode, TRAPS) {
798 JavaThread* current = THREAD;
799 LastFrameAccessor last_frame(current);
800 // extract receiver from the outgoing argument list if necessary
1174 JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1175 }
1176 JRT_END
1177
1178 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1179 assert(current == JavaThread::current(), "pre-condition");
1180 JFR_ONLY(Jfr::check_and_process_sample_request(current);)
1181 // This function is called by the interpreter when the return poll found a reason
1182 // to call the VM. The reason could be that we are returning into a not yet safe
1183 // to access frame. We handle that below.
1184 // Note that this path does not check for single stepping, because we do not want
1185 // to single step when unwinding frames for an exception being thrown. Instead,
1186 // such single stepping code will use the safepoint table, which will use the
1187 // InterpreterRuntime::at_safepoint callback.
1188 StackWatermarkSet::before_unwind(current);
1189 JRT_END
1190
1191 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1192 ResolvedFieldEntry *entry))
1193
1194 // check the access_flags for the field in the klass
1195
1196 InstanceKlass* ik = entry->field_holder();
1197 int index = entry->field_index();
1198 if (!ik->field_status(index).is_access_watched()) return;
1199
1200 bool is_static = (obj == nullptr);
1201 HandleMark hm(current);
1202
1203 Handle h_obj;
1204 if (!is_static) {
1205 // non-static field accessors have an object, but we need a handle
1206 h_obj = Handle(current, obj);
1207 }
1208 InstanceKlass* field_holder = entry->field_holder(); // HERE
1209 jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static);
1210 LastFrameAccessor last_frame(current);
1211 JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1212 JRT_END
1213
1214 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1215 ResolvedFieldEntry *entry, jvalue *value))
1216
1217 InstanceKlass* ik = entry->field_holder();
1218
1219 // check the access_flags for the field in the klass
1220 int index = entry->field_index();
1221 // bail out if field modifications are not watched
1222 if (!ik->field_status(index).is_modification_watched()) return;
1223
1224 char sig_type = '\0';
1225
1226 switch((TosState)entry->tos_state()) {
1227 case btos: sig_type = JVM_SIGNATURE_BYTE; break;
1228 case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1229 case ctos: sig_type = JVM_SIGNATURE_CHAR; break;
1230 case stos: sig_type = JVM_SIGNATURE_SHORT; break;
1231 case itos: sig_type = JVM_SIGNATURE_INT; break;
1232 case ftos: sig_type = JVM_SIGNATURE_FLOAT; break;
1233 case atos: sig_type = JVM_SIGNATURE_CLASS; break;
1234 case ltos: sig_type = JVM_SIGNATURE_LONG; break;
1235 case dtos: sig_type = JVM_SIGNATURE_DOUBLE; break;
1236 default: ShouldNotReachHere(); return;
1237 }
1238 bool is_static = (obj == nullptr);
1239
1240 HandleMark hm(current);
1241 jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static);
1242 jvalue fvalue;
1243 #ifdef _LP64
1244 fvalue = *value;
1245 #else
1246 // Long/double values are stored unaligned and also noncontiguously with
1247 // tagged stacks. We can't just do a simple assignment even in the non-
1248 // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1249 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1250 // We assume that the two halves of longs/doubles are stored in interpreter
1251 // stack slots in platform-endian order.
1252 jlong_accessor u;
1253 jint* newval = (jint*)value;
1254 u.words[0] = newval[0];
1255 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1256 fvalue.j = u.long_value;
1257 #endif // _LP64
1258
1259 Handle h_obj;
1260 if (!is_static) {
1261 // non-static field accessors have an object, but we need a handle
|
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); }
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,
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());
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()),
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
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
|