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