1 /* 2 * Copyright (c) 1997, 2011, 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/javaClasses.hpp" 27 #include "gc_implementation/shared/markSweep.inline.hpp" 28 #include "gc_interface/collectedHeap.inline.hpp" 29 #include "memory/oopFactory.hpp" 30 #include "memory/permGen.hpp" 31 #include "memory/universe.inline.hpp" 32 #include "oops/constantPoolKlass.hpp" 33 #include "oops/constantPoolOop.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "oops/oop.inline2.hpp" 36 #include "oops/symbol.hpp" 37 #include "runtime/handles.inline.hpp" 38 #ifdef TARGET_OS_FAMILY_linux 39 # include "thread_linux.inline.hpp" 40 #endif 41 #ifdef TARGET_OS_FAMILY_solaris 42 # include "thread_solaris.inline.hpp" 43 #endif 44 #ifdef TARGET_OS_FAMILY_windows 45 # include "thread_windows.inline.hpp" 46 #endif 47 #ifdef TARGET_OS_FAMILY_bsd 48 # include "thread_bsd.inline.hpp" 49 #endif 50 #ifndef SERIALGC 51 #include "gc_implementation/parNew/parOopClosures.inline.hpp" 52 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp" 53 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp" 54 #include "memory/cardTableRS.hpp" 55 #include "oops/oop.pcgc.inline.hpp" 56 #endif 57 58 constantPoolOop constantPoolKlass::allocate(int length, bool is_conc_safe, TRAPS) { 59 int size = constantPoolOopDesc::object_size(length); 60 KlassHandle klass (THREAD, as_klassOop()); 61 assert(klass()->is_oop(), "Can't be null, else handlizing of c below won't work"); 62 constantPoolHandle pool; 63 { 64 constantPoolOop c = 65 (constantPoolOop)CollectedHeap::permanent_obj_allocate(klass, size, CHECK_NULL); 66 assert(c->klass_or_null() != NULL, "Handlizing below won't work"); 67 pool = constantPoolHandle(THREAD, c); 68 } 69 70 pool->set_length(length); 71 pool->set_tags(NULL); 72 pool->set_cache(NULL); 73 pool->set_operands(NULL); 74 pool->set_pool_holder(NULL); 75 pool->set_flags(0); 76 // only set to non-zero if constant pool is merged by RedefineClasses 77 pool->set_orig_length(0); 78 // if constant pool may change during RedefineClasses, it is created 79 // unsafe for GC concurrent processing. 80 pool->set_is_conc_safe(is_conc_safe); 81 // all fields are initialized; needed for GC 82 83 // Note: because we may be in this "conc_unsafe" state when allocating 84 // t_oop below, which may in turn cause a GC, it is imperative that our 85 // size be correct, consistent and henceforth stable, at this stage. 86 assert(pool->is_oop() && pool->is_parsable(), "Else size() below is unreliable"); 87 assert(size == pool->size(), "size() is wrong"); 88 89 // initialize tag array 90 typeArrayOop t_oop = oopFactory::new_permanent_byteArray(length, CHECK_NULL); 91 typeArrayHandle tags (THREAD, t_oop); 92 for (int index = 0; index < length; index++) { 93 tags()->byte_at_put(index, JVM_CONSTANT_Invalid); 94 } 95 pool->set_tags(tags()); 96 97 // Check that our size was stable at its old value. 98 assert(size == pool->size(), "size() changed"); 99 return pool(); 100 } 101 102 klassOop constantPoolKlass::create_klass(TRAPS) { 103 constantPoolKlass o; 104 KlassHandle h_this_klass(THREAD, Universe::klassKlassObj()); 105 KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL); 106 // Make sure size calculation is right 107 assert(k()->size() == align_object_size(header_size()), "wrong size for object"); 108 java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror 109 return k(); 110 } 111 112 int constantPoolKlass::oop_size(oop obj) const { 113 assert(obj->is_constantPool(), "must be constantPool"); 114 return constantPoolOop(obj)->object_size(); 115 } 116 117 118 void constantPoolKlass::oop_follow_contents(oop obj) { 119 assert (obj->is_constantPool(), "obj must be constant pool"); 120 constantPoolOop cp = (constantPoolOop) obj; 121 // Performance tweak: We skip iterating over the klass pointer since we 122 // know that Universe::constantPoolKlassObj never moves. 123 124 // If the tags array is null we are in the middle of allocating this constant pool 125 if (cp->tags() != NULL) { 126 // gc of constant pool contents 127 oop* base = (oop*)cp->base(); 128 for (int i = 0; i < cp->length(); i++) { 129 if (cp->is_pointer_entry(i)) { 130 if (*base != NULL) MarkSweep::mark_and_push(base); 131 } 132 base++; 133 } 134 // gc of constant pool instance variables 135 MarkSweep::mark_and_push(cp->tags_addr()); 136 MarkSweep::mark_and_push(cp->cache_addr()); 137 MarkSweep::mark_and_push(cp->operands_addr()); 138 MarkSweep::mark_and_push(cp->pool_holder_addr()); 139 } 140 } 141 142 #ifndef SERIALGC 143 void constantPoolKlass::oop_follow_contents(ParCompactionManager* cm, 144 oop obj) { 145 assert (obj->is_constantPool(), "obj must be constant pool"); 146 constantPoolOop cp = (constantPoolOop) obj; 147 // Performance tweak: We skip iterating over the klass pointer since we 148 // know that Universe::constantPoolKlassObj never moves. 149 150 // If the tags array is null we are in the middle of allocating this constant 151 // pool. 152 if (cp->tags() != NULL) { 153 // gc of constant pool contents 154 oop* base = (oop*)cp->base(); 155 for (int i = 0; i < cp->length(); i++) { 156 if (cp->is_pointer_entry(i)) { 157 if (*base != NULL) PSParallelCompact::mark_and_push(cm, base); 158 } 159 base++; 160 } 161 // gc of constant pool instance variables 162 PSParallelCompact::mark_and_push(cm, cp->tags_addr()); 163 PSParallelCompact::mark_and_push(cm, cp->cache_addr()); 164 PSParallelCompact::mark_and_push(cm, cp->operands_addr()); 165 PSParallelCompact::mark_and_push(cm, cp->pool_holder_addr()); 166 } 167 } 168 #endif // SERIALGC 169 170 171 int constantPoolKlass::oop_adjust_pointers(oop obj) { 172 assert (obj->is_constantPool(), "obj must be constant pool"); 173 constantPoolOop cp = (constantPoolOop) obj; 174 // Get size before changing pointers. 175 // Don't call size() or oop_size() since that is a virtual call. 176 int size = cp->object_size(); 177 // Performance tweak: We skip iterating over the klass pointer since we 178 // know that Universe::constantPoolKlassObj never moves. 179 180 // If the tags array is null we are in the middle of allocating this constant 181 // pool. 182 if (cp->tags() != NULL) { 183 oop* base = (oop*)cp->base(); 184 for (int i = 0; i< cp->length(); i++) { 185 if (cp->is_pointer_entry(i)) { 186 MarkSweep::adjust_pointer(base); 187 } 188 base++; 189 } 190 } 191 MarkSweep::adjust_pointer(cp->tags_addr()); 192 MarkSweep::adjust_pointer(cp->cache_addr()); 193 MarkSweep::adjust_pointer(cp->operands_addr()); 194 MarkSweep::adjust_pointer(cp->pool_holder_addr()); 195 return size; 196 } 197 198 199 int constantPoolKlass::oop_oop_iterate(oop obj, OopClosure* blk) { 200 assert (obj->is_constantPool(), "obj must be constant pool"); 201 // Performance tweak: We skip iterating over the klass pointer since we 202 // know that Universe::constantPoolKlassObj never moves. 203 constantPoolOop cp = (constantPoolOop) obj; 204 // Get size before changing pointers. 205 // Don't call size() or oop_size() since that is a virtual call. 206 int size = cp->object_size(); 207 208 // If the tags array is null we are in the middle of allocating this constant 209 // pool. 210 if (cp->tags() != NULL) { 211 oop* base = (oop*)cp->base(); 212 for (int i = 0; i < cp->length(); i++) { 213 if (cp->is_pointer_entry(i)) { 214 blk->do_oop(base); 215 } 216 base++; 217 } 218 } 219 blk->do_oop(cp->tags_addr()); 220 blk->do_oop(cp->cache_addr()); 221 blk->do_oop(cp->operands_addr()); 222 blk->do_oop(cp->pool_holder_addr()); 223 return size; 224 } 225 226 227 int constantPoolKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) { 228 assert (obj->is_constantPool(), "obj must be constant pool"); 229 // Performance tweak: We skip iterating over the klass pointer since we 230 // know that Universe::constantPoolKlassObj never moves. 231 constantPoolOop cp = (constantPoolOop) obj; 232 // Get size before changing pointers. 233 // Don't call size() or oop_size() since that is a virtual call. 234 int size = cp->object_size(); 235 236 // If the tags array is null we are in the middle of allocating this constant 237 // pool. 238 if (cp->tags() != NULL) { 239 oop* base = (oop*)cp->base(); 240 for (int i = 0; i < cp->length(); i++) { 241 if (mr.contains(base)) { 242 if (cp->is_pointer_entry(i)) { 243 blk->do_oop(base); 244 } 245 } 246 base++; 247 } 248 } 249 oop* addr; 250 addr = cp->tags_addr(); 251 if (mr.contains(addr)) blk->do_oop(addr); 252 addr = cp->cache_addr(); 253 if (mr.contains(addr)) blk->do_oop(addr); 254 addr = cp->operands_addr(); 255 if (mr.contains(addr)) blk->do_oop(addr); 256 addr = cp->pool_holder_addr(); 257 if (mr.contains(addr)) blk->do_oop(addr); 258 return size; 259 } 260 261 bool constantPoolKlass::oop_is_conc_safe(oop obj) const { 262 assert(obj->is_constantPool(), "must be constantPool"); 263 return constantPoolOop(obj)->is_conc_safe(); 264 } 265 266 #ifndef SERIALGC 267 int constantPoolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { 268 assert (obj->is_constantPool(), "obj must be constant pool"); 269 constantPoolOop cp = (constantPoolOop) obj; 270 271 // If the tags array is null we are in the middle of allocating this constant 272 // pool. 273 if (cp->tags() != NULL) { 274 oop* base = (oop*)cp->base(); 275 for (int i = 0; i < cp->length(); ++i, ++base) { 276 if (cp->is_pointer_entry(i)) { 277 PSParallelCompact::adjust_pointer(base); 278 } 279 } 280 } 281 PSParallelCompact::adjust_pointer(cp->tags_addr()); 282 PSParallelCompact::adjust_pointer(cp->cache_addr()); 283 PSParallelCompact::adjust_pointer(cp->operands_addr()); 284 PSParallelCompact::adjust_pointer(cp->pool_holder_addr()); 285 return cp->object_size(); 286 } 287 288 void constantPoolKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { 289 assert(obj->is_constantPool(), "should be constant pool"); 290 constantPoolOop cp = (constantPoolOop) obj; 291 if (cp->tags() != NULL) { 292 for (int i = 1; i < cp->length(); ++i) { 293 if (cp->is_pointer_entry(i)) { 294 oop* base = cp->obj_at_addr_raw(i); 295 if (PSScavenge::should_scavenge(base)) { 296 pm->claim_or_forward_depth(base); 297 } 298 } 299 } 300 } 301 } 302 #endif // SERIALGC 303 304 // Printing 305 306 void constantPoolKlass::oop_print_on(oop obj, outputStream* st) { 307 EXCEPTION_MARK; 308 oop anObj; 309 assert(obj->is_constantPool(), "must be constantPool"); 310 Klass::oop_print_on(obj, st); 311 constantPoolOop cp = constantPoolOop(obj); 312 if (cp->flags() != 0) { 313 st->print(" - flags: 0x%x", cp->flags()); 314 if (cp->has_pseudo_string()) st->print(" has_pseudo_string"); 315 if (cp->has_invokedynamic()) st->print(" has_invokedynamic"); 316 if (cp->has_preresolution()) st->print(" has_preresolution"); 317 st->cr(); 318 } 319 if (cp->pool_holder() != NULL) { 320 bool extra = (instanceKlass::cast(cp->pool_holder())->constants() != cp); 321 st->print_cr(" - holder: " INTPTR_FORMAT "%s", cp->pool_holder(), (extra? " (extra)" : "")); 322 } 323 st->print_cr(" - cache: " INTPTR_FORMAT, cp->cache()); 324 for (int index = 1; index < cp->length(); index++) { // Index 0 is unused 325 st->print(" - %3d : ", index); 326 cp->tag_at(index).print_on(st); 327 st->print(" : "); 328 switch (cp->tag_at(index).value()) { 329 case JVM_CONSTANT_Class : 330 { anObj = cp->klass_at(index, CATCH); 331 anObj->print_value_on(st); 332 st->print(" {0x%lx}", (address)anObj); 333 } 334 break; 335 case JVM_CONSTANT_Fieldref : 336 case JVM_CONSTANT_Methodref : 337 case JVM_CONSTANT_InterfaceMethodref : 338 st->print("klass_index=%d", cp->uncached_klass_ref_index_at(index)); 339 st->print(" name_and_type_index=%d", cp->uncached_name_and_type_ref_index_at(index)); 340 break; 341 case JVM_CONSTANT_UnresolvedString : 342 case JVM_CONSTANT_String : 343 if (cp->is_pseudo_string_at(index)) { 344 anObj = cp->pseudo_string_at(index); 345 } else { 346 anObj = cp->string_at(index, CATCH); 347 } 348 anObj->print_value_on(st); 349 st->print(" {0x%lx}", (address)anObj); 350 break; 351 case JVM_CONSTANT_Object : 352 anObj = cp->object_at(index); 353 anObj->print_value_on(st); 354 st->print(" {0x%lx}", (address)anObj); 355 break; 356 case JVM_CONSTANT_Integer : 357 st->print("%d", cp->int_at(index)); 358 break; 359 case JVM_CONSTANT_Float : 360 st->print("%f", cp->float_at(index)); 361 break; 362 case JVM_CONSTANT_Long : 363 st->print_jlong(cp->long_at(index)); 364 index++; // Skip entry following eigth-byte constant 365 break; 366 case JVM_CONSTANT_Double : 367 st->print("%lf", cp->double_at(index)); 368 index++; // Skip entry following eigth-byte constant 369 break; 370 case JVM_CONSTANT_NameAndType : 371 st->print("name_index=%d", cp->name_ref_index_at(index)); 372 st->print(" signature_index=%d", cp->signature_ref_index_at(index)); 373 break; 374 case JVM_CONSTANT_Utf8 : 375 cp->symbol_at(index)->print_value_on(st); 376 break; 377 case JVM_CONSTANT_UnresolvedClass : // fall-through 378 case JVM_CONSTANT_UnresolvedClassInError: { 379 // unresolved_klass_at requires lock or safe world. 380 CPSlot entry = cp->slot_at(index); 381 if (entry.is_oop()) { 382 entry.get_oop()->print_value_on(st); 383 } else { 384 entry.get_symbol()->print_value_on(st); 385 } 386 } 387 break; 388 case JVM_CONSTANT_MethodHandle : 389 st->print("ref_kind=%d", cp->method_handle_ref_kind_at(index)); 390 st->print(" ref_index=%d", cp->method_handle_index_at(index)); 391 break; 392 case JVM_CONSTANT_MethodType : 393 st->print("signature_index=%d", cp->method_type_index_at(index)); 394 break; 395 case JVM_CONSTANT_InvokeDynamic : 396 { 397 st->print("bootstrap_method_index=%d", cp->invoke_dynamic_bootstrap_method_ref_index_at(index)); 398 st->print(" name_and_type_index=%d", cp->invoke_dynamic_name_and_type_ref_index_at(index)); 399 int argc = cp->invoke_dynamic_argument_count_at(index); 400 if (argc > 0) { 401 for (int arg_i = 0; arg_i < argc; arg_i++) { 402 int arg = cp->invoke_dynamic_argument_index_at(index, arg_i); 403 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 404 } 405 st->print("}"); 406 } 407 } 408 break; 409 default: 410 ShouldNotReachHere(); 411 break; 412 } 413 st->cr(); 414 } 415 st->cr(); 416 } 417 418 void constantPoolKlass::oop_print_value_on(oop obj, outputStream* st) { 419 assert(obj->is_constantPool(), "must be constantPool"); 420 constantPoolOop cp = constantPoolOop(obj); 421 st->print("constant pool [%d]", cp->length()); 422 if (cp->has_pseudo_string()) st->print("/pseudo_string"); 423 if (cp->has_invokedynamic()) st->print("/invokedynamic"); 424 if (cp->has_preresolution()) st->print("/preresolution"); 425 if (cp->operands() != NULL) st->print("/operands[%d]", cp->operands()->length()); 426 cp->print_address_on(st); 427 st->print(" for "); 428 cp->pool_holder()->print_value_on(st); 429 if (cp->pool_holder() != NULL) { 430 bool extra = (instanceKlass::cast(cp->pool_holder())->constants() != cp); 431 if (extra) st->print(" (extra)"); 432 } 433 if (cp->cache() != NULL) { 434 st->print(" cache=" PTR_FORMAT, cp->cache()); 435 } 436 } 437 438 const char* constantPoolKlass::internal_name() const { 439 return "{constant pool}"; 440 } 441 442 // Verification 443 444 void constantPoolKlass::oop_verify_on(oop obj, outputStream* st) { 445 Klass::oop_verify_on(obj, st); 446 guarantee(obj->is_constantPool(), "object must be constant pool"); 447 constantPoolOop cp = constantPoolOop(obj); 448 guarantee(cp->is_perm(), "should be in permspace"); 449 if (!cp->partially_loaded()) { 450 for (int i = 0; i< cp->length(); i++) { 451 constantTag tag = cp->tag_at(i); 452 CPSlot entry = cp->slot_at(i); 453 if (tag.is_klass()) { 454 if (entry.is_oop()) { 455 guarantee(entry.get_oop()->is_perm(), "should be in permspace"); 456 guarantee(entry.get_oop()->is_klass(), "should be klass"); 457 } 458 } else if (tag.is_unresolved_klass()) { 459 if (entry.is_oop()) { 460 guarantee(entry.get_oop()->is_perm(), "should be in permspace"); 461 guarantee(entry.get_oop()->is_klass(), "should be klass"); 462 } 463 } else if (tag.is_symbol()) { 464 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 465 } else if (tag.is_unresolved_string()) { 466 if (entry.is_oop()) { 467 guarantee(entry.get_oop()->is_perm(), "should be in permspace"); 468 guarantee(entry.get_oop()->is_instance(), "should be instance"); 469 } 470 else { 471 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 472 } 473 } else if (tag.is_string()) { 474 if (!cp->has_pseudo_string()) { 475 if (entry.is_oop()) { 476 guarantee(!JavaObjectsInPerm || entry.get_oop()->is_perm(), 477 "should be in permspace"); 478 guarantee(entry.get_oop()->is_instance(), "should be instance"); 479 } 480 } else { 481 // can be non-perm, can be non-instance (array) 482 } 483 } else if (tag.is_object()) { 484 assert(entry.get_oop()->is_oop(), "should be some valid oop"); 485 } else { 486 assert(!cp->is_pointer_entry(i), "unhandled oop type in constantPoolKlass::verify_on"); 487 } 488 } 489 guarantee(cp->tags()->is_perm(), "should be in permspace"); 490 guarantee(cp->tags()->is_typeArray(), "should be type array"); 491 if (cp->cache() != NULL) { 492 // Note: cache() can be NULL before a class is completely setup or 493 // in temporary constant pools used during constant pool merging 494 guarantee(cp->cache()->is_perm(), "should be in permspace"); 495 guarantee(cp->cache()->is_constantPoolCache(), "should be constant pool cache"); 496 } 497 if (cp->operands() != NULL) { 498 guarantee(cp->operands()->is_perm(), "should be in permspace"); 499 guarantee(cp->operands()->is_typeArray(), "should be type array"); 500 } 501 if (cp->pool_holder() != NULL) { 502 // Note: pool_holder() can be NULL in temporary constant pools 503 // used during constant pool merging 504 guarantee(cp->pool_holder()->is_perm(), "should be in permspace"); 505 guarantee(cp->pool_holder()->is_klass(), "should be klass"); 506 } 507 } 508 } 509 510 bool constantPoolKlass::oop_partially_loaded(oop obj) const { 511 assert(obj->is_constantPool(), "object must be constant pool"); 512 constantPoolOop cp = constantPoolOop(obj); 513 return cp->tags() == NULL || cp->pool_holder() == (klassOop) cp; // Check whether pool holder points to self 514 } 515 516 517 void constantPoolKlass::oop_set_partially_loaded(oop obj) { 518 assert(obj->is_constantPool(), "object must be constant pool"); 519 constantPoolOop cp = constantPoolOop(obj); 520 assert(cp->pool_holder() == NULL, "just checking"); 521 cp->set_pool_holder((klassOop) cp); // Temporarily set pool holder to point to self 522 } 523 524 #ifndef PRODUCT 525 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool 526 void constantPoolKlass::preload_and_initialize_all_classes(oop obj, TRAPS) { 527 guarantee(obj->is_constantPool(), "object must be constant pool"); 528 constantPoolHandle cp(THREAD, (constantPoolOop)obj); 529 guarantee(!cp->partially_loaded(), "must be fully loaded"); 530 531 for (int i = 0; i< cp->length(); i++) { 532 if (cp->tag_at(i).is_unresolved_klass()) { 533 // This will force loading of the class 534 klassOop klass = cp->klass_at(i, CHECK); 535 if (klass->is_instance()) { 536 // Force initialization of class 537 instanceKlass::cast(klass)->initialize(CHECK); 538 } 539 } 540 } 541 } 542 543 #endif