< prev index next >

src/hotspot/share/runtime/javaCalls.cpp

Print this page

 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "code/nmethod.hpp"
 29 #include "compiler/compilationPolicy.hpp"
 30 #include "compiler/compileBroker.hpp"
 31 #include "interpreter/interpreter.hpp"
 32 #include "interpreter/linkResolver.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/method.inline.hpp"
 35 #include "oops/oop.inline.hpp"

 36 #include "prims/jniCheck.hpp"
 37 #include "prims/jvmtiExport.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/interfaceSupport.inline.hpp"
 40 #include "runtime/javaCalls.hpp"
 41 #include "runtime/javaThread.hpp"
 42 #include "runtime/jniHandles.inline.hpp"
 43 #include "runtime/mutexLocker.hpp"
 44 #include "runtime/os.inline.hpp"
 45 #include "runtime/sharedRuntime.hpp"
 46 #include "runtime/signature.hpp"
 47 #include "runtime/stubRoutines.hpp"
 48 #include "runtime/thread.inline.hpp"
 49 #if INCLUDE_JVMCI
 50 #include "jvmci/jvmciJavaClasses.hpp"
 51 #endif
 52 
 53 // -----------------------------------------------------
 54 // Implementation of JavaCallWrapper
 55 

129 
130   if (_thread->has_pending_exception() && _thread->has_last_Java_frame()) {
131     // If we get here, the Java code threw an exception that unwound a frame.
132     // It could be that the new frame anchor has not passed through the required
133     // StackWatermark barriers. Therefore, we process any such deferred unwind
134     // requests here.
135     StackWatermarkSet::after_unwind(_thread);
136   }
137 }
138 
139 
140 void JavaCallWrapper::oops_do(OopClosure* f) {
141   f->do_oop((oop*)&_receiver);
142   handles()->oops_do(f);
143 }
144 
145 
146 // Helper methods
147 static BasicType runtime_type_from(JavaValue* result) {
148   switch (result->get_type()) {
149     case T_BOOLEAN: // fall through
150     case T_CHAR   : // fall through
151     case T_SHORT  : // fall through
152     case T_INT    : // fall through
153 #ifndef _LP64
154     case T_OBJECT : // fall through
155     case T_ARRAY  : // fall through

156 #endif
157     case T_BYTE   : // fall through
158     case T_VOID   : return T_INT;
159     case T_LONG   : return T_LONG;
160     case T_FLOAT  : return T_FLOAT;
161     case T_DOUBLE : return T_DOUBLE;
162 #ifdef _LP64
163     case T_ARRAY  : // fall through
164     case T_OBJECT:  return T_OBJECT;

165 #endif
166     default:
167       ShouldNotReachHere();
168       return T_ILLEGAL;
169   }
170 }
171 
172 // ============ Virtual calls ============
173 
174 void JavaCalls::call_virtual(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
175   CallInfo callinfo;
176   Handle receiver = args->receiver();
177   Klass* recvrKlass = receiver.is_null() ? (Klass*)nullptr : receiver->klass();
178   LinkInfo link_info(spec_klass, name, signature);
179   LinkResolver::resolve_virtual_call(
180           callinfo, receiver, recvrKlass, link_info, true, CHECK);
181   methodHandle method(THREAD, callinfo.selected_method());
182   assert(method.not_null(), "should have thrown exception");
183 
184   // Invoke the method

272 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS) {
273   JavaCallArguments args;
274   args.push_oop(arg1);
275   args.push_oop(arg2);
276   call_static(result, klass, name, signature, &args, CHECK);
277 }
278 
279 
280 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, Handle arg3, TRAPS) {
281   JavaCallArguments args;
282   args.push_oop(arg1);
283   args.push_oop(arg2);
284   args.push_oop(arg3);
285   call_static(result, klass, name, signature, &args, CHECK);
286 }
287 
288 // ============ allocate and initialize new object instance ============
289 
290 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, JavaCallArguments* args, TRAPS) {
291   klass->initialize(CHECK_NH); // Quick no-op if already initialized.













292   Handle obj = klass->allocate_instance_handle(CHECK_NH);
293   JavaValue void_result(T_VOID);
294   args->set_receiver(obj); // inserts <obj> as the first argument.
295   JavaCalls::call_special(&void_result, klass,
296                           vmSymbols::object_initializer_name(),
297                           constructor_signature, args, CHECK_NH);
298   // Already returned a Null Handle if any exception is pending.
299   return obj;
300 }
301 
302 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, TRAPS) {
303   JavaCallArguments args;
304   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
305 }
306 
307 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, Handle arg1, TRAPS) {
308   JavaCallArguments args;
309   args.push_oop(arg1);
310   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
311 }

373   // Find receiver
374   Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
375 
376   // When we reenter Java, we need to re-enable the reserved/yellow zone which
377   // might already be disabled when we are in VM.
378   thread->stack_overflow_state()->reguard_stack_if_needed();
379 
380   // Check that there are shadow pages available before changing thread state
381   // to Java. Calculate current_stack_pointer here to make sure
382   // stack_shadow_pages_available() and map_stack_shadow_pages() use the same sp.
383   address sp = os::current_stack_pointer();
384   if (!os::stack_shadow_pages_available(THREAD, method, sp)) {
385     // Throw stack overflow exception with preinitialized exception.
386     Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
387     return;
388   } else {
389     // Touch pages checked if the OS needs them to be touched to be mapped.
390     os::map_stack_shadow_pages(sp);
391   }
392 












393   // do call
394   { JavaCallWrapper link(method, receiver, result, CHECK);
395     { HandleMark hm(thread);  // HandleMark used by HandleMarkCleaner
396 
397       // NOTE: if we move the computation of the result_val_address inside
398       // the call to call_stub, the optimizer produces wrong code.
399       intptr_t* result_val_address = (intptr_t*)(result->get_value_addr());
400       intptr_t* parameter_address = args->parameters();
401 #if INCLUDE_JVMCI
402       // Gets the alternative target (if any) that should be called
403       Handle alternative_target = args->alternative_target();
404       if (!alternative_target.is_null()) {
405         // Must extract verified entry point from HotSpotNmethod after VM to Java
406         // transition in JavaCallWrapper constructor so that it is safe with
407         // respect to nmethod sweeping.
408         address verified_entry_point = (address) HotSpotJVMCI::InstalledCode::entryPoint(nullptr, alternative_target());
409         if (verified_entry_point != nullptr) {
410           thread->set_jvmci_alternate_call_target(verified_entry_point);
411           entry_point = method->adapter()->get_i2c_entry();
412         }

423         args->size_of_parameters(),
424         CHECK
425       );
426 
427       result = link.result();  // circumvent MS C++ 5.0 compiler bug (result is clobbered across call)
428       // Preserve oop return value across possible gc points
429       if (oop_result_flag) {
430         thread->set_vm_result(result->get_oop());
431       }
432     }
433   } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
434 
435   // Check if a thread stop or suspend should be executed
436   // The following assert was not realistic.  Thread.stop can set that bit at any moment.
437   //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
438 
439   // Restore possible oop return
440   if (oop_result_flag) {
441     result->set_oop(thread->vm_result());
442     thread->set_vm_result(nullptr);

443   }
444 }
445 
446 
447 //--------------------------------------------------------------------------------------
448 // Implementation of JavaCallArguments
449 
450 inline bool is_value_state_indirect_oop(uint state) {
451   assert(state != JavaCallArguments::value_state_oop,
452          "Checking for handles after removal");
453   assert(state < JavaCallArguments::value_state_limit,
454          "Invalid value state %u", state);
455   return state != JavaCallArguments::value_state_primitive;
456 }
457 
458 inline oop resolve_indirect_oop(intptr_t value, uint state) {
459   switch (state) {
460   case JavaCallArguments::value_state_handle:
461   {
462     oop* ptr = reinterpret_cast<oop*>(value);

559     }
560 
561     check_value(true);          // Verify value state.
562   }
563 
564   friend class SignatureIterator;  // so do_parameters_on can call do_type
565   void do_type(BasicType type) {
566     switch (type) {
567     case T_BYTE:
568     case T_BOOLEAN:
569     case T_CHAR:
570     case T_SHORT:
571     case T_INT:
572     case T_FLOAT:  // this one also
573       check_single_word(); break;
574     case T_LONG:
575     case T_DOUBLE:
576       check_double_word(); break;
577     case T_ARRAY:
578     case T_OBJECT:

579       check_reference(); break;
580     default:
581       ShouldNotReachHere();
582     }
583   }
584 };
585 
586 
587 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type) {
588   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
589 
590   // Treat T_OBJECT and T_ARRAY as the same
591   if (is_reference_type(return_type)) return_type = T_OBJECT;
592 
593   // Check that oop information is correct
594   Symbol* signature = method->signature();
595 
596   SignatureChekker sc(signature,
597                       return_type,
598                       method->is_static(),
599                       _value_state,
600                       _value);
601 }

 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "code/nmethod.hpp"
 29 #include "compiler/compilationPolicy.hpp"
 30 #include "compiler/compileBroker.hpp"
 31 #include "interpreter/interpreter.hpp"
 32 #include "interpreter/linkResolver.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/method.inline.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "oops/inlineKlass.hpp"
 37 #include "prims/jniCheck.hpp"
 38 #include "prims/jvmtiExport.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/interfaceSupport.inline.hpp"
 41 #include "runtime/javaCalls.hpp"
 42 #include "runtime/javaThread.hpp"
 43 #include "runtime/jniHandles.inline.hpp"
 44 #include "runtime/mutexLocker.hpp"
 45 #include "runtime/os.inline.hpp"
 46 #include "runtime/sharedRuntime.hpp"
 47 #include "runtime/signature.hpp"
 48 #include "runtime/stubRoutines.hpp"
 49 #include "runtime/thread.inline.hpp"
 50 #if INCLUDE_JVMCI
 51 #include "jvmci/jvmciJavaClasses.hpp"
 52 #endif
 53 
 54 // -----------------------------------------------------
 55 // Implementation of JavaCallWrapper
 56 

130 
131   if (_thread->has_pending_exception() && _thread->has_last_Java_frame()) {
132     // If we get here, the Java code threw an exception that unwound a frame.
133     // It could be that the new frame anchor has not passed through the required
134     // StackWatermark barriers. Therefore, we process any such deferred unwind
135     // requests here.
136     StackWatermarkSet::after_unwind(_thread);
137   }
138 }
139 
140 
141 void JavaCallWrapper::oops_do(OopClosure* f) {
142   f->do_oop((oop*)&_receiver);
143   handles()->oops_do(f);
144 }
145 
146 
147 // Helper methods
148 static BasicType runtime_type_from(JavaValue* result) {
149   switch (result->get_type()) {
150     case T_BOOLEAN  : // fall through
151     case T_CHAR     : // fall through
152     case T_SHORT    : // fall through
153     case T_INT      : // fall through
154 #ifndef _LP64
155     case T_OBJECT   : // fall through
156     case T_ARRAY    : // fall through
157     case T_PRIMITIVE_OBJECT: // fall through
158 #endif
159     case T_BYTE     : // fall through
160     case T_VOID     : return T_INT;
161     case T_LONG     : return T_LONG;
162     case T_FLOAT    : return T_FLOAT;
163     case T_DOUBLE   : return T_DOUBLE;
164 #ifdef _LP64
165     case T_ARRAY    : // fall through
166     case T_OBJECT   : return T_OBJECT;
167     case T_PRIMITIVE_OBJECT: return T_PRIMITIVE_OBJECT;
168 #endif
169     default:
170       ShouldNotReachHere();
171       return T_ILLEGAL;
172   }
173 }
174 
175 // ============ Virtual calls ============
176 
177 void JavaCalls::call_virtual(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
178   CallInfo callinfo;
179   Handle receiver = args->receiver();
180   Klass* recvrKlass = receiver.is_null() ? (Klass*)nullptr : receiver->klass();
181   LinkInfo link_info(spec_klass, name, signature);
182   LinkResolver::resolve_virtual_call(
183           callinfo, receiver, recvrKlass, link_info, true, CHECK);
184   methodHandle method(THREAD, callinfo.selected_method());
185   assert(method.not_null(), "should have thrown exception");
186 
187   // Invoke the method

275 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS) {
276   JavaCallArguments args;
277   args.push_oop(arg1);
278   args.push_oop(arg2);
279   call_static(result, klass, name, signature, &args, CHECK);
280 }
281 
282 
283 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, Handle arg3, TRAPS) {
284   JavaCallArguments args;
285   args.push_oop(arg1);
286   args.push_oop(arg2);
287   args.push_oop(arg3);
288   call_static(result, klass, name, signature, &args, CHECK);
289 }
290 
291 // ============ allocate and initialize new object instance ============
292 
293 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, JavaCallArguments* args, TRAPS) {
294   klass->initialize(CHECK_NH); // Quick no-op if already initialized.
295 
296   // Special case for factory methods
297   if (EnableValhalla && !constructor_signature->is_void_method_signature()) {
298     guarantee(klass->is_inline_klass(), "inline classes must use factory methods");
299     JavaValue factory_result(T_OBJECT);
300     JavaCalls::call_static(&factory_result, klass,
301                            vmSymbols::inline_factory_name(),
302                            constructor_signature, args, CHECK_NH);
303     return Handle(THREAD, factory_result.get_oop());
304   }
305 
306   // main branch of code creates a non-inline object:
307   assert(!klass->is_inline_klass(), "classic constructors are only for non-inline classes");
308   Handle obj = klass->allocate_instance_handle(CHECK_NH);
309   JavaValue void_result(T_VOID);
310   args->set_receiver(obj); // inserts <obj> as the first argument.
311   JavaCalls::call_special(&void_result, klass,
312                           vmSymbols::object_initializer_name(),
313                           constructor_signature, args, CHECK_NH);
314   // Already returned a Null Handle if any exception is pending.
315   return obj;
316 }
317 
318 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, TRAPS) {
319   JavaCallArguments args;
320   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
321 }
322 
323 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, Handle arg1, TRAPS) {
324   JavaCallArguments args;
325   args.push_oop(arg1);
326   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
327 }

389   // Find receiver
390   Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
391 
392   // When we reenter Java, we need to re-enable the reserved/yellow zone which
393   // might already be disabled when we are in VM.
394   thread->stack_overflow_state()->reguard_stack_if_needed();
395 
396   // Check that there are shadow pages available before changing thread state
397   // to Java. Calculate current_stack_pointer here to make sure
398   // stack_shadow_pages_available() and map_stack_shadow_pages() use the same sp.
399   address sp = os::current_stack_pointer();
400   if (!os::stack_shadow_pages_available(THREAD, method, sp)) {
401     // Throw stack overflow exception with preinitialized exception.
402     Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
403     return;
404   } else {
405     // Touch pages checked if the OS needs them to be touched to be mapped.
406     os::map_stack_shadow_pages(sp);
407   }
408 
409   jobject value_buffer = nullptr;
410   if (InlineTypeReturnedAsFields && (result->get_type() == T_PRIMITIVE_OBJECT || result->get_type() == T_OBJECT)) {
411     // Pre allocate a buffered inline type in case the result is returned
412     // flattened by compiled code
413     InlineKlass* vk = method->returns_inline_type(thread);
414     if (vk != nullptr && vk->can_be_returned_as_fields()) {
415       oop instance = vk->allocate_instance(CHECK);
416       value_buffer = JNIHandles::make_local(thread, instance);
417       result->set_jobject(value_buffer);
418     }
419   }
420 
421   // do call
422   { JavaCallWrapper link(method, receiver, result, CHECK);
423     { HandleMark hm(thread);  // HandleMark used by HandleMarkCleaner
424 
425       // NOTE: if we move the computation of the result_val_address inside
426       // the call to call_stub, the optimizer produces wrong code.
427       intptr_t* result_val_address = (intptr_t*)(result->get_value_addr());
428       intptr_t* parameter_address = args->parameters();
429 #if INCLUDE_JVMCI
430       // Gets the alternative target (if any) that should be called
431       Handle alternative_target = args->alternative_target();
432       if (!alternative_target.is_null()) {
433         // Must extract verified entry point from HotSpotNmethod after VM to Java
434         // transition in JavaCallWrapper constructor so that it is safe with
435         // respect to nmethod sweeping.
436         address verified_entry_point = (address) HotSpotJVMCI::InstalledCode::entryPoint(nullptr, alternative_target());
437         if (verified_entry_point != nullptr) {
438           thread->set_jvmci_alternate_call_target(verified_entry_point);
439           entry_point = method->adapter()->get_i2c_entry();
440         }

451         args->size_of_parameters(),
452         CHECK
453       );
454 
455       result = link.result();  // circumvent MS C++ 5.0 compiler bug (result is clobbered across call)
456       // Preserve oop return value across possible gc points
457       if (oop_result_flag) {
458         thread->set_vm_result(result->get_oop());
459       }
460     }
461   } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
462 
463   // Check if a thread stop or suspend should be executed
464   // The following assert was not realistic.  Thread.stop can set that bit at any moment.
465   //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
466 
467   // Restore possible oop return
468   if (oop_result_flag) {
469     result->set_oop(thread->vm_result());
470     thread->set_vm_result(nullptr);
471     JNIHandles::destroy_local(value_buffer);
472   }
473 }
474 
475 
476 //--------------------------------------------------------------------------------------
477 // Implementation of JavaCallArguments
478 
479 inline bool is_value_state_indirect_oop(uint state) {
480   assert(state != JavaCallArguments::value_state_oop,
481          "Checking for handles after removal");
482   assert(state < JavaCallArguments::value_state_limit,
483          "Invalid value state %u", state);
484   return state != JavaCallArguments::value_state_primitive;
485 }
486 
487 inline oop resolve_indirect_oop(intptr_t value, uint state) {
488   switch (state) {
489   case JavaCallArguments::value_state_handle:
490   {
491     oop* ptr = reinterpret_cast<oop*>(value);

588     }
589 
590     check_value(true);          // Verify value state.
591   }
592 
593   friend class SignatureIterator;  // so do_parameters_on can call do_type
594   void do_type(BasicType type) {
595     switch (type) {
596     case T_BYTE:
597     case T_BOOLEAN:
598     case T_CHAR:
599     case T_SHORT:
600     case T_INT:
601     case T_FLOAT:  // this one also
602       check_single_word(); break;
603     case T_LONG:
604     case T_DOUBLE:
605       check_double_word(); break;
606     case T_ARRAY:
607     case T_OBJECT:
608     case T_PRIMITIVE_OBJECT:
609       check_reference(); break;
610     default:
611       ShouldNotReachHere();
612     }
613   }
614 };
615 
616 
617 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type) {
618   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
619 
620   // Treat T_OBJECT and T_ARRAY as the same
621   if (return_type == T_ARRAY) return_type = T_OBJECT;
622 
623   // Check that oop information is correct
624   Symbol* signature = method->signature();
625 
626   SignatureChekker sc(signature,
627                       return_type,
628                       method->is_static(),
629                       _value_state,
630                       _value);
631 }
< prev index next >