1 /*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
308 }
309
310 void JavaCalls::call_helper(JavaValue* result, methodHandle* m, JavaCallArguments* args, TRAPS) {
311 // During dumping, Java execution environment is not fully initialized. Also, Java execution
312 // may cause undesirable side-effects in the class metadata.
313 assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
314
315 methodHandle method = *m;
316 JavaThread* thread = (JavaThread*)THREAD;
317 assert(thread->is_Java_thread(), "must be called by a java thread");
318 assert(method.not_null(), "must have a method to call");
319 assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
320 assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
321
322
323 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
324
325 // Verify the arguments
326
327 if (CheckJNICalls) {
328 args->verify(method, result->get_type(), thread);
329 }
330 else debug_only(args->verify(method, result->get_type(), thread));
331
332 // Ignore call if method is empty
333 if (method->is_empty_method()) {
334 assert(result->get_type() == T_VOID, "an empty method must return a void value");
335 return;
336 }
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(), "rewritting must have taken place");
345 }
346 #endif
347
348
349 assert(!thread->is_Compiler_thread(), "cannot compile from the compiler");
350 if (CompilationPolicy::must_be_compiled(method)) {
412 thread->set_vm_result((oop) result->get_jobject());
413 }
414 }
415 } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
416
417 // Check if a thread stop or suspend should be executed
418 // The following assert was not realistic. Thread.stop can set that bit at any moment.
419 //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
420
421 // Restore possible oop return
422 if (oop_result_flag) {
423 result->set_jobject((jobject)thread->vm_result());
424 thread->set_vm_result(NULL);
425 }
426 }
427
428
429 //--------------------------------------------------------------------------------------
430 // Implementation of JavaCallArguments
431
432 intptr_t* JavaCallArguments::parameters() {
433 // First convert all handles to oops
434 for(int i = 0; i < _size; i++) {
435 if (_is_oop[i]) {
436 // Handle conversion
437 _value[i] = cast_from_oop<intptr_t>(Handle::raw_resolve((oop *)_value[i]));
438 }
439 }
440 // Return argument vector
441 return _value;
442 }
443
444
445 class SignatureChekker : public SignatureIterator {
446 private:
447 bool *_is_oop;
448 int _pos;
449 BasicType _return_type;
450 intptr_t* _value;
451 Thread* _thread;
452
453 public:
454 bool _is_return;
455
456 SignatureChekker(Symbol* signature, BasicType return_type, bool is_static, bool* is_oop, intptr_t* value, Thread* thread) : SignatureIterator(signature) {
457 _is_oop = is_oop;
458 _is_return = false;
459 _return_type = return_type;
460 _pos = 0;
461 _value = value;
462 _thread = thread;
463
464 if (!is_static) {
465 check_value(true); // Receiver must be an oop
466 }
467 }
468
469 void check_value(bool type) {
470 guarantee(_is_oop[_pos++] == type, "signature does not match pushed arguments");
471 }
472
473 void check_doing_return(bool state) { _is_return = state; }
474
475 void check_return_type(BasicType t) {
476 guarantee(_is_return && t == _return_type, "return type does not match");
477 }
478
479 void check_int(BasicType t) {
480 if (_is_return) {
481 check_return_type(t);
482 return;
483 }
484 check_value(false);
485 }
486
487 void check_double(BasicType t) { check_long(t); }
488
489 void check_long(BasicType t) {
490 if (_is_return) {
491 check_return_type(t);
492 return;
493 }
494
495 check_value(false);
496 check_value(false);
497 }
498
499 void check_obj(BasicType t) {
500 if (_is_return) {
501 check_return_type(t);
502 return;
503 }
504
505 // verify handle and the oop pointed to by handle
506 int p = _pos;
507 bool bad = false;
508 // If argument is oop
509 if (_is_oop[p]) {
510 intptr_t v = _value[p];
511 if (v != 0 ) {
512 size_t t = (size_t)v;
513 bad = (t < (size_t)os::vm_page_size() ) || !Handle::raw_resolve((oop *)v)->is_oop_or_null(true);
514 if (CheckJNICalls && bad) {
515 ReportJNIFatalError((JavaThread*)_thread, "Bad JNI oop argument");
516 }
517 }
518 // for the regular debug case.
519 assert(!bad, "Bad JNI oop argument");
520 }
521
522 check_value(true);
523 }
524
525 void do_bool() { check_int(T_BOOLEAN); }
526 void do_char() { check_int(T_CHAR); }
527 void do_float() { check_int(T_FLOAT); }
528 void do_double() { check_double(T_DOUBLE); }
529 void do_byte() { check_int(T_BYTE); }
530 void do_short() { check_int(T_SHORT); }
531 void do_int() { check_int(T_INT); }
532 void do_long() { check_long(T_LONG); }
533 void do_void() { check_return_type(T_VOID); }
534 void do_object(int begin, int end) { check_obj(T_OBJECT); }
535 void do_array(int begin, int end) { check_obj(T_OBJECT); }
536 };
537
538
539 void JavaCallArguments::verify(methodHandle method, BasicType return_type,
540 Thread *thread) {
541 guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
542
543 // Treat T_OBJECT and T_ARRAY as the same
544 if (return_type == T_ARRAY) return_type = T_OBJECT;
545
546 // Check that oop information is correct
547 Symbol* signature = method->signature();
548
549 SignatureChekker sc(signature, return_type, method->is_static(),_is_oop, _value, thread);
550 sc.iterate_parameters();
551 sc.check_doing_return(true);
552 sc.iterate_returntype();
553 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
308 }
309
310 void JavaCalls::call_helper(JavaValue* result, methodHandle* m, JavaCallArguments* args, TRAPS) {
311 // During dumping, Java execution environment is not fully initialized. Also, Java execution
312 // may cause undesirable side-effects in the class metadata.
313 assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
314
315 methodHandle method = *m;
316 JavaThread* thread = (JavaThread*)THREAD;
317 assert(thread->is_Java_thread(), "must be called by a java thread");
318 assert(method.not_null(), "must have a method to call");
319 assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
320 assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
321
322
323 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
324
325 // Verify the arguments
326
327 if (CheckJNICalls) {
328 args->verify(method, result->get_type());
329 }
330 else debug_only(args->verify(method, result->get_type()));
331
332 // Ignore call if method is empty
333 if (method->is_empty_method()) {
334 assert(result->get_type() == T_VOID, "an empty method must return a void value");
335 return;
336 }
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(), "rewritting must have taken place");
345 }
346 #endif
347
348
349 assert(!thread->is_Compiler_thread(), "cannot compile from the compiler");
350 if (CompilationPolicy::must_be_compiled(method)) {
412 thread->set_vm_result((oop) result->get_jobject());
413 }
414 }
415 } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
416
417 // Check if a thread stop or suspend should be executed
418 // The following assert was not realistic. Thread.stop can set that bit at any moment.
419 //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
420
421 // Restore possible oop return
422 if (oop_result_flag) {
423 result->set_jobject((jobject)thread->vm_result());
424 thread->set_vm_result(NULL);
425 }
426 }
427
428
429 //--------------------------------------------------------------------------------------
430 // Implementation of JavaCallArguments
431
432 inline bool is_value_state_indirect_oop(uint state) {
433 assert(state != JavaCallArguments::value_state_oop,
434 "Checking for handles after removal");
435 assert(state < JavaCallArguments::value_state_limit, "Invalid value state");
436 return state != JavaCallArguments::value_state_primitive;
437 }
438
439 inline oop resolve_indirect_oop(intptr_t value, uint state) {
440 switch (state) {
441 case JavaCallArguments::value_state_handle:
442 {
443 oop* ptr = reinterpret_cast<oop*>(value);
444 return Handle::raw_resolve(ptr);
445 }
446
447 case JavaCallArguments::value_state_jobject:
448 {
449 jobject obj = reinterpret_cast<jobject>(value);
450 return JNIHandles::resolve(obj);
451 }
452
453 default:
454 ShouldNotReachHere();
455 return NULL;
456 }
457 }
458
459 intptr_t* JavaCallArguments::parameters() {
460 // First convert all handles to oops
461 for(int i = 0; i < _size; i++) {
462 uint state = _value_state[i];
463 assert(state != value_state_oop, "Multiple handle conversions");
464 if (is_value_state_indirect_oop(state)) {
465 oop obj = resolve_indirect_oop(_value[i], state);
466 _value[i] = cast_from_oop<intptr_t>(obj);
467 _value_state[i] = value_state_oop;
468 }
469 }
470 // Return argument vector
471 return _value;
472 }
473
474
475 class SignatureChekker : public SignatureIterator {
476 private:
477 int _pos;
478 BasicType _return_type;
479 u_char* _value_state;
480 intptr_t* _value;
481
482 public:
483 bool _is_return;
484
485 SignatureChekker(Symbol* signature,
486 BasicType return_type,
487 bool is_static,
488 u_char* value_state,
489 intptr_t* value) :
490 SignatureIterator(signature),
491 _pos(0),
492 _return_type(return_type),
493 _value_state(value_state),
494 _value(value),
495 _is_return(false)
496 {
497 if (!is_static) {
498 check_value(true); // Receiver must be an oop
499 }
500 }
501
502 void check_value(bool type) {
503 uint state = _value_state[_pos++];
504 if (type) {
505 guarantee(is_value_state_indirect_oop(state),
506 "signature does not match pushed arguments");
507 } else {
508 guarantee(state == JavaCallArguments::value_state_primitive,
509 "signature does not match pushed arguments");
510 }
511 }
512
513 void check_doing_return(bool state) { _is_return = state; }
514
515 void check_return_type(BasicType t) {
516 guarantee(_is_return && t == _return_type, "return type does not match");
517 }
518
519 void check_int(BasicType t) {
520 if (_is_return) {
521 check_return_type(t);
522 return;
523 }
524 check_value(false);
525 }
526
527 void check_double(BasicType t) { check_long(t); }
528
529 void check_long(BasicType t) {
530 if (_is_return) {
531 check_return_type(t);
532 return;
533 }
534
535 check_value(false);
536 check_value(false);
537 }
538
539 void check_obj(BasicType t) {
540 if (_is_return) {
541 check_return_type(t);
542 return;
543 }
544
545 intptr_t v = _value[_pos];
546 if (v != 0) {
547 // v is a "handle" referring to an oop, cast to integral type.
548 // There shouldn't be any handles in very low memory.
549 guarantee((size_t)v >= (size_t)os::vm_page_size(),
550 "Bad JNI oop argument");
551 // Verify the pointee.
552 oop vv = resolve_indirect_oop(v, _value_state[_pos]);
553 guarantee(vv->is_oop_or_null(true),
554 "Bad JNI oop argument");
555 }
556
557 check_value(true); // Verify value state.
558 }
559
560 void do_bool() { check_int(T_BOOLEAN); }
561 void do_char() { check_int(T_CHAR); }
562 void do_float() { check_int(T_FLOAT); }
563 void do_double() { check_double(T_DOUBLE); }
564 void do_byte() { check_int(T_BYTE); }
565 void do_short() { check_int(T_SHORT); }
566 void do_int() { check_int(T_INT); }
567 void do_long() { check_long(T_LONG); }
568 void do_void() { check_return_type(T_VOID); }
569 void do_object(int begin, int end) { check_obj(T_OBJECT); }
570 void do_array(int begin, int end) { check_obj(T_OBJECT); }
571 };
572
573
574 void JavaCallArguments::verify(methodHandle method, BasicType return_type) {
575 guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
576
577 // Treat T_OBJECT and T_ARRAY as the same
578 if (return_type == T_ARRAY) return_type = T_OBJECT;
579
580 // Check that oop information is correct
581 Symbol* signature = method->signature();
582
583 SignatureChekker sc(signature,
584 return_type,
585 method->is_static(),
586 _value_state,
587 _value);
588 sc.iterate_parameters();
589 sc.check_doing_return(true);
590 sc.iterate_returntype();
591 }
|