< prev index next >

src/hotspot/share/interpreter/interpreterRuntime.cpp

Print this page

   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/methodData.hpp"
  50 #include "oops/method.inline.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/atomic.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.inline.hpp"
  74 #include "runtime/threadCritical.hpp"
  75 #include "utilities/align.hpp"
  76 #include "utilities/checkedCast.hpp"
  77 #include "utilities/copy.hpp"
  78 #include "utilities/events.hpp"

  79 
  80 // Helper class to access current interpreter state
  81 class LastFrameAccessor : public StackObj {
  82   frame _last_frame;
  83 public:
  84   LastFrameAccessor(JavaThread* current) {
  85     assert(current == Thread::current(), "sanity");
  86     _last_frame = current->last_frame();
  87   }
  88   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  89   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  90   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  91   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  92   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
  93 
  94   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
  95   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }
  96 
  97   // pass method to avoid calling unsafe bcp_to_method (partial fix 4926272)
  98   Bytecodes::Code code() const                   { return Bytecodes::code_at(method(), bcp()); }

 206 JRT_END
 207 
 208 
 209 //------------------------------------------------------------------------------------------------------------------------
 210 // Allocation
 211 
 212 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 213   Klass* k = pool->klass_at(index, CHECK);
 214   InstanceKlass* klass = InstanceKlass::cast(k);
 215 
 216   // Make sure we are not instantiating an abstract klass
 217   klass->check_valid_for_instantiation(true, CHECK);
 218 
 219   // Make sure klass is initialized
 220   klass->initialize(CHECK);
 221 
 222   oop obj = klass->allocate_instance(CHECK);
 223   current->set_vm_result(obj);
 224 JRT_END
 225 






















































 226 
 227 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 228   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 229   current->set_vm_result(obj);
 230 JRT_END
 231 
 232 
 233 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 234   Klass*    klass = pool->klass_at(index, CHECK);
 235   objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 236   current->set_vm_result(obj);
 237 JRT_END
 238 












 239 
 240 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 241   // We may want to pass in more arguments - could make this slightly faster
 242   LastFrameAccessor last_frame(current);
 243   ConstantPool* constants = last_frame.method()->constants();
 244   int          i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 245   Klass* klass   = constants->klass_at(i, CHECK);
 246   int   nof_dims = last_frame.number_of_dimensions();
 247   assert(klass->is_klass(), "not a class");
 248   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 249 
 250   // We must create an array of jints to pass to multi_allocate.
 251   ResourceMark rm(current);
 252   const int small_dims = 10;
 253   jint dim_array[small_dims];
 254   jint *dims = &dim_array[0];
 255   if (nof_dims > small_dims) {
 256     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 257   }
 258   for (int index = 0; index < nof_dims; index++) {
 259     // offset from first_size_address is addressed as local[index]
 260     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 261     dims[index] = first_size_address[n];
 262   }
 263   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 264   current->set_vm_result(obj);
 265 JRT_END
 266 
 267 
 268 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 269   assert(oopDesc::is_oop(obj), "must be a valid oop");
 270   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 271   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 272 JRT_END
 273 
























 274 
 275 // Quicken instance-of and check-cast bytecodes
 276 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 277   // Force resolving; quicken the bytecode
 278   LastFrameAccessor last_frame(current);
 279   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 280   ConstantPool* cpool = last_frame.method()->constants();
 281   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 282   // program we might have seen an unquick'd bytecode in the interpreter but have another
 283   // thread quicken the bytecode before we get here.
 284   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 285   Klass* klass = cpool->klass_at(which, CHECK);
 286   current->set_vm_result_2(klass);
 287 JRT_END
 288 
 289 
 290 //------------------------------------------------------------------------------------------------------------------------
 291 // Exceptions
 292 
 293 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 597 // and therefore we don't have the receiver object at our fingertips. (Though,
 598 // on some platforms the receiver still resides in a register...). Thus,
 599 // we have no choice but print an error message not containing the receiver
 600 // type.
 601 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 602                                                                         Method* missingMethod))
 603   ResourceMark rm(current);
 604   assert(missingMethod != nullptr, "sanity");
 605   methodHandle m(current, missingMethod);
 606   LinkResolver::throw_abstract_method_error(m, THREAD);
 607 JRT_END
 608 
 609 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 610                                                                      Klass* recvKlass,
 611                                                                      Method* missingMethod))
 612   ResourceMark rm(current);
 613   methodHandle mh = methodHandle(current, missingMethod);
 614   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 615 JRT_END
 616 




 617 
 618 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 619   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 620 JRT_END
 621 
 622 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 623                                                                               Klass* recvKlass,
 624                                                                               Klass* interfaceKlass))
 625   ResourceMark rm(current);
 626   char buf[1000];
 627   buf[0] = '\0';
 628   jio_snprintf(buf, sizeof(buf),
 629                "Class %s does not implement the requested interface %s",
 630                recvKlass ? recvKlass->external_name() : "nullptr",
 631                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 632   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 633 JRT_END
 634 
 635 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 636   THROW(vmSymbols::java_lang_NullPointerException());

 681   // initializer method <init>. If resolution were not inhibited, a putfield
 682   // in an initializer method could be resolved in the initializer. Subsequent
 683   // putfield instructions to the same field would then use cached information.
 684   // As a result, those instructions would not pass through the VM. That is,
 685   // checks in resolve_field_access() would not be executed for those instructions
 686   // and the required IllegalAccessError would not be thrown.
 687   //
 688   // Also, we need to delay resolving getstatic and putstatic instructions until the
 689   // class is initialized.  This is required so that access to the static
 690   // field will call the initialization function every time until the class
 691   // is completely initialized ala. in 2.17.5 in JVM Specification.
 692   InstanceKlass* klass = info.field_holder();
 693   bool uninitialized_static = is_static && !klass->is_initialized();
 694   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 695                                       info.has_initialized_final_update();
 696   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 697 
 698   Bytecodes::Code get_code = (Bytecodes::Code)0;
 699   Bytecodes::Code put_code = (Bytecodes::Code)0;
 700   if (!uninitialized_static) {
 701     get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);




 702     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 703       put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 704     }
 705   }
 706 
 707   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 708   entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile());



 709   entry->fill_in(info.field_holder(), info.offset(),
 710                  checked_cast<u2>(info.index()), checked_cast<u1>(state),
 711                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 712 }
 713 
 714 
 715 //------------------------------------------------------------------------------------------------------------------------
 716 // Synchronization
 717 //
 718 // The interpreter's synchronization code is factored out so that it can
 719 // be shared by method invocation and synchronized blocks.
 720 //%note synchronization_3
 721 
 722 //%note monitor_1
 723 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 724 #ifdef ASSERT
 725   current->last_frame().interpreter_frame_verify_monitor(elem);
 726 #endif
 727   Handle h_obj(current, elem->obj());
 728   assert(Universe::heap()->is_in_or_null(h_obj()),

 735 #endif
 736 JRT_END
 737 
 738 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 739   oop obj = elem->obj();
 740   assert(Universe::heap()->is_in(obj), "must be an object");
 741   // The object could become unlocked through a JNI call, which we have no other checks for.
 742   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 743   if (obj->is_unlocked()) {
 744     if (CheckJNICalls) {
 745       fatal("Object has been unlocked by JNI");
 746     }
 747     return;
 748   }
 749   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 750   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 751   // again at method exit or in the case of an exception.
 752   elem->set_obj(nullptr);
 753 JRT_END
 754 
 755 
 756 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 757   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 758 JRT_END
 759 
 760 
 761 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
 762   // Returns an illegal exception to install into the current thread. The
 763   // pending_exception flag is cleared so normal exception handling does not
 764   // trigger. Any current installed exception will be overwritten. This
 765   // method will be called during an exception unwind.
 766 
 767   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 768   Handle exception(current, current->vm_result());
 769   assert(exception() != nullptr, "vm result should be set");
 770   current->set_vm_result(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
 771   exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
 772   current->set_vm_result(exception());
 773 JRT_END
 774 















 775 
 776 //------------------------------------------------------------------------------------------------------------------------
 777 // Invokes
 778 
 779 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
 780   return method->orig_bytecode_at(method->bci_from(bcp));
 781 JRT_END
 782 
 783 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
 784   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 785 JRT_END
 786 
 787 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
 788   JvmtiExport::post_raw_breakpoint(current, method, bcp);
 789 JRT_END
 790 
 791 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) {
 792   LastFrameAccessor last_frame(current);
 793   // extract receiver from the outgoing argument list if necessary
 794   Handle receiver(current, nullptr);

1164     LastFrameAccessor last_frame(current);
1165     JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1166   }
1167 JRT_END
1168 
1169 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1170   assert(current == JavaThread::current(), "pre-condition");
1171   // This function is called by the interpreter when the return poll found a reason
1172   // to call the VM. The reason could be that we are returning into a not yet safe
1173   // to access frame. We handle that below.
1174   // Note that this path does not check for single stepping, because we do not want
1175   // to single step when unwinding frames for an exception being thrown. Instead,
1176   // such single stepping code will use the safepoint table, which will use the
1177   // InterpreterRuntime::at_safepoint callback.
1178   StackWatermarkSet::before_unwind(current);
1179 JRT_END
1180 
1181 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1182                                                       ResolvedFieldEntry *entry))
1183 

1184   // check the access_flags for the field in the klass
1185 
1186   InstanceKlass* ik = entry->field_holder();
1187   int index = entry->field_index();
1188   if (!ik->field_status(index).is_access_watched()) return;
1189 
1190   bool is_static = (obj == nullptr);

1191   HandleMark hm(current);
1192 
1193   Handle h_obj;
1194   if (!is_static) {
1195     // non-static field accessors have an object, but we need a handle
1196     h_obj = Handle(current, obj);
1197   }
1198   InstanceKlass* field_holder = entry->field_holder(); // HERE
1199   jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static);
1200   LastFrameAccessor last_frame(current);
1201   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1202 JRT_END
1203 
1204 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1205                                                             ResolvedFieldEntry *entry, jvalue *value))
1206 

1207   InstanceKlass* ik = entry->field_holder();
1208 
1209   // check the access_flags for the field in the klass
1210   int index = entry->field_index();
1211   // bail out if field modifications are not watched
1212   if (!ik->field_status(index).is_modification_watched()) return;
1213 
1214   char sig_type = '\0';
1215 
1216   switch((TosState)entry->tos_state()) {
1217     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1218     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1219     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1220     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1221     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1222     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1223     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1224     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1225     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1226     default:  ShouldNotReachHere(); return;
1227   }

1228   bool is_static = (obj == nullptr);

1229 
1230   HandleMark hm(current);
1231   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static);
1232   jvalue fvalue;
1233 #ifdef _LP64
1234   fvalue = *value;
1235 #else
1236   // Long/double values are stored unaligned and also noncontiguously with
1237   // tagged stacks.  We can't just do a simple assignment even in the non-
1238   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1239   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1240   // We assume that the two halves of longs/doubles are stored in interpreter
1241   // stack slots in platform-endian order.
1242   jlong_accessor u;
1243   jint* newval = (jint*)value;
1244   u.words[0] = newval[0];
1245   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1246   fvalue.j = u.long_value;
1247 #endif // _LP64
1248 
1249   Handle h_obj;
1250   if (!is_static) {
1251     // non-static field accessors have an object, but we need a handle

   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/methodData.hpp"
  54 #include "oops/method.inline.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/atomic.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.inline.hpp"
  78 #include "runtime/threadCritical.hpp"
  79 #include "utilities/align.hpp"
  80 #include "utilities/checkedCast.hpp"
  81 #include "utilities/copy.hpp"
  82 #include "utilities/events.hpp"
  83 #include "utilities/globalDefinitions.hpp"
  84 
  85 // Helper class to access current interpreter state
  86 class LastFrameAccessor : public StackObj {
  87   frame _last_frame;
  88 public:
  89   LastFrameAccessor(JavaThread* current) {
  90     assert(current == Thread::current(), "sanity");
  91     _last_frame = current->last_frame();
  92   }
  93   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  94   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  95   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  96   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  97   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
  98 
  99   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 100   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }
 101 
 102   // pass method to avoid calling unsafe bcp_to_method (partial fix 4926272)
 103   Bytecodes::Code code() const                   { return Bytecodes::code_at(method(), bcp()); }

 211 JRT_END
 212 
 213 
 214 //------------------------------------------------------------------------------------------------------------------------
 215 // Allocation
 216 
 217 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 218   Klass* k = pool->klass_at(index, CHECK);
 219   InstanceKlass* klass = InstanceKlass::cast(k);
 220 
 221   // Make sure we are not instantiating an abstract klass
 222   klass->check_valid_for_instantiation(true, CHECK);
 223 
 224   // Make sure klass is initialized
 225   klass->initialize(CHECK);
 226 
 227   oop obj = klass->allocate_instance(CHECK);
 228   current->set_vm_result(obj);
 229 JRT_END
 230 
 231 JRT_ENTRY(void, InterpreterRuntime::read_flat_field(JavaThread* current, oopDesc* obj, ResolvedFieldEntry* entry))
 232   assert(oopDesc::is_oop(obj), "Sanity check");
 233   Handle obj_h(THREAD, obj);
 234 
 235   InstanceKlass* holder = InstanceKlass::cast(entry->field_holder());
 236   assert(entry->field_holder()->field_is_flat(entry->field_index()), "Sanity check");
 237 
 238   InlineLayoutInfo* layout_info = holder->inline_layout_info_adr(entry->field_index());
 239   InlineKlass* field_vklass = layout_info->klass();
 240 
 241 #ifdef ASSERT
 242   fieldDescriptor fd;
 243   bool found = holder->find_field_from_offset(entry->field_offset(), false, &fd);
 244   assert(found, "Field not found");
 245   assert(fd.is_flat(), "Field must be flat");
 246 #endif // ASSERT
 247 
 248   oop res = field_vklass->read_payload_from_addr(obj_h(), entry->field_offset(), layout_info->kind(), CHECK);
 249   current->set_vm_result(res);
 250 JRT_END
 251 
 252 JRT_ENTRY(void, InterpreterRuntime::read_nullable_flat_field(JavaThread* current, oopDesc* obj, ResolvedFieldEntry* entry))
 253   assert(oopDesc::is_oop(obj), "Sanity check");
 254   assert(entry->has_null_marker(), "Otherwise should not get there");
 255   Handle obj_h(THREAD, obj);
 256 
 257   InstanceKlass* holder = entry->field_holder();
 258   int field_index = entry->field_index();
 259   InlineLayoutInfo* li= holder->inline_layout_info_adr(field_index);
 260 
 261 #ifdef ASSERT
 262   fieldDescriptor fd;
 263   bool found = holder->find_field_from_offset(entry->field_offset(), false, &fd);
 264   assert(found, "Field not found");
 265   assert(fd.is_flat(), "Field must be flat");
 266 #endif // ASSERT
 267 
 268   InlineKlass* field_vklass = InlineKlass::cast(li->klass());
 269   oop res = field_vklass->read_payload_from_addr(obj_h(), entry->field_offset(), li->kind(), CHECK);
 270   current->set_vm_result(res);
 271 
 272 JRT_END
 273 
 274 JRT_ENTRY(void, InterpreterRuntime::write_nullable_flat_field(JavaThread* current, oopDesc* obj, oopDesc* value, ResolvedFieldEntry* entry))
 275   assert(oopDesc::is_oop(obj), "Sanity check");
 276   Handle obj_h(THREAD, obj);
 277   assert(value == nullptr || oopDesc::is_oop(value), "Sanity check");
 278   Handle val_h(THREAD, value);
 279 
 280   InstanceKlass* holder = entry->field_holder();
 281   InlineLayoutInfo* li = holder->inline_layout_info_adr(entry->field_index());
 282   InlineKlass* vk = li->klass();
 283   vk->write_value_to_addr(val_h(), ((char*)(oopDesc*)obj_h()) + entry->field_offset(), li->kind(), true, CHECK);
 284 JRT_END
 285 
 286 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 287   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 288   current->set_vm_result(obj);
 289 JRT_END
 290 
 291 
 292 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 293   Klass*    klass = pool->klass_at(index, CHECK);
 294   arrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 295   current->set_vm_result(obj);
 296 JRT_END
 297 
 298 JRT_ENTRY(void, InterpreterRuntime::flat_array_load(JavaThread* current, arrayOopDesc* array, int index))
 299   assert(array->is_flatArray(), "Must be");
 300   flatArrayOop farray = (flatArrayOop)array;
 301   oop res = farray->read_value_from_flat_array(index, CHECK);
 302   current->set_vm_result(res);
 303 JRT_END
 304 
 305 JRT_ENTRY(void, InterpreterRuntime::flat_array_store(JavaThread* current, oopDesc* val, arrayOopDesc* array, int index))
 306   assert(array->is_flatArray(), "Must be");
 307   flatArrayOop farray = (flatArrayOop)array;
 308   farray->write_value_to_flat_array(val, index, CHECK);
 309 JRT_END
 310 
 311 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 312   // We may want to pass in more arguments - could make this slightly faster
 313   LastFrameAccessor last_frame(current);
 314   ConstantPool* constants = last_frame.method()->constants();
 315   int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 316   Klass* klass = constants->klass_at(i, CHECK);
 317   int   nof_dims = last_frame.number_of_dimensions();
 318   assert(klass->is_klass(), "not a class");
 319   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 320 
 321   // We must create an array of jints to pass to multi_allocate.
 322   ResourceMark rm(current);
 323   const int small_dims = 10;
 324   jint dim_array[small_dims];
 325   jint *dims = &dim_array[0];
 326   if (nof_dims > small_dims) {
 327     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 328   }
 329   for (int index = 0; index < nof_dims; index++) {
 330     // offset from first_size_address is addressed as local[index]
 331     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 332     dims[index] = first_size_address[n];
 333   }
 334   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 335   current->set_vm_result(obj);
 336 JRT_END
 337 
 338 
 339 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 340   assert(oopDesc::is_oop(obj), "must be a valid oop");
 341   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 342   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 343 JRT_END
 344 
 345 JRT_ENTRY(jboolean, InterpreterRuntime::is_substitutable(JavaThread* current, oopDesc* aobj, oopDesc* bobj))
 346   assert(oopDesc::is_oop(aobj) && oopDesc::is_oop(bobj), "must be valid oops");
 347 
 348   Handle ha(THREAD, aobj);
 349   Handle hb(THREAD, bobj);
 350   JavaValue result(T_BOOLEAN);
 351   JavaCallArguments args;
 352   args.push_oop(ha);
 353   args.push_oop(hb);
 354   methodHandle method(current, Universe::is_substitutable_method());
 355   method->method_holder()->initialize(CHECK_false); // Ensure class ValueObjectMethods is initialized
 356   JavaCalls::call(&result, method, &args, THREAD);
 357   if (HAS_PENDING_EXCEPTION) {
 358     // Something really bad happened because isSubstitutable() should not throw exceptions
 359     // If it is an error, just let it propagate
 360     // If it is an exception, wrap it into an InternalError
 361     if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 362       Handle e(THREAD, PENDING_EXCEPTION);
 363       CLEAR_PENDING_EXCEPTION;
 364       THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
 365     }
 366   }
 367   return result.get_jboolean();
 368 JRT_END
 369 
 370 // Quicken instance-of and check-cast bytecodes
 371 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 372   // Force resolving; quicken the bytecode
 373   LastFrameAccessor last_frame(current);
 374   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 375   ConstantPool* cpool = last_frame.method()->constants();
 376   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 377   // program we might have seen an unquick'd bytecode in the interpreter but have another
 378   // thread quicken the bytecode before we get here.
 379   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 380   Klass* klass = cpool->klass_at(which, CHECK);
 381   current->set_vm_result_2(klass);
 382 JRT_END
 383 
 384 
 385 //------------------------------------------------------------------------------------------------------------------------
 386 // Exceptions
 387 
 388 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 692 // and therefore we don't have the receiver object at our fingertips. (Though,
 693 // on some platforms the receiver still resides in a register...). Thus,
 694 // we have no choice but print an error message not containing the receiver
 695 // type.
 696 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 697                                                                         Method* missingMethod))
 698   ResourceMark rm(current);
 699   assert(missingMethod != nullptr, "sanity");
 700   methodHandle m(current, missingMethod);
 701   LinkResolver::throw_abstract_method_error(m, THREAD);
 702 JRT_END
 703 
 704 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 705                                                                      Klass* recvKlass,
 706                                                                      Method* missingMethod))
 707   ResourceMark rm(current);
 708   methodHandle mh = methodHandle(current, missingMethod);
 709   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 710 JRT_END
 711 
 712 JRT_ENTRY(void, InterpreterRuntime::throw_InstantiationError(JavaThread* current))
 713   THROW(vmSymbols::java_lang_InstantiationError());
 714 JRT_END
 715 
 716 
 717 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 718   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 719 JRT_END
 720 
 721 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 722                                                                               Klass* recvKlass,
 723                                                                               Klass* interfaceKlass))
 724   ResourceMark rm(current);
 725   char buf[1000];
 726   buf[0] = '\0';
 727   jio_snprintf(buf, sizeof(buf),
 728                "Class %s does not implement the requested interface %s",
 729                recvKlass ? recvKlass->external_name() : "nullptr",
 730                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 731   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 732 JRT_END
 733 
 734 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 735   THROW(vmSymbols::java_lang_NullPointerException());

 780   // initializer method <init>. If resolution were not inhibited, a putfield
 781   // in an initializer method could be resolved in the initializer. Subsequent
 782   // putfield instructions to the same field would then use cached information.
 783   // As a result, those instructions would not pass through the VM. That is,
 784   // checks in resolve_field_access() would not be executed for those instructions
 785   // and the required IllegalAccessError would not be thrown.
 786   //
 787   // Also, we need to delay resolving getstatic and putstatic instructions until the
 788   // class is initialized.  This is required so that access to the static
 789   // field will call the initialization function every time until the class
 790   // is completely initialized ala. in 2.17.5 in JVM Specification.
 791   InstanceKlass* klass = info.field_holder();
 792   bool uninitialized_static = is_static && !klass->is_initialized();
 793   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 794                                       info.has_initialized_final_update();
 795   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 796 
 797   Bytecodes::Code get_code = (Bytecodes::Code)0;
 798   Bytecodes::Code put_code = (Bytecodes::Code)0;
 799   if (!uninitialized_static) {
 800     if (is_static) {
 801       get_code = Bytecodes::_getstatic;
 802     } else {
 803       get_code = Bytecodes::_getfield;
 804     }
 805     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 806         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 807     }
 808   }
 809 
 810   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 811   entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile(),
 812                    info.is_flat(), info.is_null_free_inline_type(),
 813                    info.has_null_marker());
 814 
 815   entry->fill_in(info.field_holder(), info.offset(),
 816                  checked_cast<u2>(info.index()), checked_cast<u1>(state),
 817                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 818 }
 819 
 820 
 821 //------------------------------------------------------------------------------------------------------------------------
 822 // Synchronization
 823 //
 824 // The interpreter's synchronization code is factored out so that it can
 825 // be shared by method invocation and synchronized blocks.
 826 //%note synchronization_3
 827 
 828 //%note monitor_1
 829 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 830 #ifdef ASSERT
 831   current->last_frame().interpreter_frame_verify_monitor(elem);
 832 #endif
 833   Handle h_obj(current, elem->obj());
 834   assert(Universe::heap()->is_in_or_null(h_obj()),

 841 #endif
 842 JRT_END
 843 
 844 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 845   oop obj = elem->obj();
 846   assert(Universe::heap()->is_in(obj), "must be an object");
 847   // The object could become unlocked through a JNI call, which we have no other checks for.
 848   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 849   if (obj->is_unlocked()) {
 850     if (CheckJNICalls) {
 851       fatal("Object has been unlocked by JNI");
 852     }
 853     return;
 854   }
 855   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 856   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 857   // again at method exit or in the case of an exception.
 858   elem->set_obj(nullptr);
 859 JRT_END
 860 

 861 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 862   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 863 JRT_END
 864 

 865 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
 866   // Returns an illegal exception to install into the current thread. The
 867   // pending_exception flag is cleared so normal exception handling does not
 868   // trigger. Any current installed exception will be overwritten. This
 869   // method will be called during an exception unwind.
 870 
 871   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 872   Handle exception(current, current->vm_result());
 873   assert(exception() != nullptr, "vm result should be set");
 874   current->set_vm_result(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
 875   exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
 876   current->set_vm_result(exception());
 877 JRT_END
 878 
 879 JRT_ENTRY(void, InterpreterRuntime::throw_identity_exception(JavaThread* current, oopDesc* obj))
 880   Klass* klass = cast_to_oop(obj)->klass();
 881   ResourceMark rm(THREAD);
 882   const char* desc = "Cannot synchronize on an instance of value class ";
 883   const char* className = klass->external_name();
 884   size_t msglen = strlen(desc) + strlen(className) + 1;
 885   char* message = NEW_RESOURCE_ARRAY(char, msglen);
 886   if (nullptr == message) {
 887     // Out of memory: can't create detailed error message
 888     THROW_MSG(vmSymbols::java_lang_IdentityException(), className);
 889   } else {
 890     jio_snprintf(message, msglen, "%s%s", desc, className);
 891     THROW_MSG(vmSymbols::java_lang_IdentityException(), message);
 892   }
 893 JRT_END
 894 
 895 //------------------------------------------------------------------------------------------------------------------------
 896 // Invokes
 897 
 898 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
 899   return method->orig_bytecode_at(method->bci_from(bcp));
 900 JRT_END
 901 
 902 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
 903   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 904 JRT_END
 905 
 906 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
 907   JvmtiExport::post_raw_breakpoint(current, method, bcp);
 908 JRT_END
 909 
 910 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) {
 911   LastFrameAccessor last_frame(current);
 912   // extract receiver from the outgoing argument list if necessary
 913   Handle receiver(current, nullptr);

1283     LastFrameAccessor last_frame(current);
1284     JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1285   }
1286 JRT_END
1287 
1288 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1289   assert(current == JavaThread::current(), "pre-condition");
1290   // This function is called by the interpreter when the return poll found a reason
1291   // to call the VM. The reason could be that we are returning into a not yet safe
1292   // to access frame. We handle that below.
1293   // Note that this path does not check for single stepping, because we do not want
1294   // to single step when unwinding frames for an exception being thrown. Instead,
1295   // such single stepping code will use the safepoint table, which will use the
1296   // InterpreterRuntime::at_safepoint callback.
1297   StackWatermarkSet::before_unwind(current);
1298 JRT_END
1299 
1300 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1301                                                       ResolvedFieldEntry *entry))
1302 
1303   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1304   // check the access_flags for the field in the klass
1305 
1306   InstanceKlass* ik = entry->field_holder();
1307   int index = entry->field_index();
1308   if (!ik->field_status(index).is_access_watched()) return;
1309 
1310   bool is_static = (obj == nullptr);
1311   bool is_flat = entry->is_flat();
1312   HandleMark hm(current);
1313 
1314   Handle h_obj;
1315   if (!is_static) {
1316     // non-static field accessors have an object, but we need a handle
1317     h_obj = Handle(current, obj);
1318   }
1319   InstanceKlass* field_holder = entry->field_holder(); // HERE
1320   jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static, is_flat);
1321   LastFrameAccessor last_frame(current);
1322   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1323 JRT_END
1324 
1325 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1326                                                             ResolvedFieldEntry *entry, jvalue *value))
1327 
1328   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1329   InstanceKlass* ik = entry->field_holder();
1330 
1331   // check the access_flags for the field in the klass
1332   int index = entry->field_index();
1333   // bail out if field modifications are not watched
1334   if (!ik->field_status(index).is_modification_watched()) return;
1335 
1336   char sig_type = '\0';
1337 
1338   switch((TosState)entry->tos_state()) {
1339     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1340     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1341     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1342     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1343     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1344     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1345     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1346     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1347     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1348     default:  ShouldNotReachHere(); return;
1349   }
1350 
1351   bool is_static = (obj == nullptr);
1352   bool is_flat = entry->is_flat();
1353 
1354   HandleMark hm(current);
1355   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static, is_flat);
1356   jvalue fvalue;
1357 #ifdef _LP64
1358   fvalue = *value;
1359 #else
1360   // Long/double values are stored unaligned and also noncontiguously with
1361   // tagged stacks.  We can't just do a simple assignment even in the non-
1362   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1363   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1364   // We assume that the two halves of longs/doubles are stored in interpreter
1365   // stack slots in platform-endian order.
1366   jlong_accessor u;
1367   jint* newval = (jint*)value;
1368   u.words[0] = newval[0];
1369   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1370   fvalue.j = u.long_value;
1371 #endif // _LP64
1372 
1373   Handle h_obj;
1374   if (!is_static) {
1375     // non-static field accessors have an object, but we need a handle
< prev index next >