1 /* 2 * Copyright (c) 2003, 2018, 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 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderData.inline.hpp" 27 #include "classfile/classLoaderDataGraph.hpp" 28 #include "classfile/dictionary.hpp" 29 #include "classfile/loaderConstraints.hpp" 30 #include "logging/log.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/safepoint.hpp" 35 #include "utilities/hashtable.inline.hpp" 36 37 void LoaderConstraintEntry::set_loader(int i, oop p) { 38 set_loader_data(i, ClassLoaderData::class_loader_data(p)); 39 } 40 41 LoaderConstraintTable::LoaderConstraintTable(int table_size) 42 : Hashtable<InstanceKlass*, mtClass>(table_size, sizeof(LoaderConstraintEntry)) {}; 43 44 45 LoaderConstraintEntry* LoaderConstraintTable::new_entry( 46 unsigned int hash, Symbol* name, 47 InstanceKlass* klass, int num_loaders, 48 int max_loaders) { 49 LoaderConstraintEntry* entry; 50 entry = (LoaderConstraintEntry*)Hashtable<InstanceKlass*, mtClass>::new_entry(hash, klass); 51 entry->set_name(name); 52 entry->set_num_loaders(num_loaders); 53 entry->set_max_loaders(max_loaders); 54 return entry; 55 } 56 57 void LoaderConstraintTable::free_entry(LoaderConstraintEntry *entry) { 58 // decrement name refcount before freeing 59 entry->name()->decrement_refcount(); 60 Hashtable<InstanceKlass*, mtClass>::free_entry(entry); 61 } 62 63 // The loaderConstraintTable must always be accessed with the 64 // SystemDictionary lock held. This is true even for readers as 65 // entries in the table could be being dynamically resized. 66 67 LoaderConstraintEntry** LoaderConstraintTable::find_loader_constraint( 68 Symbol* name, Handle loader) { 69 assert_lock_strong(SystemDictionary_lock); 70 unsigned int hash = compute_hash(name); 71 int index = hash_to_index(hash); 72 LoaderConstraintEntry** pp = bucket_addr(index); 73 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader()); 74 75 while (*pp) { 76 LoaderConstraintEntry* p = *pp; 77 if (p->hash() == hash) { 78 if (p->name() == name) { 79 for (int i = p->num_loaders() - 1; i >= 0; i--) { 80 if (p->loader_data(i) == loader_data) { 81 return pp; 82 } 83 } 84 } 85 } 86 pp = p->next_addr(); 87 } 88 return pp; 89 } 90 91 92 void LoaderConstraintTable::purge_loader_constraints() { 93 assert_locked_or_safepoint(SystemDictionary_lock); 94 LogTarget(Info, class, loader, constraints) lt; 95 // Remove unloaded entries from constraint table 96 for (int index = 0; index < table_size(); index++) { 97 LoaderConstraintEntry** p = bucket_addr(index); 98 while(*p) { 99 LoaderConstraintEntry* probe = *p; 100 InstanceKlass* klass = probe->klass(); 101 // Remove klass that is no longer alive 102 if (klass != NULL && 103 !klass->is_loader_alive()) { 104 probe->set_klass(NULL); 105 if (lt.is_enabled()) { 106 ResourceMark rm; 107 lt.print("purging class object from constraint for name %s," 108 " loader list:", 109 probe->name()->as_C_string()); 110 for (int i = 0; i < probe->num_loaders(); i++) { 111 lt.print(" [%d]: %s", i, 112 probe->loader_data(i)->loader_name_and_id()); 113 } 114 } 115 } 116 // Remove entries no longer alive from loader array 117 int n = 0; 118 while (n < probe->num_loaders()) { 119 if (probe->loader_data(n)->is_unloading()) { 120 if (lt.is_enabled()) { 121 ResourceMark rm; 122 lt.print("purging loader %s from constraint for name %s", 123 probe->loader_data(n)->loader_name_and_id(), 124 probe->name()->as_C_string() 125 ); 126 } 127 128 // Compact array 129 int num = probe->num_loaders() - 1; 130 probe->set_num_loaders(num); 131 probe->set_loader_data(n, probe->loader_data(num)); 132 probe->set_loader_data(num, NULL); 133 134 if (lt.is_enabled()) { 135 ResourceMark rm; 136 lt.print("new loader list:"); 137 for (int i = 0; i < probe->num_loaders(); i++) { 138 lt.print(" [%d]: %s", i, 139 probe->loader_data(i)->loader_name_and_id()); 140 } 141 } 142 143 continue; // current element replaced, so restart without 144 // incrementing n 145 } 146 n++; 147 } 148 // Check whether entry should be purged 149 if (probe->num_loaders() < 2) { 150 if (lt.is_enabled()) { 151 ResourceMark rm; 152 lt.print("purging complete constraint for name %s", 153 probe->name()->as_C_string()); 154 } 155 156 // Purge entry 157 *p = probe->next(); 158 FREE_C_HEAP_ARRAY(oop, probe->loaders()); 159 free_entry(probe); 160 } else { 161 #ifdef ASSERT 162 if (probe->klass() != NULL) { 163 assert(probe->klass()->is_loader_alive(), "klass should be live"); 164 } 165 #endif 166 // Go to next entry 167 p = probe->next_addr(); 168 } 169 } 170 } 171 } 172 173 void log_ldr_constraint_msg(Symbol* class_name, const char* reason, 174 Handle class_loader1, Handle class_loader2) { 175 LogTarget(Info, class, loader, constraints) lt; 176 if (lt.is_enabled()) { 177 ResourceMark rm; 178 lt.print("Failed to add constraint for name: %s, loader[0]: %s," 179 " loader[1]: %s, Reason: %s", 180 class_name->as_C_string(), 181 ClassLoaderData::class_loader_data(class_loader1())->loader_name_and_id(), 182 ClassLoaderData::class_loader_data(class_loader2())->loader_name_and_id(), 183 reason); 184 } 185 } 186 187 bool LoaderConstraintTable::add_entry(Symbol* class_name, 188 InstanceKlass* klass1, Handle class_loader1, 189 InstanceKlass* klass2, Handle class_loader2) { 190 LogTarget(Info, class, loader, constraints) lt; 191 if (klass1 != NULL && klass2 != NULL) { 192 if (klass1 == klass2) { 193 // Same type already loaded in both places. There is no need for any constraint. 194 return true; 195 } else { 196 log_ldr_constraint_msg(class_name, 197 "The class objects presented by loader[0] and loader[1] " 198 "are different", 199 class_loader1, class_loader2); 200 return false; 201 } 202 } 203 204 InstanceKlass* klass = klass1 != NULL ? klass1 : klass2; 205 LoaderConstraintEntry** pp1 = find_loader_constraint(class_name, class_loader1); 206 if (*pp1 != NULL && (*pp1)->klass() != NULL) { 207 if (klass != NULL) { 208 if (klass != (*pp1)->klass()) { 209 log_ldr_constraint_msg(class_name, 210 "The class object presented by loader[0] does not match " 211 "the stored class object in the constraint", 212 class_loader1, class_loader2); 213 return false; 214 } 215 } else { 216 klass = (*pp1)->klass(); 217 } 218 } 219 220 LoaderConstraintEntry** pp2 = find_loader_constraint(class_name, class_loader2); 221 if (*pp2 != NULL && (*pp2)->klass() != NULL) { 222 if (klass != NULL) { 223 if (klass != (*pp2)->klass()) { 224 log_ldr_constraint_msg(class_name, 225 "The class object presented by loader[1] does not match " 226 "the stored class object in the constraint", 227 class_loader1, class_loader2); 228 return false; 229 } 230 } else { 231 klass = (*pp2)->klass(); 232 } 233 } 234 235 if (*pp1 == NULL && *pp2 == NULL) { 236 unsigned int hash = compute_hash(class_name); 237 int index = hash_to_index(hash); 238 LoaderConstraintEntry* p; 239 p = new_entry(hash, class_name, klass, 2, 2); 240 p->set_loaders(NEW_C_HEAP_ARRAY(ClassLoaderData*, 2, mtClass)); 241 p->set_loader(0, class_loader1()); 242 p->set_loader(1, class_loader2()); 243 p->set_klass(klass); 244 p->set_next(bucket(index)); 245 set_entry(index, p); 246 if (lt.is_enabled()) { 247 ResourceMark rm; 248 lt.print("adding new constraint for name: %s, loader[0]: %s," 249 " loader[1]: %s", 250 class_name->as_C_string(), 251 ClassLoaderData::class_loader_data(class_loader1())->loader_name_and_id(), 252 ClassLoaderData::class_loader_data(class_loader2())->loader_name_and_id() 253 ); 254 } 255 } else if (*pp1 == *pp2) { 256 /* constraint already imposed */ 257 if ((*pp1)->klass() == NULL) { 258 (*pp1)->set_klass(klass); 259 if (lt.is_enabled()) { 260 ResourceMark rm; 261 lt.print("setting class object in existing constraint for" 262 " name: %s and loader %s", 263 class_name->as_C_string(), 264 ClassLoaderData::class_loader_data(class_loader1())->loader_name_and_id() 265 ); 266 } 267 } else { 268 assert((*pp1)->klass() == klass, "loader constraints corrupted"); 269 } 270 } else if (*pp1 == NULL) { 271 extend_loader_constraint(*pp2, class_loader1, klass); 272 } else if (*pp2 == NULL) { 273 extend_loader_constraint(*pp1, class_loader2, klass); 274 } else { 275 merge_loader_constraints(pp1, pp2, klass); 276 } 277 278 return true; 279 } 280 281 282 // return true if the constraint was updated, false if the constraint is 283 // violated 284 bool LoaderConstraintTable::check_or_update(InstanceKlass* k, 285 Handle loader, 286 Symbol* name) { 287 LogTarget(Info, class, loader, constraints) lt; 288 LoaderConstraintEntry* p = *(find_loader_constraint(name, loader)); 289 if (p && p->klass() != NULL && p->klass() != k) { 290 if (lt.is_enabled()) { 291 ResourceMark rm; 292 lt.print("constraint check failed for name %s, loader %s: " 293 "the presented class object differs from that stored", 294 name->as_C_string(), 295 ClassLoaderData::class_loader_data(loader())->loader_name_and_id()); 296 } 297 return false; 298 } else { 299 if (p && p->klass() == NULL) { 300 p->set_klass(k); 301 if (lt.is_enabled()) { 302 ResourceMark rm; 303 lt.print("updating constraint for name %s, loader %s, " 304 "by setting class object", 305 name->as_C_string(), 306 ClassLoaderData::class_loader_data(loader())->loader_name_and_id()); 307 } 308 } 309 return true; 310 } 311 } 312 313 InstanceKlass* LoaderConstraintTable::find_constrained_klass(Symbol* name, 314 Handle loader) { 315 LoaderConstraintEntry *p = *(find_loader_constraint(name, loader)); 316 if (p != NULL && p->klass() != NULL) { 317 assert(p->klass()->is_instance_klass(), "sanity"); 318 if (!p->klass()->is_loaded()) { 319 // Only return fully loaded classes. Classes found through the 320 // constraints might still be in the process of loading. 321 return NULL; 322 } 323 return p->klass(); 324 } 325 326 // No constraints, or else no klass loaded yet. 327 return NULL; 328 } 329 330 void LoaderConstraintTable::ensure_loader_constraint_capacity( 331 LoaderConstraintEntry *p, 332 int nfree) { 333 if (p->max_loaders() - p->num_loaders() < nfree) { 334 int n = nfree + p->num_loaders(); 335 ClassLoaderData** new_loaders = NEW_C_HEAP_ARRAY(ClassLoaderData*, n, mtClass); 336 memcpy(new_loaders, p->loaders(), sizeof(ClassLoaderData*) * p->num_loaders()); 337 p->set_max_loaders(n); 338 FREE_C_HEAP_ARRAY(ClassLoaderData*, p->loaders()); 339 p->set_loaders(new_loaders); 340 } 341 } 342 343 344 void LoaderConstraintTable::extend_loader_constraint(LoaderConstraintEntry* p, 345 Handle loader, 346 InstanceKlass* klass) { 347 ensure_loader_constraint_capacity(p, 1); 348 int num = p->num_loaders(); 349 p->set_loader(num, loader()); 350 p->set_num_loaders(num + 1); 351 LogTarget(Info, class, loader, constraints) lt; 352 if (lt.is_enabled()) { 353 ResourceMark rm; 354 lt.print("extending constraint for name %s by adding loader[%d]: %s %s", 355 p->name()->as_C_string(), 356 num, 357 ClassLoaderData::class_loader_data(loader())->loader_name_and_id(), 358 (p->klass() == NULL ? " and setting class object" : "") 359 ); 360 } 361 if (p->klass() == NULL) { 362 p->set_klass(klass); 363 } else { 364 assert(klass == NULL || p->klass() == klass, "constraints corrupted"); 365 } 366 } 367 368 369 void LoaderConstraintTable::merge_loader_constraints( 370 LoaderConstraintEntry** pp1, 371 LoaderConstraintEntry** pp2, 372 InstanceKlass* klass) { 373 // make sure *pp1 has higher capacity 374 if ((*pp1)->max_loaders() < (*pp2)->max_loaders()) { 375 LoaderConstraintEntry** tmp = pp2; 376 pp2 = pp1; 377 pp1 = tmp; 378 } 379 380 LoaderConstraintEntry* p1 = *pp1; 381 LoaderConstraintEntry* p2 = *pp2; 382 383 ensure_loader_constraint_capacity(p1, p2->num_loaders()); 384 385 for (int i = 0; i < p2->num_loaders(); i++) { 386 int num = p1->num_loaders(); 387 p1->set_loader_data(num, p2->loader_data(i)); 388 p1->set_num_loaders(num + 1); 389 } 390 391 LogTarget(Info, class, loader, constraints) lt; 392 if (lt.is_enabled()) { 393 ResourceMark rm; 394 lt.print("merged constraints for name %s, new loader list:", 395 p1->name()->as_C_string() 396 ); 397 398 for (int i = 0; i < p1->num_loaders(); i++) { 399 lt.print(" [%d]: %s", i, 400 p1->loader_data(i)->loader_name_and_id()); 401 } 402 if (p1->klass() == NULL) { 403 lt.print("... and setting class object"); 404 } 405 } 406 407 // p1->klass() will hold NULL if klass, p2->klass(), and old 408 // p1->klass() are all NULL. In addition, all three must have 409 // matching non-NULL values, otherwise either the constraints would 410 // have been violated, or the constraints had been corrupted (and an 411 // assertion would fail). 412 if (p2->klass() != NULL) { 413 assert(p2->klass() == klass, "constraints corrupted"); 414 } 415 if (p1->klass() == NULL) { 416 p1->set_klass(klass); 417 } else { 418 assert(p1->klass() == klass, "constraints corrupted"); 419 } 420 421 *pp2 = p2->next(); 422 FREE_C_HEAP_ARRAY(oop, p2->loaders()); 423 free_entry(p2); 424 return; 425 } 426 427 428 void LoaderConstraintTable::verify(PlaceholderTable* placeholders) { 429 Thread *thread = Thread::current(); 430 for (int cindex = 0; cindex < table_size(); cindex++) { 431 for (LoaderConstraintEntry* probe = bucket(cindex); 432 probe != NULL; 433 probe = probe->next()) { 434 if (probe->klass() != NULL) { 435 InstanceKlass* ik = probe->klass(); 436 guarantee(ik->name() == probe->name(), "name should match"); 437 Symbol* name = ik->name(); 438 ClassLoaderData* loader_data = ik->class_loader_data(); 439 Dictionary* dictionary = loader_data->dictionary(); 440 unsigned int d_hash = dictionary->compute_hash(name); 441 int d_index = dictionary->hash_to_index(d_hash); 442 InstanceKlass* k = dictionary->find_class(d_index, d_hash, name); 443 if (k != NULL) { 444 // We found the class in the dictionary, so we should 445 // make sure that the Klass* matches what we already have. 446 guarantee(k == probe->klass(), "klass should be in dictionary"); 447 } else { 448 // If we don't find the class in the dictionary, it 449 // has to be in the placeholders table. 450 unsigned int p_hash = placeholders->compute_hash(name); 451 int p_index = placeholders->hash_to_index(p_hash); 452 PlaceholderEntry* entry = placeholders->get_entry(p_index, p_hash, 453 name, loader_data); 454 455 // The InstanceKlass might not be on the entry, so the only 456 // thing we can check here is whether we were successful in 457 // finding the class in the placeholders table. 458 guarantee(entry != NULL, "klass should be in the placeholders"); 459 } 460 } 461 for (int n = 0; n< probe->num_loaders(); n++) { 462 assert(ClassLoaderDataGraph::contains_loader_data(probe->loader_data(n)), "The loader is missing"); 463 } 464 } 465 } 466 } 467 468 // Called with the system dictionary lock held 469 void LoaderConstraintTable::print_on(outputStream* st) const { 470 ResourceMark rm; 471 assert_locked_or_safepoint(SystemDictionary_lock); 472 st->print_cr("Java loader constraints (table_size=%d, constraints=%d)", 473 table_size(), number_of_entries()); 474 for (int cindex = 0; cindex < table_size(); cindex++) { 475 for (LoaderConstraintEntry* probe = bucket(cindex); 476 probe != NULL; 477 probe = probe->next()) { 478 st->print("%4d: ", cindex); 479 probe->name()->print_on(st); 480 st->print(" , loaders:"); 481 for (int n = 0; n < probe->num_loaders(); n++) { 482 probe->loader_data(n)->print_value_on(st); 483 st->print(", "); 484 } 485 st->cr(); 486 } 487 } 488 }