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