1 /*
   2  * Copyright (c) 2003, 2014, 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 "interpreter/interpreter.hpp"
  27 #include "memory/gcLocker.hpp"
  28 #include "memory/heapInspection.hpp"
  29 #include "memory/metadataFactory.hpp"
  30 #include "oops/constMethod.hpp"
  31 #include "oops/method.hpp"
  32 
  33 // Static initialization
  34 const u2 ConstMethod::MAX_IDNUM   = 0xFFFE;
  35 const u2 ConstMethod::UNSET_IDNUM = 0xFFFF;
  36 
  37 ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data,
  38                                    int byte_code_size,
  39                                    InlineTableSizes* sizes,
  40                                    MethodType method_type,
  41                                    TRAPS) {
  42   int size = ConstMethod::size(byte_code_size, sizes);
  43   return new (loader_data, size, true, MetaspaceObj::ConstMethodType, THREAD) ConstMethod(
  44       byte_code_size, sizes, method_type, size);
  45 }
  46 
  47 ConstMethod::ConstMethod(int byte_code_size,
  48                          InlineTableSizes* sizes,
  49                          MethodType method_type,
  50                          int size) {
  51 
  52   No_Safepoint_Verifier no_safepoint;
  53   init_fingerprint();
  54   set_constants(NULL);
  55   set_stackmap_data(NULL);
  56   set_code_size(byte_code_size);
  57   set_constMethod_size(size);
  58   set_inlined_tables_length(sizes); // sets _flags
  59   set_method_type(method_type);
  60   assert(this->size() == size, "wrong size for object");
  61   set_name_index(0);
  62   set_signature_index(0);
  63   set_constants(NULL);
  64   set_max_stack(0);
  65   set_max_locals(0);
  66   set_method_idnum(0);
  67   set_size_of_parameters(0);
  68 }
  69 
  70 // Accessor that copies to metadata.
  71 void ConstMethod::copy_stackmap_data(ClassLoaderData* loader_data,
  72                                      u1* sd, int length, TRAPS) {
  73   _stackmap_data = MetadataFactory::new_array<u1>(loader_data, length, CHECK);
  74   memcpy((void*)_stackmap_data->adr_at(0), (void*)sd, length);
  75 }
  76 
  77 // Deallocate metadata fields associated with ConstMethod*
  78 void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) {
  79   if (stackmap_data() != NULL) {
  80     MetadataFactory::free_array<u1>(loader_data, stackmap_data());
  81   }
  82   set_stackmap_data(NULL);
  83 
  84   // deallocate annotation arrays
  85   if (has_method_annotations())
  86     MetadataFactory::free_array<u1>(loader_data, method_annotations());
  87   if (has_parameter_annotations())
  88     MetadataFactory::free_array<u1>(loader_data, parameter_annotations());
  89   if (has_type_annotations())
  90     MetadataFactory::free_array<u1>(loader_data, type_annotations());
  91   if (has_default_annotations())
  92     MetadataFactory::free_array<u1>(loader_data, default_annotations());
  93 }
  94 
  95 // How big must this constMethodObject be?
  96 
  97 int ConstMethod::size(int code_size,
  98                       InlineTableSizes* sizes) {
  99   int extra_bytes = code_size;
 100   if (sizes->compressed_linenumber_size() > 0) {
 101     extra_bytes += sizes->compressed_linenumber_size();
 102   }
 103   if (sizes->checked_exceptions_length() > 0) {
 104     extra_bytes += sizeof(u2);
 105     extra_bytes += sizes->checked_exceptions_length() * sizeof(CheckedExceptionElement);
 106   }
 107   if (sizes->localvariable_table_length() > 0) {
 108     extra_bytes += sizeof(u2);
 109     extra_bytes +=
 110               sizes->localvariable_table_length() * sizeof(LocalVariableTableElement);
 111   }
 112   if (sizes->exception_table_length() > 0) {
 113     extra_bytes += sizeof(u2);
 114     extra_bytes += sizes->exception_table_length() * sizeof(ExceptionTableElement);
 115   }
 116   if (sizes->generic_signature_index() != 0) {
 117     extra_bytes += sizeof(u2);
 118   }
 119   if (sizes->method_parameters_length() >= 0) {
 120     extra_bytes += sizeof(u2);
 121     extra_bytes += sizes->method_parameters_length() * sizeof(MethodParametersElement);
 122   }
 123 
 124   // Align sizes up to a word.
 125   extra_bytes = align_size_up(extra_bytes, BytesPerWord);
 126 
 127   // One pointer per annotation array
 128   if (sizes->method_annotations_length() > 0) {
 129     extra_bytes += sizeof(AnnotationArray*);
 130   }
 131   if (sizes->parameter_annotations_length() > 0) {
 132     extra_bytes += sizeof(AnnotationArray*);
 133   }
 134   if (sizes->type_annotations_length() > 0) {
 135     extra_bytes += sizeof(AnnotationArray*);
 136   }
 137   if (sizes->default_annotations_length() > 0) {
 138     extra_bytes += sizeof(AnnotationArray*);
 139   }
 140 
 141   int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
 142   assert(extra_words == extra_bytes/BytesPerWord, "should already be aligned");
 143   return align_object_size(header_size() + extra_words);
 144 }
 145 
 146 Method* ConstMethod::method() const {
 147     return _constants->pool_holder()->method_with_idnum(_method_idnum);
 148   }
 149 
 150 // linenumber table - note that length is unknown until decompression,
 151 // see class CompressedLineNumberReadStream.
 152 
 153 u_char* ConstMethod::compressed_linenumber_table() const {
 154   // Located immediately following the bytecodes.
 155   assert(has_linenumber_table(), "called only if table is present");
 156   return code_end();
 157 }
 158 
 159 // Last short in ConstMethod* before annotations
 160 u2* ConstMethod::last_u2_element() const {
 161   int offset = 0;
 162   if (has_method_annotations()) offset++;
 163   if (has_parameter_annotations()) offset++;
 164   if (has_type_annotations()) offset++;
 165   if (has_default_annotations()) offset++;
 166   return (u2*)((AnnotationArray**)constMethod_end() - offset) - 1;
 167 }
 168 
 169 u2* ConstMethod::generic_signature_index_addr() const {
 170   // Located at the end of the constMethod.
 171   assert(has_generic_signature(), "called only if generic signature exists");
 172   return last_u2_element();
 173 }
 174 
 175 u2* ConstMethod::method_parameters_length_addr() const {
 176   assert(has_method_parameters(), "called only if table is present");
 177   return has_generic_signature() ? (last_u2_element() - 1) :
 178                                     last_u2_element();
 179 }
 180 
 181 u2* ConstMethod::checked_exceptions_length_addr() const {
 182   // Located immediately before the generic signature index.
 183   assert(has_checked_exceptions(), "called only if table is present");
 184   if(has_method_parameters()) {
 185     // If method parameters present, locate immediately before them.
 186     return (u2*)method_parameters_start() - 1;
 187   } else {
 188     // Else, the exception table is at the end of the constMethod.
 189     return has_generic_signature() ? (last_u2_element() - 1) :
 190                                      last_u2_element();
 191   }
 192 }
 193 
 194 u2* ConstMethod::exception_table_length_addr() const {
 195   assert(has_exception_handler(), "called only if table is present");
 196   if (has_checked_exceptions()) {
 197     // If checked_exception present, locate immediately before them.
 198     return (u2*) checked_exceptions_start() - 1;
 199   } else {
 200     if(has_method_parameters()) {
 201       // If method parameters present, locate immediately before them.
 202       return (u2*)method_parameters_start() - 1;
 203     } else {
 204       // Else, the exception table is at the end of the constMethod.
 205       return has_generic_signature() ? (last_u2_element() - 1) :
 206                                         last_u2_element();
 207     }
 208   }
 209 }
 210 
 211 u2* ConstMethod::localvariable_table_length_addr() const {
 212   assert(has_localvariable_table(), "called only if table is present");
 213   if (has_exception_handler()) {
 214     // If exception_table present, locate immediately before them.
 215     return (u2*) exception_table_start() - 1;
 216   } else {
 217     if (has_checked_exceptions()) {
 218       // If checked_exception present, locate immediately before them.
 219       return (u2*) checked_exceptions_start() - 1;
 220     } else {
 221       if(has_method_parameters()) {
 222         // If method parameters present, locate immediately before them.
 223         return (u2*)method_parameters_start() - 1;
 224       } else {
 225         // Else, the exception table is at the end of the constMethod.
 226       return has_generic_signature() ? (last_u2_element() - 1) :
 227                                         last_u2_element();
 228       }
 229     }
 230   }
 231 }
 232 
 233 // Update the flags to indicate the presence of these optional fields.
 234 void ConstMethod::set_inlined_tables_length(InlineTableSizes* sizes) {
 235   _flags = 0;
 236   if (sizes->compressed_linenumber_size() > 0)
 237     _flags |= _has_linenumber_table;
 238   if (sizes->generic_signature_index() != 0)
 239     _flags |= _has_generic_signature;
 240   if (sizes->method_parameters_length() >= 0)
 241     _flags |= _has_method_parameters;
 242   if (sizes->checked_exceptions_length() > 0)
 243     _flags |= _has_checked_exceptions;
 244   if (sizes->exception_table_length() > 0)
 245     _flags |= _has_exception_table;
 246   if (sizes->localvariable_table_length() > 0)
 247     _flags |= _has_localvariable_table;
 248 
 249   // annotations, they are all pointer sized embedded objects so don't have
 250   // a length embedded also.
 251   if (sizes->method_annotations_length() > 0)
 252     _flags |= _has_method_annotations;
 253   if (sizes->parameter_annotations_length() > 0)
 254     _flags |= _has_parameter_annotations;
 255   if (sizes->type_annotations_length() > 0)
 256     _flags |= _has_type_annotations;
 257   if (sizes->default_annotations_length() > 0)
 258     _flags |= _has_default_annotations;
 259 
 260   // This code is extremely brittle and should possibly be revised.
 261   // The *_length_addr functions walk backwards through the
 262   // constMethod data, using each of the length indexes ahead of them,
 263   // as well as the flags variable.  Therefore, the indexes must be
 264   // initialized in reverse order, or else they will compute the wrong
 265   // offsets.  Moving the initialization of _flags into a separate
 266   // block solves *half* of the problem, but the following part will
 267   // still break if the order is not exactly right.
 268   //
 269   // Also, the servicability agent needs to be informed anytime
 270   // anything is added here.  It might be advisable to have some sort
 271   // of indication of this inline.
 272   if (sizes->generic_signature_index() != 0)
 273     *(generic_signature_index_addr()) = sizes->generic_signature_index();
 274   // New data should probably go here.
 275   if (sizes->method_parameters_length() >= 0)
 276     *(method_parameters_length_addr()) = sizes->method_parameters_length();
 277   if (sizes->checked_exceptions_length() > 0)
 278     *(checked_exceptions_length_addr()) = sizes->checked_exceptions_length();
 279   if (sizes->exception_table_length() > 0)
 280     *(exception_table_length_addr()) = sizes->exception_table_length();
 281   if (sizes->localvariable_table_length() > 0)
 282     *(localvariable_table_length_addr()) = sizes->localvariable_table_length();
 283 }
 284 
 285 int ConstMethod::method_parameters_length() const {
 286   return has_method_parameters() ? *(method_parameters_length_addr()) : -1;
 287 }
 288 
 289 MethodParametersElement* ConstMethod::method_parameters_start() const {
 290   u2* addr = method_parameters_length_addr();
 291   u2 length = *addr;
 292   assert(length > 0, "should only be called if table is present");
 293   addr -= length * sizeof(MethodParametersElement) / sizeof(u2);
 294   return (MethodParametersElement*) addr;
 295 }
 296 
 297 
 298 int ConstMethod::checked_exceptions_length() const {
 299   return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0;
 300 }
 301 
 302 
 303 CheckedExceptionElement* ConstMethod::checked_exceptions_start() const {
 304   u2* addr = checked_exceptions_length_addr();
 305   u2 length = *addr;
 306   assert(length > 0, "should only be called if table is present");
 307   addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2);
 308   return (CheckedExceptionElement*) addr;
 309 }
 310 
 311 
 312 int ConstMethod::localvariable_table_length() const {
 313   return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0;
 314 }
 315 
 316 
 317 LocalVariableTableElement* ConstMethod::localvariable_table_start() const {
 318   u2* addr = localvariable_table_length_addr();
 319   u2 length = *addr;
 320   assert(length > 0, "should only be called if table is present");
 321   addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2);
 322   return (LocalVariableTableElement*) addr;
 323 }
 324 
 325 int ConstMethod::exception_table_length() const {
 326   return has_exception_handler() ? *(exception_table_length_addr()) : 0;
 327 }
 328 
 329 ExceptionTableElement* ConstMethod::exception_table_start() const {
 330   u2* addr = exception_table_length_addr();
 331   u2 length = *addr;
 332   assert(length > 0, "should only be called if table is present");
 333   addr -= length * sizeof(ExceptionTableElement) / sizeof(u2);
 334   return (ExceptionTableElement*)addr;
 335 }
 336 
 337 AnnotationArray** ConstMethod::method_annotations_addr() const {
 338   assert(has_method_annotations(), "should only be called if method annotations are present");
 339   return (AnnotationArray**)constMethod_end() - 1;
 340 }
 341 
 342 AnnotationArray** ConstMethod::parameter_annotations_addr() const {
 343   assert(has_parameter_annotations(), "should only be called if method parameter annotations are present");
 344   int offset = 1;
 345   if (has_method_annotations()) offset++;
 346   return (AnnotationArray**)constMethod_end() - offset;
 347 }
 348 
 349 AnnotationArray** ConstMethod::type_annotations_addr() const {
 350   assert(has_type_annotations(), "should only be called if method type annotations are present");
 351   int offset = 1;
 352   if (has_method_annotations()) offset++;
 353   if (has_parameter_annotations()) offset++;
 354   return (AnnotationArray**)constMethod_end() - offset;
 355 }
 356 
 357 AnnotationArray** ConstMethod::default_annotations_addr() const {
 358   assert(has_default_annotations(), "should only be called if method default annotations are present");
 359   int offset = 1;
 360   if (has_method_annotations()) offset++;
 361   if (has_parameter_annotations()) offset++;
 362   if (has_type_annotations()) offset++;
 363   return (AnnotationArray**)constMethod_end() - offset;
 364 }
 365 
 366 // copy annotations from 'cm' to 'this'
 367 void ConstMethod::copy_annotations_from(ConstMethod* cm) {
 368   if (cm->has_method_annotations()) {
 369     assert(has_method_annotations(), "should be allocated already");
 370     set_method_annotations(cm->method_annotations());
 371   }
 372   if (cm->has_parameter_annotations()) {
 373     assert(has_parameter_annotations(), "should be allocated already");
 374     set_parameter_annotations(cm->parameter_annotations());
 375   }
 376   if (cm->has_type_annotations()) {
 377     assert(has_type_annotations(), "should be allocated already");
 378     set_type_annotations(cm->type_annotations());
 379   }
 380   if (cm->has_default_annotations()) {
 381     assert(has_default_annotations(), "should be allocated already");
 382     set_default_annotations(cm->default_annotations());
 383   }
 384 }
 385 
 386 // Printing
 387 
 388 void ConstMethod::print_on(outputStream* st) const {
 389   ResourceMark rm;
 390   assert(is_constMethod(), "must be constMethod");
 391   st->print_cr("%s", internal_name());
 392   st->print(" - method:       " INTPTR_FORMAT " ", p2i((address)method()));
 393   method()->print_value_on(st); st->cr();
 394   if (has_stackmap_table()) {
 395     st->print(" - stackmap data:       ");
 396     stackmap_data()->print_value_on(st);
 397     st->cr();
 398   }
 399 }
 400 
 401 // Short version of printing ConstMethod* - just print the name of the
 402 // method it belongs to.
 403 void ConstMethod::print_value_on(outputStream* st) const {
 404   assert(is_constMethod(), "must be constMethod");
 405   st->print(" const part of method " );
 406   method()->print_value_on(st);
 407 }
 408 
 409 #if INCLUDE_SERVICES
 410 // Size Statistics
 411 void ConstMethod::collect_statistics(KlassSizeStats *sz) const {
 412   int n1, n2, n3;
 413   sz->_const_method_bytes += (n1 = sz->count(this));
 414   sz->_bytecode_bytes     += (n2 = code_size());
 415   sz->_stackmap_bytes     += (n3 = sz->count_array(stackmap_data()));
 416 
 417   // Count method annotations
 418   int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
 419   if (has_method_annotations()) {
 420     sz->_methods_annotations_bytes += (a1 = sz->count_array(method_annotations()));
 421   }
 422   if (has_parameter_annotations()) {
 423     sz->_methods_parameter_annotations_bytes += (a2 = sz->count_array(parameter_annotations()));
 424   }
 425   if (has_type_annotations()) {
 426     sz->_methods_type_annotations_bytes += (a3 = sz->count_array(type_annotations()));
 427   }
 428   if (has_default_annotations()) {
 429     sz->_methods_default_annotations_bytes += (a4 = sz->count_array(default_annotations()));
 430   }
 431 
 432   int size_annotations = a1 + a2 + a3 + a4;
 433 
 434   sz->_method_all_bytes += n1 + n3 + size_annotations; // note: n2 is part of n3
 435   sz->_ro_bytes += n1 + n3 + size_annotations;
 436 }
 437 #endif // INCLUDE_SERVICES
 438 
 439 // Verification
 440 
 441 void ConstMethod::verify_on(outputStream* st) {
 442   guarantee(is_constMethod(), "object must be constMethod");
 443 
 444   // Verification can occur during oop construction before the method or
 445   // other fields have been initialized.
 446   guarantee(method()->is_method(), "should be method");
 447 
 448   address m_end = (address)((intptr_t) this + size());
 449   address compressed_table_start = code_end();
 450   guarantee(compressed_table_start <= m_end, "invalid method layout");
 451   address compressed_table_end = compressed_table_start;
 452   // Verify line number table
 453   if (has_linenumber_table()) {
 454     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 455     while (stream.read_pair()) {
 456       guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table");
 457     }
 458     compressed_table_end += stream.position();
 459   }
 460   guarantee(compressed_table_end <= m_end, "invalid method layout");
 461   // Verify checked exceptions, exception table and local variable tables
 462   if (has_method_parameters()) {
 463     u2* addr = method_parameters_length_addr();
 464     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 465   }
 466   if (has_checked_exceptions()) {
 467     u2* addr = checked_exceptions_length_addr();
 468     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 469   }
 470   if (has_exception_handler()) {
 471     u2* addr = exception_table_length_addr();
 472      guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 473   }
 474   if (has_localvariable_table()) {
 475     u2* addr = localvariable_table_length_addr();
 476     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
 477   }
 478   // Check compressed_table_end relative to uncompressed_table_start
 479   u2* uncompressed_table_start;
 480   if (has_localvariable_table()) {
 481     uncompressed_table_start = (u2*) localvariable_table_start();
 482   } else if (has_exception_handler()) {
 483     uncompressed_table_start = (u2*) exception_table_start();
 484   } else if (has_checked_exceptions()) {
 485       uncompressed_table_start = (u2*) checked_exceptions_start();
 486   } else if (has_method_parameters()) {
 487       uncompressed_table_start = (u2*) method_parameters_start();
 488   } else {
 489       uncompressed_table_start = (u2*) m_end;
 490   }
 491   int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end;
 492   int max_gap = align_object_size(1)*BytesPerWord;
 493   guarantee(gap >= 0 && gap < max_gap, "invalid method layout");
 494 }