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