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 #ifndef SHARE_VM_OOPS_CONSTMETHODOOP_HPP 26 #define SHARE_VM_OOPS_CONSTMETHODOOP_HPP 27 28 #include "oops/oop.hpp" 29 30 // An ConstMethod represents portions of a Java method which are not written to after 31 // the classfile is parsed(*see below). This part of the method can be shared across 32 // processes in a read-only section with Class Data Sharing (CDS). It's important 33 // that this class doesn't have virtual functions because the vptr cannot be shared 34 // with CDS. 35 // 36 // Note that most applications load thousands of methods, so keeping the size of this 37 // structure small has a big impact on footprint. 38 39 // The actual bytecodes are inlined after the end of the ConstMethod struct. 40 // 41 // The line number table is compressed and inlined following the byte codes. It is 42 // found as the first byte following the byte codes. Note that accessing the line 43 // number and local variable tables is not performance critical at all. 44 // 45 // The checked exceptions table and the local variable table are inlined after the 46 // line number table, and indexed from the end of the method. We do not compress the 47 // checked exceptions table since the average length is less than 2, and it is used 48 // by reflection so access should be fast. We do not bother to compress the local 49 // variable table either since it is mostly absent. 50 // 51 // 52 // ConstMethod embedded field layout (after declared fields): 53 // [EMBEDDED byte codes] 54 // [EMBEDDED compressed linenumber table] 55 // (see class CompressedLineNumberReadStream) 56 // (note that length is unknown until decompressed) 57 // (access flags bit tells whether table is present) 58 // (indexed from start of ConstMethod) 59 // (elements not necessarily sorted!) 60 // [EMBEDDED localvariable table elements + length (length last)] 61 // (length is u2, elements are 6-tuples of u2) 62 // (see class LocalVariableTableElement) 63 // (access flags bit tells whether table is present) 64 // (indexed from end of ConstMethod*) 65 // [EMBEDDED exception table + length (length last)] 66 // (length is u2, elements are 4-tuples of u2) 67 // (see class ExceptionTableElement) 68 // (access flags bit tells whether table is present) 69 // (indexed from end of ConstMethod*) 70 // [EMBEDDED checked exceptions elements + length (length last)] 71 // (length is u2, elements are u2) 72 // (see class CheckedExceptionElement) 73 // (access flags bit tells whether table is present) 74 // (indexed from end of ConstMethod*) 75 // [EMBEDDED method parameters elements + length (length last)] 76 // (length is u2, elements are u2, u4 structures) 77 // (see class MethodParametersElement) 78 // (access flags bit tells whether table is present) 79 // (indexed from end of ConstMethod*) 80 // [EMBEDDED value factory parameters mapping + length (length last)] 81 // (length is u2, elements are union of u4 or two u2) 82 // (see class ValueFactoryParameterMappingElement) 83 // (access flags bit tells whether table is present) 84 // (indexed from end of ConstMethod*) 85 // [EMBEDDED generic signature index (u2)] 86 // (indexed from end of constMethodOop) 87 // [EMBEDDED annotations arrays - method, parameter, type, default] 88 // pointer to Array<u1> if annotation is present 89 // 90 // IMPORTANT: If anything gets added here, there need to be changes to 91 // ensure that ServicabilityAgent doesn't get broken as a result! 92 93 94 // Utility class describing elements in checked exceptions table inlined in Method*. 95 class CheckedExceptionElement VALUE_OBJ_CLASS_SPEC { 96 public: 97 u2 class_cp_index; 98 }; 99 100 101 // Utility class describing elements in local variable table inlined in Method*. 102 class LocalVariableTableElement VALUE_OBJ_CLASS_SPEC { 103 public: 104 u2 start_bci; 105 u2 length; 106 u2 name_cp_index; 107 u2 descriptor_cp_index; 108 u2 signature_cp_index; 109 u2 slot; 110 }; 111 112 // Utility class describing elements in exception table 113 class ExceptionTableElement VALUE_OBJ_CLASS_SPEC { 114 public: 115 u2 start_pc; 116 u2 end_pc; 117 u2 handler_pc; 118 u2 catch_type_index; 119 }; 120 121 // Utility class describing elements in method parameters 122 class MethodParametersElement VALUE_OBJ_CLASS_SPEC { 123 public: 124 u2 name_cp_index; 125 u2 flags; 126 }; 127 128 // Utility class describing element in ValueFactory parameter mapping 129 class ValueFactoryParameterMappingElement VALUE_OBJ_CLASS_SPEC{ 130 public: 131 union data_t { 132 u4 field_index; 133 struct temp_t { 134 u2 arg_index; 135 u2 field_index; 136 } temp; 137 } data; 138 }; 139 140 class KlassSizeStats; 141 142 // Class to collect the sizes of ConstMethod inline tables 143 #define INLINE_TABLES_DO(do_element) \ 144 do_element(localvariable_table_length) \ 145 do_element(compressed_linenumber_size) \ 146 do_element(exception_table_length) \ 147 do_element(checked_exceptions_length) \ 148 do_element(method_parameters_length) \ 149 do_element(valuefactory_parameter_mapping_length) \ 150 do_element(generic_signature_index) \ 151 do_element(method_annotations_length) \ 152 do_element(parameter_annotations_length) \ 153 do_element(type_annotations_length) \ 154 do_element(default_annotations_length) 155 156 #define INLINE_TABLE_DECLARE(sym) int _##sym; 157 #define INLINE_TABLE_PARAM(sym) int sym, 158 #define INLINE_TABLE_INIT(sym) _##sym(sym), 159 #define INLINE_TABLE_NULL(sym) _##sym(0), 160 #define INLINE_TABLE_ACCESSOR(sym) int sym() const { return _##sym; } 161 162 class InlineTableSizes : StackObj { 163 // declarations 164 INLINE_TABLES_DO(INLINE_TABLE_DECLARE) 165 int _end; 166 public: 167 InlineTableSizes( 168 INLINE_TABLES_DO(INLINE_TABLE_PARAM) 169 int end) : 170 INLINE_TABLES_DO(INLINE_TABLE_INIT) 171 _end(end) {} 172 173 // Default constructor for no inlined tables 174 InlineTableSizes() : 175 INLINE_TABLES_DO(INLINE_TABLE_NULL) 176 _end(0) {} 177 178 // Accessors 179 INLINE_TABLES_DO(INLINE_TABLE_ACCESSOR) 180 }; 181 #undef INLINE_TABLE_ACCESSOR 182 #undef INLINE_TABLE_NULL 183 #undef INLINE_TABLE_INIT 184 #undef INLINE_TABLE_PARAM 185 #undef INLINE_TABLE_DECLARE 186 187 class ConstMethod : public MetaspaceObj { 188 friend class VMStructs; 189 friend class JVMCIVMStructs; 190 191 public: 192 typedef enum { NORMAL, OVERPASS } MethodType; 193 194 private: 195 enum { 196 _has_linenumber_table = 0x0001, 197 _has_checked_exceptions = 0x0002, 198 _has_localvariable_table = 0x0004, 199 _has_exception_table = 0x0008, 200 _has_generic_signature = 0x0010, 201 _has_method_parameters = 0x0020, 202 _is_overpass = 0x0040, 203 _has_method_annotations = 0x0080, 204 _has_parameter_annotations = 0x0100, 205 _has_type_annotations = 0x0200, 206 _has_default_annotations = 0x0400, 207 _has_valuefactory_parameter_mapping = 0x0800 208 }; 209 210 // Bit vector of signature 211 // Callers interpret 0=not initialized yet and 212 // -1=too many args to fix, must parse the slow way. 213 // The real initial value is special to account for nonatomicity of 64 bit 214 // loads and stores. This value may updated and read without a lock by 215 // multiple threads, so is volatile. 216 volatile uint64_t _fingerprint; 217 218 ConstantPool* _constants; // Constant pool 219 220 // Raw stackmap data for the method 221 Array<u1>* _stackmap_data; 222 223 int _constMethod_size; 224 u2 _flags; 225 226 // Size of Java bytecodes allocated immediately after Method*. 227 u2 _code_size; 228 u2 _name_index; // Method name (index in constant pool) 229 u2 _signature_index; // Method signature (index in constant pool) 230 u2 _method_idnum; // unique identification number for the method within the class 231 // initially corresponds to the index into the methods array. 232 // but this may change with redefinition 233 u2 _max_stack; // Maximum number of entries on the expression stack 234 u2 _max_locals; // Number of local variables used by this method 235 u2 _size_of_parameters; // size of the parameter block (receiver + arguments) in words 236 u2 _orig_method_idnum; // Original unique identification number for the method 237 238 // Constructor 239 ConstMethod(int byte_code_size, 240 InlineTableSizes* sizes, 241 MethodType is_overpass, 242 int size); 243 public: 244 245 static ConstMethod* allocate(ClassLoaderData* loader_data, 246 int byte_code_size, 247 InlineTableSizes* sizes, 248 MethodType mt, 249 TRAPS); 250 251 bool is_constMethod() const { return true; } 252 253 // Inlined tables 254 void set_inlined_tables_length(InlineTableSizes* sizes); 255 256 bool has_generic_signature() const 257 { return (_flags & _has_generic_signature) != 0; } 258 259 bool has_linenumber_table() const 260 { return (_flags & _has_linenumber_table) != 0; } 261 262 bool has_checked_exceptions() const 263 { return (_flags & _has_checked_exceptions) != 0; } 264 265 bool has_localvariable_table() const 266 { return (_flags & _has_localvariable_table) != 0; } 267 268 bool has_exception_handler() const 269 { return (_flags & _has_exception_table) != 0; } 270 271 bool has_method_parameters() const 272 { return (_flags & _has_method_parameters) != 0; } 273 274 bool has_valuefactory_parameter_mapping() const 275 { return (_flags & _has_valuefactory_parameter_mapping) != 0; } 276 277 MethodType method_type() const { 278 return ((_flags & _is_overpass) == 0) ? NORMAL : OVERPASS; 279 } 280 281 void set_method_type(MethodType mt) { 282 if (mt == NORMAL) { 283 _flags &= ~(_is_overpass); 284 } else { 285 _flags |= _is_overpass; 286 } 287 } 288 289 // constant pool 290 ConstantPool* constants() const { return _constants; } 291 void set_constants(ConstantPool* c) { _constants = c; } 292 293 Method* method() const; 294 295 // stackmap table data 296 Array<u1>* stackmap_data() const { return _stackmap_data; } 297 void set_stackmap_data(Array<u1>* sd) { _stackmap_data = sd; } 298 void copy_stackmap_data(ClassLoaderData* loader_data, u1* sd, int length, TRAPS); 299 bool has_stackmap_table() const { return _stackmap_data != NULL; } 300 301 void init_fingerprint() { 302 const uint64_t initval = UCONST64(0x8000000000000000); 303 _fingerprint = initval; 304 } 305 306 uint64_t fingerprint() const { 307 // Since reads aren't atomic for 64 bits, if any of the high or low order 308 // word is the initial value, return 0. See init_fingerprint for initval. 309 uint high_fp = (uint)(_fingerprint >> 32); 310 if ((int) _fingerprint == 0 || high_fp == 0x80000000) { 311 return 0L; 312 } else { 313 return _fingerprint; 314 } 315 } 316 317 uint64_t set_fingerprint(uint64_t new_fingerprint) { 318 #ifdef ASSERT 319 // Assert only valid if complete/valid 64 bit _fingerprint value is read. 320 uint64_t oldfp = fingerprint(); 321 #endif // ASSERT 322 _fingerprint = new_fingerprint; 323 assert(oldfp == 0L || new_fingerprint == oldfp, 324 "fingerprint cannot change"); 325 assert(((new_fingerprint >> 32) != 0x80000000) && (int)new_fingerprint !=0, 326 "fingerprint should call init to set initial value"); 327 return new_fingerprint; 328 } 329 330 // name 331 int name_index() const { return _name_index; } 332 void set_name_index(int index) { _name_index = index; } 333 334 // signature 335 int signature_index() const { return _signature_index; } 336 void set_signature_index(int index) { _signature_index = index; } 337 338 // generics support 339 int generic_signature_index() const { 340 if (has_generic_signature()) { 341 return *generic_signature_index_addr(); 342 } else { 343 return 0; 344 } 345 } 346 void set_generic_signature_index(u2 index) { 347 assert(has_generic_signature(), ""); 348 u2* addr = generic_signature_index_addr(); 349 *addr = index; 350 } 351 352 // Sizing 353 static int header_size() { return sizeof(ConstMethod)/wordSize; } 354 355 // Size needed 356 static int size(int code_size, InlineTableSizes* sizes); 357 358 int size() const { return _constMethod_size;} 359 void set_constMethod_size(int size) { _constMethod_size = size; } 360 #if INCLUDE_SERVICES 361 void collect_statistics(KlassSizeStats *sz) const; 362 #endif 363 364 // code size 365 int code_size() const { return _code_size; } 366 void set_code_size(int size) { 367 assert(max_method_code_size < (1 << 16), 368 "u2 is too small to hold method code size in general"); 369 assert(0 <= size && size <= max_method_code_size, "invalid code size"); 370 _code_size = size; 371 } 372 373 // linenumber table - note that length is unknown until decompression, 374 // see class CompressedLineNumberReadStream. 375 u_char* compressed_linenumber_table() const; // not preserved by gc 376 u2* generic_signature_index_addr() const; 377 u2* checked_exceptions_length_addr() const; 378 u2* localvariable_table_length_addr() const; 379 u2* exception_table_length_addr() const; 380 u2* method_parameters_length_addr() const; 381 u2* valuefactory_parameter_mapping_length_addr() const; 382 383 // checked exceptions 384 int checked_exceptions_length() const; 385 CheckedExceptionElement* checked_exceptions_start() const; 386 387 // localvariable table 388 int localvariable_table_length() const; 389 LocalVariableTableElement* localvariable_table_start() const; 390 391 // exception table 392 int exception_table_length() const; 393 ExceptionTableElement* exception_table_start() const; 394 395 // valuefactory parameter mapping 396 int valuefactory_parameter_mapping_length() const; 397 ValueFactoryParameterMappingElement* valuefactory_parameter_mapping_start() const; 398 399 // method parameters table 400 401 // This returns -1 if no parameters are present, a non-negative 402 // value otherwise. Note: sometimes, there are 0-length parameters 403 // attributes that must be reported up to the reflection API all the 404 // same. 405 int method_parameters_length() const; 406 MethodParametersElement* method_parameters_start() const; 407 408 // method annotations 409 bool has_method_annotations() const 410 { return (_flags & _has_method_annotations) != 0; } 411 412 bool has_parameter_annotations() const 413 { return (_flags & _has_parameter_annotations) != 0; } 414 415 bool has_type_annotations() const 416 { return (_flags & _has_type_annotations) != 0; } 417 418 bool has_default_annotations() const 419 { return (_flags & _has_default_annotations) != 0; } 420 421 422 AnnotationArray** method_annotations_addr() const; 423 AnnotationArray* method_annotations() const { 424 return has_method_annotations() ? *(method_annotations_addr()) : NULL; 425 } 426 void set_method_annotations(AnnotationArray* anno) { 427 *(method_annotations_addr()) = anno; 428 } 429 430 AnnotationArray** parameter_annotations_addr() const; 431 AnnotationArray* parameter_annotations() const { 432 return has_parameter_annotations() ? *(parameter_annotations_addr()) : NULL; 433 } 434 void set_parameter_annotations(AnnotationArray* anno) { 435 *(parameter_annotations_addr()) = anno; 436 } 437 438 AnnotationArray** type_annotations_addr() const; 439 AnnotationArray* type_annotations() const { 440 return has_type_annotations() ? *(type_annotations_addr()) : NULL; 441 } 442 void set_type_annotations(AnnotationArray* anno) { 443 *(type_annotations_addr()) = anno; 444 } 445 446 AnnotationArray** default_annotations_addr() const; 447 AnnotationArray* default_annotations() const { 448 return has_default_annotations() ? *(default_annotations_addr()) : NULL; 449 } 450 void set_default_annotations(AnnotationArray* anno) { 451 *(default_annotations_addr()) = anno; 452 } 453 454 int method_annotations_length() const { 455 return has_method_annotations() ? method_annotations()->length() : 0; 456 } 457 int parameter_annotations_length() const { 458 return has_parameter_annotations() ? parameter_annotations()->length() : 0; 459 } 460 int type_annotations_length() const { 461 return has_type_annotations() ? type_annotations()->length() : 0; 462 } 463 int default_annotations_length() const { 464 return has_default_annotations() ? default_annotations()->length() : 0; 465 } 466 467 // Copy annotations from other ConstMethod 468 void copy_annotations_from(ConstMethod* cm); 469 470 // byte codes 471 void set_code(address code) { 472 if (code_size() > 0) { 473 memcpy(code_base(), code, code_size()); 474 } 475 } 476 address code_base() const { return (address) (this+1); } 477 address code_end() const { return code_base() + code_size(); } 478 bool contains(address bcp) const { return code_base() <= bcp 479 && bcp < code_end(); } 480 // Offset to bytecodes 481 static ByteSize codes_offset() 482 { return in_ByteSize(sizeof(ConstMethod)); } 483 484 static ByteSize constants_offset() 485 { return byte_offset_of(ConstMethod, _constants); } 486 487 static ByteSize max_stack_offset() 488 { return byte_offset_of(ConstMethod, _max_stack); } 489 static ByteSize size_of_locals_offset() 490 { return byte_offset_of(ConstMethod, _max_locals); } 491 static ByteSize size_of_parameters_offset() 492 { return byte_offset_of(ConstMethod, _size_of_parameters); } 493 494 495 // Unique id for the method 496 static const u2 MAX_IDNUM; 497 static const u2 UNSET_IDNUM; 498 u2 method_idnum() const { return _method_idnum; } 499 void set_method_idnum(u2 idnum) { _method_idnum = idnum; } 500 501 u2 orig_method_idnum() const { return _orig_method_idnum; } 502 void set_orig_method_idnum(u2 idnum) { _orig_method_idnum = idnum; } 503 504 // max stack 505 int max_stack() const { return _max_stack; } 506 void set_max_stack(int size) { _max_stack = size; } 507 508 // max locals 509 int max_locals() const { return _max_locals; } 510 void set_max_locals(int size) { _max_locals = size; } 511 512 // size of parameters 513 int size_of_parameters() const { return _size_of_parameters; } 514 void set_size_of_parameters(int size) { _size_of_parameters = size; } 515 516 // Deallocation for RedefineClasses 517 void deallocate_contents(ClassLoaderData* loader_data); 518 bool is_klass() const { return false; } 519 DEBUG_ONLY(bool on_stack() { return false; }) 520 521 private: 522 // Since the size of the compressed line number table is unknown, the 523 // offsets of the other variable sized sections are computed backwards 524 // from the end of the ConstMethod*. 525 526 // First byte after ConstMethod* 527 address constMethod_end() const 528 { return (address)((intptr_t*)this + _constMethod_size); } 529 530 // Last short in ConstMethod* 531 u2* last_u2_element() const; 532 533 public: 534 // Printing 535 void print_on (outputStream* st) const; 536 void print_value_on(outputStream* st) const; 537 538 const char* internal_name() const { return "{constMethod}"; } 539 540 // Verify 541 void verify_on(outputStream* st); 542 }; 543 544 #endif // SHARE_VM_OOPS_CONSTMETHODOOP_HPP