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 "interpreter/interpreter.hpp"
  30 #include "memory/gcLocker.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/constMethodKlass.hpp"
  34 #include "oops/klassOop.hpp"
  35 #include "oops/methodDataOop.hpp"
  36 #include "oops/methodKlass.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "oops/oop.inline2.hpp"
  39 #include "oops/symbolOop.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 
  42 klassOop methodKlass::create_klass(TRAPS) {
  43   methodKlass o;
  44   KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
  45   KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
  46   // Make sure size calculation is right
  47   assert(k()->size() == align_object_size(header_size()), "wrong size for object");
  48   java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
  49   return k();
  50 }
  51 
  52 
  53 int methodKlass::oop_size(oop obj) const {
  54   assert(obj->is_method(), "must be method oop");
  55   return methodOop(obj)->object_size();
  56 }
  57 
  58 
  59 bool methodKlass::oop_is_parsable(oop obj) const {
  60   assert(obj->is_method(), "must be method oop");
  61   return methodOop(obj)->object_is_parsable();
  62 }
  63 
  64 
  65 methodOop methodKlass::allocate(constMethodHandle xconst,
  66                                 AccessFlags access_flags, TRAPS) {
  67   int size = methodOopDesc::object_size(access_flags.is_native());
  68   KlassHandle h_k(THREAD, as_klassOop());
  69   assert(xconst()->is_parsable(), "possible publication protocol violation");
  70   methodOop m = (methodOop)CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL);
  71   assert(!m->is_parsable(), "not expecting parsability yet.");
  72 
  73   No_Safepoint_Verifier no_safepoint;  // until m becomes parsable below
  74   m->set_constMethod(xconst());
  75   m->set_access_flags(access_flags);
  76   m->set_method_size(size);
  77   m->set_name_index(0);
  78   m->set_signature_index(0);
  79 #ifdef CC_INTERP
  80   m->set_result_index(T_VOID);
  81 #endif
  82   m->set_constants(NULL);
  83   m->set_max_stack(0);
  84   m->set_max_locals(0);
  85   m->set_intrinsic_id(vmIntrinsics::_none);
  86   m->set_method_data(NULL);
  87   m->set_interpreter_throwout_count(0);
  88   m->set_vtable_index(methodOopDesc::garbage_vtable_index);
  89 
  90   // Fix and bury in methodOop
  91   m->set_interpreter_entry(NULL); // sets i2i entry and from_int
  92   m->set_adapter_entry(NULL);
  93   m->clear_code(); // from_c/from_i get set to c2i/i2i
  94 
  95   if (access_flags.is_native()) {
  96     m->clear_native_function();
  97     m->set_signature_handler(NULL);
  98   }
  99 
 100   NOT_PRODUCT(m->set_compiled_invocation_count(0);)
 101   m->set_interpreter_invocation_count(0);
 102   m->invocation_counter()->init();
 103   m->backedge_counter()->init();
 104   m->clear_number_of_breakpoints();
 105 
 106   assert(m->is_parsable(), "must be parsable here.");
 107   assert(m->size() == size, "wrong size for object");
 108   // We should not publish an uprasable object's reference
 109   // into one that is parsable, since that presents problems
 110   // for the concurrent parallel marking and precleaning phases
 111   // of concurrent gc (CMS).
 112   xconst->set_method(m);
 113   return m;
 114 }
 115 
 116 
 117 void methodKlass::oop_follow_contents(oop obj) {
 118   assert (obj->is_method(), "object must be method");
 119   methodOop m = methodOop(obj);
 120   // Performance tweak: We skip iterating over the klass pointer since we
 121   // know that Universe::methodKlassObj never moves.
 122   MarkSweep::mark_and_push(m->adr_constMethod());
 123   MarkSweep::mark_and_push(m->adr_constants());
 124   if (m->method_data() != NULL) {
 125     MarkSweep::mark_and_push(m->adr_method_data());
 126   }
 127 }
 128 
 129 #ifndef SERIALGC
 130 void methodKlass::oop_follow_contents(ParCompactionManager* cm,
 131                                       oop obj) {
 132   assert (obj->is_method(), "object must be method");
 133   methodOop m = methodOop(obj);
 134   // Performance tweak: We skip iterating over the klass pointer since we
 135   // know that Universe::methodKlassObj never moves.
 136   PSParallelCompact::mark_and_push(cm, m->adr_constMethod());
 137   PSParallelCompact::mark_and_push(cm, m->adr_constants());
 138 #ifdef COMPILER2
 139   if (m->method_data() != NULL) {
 140     PSParallelCompact::mark_and_push(cm, m->adr_method_data());
 141   }
 142 #endif // COMPILER2
 143 }
 144 #endif // SERIALGC
 145 
 146 int methodKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 147   assert (obj->is_method(), "object must be method");
 148   methodOop m = methodOop(obj);
 149   // Get size before changing pointers.
 150   // Don't call size() or oop_size() since that is a virtual call.
 151   int size = m->object_size();
 152   // Performance tweak: We skip iterating over the klass pointer since we
 153   // know that Universe::methodKlassObj never moves
 154   blk->do_oop(m->adr_constMethod());
 155   blk->do_oop(m->adr_constants());
 156   if (m->method_data() != NULL) {
 157     blk->do_oop(m->adr_method_data());
 158   }
 159   return size;
 160 }
 161 
 162 
 163 int methodKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
 164   assert (obj->is_method(), "object must be method");
 165   methodOop m = methodOop(obj);
 166   // Get size before changing pointers.
 167   // Don't call size() or oop_size() since that is a virtual call.
 168   int size = m->object_size();
 169   // Performance tweak: We skip iterating over the klass pointer since we
 170   // know that Universe::methodKlassObj never moves.
 171   oop* adr;
 172   adr = m->adr_constMethod();
 173   if (mr.contains(adr)) blk->do_oop(adr);
 174   adr = m->adr_constants();
 175   if (mr.contains(adr)) blk->do_oop(adr);
 176   if (m->method_data() != NULL) {
 177     adr = m->adr_method_data();
 178     if (mr.contains(adr)) blk->do_oop(adr);
 179   }
 180   return size;
 181 }
 182 
 183 
 184 int methodKlass::oop_adjust_pointers(oop obj) {
 185   assert(obj->is_method(), "should be method");
 186   methodOop m = methodOop(obj);
 187   // Get size before changing pointers.
 188   // Don't call size() or oop_size() since that is a virtual call.
 189   int size = m->object_size();
 190   // Performance tweak: We skip iterating over the klass pointer since we
 191   // know that Universe::methodKlassObj never moves.
 192   MarkSweep::adjust_pointer(m->adr_constMethod());
 193   MarkSweep::adjust_pointer(m->adr_constants());
 194   if (m->method_data() != NULL) {
 195     MarkSweep::adjust_pointer(m->adr_method_data());
 196   }
 197   return size;
 198 }
 199 
 200 #ifndef SERIALGC
 201 void methodKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 202   assert(obj->is_method(), "should be method");
 203 }
 204 
 205 int methodKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 206   assert(obj->is_method(), "should be method");
 207   methodOop m = methodOop(obj);
 208   PSParallelCompact::adjust_pointer(m->adr_constMethod());
 209   PSParallelCompact::adjust_pointer(m->adr_constants());
 210 #ifdef COMPILER2
 211   if (m->method_data() != NULL) {
 212     PSParallelCompact::adjust_pointer(m->adr_method_data());
 213   }
 214 #endif // COMPILER2
 215   return m->object_size();
 216 }
 217 
 218 int methodKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
 219                                      HeapWord* beg_addr, HeapWord* end_addr) {
 220   assert(obj->is_method(), "should be method");
 221 
 222   oop* p;
 223   methodOop m = methodOop(obj);
 224 
 225   p = m->adr_constMethod();
 226   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 227   p = m->adr_constants();
 228   PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 229 
 230 #ifdef COMPILER2
 231   if (m->method_data() != NULL) {
 232     p = m->adr_method_data();
 233     PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
 234   }
 235 #endif // COMPILER2
 236   return m->object_size();
 237 }
 238 #endif // SERIALGC
 239 
 240 #ifndef PRODUCT
 241 
 242 // Printing
 243 
 244 void methodKlass::oop_print_on(oop obj, outputStream* st) {
 245   ResourceMark rm;
 246   assert(obj->is_method(), "must be method");
 247   Klass::oop_print_on(obj, st);
 248   methodOop m = methodOop(obj);
 249   // get the effect of PrintOopAddress, always, for methods:
 250   st->print_cr(" - this oop:          "INTPTR_FORMAT, (intptr_t)m);
 251   st->print   (" - method holder:     ");    m->method_holder()->print_value_on(st); st->cr();
 252   st->print   (" - constants:         "INTPTR_FORMAT" ", (address)m->constants());
 253   m->constants()->print_value_on(st); st->cr();
 254   st->print   (" - access:            0x%x  ", m->access_flags().as_int()); m->access_flags().print_on(st); st->cr();
 255   st->print   (" - name:              ");    m->name()->print_value_on(st); st->cr();
 256   st->print   (" - signature:         ");    m->signature()->print_value_on(st); st->cr();
 257   st->print_cr(" - max stack:         %d",   m->max_stack());
 258   st->print_cr(" - max locals:        %d",   m->max_locals());
 259   st->print_cr(" - size of params:    %d",   m->size_of_parameters());
 260   st->print_cr(" - method size:       %d",   m->method_size());
 261   if (m->intrinsic_id() != vmIntrinsics::_none)
 262     st->print_cr(" - intrinsic id:      %d %s", m->intrinsic_id(), vmIntrinsics::name_at(m->intrinsic_id()));
 263   if (m->highest_comp_level() != CompLevel_none)
 264     st->print_cr(" - highest level:     %d", m->highest_comp_level());
 265   st->print_cr(" - vtable index:      %d",   m->_vtable_index);
 266   st->print_cr(" - i2i entry:         " INTPTR_FORMAT, m->interpreter_entry());
 267   st->print_cr(" - adapter:           " INTPTR_FORMAT, m->adapter());
 268   st->print_cr(" - compiled entry     " INTPTR_FORMAT, m->from_compiled_entry());
 269   st->print_cr(" - code size:         %d",   m->code_size());
 270   if (m->code_size() != 0) {
 271     st->print_cr(" - code start:        " INTPTR_FORMAT, m->code_base());
 272     st->print_cr(" - code end (excl):   " INTPTR_FORMAT, m->code_base() + m->code_size());
 273   }
 274   if (m->method_data() != NULL) {
 275     st->print_cr(" - method data:       " INTPTR_FORMAT, (address)m->method_data());
 276   }
 277   st->print_cr(" - checked ex length: %d",   m->checked_exceptions_length());
 278   if (m->checked_exceptions_length() > 0) {
 279     CheckedExceptionElement* table = m->checked_exceptions_start();
 280     st->print_cr(" - checked ex start:  " INTPTR_FORMAT, table);
 281     if (Verbose) {
 282       for (int i = 0; i < m->checked_exceptions_length(); i++) {
 283         st->print_cr("   - throws %s", m->constants()->printable_name_at(table[i].class_cp_index));
 284       }
 285     }
 286   }
 287   if (m->has_linenumber_table()) {
 288     u_char* table = m->compressed_linenumber_table();
 289     st->print_cr(" - linenumber start:  " INTPTR_FORMAT, table);
 290     if (Verbose) {
 291       CompressedLineNumberReadStream stream(table);
 292       while (stream.read_pair()) {
 293         st->print_cr("   - line %d: %d", stream.line(), stream.bci());
 294       }
 295     }
 296   }
 297   st->print_cr(" - localvar length:   %d",   m->localvariable_table_length());
 298   if (m->localvariable_table_length() > 0) {
 299     LocalVariableTableElement* table = m->localvariable_table_start();
 300     st->print_cr(" - localvar start:    " INTPTR_FORMAT, table);
 301     if (Verbose) {
 302       for (int i = 0; i < m->localvariable_table_length(); i++) {
 303         int bci = table[i].start_bci;
 304         int len = table[i].length;
 305         const char* name = m->constants()->printable_name_at(table[i].name_cp_index);
 306         const char* desc = m->constants()->printable_name_at(table[i].descriptor_cp_index);
 307         int slot = table[i].slot;
 308         st->print_cr("   - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot);
 309       }
 310     }
 311   }
 312   if (m->code() != NULL) {
 313     st->print   (" - compiled code: ");
 314     m->code()->print_value_on(st);
 315     st->cr();
 316   }
 317   if (m->is_method_handle_invoke()) {
 318     st->print_cr(" - invoke method type: " INTPTR_FORMAT, (address) m->method_handle_type());
 319     // m is classified as native, but it does not have an interesting
 320     // native_function or signature handler
 321   } else if (m->is_native()) {
 322     st->print_cr(" - native function:   " INTPTR_FORMAT, m->native_function());
 323     st->print_cr(" - signature handler: " INTPTR_FORMAT, m->signature_handler());
 324   }
 325 }
 326 
 327 #endif //PRODUCT
 328 
 329 void methodKlass::oop_print_value_on(oop obj, outputStream* st) {
 330   assert(obj->is_method(), "must be method");
 331   Klass::oop_print_value_on(obj, st);
 332   methodOop m = methodOop(obj);
 333   st->print(" ");
 334   m->name()->print_value_on(st);
 335   st->print(" ");
 336   m->signature()->print_value_on(st);
 337   st->print(" in ");
 338   m->method_holder()->print_value_on(st);
 339   if (WizardMode) st->print("[%d,%d]", m->size_of_parameters(), m->max_locals());
 340   if (WizardMode && m->code() != NULL) st->print(" ((nmethod*)%p)", m->code());
 341 }
 342 
 343 const char* methodKlass::internal_name() const {
 344   return "{method}";
 345 }
 346 
 347 
 348 // Verification
 349 
 350 void methodKlass::oop_verify_on(oop obj, outputStream* st) {
 351   Klass::oop_verify_on(obj, st);
 352   guarantee(obj->is_method(), "object must be method");
 353   if (!obj->partially_loaded()) {
 354     methodOop m = methodOop(obj);
 355     guarantee(m->is_perm(),  "should be in permspace");
 356     guarantee(m->name()->is_perm(), "should be in permspace");
 357     guarantee(m->name()->is_symbol(), "should be symbol");
 358     guarantee(m->signature()->is_perm(), "should be in permspace");
 359     guarantee(m->signature()->is_symbol(), "should be symbol");
 360     guarantee(m->constants()->is_perm(), "should be in permspace");
 361     guarantee(m->constants()->is_constantPool(), "should be constant pool");
 362     guarantee(m->constMethod()->is_constMethod(), "should be constMethodOop");
 363     guarantee(m->constMethod()->is_perm(), "should be in permspace");
 364     methodDataOop method_data = m->method_data();
 365     guarantee(method_data == NULL ||
 366               method_data->is_perm(), "should be in permspace");
 367     guarantee(method_data == NULL ||
 368               method_data->is_methodData(), "should be method data");
 369   }
 370 }
 371 
 372 bool methodKlass::oop_partially_loaded(oop obj) const {
 373   assert(obj->is_method(), "object must be method");
 374   methodOop m = methodOop(obj);
 375   constMethodOop xconst = m->constMethod();
 376   assert(xconst != NULL, "const method must be set");
 377   constMethodKlass* ck = constMethodKlass::cast(xconst->klass());
 378   return ck->oop_partially_loaded(xconst);
 379 }
 380 
 381 
 382 void methodKlass::oop_set_partially_loaded(oop obj) {
 383   assert(obj->is_method(), "object must be method");
 384   methodOop m = methodOop(obj);
 385   constMethodOop xconst = m->constMethod();
 386   assert(xconst != NULL, "const method must be set");
 387   constMethodKlass* ck = constMethodKlass::cast(xconst->klass());
 388   ck->oop_set_partially_loaded(xconst);
 389 }