< prev index next >

src/hotspot/share/runtime/javaCalls.cpp

Print this page

 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 #if INCLUDE_JVMCI
 34 #include "jvmci/jvmciJavaClasses.hpp"
 35 #endif
 36 #include "memory/universe.hpp"
 37 #include "oops/method.inline.hpp"
 38 #include "oops/oop.inline.hpp"

 39 #include "prims/jniCheck.hpp"
 40 #include "prims/jvmtiExport.hpp"
 41 #include "runtime/handles.inline.hpp"
 42 #include "runtime/interfaceSupport.inline.hpp"
 43 #include "runtime/javaCalls.hpp"
 44 #include "runtime/jniHandles.inline.hpp"
 45 #include "runtime/mutexLocker.hpp"
 46 #include "runtime/os.inline.hpp"
 47 #include "runtime/sharedRuntime.hpp"
 48 #include "runtime/signature.hpp"
 49 #include "runtime/stubRoutines.hpp"
 50 #include "runtime/thread.inline.hpp"
 51 
 52 // -----------------------------------------------------
 53 // Implementation of JavaCallWrapper
 54 
 55 JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle receiver, JavaValue* result, TRAPS) {
 56   JavaThread* thread = THREAD;
 57 
 58   guarantee(thread->is_Java_thread(), "crucial check - the VM thread cannot and must not escape to Java code");

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 #endif
158     case T_BYTE   : // fall through
159     case T_VOID   : return T_INT;
160     case T_LONG   : return T_LONG;
161     case T_FLOAT  : return T_FLOAT;
162     case T_DOUBLE : return T_DOUBLE;
163 #ifdef _LP64
164     case T_ARRAY  : // fall through
165     case T_OBJECT:  return T_OBJECT;

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

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













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

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












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

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

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

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

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

 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 #if INCLUDE_JVMCI
 34 #include "jvmci/jvmciJavaClasses.hpp"
 35 #endif
 36 #include "memory/universe.hpp"
 37 #include "oops/method.inline.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "oops/inlineKlass.hpp"
 40 #include "prims/jniCheck.hpp"
 41 #include "prims/jvmtiExport.hpp"
 42 #include "runtime/handles.inline.hpp"
 43 #include "runtime/interfaceSupport.inline.hpp"
 44 #include "runtime/javaCalls.hpp"
 45 #include "runtime/jniHandles.inline.hpp"
 46 #include "runtime/mutexLocker.hpp"
 47 #include "runtime/os.inline.hpp"
 48 #include "runtime/sharedRuntime.hpp"
 49 #include "runtime/signature.hpp"
 50 #include "runtime/stubRoutines.hpp"
 51 #include "runtime/thread.inline.hpp"
 52 
 53 // -----------------------------------------------------
 54 // Implementation of JavaCallWrapper
 55 
 56 JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle receiver, JavaValue* result, TRAPS) {
 57   JavaThread* thread = THREAD;
 58 
 59   guarantee(thread->is_Java_thread(), "crucial check - the VM thread cannot and must not escape to Java code");

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

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

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

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

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