< 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/javaThread.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 

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

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













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

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












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

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

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

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

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

 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/javaThread.hpp"
 46 #include "runtime/jniHandles.inline.hpp"
 47 #include "runtime/mutexLocker.hpp"
 48 #include "runtime/os.inline.hpp"
 49 #include "runtime/sharedRuntime.hpp"
 50 #include "runtime/signature.hpp"
 51 #include "runtime/stubRoutines.hpp"
 52 #include "runtime/thread.inline.hpp"
 53 
 54 // -----------------------------------------------------
 55 // Implementation of JavaCallWrapper
 56 
 57 JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle receiver, JavaValue* result, TRAPS) {
 58   JavaThread* thread = THREAD;
 59 

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

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

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

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

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