1 /*
   2  * Copyright (c) 2003, 2012, 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 "gc_implementation/shared/markSweep.inline.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "memory/gcLocker.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/constMethodKlass.hpp"
  31 #include "oops/constMethodOop.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/oop.inline2.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 
  36 
  37 klassOop constMethodKlass::create_klass(TRAPS) {
  38   constMethodKlass o;
  39   KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
  40   KlassHandle k = base_create_klass(h_this_klass, header_size(),
  41                                     o.vtbl_value(), CHECK_NULL);
  42   // Make sure size calculation is right
  43   assert(k()->size() == align_object_size(header_size()),
  44          "wrong size for object");
  45   //java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
  46   return k();
  47 }
  48 
  49 
  50 int constMethodKlass::oop_size(oop obj) const {
  51   assert(obj->is_constMethod(), "must be constMethod oop");
  52   return constMethodOop(obj)->object_size();
  53 }
  54 
  55 bool constMethodKlass::oop_is_parsable(oop obj) const {
  56   assert(obj->is_constMethod(), "must be constMethod oop");
  57   return constMethodOop(obj)->object_is_parsable();
  58 }
  59 
  60 bool constMethodKlass::oop_is_conc_safe(oop obj) const {
  61   assert(obj->is_constMethod(), "must be constMethod oop");
  62   return constMethodOop(obj)->is_conc_safe();
  63 }
  64 
  65 constMethodOop constMethodKlass::allocate(int byte_code_size,
  66                                           int compressed_line_number_size,
  67                                           int localvariable_table_length,
  68                                           int exception_table_length,
  69                                           int checked_exceptions_length,
  70                                           bool is_conc_safe,
  71                                           TRAPS) {
  72 
  73   int size = constMethodOopDesc::object_size(byte_code_size,
  74                                              compressed_line_number_size,
  75                                              localvariable_table_length,
  76                                              exception_table_length,
  77                                              checked_exceptions_length);
  78   KlassHandle h_k(THREAD, as_klassOop());
  79   constMethodOop cm = (constMethodOop)
  80     CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL);
  81   assert(!cm->is_parsable(), "Not yet safely parsable");
  82   No_Safepoint_Verifier no_safepoint;
  83   cm->set_interpreter_kind(Interpreter::invalid);
  84   cm->init_fingerprint();
  85   cm->set_method(NULL);
  86   cm->set_stackmap_data(NULL);
  87   cm->set_code_size(byte_code_size);
  88   cm->set_constMethod_size(size);
  89   cm->set_inlined_tables_length(checked_exceptions_length,
  90                                 compressed_line_number_size,
  91                                 localvariable_table_length,
  92                                 exception_table_length);
  93   assert(cm->size() == size, "wrong size for object");
  94   cm->set_is_conc_safe(is_conc_safe);
  95   cm->set_partially_loaded();
  96   assert(cm->is_parsable(), "Is safely parsable by gc");
  97   return cm;
  98 }
  99 
 100 void constMethodKlass::oop_follow_contents(oop obj) {
 101   assert (obj->is_constMethod(), "object must be constMethod");
 102   constMethodOop cm = constMethodOop(obj);
 103   MarkSweep::mark_and_push(cm->adr_method());
 104   MarkSweep::mark_and_push(cm->adr_stackmap_data());
 105   // Performance tweak: We skip iterating over the klass pointer since we
 106   // know that Universe::constMethodKlassObj never moves.
 107 }
 108 
 109 #ifndef SERIALGC
 110 void constMethodKlass::oop_follow_contents(ParCompactionManager* cm,
 111                                            oop obj) {
 112   assert (obj->is_constMethod(), "object must be constMethod");
 113   constMethodOop cm_oop = constMethodOop(obj);
 114   PSParallelCompact::mark_and_push(cm, cm_oop->adr_method());
 115   PSParallelCompact::mark_and_push(cm, cm_oop->adr_stackmap_data());
 116   // Performance tweak: We skip iterating over the klass pointer since we
 117   // know that Universe::constMethodKlassObj never moves.
 118 }
 119 #endif // SERIALGC
 120 
 121 int constMethodKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 122   assert (obj->is_constMethod(), "object must be constMethod");
 123   constMethodOop cm = constMethodOop(obj);
 124   blk->do_oop(cm->adr_method());
 125   blk->do_oop(cm->adr_stackmap_data());
 126   // Get size before changing pointers.
 127   // Don't call size() or oop_size() since that is a virtual call.
 128   int size = cm->object_size();
 129   return size;
 130 }
 131 
 132 
 133 int constMethodKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
 134   assert (obj->is_constMethod(), "object must be constMethod");
 135   constMethodOop cm = constMethodOop(obj);
 136   oop* adr;
 137   adr = cm->adr_method();
 138   if (mr.contains(adr)) blk->do_oop(adr);
 139   adr = cm->adr_stackmap_data();
 140   if (mr.contains(adr)) blk->do_oop(adr);
 141   // Get size before changing pointers.
 142   // Don't call size() or oop_size() since that is a virtual call.
 143   int size = cm->object_size();
 144   // Performance tweak: We skip iterating over the klass pointer since we
 145   // know that Universe::constMethodKlassObj never moves.
 146   return size;
 147 }
 148 
 149 
 150 int constMethodKlass::oop_adjust_pointers(oop obj) {
 151   assert(obj->is_constMethod(), "should be constMethod");
 152   constMethodOop cm = constMethodOop(obj);
 153   MarkSweep::adjust_pointer(cm->adr_method());
 154   MarkSweep::adjust_pointer(cm->adr_stackmap_data());
 155   // Get size before changing pointers.
 156   // Don't call size() or oop_size() since that is a virtual call.
 157   int size = cm->object_size();
 158   // Performance tweak: We skip iterating over the klass pointer since we
 159   // know that Universe::constMethodKlassObj never moves.
 160   return size;
 161 }
 162 
 163 #ifndef SERIALGC
 164 void constMethodKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 165   assert(obj->is_constMethod(), "should be constMethod");
 166 }
 167 
 168 int constMethodKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 169   assert(obj->is_constMethod(), "should be constMethod");
 170   constMethodOop cm_oop = constMethodOop(obj);
 171   oop* const beg_oop = cm_oop->oop_block_beg();
 172   oop* const end_oop = cm_oop->oop_block_end();
 173   for (oop* cur_oop = beg_oop; cur_oop < end_oop; ++cur_oop) {
 174     PSParallelCompact::adjust_pointer(cur_oop);
 175   }
 176   return cm_oop->object_size();
 177 }
 178 #endif // SERIALGC
 179 
 180 // Printing
 181 
 182 void constMethodKlass::oop_print_on(oop obj, outputStream* st) {
 183   ResourceMark rm;
 184   assert(obj->is_constMethod(), "must be constMethod");
 185   Klass::oop_print_on(obj, st);
 186   constMethodOop m = constMethodOop(obj);
 187   st->print(" - method:       " INTPTR_FORMAT " ", (address)m->method());
 188   m->method()->print_value_on(st); st->cr();
 189   if (m->has_stackmap_table()) {
 190     st->print(" - stackmap data:       ");
 191     m->stackmap_data()->print_value_on(st);
 192     st->cr();
 193   }
 194 }
 195 
 196 // Short version of printing constMethodOop - just print the name of the
 197 // method it belongs to.
 198 void constMethodKlass::oop_print_value_on(oop obj, outputStream* st) {
 199   assert(obj->is_constMethod(), "must be constMethod");
 200   constMethodOop m = constMethodOop(obj);
 201   st->print(" const part of method " );
 202   m->method()->print_value_on(st);
 203 }
 204 
 205 const char* constMethodKlass::internal_name() const {
 206   return "{constMethod}";
 207 }
 208 
 209 
 210 // Verification
 211 
 212 void constMethodKlass::oop_verify_on(oop obj, outputStream* st) {
 213   Klass::oop_verify_on(obj, st);
 214   guarantee(obj->is_constMethod(), "object must be constMethod");
 215   constMethodOop m = constMethodOop(obj);
 216   guarantee(m->is_perm(),                            "should be in permspace");
 217 
 218   // Verification can occur during oop construction before the method or
 219   // other fields have been initialized.
 220   if (!obj->partially_loaded()) {
 221     guarantee(m->method()->is_perm(), "should be in permspace");
 222     guarantee(m->method()->is_method(), "should be method");
 223     typeArrayOop stackmap_data = m->stackmap_data();
 224     guarantee(stackmap_data == NULL ||
 225               stackmap_data->is_perm(),  "should be in permspace");
 226 
 227     address m_end = (address)((oop*) m + m->size());
 228     address compressed_table_start = m->code_end();
 229     guarantee(compressed_table_start <= m_end, "invalid method layout");
 230     address compressed_table_end = compressed_table_start;
 231     // Verify line number table
 232     if (m->has_linenumber_table()) {
 233       CompressedLineNumberReadStream stream(m->compressed_linenumber_table());
 234       while (stream.read_pair()) {
 235         guarantee(stream.bci() >= 0 && stream.bci() <= m->code_size(), "invalid bci in line number table");
 236       }
 237       compressed_table_end += stream.position();
 238     }
 239     guarantee(compressed_table_end <= m_end, "invalid method layout");
 240     // Verify checked exceptions, exception table and local variable tables
 241     if (m->has_checked_exceptions()) {
 242       u2* addr = m->checked_exceptions_length_addr();
 243       guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 244     }
 245     if (m->has_exception_handler()) {
 246       u2* addr = m->exception_table_length_addr();
 247       guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 248     }
 249     if (m->has_localvariable_table()) {
 250       u2* addr = m->localvariable_table_length_addr();
 251       guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 252     }
 253     // Check compressed_table_end relative to uncompressed_table_start
 254     u2* uncompressed_table_start;
 255     if (m->has_localvariable_table()) {
 256       uncompressed_table_start = (u2*) m->localvariable_table_start();
 257     } else if (m->has_exception_handler()) {
 258       uncompressed_table_start = (u2*) m->exception_table_start();
 259     } else if (m->has_checked_exceptions()) {
 260         uncompressed_table_start = (u2*) m->checked_exceptions_start();
 261     } else {
 262         uncompressed_table_start = (u2*) m_end;
 263     }
 264     int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end;
 265     int max_gap = align_object_size(1)*BytesPerWord;
 266     guarantee(gap >= 0 && gap < max_gap, "invalid method layout");
 267   }
 268 }
 269 
 270 bool constMethodKlass::oop_partially_loaded(oop obj) const {
 271   assert(obj->is_constMethod(), "object must be klass");
 272   constMethodOop m = constMethodOop(obj);
 273   // check whether stackmap_data points to self (flag for partially loaded)
 274   return m->stackmap_data() == (typeArrayOop)obj;
 275 }
 276 
 277 
 278 // The exception_table is the last field set when loading an object.
 279 void constMethodKlass::oop_set_partially_loaded(oop obj) {
 280   assert(obj->is_constMethod(), "object must be klass");
 281   constMethodOop m = constMethodOop(obj);
 282   // Temporarily set stackmap_data to point to self
 283   m->set_stackmap_data((typeArrayOop)obj);
 284 }