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 
 50 // -----------------------------------------------------
 51 // Implementation of JavaCallWrapper
 52 
 53 JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle receiver, JavaValue* result, TRAPS) {
 54   JavaThread* thread = THREAD;
 55 
 56   guarantee(thread->is_Java_thread(), "crucial check - the VM thread cannot and must not escape to Java code");
 57   assert(!thread->owns_locks(), "must release all locks when leaving VM");
 58   guarantee(thread->can_call_java(), "cannot make java calls from the native compiler");
 59   assert(!thread->preempting(), "Unexpected Java upcall whilst processing preemption");
 60   _result   = result;
 61 
 62   // Allocate handle block for Java code. This must be done before we change thread_state to _thread_in_Java_or_stub,
 63   // since it can potentially block.
 64   JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
 65 
 66   // clear any pending exception in thread (native calls start with no exception pending)
 67   thread->clear_pending_exception();
 68 
 69   // After this, we are official in JavaCode. This needs to be done before we change any of the thread local
 70   // info, since we cannot find oops before the new information is set up completely.
 71   ThreadStateTransition::transition_from_vm(thread, _thread_in_Java, true /* check_asyncs */);
 72 
 73   // Make sure to set the oop's after the thread transition - since we can block there. No one is GC'ing
 74   // the JavaCallWrapper before the entry frame is on the stack.
 75   _callee_method = callee_method();
 76   _receiver = receiver();
 77 
 78 #ifdef CHECK_UNHANDLED_OOPS
 79   THREAD->allow_unhandled_oop(&_receiver);
 80 #endif // CHECK_UNHANDLED_OOPS
 81 
 82   _thread       = thread;
 83   _handles      = _thread->active_handles();    // save previous handle block & Java frame linkage
 84 
 85   // For the profiler, the last_Java_frame information in thread must always be in
 86   // legal state. We have no last Java frame if last_Java_sp == nullptr so
 87   // the valid transition is to clear _last_Java_sp and then reset the rest of
 88   // the (platform specific) state.
 89 
 90   _anchor.copy(_thread->frame_anchor());
 91   _thread->frame_anchor()->clear();
 92 
 93   DEBUG_ONLY(_thread->inc_java_call_counter());
 94   _thread->set_active_handles(new_handles);     // install new handle block and reset Java frame linkage
 95 }
 96 
 97 
 98 JavaCallWrapper::~JavaCallWrapper() {
 99   assert(_thread == JavaThread::current(), "must still be the same thread");
100 
101   // restore previous handle block & Java frame linkage
102   JNIHandleBlock *_old_handles = _thread->active_handles();
103   _thread->set_active_handles(_handles);
104 
105   _thread->frame_anchor()->zap();
106 
107   DEBUG_ONLY(_thread->dec_java_call_counter());
108 
109   // Old thread-local info. has been restored. We are not back in the VM.
110   ThreadStateTransition::transition_from_java(_thread, _thread_in_vm);
111 
112   // State has been restored now make the anchor frame visible for the profiler.
113   _thread->frame_anchor()->copy(&_anchor);
114 
115   // Release handles after we are marked as being inside the VM again, since this
116   // operation might block
117   JNIHandleBlock::release_block(_old_handles, _thread);
118 
119   if (_thread->has_pending_exception() && _thread->has_last_Java_frame()) {
120     // If we get here, the Java code threw an exception that unwound a frame.
121     // It could be that the new frame anchor has not passed through the required
122     // StackWatermark barriers. Therefore, we process any such deferred unwind
123     // requests here.
124     StackWatermarkSet::after_unwind(_thread);
125   }
126 }
127 
128 
129 void JavaCallWrapper::oops_do(OopClosure* f) {
130   f->do_oop((oop*)&_receiver);
131   handles()->oops_do(f);
132 }
133 
134 
135 // Helper methods
136 static BasicType runtime_type_from(JavaValue* result) {
137   switch (result->get_type()) {
138     case T_BOOLEAN  : // fall through
139     case T_CHAR     : // fall through
140     case T_SHORT    : // fall through
141     case T_INT      : // fall through
142 #ifndef _LP64
143     case T_OBJECT   : // fall through
144     case T_ARRAY    : // fall through
145     case T_FLAT_ELEMENT: // fall through
146 #endif
147     case T_BYTE     : // fall through
148     case T_VOID     : return T_INT;
149     case T_LONG     : return T_LONG;
150     case T_FLOAT    : return T_FLOAT;
151     case T_DOUBLE   : return T_DOUBLE;
152 #ifdef _LP64
153     case T_ARRAY    : // fall through
154     case T_OBJECT   : return T_OBJECT;
155 #endif
156     default:
157       ShouldNotReachHere();
158       return T_ILLEGAL;
159   }
160 }
161 
162 // ============ Virtual calls ============
163 
164 void JavaCalls::call_virtual(JavaValue* result, Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
165   CallInfo callinfo;
166   Handle receiver = args->receiver();
167   Klass* recvrKlass = receiver.is_null() ? (Klass*)nullptr : receiver->klass();
168   LinkInfo link_info(spec_klass, name, signature);
169   LinkResolver::resolve_virtual_call(
170           callinfo, receiver, recvrKlass, link_info, true, CHECK);
171   methodHandle method(THREAD, callinfo.selected_method());
172   assert(method.not_null(), "should have thrown exception");
173 
174   // Invoke the method
175   JavaCalls::call(result, method, args, CHECK);
176 }
177 
178 
179 void JavaCalls::call_virtual(JavaValue* result, Handle receiver, Klass* spec_klass, Symbol* name, Symbol* signature, TRAPS) {
180   JavaCallArguments args(receiver);
181   call_virtual(result, spec_klass, name, signature, &args, CHECK);
182 }
183 
184 
185 void JavaCalls::call_virtual(JavaValue* result, Handle receiver, Klass* spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS) {
186   JavaCallArguments args(receiver);
187   args.push_oop(arg1);
188   call_virtual(result, spec_klass, name, signature, &args, CHECK);
189 }
190 
191 
192 
193 void JavaCalls::call_virtual(JavaValue* result, Handle receiver, Klass* spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS) {
194   JavaCallArguments args(receiver);
195   args.push_oop(arg1);
196   args.push_oop(arg2);
197   call_virtual(result, spec_klass, name, signature, &args, CHECK);
198 }
199 
200 
201 // ============ Special calls ============
202 
203 void JavaCalls::call_special(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
204   CallInfo callinfo;
205   LinkInfo link_info(klass, name, signature);
206   LinkResolver::resolve_special_call(callinfo, args->receiver(), link_info, CHECK);
207   methodHandle method(THREAD, callinfo.selected_method());
208   assert(method.not_null(), "should have thrown exception");
209 
210   // Invoke the method
211   JavaCalls::call(result, method, args, CHECK);
212 }
213 
214 
215 void JavaCalls::call_special(JavaValue* result, Handle receiver, Klass* klass, Symbol* name, Symbol* signature, TRAPS) {
216   JavaCallArguments args(receiver);
217   call_special(result, klass, name, signature, &args, CHECK);
218 }
219 
220 
221 void JavaCalls::call_special(JavaValue* result, Handle receiver, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS) {
222   JavaCallArguments args(receiver);
223   args.push_oop(arg1);
224   call_special(result, klass, name, signature, &args, CHECK);
225 }
226 
227 
228 void JavaCalls::call_special(JavaValue* result, Handle receiver, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS) {
229   JavaCallArguments args(receiver);
230   args.push_oop(arg1);
231   args.push_oop(arg2);
232   call_special(result, klass, name, signature, &args, CHECK);
233 }
234 
235 
236 // ============ Static calls ============
237 
238 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) {
239   CallInfo callinfo;
240   LinkInfo link_info(klass, name, signature);
241   LinkResolver::resolve_static_call(callinfo, link_info, ClassInitMode::init, CHECK);
242   methodHandle method(THREAD, callinfo.selected_method());
243   assert(method.not_null(), "should have thrown exception");
244 
245   // Invoke the method
246   JavaCalls::call(result, method, args, CHECK);
247 }
248 
249 
250 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, TRAPS) {
251   JavaCallArguments args;
252   call_static(result, klass, name, signature, &args, CHECK);
253 }
254 
255 
256 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS) {
257   JavaCallArguments args(arg1);
258   call_static(result, klass, name, signature, &args, CHECK);
259 }
260 
261 
262 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS) {
263   JavaCallArguments args;
264   args.push_oop(arg1);
265   args.push_oop(arg2);
266   call_static(result, klass, name, signature, &args, CHECK);
267 }
268 
269 
270 void JavaCalls::call_static(JavaValue* result, Klass* klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, Handle arg3, TRAPS) {
271   JavaCallArguments args;
272   args.push_oop(arg1);
273   args.push_oop(arg2);
274   args.push_oop(arg3);
275   call_static(result, klass, name, signature, &args, CHECK);
276 }
277 
278 // ============ allocate and initialize new object instance ============
279 
280 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, JavaCallArguments* args, TRAPS) {
281   klass->initialize(CHECK_NH); // Quick no-op if already initialized.
282   Handle obj = klass->allocate_instance_handle(CHECK_NH);
283   JavaValue void_result(T_VOID);
284   args->set_receiver(obj); // inserts <obj> as the first argument.
285   JavaCalls::call_special(&void_result, klass,
286                           vmSymbols::object_initializer_name(),
287                           constructor_signature, args, CHECK_NH);
288   // Already returned a Null Handle if any exception is pending.
289   return obj;
290 }
291 
292 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, TRAPS) {
293   JavaCallArguments args;
294   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
295 }
296 
297 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, Handle arg1, TRAPS) {
298   JavaCallArguments args;
299   args.push_oop(arg1);
300   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
301 }
302 
303 Handle JavaCalls::construct_new_instance(InstanceKlass* klass, Symbol* constructor_signature, Handle arg1, Handle arg2, TRAPS) {
304   JavaCallArguments args;
305   args.push_oop(arg1);
306   args.push_oop(arg2);
307   return JavaCalls::construct_new_instance(klass, constructor_signature, &args, THREAD);
308 }
309 
310 // -------------------------------------------------
311 // Implementation of JavaCalls (low level)
312 
313 
314 void JavaCalls::call(JavaValue* result, const methodHandle& method, JavaCallArguments* args, TRAPS) {
315   // Check if we need to wrap a potential OS exception handler around thread.
316   // This is used for e.g. Win32 structured exception handlers.
317   // Need to wrap each and every time, since there might be native code down the
318   // stack that has installed its own exception handlers.
319   os::os_exception_wrapper(call_helper, result, method, args, THREAD);
320 }
321 
322 void JavaCalls::call_helper(JavaValue* result, const methodHandle& method, JavaCallArguments* args, TRAPS) {
323 
324   JavaThread* thread = THREAD;
325   assert(method.not_null(), "must have a method to call");
326   assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
327   assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
328 
329   // Verify the arguments
330   if ((DEBUG_ONLY(true ||) CheckJNICalls)) {
331     args->verify(method, result->get_type());
332   }
333   // Ignore call if method is empty
334   if (method->is_empty_method()) {
335     assert(result->get_type() == T_VOID, "an empty method must return a void value");
336     return;
337   }
338 
339 #ifdef ASSERT
340   { InstanceKlass* holder = method->method_holder();
341     // A klass might not be initialized since JavaCall's might be used during the executing of
342     // the <clinit>. For example, a Thread.start might start executing on an object that is
343     // not fully initialized! (bad Java programming style)
344     assert(holder->is_linked(), "rewriting must have taken place");
345   }
346 #endif
347 
348   CompilationPolicy::compile_if_required(method, CHECK);
349 
350   // Figure out if the result value is an oop or not (Note: This is a different value
351   // than result_type. result_type will be T_INT of oops. (it is about size)
352   BasicType result_type = runtime_type_from(result);
353   bool oop_result_flag = is_reference_type(result->get_type());
354 
355   // Find receiver
356   Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
357 
358   // When we reenter Java, we need to re-enable the reserved/yellow zone which
359   // might already be disabled when we are in VM.
360   thread->stack_overflow_state()->reguard_stack_if_needed();
361 
362   // Check that there are shadow pages available before changing thread state
363   // to Java. Calculate current_stack_pointer here to make sure
364   // stack_shadow_pages_available() and map_stack_shadow_pages() use the same sp.
365   address sp = os::current_stack_pointer();
366   if (!os::stack_shadow_pages_available(THREAD, method, sp)) {
367     // Throw stack overflow exception with preinitialized exception.
368     Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
369     return;
370   } else {
371     // Touch pages checked if the OS needs them to be touched to be mapped.
372     os::map_stack_shadow_pages(sp);
373   }
374 
375   jobject value_buffer = nullptr;
376   if (InlineTypeReturnedAsFields && (result->get_type() == T_OBJECT)) {
377     // Pre allocate a buffered inline type in case the result is returned
378     // flattened by compiled code
379     InlineKlass* vk = method->returns_inline_type();
380     if (vk != nullptr && vk->can_be_returned_as_fields()) {
381       oop instance = vk->allocate_instance(CHECK);
382       value_buffer = JNIHandles::make_local(thread, instance);
383       result->set_jobject(value_buffer);
384     }
385   }
386 
387   // do call
388   { JavaCallWrapper link(method, receiver, result, CHECK);
389     { HandleMark hm(thread);  // HandleMark used by HandleMarkCleaner
390 
391       // NOTE: if we move the computation of the result_val_address inside
392       // the call to call_stub, the optimizer produces wrong code.
393       intptr_t* result_val_address = (intptr_t*)(result->get_value_addr());
394       intptr_t* parameter_address = args->parameters();
395 
396       address entry_point;
397       {
398         // The enter_interp_only_mode use handshake to set interp_only mode
399         // so no safepoint should be allowed between is_interp_only_mode() and call
400         NoSafepointVerifier nsv;
401         bool is_interp_only_mode = (StressCallingConvention && (os::random() % (1 << 10)) == 0) || thread->is_interp_only_mode();
402         if (JvmtiExport::can_post_interpreter_events() && is_interp_only_mode) {
403           entry_point = method->interpreter_entry();
404         } else {
405           // Since the call stub sets up like the interpreter we call the from_interpreted_entry
406           // so we can go compiled via a i2c.
407           entry_point = method->from_interpreted_entry();
408         }
409       }
410       {
411         MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, thread));
412         StubRoutines::call_stub()(
413           (address)&link,
414           // (intptr_t*)&(result->_value), // see NOTE above (compiler problem)
415           result_val_address,          // see NOTE above (compiler problem)
416           result_type,
417           method(),
418           entry_point,
419           parameter_address,
420           args->size_of_parameters(),
421           CHECK
422         );
423       }
424 
425       result = link.result();  // circumvent MS C++ 5.0 compiler bug (result is clobbered across call)
426       // Preserve oop return value across possible gc points
427       if (oop_result_flag) {
428         thread->set_vm_result_oop(result->get_oop());
429       }
430     }
431   } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
432 
433   // Check if a thread stop or suspend should be executed
434   // The following assert was not realistic.  Thread.stop can set that bit at any moment.
435   //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
436 
437   // Restore possible oop return
438   if (oop_result_flag) {
439     result->set_oop(thread->vm_result_oop());
440     thread->set_vm_result_oop(nullptr);
441     JNIHandles::destroy_local(value_buffer);
442   }
443 }
444 
445 
446 //--------------------------------------------------------------------------------------
447 // Implementation of JavaCallArguments
448 
449 inline bool is_value_state_indirect_oop(uint state) {
450   assert(state != JavaCallArguments::value_state_oop,
451          "Checking for handles after removal");
452   assert(state < JavaCallArguments::value_state_limit,
453          "Invalid value state %u", state);
454   return state != JavaCallArguments::value_state_primitive;
455 }
456 
457 inline oop resolve_indirect_oop(intptr_t value, uint state) {
458   switch (state) {
459   case JavaCallArguments::value_state_handle:
460   {
461     oop* ptr = reinterpret_cast<oop*>(value);
462     return Handle::raw_resolve(ptr);
463   }
464 
465   case JavaCallArguments::value_state_jobject:
466   {
467     jobject obj = reinterpret_cast<jobject>(value);
468     return JNIHandles::resolve(obj);
469   }
470 
471   default:
472     ShouldNotReachHere();
473     return nullptr;
474   }
475 }
476 
477 intptr_t* JavaCallArguments::parameters() {
478   // First convert all handles to oops
479   for(int i = 0; i < _size; i++) {
480     uint state = _value_state[i];
481     assert(state != value_state_oop, "Multiple handle conversions");
482     if (is_value_state_indirect_oop(state)) {
483       oop obj = resolve_indirect_oop(_value[i], state);
484       _value[i] = cast_from_oop<intptr_t>(obj);
485       _value_state[i] = value_state_oop;
486     }
487   }
488   // Return argument vector
489   return _value;
490 }
491 
492 
493 class SignatureChekker : public SignatureIterator {
494  private:
495    int _pos;
496    BasicType _return_type;
497    u_char* _value_state;
498    intptr_t* _value;
499 
500  public:
501   SignatureChekker(Symbol* signature,
502                    BasicType return_type,
503                    bool is_static,
504                    u_char* value_state,
505                    intptr_t* value) :
506     SignatureIterator(signature),
507     _pos(0),
508     _return_type(return_type),
509     _value_state(value_state),
510     _value(value)
511   {
512     if (!is_static) {
513       check_value(true); // Receiver must be an oop
514     }
515     do_parameters_on(this);
516     check_return_type(return_type);
517   }
518 
519  private:
520   void check_value(bool is_reference) {
521     uint state = _value_state[_pos++];
522     if (is_reference) {
523       guarantee(is_value_state_indirect_oop(state),
524                 "signature does not match pushed arguments: %u at %d",
525                 state, _pos - 1);
526     } else {
527       guarantee(state == JavaCallArguments::value_state_primitive,
528                 "signature does not match pushed arguments: %u at %d",
529                 state, _pos - 1);
530     }
531   }
532 
533   void check_return_type(BasicType t) {
534     guarantee(t == _return_type, "return type does not match");
535   }
536 
537   void check_single_word() {
538     check_value(false);
539   }
540 
541   void check_double_word() {
542     check_value(false);
543     check_value(false);
544   }
545 
546   void check_reference() {
547     intptr_t v = _value[_pos];
548     if (v != 0) {
549       // v is a "handle" referring to an oop, cast to integral type.
550       // There shouldn't be any handles in very low memory.
551       guarantee((size_t)v >= os::vm_page_size(),
552                 "Bad JNI oop argument %d: " PTR_FORMAT, _pos, v);
553       // Verify the pointee.
554       oop vv = resolve_indirect_oop(v, _value_state[_pos]);
555       guarantee(oopDesc::is_oop_or_null(vv),
556                 "Bad JNI oop argument %d: " PTR_FORMAT " -> " PTR_FORMAT,
557                 _pos, v, p2i(vv));
558     }
559 
560     check_value(true);          // Verify value state.
561   }
562 
563   friend class SignatureIterator;  // so do_parameters_on can call do_type
564   void do_type(BasicType type) {
565     switch (type) {
566     case T_BYTE:
567     case T_BOOLEAN:
568     case T_CHAR:
569     case T_SHORT:
570     case T_INT:
571     case T_FLOAT:  // this one also
572       check_single_word(); break;
573     case T_LONG:
574     case T_DOUBLE:
575       check_double_word(); break;
576     case T_ARRAY:
577     case T_OBJECT:
578       check_reference(); break;
579     default:
580       ShouldNotReachHere();
581     }
582   }
583 };
584 
585 
586 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type) {
587   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
588 
589   // Treat T_OBJECT and T_ARRAY as the same
590   if (return_type == T_ARRAY) return_type = T_OBJECT;
591 
592   // Check that oop information is correct
593   Symbol* signature = method->signature();
594 
595   SignatureChekker sc(signature,
596                       return_type,
597                       method->is_static(),
598                       _value_state,
599                       _value);
600 }