< prev index next >

src/hotspot/share/runtime/javaCalls.cpp

Print this page

  1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 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 "classfile/vmSymbols.hpp"
 27 #include "code/nmethod.hpp"
 28 #include "compiler/compilationPolicy.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "interpreter/interpreter.hpp"
 31 #include "interpreter/linkResolver.hpp"
 32 #include "memory/universe.hpp"

 33 #include "oops/method.inline.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "prims/jniCheck.hpp"
 36 #include "prims/jvmtiExport.hpp"
 37 #include "runtime/handles.inline.hpp"
 38 #include "runtime/interfaceSupport.inline.hpp"
 39 #include "runtime/javaCalls.hpp"
 40 #include "runtime/javaThread.hpp"
 41 #include "runtime/jniHandles.inline.hpp"
 42 #include "runtime/mutexLocker.hpp"
 43 #include "runtime/os.inline.hpp"
 44 #include "runtime/sharedRuntime.hpp"
 45 #include "runtime/signature.hpp"
 46 #include "runtime/stubRoutines.hpp"
 47 #include "runtime/thread.inline.hpp"
 48 #if INCLUDE_JVMCI
 49 #include "jvmci/jvmciJavaClasses.hpp"
 50 #endif
 51 
 52 // -----------------------------------------------------

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

147 #endif
148     case T_BYTE   : // fall through
149     case T_VOID   : return T_INT;
150     case T_LONG   : return T_LONG;
151     case T_FLOAT  : return T_FLOAT;
152     case T_DOUBLE : return T_DOUBLE;
153 #ifdef _LP64
154     case T_ARRAY  : // fall through
155     case T_OBJECT:  return T_OBJECT;
156 #endif
157     default:
158       ShouldNotReachHere();
159       return T_ILLEGAL;
160   }
161 }
162 
163 // ============ Virtual calls ============
164 
165 void JavaCalls::call_virtual(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
166   CallInfo callinfo;
167   Handle receiver = args->receiver();
168   Klass* recvrKlass = receiver.is_null() ? (Klass*)nullptr : receiver->klass();
169   LinkInfo link_info(spec_klass, name, signature);
170   LinkResolver::resolve_virtual_call(
171           callinfo, receiver, recvrKlass, link_info, true, CHECK);
172   methodHandle method(THREAD, callinfo.selected_method());
173   assert(method.not_null(), "should have thrown exception");
174 
175   // Invoke the method

356   // Find receiver
357   Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
358 
359   // When we reenter Java, we need to re-enable the reserved/yellow zone which
360   // might already be disabled when we are in VM.
361   thread->stack_overflow_state()->reguard_stack_if_needed();
362 
363   // Check that there are shadow pages available before changing thread state
364   // to Java. Calculate current_stack_pointer here to make sure
365   // stack_shadow_pages_available() and map_stack_shadow_pages() use the same sp.
366   address sp = os::current_stack_pointer();
367   if (!os::stack_shadow_pages_available(THREAD, method, sp)) {
368     // Throw stack overflow exception with preinitialized exception.
369     Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
370     return;
371   } else {
372     // Touch pages checked if the OS needs them to be touched to be mapped.
373     os::map_stack_shadow_pages(sp);
374   }
375 












376   // do call
377   { JavaCallWrapper link(method, receiver, result, CHECK);
378     { HandleMark hm(thread);  // HandleMark used by HandleMarkCleaner
379 
380       // NOTE: if we move the computation of the result_val_address inside
381       // the call to call_stub, the optimizer produces wrong code.
382       intptr_t* result_val_address = (intptr_t*)(result->get_value_addr());
383       intptr_t* parameter_address = args->parameters();
384 
385       address entry_point;
386       {
387         // The enter_interp_only_mode use handshake to set interp_only mode
388         // so no safepoint should be allowed between is_interp_only_mode() and call
389         NoSafepointVerifier nsv;
390         if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {

391           entry_point = method->interpreter_entry();
392         } else {
393           // Since the call stub sets up like the interpreter we call the from_interpreted_entry
394           // so we can go compiled via a i2c.
395           entry_point = method->from_interpreted_entry();
396 #if INCLUDE_JVMCI
397           // Gets the alternative target (if any) that should be called
398           Handle alternative_target = args->alternative_target();
399           if (!alternative_target.is_null()) {
400             // Must extract verified entry point from HotSpotNmethod after VM to Java
401             // transition in JavaCallWrapper constructor so that it is safe with
402             // respect to nmethod sweeping.
403             address verified_entry_point = (address) HotSpotJVMCI::InstalledCode::entryPoint(nullptr, alternative_target());
404             if (verified_entry_point != nullptr) {
405               thread->set_jvmci_alternate_call_target(verified_entry_point);
406               entry_point = method->get_i2c_entry();
407             }
408           }
409 #endif
410         }

423           CHECK
424         );
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_oop(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_oop());
442     thread->set_vm_result_oop(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);

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 }

  1 /*
  2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 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 "classfile/vmSymbols.hpp"
 27 #include "code/nmethod.hpp"
 28 #include "compiler/compilationPolicy.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "interpreter/interpreter.hpp"
 31 #include "interpreter/linkResolver.hpp"
 32 #include "memory/universe.hpp"
 33 #include "oops/inlineKlass.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 // -----------------------------------------------------

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

358   // Find receiver
359   Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
360 
361   // When we reenter Java, we need to re-enable the reserved/yellow zone which
362   // might already be disabled when we are in VM.
363   thread->stack_overflow_state()->reguard_stack_if_needed();
364 
365   // Check that there are shadow pages available before changing thread state
366   // to Java. Calculate current_stack_pointer here to make sure
367   // stack_shadow_pages_available() and map_stack_shadow_pages() use the same sp.
368   address sp = os::current_stack_pointer();
369   if (!os::stack_shadow_pages_available(THREAD, method, sp)) {
370     // Throw stack overflow exception with preinitialized exception.
371     Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
372     return;
373   } else {
374     // Touch pages checked if the OS needs them to be touched to be mapped.
375     os::map_stack_shadow_pages(sp);
376   }
377 
378   jobject value_buffer = nullptr;
379   if (InlineTypeReturnedAsFields && (result->get_type() == T_OBJECT)) {
380     // Pre allocate a buffered inline type in case the result is returned
381     // flattened by compiled code
382     InlineKlass* vk = method->returns_inline_type();
383     if (vk != nullptr && vk->can_be_returned_as_fields()) {
384       oop instance = vk->allocate_instance(CHECK);
385       value_buffer = JNIHandles::make_local(thread, instance);
386       result->set_jobject(value_buffer);
387     }
388   }
389 
390   // do call
391   { JavaCallWrapper link(method, receiver, result, CHECK);
392     { HandleMark hm(thread);  // HandleMark used by HandleMarkCleaner
393 
394       // NOTE: if we move the computation of the result_val_address inside
395       // the call to call_stub, the optimizer produces wrong code.
396       intptr_t* result_val_address = (intptr_t*)(result->get_value_addr());
397       intptr_t* parameter_address = args->parameters();
398 
399       address entry_point;
400       {
401         // The enter_interp_only_mode use handshake to set interp_only mode
402         // so no safepoint should be allowed between is_interp_only_mode() and call
403         NoSafepointVerifier nsv;
404         bool is_interp_only_mode = (StressCallingConvention && (os::random() % (1 << 10)) == 0) || thread->is_interp_only_mode();
405         if (JvmtiExport::can_post_interpreter_events() && is_interp_only_mode) {
406           entry_point = method->interpreter_entry();
407         } else {
408           // Since the call stub sets up like the interpreter we call the from_interpreted_entry
409           // so we can go compiled via a i2c.
410           entry_point = method->from_interpreted_entry();
411 #if INCLUDE_JVMCI
412           // Gets the alternative target (if any) that should be called
413           Handle alternative_target = args->alternative_target();
414           if (!alternative_target.is_null()) {
415             // Must extract verified entry point from HotSpotNmethod after VM to Java
416             // transition in JavaCallWrapper constructor so that it is safe with
417             // respect to nmethod sweeping.
418             address verified_entry_point = (address) HotSpotJVMCI::InstalledCode::entryPoint(nullptr, alternative_target());
419             if (verified_entry_point != nullptr) {
420               thread->set_jvmci_alternate_call_target(verified_entry_point);
421               entry_point = method->get_i2c_entry();
422             }
423           }
424 #endif
425         }

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

587     case T_INT:
588     case T_FLOAT:  // this one also
589       check_single_word(); break;
590     case T_LONG:
591     case T_DOUBLE:
592       check_double_word(); break;
593     case T_ARRAY:
594     case T_OBJECT:
595       check_reference(); break;
596     default:
597       ShouldNotReachHere();
598     }
599   }
600 };
601 
602 
603 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type) {
604   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
605 
606   // Treat T_OBJECT and T_ARRAY as the same
607   if (return_type == T_ARRAY) return_type = T_OBJECT;
608 
609   // Check that oop information is correct
610   Symbol* signature = method->signature();
611 
612   SignatureChekker sc(signature,
613                       return_type,
614                       method->is_static(),
615                       _value_state,
616                       _value);
617 }
< prev index next >