24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "code/nmethod.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/linkResolver.hpp"
32 #include "memory/universe.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "prims/jniCheck.hpp"
35 #include "runtime/compilationPolicy.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/interfaceSupport.hpp"
38 #include "runtime/javaCalls.hpp"
39 #include "runtime/mutexLocker.hpp"
40 #include "runtime/os.inline.hpp"
41 #include "runtime/signature.hpp"
42 #include "runtime/stubRoutines.hpp"
43 #include "runtime/thread.inline.hpp"
44
45 // -----------------------------------------------------
46 // Implementation of JavaCallWrapper
47
48 JavaCallWrapper::JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS) {
49 JavaThread* thread = (JavaThread *)THREAD;
50 bool clear_pending_exception = true;
51
52 guarantee(thread->is_Java_thread(), "crucial check - the VM thread cannot and must not escape to Java code");
53 assert(!thread->owns_locks(), "must release all locks when leaving VM");
54 guarantee(!thread->is_Compiler_thread(), "cannot make java calls from the compiler");
55 _result = result;
56
57 // Allocate handle block for Java code. This must be done before we change thread_state to _thread_in_Java_or_stub,
58 // since it can potentially block.
59 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
60
61 // After this, we are official in JavaCode. This needs to be done before we change any of the thread local
62 // info, since we cannot find oops before the new information is set up completely.
63 ThreadStateTransition::transition(thread, _thread_in_vm, _thread_in_Java);
64
65 // Make sure that we handle asynchronous stops and suspends _before_ we clear all thread state
66 // in JavaCallWrapper::JavaCallWrapper(). This way, we can decide if we need to do any pd actions
67 // to prepare for stop/suspend (flush register windows on sparcs, cache sp, or other state).
68 if (thread->has_special_runtime_exit_condition()) {
69 thread->handle_special_runtime_exit_condition();
70 if (HAS_PENDING_EXCEPTION) {
71 clear_pending_exception = false;
72 }
73 }
74
292 // Need to wrap each and every time, since there might be native code down the
293 // stack that has installed its own exception handlers
294 os::os_exception_wrapper(call_helper, result, &method, args, THREAD);
295 }
296
297 void JavaCalls::call_helper(JavaValue* result, methodHandle* m, JavaCallArguments* args, TRAPS) {
298 // During dumping, Java execution environment is not fully initialized. Also, Java execution
299 // may cause undesirable side-effects in the class metadata.
300 assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
301
302 methodHandle method = *m;
303 JavaThread* thread = (JavaThread*)THREAD;
304 assert(thread->is_Java_thread(), "must be called by a java thread");
305 assert(method.not_null(), "must have a method to call");
306 assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
307 assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
308
309
310 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
311
312 // Verify the arguments
313
314 if (CheckJNICalls) {
315 args->verify(method, result->get_type(), thread);
316 }
317 else debug_only(args->verify(method, result->get_type(), thread));
318
319 // Ignore call if method is empty
320 if (method->is_empty_method()) {
321 assert(result->get_type() == T_VOID, "an empty method must return a void value");
322 return;
323 }
324
325
326 #ifdef ASSERT
327 { InstanceKlass* holder = method->method_holder();
328 // A klass might not be initialized since JavaCall's might be used during the executing of
329 // the <clinit>. For example, a Thread.start might start executing on an object that is
330 // not fully initialized! (bad Java programming style)
331 assert(holder->is_linked(), "rewriting must have taken place");
332 }
333 #endif
334
335
336 assert(!thread->is_Compiler_thread(), "cannot compile from the compiler");
337 if (CompilationPolicy::must_be_compiled(method)) {
338 CompileBroker::compile_method(method, InvocationEntryBci,
339 CompilationPolicy::policy()->initial_compile_level(),
340 methodHandle(), 0, "must_be_compiled", CHECK);
341 }
342
343 // Since the call stub sets up like the interpreter we call the from_interpreted_entry
344 // so we can go compiled via a i2c. Otherwise initial entry method will always
345 // run interpreted.
346 address entry_point = method->from_interpreted_entry();
347 if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
348 entry_point = method->interpreter_entry();
349 }
350
351 // Figure out if the result value is an oop or not (Note: This is a different value
352 // than result_type. result_type will be T_INT of oops. (it is about size)
353 BasicType result_type = runtime_type_from(result);
354 bool oop_result_flag = (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY);
355
356 // NOTE: if we move the computation of the result_val_address inside
360 // Find receiver
361 Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
362
363 // When we reenter Java, we need to reenable the yellow zone which
364 // might already be disabled when we are in VM.
365 if (thread->stack_yellow_zone_disabled()) {
366 thread->reguard_stack();
367 }
368
369 // Check that there are shadow pages available before changing thread state
370 // to Java
371 if (!os::stack_shadow_pages_available(THREAD, method)) {
372 // Throw stack overflow exception with preinitialized exception.
373 Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
374 return;
375 } else {
376 // Touch pages checked if the OS needs them to be touched to be mapped.
377 os::bang_stack_shadow_pages();
378 }
379
380 // do call
381 { JavaCallWrapper link(method, receiver, result, CHECK);
382 { HandleMark hm(thread); // HandleMark used by HandleMarkCleaner
383
384 StubRoutines::call_stub()(
385 (address)&link,
386 // (intptr_t*)&(result->_value), // see NOTE above (compiler problem)
387 result_val_address, // see NOTE above (compiler problem)
388 result_type,
389 method(),
390 entry_point,
391 args->parameters(),
392 args->size_of_parameters(),
393 CHECK
394 );
395
396 result = link.result(); // circumvent MS C++ 5.0 compiler bug (result is clobbered across call)
397 // Preserve oop return value across possible gc points
398 if (oop_result_flag) {
399 thread->set_vm_result((oop) result->get_jobject());
|
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "code/nmethod.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/linkResolver.hpp"
32 #include "memory/universe.inline.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "prims/jniCheck.hpp"
35 #include "runtime/compilationPolicy.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/interfaceSupport.hpp"
38 #include "runtime/javaCalls.hpp"
39 #include "runtime/mutexLocker.hpp"
40 #include "runtime/os.inline.hpp"
41 #include "runtime/signature.hpp"
42 #include "runtime/stubRoutines.hpp"
43 #include "runtime/thread.inline.hpp"
44 #if INCLUDE_JVMCI
45 #include "jvmci/jvmciJavaClasses.hpp"
46 #include "jvmci/jvmciRuntime.hpp"
47 #endif
48
49 // -----------------------------------------------------
50 // Implementation of JavaCallWrapper
51
52 JavaCallWrapper::JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS) {
53 JavaThread* thread = (JavaThread *)THREAD;
54 bool clear_pending_exception = true;
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 _result = result;
60
61 // Allocate handle block for Java code. This must be done before we change thread_state to _thread_in_Java_or_stub,
62 // since it can potentially block.
63 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
64
65 // After this, we are official in JavaCode. This needs to be done before we change any of the thread local
66 // info, since we cannot find oops before the new information is set up completely.
67 ThreadStateTransition::transition(thread, _thread_in_vm, _thread_in_Java);
68
69 // Make sure that we handle asynchronous stops and suspends _before_ we clear all thread state
70 // in JavaCallWrapper::JavaCallWrapper(). This way, we can decide if we need to do any pd actions
71 // to prepare for stop/suspend (flush register windows on sparcs, cache sp, or other state).
72 if (thread->has_special_runtime_exit_condition()) {
73 thread->handle_special_runtime_exit_condition();
74 if (HAS_PENDING_EXCEPTION) {
75 clear_pending_exception = false;
76 }
77 }
78
296 // Need to wrap each and every time, since there might be native code down the
297 // stack that has installed its own exception handlers
298 os::os_exception_wrapper(call_helper, result, &method, args, THREAD);
299 }
300
301 void JavaCalls::call_helper(JavaValue* result, methodHandle* m, JavaCallArguments* args, TRAPS) {
302 // During dumping, Java execution environment is not fully initialized. Also, Java execution
303 // may cause undesirable side-effects in the class metadata.
304 assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
305
306 methodHandle method = *m;
307 JavaThread* thread = (JavaThread*)THREAD;
308 assert(thread->is_Java_thread(), "must be called by a java thread");
309 assert(method.not_null(), "must have a method to call");
310 assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
311 assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
312
313
314 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
315
316 #if INCLUDE_JVMCI
317 // Gets the nmethod (if any) that should be called instead of normal target
318 nmethod* alternative_target = args->alternative_target();
319 if (alternative_target == NULL) {
320 #endif
321 // Verify the arguments
322
323 if (CheckJNICalls) {
324 args->verify(method, result->get_type(), thread);
325 }
326 else debug_only(args->verify(method, result->get_type(), thread));
327 #if INCLUDE_JVMCI
328 }
329 #else
330
331 // Ignore call if method is empty
332 if (method->is_empty_method()) {
333 assert(result->get_type() == T_VOID, "an empty method must return a void value");
334 return;
335 }
336 #endif
337
338 #ifdef ASSERT
339 { InstanceKlass* holder = method->method_holder();
340 // A klass might not be initialized since JavaCall's might be used during the executing of
341 // the <clinit>. For example, a Thread.start might start executing on an object that is
342 // not fully initialized! (bad Java programming style)
343 assert(holder->is_linked(), "rewriting must have taken place");
344 }
345 #endif
346
347
348 assert(thread->can_call_java(), "cannot compile from the native compiler");
349 if (CompilationPolicy::must_be_compiled(method)) {
350 CompileBroker::compile_method(method, InvocationEntryBci,
351 CompilationPolicy::policy()->initial_compile_level(),
352 methodHandle(), 0, "must_be_compiled", CHECK);
353 }
354
355 // Since the call stub sets up like the interpreter we call the from_interpreted_entry
356 // so we can go compiled via a i2c. Otherwise initial entry method will always
357 // run interpreted.
358 address entry_point = method->from_interpreted_entry();
359 if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
360 entry_point = method->interpreter_entry();
361 }
362
363 // Figure out if the result value is an oop or not (Note: This is a different value
364 // than result_type. result_type will be T_INT of oops. (it is about size)
365 BasicType result_type = runtime_type_from(result);
366 bool oop_result_flag = (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY);
367
368 // NOTE: if we move the computation of the result_val_address inside
372 // Find receiver
373 Handle receiver = (!method->is_static()) ? args->receiver() : Handle();
374
375 // When we reenter Java, we need to reenable the yellow zone which
376 // might already be disabled when we are in VM.
377 if (thread->stack_yellow_zone_disabled()) {
378 thread->reguard_stack();
379 }
380
381 // Check that there are shadow pages available before changing thread state
382 // to Java
383 if (!os::stack_shadow_pages_available(THREAD, method)) {
384 // Throw stack overflow exception with preinitialized exception.
385 Exceptions::throw_stack_overflow_exception(THREAD, __FILE__, __LINE__, method);
386 return;
387 } else {
388 // Touch pages checked if the OS needs them to be touched to be mapped.
389 os::bang_stack_shadow_pages();
390 }
391
392 #if INCLUDE_JVMCI
393 if (alternative_target != NULL) {
394 if (alternative_target->is_alive()) {
395 thread->set_jvmci_alternate_call_target(alternative_target->verified_entry_point());
396 entry_point = method->adapter()->get_i2c_entry();
397 } else {
398 THROW(vmSymbols::jdk_vm_ci_code_InvalidInstalledCodeException());
399 }
400 }
401 #endif
402
403 // do call
404 { JavaCallWrapper link(method, receiver, result, CHECK);
405 { HandleMark hm(thread); // HandleMark used by HandleMarkCleaner
406
407 StubRoutines::call_stub()(
408 (address)&link,
409 // (intptr_t*)&(result->_value), // see NOTE above (compiler problem)
410 result_val_address, // see NOTE above (compiler problem)
411 result_type,
412 method(),
413 entry_point,
414 args->parameters(),
415 args->size_of_parameters(),
416 CHECK
417 );
418
419 result = link.result(); // circumvent MS C++ 5.0 compiler bug (result is clobbered across call)
420 // Preserve oop return value across possible gc points
421 if (oop_result_flag) {
422 thread->set_vm_result((oop) result->get_jobject());
|