src/share/vm/classfile/classLoaderData.cpp

Wed, 25 Mar 2015 08:16:48 -0400

author
hseigel
date
Wed, 25 Mar 2015 08:16:48 -0400
changeset 7666
6b65121b3258
parent 7367
82d3e7b5277a
child 7535
7ae4e26cb1e0
child 7682
04e84c0579be
permissions
-rw-r--r--

7127066: Class verifier accepts an invalid class file
Summary: For *store bytecodes, compare incoming, not outgoing, type state with exception handlers' stack maps.
Reviewed-by: acorn, dholmes

     1 /*
     2  * Copyright (c) 2012, 2014, 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  *
    23  */
    25 // A ClassLoaderData identifies the full set of class types that a class
    26 // loader's name resolution strategy produces for a given configuration of the
    27 // class loader.
    28 // Class types in the ClassLoaderData may be defined by from class file binaries
    29 // provided by the class loader, or from other class loader it interacts with
    30 // according to its name resolution strategy.
    31 //
    32 // Class loaders that implement a deterministic name resolution strategy
    33 // (including with respect to their delegation behavior), such as the boot, the
    34 // extension, and the system loaders of the JDK's built-in class loader
    35 // hierarchy, always produce the same linkset for a given configuration.
    36 //
    37 // ClassLoaderData carries information related to a linkset (e.g.,
    38 // metaspace holding its klass definitions).
    39 // The System Dictionary and related data structures (e.g., placeholder table,
    40 // loader constraints table) as well as the runtime representation of classes
    41 // only reference ClassLoaderData.
    42 //
    43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
    44 // that represent the loader's "linking domain" in the JVM.
    45 //
    46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
    47 // the singleton class the_null_class_loader_data().
    49 #include "precompiled.hpp"
    50 #include "classfile/classLoaderData.hpp"
    51 #include "classfile/classLoaderData.inline.hpp"
    52 #include "classfile/javaClasses.hpp"
    53 #include "classfile/metadataOnStackMark.hpp"
    54 #include "classfile/systemDictionary.hpp"
    55 #include "code/codeCache.hpp"
    56 #include "memory/gcLocker.hpp"
    57 #include "memory/metadataFactory.hpp"
    58 #include "memory/metaspaceShared.hpp"
    59 #include "memory/oopFactory.hpp"
    60 #include "runtime/jniHandles.hpp"
    61 #include "runtime/mutex.hpp"
    62 #include "runtime/safepoint.hpp"
    63 #include "runtime/synchronizer.hpp"
    64 #include "utilities/growableArray.hpp"
    65 #include "utilities/macros.hpp"
    66 #include "utilities/ostream.hpp"
    67 #if INCLUDE_TRACE
    68 #include "trace/tracing.hpp"
    69 #endif
    71 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
    73 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
    74   _class_loader(h_class_loader()),
    75   _is_anonymous(is_anonymous),
    76   // An anonymous class loader data doesn't have anything to keep
    77   // it from being unloaded during parsing of the anonymous class.
    78   // The null-class-loader should always be kept alive.
    79   _keep_alive(is_anonymous || h_class_loader.is_null()),
    80   _metaspace(NULL), _unloading(false), _klasses(NULL),
    81   _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
    82   _next(NULL), _dependencies(dependencies),
    83   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
    84     // empty
    85 }
    87 void ClassLoaderData::init_dependencies(TRAPS) {
    88   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
    89   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
    90   _dependencies.init(CHECK);
    91 }
    93 void ClassLoaderData::Dependencies::init(TRAPS) {
    94   // Create empty dependencies array to add to. CMS requires this to be
    95   // an oop so that it can track additions via card marks.  We think.
    96   _list_head = oopFactory::new_objectArray(2, CHECK);
    97 }
    99 bool ClassLoaderData::claim() {
   100   if (_claimed == 1) {
   101     return false;
   102   }
   104   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
   105 }
   107 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   108   if (must_claim && !claim()) {
   109     return;
   110   }
   112   f->do_oop(&_class_loader);
   113   _dependencies.oops_do(f);
   114   _handles->oops_do(f);
   115   if (klass_closure != NULL) {
   116     classes_do(klass_closure);
   117   }
   118 }
   120 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
   121   f->do_oop((oop*)&_list_head);
   122 }
   124 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
   125   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   126     klass_closure->do_klass(k);
   127     assert(k != k->next_link(), "no loops!");
   128   }
   129 }
   131 void ClassLoaderData::classes_do(void f(Klass * const)) {
   132   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   133     f(k);
   134   }
   135 }
   137 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
   138   // Lock to avoid classes being modified/added/removed during iteration
   139   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   140   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   141     // Do not filter ArrayKlass oops here...
   142     if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) {
   143       klass_closure->do_klass(k);
   144     }
   145   }
   146 }
   148 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
   149   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   150     if (k->oop_is_instance()) {
   151       f(InstanceKlass::cast(k));
   152     }
   153     assert(k != k->next_link(), "no loops!");
   154   }
   155 }
   157 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
   158   ClassLoaderData * const from_cld = this;
   159   ClassLoaderData * const to_cld = k->class_loader_data();
   161   // Dependency to the null class loader data doesn't need to be recorded
   162   // because the null class loader data never goes away.
   163   if (to_cld->is_the_null_class_loader_data()) {
   164     return;
   165   }
   167   oop to;
   168   if (to_cld->is_anonymous()) {
   169     // Anonymous class dependencies are through the mirror.
   170     to = k->java_mirror();
   171   } else {
   172     to = to_cld->class_loader();
   174     // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
   175     // we still have to add it.  The class_loader won't keep from_cld alive.
   176     if (!from_cld->is_anonymous()) {
   177       // Check that this dependency isn't from the same or parent class_loader
   178       oop from = from_cld->class_loader();
   180       oop curr = from;
   181       while (curr != NULL) {
   182         if (curr == to) {
   183           return; // this class loader is in the parent list, no need to add it.
   184         }
   185         curr = java_lang_ClassLoader::parent(curr);
   186       }
   187     }
   188   }
   190   // It's a dependency we won't find through GC, add it. This is relatively rare
   191   // Must handle over GC point.
   192   Handle dependency(THREAD, to);
   193   from_cld->_dependencies.add(dependency, CHECK);
   194 }
   197 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
   198   // Check first if this dependency is already in the list.
   199   // Save a pointer to the last to add to under the lock.
   200   objArrayOop ok = _list_head;
   201   objArrayOop last = NULL;
   202   while (ok != NULL) {
   203     last = ok;
   204     if (ok->obj_at(0) == dependency()) {
   205       // Don't need to add it
   206       return;
   207     }
   208     ok = (objArrayOop)ok->obj_at(1);
   209   }
   211   // Must handle over GC points
   212   assert (last != NULL, "dependencies should be initialized");
   213   objArrayHandle last_handle(THREAD, last);
   215   // Create a new dependency node with fields for (class_loader or mirror, next)
   216   objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
   217   deps->obj_at_put(0, dependency());
   219   // Must handle over GC points
   220   objArrayHandle new_dependency(THREAD, deps);
   222   // Add the dependency under lock
   223   locked_add(last_handle, new_dependency, THREAD);
   224 }
   226 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
   227                                                objArrayHandle new_dependency,
   228                                                Thread* THREAD) {
   230   // Have to lock and put the new dependency on the end of the dependency
   231   // array so the card mark for CMS sees that this dependency is new.
   232   // Can probably do this lock free with some effort.
   233   ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
   235   oop loader_or_mirror = new_dependency->obj_at(0);
   237   // Since the dependencies are only added, add to the end.
   238   objArrayOop end = last_handle();
   239   objArrayOop last = NULL;
   240   while (end != NULL) {
   241     last = end;
   242     // check again if another thread added it to the end.
   243     if (end->obj_at(0) == loader_or_mirror) {
   244       // Don't need to add it
   245       return;
   246     }
   247     end = (objArrayOop)end->obj_at(1);
   248   }
   249   assert (last != NULL, "dependencies should be initialized");
   250   // fill in the first element with the oop in new_dependency.
   251   if (last->obj_at(0) == NULL) {
   252     last->obj_at_put(0, new_dependency->obj_at(0));
   253   } else {
   254     last->obj_at_put(1, new_dependency());
   255   }
   256 }
   258 void ClassLoaderDataGraph::clear_claimed_marks() {
   259   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   260     cld->clear_claimed();
   261   }
   262 }
   264 void ClassLoaderData::add_class(Klass* k) {
   265   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   266   Klass* old_value = _klasses;
   267   k->set_next_link(old_value);
   268   // link the new item into the list
   269   _klasses = k;
   271   if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
   272     ResourceMark rm;
   273     tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
   274                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
   275                   p2i(k),
   276                   k->external_name(),
   277                   p2i(k->class_loader_data()),
   278                   p2i((void *)k->class_loader()),
   279                   loader_name());
   280   }
   281 }
   283 // This is called by InstanceKlass::deallocate_contents() to remove the
   284 // scratch_class for redefine classes.  We need a lock because there it may not
   285 // be called at a safepoint if there's an error.
   286 void ClassLoaderData::remove_class(Klass* scratch_class) {
   287   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   288   Klass* prev = NULL;
   289   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   290     if (k == scratch_class) {
   291       if (prev == NULL) {
   292         _klasses = k->next_link();
   293       } else {
   294         Klass* next = k->next_link();
   295         prev->set_next_link(next);
   296       }
   297       return;
   298     }
   299     prev = k;
   300     assert(k != k->next_link(), "no loops!");
   301   }
   302   ShouldNotReachHere();   // should have found this class!!
   303 }
   305 void ClassLoaderData::unload() {
   306   _unloading = true;
   308   // Tell serviceability tools these classes are unloading
   309   classes_do(InstanceKlass::notify_unload_class);
   311   if (TraceClassLoaderData) {
   312     ResourceMark rm;
   313     tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
   314     tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
   315                loader_name());
   316     if (is_anonymous()) {
   317       tty->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
   318     }
   319     tty->print_cr("]");
   320   }
   321 }
   323 #ifdef ASSERT
   324 class AllAliveClosure : public OopClosure {
   325   BoolObjectClosure* _is_alive_closure;
   326   bool _found_dead;
   327  public:
   328   AllAliveClosure(BoolObjectClosure* is_alive_closure) : _is_alive_closure(is_alive_closure), _found_dead(false) {}
   329   template <typename T> void do_oop_work(T* p) {
   330     T heap_oop = oopDesc::load_heap_oop(p);
   331     if (!oopDesc::is_null(heap_oop)) {
   332       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
   333       if (!_is_alive_closure->do_object_b(obj)) {
   334         _found_dead = true;
   335       }
   336     }
   337   }
   338   void do_oop(oop* p)       { do_oop_work<oop>(p); }
   339   void do_oop(narrowOop* p) { do_oop_work<narrowOop>(p); }
   340   bool found_dead()         { return _found_dead; }
   341 };
   342 #endif
   344 oop ClassLoaderData::keep_alive_object() const {
   345   assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
   346   return is_anonymous() ? _klasses->java_mirror() : class_loader();
   347 }
   349 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
   350   bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
   351       || is_alive_closure->do_object_b(keep_alive_object());
   353 #ifdef ASSERT
   354   if (alive) {
   355     AllAliveClosure all_alive_closure(is_alive_closure);
   356     KlassToOopClosure klass_closure(&all_alive_closure);
   357     const_cast<ClassLoaderData*>(this)->oops_do(&all_alive_closure, &klass_closure, false);
   358     assert(!all_alive_closure.found_dead(), err_msg("Found dead oop in alive cld: " PTR_FORMAT, p2i(this)));
   359   }
   360 #endif
   362   return alive;
   363 }
   366 ClassLoaderData::~ClassLoaderData() {
   367   // Release C heap structures for all the classes.
   368   classes_do(InstanceKlass::release_C_heap_structures);
   370   Metaspace *m = _metaspace;
   371   if (m != NULL) {
   372     _metaspace = NULL;
   373     // release the metaspace
   374     delete m;
   375     // release the handles
   376     if (_handles != NULL) {
   377       JNIHandleBlock::release_block(_handles);
   378       _handles = NULL;
   379     }
   380   }
   382   // Clear all the JNI handles for methods
   383   // These aren't deallocated and are going to look like a leak, but that's
   384   // needed because we can't really get rid of jmethodIDs because we don't
   385   // know when native code is going to stop using them.  The spec says that
   386   // they're "invalid" but existing programs likely rely on their being
   387   // NULL after class unloading.
   388   if (_jmethod_ids != NULL) {
   389     Method::clear_jmethod_ids(this);
   390   }
   391   // Delete lock
   392   delete _metaspace_lock;
   394   // Delete free list
   395   if (_deallocate_list != NULL) {
   396     delete _deallocate_list;
   397   }
   398 }
   400 /**
   401  * Returns true if this class loader data is for the extension class loader.
   402  */
   403 bool ClassLoaderData::is_ext_class_loader_data() const {
   404   return SystemDictionary::is_ext_class_loader(class_loader());
   405 }
   407 Metaspace* ClassLoaderData::metaspace_non_null() {
   408   assert(!DumpSharedSpaces, "wrong metaspace!");
   409   // If the metaspace has not been allocated, create a new one.  Might want
   410   // to create smaller arena for Reflection class loaders also.
   411   // The reason for the delayed allocation is because some class loaders are
   412   // simply for delegating with no metadata of their own.
   413   if (_metaspace == NULL) {
   414     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   415     // Check again if metaspace has been allocated while we were getting this lock.
   416     if (_metaspace != NULL) {
   417       return _metaspace;
   418     }
   419     if (this == the_null_class_loader_data()) {
   420       assert (class_loader() == NULL, "Must be");
   421       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
   422     } else if (is_anonymous()) {
   423       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   424         tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
   425       }
   426       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
   427     } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   428       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   429         tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
   430       }
   431       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
   432     } else {
   433       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
   434     }
   435   }
   436   return _metaspace;
   437 }
   439 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
   440 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
   442 jobject ClassLoaderData::add_handle(Handle h) {
   443   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   444   if (handles() == NULL) {
   445     set_handles(JNIHandleBlock::allocate_block());
   446   }
   447   return handles()->allocate_handle(h());
   448 }
   450 // Add this metadata pointer to be freed when it's safe.  This is only during
   451 // class unloading because Handles might point to this metadata field.
   452 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   453   // Metadata in shared region isn't deleted.
   454   if (!m->is_shared()) {
   455     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   456     if (_deallocate_list == NULL) {
   457       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   458     }
   459     _deallocate_list->append_if_missing(m);
   460   }
   461 }
   463 // Deallocate free metadata on the free list.  How useful the PermGen was!
   464 void ClassLoaderData::free_deallocate_list() {
   465   // Don't need lock, at safepoint
   466   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   467   if (_deallocate_list == NULL) {
   468     return;
   469   }
   470   // Go backwards because this removes entries that are freed.
   471   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   472     Metadata* m = _deallocate_list->at(i);
   473     if (!m->on_stack()) {
   474       _deallocate_list->remove_at(i);
   475       // There are only three types of metadata that we deallocate directly.
   476       // Cast them so they can be used by the template function.
   477       if (m->is_method()) {
   478         MetadataFactory::free_metadata(this, (Method*)m);
   479       } else if (m->is_constantPool()) {
   480         MetadataFactory::free_metadata(this, (ConstantPool*)m);
   481       } else if (m->is_klass()) {
   482         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
   483       } else {
   484         ShouldNotReachHere();
   485       }
   486     }
   487   }
   488 }
   490 // These anonymous class loaders are to contain classes used for JSR292
   491 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   492   // Add a new class loader data to the graph.
   493   return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
   494 }
   496 const char* ClassLoaderData::loader_name() {
   497   // Handles null class loader
   498   return SystemDictionary::loader_name(class_loader());
   499 }
   501 #ifndef PRODUCT
   502 // Define to dump klasses
   503 #undef CLD_DUMP_KLASSES
   505 void ClassLoaderData::dump(outputStream * const out) {
   506   ResourceMark rm;
   507   out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
   508       p2i(this), p2i((void *)class_loader()),
   509       p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
   510   if (claimed()) out->print(" claimed ");
   511   if (is_unloading()) out->print(" unloading ");
   512   out->print(" handles " INTPTR_FORMAT, p2i(handles()));
   513   out->cr();
   514   if (metaspace_or_null() != NULL) {
   515     out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
   516     metaspace_or_null()->dump(out);
   517   } else {
   518     out->print_cr("metaspace: NULL");
   519   }
   521 #ifdef CLD_DUMP_KLASSES
   522   if (Verbose) {
   523     ResourceMark rm;
   524     Klass* k = _klasses;
   525     while (k != NULL) {
   526       out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
   527           k->has_modified_oops(), k->has_accumulated_modified_oops());
   528       assert(k != k->next_link(), "no loops!");
   529       k = k->next_link();
   530     }
   531   }
   532 #endif  // CLD_DUMP_KLASSES
   533 #undef CLD_DUMP_KLASSES
   534   if (_jmethod_ids != NULL) {
   535     Method::print_jmethod_ids(this, out);
   536   }
   537   out->print_cr("}");
   538 }
   539 #endif // PRODUCT
   541 void ClassLoaderData::verify() {
   542   oop cl = class_loader();
   544   guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
   545   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
   547   // Verify the integrity of the allocated space.
   548   if (metaspace_or_null() != NULL) {
   549     metaspace_or_null()->verify();
   550   }
   552   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   553     guarantee(k->class_loader_data() == this, "Must be the same");
   554     k->verify();
   555     assert(k != k->next_link(), "no loops!");
   556   }
   557 }
   559 bool ClassLoaderData::contains_klass(Klass* klass) {
   560   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   561     if (k == klass) return true;
   562   }
   563   return false;
   564 }
   567 // GC root of class loader data created.
   568 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
   569 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   570 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
   571 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   573 bool ClassLoaderDataGraph::_should_purge = false;
   575 // Add a new class loader data node to the list.  Assign the newly created
   576 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
   577 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
   578   // We need to allocate all the oops for the ClassLoaderData before allocating the
   579   // actual ClassLoaderData object.
   580   ClassLoaderData::Dependencies dependencies(CHECK_NULL);
   582   No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
   583                                        // ClassLoaderData in the graph since the CLD
   584                                        // contains unhandled oops
   586   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
   589   if (!is_anonymous) {
   590     ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
   591     // First, Atomically set it
   592     ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
   593     if (old != NULL) {
   594       delete cld;
   595       // Returns the data.
   596       return old;
   597     }
   598   }
   600   // We won the race, and therefore the task of adding the data to the list of
   601   // class loader data
   602   ClassLoaderData** list_head = &_head;
   603   ClassLoaderData* next = _head;
   605   do {
   606     cld->set_next(next);
   607     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
   608     if (exchanged == next) {
   609       if (TraceClassLoaderData) {
   610         ResourceMark rm;
   611         tty->print("[ClassLoaderData: ");
   612         tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
   613         tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
   614                    cld->loader_name());
   615         tty->print_cr("]");
   616       }
   617       return cld;
   618     }
   619     next = exchanged;
   620   } while (true);
   622 }
   624 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   625   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   626     cld->oops_do(f, klass_closure, must_claim);
   627   }
   628 }
   630 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   631   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   632     if (cld->keep_alive()) {
   633       cld->oops_do(f, klass_closure, must_claim);
   634     }
   635   }
   636 }
   638 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   639   if (ClassUnloading) {
   640     keep_alive_oops_do(f, klass_closure, must_claim);
   641   } else {
   642     oops_do(f, klass_closure, must_claim);
   643   }
   644 }
   646 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
   647   for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
   648     cl->do_cld(cld);
   649   }
   650 }
   652 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
   653   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
   654     CLDClosure* closure = cld->keep_alive() ? strong : weak;
   655     if (closure != NULL) {
   656       closure->do_cld(cld);
   657     }
   658   }
   659 }
   661 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
   662   roots_cld_do(cl, NULL);
   663 }
   665 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
   666   if (ClassUnloading) {
   667     keep_alive_cld_do(cl);
   668   } else {
   669     cld_do(cl);
   670   }
   671 }
   673 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
   674   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   675     cld->classes_do(klass_closure);
   676   }
   677 }
   679 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
   680   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   681     cld->classes_do(f);
   682   }
   683 }
   685 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
   686   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   687     cld->loaded_classes_do(klass_closure);
   688   }
   689 }
   691 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
   692   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   693   // Only walk the head until any clds not purged from prior unloading
   694   // (CMS doesn't purge right away).
   695   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
   696     cld->classes_do(f);
   697   }
   698 }
   700 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
   701   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
   703   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   705   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   706   ClassLoaderData* curr = _head;
   707   while (curr != _saved_head) {
   708     if (!curr->claimed()) {
   709       array->push(curr);
   711       if (TraceClassLoaderData) {
   712         tty->print("[ClassLoaderData] found new CLD: ");
   713         curr->print_value_on(tty);
   714         tty->cr();
   715       }
   716     }
   718     curr = curr->_next;
   719   }
   721   return array;
   722 }
   724 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
   725   assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
   726   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
   727     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
   728       return true;
   729     }
   730   }
   731   return false;
   732 }
   734 #ifndef PRODUCT
   735 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
   736   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   737     if (loader_data == data) {
   738       return true;
   739     }
   740   }
   742   return false;
   743 }
   744 #endif // PRODUCT
   747 // Move class loader data from main list to the unloaded list for unloading
   748 // and deallocation later.
   749 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {
   750   ClassLoaderData* data = _head;
   751   ClassLoaderData* prev = NULL;
   752   bool seen_dead_loader = false;
   754   // Save previous _unloading pointer for CMS which may add to unloading list before
   755   // purging and we don't want to rewalk the previously unloaded class loader data.
   756   _saved_unloading = _unloading;
   758   while (data != NULL) {
   759     if (data->is_alive(is_alive_closure)) {
   760       prev = data;
   761       data = data->next();
   762       continue;
   763     }
   764     seen_dead_loader = true;
   765     ClassLoaderData* dead = data;
   766     dead->unload();
   767     data = data->next();
   768     // Remove from loader list.
   769     // This class loader data will no longer be found
   770     // in the ClassLoaderDataGraph.
   771     if (prev != NULL) {
   772       prev->set_next(data);
   773     } else {
   774       assert(dead == _head, "sanity check");
   775       _head = data;
   776     }
   777     dead->set_next(_unloading);
   778     _unloading = dead;
   779   }
   781   if (clean_alive) {
   782     // Clean previous versions and the deallocate list.
   783     ClassLoaderDataGraph::clean_metaspaces();
   784   }
   786   if (seen_dead_loader) {
   787     post_class_unload_events();
   788   }
   790   return seen_dead_loader;
   791 }
   793 void ClassLoaderDataGraph::clean_metaspaces() {
   794   // mark metadata seen on the stack and code cache so we can delete unneeded entries.
   795   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
   796   MetadataOnStackMark md_on_stack(has_redefined_a_class);
   798   if (has_redefined_a_class) {
   799     // purge_previous_versions also cleans weak method links. Because
   800     // one method's MDO can reference another method from another
   801     // class loader, we need to first clean weak method links for all
   802     // class loaders here. Below, we can then free redefined methods
   803     // for all class loaders.
   804     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   805       data->classes_do(InstanceKlass::purge_previous_versions);
   806     }
   807   }
   809   // Need to purge the previous version before deallocating.
   810   free_deallocate_lists();
   811 }
   813 void ClassLoaderDataGraph::purge() {
   814   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   815   ClassLoaderData* list = _unloading;
   816   _unloading = NULL;
   817   ClassLoaderData* next = list;
   818   while (next != NULL) {
   819     ClassLoaderData* purge_me = next;
   820     next = purge_me->next();
   821     delete purge_me;
   822   }
   823   Metaspace::purge();
   824 }
   826 void ClassLoaderDataGraph::post_class_unload_events(void) {
   827 #if INCLUDE_TRACE
   828   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   829   if (Tracing::enabled()) {
   830     if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
   831       assert(_unloading != NULL, "need class loader data unload list!");
   832       _class_unload_time = Ticks::now();
   833       classes_unloading_do(&class_unload_event);
   834     }
   835     Tracing::on_unloading_classes();
   836   }
   837 #endif
   838 }
   840 void ClassLoaderDataGraph::free_deallocate_lists() {
   841   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   842     // We need to keep this data until InstanceKlass::purge_previous_version has been
   843     // called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.
   844     cld->free_deallocate_list();
   845   }
   846 }
   848 // CDS support
   850 // Global metaspaces for writing information to the shared archive.  When
   851 // application CDS is supported, we may need one per metaspace, so this
   852 // sort of looks like it.
   853 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
   854 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
   855 static bool _shared_metaspaces_initialized = false;
   857 // Initialize shared metaspaces (change to call from somewhere not lazily)
   858 void ClassLoaderData::initialize_shared_metaspaces() {
   859   assert(DumpSharedSpaces, "only use this for dumping shared spaces");
   860   assert(this == ClassLoaderData::the_null_class_loader_data(),
   861          "only supported for null loader data for now");
   862   assert (!_shared_metaspaces_initialized, "only initialize once");
   863   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   864   _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
   865   _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
   866   _shared_metaspaces_initialized = true;
   867 }
   869 Metaspace* ClassLoaderData::ro_metaspace() {
   870   assert(_ro_metaspace != NULL, "should already be initialized");
   871   return _ro_metaspace;
   872 }
   874 Metaspace* ClassLoaderData::rw_metaspace() {
   875   assert(_rw_metaspace != NULL, "should already be initialized");
   876   return _rw_metaspace;
   877 }
   879 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
   880     : _next_klass(NULL) {
   881   ClassLoaderData* cld = ClassLoaderDataGraph::_head;
   882   Klass* klass = NULL;
   884   // Find the first klass in the CLDG.
   885   while (cld != NULL) {
   886     klass = cld->_klasses;
   887     if (klass != NULL) {
   888       _next_klass = klass;
   889       return;
   890     }
   891     cld = cld->next();
   892   }
   893 }
   895 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
   896   Klass* next = klass->next_link();
   897   if (next != NULL) {
   898     return next;
   899   }
   901   // No more klasses in the current CLD. Time to find a new CLD.
   902   ClassLoaderData* cld = klass->class_loader_data();
   903   while (next == NULL) {
   904     cld = cld->next();
   905     if (cld == NULL) {
   906       break;
   907     }
   908     next = cld->_klasses;
   909   }
   911   return next;
   912 }
   914 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
   915   Klass* head = (Klass*)_next_klass;
   917   while (head != NULL) {
   918     Klass* next = next_klass_in_cldg(head);
   920     Klass* old_head = (Klass*)Atomic::cmpxchg_ptr(next, &_next_klass, head);
   922     if (old_head == head) {
   923       return head; // Won the CAS.
   924     }
   926     head = old_head;
   927   }
   929   // Nothing more for the iterator to hand out.
   930   assert(head == NULL, err_msg("head is " PTR_FORMAT ", expected not null:", p2i(head)));
   931   return NULL;
   932 }
   934 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
   935   _data = ClassLoaderDataGraph::_head;
   936 }
   938 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
   940 #ifndef PRODUCT
   941 // callable from debugger
   942 extern "C" int print_loader_data_graph() {
   943   ClassLoaderDataGraph::dump_on(tty);
   944   return 0;
   945 }
   947 void ClassLoaderDataGraph::verify() {
   948   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   949     data->verify();
   950   }
   951 }
   953 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
   954   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   955     data->dump(out);
   956   }
   957   MetaspaceAux::dump(out);
   958 }
   959 #endif // PRODUCT
   961 void ClassLoaderData::print_value_on(outputStream* out) const {
   962   if (class_loader() == NULL) {
   963     out->print("NULL class_loader");
   964   } else {
   965     out->print("class loader " INTPTR_FORMAT, p2i(this));
   966     class_loader()->print_value_on(out);
   967   }
   968 }
   970 #if INCLUDE_TRACE
   972 Ticks ClassLoaderDataGraph::_class_unload_time;
   974 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
   976   // post class unload event
   977   EventClassUnload event(UNTIMED);
   978   event.set_endtime(_class_unload_time);
   979   event.set_unloadedClass(k);
   980   oop defining_class_loader = k->class_loader();
   981   event.set_definingClassLoader(defining_class_loader != NULL ?
   982                                 defining_class_loader->klass() : (Klass*)NULL);
   983   event.commit();
   984 }
   986 #endif // INCLUDE_TRACE

mercurial