< prev index next >

src/share/vm/runtime/javaCalls.cpp

Print this page
rev 12692 : 8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
Reviewed-by:
   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  *


 311   // may cause undesirable side-effects in the class metadata.
 312   assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
 313 
 314   JavaThread* thread = (JavaThread*)THREAD;
 315   assert(thread->is_Java_thread(), "must be called by a java thread");
 316   assert(method.not_null(), "must have a method to call");
 317   assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
 318   assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
 319 
 320 
 321   CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
 322 
 323 #if INCLUDE_JVMCI
 324   // Gets the nmethod (if any) that should be called instead of normal target
 325   nmethod* alternative_target = args->alternative_target();
 326   if (alternative_target == NULL) {
 327 #endif
 328 // Verify the arguments
 329 
 330   if (CheckJNICalls)  {
 331     args->verify(method, result->get_type(), thread);
 332   }
 333   else debug_only(args->verify(method, result->get_type(), thread));
 334 #if INCLUDE_JVMCI
 335   }
 336 #else
 337 
 338   // Ignore call if method is empty
 339   if (method->is_empty_method()) {
 340     assert(result->get_type() == T_VOID, "an empty method must return a void value");
 341     return;
 342   }
 343 #endif
 344 
 345 #ifdef ASSERT
 346   { InstanceKlass* holder = method->method_holder();
 347     // A klass might not be initialized since JavaCall's might be used during the executing of
 348     // the <clinit>. For example, a Thread.start might start executing on an object that is
 349     // not fully initialized! (bad Java programming style)
 350     assert(holder->is_linked(), "rewriting must have taken place");
 351   }
 352 #endif
 353 


 425         thread->set_vm_result((oop) result->get_jobject());
 426       }
 427     }
 428   } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
 429 
 430   // Check if a thread stop or suspend should be executed
 431   // The following assert was not realistic.  Thread.stop can set that bit at any moment.
 432   //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
 433 
 434   // Restore possible oop return
 435   if (oop_result_flag) {
 436     result->set_jobject((jobject)thread->vm_result());
 437     thread->set_vm_result(NULL);
 438   }
 439 }
 440 
 441 
 442 //--------------------------------------------------------------------------------------
 443 // Implementation of JavaCallArguments
 444 




























 445 intptr_t* JavaCallArguments::parameters() {
 446   // First convert all handles to oops
 447   for(int i = 0; i < _size; i++) {
 448     if (_is_oop[i]) {
 449       // Handle conversion
 450       _value[i] = cast_from_oop<intptr_t>(Handle::raw_resolve((oop *)_value[i]));



 451     }
 452   }
 453   // Return argument vector
 454   return _value;
 455 }
 456 
 457 
 458 class SignatureChekker : public SignatureIterator {
 459  private:
 460    bool *_is_oop;
 461    int   _pos;
 462    BasicType _return_type;

 463    intptr_t*   _value;
 464    Thread* _thread;
 465 
 466  public:
 467   bool _is_return;
 468 
 469   SignatureChekker(Symbol* signature, BasicType return_type, bool is_static, bool* is_oop, intptr_t* value, Thread* thread) : SignatureIterator(signature) {
 470     _is_oop = is_oop;
 471     _is_return = false;
 472     _return_type = return_type;
 473     _pos = 0;
 474     _value = value;
 475     _thread = thread;
 476 




 477     if (!is_static) {
 478       check_value(true); // Receiver must be an oop
 479     }
 480   }
 481 
 482   void check_value(bool type) {
 483     guarantee(_is_oop[_pos++] == type, "signature does not match pushed arguments");









 484   }
 485 
 486   void check_doing_return(bool state) { _is_return = state; }
 487 
 488   void check_return_type(BasicType t) {
 489     guarantee(_is_return && t == _return_type, "return type does not match");
 490   }
 491 
 492   void check_int(BasicType t) {
 493     if (_is_return) {
 494       check_return_type(t);
 495       return;
 496     }
 497     check_value(false);
 498   }
 499 
 500   void check_double(BasicType t) { check_long(t); }
 501 
 502   void check_long(BasicType t) {
 503     if (_is_return) {
 504       check_return_type(t);
 505       return;
 506     }
 507 
 508     check_value(false);
 509     check_value(false);
 510   }
 511 
 512   void check_obj(BasicType t) {
 513     if (_is_return) {
 514       check_return_type(t);
 515       return;
 516     }
 517 
 518     // verify handle and the oop pointed to by handle
 519     int p = _pos;
 520     bool bad = false;
 521     // If argument is oop
 522     if (_is_oop[p]) {
 523       intptr_t v = _value[p];
 524       if (v != 0 ) {
 525         size_t t = (size_t)v;
 526         bad = (t < (size_t)os::vm_page_size() ) || !Handle::raw_resolve((oop *)v)->is_oop_or_null(true);
 527         if (CheckJNICalls && bad) {
 528           ReportJNIFatalError((JavaThread*)_thread, "Bad JNI oop argument");
 529         }
 530       }
 531       // for the regular debug case.
 532       assert(!bad, "Bad JNI oop argument");
 533     }
 534 
 535     check_value(true);
 536   }
 537 
 538   void do_bool()                       { check_int(T_BOOLEAN);       }
 539   void do_char()                       { check_int(T_CHAR);          }
 540   void do_float()                      { check_int(T_FLOAT);         }
 541   void do_double()                     { check_double(T_DOUBLE);     }
 542   void do_byte()                       { check_int(T_BYTE);          }
 543   void do_short()                      { check_int(T_SHORT);         }
 544   void do_int()                        { check_int(T_INT);           }
 545   void do_long()                       { check_long(T_LONG);         }
 546   void do_void()                       { check_return_type(T_VOID);  }
 547   void do_object(int begin, int end)   { check_obj(T_OBJECT);        }
 548   void do_array(int begin, int end)    { check_obj(T_OBJECT);        }
 549 };
 550 
 551 
 552 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type,
 553   Thread *thread) {
 554   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
 555 
 556   // Treat T_OBJECT and T_ARRAY as the same
 557   if (return_type == T_ARRAY) return_type = T_OBJECT;
 558 
 559   // Check that oop information is correct
 560   Symbol* signature = method->signature();
 561 
 562   SignatureChekker sc(signature, return_type, method->is_static(),_is_oop, _value, thread);




 563   sc.iterate_parameters();
 564   sc.check_doing_return(true);
 565   sc.iterate_returntype();
 566 }
   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  *


 311   // may cause undesirable side-effects in the class metadata.
 312   assert(!DumpSharedSpaces, "must not execute Java bytecodes when dumping");
 313 
 314   JavaThread* thread = (JavaThread*)THREAD;
 315   assert(thread->is_Java_thread(), "must be called by a java thread");
 316   assert(method.not_null(), "must have a method to call");
 317   assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation");
 318   assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here");
 319 
 320 
 321   CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();)
 322 
 323 #if INCLUDE_JVMCI
 324   // Gets the nmethod (if any) that should be called instead of normal target
 325   nmethod* alternative_target = args->alternative_target();
 326   if (alternative_target == NULL) {
 327 #endif
 328 // Verify the arguments
 329 
 330   if (CheckJNICalls)  {
 331     args->verify(method, result->get_type());
 332   }
 333   else debug_only(args->verify(method, result->get_type()));
 334 #if INCLUDE_JVMCI
 335   }
 336 #else
 337 
 338   // Ignore call if method is empty
 339   if (method->is_empty_method()) {
 340     assert(result->get_type() == T_VOID, "an empty method must return a void value");
 341     return;
 342   }
 343 #endif
 344 
 345 #ifdef ASSERT
 346   { InstanceKlass* holder = method->method_holder();
 347     // A klass might not be initialized since JavaCall's might be used during the executing of
 348     // the <clinit>. For example, a Thread.start might start executing on an object that is
 349     // not fully initialized! (bad Java programming style)
 350     assert(holder->is_linked(), "rewriting must have taken place");
 351   }
 352 #endif
 353 


 425         thread->set_vm_result((oop) result->get_jobject());
 426       }
 427     }
 428   } // Exit JavaCallWrapper (can block - potential return oop must be preserved)
 429 
 430   // Check if a thread stop or suspend should be executed
 431   // The following assert was not realistic.  Thread.stop can set that bit at any moment.
 432   //assert(!thread->has_special_runtime_exit_condition(), "no async. exceptions should be installed");
 433 
 434   // Restore possible oop return
 435   if (oop_result_flag) {
 436     result->set_jobject((jobject)thread->vm_result());
 437     thread->set_vm_result(NULL);
 438   }
 439 }
 440 
 441 
 442 //--------------------------------------------------------------------------------------
 443 // Implementation of JavaCallArguments
 444 
 445 inline bool is_value_state_indirect_oop(uint state) {
 446   assert(state != JavaCallArguments::value_state_oop,
 447          "Checking for handles after removal");
 448   assert(state < JavaCallArguments::value_state_limit,
 449          "Invalid value state %u", state);
 450   return state != JavaCallArguments::value_state_primitive;
 451 }
 452 
 453 inline oop resolve_indirect_oop(intptr_t value, uint state) {
 454   switch (state) {
 455   case JavaCallArguments::value_state_handle:
 456   {
 457     oop* ptr = reinterpret_cast<oop*>(value);
 458     return Handle::raw_resolve(ptr);
 459   }
 460 
 461   case JavaCallArguments::value_state_jobject:
 462   {
 463     jobject obj = reinterpret_cast<jobject>(value);
 464     return JNIHandles::resolve(obj);
 465   }
 466 
 467   default:
 468     ShouldNotReachHere();
 469     return NULL;
 470   }
 471 }
 472 
 473 intptr_t* JavaCallArguments::parameters() {
 474   // First convert all handles to oops
 475   for(int i = 0; i < _size; i++) {
 476     uint state = _value_state[i];
 477     assert(state != value_state_oop, "Multiple handle conversions");
 478     if (is_value_state_indirect_oop(state)) {
 479       oop obj = resolve_indirect_oop(_value[i], state);
 480       _value[i] = cast_from_oop<intptr_t>(obj);
 481       _value_state[i] = value_state_oop;
 482     }
 483   }
 484   // Return argument vector
 485   return _value;
 486 }
 487 
 488 
 489 class SignatureChekker : public SignatureIterator {
 490  private:

 491    int _pos;
 492    BasicType _return_type;
 493    u_char* _value_state;
 494    intptr_t* _value;

 495 
 496  public:
 497   bool _is_return;
 498 
 499   SignatureChekker(Symbol* signature,
 500                    BasicType return_type,
 501                    bool is_static,
 502                    u_char* value_state,
 503                    intptr_t* value) :
 504     SignatureIterator(signature),
 505     _pos(0),
 506     _return_type(return_type),
 507     _value_state(value_state),
 508     _value(value),
 509     _is_return(false)
 510   {
 511     if (!is_static) {
 512       check_value(true); // Receiver must be an oop
 513     }
 514   }
 515 
 516   void check_value(bool type) {
 517     uint state = _value_state[_pos++];
 518     if (type) {
 519       guarantee(is_value_state_indirect_oop(state),
 520                 "signature does not match pushed arguments: %u at %d",
 521                 state, _pos - 1);
 522     } else {
 523       guarantee(state == JavaCallArguments::value_state_primitive,
 524                 "signature does not match pushed arguments: %u at %d",
 525                 state, _pos - 1);
 526     }
 527   }
 528 
 529   void check_doing_return(bool state) { _is_return = state; }
 530 
 531   void check_return_type(BasicType t) {
 532     guarantee(_is_return && t == _return_type, "return type does not match");
 533   }
 534 
 535   void check_int(BasicType t) {
 536     if (_is_return) {
 537       check_return_type(t);
 538       return;
 539     }
 540     check_value(false);
 541   }
 542 
 543   void check_double(BasicType t) { check_long(t); }
 544 
 545   void check_long(BasicType t) {
 546     if (_is_return) {
 547       check_return_type(t);
 548       return;
 549     }
 550 
 551     check_value(false);
 552     check_value(false);
 553   }
 554 
 555   void check_obj(BasicType t) {
 556     if (_is_return) {
 557       check_return_type(t);
 558       return;
 559     }
 560 
 561     intptr_t v = _value[_pos];
 562     if (v != 0) {
 563       // v is a "handle" referring to an oop, cast to integral type.
 564       // There shouldn't be any handles in very low memory.
 565       guarantee((size_t)v >= (size_t)os::vm_page_size(),
 566                 "Bad JNI oop argument %d: " PTR_FORMAT, _pos, v);
 567       // Verify the pointee.
 568       oop vv = resolve_indirect_oop(v, _value_state[_pos]);
 569       guarantee(vv->is_oop_or_null(true),
 570                 "Bad JNI oop argument %d: " PTR_FORMAT " -> " PTR_FORMAT,
 571                 _pos, v, p2i(vv));




 572     }
 573 
 574     check_value(true);          // Verify value state.
 575   }
 576 
 577   void do_bool()                       { check_int(T_BOOLEAN);       }
 578   void do_char()                       { check_int(T_CHAR);          }
 579   void do_float()                      { check_int(T_FLOAT);         }
 580   void do_double()                     { check_double(T_DOUBLE);     }
 581   void do_byte()                       { check_int(T_BYTE);          }
 582   void do_short()                      { check_int(T_SHORT);         }
 583   void do_int()                        { check_int(T_INT);           }
 584   void do_long()                       { check_long(T_LONG);         }
 585   void do_void()                       { check_return_type(T_VOID);  }
 586   void do_object(int begin, int end)   { check_obj(T_OBJECT);        }
 587   void do_array(int begin, int end)    { check_obj(T_OBJECT);        }
 588 };
 589 
 590 
 591 void JavaCallArguments::verify(const methodHandle& method, BasicType return_type) {

 592   guarantee(method->size_of_parameters() == size_of_parameters(), "wrong no. of arguments pushed");
 593 
 594   // Treat T_OBJECT and T_ARRAY as the same
 595   if (return_type == T_ARRAY) return_type = T_OBJECT;
 596 
 597   // Check that oop information is correct
 598   Symbol* signature = method->signature();
 599 
 600   SignatureChekker sc(signature,
 601                       return_type,
 602                       method->is_static(),
 603                       _value_state,
 604                       _value);
 605   sc.iterate_parameters();
 606   sc.check_doing_return(true);
 607   sc.iterate_returntype();
 608 }
< prev index next >