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