hotspot/src/share/vm/oops/instanceKlass.cpp
changeset 46271 979ebd346ecf
parent 42650 1f304d0c888b
child 46289 1904e7ec236e
equal deleted inserted replaced
46270:2e7898927798 46271:979ebd346ecf
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   432   assert(!is_not_initialized(), "class must be initialized now");
   432   assert(!is_not_initialized(), "class must be initialized now");
   433 }
   433 }
   434 
   434 
   435 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
   435 void InstanceKlass::eager_initialize_impl(instanceKlassHandle this_k) {
   436   EXCEPTION_MARK;
   436   EXCEPTION_MARK;
   437   oop init_lock = this_k->init_lock();
   437   HandleMark hm(THREAD);
   438   ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   438   Handle init_lock(THREAD, this_k->init_lock());
       
   439   ObjectLocker ol(init_lock, THREAD, init_lock() != NULL);
   439 
   440 
   440   // abort if someone beat us to the initialization
   441   // abort if someone beat us to the initialization
   441   if (!this_k->is_not_initialized()) return;  // note: not equivalent to is_initialized()
   442   if (!this_k->is_not_initialized()) return;  // note: not equivalent to is_initialized()
   442 
   443 
   443   ClassState old_state = this_k->init_state();
   444   ClassState old_state = this_k->init_state();
   467 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
   468 // See "The Virtual Machine Specification" section 2.16.5 for a detailed explanation of the class initialization
   468 // process. The step comments refers to the procedure described in that section.
   469 // process. The step comments refers to the procedure described in that section.
   469 // Note: implementation moved to static method to expose the this pointer.
   470 // Note: implementation moved to static method to expose the this pointer.
   470 void InstanceKlass::initialize(TRAPS) {
   471 void InstanceKlass::initialize(TRAPS) {
   471   if (this->should_be_initialized()) {
   472   if (this->should_be_initialized()) {
   472     HandleMark hm(THREAD);
       
   473     instanceKlassHandle this_k(THREAD, this);
   473     instanceKlassHandle this_k(THREAD, this);
   474     initialize_impl(this_k, CHECK);
   474     initialize_impl(this_k, CHECK);
   475     // Note: at this point the class may be initialized
   475     // Note: at this point the class may be initialized
   476     //       OR it may be in the state of being initialized
   476     //       OR it may be in the state of being initialized
   477     //       in case of recursive initialization!
   477     //       in case of recursive initialization!
   499 }
   499 }
   500 
   500 
   501 void InstanceKlass::link_class(TRAPS) {
   501 void InstanceKlass::link_class(TRAPS) {
   502   assert(is_loaded(), "must be loaded");
   502   assert(is_loaded(), "must be loaded");
   503   if (!is_linked()) {
   503   if (!is_linked()) {
   504     HandleMark hm(THREAD);
       
   505     instanceKlassHandle this_k(THREAD, this);
   504     instanceKlassHandle this_k(THREAD, this);
   506     link_class_impl(this_k, true, CHECK);
   505     link_class_impl(this_k, true, CHECK);
   507   }
   506   }
   508 }
   507 }
   509 
   508 
   510 // Called to verify that a class can link during initialization, without
   509 // Called to verify that a class can link during initialization, without
   511 // throwing a VerifyError.
   510 // throwing a VerifyError.
   512 bool InstanceKlass::link_class_or_fail(TRAPS) {
   511 bool InstanceKlass::link_class_or_fail(TRAPS) {
   513   assert(is_loaded(), "must be loaded");
   512   assert(is_loaded(), "must be loaded");
   514   if (!is_linked()) {
   513   if (!is_linked()) {
   515     HandleMark hm(THREAD);
       
   516     instanceKlassHandle this_k(THREAD, this);
   514     instanceKlassHandle this_k(THREAD, this);
   517     link_class_impl(this_k, false, CHECK_false);
   515     link_class_impl(this_k, false, CHECK_false);
   518   }
   516   }
   519   return is_linked();
   517   return is_linked();
   520 }
   518 }
   563 
   561 
   564   // link all interfaces implemented by this class before linking this class
   562   // link all interfaces implemented by this class before linking this class
   565   Array<Klass*>* interfaces = this_k->local_interfaces();
   563   Array<Klass*>* interfaces = this_k->local_interfaces();
   566   int num_interfaces = interfaces->length();
   564   int num_interfaces = interfaces->length();
   567   for (int index = 0; index < num_interfaces; index++) {
   565   for (int index = 0; index < num_interfaces; index++) {
   568     HandleMark hm(THREAD);
       
   569     instanceKlassHandle ih(THREAD, interfaces->at(index));
   566     instanceKlassHandle ih(THREAD, interfaces->at(index));
   570     link_class_impl(ih, throw_verifyerror, CHECK_false);
   567     link_class_impl(ih, throw_verifyerror, CHECK_false);
   571   }
   568   }
   572 
   569 
   573   // in case the class is linked in the process of linking its superclasses
   570   // in case the class is linked in the process of linking its superclasses
   584                              jt->get_thread_stat()->perf_timers_addr(),
   581                              jt->get_thread_stat()->perf_timers_addr(),
   585                              PerfClassTraceTime::CLASS_LINK);
   582                              PerfClassTraceTime::CLASS_LINK);
   586 
   583 
   587   // verification & rewriting
   584   // verification & rewriting
   588   {
   585   {
   589     oop init_lock = this_k->init_lock();
   586     HandleMark hm(THREAD);
   590     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   587     Handle init_lock(THREAD, this_k->init_lock());
       
   588     ObjectLocker ol(init_lock, THREAD, init_lock() != NULL);
   591     // rewritten will have been set if loader constraint error found
   589     // rewritten will have been set if loader constraint error found
   592     // on an earlier link attempt
   590     // on an earlier link attempt
   593     // don't verify or rewrite if already rewritten
   591     // don't verify or rewrite if already rewritten
       
   592     //
   594 
   593 
   595     if (!this_k->is_linked()) {
   594     if (!this_k->is_linked()) {
   596       if (!this_k->is_rewritten()) {
   595       if (!this_k->is_rewritten()) {
   597         {
   596         {
   598           bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
   597           bool verify_ok = verify_code(this_k, throw_verifyerror, THREAD);
   698     }
   697     }
   699   }
   698   }
   700 }
   699 }
   701 
   700 
   702 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
   701 void InstanceKlass::initialize_impl(instanceKlassHandle this_k, TRAPS) {
       
   702   HandleMark hm(THREAD);
       
   703 
   703   // Make sure klass is linked (verified) before initialization
   704   // Make sure klass is linked (verified) before initialization
   704   // A class could already be verified, since it has been reflected upon.
   705   // A class could already be verified, since it has been reflected upon.
   705   this_k->link_class(CHECK);
   706   this_k->link_class(CHECK);
   706 
   707 
   707   DTRACE_CLASSINIT_PROBE(required, this_k(), -1);
   708   DTRACE_CLASSINIT_PROBE(required, this_k(), -1);
   709   bool wait = false;
   710   bool wait = false;
   710 
   711 
   711   // refer to the JVM book page 47 for description of steps
   712   // refer to the JVM book page 47 for description of steps
   712   // Step 1
   713   // Step 1
   713   {
   714   {
   714     oop init_lock = this_k->init_lock();
   715     Handle init_lock(THREAD, this_k->init_lock());
   715     ObjectLocker ol(init_lock, THREAD, init_lock != NULL);
   716     ObjectLocker ol(init_lock, THREAD, init_lock() != NULL);
   716 
   717 
   717     Thread *self = THREAD; // it's passed the current thread
   718     Thread *self = THREAD; // it's passed the current thread
   718 
   719 
   719     // Step 2
   720     // Step 2
   720     // If we were to use wait() instead of waitInterruptibly() then
   721     // If we were to use wait() instead of waitInterruptibly() then
   851   instanceKlassHandle kh(THREAD, this);
   852   instanceKlassHandle kh(THREAD, this);
   852   set_initialization_state_and_notify_impl(kh, state, CHECK);
   853   set_initialization_state_and_notify_impl(kh, state, CHECK);
   853 }
   854 }
   854 
   855 
   855 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
   856 void InstanceKlass::set_initialization_state_and_notify_impl(instanceKlassHandle this_k, ClassState state, TRAPS) {
   856   oop init_lock = this_k->init_lock();
   857   Handle init_lock(THREAD, this_k->init_lock());
   857   if (init_lock != NULL) {
   858   if (init_lock() != NULL) {
   858     ObjectLocker ol(init_lock, THREAD);
   859     ObjectLocker ol(init_lock, THREAD);
   859     this_k->set_init_state(state);
   860     this_k->set_init_state(state);
   860     this_k->fence_and_clear_init_lock();
   861     this_k->fence_and_clear_init_lock();
   861     ol.notify_all(CHECK);
   862     ol.notify_all(CHECK);
   862   } else {
   863   } else {
   863     assert(init_lock != NULL, "The initialization state should never be set twice");
   864     assert(init_lock() != NULL, "The initialization state should never be set twice");
   864     this_k->set_init_state(state);
   865     this_k->set_init_state(state);
   865   }
   866   }
   866 }
   867 }
   867 
   868 
   868 // The embedded _implementor field can only record one implementor.
   869 // The embedded _implementor field can only record one implementor.
  2298 }
  2299 }
  2299 
  2300 
  2300 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
  2301 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
  2301 
  2302 
  2302   // ensure java/ packages only loaded by boot or platform builtin loaders
  2303   // ensure java/ packages only loaded by boot or platform builtin loaders
  2303   check_prohibited_package(name(), loader_data->class_loader(), CHECK);
  2304   Handle class_loader(THREAD, loader_data->class_loader());
       
  2305   check_prohibited_package(name(), class_loader, CHECK);
  2304 
  2306 
  2305   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
  2307   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
  2306 
  2308 
  2307   if (pkg_name != NULL && loader_data != NULL) {
  2309   if (pkg_name != NULL && loader_data != NULL) {
  2308 
  2310 
  2458 void InstanceKlass::check_prohibited_package(Symbol* class_name,
  2460 void InstanceKlass::check_prohibited_package(Symbol* class_name,
  2459                                              Handle class_loader,
  2461                                              Handle class_loader,
  2460                                              TRAPS) {
  2462                                              TRAPS) {
  2461   ResourceMark rm(THREAD);
  2463   ResourceMark rm(THREAD);
  2462   if (!class_loader.is_null() &&
  2464   if (!class_loader.is_null() &&
  2463       !SystemDictionary::is_platform_class_loader(class_loader) &&
  2465       !SystemDictionary::is_platform_class_loader(class_loader()) &&
  2464       class_name != NULL &&
  2466       class_name != NULL &&
  2465       strncmp(class_name->as_C_string(), JAVAPKG, JAVAPKG_LEN) == 0) {
  2467       strncmp(class_name->as_C_string(), JAVAPKG, JAVAPKG_LEN) == 0) {
  2466     TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
  2468     TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK);
  2467     assert(pkg_name != NULL, "Error in parsing package name starting with 'java/'");
  2469     assert(pkg_name != NULL, "Error in parsing package name starting with 'java/'");
  2468     char* name = pkg_name->as_C_string();
  2470     char* name = pkg_name->as_C_string();
  3138         log->print(" source: instance of %s", caller->external_name());
  3140         log->print(" source: instance of %s", caller->external_name());
  3139       } else {
  3141       } else {
  3140         // source is unknown
  3142         // source is unknown
  3141       }
  3143       }
  3142     } else {
  3144     } else {
  3143       Handle class_loader(loader_data->class_loader());
  3145       oop class_loader = loader_data->class_loader();
  3144       log->print(" source: %s", class_loader->klass()->external_name());
  3146       log->print(" source: %s", class_loader->klass()->external_name());
  3145     }
  3147     }
  3146   } else {
  3148   } else {
  3147     log->print(" source: shared objects file");
  3149     log->print(" source: shared objects file");
  3148   }
  3150   }