src/share/vm/classfile/classLoaderData.cpp

Mon, 25 Mar 2013 17:13:26 -0700

author
twisti
date
Mon, 25 Mar 2013 17:13:26 -0700
changeset 4866
16885e702c88
parent 4751
1fc4d4768b90
child 4903
ba42fd5e00e6
permissions
-rw-r--r--

7198429: need checked categorization of caller-sensitive methods in the JDK
Reviewed-by: kvn, jrose

coleenp@4037 1 /*
coleenp@4490 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
coleenp@4037 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@4037 4 *
coleenp@4037 5 * This code is free software; you can redistribute it and/or modify it
coleenp@4037 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@4037 7 * published by the Free Software Foundation.
coleenp@4037 8 *
coleenp@4037 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@4037 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@4037 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@4037 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@4037 13 * accompanied this code).
coleenp@4037 14 *
coleenp@4037 15 * You should have received a copy of the GNU General Public License version
coleenp@4037 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@4037 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@4037 18 *
coleenp@4037 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@4037 20 * or visit www.oracle.com if you need additional information or have any
coleenp@4037 21 * questions.
coleenp@4037 22 *
coleenp@4037 23 */
coleenp@4037 24
coleenp@4037 25 // A ClassLoaderData identifies the full set of class types that a class
coleenp@4037 26 // loader's name resolution strategy produces for a given configuration of the
coleenp@4037 27 // class loader.
coleenp@4037 28 // Class types in the ClassLoaderData may be defined by from class file binaries
coleenp@4037 29 // provided by the class loader, or from other class loader it interacts with
coleenp@4037 30 // according to its name resolution strategy.
coleenp@4037 31 //
coleenp@4037 32 // Class loaders that implement a deterministic name resolution strategy
coleenp@4037 33 // (including with respect to their delegation behavior), such as the boot, the
coleenp@4037 34 // extension, and the system loaders of the JDK's built-in class loader
coleenp@4037 35 // hierarchy, always produce the same linkset for a given configuration.
coleenp@4037 36 //
coleenp@4037 37 // ClassLoaderData carries information related to a linkset (e.g.,
coleenp@4037 38 // metaspace holding its klass definitions).
coleenp@4037 39 // The System Dictionary and related data structures (e.g., placeholder table,
coleenp@4037 40 // loader constraints table) as well as the runtime representation of classes
coleenp@4037 41 // only reference ClassLoaderData.
coleenp@4037 42 //
coleenp@4037 43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
coleenp@4037 44 // that represent the loader's "linking domain" in the JVM.
coleenp@4037 45 //
coleenp@4037 46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
coleenp@4037 47 // the singleton class the_null_class_loader_data().
coleenp@4037 48
coleenp@4037 49 #include "precompiled.hpp"
coleenp@4037 50 #include "classfile/classLoaderData.hpp"
coleenp@4037 51 #include "classfile/classLoaderData.inline.hpp"
coleenp@4037 52 #include "classfile/javaClasses.hpp"
coleenp@4490 53 #include "classfile/metadataOnStackMark.hpp"
coleenp@4037 54 #include "classfile/systemDictionary.hpp"
coleenp@4037 55 #include "code/codeCache.hpp"
coleenp@4037 56 #include "memory/metadataFactory.hpp"
coleenp@4037 57 #include "memory/metaspaceShared.hpp"
coleenp@4490 58 #include "memory/oopFactory.hpp"
coleenp@4037 59 #include "runtime/jniHandles.hpp"
coleenp@4037 60 #include "runtime/mutex.hpp"
coleenp@4037 61 #include "runtime/safepoint.hpp"
coleenp@4037 62 #include "runtime/synchronizer.hpp"
coleenp@4037 63 #include "utilities/growableArray.hpp"
coleenp@4037 64 #include "utilities/ostream.hpp"
coleenp@4037 65
coleenp@4037 66 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
coleenp@4037 67
coleenp@4345 68 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
coleenp@4345 69 _class_loader(h_class_loader()),
coleenp@4345 70 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
coleenp@4345 71 _metaspace(NULL), _unloading(false), _klasses(NULL),
coleenp@4304 72 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
coleenp@4304 73 _next(NULL), _dependencies(NULL),
coleenp@4037 74 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
coleenp@4037 75 // empty
coleenp@4037 76 }
coleenp@4037 77
coleenp@4304 78 void ClassLoaderData::init_dependencies(TRAPS) {
coleenp@4304 79 // Create empty dependencies array to add to. CMS requires this to be
coleenp@4304 80 // an oop so that it can track additions via card marks. We think.
coleenp@4304 81 _dependencies = (oop)oopFactory::new_objectArray(2, CHECK);
coleenp@4304 82 }
coleenp@4304 83
coleenp@4037 84 bool ClassLoaderData::claim() {
coleenp@4037 85 if (_claimed == 1) {
coleenp@4037 86 return false;
coleenp@4037 87 }
coleenp@4037 88
coleenp@4037 89 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
coleenp@4037 90 }
coleenp@4037 91
coleenp@4037 92 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 93 if (must_claim && !claim()) {
coleenp@4037 94 return;
coleenp@4037 95 }
coleenp@4037 96
coleenp@4037 97 f->do_oop(&_class_loader);
coleenp@4304 98 f->do_oop(&_dependencies);
coleenp@4037 99 _handles->oops_do(f);
coleenp@4037 100 if (klass_closure != NULL) {
coleenp@4037 101 classes_do(klass_closure);
coleenp@4037 102 }
coleenp@4037 103 }
coleenp@4037 104
coleenp@4037 105 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
coleenp@4037 106 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 107 klass_closure->do_klass(k);
coleenp@4751 108 assert(k != k->next_link(), "no loops!");
coleenp@4037 109 }
coleenp@4037 110 }
coleenp@4037 111
coleenp@4037 112 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
coleenp@4037 113 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 114 if (k->oop_is_instance()) {
coleenp@4037 115 f(InstanceKlass::cast(k));
coleenp@4037 116 }
coleenp@4751 117 assert(k != k->next_link(), "no loops!");
coleenp@4037 118 }
coleenp@4037 119 }
coleenp@4037 120
coleenp@4037 121 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
coleenp@4037 122 ClassLoaderData * const from_cld = this;
coleenp@4037 123 ClassLoaderData * const to_cld = k->class_loader_data();
coleenp@4037 124
coleenp@4304 125 // Dependency to the null class loader data doesn't need to be recorded
coleenp@4304 126 // because the null class loader data never goes away.
coleenp@4304 127 if (to_cld->is_the_null_class_loader_data()) {
coleenp@4037 128 return;
coleenp@4037 129 }
coleenp@4037 130
coleenp@4304 131 oop to;
coleenp@4304 132 if (to_cld->is_anonymous()) {
coleenp@4304 133 // Anonymous class dependencies are through the mirror.
coleenp@4304 134 to = k->java_mirror();
coleenp@4304 135 } else {
coleenp@4304 136 to = to_cld->class_loader();
coleenp@4037 137
coleenp@4304 138 // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
coleenp@4304 139 // we still have to add it. The class_loader won't keep from_cld alive.
coleenp@4304 140 if (!from_cld->is_anonymous()) {
coleenp@4304 141 // Check that this dependency isn't from the same or parent class_loader
coleenp@4304 142 oop from = from_cld->class_loader();
coleenp@4304 143
coleenp@4304 144 oop curr = from;
coleenp@4304 145 while (curr != NULL) {
coleenp@4304 146 if (curr == to) {
coleenp@4304 147 return; // this class loader is in the parent list, no need to add it.
coleenp@4304 148 }
coleenp@4304 149 curr = java_lang_ClassLoader::parent(curr);
coleenp@4304 150 }
coleenp@4037 151 }
coleenp@4037 152 }
coleenp@4037 153
coleenp@4037 154 // It's a dependency we won't find through GC, add it. This is relatively rare
coleenp@4304 155 // Must handle over GC point.
coleenp@4304 156 Handle dependency(THREAD, to);
coleenp@4304 157 from_cld->add_dependency(dependency, CHECK);
coleenp@4037 158 }
coleenp@4037 159
coleenp@4037 160
coleenp@4304 161 void ClassLoaderData::add_dependency(Handle dependency, TRAPS) {
coleenp@4304 162 // Check first if this dependency is already in the list.
coleenp@4304 163 // Save a pointer to the last to add to under the lock.
coleenp@4304 164 objArrayOop ok = (objArrayOop)_dependencies;
coleenp@4304 165 objArrayOop last = NULL;
coleenp@4037 166 while (ok != NULL) {
coleenp@4304 167 last = ok;
coleenp@4304 168 if (ok->obj_at(0) == dependency()) {
coleenp@4304 169 // Don't need to add it
coleenp@4304 170 return;
coleenp@4037 171 }
coleenp@4037 172 ok = (objArrayOop)ok->obj_at(1);
coleenp@4037 173 }
coleenp@4304 174
stefank@4353 175 // Must handle over GC points
stefank@4353 176 assert (last != NULL, "dependencies should be initialized");
stefank@4353 177 objArrayHandle last_handle(THREAD, last);
stefank@4353 178
coleenp@4304 179 // Create a new dependency node with fields for (class_loader or mirror, next)
coleenp@4304 180 objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
coleenp@4304 181 deps->obj_at_put(0, dependency());
coleenp@4304 182
stefank@4353 183 // Must handle over GC points
coleenp@4304 184 objArrayHandle new_dependency(THREAD, deps);
coleenp@4304 185
coleenp@4304 186 // Add the dependency under lock
coleenp@4304 187 locked_add_dependency(last_handle, new_dependency);
coleenp@4037 188 }
coleenp@4037 189
coleenp@4304 190 void ClassLoaderData::locked_add_dependency(objArrayHandle last_handle,
coleenp@4304 191 objArrayHandle new_dependency) {
coleenp@4304 192
coleenp@4304 193 // Have to lock and put the new dependency on the end of the dependency
coleenp@4304 194 // array so the card mark for CMS sees that this dependency is new.
coleenp@4304 195 // Can probably do this lock free with some effort.
coleenp@4304 196 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4304 197
coleenp@4304 198 oop loader_or_mirror = new_dependency->obj_at(0);
coleenp@4304 199
coleenp@4304 200 // Since the dependencies are only added, add to the end.
coleenp@4304 201 objArrayOop end = last_handle();
coleenp@4304 202 objArrayOop last = NULL;
coleenp@4304 203 while (end != NULL) {
coleenp@4304 204 last = end;
coleenp@4304 205 // check again if another thread added it to the end.
coleenp@4304 206 if (end->obj_at(0) == loader_or_mirror) {
coleenp@4304 207 // Don't need to add it
coleenp@4304 208 return;
coleenp@4304 209 }
coleenp@4304 210 end = (objArrayOop)end->obj_at(1);
coleenp@4037 211 }
coleenp@4304 212 assert (last != NULL, "dependencies should be initialized");
coleenp@4304 213 // fill in the first element with the oop in new_dependency.
coleenp@4304 214 if (last->obj_at(0) == NULL) {
coleenp@4304 215 last->obj_at_put(0, new_dependency->obj_at(0));
coleenp@4304 216 } else {
coleenp@4304 217 last->obj_at_put(1, new_dependency());
coleenp@4037 218 }
coleenp@4037 219 }
coleenp@4037 220
coleenp@4037 221 void ClassLoaderDataGraph::clear_claimed_marks() {
coleenp@4037 222 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 223 cld->clear_claimed();
coleenp@4037 224 }
coleenp@4037 225 }
coleenp@4037 226
coleenp@4037 227 void ClassLoaderData::add_class(Klass* k) {
coleenp@4037 228 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 229 Klass* old_value = _klasses;
coleenp@4037 230 k->set_next_link(old_value);
coleenp@4037 231 // link the new item into the list
coleenp@4037 232 _klasses = k;
coleenp@4037 233
coleenp@4304 234 if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
coleenp@4037 235 ResourceMark rm;
coleenp@4037 236 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
coleenp@4037 237 PTR_FORMAT " loader: " PTR_FORMAT " %s",
coleenp@4037 238 k,
coleenp@4037 239 k->external_name(),
coleenp@4037 240 k->class_loader_data(),
coleenp@4037 241 k->class_loader(),
coleenp@4304 242 loader_name());
coleenp@4037 243 }
coleenp@4037 244 }
coleenp@4037 245
coleenp@4037 246 // This is called by InstanceKlass::deallocate_contents() to remove the
coleenp@4037 247 // scratch_class for redefine classes. We need a lock because there it may not
coleenp@4037 248 // be called at a safepoint if there's an error.
coleenp@4037 249 void ClassLoaderData::remove_class(Klass* scratch_class) {
coleenp@4037 250 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 251 Klass* prev = NULL;
coleenp@4037 252 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 253 if (k == scratch_class) {
coleenp@4037 254 if (prev == NULL) {
coleenp@4037 255 _klasses = k->next_link();
coleenp@4037 256 } else {
coleenp@4037 257 Klass* next = k->next_link();
coleenp@4037 258 prev->set_next_link(next);
coleenp@4037 259 }
coleenp@4037 260 return;
coleenp@4037 261 }
coleenp@4037 262 prev = k;
coleenp@4751 263 assert(k != k->next_link(), "no loops!");
coleenp@4037 264 }
coleenp@4037 265 ShouldNotReachHere(); // should have found this class!!
coleenp@4037 266 }
coleenp@4037 267
coleenp@4304 268 void ClassLoaderData::unload() {
coleenp@4304 269 _unloading = true;
coleenp@4304 270
coleenp@4304 271 if (TraceClassLoaderData) {
coleenp@4304 272 ResourceMark rm;
coleenp@4304 273 tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this);
coleenp@4304 274 tty->print(" for instance "PTR_FORMAT" of %s", class_loader(),
coleenp@4304 275 loader_name());
coleenp@4304 276 if (is_anonymous()) {
coleenp@4304 277 tty->print(" for anonymous class "PTR_FORMAT " ", _klasses);
coleenp@4304 278 }
coleenp@4304 279 tty->print_cr("]");
coleenp@4304 280 }
coleenp@4304 281 }
coleenp@4304 282
coleenp@4304 283 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
coleenp@4304 284 bool alive =
coleenp@4304 285 is_anonymous() ?
coleenp@4304 286 is_alive_closure->do_object_b(_klasses->java_mirror()) :
coleenp@4304 287 class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
coleenp@4304 288 assert(!alive || claimed(), "must be claimed");
coleenp@4304 289 return alive;
coleenp@4304 290 }
coleenp@4304 291
coleenp@4304 292
coleenp@4037 293 ClassLoaderData::~ClassLoaderData() {
coleenp@4037 294 Metaspace *m = _metaspace;
coleenp@4037 295 if (m != NULL) {
coleenp@4037 296 _metaspace = NULL;
coleenp@4037 297 // release the metaspace
coleenp@4037 298 delete m;
coleenp@4037 299 // release the handles
coleenp@4037 300 if (_handles != NULL) {
coleenp@4037 301 JNIHandleBlock::release_block(_handles);
coleenp@4037 302 _handles = NULL;
coleenp@4037 303 }
coleenp@4037 304 }
coleenp@4037 305
coleenp@4037 306 // Clear all the JNI handles for methods
coleenp@4037 307 // These aren't deallocated and are going to look like a leak, but that's
coleenp@4037 308 // needed because we can't really get rid of jmethodIDs because we don't
coleenp@4037 309 // know when native code is going to stop using them. The spec says that
coleenp@4037 310 // they're "invalid" but existing programs likely rely on their being
coleenp@4037 311 // NULL after class unloading.
coleenp@4037 312 if (_jmethod_ids != NULL) {
coleenp@4037 313 Method::clear_jmethod_ids(this);
coleenp@4037 314 }
coleenp@4037 315 // Delete lock
coleenp@4037 316 delete _metaspace_lock;
coleenp@4037 317
coleenp@4037 318 // Delete free list
coleenp@4037 319 if (_deallocate_list != NULL) {
coleenp@4037 320 delete _deallocate_list;
coleenp@4037 321 }
coleenp@4037 322 }
coleenp@4037 323
twisti@4866 324 /**
twisti@4866 325 * Returns true if this class loader data is for the extension class loader.
twisti@4866 326 */
twisti@4866 327 bool ClassLoaderData::is_ext_class_loader_data() const {
twisti@4866 328 return SystemDictionary::is_ext_class_loader(class_loader());
twisti@4866 329 }
twisti@4866 330
coleenp@4037 331 Metaspace* ClassLoaderData::metaspace_non_null() {
jmasa@4457 332 assert(!DumpSharedSpaces, "wrong metaspace!");
coleenp@4037 333 // If the metaspace has not been allocated, create a new one. Might want
coleenp@4037 334 // to create smaller arena for Reflection class loaders also.
coleenp@4037 335 // The reason for the delayed allocation is because some class loaders are
coleenp@4037 336 // simply for delegating with no metadata of their own.
coleenp@4037 337 if (_metaspace == NULL) {
coleenp@4037 338 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 339 // Check again if metaspace has been allocated while we were getting this lock.
coleenp@4037 340 if (_metaspace != NULL) {
coleenp@4037 341 return _metaspace;
coleenp@4037 342 }
coleenp@4304 343 if (this == the_null_class_loader_data()) {
coleenp@4304 344 assert (class_loader() == NULL, "Must be");
jmasa@4382 345 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
jmasa@4382 346 } else if (is_anonymous()) {
jmasa@4382 347 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
jmasa@4382 348 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
jmasa@4382 349 }
jmasa@4382 350 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
jmasa@4382 351 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
jmasa@4382 352 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
jmasa@4382 353 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
jmasa@4382 354 }
jmasa@4382 355 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
coleenp@4037 356 } else {
jmasa@4382 357 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
coleenp@4037 358 }
coleenp@4037 359 }
coleenp@4037 360 return _metaspace;
coleenp@4037 361 }
coleenp@4037 362
coleenp@4037 363 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
coleenp@4037 364 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
coleenp@4037 365
coleenp@4037 366 jobject ClassLoaderData::add_handle(Handle h) {
coleenp@4037 367 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 368 if (handles() == NULL) {
coleenp@4037 369 set_handles(JNIHandleBlock::allocate_block());
coleenp@4037 370 }
coleenp@4037 371 return handles()->allocate_handle(h());
coleenp@4037 372 }
coleenp@4037 373
coleenp@4037 374 // Add this metadata pointer to be freed when it's safe. This is only during
coleenp@4037 375 // class unloading because Handles might point to this metadata field.
coleenp@4037 376 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
coleenp@4037 377 // Metadata in shared region isn't deleted.
coleenp@4037 378 if (!m->is_shared()) {
coleenp@4037 379 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 380 if (_deallocate_list == NULL) {
coleenp@4037 381 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
coleenp@4037 382 }
coleenp@4037 383 _deallocate_list->append_if_missing(m);
coleenp@4037 384 }
coleenp@4037 385 }
coleenp@4037 386
coleenp@4037 387 // Deallocate free metadata on the free list. How useful the PermGen was!
coleenp@4037 388 void ClassLoaderData::free_deallocate_list() {
coleenp@4037 389 // Don't need lock, at safepoint
coleenp@4037 390 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
coleenp@4037 391 if (_deallocate_list == NULL) {
coleenp@4037 392 return;
coleenp@4037 393 }
coleenp@4037 394 // Go backwards because this removes entries that are freed.
coleenp@4037 395 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
coleenp@4037 396 Metadata* m = _deallocate_list->at(i);
coleenp@4037 397 if (!m->on_stack()) {
coleenp@4037 398 _deallocate_list->remove_at(i);
coleenp@4037 399 // There are only three types of metadata that we deallocate directly.
coleenp@4037 400 // Cast them so they can be used by the template function.
coleenp@4037 401 if (m->is_method()) {
coleenp@4037 402 MetadataFactory::free_metadata(this, (Method*)m);
coleenp@4037 403 } else if (m->is_constantPool()) {
coleenp@4037 404 MetadataFactory::free_metadata(this, (ConstantPool*)m);
coleenp@4037 405 } else if (m->is_klass()) {
coleenp@4037 406 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
coleenp@4037 407 } else {
coleenp@4037 408 ShouldNotReachHere();
coleenp@4037 409 }
coleenp@4037 410 }
coleenp@4037 411 }
coleenp@4037 412 }
coleenp@4037 413
coleenp@4304 414 // These anonymous class loaders are to contain classes used for JSR292
coleenp@4304 415 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
coleenp@4304 416 // Add a new class loader data to the graph.
coleenp@4345 417 return ClassLoaderDataGraph::add(NULL, loader, CHECK_NULL);
coleenp@4037 418 }
coleenp@4037 419
coleenp@4304 420 const char* ClassLoaderData::loader_name() {
coleenp@4304 421 // Handles null class loader
coleenp@4304 422 return SystemDictionary::loader_name(class_loader());
coleenp@4304 423 }
coleenp@4304 424
coleenp@4304 425 #ifndef PRODUCT
coleenp@4037 426 // Define to dump klasses
coleenp@4037 427 #undef CLD_DUMP_KLASSES
coleenp@4037 428
coleenp@4037 429 void ClassLoaderData::dump(outputStream * const out) {
coleenp@4037 430 ResourceMark rm;
coleenp@4037 431 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
coleenp@4037 432 this, class_loader(),
coleenp@4304 433 class_loader() != NULL ? class_loader()->klass() : NULL, loader_name());
coleenp@4037 434 if (claimed()) out->print(" claimed ");
coleenp@4037 435 if (is_unloading()) out->print(" unloading ");
coleenp@4037 436 out->print(" handles " INTPTR_FORMAT, handles());
coleenp@4037 437 out->cr();
coleenp@4037 438 if (metaspace_or_null() != NULL) {
coleenp@4037 439 out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
coleenp@4037 440 metaspace_or_null()->dump(out);
coleenp@4037 441 } else {
coleenp@4037 442 out->print_cr("metaspace: NULL");
coleenp@4037 443 }
coleenp@4037 444
coleenp@4037 445 #ifdef CLD_DUMP_KLASSES
coleenp@4037 446 if (Verbose) {
coleenp@4037 447 ResourceMark rm;
coleenp@4037 448 Klass* k = _klasses;
coleenp@4037 449 while (k != NULL) {
coleenp@4037 450 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
coleenp@4037 451 k->has_modified_oops(), k->has_accumulated_modified_oops());
coleenp@4751 452 assert(k != k->next_link(), "no loops!");
coleenp@4037 453 k = k->next_link();
coleenp@4037 454 }
coleenp@4037 455 }
coleenp@4037 456 #endif // CLD_DUMP_KLASSES
coleenp@4037 457 #undef CLD_DUMP_KLASSES
coleenp@4037 458 if (_jmethod_ids != NULL) {
coleenp@4037 459 Method::print_jmethod_ids(this, out);
coleenp@4037 460 }
coleenp@4037 461 out->print_cr("}");
coleenp@4037 462 }
coleenp@4037 463 #endif // PRODUCT
coleenp@4037 464
coleenp@4037 465 void ClassLoaderData::verify() {
coleenp@4037 466 oop cl = class_loader();
coleenp@4037 467
coleenp@4304 468 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
coleenp@4304 469 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
coleenp@4037 470
coleenp@4037 471 // Verify the integrity of the allocated space.
coleenp@4037 472 if (metaspace_or_null() != NULL) {
coleenp@4037 473 metaspace_or_null()->verify();
coleenp@4037 474 }
coleenp@4037 475
coleenp@4037 476 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
coleenp@4037 477 guarantee(k->class_loader_data() == this, "Must be the same");
coleenp@4037 478 k->verify();
coleenp@4751 479 assert(k != k->next_link(), "no loops!");
coleenp@4037 480 }
coleenp@4037 481 }
coleenp@4037 482
coleenp@4304 483
coleenp@4037 484 // GC root of class loader data created.
coleenp@4037 485 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
coleenp@4037 486 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
coleenp@4037 487 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
coleenp@4037 488
coleenp@4037 489
coleenp@4037 490 // Add a new class loader data node to the list. Assign the newly created
coleenp@4037 491 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
coleenp@4304 492 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader, TRAPS) {
coleenp@4037 493 // Not assigned a class loader data yet.
coleenp@4037 494 // Create one.
coleenp@4037 495 ClassLoaderData* *list_head = &_head;
coleenp@4037 496 ClassLoaderData* next = _head;
coleenp@4345 497
coleenp@4345 498 bool is_anonymous = (cld_addr == NULL);
coleenp@4345 499 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
coleenp@4037 500
coleenp@4304 501 if (cld_addr != NULL) {
coleenp@4304 502 // First, Atomically set it
coleenp@4304 503 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
coleenp@4304 504 if (old != NULL) {
coleenp@4304 505 delete cld;
coleenp@4304 506 // Returns the data.
coleenp@4304 507 return old;
coleenp@4304 508 }
coleenp@4037 509 }
coleenp@4037 510
coleenp@4037 511 // We won the race, and therefore the task of adding the data to the list of
coleenp@4037 512 // class loader data
coleenp@4037 513 do {
coleenp@4037 514 cld->set_next(next);
coleenp@4037 515 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
coleenp@4037 516 if (exchanged == next) {
coleenp@4037 517 if (TraceClassLoaderData) {
coleenp@4304 518 ResourceMark rm;
coleenp@4037 519 tty->print("[ClassLoaderData: ");
coleenp@4037 520 tty->print("create class loader data "PTR_FORMAT, cld);
coleenp@4304 521 tty->print(" for instance "PTR_FORMAT" of %s", cld->class_loader(),
coleenp@4304 522 cld->loader_name());
coleenp@4037 523 tty->print_cr("]");
coleenp@4037 524 }
coleenp@4304 525 // Create dependencies after the CLD is added to the list. Otherwise,
coleenp@4304 526 // the GC GC will not find the CLD and the _class_loader field will
coleenp@4304 527 // not be updated.
coleenp@4304 528 cld->init_dependencies(CHECK_NULL);
coleenp@4037 529 return cld;
coleenp@4037 530 }
coleenp@4037 531 next = exchanged;
coleenp@4037 532 } while (true);
coleenp@4304 533
coleenp@4037 534 }
coleenp@4037 535
coleenp@4037 536 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 537 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 538 cld->oops_do(f, klass_closure, must_claim);
coleenp@4037 539 }
coleenp@4037 540 }
coleenp@4037 541
coleenp@4304 542 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4304 543 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4304 544 if (cld->keep_alive()) {
coleenp@4304 545 cld->oops_do(f, klass_closure, must_claim);
coleenp@4304 546 }
coleenp@4304 547 }
coleenp@4304 548 }
coleenp@4304 549
coleenp@4037 550 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
coleenp@4037 551 if (ClassUnloading) {
coleenp@4037 552 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
coleenp@4304 553 // keep any special CLDs alive.
coleenp@4304 554 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
coleenp@4037 555 } else {
coleenp@4037 556 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
coleenp@4037 557 }
coleenp@4037 558 }
coleenp@4037 559
coleenp@4037 560 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
coleenp@4037 561 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 562 cld->classes_do(klass_closure);
coleenp@4037 563 }
coleenp@4037 564 }
coleenp@4037 565
coleenp@4037 566 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
coleenp@4037 567 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
coleenp@4037 568
coleenp@4037 569 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
coleenp@4037 570
coleenp@4037 571 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
coleenp@4037 572 ClassLoaderData* curr = _head;
coleenp@4037 573 while (curr != _saved_head) {
coleenp@4037 574 if (!curr->claimed()) {
coleenp@4037 575 array->push(curr);
coleenp@4037 576
coleenp@4037 577 if (TraceClassLoaderData) {
coleenp@4037 578 tty->print("[ClassLoaderData] found new CLD: ");
coleenp@4037 579 curr->print_value_on(tty);
coleenp@4037 580 tty->cr();
coleenp@4037 581 }
coleenp@4037 582 }
coleenp@4037 583
coleenp@4037 584 curr = curr->_next;
coleenp@4037 585 }
coleenp@4037 586
coleenp@4037 587 return array;
coleenp@4037 588 }
coleenp@4037 589
coleenp@4037 590 #ifndef PRODUCT
coleenp@4037 591 // for debugging and hsfind(x)
coleenp@4037 592 bool ClassLoaderDataGraph::contains(address x) {
coleenp@4037 593 // I think we need the _metaspace_lock taken here because the class loader
coleenp@4037 594 // data graph could be changing while we are walking it (new entries added,
coleenp@4037 595 // new entries being unloaded, etc).
coleenp@4037 596 if (DumpSharedSpaces) {
coleenp@4037 597 // There are only two metaspaces to worry about.
coleenp@4037 598 ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
coleenp@4037 599 return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
coleenp@4037 600 }
coleenp@4037 601
coleenp@4037 602 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
coleenp@4037 603 return true;
coleenp@4037 604 }
coleenp@4037 605
coleenp@4037 606 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
coleenp@4037 607 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
coleenp@4037 608 return true;
coleenp@4037 609 }
coleenp@4037 610 }
coleenp@4037 611
coleenp@4037 612 // Could also be on an unloading list which is okay, ie. still allocated
coleenp@4037 613 // for a little while.
coleenp@4037 614 for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
coleenp@4037 615 if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
coleenp@4037 616 return true;
coleenp@4037 617 }
coleenp@4037 618 }
coleenp@4037 619 return false;
coleenp@4037 620 }
coleenp@4037 621
coleenp@4037 622 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
coleenp@4037 623 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 624 if (loader_data == data) {
coleenp@4037 625 return true;
coleenp@4037 626 }
coleenp@4037 627 }
coleenp@4037 628
coleenp@4037 629 return false;
coleenp@4037 630 }
coleenp@4037 631 #endif // PRODUCT
coleenp@4037 632
coleenp@4304 633
coleenp@4037 634 // Move class loader data from main list to the unloaded list for unloading
coleenp@4037 635 // and deallocation later.
coleenp@4304 636 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
coleenp@4037 637 ClassLoaderData* data = _head;
coleenp@4037 638 ClassLoaderData* prev = NULL;
coleenp@4037 639 bool seen_dead_loader = false;
coleenp@4037 640 // mark metadata seen on the stack and code cache so we can delete
coleenp@4037 641 // unneeded entries.
coleenp@4037 642 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
coleenp@4037 643 MetadataOnStackMark md_on_stack;
coleenp@4037 644 while (data != NULL) {
coleenp@4304 645 if (data->keep_alive() || data->is_alive(is_alive_closure)) {
coleenp@4037 646 if (has_redefined_a_class) {
coleenp@4037 647 data->classes_do(InstanceKlass::purge_previous_versions);
coleenp@4037 648 }
coleenp@4037 649 data->free_deallocate_list();
coleenp@4037 650 prev = data;
coleenp@4037 651 data = data->next();
coleenp@4037 652 continue;
coleenp@4037 653 }
coleenp@4037 654 seen_dead_loader = true;
coleenp@4037 655 ClassLoaderData* dead = data;
coleenp@4304 656 dead->unload();
coleenp@4037 657 data = data->next();
coleenp@4037 658 // Remove from loader list.
coleenp@4037 659 if (prev != NULL) {
coleenp@4037 660 prev->set_next(data);
coleenp@4037 661 } else {
coleenp@4037 662 assert(dead == _head, "sanity check");
coleenp@4037 663 _head = data;
coleenp@4037 664 }
coleenp@4037 665 dead->set_next(_unloading);
coleenp@4037 666 _unloading = dead;
coleenp@4037 667 }
coleenp@4037 668 return seen_dead_loader;
coleenp@4037 669 }
coleenp@4037 670
coleenp@4037 671 void ClassLoaderDataGraph::purge() {
coleenp@4037 672 ClassLoaderData* list = _unloading;
coleenp@4037 673 _unloading = NULL;
coleenp@4037 674 ClassLoaderData* next = list;
coleenp@4037 675 while (next != NULL) {
coleenp@4037 676 ClassLoaderData* purge_me = next;
coleenp@4037 677 next = purge_me->next();
coleenp@4037 678 delete purge_me;
coleenp@4037 679 }
coleenp@4037 680 }
coleenp@4037 681
coleenp@4037 682 // CDS support
coleenp@4037 683
coleenp@4037 684 // Global metaspaces for writing information to the shared archive. When
coleenp@4037 685 // application CDS is supported, we may need one per metaspace, so this
coleenp@4037 686 // sort of looks like it.
coleenp@4037 687 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
coleenp@4037 688 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
coleenp@4037 689 static bool _shared_metaspaces_initialized = false;
coleenp@4037 690
coleenp@4037 691 // Initialize shared metaspaces (change to call from somewhere not lazily)
coleenp@4037 692 void ClassLoaderData::initialize_shared_metaspaces() {
coleenp@4037 693 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
coleenp@4037 694 assert(this == ClassLoaderData::the_null_class_loader_data(),
coleenp@4037 695 "only supported for null loader data for now");
coleenp@4037 696 assert (!_shared_metaspaces_initialized, "only initialize once");
coleenp@4037 697 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
jmasa@4382 698 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
jmasa@4382 699 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
coleenp@4037 700 _shared_metaspaces_initialized = true;
coleenp@4037 701 }
coleenp@4037 702
coleenp@4037 703 Metaspace* ClassLoaderData::ro_metaspace() {
coleenp@4037 704 assert(_ro_metaspace != NULL, "should already be initialized");
coleenp@4037 705 return _ro_metaspace;
coleenp@4037 706 }
coleenp@4037 707
coleenp@4037 708 Metaspace* ClassLoaderData::rw_metaspace() {
coleenp@4037 709 assert(_rw_metaspace != NULL, "should already be initialized");
coleenp@4037 710 return _rw_metaspace;
coleenp@4037 711 }
coleenp@4037 712
coleenp@4037 713
coleenp@4037 714 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
coleenp@4037 715 _data = ClassLoaderDataGraph::_head;
coleenp@4037 716 }
coleenp@4037 717
coleenp@4037 718 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
coleenp@4037 719
coleenp@4037 720 #ifndef PRODUCT
coleenp@4037 721 // callable from debugger
coleenp@4037 722 extern "C" int print_loader_data_graph() {
coleenp@4037 723 ClassLoaderDataGraph::dump_on(tty);
coleenp@4037 724 return 0;
coleenp@4037 725 }
coleenp@4037 726
coleenp@4037 727 void ClassLoaderDataGraph::verify() {
coleenp@4037 728 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 729 data->verify();
coleenp@4037 730 }
coleenp@4037 731 }
coleenp@4037 732
coleenp@4037 733 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
coleenp@4037 734 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
coleenp@4037 735 data->dump(out);
coleenp@4037 736 }
coleenp@4037 737 MetaspaceAux::dump(out);
coleenp@4037 738 }
acorn@4497 739 #endif // PRODUCT
coleenp@4037 740
coleenp@4037 741 void ClassLoaderData::print_value_on(outputStream* out) const {
coleenp@4037 742 if (class_loader() == NULL) {
acorn@4497 743 out->print("NULL class_loader");
coleenp@4037 744 } else {
coleenp@4037 745 out->print("class loader "PTR_FORMAT, this);
coleenp@4037 746 class_loader()->print_value_on(out);
coleenp@4037 747 }
coleenp@4037 748 }

mercurial