1 /* 2 * Copyright (c) 1997, 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 #include "precompiled.hpp" 25 #include "jvm.h" 26 #include "aot/aotLoader.hpp" 27 #include "classfile/classFileParser.hpp" 28 #include "classfile/classFileStream.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/defaultMethods.hpp" 32 #include "classfile/dictionary.hpp" 33 #include "classfile/javaClasses.inline.hpp" 34 #include "classfile/moduleEntry.hpp" 35 #include "classfile/packageEntry.hpp" 36 #include "classfile/symbolTable.hpp" 37 #include "classfile/systemDictionary.hpp" 38 #include "classfile/verificationType.hpp" 39 #include "classfile/verifier.hpp" 40 #include "classfile/vmSymbols.hpp" 41 #include "logging/log.hpp" 42 #include "logging/logStream.hpp" 43 #include "memory/allocation.hpp" 44 #include "memory/metadataFactory.hpp" 45 #include "memory/oopFactory.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "memory/universe.hpp" 48 #include "oops/annotations.hpp" 49 #include "oops/constantPool.inline.hpp" 50 #include "oops/fieldStreams.hpp" 51 #include "oops/instanceKlass.hpp" 52 #include "oops/instanceMirrorKlass.hpp" 53 #include "oops/klass.inline.hpp" 54 #include "oops/klassVtable.hpp" 55 #include "oops/metadata.hpp" 56 #include "oops/method.inline.hpp" 57 #include "oops/oop.inline.hpp" 58 #include "oops/symbol.hpp" 59 #include "oops/valueKlass.hpp" 60 #include "prims/jvmtiExport.hpp" 61 #include "prims/jvmtiThreadState.hpp" 62 #include "runtime/arguments.hpp" 63 #include "runtime/handles.inline.hpp" 64 #include "runtime/javaCalls.hpp" 65 #include "runtime/os.hpp" 66 #include "runtime/perfData.hpp" 67 #include "runtime/reflection.hpp" 68 #include "runtime/safepointVerifiers.hpp" 69 #include "runtime/signature.hpp" 70 #include "runtime/timer.hpp" 71 #include "services/classLoadingService.hpp" 72 #include "services/threadService.hpp" 73 #include "utilities/align.hpp" 74 #include "utilities/bitMap.inline.hpp" 75 #include "utilities/copy.hpp" 76 #include "utilities/exceptions.hpp" 77 #include "utilities/globalDefinitions.hpp" 78 #include "utilities/growableArray.hpp" 79 #include "utilities/macros.hpp" 80 #include "utilities/ostream.hpp" 81 #include "utilities/resourceHash.hpp" 82 #include "utilities/utf8.hpp" 83 84 #if INCLUDE_CDS 85 #include "classfile/systemDictionaryShared.hpp" 86 #endif 87 #if INCLUDE_JFR 88 #include "jfr/support/jfrTraceIdExtension.hpp" 89 #endif 90 91 // We generally try to create the oops directly when parsing, rather than 92 // allocating temporary data structures and copying the bytes twice. A 93 // temporary area is only needed when parsing utf8 entries in the constant 94 // pool and when parsing line number tables. 95 96 // We add assert in debug mode when class format is not checked. 97 98 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE 99 #define JAVA_MIN_SUPPORTED_VERSION 45 100 #define JAVA_PREVIEW_MINOR_VERSION 65535 101 102 // Used for two backward compatibility reasons: 103 // - to check for new additions to the class file format in JDK1.5 104 // - to check for bug fixes in the format checker in JDK1.5 105 #define JAVA_1_5_VERSION 49 106 107 // Used for backward compatibility reasons: 108 // - to check for javac bug fixes that happened after 1.5 109 // - also used as the max version when running in jdk6 110 #define JAVA_6_VERSION 50 111 112 // Used for backward compatibility reasons: 113 // - to disallow argument and require ACC_STATIC for <clinit> methods 114 #define JAVA_7_VERSION 51 115 116 // Extension method support. 117 #define JAVA_8_VERSION 52 118 119 #define JAVA_9_VERSION 53 120 121 #define JAVA_10_VERSION 54 122 123 #define JAVA_11_VERSION 55 124 125 #define JAVA_12_VERSION 56 126 127 #define JAVA_13_VERSION 57 128 129 #define CONSTANT_CLASS_DESCRIPTORS 57 130 131 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) { 132 assert((bad_constant == 19 || bad_constant == 20) && _major_version >= JAVA_9_VERSION, 133 "Unexpected bad constant pool entry"); 134 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant; 135 } 136 137 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream, 138 ConstantPool* cp, 139 const int length, 140 TRAPS) { 141 assert(stream != NULL, "invariant"); 142 assert(cp != NULL, "invariant"); 143 144 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize 145 // this function (_current can be allocated in a register, with scalar 146 // replacement of aggregates). The _current pointer is copied back to 147 // stream() when this function returns. DON'T call another method within 148 // this method that uses stream(). 149 const ClassFileStream cfs1 = *stream; 150 const ClassFileStream* const cfs = &cfs1; 151 152 assert(cfs->allocated_on_stack(), "should be local"); 153 debug_only(const u1* const old_current = stream->current();) 154 155 // Used for batching symbol allocations. 156 const char* names[SymbolTable::symbol_alloc_batch_size]; 157 int lengths[SymbolTable::symbol_alloc_batch_size]; 158 int indices[SymbolTable::symbol_alloc_batch_size]; 159 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size]; 160 int names_count = 0; 161 162 // parsing Index 0 is unused 163 for (int index = 1; index < length; index++) { 164 // Each of the following case guarantees one more byte in the stream 165 // for the following tag or the access_flags following constant pool, 166 // so we don't need bounds-check for reading tag. 167 const u1 tag = cfs->get_u1_fast(); 168 switch (tag) { 169 case JVM_CONSTANT_Class: { 170 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags 171 const u2 name_index = cfs->get_u2_fast(); 172 cp->klass_index_at_put(index, name_index); 173 break; 174 } 175 case JVM_CONSTANT_Fieldref: { 176 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 177 const u2 class_index = cfs->get_u2_fast(); 178 const u2 name_and_type_index = cfs->get_u2_fast(); 179 cp->field_at_put(index, class_index, name_and_type_index); 180 break; 181 } 182 case JVM_CONSTANT_Methodref: { 183 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 184 const u2 class_index = cfs->get_u2_fast(); 185 const u2 name_and_type_index = cfs->get_u2_fast(); 186 cp->method_at_put(index, class_index, name_and_type_index); 187 break; 188 } 189 case JVM_CONSTANT_InterfaceMethodref: { 190 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags 191 const u2 class_index = cfs->get_u2_fast(); 192 const u2 name_and_type_index = cfs->get_u2_fast(); 193 cp->interface_method_at_put(index, class_index, name_and_type_index); 194 break; 195 } 196 case JVM_CONSTANT_String : { 197 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags 198 const u2 string_index = cfs->get_u2_fast(); 199 cp->string_index_at_put(index, string_index); 200 break; 201 } 202 case JVM_CONSTANT_MethodHandle : 203 case JVM_CONSTANT_MethodType: { 204 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 205 classfile_parse_error( 206 "Class file version does not support constant tag %u in class file %s", 207 tag, CHECK); 208 } 209 if (tag == JVM_CONSTANT_MethodHandle) { 210 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags 211 const u1 ref_kind = cfs->get_u1_fast(); 212 const u2 method_index = cfs->get_u2_fast(); 213 cp->method_handle_index_at_put(index, ref_kind, method_index); 214 } 215 else if (tag == JVM_CONSTANT_MethodType) { 216 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags 217 const u2 signature_index = cfs->get_u2_fast(); 218 cp->method_type_index_at_put(index, signature_index); 219 } 220 else { 221 ShouldNotReachHere(); 222 } 223 break; 224 } 225 case JVM_CONSTANT_Dynamic : { 226 if (_major_version < Verifier::DYNAMICCONSTANT_MAJOR_VERSION) { 227 classfile_parse_error( 228 "Class file version does not support constant tag %u in class file %s", 229 tag, CHECK); 230 } 231 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 232 const u2 bootstrap_specifier_index = cfs->get_u2_fast(); 233 const u2 name_and_type_index = cfs->get_u2_fast(); 234 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) { 235 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 236 } 237 cp->dynamic_constant_at_put(index, bootstrap_specifier_index, name_and_type_index); 238 break; 239 } 240 case JVM_CONSTANT_InvokeDynamic : { 241 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 242 classfile_parse_error( 243 "Class file version does not support constant tag %u in class file %s", 244 tag, CHECK); 245 } 246 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags 247 const u2 bootstrap_specifier_index = cfs->get_u2_fast(); 248 const u2 name_and_type_index = cfs->get_u2_fast(); 249 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) { 250 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later 251 } 252 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index); 253 break; 254 } 255 case JVM_CONSTANT_Integer: { 256 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 257 const u4 bytes = cfs->get_u4_fast(); 258 cp->int_at_put(index, (jint)bytes); 259 break; 260 } 261 case JVM_CONSTANT_Float: { 262 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags 263 const u4 bytes = cfs->get_u4_fast(); 264 cp->float_at_put(index, *(jfloat*)&bytes); 265 break; 266 } 267 case JVM_CONSTANT_Long: { 268 // A mangled type might cause you to overrun allocated memory 269 guarantee_property(index + 1 < length, 270 "Invalid constant pool entry %u in class file %s", 271 index, 272 CHECK); 273 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 274 const u8 bytes = cfs->get_u8_fast(); 275 cp->long_at_put(index, bytes); 276 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 277 break; 278 } 279 case JVM_CONSTANT_Double: { 280 // A mangled type might cause you to overrun allocated memory 281 guarantee_property(index+1 < length, 282 "Invalid constant pool entry %u in class file %s", 283 index, 284 CHECK); 285 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags 286 const u8 bytes = cfs->get_u8_fast(); 287 cp->double_at_put(index, *(jdouble*)&bytes); 288 index++; // Skip entry following eigth-byte constant, see JVM book p. 98 289 break; 290 } 291 case JVM_CONSTANT_NameAndType: { 292 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags 293 const u2 name_index = cfs->get_u2_fast(); 294 const u2 signature_index = cfs->get_u2_fast(); 295 cp->name_and_type_at_put(index, name_index, signature_index); 296 break; 297 } 298 case JVM_CONSTANT_Utf8 : { 299 cfs->guarantee_more(2, CHECK); // utf8_length 300 u2 utf8_length = cfs->get_u2_fast(); 301 const u1* utf8_buffer = cfs->current(); 302 assert(utf8_buffer != NULL, "null utf8 buffer"); 303 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward. 304 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags 305 cfs->skip_u1_fast(utf8_length); 306 307 // Before storing the symbol, make sure it's legal 308 if (_need_verify) { 309 verify_legal_utf8(utf8_buffer, utf8_length, CHECK); 310 } 311 312 if (has_cp_patch_at(index)) { 313 Handle patch = clear_cp_patch_at(index); 314 guarantee_property(java_lang_String::is_instance(patch()), 315 "Illegal utf8 patch at %d in class file %s", 316 index, 317 CHECK); 318 const char* const str = java_lang_String::as_utf8_string(patch()); 319 // (could use java_lang_String::as_symbol instead, but might as well batch them) 320 utf8_buffer = (const u1*) str; 321 utf8_length = (u2) strlen(str); 322 } 323 324 unsigned int hash; 325 Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer, 326 utf8_length, 327 hash); 328 if (result == NULL) { 329 names[names_count] = (const char*)utf8_buffer; 330 lengths[names_count] = utf8_length; 331 indices[names_count] = index; 332 hashValues[names_count++] = hash; 333 if (names_count == SymbolTable::symbol_alloc_batch_size) { 334 SymbolTable::new_symbols(_loader_data, 335 cp, 336 names_count, 337 names, 338 lengths, 339 indices, 340 hashValues, 341 CHECK); 342 names_count = 0; 343 } 344 } else { 345 cp->symbol_at_put(index, result); 346 } 347 break; 348 } 349 case 19: 350 case 20: { 351 // Record that an error occurred in these two cases but keep parsing so 352 // that ACC_Module can be checked for in the access_flags. Need to 353 // throw NoClassDefFoundError in that case. 354 if (_major_version >= JAVA_9_VERSION) { 355 cfs->guarantee_more(3, CHECK); 356 cfs->get_u2_fast(); 357 set_class_bad_constant_seen(tag); 358 break; 359 } 360 } 361 default: { 362 classfile_parse_error("Unknown constant tag %u in class file %s", 363 tag, 364 CHECK); 365 break; 366 } 367 } // end of switch(tag) 368 } // end of for 369 370 // Allocate the remaining symbols 371 if (names_count > 0) { 372 SymbolTable::new_symbols(_loader_data, 373 cp, 374 names_count, 375 names, 376 lengths, 377 indices, 378 hashValues, 379 CHECK); 380 } 381 382 // Copy _current pointer of local copy back to stream. 383 assert(stream->current() == old_current, "non-exclusive use of stream"); 384 stream->set_current(cfs1.current()); 385 386 } 387 388 static inline bool valid_cp_range(int index, int length) { 389 return (index > 0 && index < length); 390 } 391 392 static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) { 393 assert(cp != NULL, "invariant"); 394 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) { 395 return cp->symbol_at(index); 396 } 397 return NULL; 398 } 399 400 #ifdef ASSERT 401 PRAGMA_DIAG_PUSH 402 PRAGMA_FORMAT_NONLITERAL_IGNORED 403 void ClassFileParser::report_assert_property_failure(const char* msg, TRAPS) const { 404 ResourceMark rm(THREAD); 405 fatal(msg, _class_name->as_C_string()); 406 } 407 408 void ClassFileParser::report_assert_property_failure(const char* msg, 409 int index, 410 TRAPS) const { 411 ResourceMark rm(THREAD); 412 fatal(msg, index, _class_name->as_C_string()); 413 } 414 PRAGMA_DIAG_POP 415 #endif 416 417 void ClassFileParser::parse_constant_pool(const ClassFileStream* const stream, 418 ConstantPool* const cp, 419 const int length, 420 TRAPS) { 421 assert(cp != NULL, "invariant"); 422 assert(stream != NULL, "invariant"); 423 424 // parsing constant pool entries 425 parse_constant_pool_entries(stream, cp, length, CHECK); 426 if (class_bad_constant_seen() != 0) { 427 // a bad CP entry has been detected previously so stop parsing and just return. 428 return; 429 } 430 431 int index = 1; // declared outside of loops for portability 432 int num_klasses = 0; 433 434 // first verification pass - validate cross references 435 // and fixup class and string constants 436 for (index = 1; index < length; index++) { // Index 0 is unused 437 const jbyte tag = cp->tag_at(index).value(); 438 switch (tag) { 439 case JVM_CONSTANT_Class: { 440 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 441 break; 442 } 443 case JVM_CONSTANT_Fieldref: 444 // fall through 445 case JVM_CONSTANT_Methodref: 446 // fall through 447 case JVM_CONSTANT_InterfaceMethodref: { 448 if (!_need_verify) break; 449 const int klass_ref_index = cp->klass_ref_index_at(index); 450 const int name_and_type_ref_index = cp->name_and_type_ref_index_at(index); 451 check_property(valid_klass_reference_at(klass_ref_index), 452 "Invalid constant pool index %u in class file %s", 453 klass_ref_index, CHECK); 454 check_property(valid_cp_range(name_and_type_ref_index, length) && 455 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 456 "Invalid constant pool index %u in class file %s", 457 name_and_type_ref_index, CHECK); 458 break; 459 } 460 case JVM_CONSTANT_String: { 461 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present 462 break; 463 } 464 case JVM_CONSTANT_Integer: 465 break; 466 case JVM_CONSTANT_Float: 467 break; 468 case JVM_CONSTANT_Long: 469 case JVM_CONSTANT_Double: { 470 index++; 471 check_property( 472 (index < length && cp->tag_at(index).is_invalid()), 473 "Improper constant pool long/double index %u in class file %s", 474 index, CHECK); 475 break; 476 } 477 case JVM_CONSTANT_NameAndType: { 478 if (!_need_verify) break; 479 const int name_ref_index = cp->name_ref_index_at(index); 480 const int signature_ref_index = cp->signature_ref_index_at(index); 481 check_property(valid_symbol_at(name_ref_index), 482 "Invalid constant pool index %u in class file %s", 483 name_ref_index, CHECK); 484 check_property(valid_symbol_at(signature_ref_index), 485 "Invalid constant pool index %u in class file %s", 486 signature_ref_index, CHECK); 487 break; 488 } 489 case JVM_CONSTANT_Utf8: 490 break; 491 case JVM_CONSTANT_UnresolvedClass: // fall-through 492 case JVM_CONSTANT_UnresolvedClassInError: { 493 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present 494 break; 495 } 496 case JVM_CONSTANT_ClassIndex: { 497 const int class_index = cp->klass_index_at(index); 498 check_property(valid_symbol_at(class_index), 499 "Invalid constant pool index %u in class file %s", 500 class_index, CHECK); 501 502 Symbol* const name = cp->symbol_at(class_index); 503 const unsigned int name_len = name->utf8_length(); 504 if (name->is_Q_signature()) { 505 cp->unresolved_qdescriptor_at_put(index, class_index, num_klasses++); 506 } else { 507 cp->unresolved_klass_at_put(index, class_index, num_klasses++); 508 } 509 break; 510 } 511 case JVM_CONSTANT_StringIndex: { 512 const int string_index = cp->string_index_at(index); 513 check_property(valid_symbol_at(string_index), 514 "Invalid constant pool index %u in class file %s", 515 string_index, CHECK); 516 Symbol* const sym = cp->symbol_at(string_index); 517 cp->unresolved_string_at_put(index, sym); 518 break; 519 } 520 case JVM_CONSTANT_MethodHandle: { 521 const int ref_index = cp->method_handle_index_at(index); 522 check_property(valid_cp_range(ref_index, length), 523 "Invalid constant pool index %u in class file %s", 524 ref_index, CHECK); 525 const constantTag tag = cp->tag_at(ref_index); 526 const int ref_kind = cp->method_handle_ref_kind_at(index); 527 528 switch (ref_kind) { 529 case JVM_REF_getField: 530 case JVM_REF_getStatic: 531 case JVM_REF_putField: 532 case JVM_REF_putStatic: { 533 check_property( 534 tag.is_field(), 535 "Invalid constant pool index %u in class file %s (not a field)", 536 ref_index, CHECK); 537 break; 538 } 539 case JVM_REF_invokeVirtual: 540 case JVM_REF_newInvokeSpecial: { 541 check_property( 542 tag.is_method(), 543 "Invalid constant pool index %u in class file %s (not a method)", 544 ref_index, CHECK); 545 break; 546 } 547 case JVM_REF_invokeStatic: 548 case JVM_REF_invokeSpecial: { 549 check_property( 550 tag.is_method() || 551 ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()), 552 "Invalid constant pool index %u in class file %s (not a method)", 553 ref_index, CHECK); 554 break; 555 } 556 case JVM_REF_invokeInterface: { 557 check_property( 558 tag.is_interface_method(), 559 "Invalid constant pool index %u in class file %s (not an interface method)", 560 ref_index, CHECK); 561 break; 562 } 563 default: { 564 classfile_parse_error( 565 "Bad method handle kind at constant pool index %u in class file %s", 566 index, CHECK); 567 } 568 } // switch(refkind) 569 // Keep the ref_index unchanged. It will be indirected at link-time. 570 break; 571 } // case MethodHandle 572 case JVM_CONSTANT_MethodType: { 573 const int ref_index = cp->method_type_index_at(index); 574 check_property(valid_symbol_at(ref_index), 575 "Invalid constant pool index %u in class file %s", 576 ref_index, CHECK); 577 break; 578 } 579 case JVM_CONSTANT_Dynamic: { 580 const int name_and_type_ref_index = 581 cp->bootstrap_name_and_type_ref_index_at(index); 582 583 check_property(valid_cp_range(name_and_type_ref_index, length) && 584 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 585 "Invalid constant pool index %u in class file %s", 586 name_and_type_ref_index, CHECK); 587 // bootstrap specifier index must be checked later, 588 // when BootstrapMethods attr is available 589 590 // Mark the constant pool as having a CONSTANT_Dynamic_info structure 591 cp->set_has_dynamic_constant(); 592 break; 593 } 594 case JVM_CONSTANT_InvokeDynamic: { 595 const int name_and_type_ref_index = 596 cp->bootstrap_name_and_type_ref_index_at(index); 597 598 check_property(valid_cp_range(name_and_type_ref_index, length) && 599 cp->tag_at(name_and_type_ref_index).is_name_and_type(), 600 "Invalid constant pool index %u in class file %s", 601 name_and_type_ref_index, CHECK); 602 // bootstrap specifier index must be checked later, 603 // when BootstrapMethods attr is available 604 break; 605 } 606 default: { 607 fatal("bad constant pool tag value %u", cp->tag_at(index).value()); 608 ShouldNotReachHere(); 609 break; 610 } 611 } // switch(tag) 612 } // end of for 613 614 _first_patched_klass_resolved_index = num_klasses; 615 cp->allocate_resolved_klasses(_loader_data, num_klasses + _max_num_patched_klasses, CHECK); 616 617 if (_cp_patches != NULL) { 618 // need to treat this_class specially... 619 620 // Add dummy utf8 entries in the space reserved for names of patched classes. We'll use "*" 621 // for now. These will be replaced with actual names of the patched classes in patch_class(). 622 Symbol* s = vmSymbols::star_name(); 623 for (int n=_orig_cp_size; n<cp->length(); n++) { 624 cp->symbol_at_put(n, s); 625 } 626 627 int this_class_index; 628 { 629 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 630 const u1* const mark = stream->current(); 631 stream->skip_u2_fast(1); // skip flags 632 this_class_index = stream->get_u2_fast(); 633 stream->set_current(mark); // revert to mark 634 } 635 636 for (index = 1; index < length; index++) { // Index 0 is unused 637 if (has_cp_patch_at(index)) { 638 guarantee_property(index != this_class_index, 639 "Illegal constant pool patch to self at %d in class file %s", 640 index, CHECK); 641 patch_constant_pool(cp, index, cp_patch_at(index), CHECK); 642 } 643 } 644 } 645 646 if (!_need_verify) { 647 return; 648 } 649 650 // second verification pass - checks the strings are of the right format. 651 // but not yet to the other entries 652 for (index = 1; index < length; index++) { 653 const jbyte tag = cp->tag_at(index).value(); 654 switch (tag) { 655 case JVM_CONSTANT_UnresolvedClass: { 656 const Symbol* const class_name = cp->klass_name_at(index); 657 // check the name, even if _cp_patches will overwrite it 658 verify_legal_class_name(class_name, CHECK); 659 break; 660 } 661 case JVM_CONSTANT_NameAndType: { 662 if (_need_verify) { 663 const int sig_index = cp->signature_ref_index_at(index); 664 const int name_index = cp->name_ref_index_at(index); 665 const Symbol* const name = cp->symbol_at(name_index); 666 const Symbol* const sig = cp->symbol_at(sig_index); 667 guarantee_property(sig->utf8_length() != 0, 668 "Illegal zero length constant pool entry at %d in class %s", 669 sig_index, CHECK); 670 guarantee_property(name->utf8_length() != 0, 671 "Illegal zero length constant pool entry at %d in class %s", 672 name_index, CHECK); 673 674 if (sig->char_at(0) == JVM_SIGNATURE_FUNC) { 675 // Format check method name and signature 676 verify_legal_method_name(name, CHECK); 677 verify_legal_method_signature(name, sig, CHECK); 678 } else { 679 // Format check field name and signature 680 verify_legal_field_name(name, CHECK); 681 verify_legal_field_signature(name, sig, CHECK); 682 } 683 } 684 break; 685 } 686 case JVM_CONSTANT_Dynamic: { 687 const int name_and_type_ref_index = 688 cp->name_and_type_ref_index_at(index); 689 // already verified to be utf8 690 const int name_ref_index = 691 cp->name_ref_index_at(name_and_type_ref_index); 692 // already verified to be utf8 693 const int signature_ref_index = 694 cp->signature_ref_index_at(name_and_type_ref_index); 695 const Symbol* const name = cp->symbol_at(name_ref_index); 696 const Symbol* const signature = cp->symbol_at(signature_ref_index); 697 if (_need_verify) { 698 // CONSTANT_Dynamic's name and signature are verified above, when iterating NameAndType_info. 699 // Need only to be sure signature is non-zero length and the right type. 700 if (signature->utf8_length() == 0 || 701 signature->char_at(0) == JVM_SIGNATURE_FUNC) { 702 throwIllegalSignature("CONSTANT_Dynamic", name, signature, CHECK); 703 } 704 } 705 break; 706 } 707 case JVM_CONSTANT_InvokeDynamic: 708 case JVM_CONSTANT_Fieldref: 709 case JVM_CONSTANT_Methodref: 710 case JVM_CONSTANT_InterfaceMethodref: { 711 const int name_and_type_ref_index = 712 cp->name_and_type_ref_index_at(index); 713 // already verified to be utf8 714 const int name_ref_index = 715 cp->name_ref_index_at(name_and_type_ref_index); 716 // already verified to be utf8 717 const int signature_ref_index = 718 cp->signature_ref_index_at(name_and_type_ref_index); 719 const Symbol* const name = cp->symbol_at(name_ref_index); 720 const Symbol* const signature = cp->symbol_at(signature_ref_index); 721 if (tag == JVM_CONSTANT_Fieldref) { 722 if (_need_verify) { 723 // Field name and signature are verified above, when iterating NameAndType_info. 724 // Need only to be sure signature is non-zero length and the right type. 725 if (signature->utf8_length() == 0 || 726 signature->char_at(0) == JVM_SIGNATURE_FUNC) { 727 throwIllegalSignature("Field", name, signature, CHECK); 728 } 729 } 730 } else { 731 if (_need_verify) { 732 // Method name and signature are verified above, when iterating NameAndType_info. 733 // Need only to be sure signature is non-zero length and the right type. 734 if (signature->utf8_length() == 0 || 735 signature->char_at(0) != JVM_SIGNATURE_FUNC) { 736 throwIllegalSignature("Method", name, signature, CHECK); 737 } 738 } 739 // 4509014: If a class method name begins with '<', it must be "<init>" 740 const unsigned int name_len = name->utf8_length(); 741 if (tag == JVM_CONSTANT_Methodref && 742 name_len != 0 && 743 name->char_at(0) == '<' && 744 name != vmSymbols::object_initializer_name()) { 745 classfile_parse_error( 746 "Bad method name at constant pool index %u in class file %s", 747 name_ref_index, CHECK); 748 } 749 } 750 break; 751 } 752 case JVM_CONSTANT_MethodHandle: { 753 const int ref_index = cp->method_handle_index_at(index); 754 const int ref_kind = cp->method_handle_ref_kind_at(index); 755 switch (ref_kind) { 756 case JVM_REF_invokeVirtual: 757 case JVM_REF_invokeStatic: 758 case JVM_REF_invokeSpecial: 759 case JVM_REF_newInvokeSpecial: { 760 const int name_and_type_ref_index = 761 cp->name_and_type_ref_index_at(ref_index); 762 const int name_ref_index = 763 cp->name_ref_index_at(name_and_type_ref_index); 764 const Symbol* const name = cp->symbol_at(name_ref_index); 765 if (ref_kind == JVM_REF_newInvokeSpecial) { 766 if (name != vmSymbols::object_initializer_name()) { 767 classfile_parse_error( 768 "Bad constructor name at constant pool index %u in class file %s", 769 name_ref_index, CHECK); 770 } 771 } else { 772 if (name == vmSymbols::object_initializer_name()) { 773 classfile_parse_error( 774 "Bad method name at constant pool index %u in class file %s", 775 name_ref_index, CHECK); 776 } 777 } 778 break; 779 } 780 // Other ref_kinds are already fully checked in previous pass. 781 } // switch(ref_kind) 782 break; 783 } 784 case JVM_CONSTANT_MethodType: { 785 const Symbol* const no_name = vmSymbols::type_name(); // place holder 786 const Symbol* const signature = cp->method_type_signature_at(index); 787 verify_legal_method_signature(no_name, signature, CHECK); 788 break; 789 } 790 case JVM_CONSTANT_Utf8: { 791 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted"); 792 } 793 } // switch(tag) 794 } // end of for 795 } 796 797 Handle ClassFileParser::clear_cp_patch_at(int index) { 798 Handle patch = cp_patch_at(index); 799 _cp_patches->at_put(index, Handle()); 800 assert(!has_cp_patch_at(index), ""); 801 return patch; 802 } 803 804 void ClassFileParser::patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name) { 805 int name_index = _orig_cp_size + _num_patched_klasses; 806 int resolved_klass_index = _first_patched_klass_resolved_index + _num_patched_klasses; 807 808 cp->klass_at_put(class_index, name_index, resolved_klass_index, k, name); 809 _num_patched_klasses ++; 810 } 811 812 void ClassFileParser::patch_constant_pool(ConstantPool* cp, 813 int index, 814 Handle patch, 815 TRAPS) { 816 assert(cp != NULL, "invariant"); 817 818 BasicType patch_type = T_VOID; 819 820 switch (cp->tag_at(index).value()) { 821 822 case JVM_CONSTANT_UnresolvedClass: { 823 // Patching a class means pre-resolving it. 824 // The name in the constant pool is ignored. 825 if (java_lang_Class::is_instance(patch())) { 826 guarantee_property(!java_lang_Class::is_primitive(patch()), 827 "Illegal class patch at %d in class file %s", 828 index, CHECK); 829 Klass* k = java_lang_Class::as_Klass(patch()); 830 patch_class(cp, index, k, k->name()); 831 } else { 832 guarantee_property(java_lang_String::is_instance(patch()), 833 "Illegal class patch at %d in class file %s", 834 index, CHECK); 835 Symbol* const name = java_lang_String::as_symbol(patch(), CHECK); 836 patch_class(cp, index, NULL, name); 837 } 838 break; 839 } 840 841 case JVM_CONSTANT_String: { 842 // skip this patch and don't clear it. Needs the oop array for resolved 843 // references to be created first. 844 return; 845 } 846 case JVM_CONSTANT_Integer: patch_type = T_INT; goto patch_prim; 847 case JVM_CONSTANT_Float: patch_type = T_FLOAT; goto patch_prim; 848 case JVM_CONSTANT_Long: patch_type = T_LONG; goto patch_prim; 849 case JVM_CONSTANT_Double: patch_type = T_DOUBLE; goto patch_prim; 850 patch_prim: 851 { 852 jvalue value; 853 BasicType value_type = java_lang_boxing_object::get_value(patch(), &value); 854 guarantee_property(value_type == patch_type, 855 "Illegal primitive patch at %d in class file %s", 856 index, CHECK); 857 switch (value_type) { 858 case T_INT: cp->int_at_put(index, value.i); break; 859 case T_FLOAT: cp->float_at_put(index, value.f); break; 860 case T_LONG: cp->long_at_put(index, value.j); break; 861 case T_DOUBLE: cp->double_at_put(index, value.d); break; 862 default: assert(false, ""); 863 } 864 } // end patch_prim label 865 break; 866 867 default: { 868 // %%% TODO: put method handles into CONSTANT_InterfaceMethodref, etc. 869 guarantee_property(!has_cp_patch_at(index), 870 "Illegal unexpected patch at %d in class file %s", 871 index, CHECK); 872 return; 873 } 874 } // end of switch(tag) 875 876 // On fall-through, mark the patch as used. 877 clear_cp_patch_at(index); 878 } 879 class NameSigHash: public ResourceObj { 880 public: 881 const Symbol* _name; // name 882 const Symbol* _sig; // signature 883 NameSigHash* _next; // Next entry in hash table 884 }; 885 886 static const int HASH_ROW_SIZE = 256; 887 888 static unsigned int hash(const Symbol* name, const Symbol* sig) { 889 unsigned int raw_hash = 0; 890 raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2); 891 raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize; 892 893 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE; 894 } 895 896 897 static void initialize_hashtable(NameSigHash** table) { 898 memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE); 899 } 900 // Return false if the name/sig combination is found in table. 901 // Return true if no duplicate is found. And name/sig is added as a new entry in table. 902 // The old format checker uses heap sort to find duplicates. 903 // NOTE: caller should guarantee that GC doesn't happen during the life cycle 904 // of table since we don't expect Symbol*'s to move. 905 static bool put_after_lookup(const Symbol* name, const Symbol* sig, NameSigHash** table) { 906 assert(name != NULL, "name in constant pool is NULL"); 907 908 // First lookup for duplicates 909 int index = hash(name, sig); 910 NameSigHash* entry = table[index]; 911 while (entry != NULL) { 912 if (entry->_name == name && entry->_sig == sig) { 913 return false; 914 } 915 entry = entry->_next; 916 } 917 918 // No duplicate is found, allocate a new entry and fill it. 919 entry = new NameSigHash(); 920 entry->_name = name; 921 entry->_sig = sig; 922 923 // Insert into hash table 924 entry->_next = table[index]; 925 table[index] = entry; 926 927 return true; 928 } 929 930 // Side-effects: populates the _local_interfaces field 931 void ClassFileParser::parse_interfaces(const ClassFileStream* const stream, 932 const int itfs_len, 933 ConstantPool* const cp, 934 bool* const has_nonstatic_concrete_methods, 935 TRAPS) { 936 assert(stream != NULL, "invariant"); 937 assert(cp != NULL, "invariant"); 938 assert(has_nonstatic_concrete_methods != NULL, "invariant"); 939 940 if (itfs_len == 0) { 941 _local_interfaces = Universe::the_empty_instance_klass_array(); 942 } else { 943 assert(itfs_len > 0, "only called for len>0"); 944 _local_interfaces = MetadataFactory::new_array<InstanceKlass*>(_loader_data, itfs_len, NULL, CHECK); 945 946 int index; 947 for (index = 0; index < itfs_len; index++) { 948 const u2 interface_index = stream->get_u2(CHECK); 949 Klass* interf; 950 check_property( 951 valid_klass_reference_at(interface_index), 952 "Interface name has bad constant pool index %u in class file %s", 953 interface_index, CHECK); 954 if (cp->tag_at(interface_index).is_klass()) { 955 interf = cp->resolved_klass_at(interface_index); 956 } else { 957 Symbol* const unresolved_klass = cp->klass_name_at(interface_index); 958 959 // Don't need to check legal name because it's checked when parsing constant pool. 960 // But need to make sure it's not an array type. 961 guarantee_property(unresolved_klass->char_at(0) != JVM_SIGNATURE_ARRAY, 962 "Bad interface name in class file %s", CHECK); 963 964 // Call resolve_super so classcircularity is checked 965 interf = SystemDictionary::resolve_super_or_fail( 966 _class_name, 967 unresolved_klass, 968 Handle(THREAD, _loader_data->class_loader()), 969 _protection_domain, 970 false, 971 CHECK); 972 } 973 974 if (!interf->is_interface()) { 975 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 976 err_msg("class %s can not implement %s, because it is not an interface (%s)", 977 _class_name->as_klass_external_name(), 978 interf->external_name(), 979 interf->class_in_module_of_loader())); 980 } 981 982 if (InstanceKlass::cast(interf)->has_nonstatic_concrete_methods()) { 983 *has_nonstatic_concrete_methods = true; 984 } 985 _local_interfaces->at_put(index, InstanceKlass::cast(interf)); 986 } 987 988 if (!_need_verify || itfs_len <= 1) { 989 return; 990 } 991 992 // Check if there's any duplicates in interfaces 993 ResourceMark rm(THREAD); 994 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 995 NameSigHash*, 996 HASH_ROW_SIZE); 997 initialize_hashtable(interface_names); 998 bool dup = false; 999 const Symbol* name = NULL; 1000 { 1001 debug_only(NoSafepointVerifier nsv;) 1002 for (index = 0; index < itfs_len; index++) { 1003 const InstanceKlass* const k = _local_interfaces->at(index); 1004 name = k->name(); 1005 // If no duplicates, add (name, NULL) in hashtable interface_names. 1006 if (!put_after_lookup(name, NULL, interface_names)) { 1007 dup = true; 1008 break; 1009 } 1010 } 1011 } 1012 if (dup) { 1013 classfile_parse_error("Duplicate interface name \"%s\" in class file %s", 1014 name->as_C_string(), CHECK); 1015 } 1016 } 1017 } 1018 1019 void ClassFileParser::verify_constantvalue(const ConstantPool* const cp, 1020 int constantvalue_index, 1021 int signature_index, 1022 TRAPS) const { 1023 // Make sure the constant pool entry is of a type appropriate to this field 1024 guarantee_property( 1025 (constantvalue_index > 0 && 1026 constantvalue_index < cp->length()), 1027 "Bad initial value index %u in ConstantValue attribute in class file %s", 1028 constantvalue_index, CHECK); 1029 1030 const constantTag value_type = cp->tag_at(constantvalue_index); 1031 switch(cp->basic_type_for_signature_at(signature_index)) { 1032 case T_LONG: { 1033 guarantee_property(value_type.is_long(), 1034 "Inconsistent constant value type in class file %s", 1035 CHECK); 1036 break; 1037 } 1038 case T_FLOAT: { 1039 guarantee_property(value_type.is_float(), 1040 "Inconsistent constant value type in class file %s", 1041 CHECK); 1042 break; 1043 } 1044 case T_DOUBLE: { 1045 guarantee_property(value_type.is_double(), 1046 "Inconsistent constant value type in class file %s", 1047 CHECK); 1048 break; 1049 } 1050 case T_BYTE: 1051 case T_CHAR: 1052 case T_SHORT: 1053 case T_BOOLEAN: 1054 case T_INT: { 1055 guarantee_property(value_type.is_int(), 1056 "Inconsistent constant value type in class file %s", 1057 CHECK); 1058 break; 1059 } 1060 case T_OBJECT: { 1061 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;") 1062 && value_type.is_string()), 1063 "Bad string initial value in class file %s", 1064 CHECK); 1065 break; 1066 } 1067 default: { 1068 classfile_parse_error("Unable to set initial value %u in class file %s", 1069 constantvalue_index, 1070 CHECK); 1071 } 1072 } 1073 } 1074 1075 class AnnotationCollector : public ResourceObj{ 1076 public: 1077 enum Location { _in_field, _in_method, _in_class }; 1078 enum ID { 1079 _unknown = 0, 1080 _method_CallerSensitive, 1081 _method_ForceInline, 1082 _method_DontInline, 1083 _method_InjectedProfile, 1084 _method_LambdaForm_Compiled, 1085 _method_Hidden, 1086 _method_HotSpotIntrinsicCandidate, 1087 _jdk_internal_vm_annotation_Contended, 1088 _field_Stable, 1089 _jdk_internal_vm_annotation_ReservedStackAccess, 1090 _annotation_LIMIT 1091 }; 1092 const Location _location; 1093 int _annotations_present; 1094 u2 _contended_group; 1095 1096 AnnotationCollector(Location location) 1097 : _location(location), _annotations_present(0) 1098 { 1099 assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, ""); 1100 } 1101 // If this annotation name has an ID, report it (or _none). 1102 ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name); 1103 // Set the annotation name: 1104 void set_annotation(ID id) { 1105 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 1106 _annotations_present |= nth_bit((int)id); 1107 } 1108 1109 void remove_annotation(ID id) { 1110 assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob"); 1111 _annotations_present &= ~nth_bit((int)id); 1112 } 1113 1114 // Report if the annotation is present. 1115 bool has_any_annotations() const { return _annotations_present != 0; } 1116 bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; } 1117 1118 void set_contended_group(u2 group) { _contended_group = group; } 1119 u2 contended_group() const { return _contended_group; } 1120 1121 bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); } 1122 1123 void set_stable(bool stable) { set_annotation(_field_Stable); } 1124 bool is_stable() const { return has_annotation(_field_Stable); } 1125 }; 1126 1127 // This class also doubles as a holder for metadata cleanup. 1128 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector { 1129 private: 1130 ClassLoaderData* _loader_data; 1131 AnnotationArray* _field_annotations; 1132 AnnotationArray* _field_type_annotations; 1133 public: 1134 FieldAnnotationCollector(ClassLoaderData* loader_data) : 1135 AnnotationCollector(_in_field), 1136 _loader_data(loader_data), 1137 _field_annotations(NULL), 1138 _field_type_annotations(NULL) {} 1139 ~FieldAnnotationCollector(); 1140 void apply_to(FieldInfo* f); 1141 AnnotationArray* field_annotations() { return _field_annotations; } 1142 AnnotationArray* field_type_annotations() { return _field_type_annotations; } 1143 1144 void set_field_annotations(AnnotationArray* a) { _field_annotations = a; } 1145 void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; } 1146 }; 1147 1148 class MethodAnnotationCollector : public AnnotationCollector{ 1149 public: 1150 MethodAnnotationCollector() : AnnotationCollector(_in_method) { } 1151 void apply_to(const methodHandle& m); 1152 }; 1153 1154 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{ 1155 public: 1156 ClassAnnotationCollector() : AnnotationCollector(_in_class) { } 1157 void apply_to(InstanceKlass* ik); 1158 }; 1159 1160 1161 static int skip_annotation_value(const u1*, int, int); // fwd decl 1162 1163 // Safely increment index by val if does not pass limit 1164 #define SAFE_ADD(index, limit, val) \ 1165 if (index >= limit - val) return limit; \ 1166 index += val; 1167 1168 // Skip an annotation. Return >=limit if there is any problem. 1169 static int skip_annotation(const u1* buffer, int limit, int index) { 1170 assert(buffer != NULL, "invariant"); 1171 // annotation := atype:u2 do(nmem:u2) {member:u2 value} 1172 // value := switch (tag:u1) { ... } 1173 SAFE_ADD(index, limit, 4); // skip atype and read nmem 1174 int nmem = Bytes::get_Java_u2((address)buffer + index - 2); 1175 while (--nmem >= 0 && index < limit) { 1176 SAFE_ADD(index, limit, 2); // skip member 1177 index = skip_annotation_value(buffer, limit, index); 1178 } 1179 return index; 1180 } 1181 1182 // Skip an annotation value. Return >=limit if there is any problem. 1183 static int skip_annotation_value(const u1* buffer, int limit, int index) { 1184 assert(buffer != NULL, "invariant"); 1185 1186 // value := switch (tag:u1) { 1187 // case B, C, I, S, Z, D, F, J, c: con:u2; 1188 // case e: e_class:u2 e_name:u2; 1189 // case s: s_con:u2; 1190 // case [: do(nval:u2) {value}; 1191 // case @: annotation; 1192 // case s: s_con:u2; 1193 // } 1194 SAFE_ADD(index, limit, 1); // read tag 1195 const u1 tag = buffer[index - 1]; 1196 switch (tag) { 1197 case 'B': 1198 case 'C': 1199 case 'I': 1200 case 'S': 1201 case 'Z': 1202 case 'D': 1203 case 'F': 1204 case 'J': 1205 case 'c': 1206 case 's': 1207 SAFE_ADD(index, limit, 2); // skip con or s_con 1208 break; 1209 case 'e': 1210 SAFE_ADD(index, limit, 4); // skip e_class, e_name 1211 break; 1212 case '[': 1213 { 1214 SAFE_ADD(index, limit, 2); // read nval 1215 int nval = Bytes::get_Java_u2((address)buffer + index - 2); 1216 while (--nval >= 0 && index < limit) { 1217 index = skip_annotation_value(buffer, limit, index); 1218 } 1219 } 1220 break; 1221 case '@': 1222 index = skip_annotation(buffer, limit, index); 1223 break; 1224 default: 1225 return limit; // bad tag byte 1226 } 1227 return index; 1228 } 1229 1230 // Sift through annotations, looking for those significant to the VM: 1231 static void parse_annotations(const ConstantPool* const cp, 1232 const u1* buffer, int limit, 1233 AnnotationCollector* coll, 1234 ClassLoaderData* loader_data, 1235 TRAPS) { 1236 1237 assert(cp != NULL, "invariant"); 1238 assert(buffer != NULL, "invariant"); 1239 assert(coll != NULL, "invariant"); 1240 assert(loader_data != NULL, "invariant"); 1241 1242 // annotations := do(nann:u2) {annotation} 1243 int index = 2; // read nann 1244 if (index >= limit) return; 1245 int nann = Bytes::get_Java_u2((address)buffer + index - 2); 1246 enum { // initial annotation layout 1247 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;' 1248 count_off = 2, // u2 such as 1 (one value) 1249 member_off = 4, // utf8 such as 'value' 1250 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum) 1251 e_tag_val = 'e', 1252 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;' 1253 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME' 1254 e_size = 11, // end of 'e' annotation 1255 c_tag_val = 'c', // payload is type 1256 c_con_off = 7, // utf8 payload, such as 'I' 1257 c_size = 9, // end of 'c' annotation 1258 s_tag_val = 's', // payload is String 1259 s_con_off = 7, // utf8 payload, such as 'Ljava/lang/String;' 1260 s_size = 9, 1261 min_size = 6 // smallest possible size (zero members) 1262 }; 1263 // Cannot add min_size to index in case of overflow MAX_INT 1264 while ((--nann) >= 0 && (index - 2 <= limit - min_size)) { 1265 int index0 = index; 1266 index = skip_annotation(buffer, limit, index); 1267 const u1* const abase = buffer + index0; 1268 const int atype = Bytes::get_Java_u2((address)abase + atype_off); 1269 const int count = Bytes::get_Java_u2((address)abase + count_off); 1270 const Symbol* const aname = check_symbol_at(cp, atype); 1271 if (aname == NULL) break; // invalid annotation name 1272 const Symbol* member = NULL; 1273 if (count >= 1) { 1274 const int member_index = Bytes::get_Java_u2((address)abase + member_off); 1275 member = check_symbol_at(cp, member_index); 1276 if (member == NULL) break; // invalid member name 1277 } 1278 1279 // Here is where parsing particular annotations will take place. 1280 AnnotationCollector::ID id = coll->annotation_index(loader_data, aname); 1281 if (AnnotationCollector::_unknown == id) continue; 1282 coll->set_annotation(id); 1283 1284 if (AnnotationCollector::_jdk_internal_vm_annotation_Contended == id) { 1285 // @Contended can optionally specify the contention group. 1286 // 1287 // Contended group defines the equivalence class over the fields: 1288 // the fields within the same contended group are not treated distinct. 1289 // The only exception is default group, which does not incur the 1290 // equivalence. Naturally, contention group for classes is meaningless. 1291 // 1292 // While the contention group is specified as String, annotation 1293 // values are already interned, and we might as well use the constant 1294 // pool index as the group tag. 1295 // 1296 u2 group_index = 0; // default contended group 1297 if (count == 1 1298 && s_size == (index - index0) // match size 1299 && s_tag_val == *(abase + tag_off) 1300 && member == vmSymbols::value_name()) { 1301 group_index = Bytes::get_Java_u2((address)abase + s_con_off); 1302 if (cp->symbol_at(group_index)->utf8_length() == 0) { 1303 group_index = 0; // default contended group 1304 } 1305 } 1306 coll->set_contended_group(group_index); 1307 } 1308 } 1309 } 1310 1311 1312 // Parse attributes for a field. 1313 void ClassFileParser::parse_field_attributes(const ClassFileStream* const cfs, 1314 u2 attributes_count, 1315 bool is_static, u2 signature_index, 1316 u2* const constantvalue_index_addr, 1317 bool* const is_synthetic_addr, 1318 u2* const generic_signature_index_addr, 1319 ClassFileParser::FieldAnnotationCollector* parsed_annotations, 1320 TRAPS) { 1321 assert(cfs != NULL, "invariant"); 1322 assert(constantvalue_index_addr != NULL, "invariant"); 1323 assert(is_synthetic_addr != NULL, "invariant"); 1324 assert(generic_signature_index_addr != NULL, "invariant"); 1325 assert(parsed_annotations != NULL, "invariant"); 1326 assert(attributes_count > 0, "attributes_count should be greater than 0"); 1327 1328 u2 constantvalue_index = 0; 1329 u2 generic_signature_index = 0; 1330 bool is_synthetic = false; 1331 const u1* runtime_visible_annotations = NULL; 1332 int runtime_visible_annotations_length = 0; 1333 const u1* runtime_invisible_annotations = NULL; 1334 int runtime_invisible_annotations_length = 0; 1335 const u1* runtime_visible_type_annotations = NULL; 1336 int runtime_visible_type_annotations_length = 0; 1337 const u1* runtime_invisible_type_annotations = NULL; 1338 int runtime_invisible_type_annotations_length = 0; 1339 bool runtime_invisible_annotations_exists = false; 1340 bool runtime_invisible_type_annotations_exists = false; 1341 const ConstantPool* const cp = _cp; 1342 1343 while (attributes_count--) { 1344 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 1345 const u2 attribute_name_index = cfs->get_u2_fast(); 1346 const u4 attribute_length = cfs->get_u4_fast(); 1347 check_property(valid_symbol_at(attribute_name_index), 1348 "Invalid field attribute index %u in class file %s", 1349 attribute_name_index, 1350 CHECK); 1351 1352 const Symbol* const attribute_name = cp->symbol_at(attribute_name_index); 1353 if (is_static && attribute_name == vmSymbols::tag_constant_value()) { 1354 // ignore if non-static 1355 if (constantvalue_index != 0) { 1356 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK); 1357 } 1358 check_property( 1359 attribute_length == 2, 1360 "Invalid ConstantValue field attribute length %u in class file %s", 1361 attribute_length, CHECK); 1362 1363 constantvalue_index = cfs->get_u2(CHECK); 1364 if (_need_verify) { 1365 verify_constantvalue(cp, constantvalue_index, signature_index, CHECK); 1366 } 1367 } else if (attribute_name == vmSymbols::tag_synthetic()) { 1368 if (attribute_length != 0) { 1369 classfile_parse_error( 1370 "Invalid Synthetic field attribute length %u in class file %s", 1371 attribute_length, CHECK); 1372 } 1373 is_synthetic = true; 1374 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120 1375 if (attribute_length != 0) { 1376 classfile_parse_error( 1377 "Invalid Deprecated field attribute length %u in class file %s", 1378 attribute_length, CHECK); 1379 } 1380 } else if (_major_version >= JAVA_1_5_VERSION) { 1381 if (attribute_name == vmSymbols::tag_signature()) { 1382 if (generic_signature_index != 0) { 1383 classfile_parse_error( 1384 "Multiple Signature attributes for field in class file %s", CHECK); 1385 } 1386 if (attribute_length != 2) { 1387 classfile_parse_error( 1388 "Wrong size %u for field's Signature attribute in class file %s", 1389 attribute_length, CHECK); 1390 } 1391 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK); 1392 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 1393 if (runtime_visible_annotations != NULL) { 1394 classfile_parse_error( 1395 "Multiple RuntimeVisibleAnnotations attributes for field in class file %s", CHECK); 1396 } 1397 runtime_visible_annotations_length = attribute_length; 1398 runtime_visible_annotations = cfs->current(); 1399 assert(runtime_visible_annotations != NULL, "null visible annotations"); 1400 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 1401 parse_annotations(cp, 1402 runtime_visible_annotations, 1403 runtime_visible_annotations_length, 1404 parsed_annotations, 1405 _loader_data, 1406 CHECK); 1407 cfs->skip_u1_fast(runtime_visible_annotations_length); 1408 } else if (attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 1409 if (runtime_invisible_annotations_exists) { 1410 classfile_parse_error( 1411 "Multiple RuntimeInvisibleAnnotations attributes for field in class file %s", CHECK); 1412 } 1413 runtime_invisible_annotations_exists = true; 1414 if (PreserveAllAnnotations) { 1415 runtime_invisible_annotations_length = attribute_length; 1416 runtime_invisible_annotations = cfs->current(); 1417 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 1418 } 1419 cfs->skip_u1(attribute_length, CHECK); 1420 } else if (attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 1421 if (runtime_visible_type_annotations != NULL) { 1422 classfile_parse_error( 1423 "Multiple RuntimeVisibleTypeAnnotations attributes for field in class file %s", CHECK); 1424 } 1425 runtime_visible_type_annotations_length = attribute_length; 1426 runtime_visible_type_annotations = cfs->current(); 1427 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 1428 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 1429 } else if (attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 1430 if (runtime_invisible_type_annotations_exists) { 1431 classfile_parse_error( 1432 "Multiple RuntimeInvisibleTypeAnnotations attributes for field in class file %s", CHECK); 1433 } else { 1434 runtime_invisible_type_annotations_exists = true; 1435 } 1436 if (PreserveAllAnnotations) { 1437 runtime_invisible_type_annotations_length = attribute_length; 1438 runtime_invisible_type_annotations = cfs->current(); 1439 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 1440 } 1441 cfs->skip_u1(attribute_length, CHECK); 1442 } else { 1443 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1444 } 1445 } else { 1446 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes 1447 } 1448 } 1449 1450 *constantvalue_index_addr = constantvalue_index; 1451 *is_synthetic_addr = is_synthetic; 1452 *generic_signature_index_addr = generic_signature_index; 1453 AnnotationArray* a = assemble_annotations(runtime_visible_annotations, 1454 runtime_visible_annotations_length, 1455 runtime_invisible_annotations, 1456 runtime_invisible_annotations_length, 1457 CHECK); 1458 parsed_annotations->set_field_annotations(a); 1459 a = assemble_annotations(runtime_visible_type_annotations, 1460 runtime_visible_type_annotations_length, 1461 runtime_invisible_type_annotations, 1462 runtime_invisible_type_annotations_length, 1463 CHECK); 1464 parsed_annotations->set_field_type_annotations(a); 1465 return; 1466 } 1467 1468 1469 // Field allocation types. Used for computing field offsets. 1470 1471 enum FieldAllocationType { 1472 STATIC_OOP, // Oops 1473 STATIC_BYTE, // Boolean, Byte, char 1474 STATIC_SHORT, // shorts 1475 STATIC_WORD, // ints 1476 STATIC_DOUBLE, // aligned long or double 1477 STATIC_FLATTENABLE, // flattenable field 1478 NONSTATIC_OOP, 1479 NONSTATIC_BYTE, 1480 NONSTATIC_SHORT, 1481 NONSTATIC_WORD, 1482 NONSTATIC_DOUBLE, 1483 NONSTATIC_FLATTENABLE, 1484 MAX_FIELD_ALLOCATION_TYPE, 1485 BAD_ALLOCATION_TYPE = -1 1486 }; 1487 1488 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = { 1489 BAD_ALLOCATION_TYPE, // 0 1490 BAD_ALLOCATION_TYPE, // 1 1491 BAD_ALLOCATION_TYPE, // 2 1492 BAD_ALLOCATION_TYPE, // 3 1493 NONSTATIC_BYTE , // T_BOOLEAN = 4, 1494 NONSTATIC_SHORT, // T_CHAR = 5, 1495 NONSTATIC_WORD, // T_FLOAT = 6, 1496 NONSTATIC_DOUBLE, // T_DOUBLE = 7, 1497 NONSTATIC_BYTE, // T_BYTE = 8, 1498 NONSTATIC_SHORT, // T_SHORT = 9, 1499 NONSTATIC_WORD, // T_INT = 10, 1500 NONSTATIC_DOUBLE, // T_LONG = 11, 1501 NONSTATIC_OOP, // T_OBJECT = 12, 1502 NONSTATIC_OOP, // T_ARRAY = 13, 1503 NONSTATIC_OOP, // T_VALUETYPE = 14, 1504 BAD_ALLOCATION_TYPE, // T_VOID = 15, 1505 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16, 1506 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17, 1507 BAD_ALLOCATION_TYPE, // T_METADATA = 18, 1508 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19, 1509 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20, 1510 BAD_ALLOCATION_TYPE, // 0 1511 BAD_ALLOCATION_TYPE, // 1 1512 BAD_ALLOCATION_TYPE, // 2 1513 BAD_ALLOCATION_TYPE, // 3 1514 STATIC_BYTE , // T_BOOLEAN = 4, 1515 STATIC_SHORT, // T_CHAR = 5, 1516 STATIC_WORD, // T_FLOAT = 6, 1517 STATIC_DOUBLE, // T_DOUBLE = 7, 1518 STATIC_BYTE, // T_BYTE = 8, 1519 STATIC_SHORT, // T_SHORT = 9, 1520 STATIC_WORD, // T_INT = 10, 1521 STATIC_DOUBLE, // T_LONG = 11, 1522 STATIC_OOP, // T_OBJECT = 12, 1523 STATIC_OOP, // T_ARRAY = 13, 1524 STATIC_OOP, // T_VALUETYPE = 14, 1525 BAD_ALLOCATION_TYPE, // T_VOID = 15, 1526 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16, 1527 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17, 1528 BAD_ALLOCATION_TYPE, // T_METADATA = 18, 1529 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19, 1530 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20 1531 }; 1532 1533 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type, bool is_flattenable) { 1534 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values"); 1535 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)]; 1536 assert(result != BAD_ALLOCATION_TYPE, "bad type"); 1537 if (is_flattenable) { 1538 result = is_static ? STATIC_FLATTENABLE : NONSTATIC_FLATTENABLE; 1539 } 1540 return result; 1541 } 1542 1543 class ClassFileParser::FieldAllocationCount : public ResourceObj { 1544 public: 1545 u2 count[MAX_FIELD_ALLOCATION_TYPE]; 1546 1547 FieldAllocationCount() { 1548 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) { 1549 count[i] = 0; 1550 } 1551 } 1552 1553 FieldAllocationType update(bool is_static, BasicType type, bool is_flattenable) { 1554 FieldAllocationType atype = basic_type_to_atype(is_static, type, is_flattenable); 1555 if (atype != BAD_ALLOCATION_TYPE) { 1556 // Make sure there is no overflow with injected fields. 1557 assert(count[atype] < 0xFFFF, "More than 65535 fields"); 1558 count[atype]++; 1559 } 1560 return atype; 1561 } 1562 }; 1563 1564 // Side-effects: populates the _fields, _fields_annotations, 1565 // _fields_type_annotations fields 1566 void ClassFileParser::parse_fields(const ClassFileStream* const cfs, 1567 bool is_interface, 1568 bool is_value_type, 1569 FieldAllocationCount* const fac, 1570 ConstantPool* cp, 1571 const int cp_size, 1572 u2* const java_fields_count_ptr, 1573 TRAPS) { 1574 1575 assert(cfs != NULL, "invariant"); 1576 assert(fac != NULL, "invariant"); 1577 assert(cp != NULL, "invariant"); 1578 assert(java_fields_count_ptr != NULL, "invariant"); 1579 1580 assert(NULL == _fields, "invariant"); 1581 assert(NULL == _fields_annotations, "invariant"); 1582 assert(NULL == _fields_type_annotations, "invariant"); 1583 1584 cfs->guarantee_more(2, CHECK); // length 1585 const u2 length = cfs->get_u2_fast(); 1586 *java_fields_count_ptr = length; 1587 1588 int num_injected = 0; 1589 const InjectedField* const injected = JavaClasses::get_injected(_class_name, 1590 &num_injected); 1591 1592 const int total_fields = length + num_injected + (is_value_type ? 1 : 0); 1593 1594 // The field array starts with tuples of shorts 1595 // [access, name index, sig index, initial value index, byte offset]. 1596 // A generic signature slot only exists for field with generic 1597 // signature attribute. And the access flag is set with 1598 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic 1599 // signature slots are at the end of the field array and after all 1600 // other fields data. 1601 // 1602 // f1: [access, name index, sig index, initial value index, low_offset, high_offset] 1603 // f2: [access, name index, sig index, initial value index, low_offset, high_offset] 1604 // ... 1605 // fn: [access, name index, sig index, initial value index, low_offset, high_offset] 1606 // [generic signature index] 1607 // [generic signature index] 1608 // ... 1609 // 1610 // Allocate a temporary resource array for field data. For each field, 1611 // a slot is reserved in the temporary array for the generic signature 1612 // index. After parsing all fields, the data are copied to a permanent 1613 // array and any unused slots will be discarded. 1614 ResourceMark rm(THREAD); 1615 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, 1616 u2, 1617 total_fields * (FieldInfo::field_slots + 1)); 1618 1619 // The generic signature slots start after all other fields' data. 1620 int generic_signature_slot = total_fields * FieldInfo::field_slots; 1621 int num_generic_signature = 0; 1622 for (int n = 0; n < length; n++) { 1623 // access_flags, name_index, descriptor_index, attributes_count 1624 cfs->guarantee_more(8, CHECK); 1625 1626 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS; 1627 1628 const jint flags = cfs->get_u2_fast() & recognized_modifiers; 1629 verify_legal_field_modifiers(flags, is_interface, is_value_type, CHECK); 1630 AccessFlags access_flags; 1631 access_flags.set_flags(flags); 1632 1633 const u2 name_index = cfs->get_u2_fast(); 1634 check_property(valid_symbol_at(name_index), 1635 "Invalid constant pool index %u for field name in class file %s", 1636 name_index, CHECK); 1637 const Symbol* const name = cp->symbol_at(name_index); 1638 verify_legal_field_name(name, CHECK); 1639 1640 const u2 signature_index = cfs->get_u2_fast(); 1641 check_property(valid_symbol_at(signature_index), 1642 "Invalid constant pool index %u for field signature in class file %s", 1643 signature_index, CHECK); 1644 const Symbol* const sig = cp->symbol_at(signature_index); 1645 verify_legal_field_signature(name, sig, CHECK); 1646 assert(!access_flags.is_flattenable(), "ACC_FLATTENABLE should have been filtered out"); 1647 if (sig->is_Q_signature()) { 1648 // assert(_major_version >= CONSTANT_CLASS_DESCRIPTORS, "Q-descriptors are only supported in recent classfiles"); 1649 access_flags.set_is_flattenable(); 1650 } 1651 if (access_flags.is_flattenable()) { 1652 // Array flattenability cannot be specified. Arrays of value classes are 1653 // are always flattenable. Arrays of other classes are not flattenable. 1654 if (sig->utf8_length() > 1 && sig->char_at(0) == '[') { 1655 classfile_parse_error( 1656 "Field \"%s\" with signature \"%s\" in class file %s is invalid." 1657 " ACC_FLATTENABLE cannot be specified for an array", 1658 name->as_C_string(), sig->as_klass_external_name(), CHECK); 1659 } 1660 _has_flattenable_fields = true; 1661 } 1662 1663 u2 constantvalue_index = 0; 1664 bool is_synthetic = false; 1665 u2 generic_signature_index = 0; 1666 const bool is_static = access_flags.is_static(); 1667 FieldAnnotationCollector parsed_annotations(_loader_data); 1668 1669 const u2 attributes_count = cfs->get_u2_fast(); 1670 if (attributes_count > 0) { 1671 parse_field_attributes(cfs, 1672 attributes_count, 1673 is_static, 1674 signature_index, 1675 &constantvalue_index, 1676 &is_synthetic, 1677 &generic_signature_index, 1678 &parsed_annotations, 1679 CHECK); 1680 1681 if (parsed_annotations.field_annotations() != NULL) { 1682 if (_fields_annotations == NULL) { 1683 _fields_annotations = MetadataFactory::new_array<AnnotationArray*>( 1684 _loader_data, length, NULL, 1685 CHECK); 1686 } 1687 _fields_annotations->at_put(n, parsed_annotations.field_annotations()); 1688 parsed_annotations.set_field_annotations(NULL); 1689 } 1690 if (parsed_annotations.field_type_annotations() != NULL) { 1691 if (_fields_type_annotations == NULL) { 1692 _fields_type_annotations = 1693 MetadataFactory::new_array<AnnotationArray*>(_loader_data, 1694 length, 1695 NULL, 1696 CHECK); 1697 } 1698 _fields_type_annotations->at_put(n, parsed_annotations.field_type_annotations()); 1699 parsed_annotations.set_field_type_annotations(NULL); 1700 } 1701 1702 if (is_synthetic) { 1703 access_flags.set_is_synthetic(); 1704 } 1705 if (generic_signature_index != 0) { 1706 access_flags.set_field_has_generic_signature(); 1707 fa[generic_signature_slot] = generic_signature_index; 1708 generic_signature_slot ++; 1709 num_generic_signature ++; 1710 } 1711 } 1712 1713 FieldInfo* const field = FieldInfo::from_field_array(fa, n); 1714 field->initialize(access_flags.as_short(), 1715 name_index, 1716 signature_index, 1717 constantvalue_index); 1718 const BasicType type = cp->basic_type_for_signature_at(signature_index); 1719 1720 // Remember how many oops we encountered and compute allocation type 1721 const FieldAllocationType atype = fac->update(is_static, type, access_flags.is_flattenable()); 1722 field->set_allocation_type(atype); 1723 1724 // After field is initialized with type, we can augment it with aux info 1725 if (parsed_annotations.has_any_annotations()) 1726 parsed_annotations.apply_to(field); 1727 } 1728 1729 int index = length; 1730 if (num_injected != 0) { 1731 for (int n = 0; n < num_injected; n++) { 1732 // Check for duplicates 1733 if (injected[n].may_be_java) { 1734 const Symbol* const name = injected[n].name(); 1735 const Symbol* const signature = injected[n].signature(); 1736 bool duplicate = false; 1737 for (int i = 0; i < length; i++) { 1738 const FieldInfo* const f = FieldInfo::from_field_array(fa, i); 1739 if (name == cp->symbol_at(f->name_index()) && 1740 signature == cp->symbol_at(f->signature_index())) { 1741 // Symbol is desclared in Java so skip this one 1742 duplicate = true; 1743 break; 1744 } 1745 } 1746 if (duplicate) { 1747 // These will be removed from the field array at the end 1748 continue; 1749 } 1750 } 1751 1752 // Injected field 1753 FieldInfo* const field = FieldInfo::from_field_array(fa, index); 1754 field->initialize(JVM_ACC_FIELD_INTERNAL, 1755 injected[n].name_index, 1756 injected[n].signature_index, 1757 0); 1758 1759 const BasicType type = FieldType::basic_type(injected[n].signature()); 1760 1761 // Remember how many oops we encountered and compute allocation type 1762 const FieldAllocationType atype = fac->update(false, type, false); 1763 field->set_allocation_type(atype); 1764 index++; 1765 } 1766 } 1767 1768 if (is_value_type) { 1769 index = length + num_injected; 1770 FieldInfo* const field = FieldInfo::from_field_array(fa, index); 1771 field->initialize(JVM_ACC_FIELD_INTERNAL | JVM_ACC_STATIC, 1772 vmSymbols::default_value_name_enum, 1773 vmSymbols::java_lang_Object_enum, 1774 0); 1775 const BasicType type = FieldType::basic_type(vmSymbols::object_signature()); 1776 const FieldAllocationType atype = fac->update(true, type, false); 1777 field->set_allocation_type(atype); 1778 index++; 1779 } 1780 1781 assert(NULL == _fields, "invariant"); 1782 1783 _fields = 1784 MetadataFactory::new_array<u2>(_loader_data, 1785 index * FieldInfo::field_slots + num_generic_signature, 1786 CHECK); 1787 // Sometimes injected fields already exist in the Java source so 1788 // the fields array could be too long. In that case the 1789 // fields array is trimed. Also unused slots that were reserved 1790 // for generic signature indexes are discarded. 1791 { 1792 int i = 0; 1793 for (; i < index * FieldInfo::field_slots; i++) { 1794 _fields->at_put(i, fa[i]); 1795 } 1796 for (int j = total_fields * FieldInfo::field_slots; 1797 j < generic_signature_slot; j++) { 1798 _fields->at_put(i++, fa[j]); 1799 } 1800 assert(_fields->length() == i, ""); 1801 } 1802 1803 if (_need_verify && length > 1) { 1804 // Check duplicated fields 1805 ResourceMark rm(THREAD); 1806 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 1807 THREAD, NameSigHash*, HASH_ROW_SIZE); 1808 initialize_hashtable(names_and_sigs); 1809 bool dup = false; 1810 const Symbol* name = NULL; 1811 const Symbol* sig = NULL; 1812 { 1813 debug_only(NoSafepointVerifier nsv;) 1814 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 1815 name = fs.name(); 1816 sig = fs.signature(); 1817 // If no duplicates, add name/signature in hashtable names_and_sigs. 1818 if (!put_after_lookup(name, sig, names_and_sigs)) { 1819 dup = true; 1820 break; 1821 } 1822 } 1823 } 1824 if (dup) { 1825 classfile_parse_error("Duplicate field name \"%s\" with signature \"%s\" in class file %s", 1826 name->as_C_string(), sig->as_klass_external_name(), CHECK); 1827 } 1828 } 1829 } 1830 1831 1832 const ClassFileParser::unsafe_u2* ClassFileParser::parse_exception_table(const ClassFileStream* const cfs, 1833 u4 code_length, 1834 u4 exception_table_length, 1835 TRAPS) { 1836 assert(cfs != NULL, "invariant"); 1837 1838 const unsafe_u2* const exception_table_start = cfs->current(); 1839 assert(exception_table_start != NULL, "null exception table"); 1840 1841 cfs->guarantee_more(8 * exception_table_length, CHECK_NULL); // start_pc, 1842 // end_pc, 1843 // handler_pc, 1844 // catch_type_index 1845 1846 // Will check legal target after parsing code array in verifier. 1847 if (_need_verify) { 1848 for (unsigned int i = 0; i < exception_table_length; i++) { 1849 const u2 start_pc = cfs->get_u2_fast(); 1850 const u2 end_pc = cfs->get_u2_fast(); 1851 const u2 handler_pc = cfs->get_u2_fast(); 1852 const u2 catch_type_index = cfs->get_u2_fast(); 1853 guarantee_property((start_pc < end_pc) && (end_pc <= code_length), 1854 "Illegal exception table range in class file %s", 1855 CHECK_NULL); 1856 guarantee_property(handler_pc < code_length, 1857 "Illegal exception table handler in class file %s", 1858 CHECK_NULL); 1859 if (catch_type_index != 0) { 1860 guarantee_property(valid_klass_reference_at(catch_type_index), 1861 "Catch type in exception table has bad constant type in class file %s", CHECK_NULL); 1862 } 1863 } 1864 } else { 1865 cfs->skip_u2_fast(exception_table_length * 4); 1866 } 1867 return exception_table_start; 1868 } 1869 1870 void ClassFileParser::parse_linenumber_table(u4 code_attribute_length, 1871 u4 code_length, 1872 CompressedLineNumberWriteStream**const write_stream, 1873 TRAPS) { 1874 1875 const ClassFileStream* const cfs = _stream; 1876 unsigned int num_entries = cfs->get_u2(CHECK); 1877 1878 // Each entry is a u2 start_pc, and a u2 line_number 1879 const unsigned int length_in_bytes = num_entries * (sizeof(u2) * 2); 1880 1881 // Verify line number attribute and table length 1882 check_property( 1883 code_attribute_length == sizeof(u2) + length_in_bytes, 1884 "LineNumberTable attribute has wrong length in class file %s", CHECK); 1885 1886 cfs->guarantee_more(length_in_bytes, CHECK); 1887 1888 if ((*write_stream) == NULL) { 1889 if (length_in_bytes > fixed_buffer_size) { 1890 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes); 1891 } else { 1892 (*write_stream) = new CompressedLineNumberWriteStream( 1893 _linenumbertable_buffer, fixed_buffer_size); 1894 } 1895 } 1896 1897 while (num_entries-- > 0) { 1898 const u2 bci = cfs->get_u2_fast(); // start_pc 1899 const u2 line = cfs->get_u2_fast(); // line_number 1900 guarantee_property(bci < code_length, 1901 "Invalid pc in LineNumberTable in class file %s", CHECK); 1902 (*write_stream)->write_pair(bci, line); 1903 } 1904 } 1905 1906 1907 class LVT_Hash : public AllStatic { 1908 public: 1909 1910 static bool equals(LocalVariableTableElement const& e0, LocalVariableTableElement const& e1) { 1911 /* 1912 * 3-tuple start_bci/length/slot has to be unique key, 1913 * so the following comparison seems to be redundant: 1914 * && elem->name_cp_index == entry->_elem->name_cp_index 1915 */ 1916 return (e0.start_bci == e1.start_bci && 1917 e0.length == e1.length && 1918 e0.name_cp_index == e1.name_cp_index && 1919 e0.slot == e1.slot); 1920 } 1921 1922 static unsigned int hash(LocalVariableTableElement const& e0) { 1923 unsigned int raw_hash = e0.start_bci; 1924 1925 raw_hash = e0.length + raw_hash * 37; 1926 raw_hash = e0.name_cp_index + raw_hash * 37; 1927 raw_hash = e0.slot + raw_hash * 37; 1928 1929 return raw_hash; 1930 } 1931 }; 1932 1933 1934 // Class file LocalVariableTable elements. 1935 class Classfile_LVT_Element { 1936 public: 1937 u2 start_bci; 1938 u2 length; 1939 u2 name_cp_index; 1940 u2 descriptor_cp_index; 1941 u2 slot; 1942 }; 1943 1944 static void copy_lvt_element(const Classfile_LVT_Element* const src, 1945 LocalVariableTableElement* const lvt) { 1946 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci); 1947 lvt->length = Bytes::get_Java_u2((u1*) &src->length); 1948 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index); 1949 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index); 1950 lvt->signature_cp_index = 0; 1951 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot); 1952 } 1953 1954 // Function is used to parse both attributes: 1955 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT) 1956 const ClassFileParser::unsafe_u2* ClassFileParser::parse_localvariable_table(const ClassFileStream* cfs, 1957 u4 code_length, 1958 u2 max_locals, 1959 u4 code_attribute_length, 1960 u2* const localvariable_table_length, 1961 bool isLVTT, 1962 TRAPS) { 1963 const char* const tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable"; 1964 *localvariable_table_length = cfs->get_u2(CHECK_NULL); 1965 const unsigned int size = 1966 (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2); 1967 1968 const ConstantPool* const cp = _cp; 1969 1970 // Verify local variable table attribute has right length 1971 if (_need_verify) { 1972 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)), 1973 "%s has wrong length in class file %s", tbl_name, CHECK_NULL); 1974 } 1975 1976 const unsafe_u2* const localvariable_table_start = cfs->current(); 1977 assert(localvariable_table_start != NULL, "null local variable table"); 1978 if (!_need_verify) { 1979 cfs->skip_u2_fast(size); 1980 } else { 1981 cfs->guarantee_more(size * 2, CHECK_NULL); 1982 for(int i = 0; i < (*localvariable_table_length); i++) { 1983 const u2 start_pc = cfs->get_u2_fast(); 1984 const u2 length = cfs->get_u2_fast(); 1985 const u2 name_index = cfs->get_u2_fast(); 1986 const u2 descriptor_index = cfs->get_u2_fast(); 1987 const u2 index = cfs->get_u2_fast(); 1988 // Assign to a u4 to avoid overflow 1989 const u4 end_pc = (u4)start_pc + (u4)length; 1990 1991 if (start_pc >= code_length) { 1992 classfile_parse_error( 1993 "Invalid start_pc %u in %s in class file %s", 1994 start_pc, tbl_name, CHECK_NULL); 1995 } 1996 if (end_pc > code_length) { 1997 classfile_parse_error( 1998 "Invalid length %u in %s in class file %s", 1999 length, tbl_name, CHECK_NULL); 2000 } 2001 const int cp_size = cp->length(); 2002 guarantee_property(valid_symbol_at(name_index), 2003 "Name index %u in %s has bad constant type in class file %s", 2004 name_index, tbl_name, CHECK_NULL); 2005 guarantee_property(valid_symbol_at(descriptor_index), 2006 "Signature index %u in %s has bad constant type in class file %s", 2007 descriptor_index, tbl_name, CHECK_NULL); 2008 2009 const Symbol* const name = cp->symbol_at(name_index); 2010 const Symbol* const sig = cp->symbol_at(descriptor_index); 2011 verify_legal_field_name(name, CHECK_NULL); 2012 u2 extra_slot = 0; 2013 if (!isLVTT) { 2014 verify_legal_field_signature(name, sig, CHECK_NULL); 2015 2016 // 4894874: check special cases for double and long local variables 2017 if (sig == vmSymbols::type_signature(T_DOUBLE) || 2018 sig == vmSymbols::type_signature(T_LONG)) { 2019 extra_slot = 1; 2020 } 2021 } 2022 guarantee_property((index + extra_slot) < max_locals, 2023 "Invalid index %u in %s in class file %s", 2024 index, tbl_name, CHECK_NULL); 2025 } 2026 } 2027 return localvariable_table_start; 2028 } 2029 2030 static const u1* parse_stackmap_table(const ClassFileStream* const cfs, 2031 u4 code_attribute_length, 2032 bool need_verify, 2033 TRAPS) { 2034 assert(cfs != NULL, "invariant"); 2035 2036 if (0 == code_attribute_length) { 2037 return NULL; 2038 } 2039 2040 const u1* const stackmap_table_start = cfs->current(); 2041 assert(stackmap_table_start != NULL, "null stackmap table"); 2042 2043 // check code_attribute_length first 2044 cfs->skip_u1(code_attribute_length, CHECK_NULL); 2045 2046 if (!need_verify && !DumpSharedSpaces) { 2047 return NULL; 2048 } 2049 return stackmap_table_start; 2050 } 2051 2052 const ClassFileParser::unsafe_u2* ClassFileParser::parse_checked_exceptions(const ClassFileStream* const cfs, 2053 u2* const checked_exceptions_length, 2054 u4 method_attribute_length, 2055 TRAPS) { 2056 assert(cfs != NULL, "invariant"); 2057 assert(checked_exceptions_length != NULL, "invariant"); 2058 2059 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length 2060 *checked_exceptions_length = cfs->get_u2_fast(); 2061 const unsigned int size = 2062 (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2); 2063 const unsafe_u2* const checked_exceptions_start = cfs->current(); 2064 assert(checked_exceptions_start != NULL, "null checked exceptions"); 2065 if (!_need_verify) { 2066 cfs->skip_u2_fast(size); 2067 } else { 2068 // Verify each value in the checked exception table 2069 u2 checked_exception; 2070 const u2 len = *checked_exceptions_length; 2071 cfs->guarantee_more(2 * len, CHECK_NULL); 2072 for (int i = 0; i < len; i++) { 2073 checked_exception = cfs->get_u2_fast(); 2074 check_property( 2075 valid_klass_reference_at(checked_exception), 2076 "Exception name has bad type at constant pool %u in class file %s", 2077 checked_exception, CHECK_NULL); 2078 } 2079 } 2080 // check exceptions attribute length 2081 if (_need_verify) { 2082 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) + 2083 sizeof(u2) * size), 2084 "Exceptions attribute has wrong length in class file %s", CHECK_NULL); 2085 } 2086 return checked_exceptions_start; 2087 } 2088 2089 void ClassFileParser::throwIllegalSignature(const char* type, 2090 const Symbol* name, 2091 const Symbol* sig, 2092 TRAPS) const { 2093 assert(name != NULL, "invariant"); 2094 assert(sig != NULL, "invariant"); 2095 2096 ResourceMark rm(THREAD); 2097 Exceptions::fthrow(THREAD_AND_LOCATION, 2098 vmSymbols::java_lang_ClassFormatError(), 2099 "%s \"%s\" in class %s has illegal signature \"%s\"", type, 2100 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); 2101 } 2102 2103 AnnotationCollector::ID 2104 AnnotationCollector::annotation_index(const ClassLoaderData* loader_data, 2105 const Symbol* name) { 2106 const vmSymbols::SID sid = vmSymbols::find_sid(name); 2107 // Privileged code can use all annotations. Other code silently drops some. 2108 const bool privileged = loader_data->is_the_null_class_loader_data() || 2109 loader_data->is_platform_class_loader_data() || 2110 loader_data->is_unsafe_anonymous(); 2111 switch (sid) { 2112 case vmSymbols::VM_SYMBOL_ENUM_NAME(reflect_CallerSensitive_signature): { 2113 if (_location != _in_method) break; // only allow for methods 2114 if (!privileged) break; // only allow in privileged code 2115 return _method_CallerSensitive; 2116 } 2117 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ForceInline_signature): { 2118 if (_location != _in_method) break; // only allow for methods 2119 if (!privileged) break; // only allow in privileged code 2120 return _method_ForceInline; 2121 } 2122 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_DontInline_signature): { 2123 if (_location != _in_method) break; // only allow for methods 2124 if (!privileged) break; // only allow in privileged code 2125 return _method_DontInline; 2126 } 2127 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_InjectedProfile_signature): { 2128 if (_location != _in_method) break; // only allow for methods 2129 if (!privileged) break; // only allow in privileged code 2130 return _method_InjectedProfile; 2131 } 2132 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_LambdaForm_Compiled_signature): { 2133 if (_location != _in_method) break; // only allow for methods 2134 if (!privileged) break; // only allow in privileged code 2135 return _method_LambdaForm_Compiled; 2136 } 2137 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Hidden_signature): { 2138 if (_location != _in_method) break; // only allow for methods 2139 if (!privileged) break; // only allow in privileged code 2140 return _method_Hidden; 2141 } 2142 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_HotSpotIntrinsicCandidate_signature): { 2143 if (_location != _in_method) break; // only allow for methods 2144 if (!privileged) break; // only allow in privileged code 2145 return _method_HotSpotIntrinsicCandidate; 2146 } 2147 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Stable_signature): { 2148 if (_location != _in_field) break; // only allow for fields 2149 if (!privileged) break; // only allow in privileged code 2150 return _field_Stable; 2151 } 2152 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_Contended_signature): { 2153 if (_location != _in_field && _location != _in_class) { 2154 break; // only allow for fields and classes 2155 } 2156 if (!EnableContended || (RestrictContended && !privileged)) { 2157 break; // honor privileges 2158 } 2159 return _jdk_internal_vm_annotation_Contended; 2160 } 2161 case vmSymbols::VM_SYMBOL_ENUM_NAME(jdk_internal_vm_annotation_ReservedStackAccess_signature): { 2162 if (_location != _in_method) break; // only allow for methods 2163 if (RestrictReservedStack && !privileged) break; // honor privileges 2164 return _jdk_internal_vm_annotation_ReservedStackAccess; 2165 } 2166 default: { 2167 break; 2168 } 2169 } 2170 return AnnotationCollector::_unknown; 2171 } 2172 2173 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) { 2174 if (is_contended()) 2175 f->set_contended_group(contended_group()); 2176 if (is_stable()) 2177 f->set_stable(true); 2178 } 2179 2180 ClassFileParser::FieldAnnotationCollector::~FieldAnnotationCollector() { 2181 // If there's an error deallocate metadata for field annotations 2182 MetadataFactory::free_array<u1>(_loader_data, _field_annotations); 2183 MetadataFactory::free_array<u1>(_loader_data, _field_type_annotations); 2184 } 2185 2186 void MethodAnnotationCollector::apply_to(const methodHandle& m) { 2187 if (has_annotation(_method_CallerSensitive)) 2188 m->set_caller_sensitive(true); 2189 if (has_annotation(_method_ForceInline)) 2190 m->set_force_inline(true); 2191 if (has_annotation(_method_DontInline)) 2192 m->set_dont_inline(true); 2193 if (has_annotation(_method_InjectedProfile)) 2194 m->set_has_injected_profile(true); 2195 if (has_annotation(_method_LambdaForm_Compiled) && m->intrinsic_id() == vmIntrinsics::_none) 2196 m->set_intrinsic_id(vmIntrinsics::_compiledLambdaForm); 2197 if (has_annotation(_method_Hidden)) 2198 m->set_hidden(true); 2199 if (has_annotation(_method_HotSpotIntrinsicCandidate) && !m->is_synthetic()) 2200 m->set_intrinsic_candidate(true); 2201 if (has_annotation(_jdk_internal_vm_annotation_ReservedStackAccess)) 2202 m->set_has_reserved_stack_access(true); 2203 } 2204 2205 void ClassFileParser::ClassAnnotationCollector::apply_to(InstanceKlass* ik) { 2206 assert(ik != NULL, "invariant"); 2207 ik->set_is_contended(is_contended()); 2208 } 2209 2210 #define MAX_ARGS_SIZE 255 2211 #define MAX_CODE_SIZE 65535 2212 #define INITIAL_MAX_LVT_NUMBER 256 2213 2214 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2215 * 2216 * Rules for LVT's and LVTT's are: 2217 * - There can be any number of LVT's and LVTT's. 2218 * - If there are n LVT's, it is the same as if there was just 2219 * one LVT containing all the entries from the n LVT's. 2220 * - There may be no more than one LVT entry per local variable. 2221 * Two LVT entries are 'equal' if these fields are the same: 2222 * start_pc, length, name, slot 2223 * - There may be no more than one LVTT entry per each LVT entry. 2224 * Each LVTT entry has to match some LVT entry. 2225 * - HotSpot internal LVT keeps natural ordering of class file LVT entries. 2226 */ 2227 void ClassFileParser::copy_localvariable_table(const ConstMethod* cm, 2228 int lvt_cnt, 2229 u2* const localvariable_table_length, 2230 const unsafe_u2** const localvariable_table_start, 2231 int lvtt_cnt, 2232 u2* const localvariable_type_table_length, 2233 const unsafe_u2** const localvariable_type_table_start, 2234 TRAPS) { 2235 2236 ResourceMark rm(THREAD); 2237 2238 typedef ResourceHashtable<LocalVariableTableElement, LocalVariableTableElement*, 2239 &LVT_Hash::hash, &LVT_Hash::equals> LVT_HashTable; 2240 2241 LVT_HashTable* const table = new LVT_HashTable(); 2242 2243 // To fill LocalVariableTable in 2244 const Classfile_LVT_Element* cf_lvt; 2245 LocalVariableTableElement* lvt = cm->localvariable_table_start(); 2246 2247 for (int tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) { 2248 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no]; 2249 for (int idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) { 2250 copy_lvt_element(&cf_lvt[idx], lvt); 2251 // If no duplicates, add LVT elem in hashtable. 2252 if (table->put(*lvt, lvt) == false 2253 && _need_verify 2254 && _major_version >= JAVA_1_5_VERSION) { 2255 classfile_parse_error("Duplicated LocalVariableTable attribute " 2256 "entry for '%s' in class file %s", 2257 _cp->symbol_at(lvt->name_cp_index)->as_utf8(), 2258 CHECK); 2259 } 2260 } 2261 } 2262 2263 // To merge LocalVariableTable and LocalVariableTypeTable 2264 const Classfile_LVT_Element* cf_lvtt; 2265 LocalVariableTableElement lvtt_elem; 2266 2267 for (int tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) { 2268 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no]; 2269 for (int idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) { 2270 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem); 2271 LocalVariableTableElement** entry = table->get(lvtt_elem); 2272 if (entry == NULL) { 2273 if (_need_verify) { 2274 classfile_parse_error("LVTT entry for '%s' in class file %s " 2275 "does not match any LVT entry", 2276 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2277 CHECK); 2278 } 2279 } else if ((*entry)->signature_cp_index != 0 && _need_verify) { 2280 classfile_parse_error("Duplicated LocalVariableTypeTable attribute " 2281 "entry for '%s' in class file %s", 2282 _cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(), 2283 CHECK); 2284 } else { 2285 // to add generic signatures into LocalVariableTable 2286 (*entry)->signature_cp_index = lvtt_elem.descriptor_cp_index; 2287 } 2288 } 2289 } 2290 } 2291 2292 2293 void ClassFileParser::copy_method_annotations(ConstMethod* cm, 2294 const u1* runtime_visible_annotations, 2295 int runtime_visible_annotations_length, 2296 const u1* runtime_invisible_annotations, 2297 int runtime_invisible_annotations_length, 2298 const u1* runtime_visible_parameter_annotations, 2299 int runtime_visible_parameter_annotations_length, 2300 const u1* runtime_invisible_parameter_annotations, 2301 int runtime_invisible_parameter_annotations_length, 2302 const u1* runtime_visible_type_annotations, 2303 int runtime_visible_type_annotations_length, 2304 const u1* runtime_invisible_type_annotations, 2305 int runtime_invisible_type_annotations_length, 2306 const u1* annotation_default, 2307 int annotation_default_length, 2308 TRAPS) { 2309 2310 AnnotationArray* a; 2311 2312 if (runtime_visible_annotations_length + 2313 runtime_invisible_annotations_length > 0) { 2314 a = assemble_annotations(runtime_visible_annotations, 2315 runtime_visible_annotations_length, 2316 runtime_invisible_annotations, 2317 runtime_invisible_annotations_length, 2318 CHECK); 2319 cm->set_method_annotations(a); 2320 } 2321 2322 if (runtime_visible_parameter_annotations_length + 2323 runtime_invisible_parameter_annotations_length > 0) { 2324 a = assemble_annotations(runtime_visible_parameter_annotations, 2325 runtime_visible_parameter_annotations_length, 2326 runtime_invisible_parameter_annotations, 2327 runtime_invisible_parameter_annotations_length, 2328 CHECK); 2329 cm->set_parameter_annotations(a); 2330 } 2331 2332 if (annotation_default_length > 0) { 2333 a = assemble_annotations(annotation_default, 2334 annotation_default_length, 2335 NULL, 2336 0, 2337 CHECK); 2338 cm->set_default_annotations(a); 2339 } 2340 2341 if (runtime_visible_type_annotations_length + 2342 runtime_invisible_type_annotations_length > 0) { 2343 a = assemble_annotations(runtime_visible_type_annotations, 2344 runtime_visible_type_annotations_length, 2345 runtime_invisible_type_annotations, 2346 runtime_invisible_type_annotations_length, 2347 CHECK); 2348 cm->set_type_annotations(a); 2349 } 2350 } 2351 2352 2353 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions 2354 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the 2355 // Method* to save footprint, so we only know the size of the resulting Method* when the 2356 // entire method attribute is parsed. 2357 // 2358 // The promoted_flags parameter is used to pass relevant access_flags 2359 // from the method back up to the containing klass. These flag values 2360 // are added to klass's access_flags. 2361 2362 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs, 2363 bool is_interface, 2364 bool is_value_type, 2365 const ConstantPool* cp, 2366 AccessFlags* const promoted_flags, 2367 TRAPS) { 2368 assert(cfs != NULL, "invariant"); 2369 assert(cp != NULL, "invariant"); 2370 assert(promoted_flags != NULL, "invariant"); 2371 2372 ResourceMark rm(THREAD); 2373 // Parse fixed parts: 2374 // access_flags, name_index, descriptor_index, attributes_count 2375 cfs->guarantee_more(8, CHECK_NULL); 2376 2377 int flags = cfs->get_u2_fast(); 2378 const u2 name_index = cfs->get_u2_fast(); 2379 const int cp_size = cp->length(); 2380 check_property( 2381 valid_symbol_at(name_index), 2382 "Illegal constant pool index %u for method name in class file %s", 2383 name_index, CHECK_NULL); 2384 const Symbol* const name = cp->symbol_at(name_index); 2385 verify_legal_method_name(name, CHECK_NULL); 2386 2387 const u2 signature_index = cfs->get_u2_fast(); 2388 guarantee_property( 2389 valid_symbol_at(signature_index), 2390 "Illegal constant pool index %u for method signature in class file %s", 2391 signature_index, CHECK_NULL); 2392 const Symbol* const signature = cp->symbol_at(signature_index); 2393 2394 if (name == vmSymbols::class_initializer_name()) { 2395 // We ignore the other access flags for a valid class initializer. 2396 // (JVM Spec 2nd ed., chapter 4.6) 2397 if (_major_version < 51) { // backward compatibility 2398 flags = JVM_ACC_STATIC; 2399 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) { 2400 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT; 2401 } else { 2402 classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL); 2403 } 2404 } else { 2405 verify_legal_method_modifiers(flags, is_interface, is_value_type, name, CHECK_NULL); 2406 } 2407 2408 if (name == vmSymbols::object_initializer_name()) { 2409 if (is_interface) { 2410 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL); 2411 /* TBD: uncomment when javac stops generating <init>() for value types. 2412 } else if (is_value_type) { 2413 classfile_parse_error("Value Type cannot have a method named <init>, class file %s", CHECK_NULL); 2414 */ 2415 } 2416 } 2417 2418 int args_size = -1; // only used when _need_verify is true 2419 if (_need_verify) { 2420 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) + 2421 verify_legal_method_signature(name, signature, CHECK_NULL); 2422 if (args_size > MAX_ARGS_SIZE) { 2423 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL); 2424 } 2425 } 2426 2427 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS); 2428 2429 // Default values for code and exceptions attribute elements 2430 u2 max_stack = 0; 2431 u2 max_locals = 0; 2432 u4 code_length = 0; 2433 const u1* code_start = 0; 2434 u2 exception_table_length = 0; 2435 const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements 2436 Array<int>* exception_handlers = Universe::the_empty_int_array(); 2437 u2 checked_exceptions_length = 0; 2438 const unsafe_u2* checked_exceptions_start = NULL; // (potentially unaligned) pointer to array of u2 elements 2439 CompressedLineNumberWriteStream* linenumber_table = NULL; 2440 int linenumber_table_length = 0; 2441 int total_lvt_length = 0; 2442 u2 lvt_cnt = 0; 2443 u2 lvtt_cnt = 0; 2444 bool lvt_allocated = false; 2445 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER; 2446 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER; 2447 u2* localvariable_table_length = NULL; 2448 const unsafe_u2** localvariable_table_start = NULL; // (potentially unaligned) pointer to array of LVT attributes 2449 u2* localvariable_type_table_length = NULL; 2450 const unsafe_u2** localvariable_type_table_start = NULL; // (potentially unaligned) pointer to LVTT attributes 2451 int method_parameters_length = -1; 2452 const u1* method_parameters_data = NULL; 2453 bool method_parameters_seen = false; 2454 bool parsed_code_attribute = false; 2455 bool parsed_checked_exceptions_attribute = false; 2456 bool parsed_stackmap_attribute = false; 2457 // stackmap attribute - JDK1.5 2458 const u1* stackmap_data = NULL; 2459 int stackmap_data_length = 0; 2460 u2 generic_signature_index = 0; 2461 MethodAnnotationCollector parsed_annotations; 2462 const u1* runtime_visible_annotations = NULL; 2463 int runtime_visible_annotations_length = 0; 2464 const u1* runtime_invisible_annotations = NULL; 2465 int runtime_invisible_annotations_length = 0; 2466 const u1* runtime_visible_parameter_annotations = NULL; 2467 int runtime_visible_parameter_annotations_length = 0; 2468 const u1* runtime_invisible_parameter_annotations = NULL; 2469 int runtime_invisible_parameter_annotations_length = 0; 2470 const u1* runtime_visible_type_annotations = NULL; 2471 int runtime_visible_type_annotations_length = 0; 2472 const u1* runtime_invisible_type_annotations = NULL; 2473 int runtime_invisible_type_annotations_length = 0; 2474 bool runtime_invisible_annotations_exists = false; 2475 bool runtime_invisible_type_annotations_exists = false; 2476 bool runtime_invisible_parameter_annotations_exists = false; 2477 const u1* annotation_default = NULL; 2478 int annotation_default_length = 0; 2479 2480 // Parse code and exceptions attribute 2481 u2 method_attributes_count = cfs->get_u2_fast(); 2482 while (method_attributes_count--) { 2483 cfs->guarantee_more(6, CHECK_NULL); // method_attribute_name_index, method_attribute_length 2484 const u2 method_attribute_name_index = cfs->get_u2_fast(); 2485 const u4 method_attribute_length = cfs->get_u4_fast(); 2486 check_property( 2487 valid_symbol_at(method_attribute_name_index), 2488 "Invalid method attribute name index %u in class file %s", 2489 method_attribute_name_index, CHECK_NULL); 2490 2491 const Symbol* const method_attribute_name = cp->symbol_at(method_attribute_name_index); 2492 if (method_attribute_name == vmSymbols::tag_code()) { 2493 // Parse Code attribute 2494 if (_need_verify) { 2495 guarantee_property( 2496 !access_flags.is_native() && !access_flags.is_abstract(), 2497 "Code attribute in native or abstract methods in class file %s", 2498 CHECK_NULL); 2499 } 2500 if (parsed_code_attribute) { 2501 classfile_parse_error("Multiple Code attributes in class file %s", 2502 CHECK_NULL); 2503 } 2504 parsed_code_attribute = true; 2505 2506 // Stack size, locals size, and code size 2507 if (_major_version == 45 && _minor_version <= 2) { 2508 cfs->guarantee_more(4, CHECK_NULL); 2509 max_stack = cfs->get_u1_fast(); 2510 max_locals = cfs->get_u1_fast(); 2511 code_length = cfs->get_u2_fast(); 2512 } else { 2513 cfs->guarantee_more(8, CHECK_NULL); 2514 max_stack = cfs->get_u2_fast(); 2515 max_locals = cfs->get_u2_fast(); 2516 code_length = cfs->get_u4_fast(); 2517 } 2518 if (_need_verify) { 2519 guarantee_property(args_size <= max_locals, 2520 "Arguments can't fit into locals in class file %s", 2521 CHECK_NULL); 2522 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE, 2523 "Invalid method Code length %u in class file %s", 2524 code_length, CHECK_NULL); 2525 } 2526 // Code pointer 2527 code_start = cfs->current(); 2528 assert(code_start != NULL, "null code start"); 2529 cfs->guarantee_more(code_length, CHECK_NULL); 2530 cfs->skip_u1_fast(code_length); 2531 2532 // Exception handler table 2533 cfs->guarantee_more(2, CHECK_NULL); // exception_table_length 2534 exception_table_length = cfs->get_u2_fast(); 2535 if (exception_table_length > 0) { 2536 exception_table_start = parse_exception_table(cfs, 2537 code_length, 2538 exception_table_length, 2539 CHECK_NULL); 2540 } 2541 2542 // Parse additional attributes in code attribute 2543 cfs->guarantee_more(2, CHECK_NULL); // code_attributes_count 2544 u2 code_attributes_count = cfs->get_u2_fast(); 2545 2546 unsigned int calculated_attribute_length = 0; 2547 2548 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) { 2549 calculated_attribute_length = 2550 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length); 2551 } else { 2552 // max_stack, locals and length are smaller in pre-version 45.2 classes 2553 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2); 2554 } 2555 calculated_attribute_length += 2556 code_length + 2557 sizeof(exception_table_length) + 2558 sizeof(code_attributes_count) + 2559 exception_table_length * 2560 ( sizeof(u2) + // start_pc 2561 sizeof(u2) + // end_pc 2562 sizeof(u2) + // handler_pc 2563 sizeof(u2) ); // catch_type_index 2564 2565 while (code_attributes_count--) { 2566 cfs->guarantee_more(6, CHECK_NULL); // code_attribute_name_index, code_attribute_length 2567 const u2 code_attribute_name_index = cfs->get_u2_fast(); 2568 const u4 code_attribute_length = cfs->get_u4_fast(); 2569 calculated_attribute_length += code_attribute_length + 2570 sizeof(code_attribute_name_index) + 2571 sizeof(code_attribute_length); 2572 check_property(valid_symbol_at(code_attribute_name_index), 2573 "Invalid code attribute name index %u in class file %s", 2574 code_attribute_name_index, 2575 CHECK_NULL); 2576 if (LoadLineNumberTables && 2577 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) { 2578 // Parse and compress line number table 2579 parse_linenumber_table(code_attribute_length, 2580 code_length, 2581 &linenumber_table, 2582 CHECK_NULL); 2583 2584 } else if (LoadLocalVariableTables && 2585 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) { 2586 // Parse local variable table 2587 if (!lvt_allocated) { 2588 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2589 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2590 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2591 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2592 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2593 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2594 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2595 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2596 lvt_allocated = true; 2597 } 2598 if (lvt_cnt == max_lvt_cnt) { 2599 max_lvt_cnt <<= 1; 2600 localvariable_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt); 2601 localvariable_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt); 2602 } 2603 localvariable_table_start[lvt_cnt] = 2604 parse_localvariable_table(cfs, 2605 code_length, 2606 max_locals, 2607 code_attribute_length, 2608 &localvariable_table_length[lvt_cnt], 2609 false, // is not LVTT 2610 CHECK_NULL); 2611 total_lvt_length += localvariable_table_length[lvt_cnt]; 2612 lvt_cnt++; 2613 } else if (LoadLocalVariableTypeTables && 2614 _major_version >= JAVA_1_5_VERSION && 2615 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) { 2616 if (!lvt_allocated) { 2617 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2618 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2619 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2620 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2621 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD( 2622 THREAD, u2, INITIAL_MAX_LVT_NUMBER); 2623 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD( 2624 THREAD, const unsafe_u2*, INITIAL_MAX_LVT_NUMBER); 2625 lvt_allocated = true; 2626 } 2627 // Parse local variable type table 2628 if (lvtt_cnt == max_lvtt_cnt) { 2629 max_lvtt_cnt <<= 1; 2630 localvariable_type_table_length = REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt); 2631 localvariable_type_table_start = REALLOC_RESOURCE_ARRAY(const unsafe_u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt); 2632 } 2633 localvariable_type_table_start[lvtt_cnt] = 2634 parse_localvariable_table(cfs, 2635 code_length, 2636 max_locals, 2637 code_attribute_length, 2638 &localvariable_type_table_length[lvtt_cnt], 2639 true, // is LVTT 2640 CHECK_NULL); 2641 lvtt_cnt++; 2642 } else if (_major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION && 2643 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) { 2644 // Stack map is only needed by the new verifier in JDK1.5. 2645 if (parsed_stackmap_attribute) { 2646 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_NULL); 2647 } 2648 stackmap_data = parse_stackmap_table(cfs, code_attribute_length, _need_verify, CHECK_NULL); 2649 stackmap_data_length = code_attribute_length; 2650 parsed_stackmap_attribute = true; 2651 } else { 2652 // Skip unknown attributes 2653 cfs->skip_u1(code_attribute_length, CHECK_NULL); 2654 } 2655 } 2656 // check method attribute length 2657 if (_need_verify) { 2658 guarantee_property(method_attribute_length == calculated_attribute_length, 2659 "Code segment has wrong length in class file %s", 2660 CHECK_NULL); 2661 } 2662 } else if (method_attribute_name == vmSymbols::tag_exceptions()) { 2663 // Parse Exceptions attribute 2664 if (parsed_checked_exceptions_attribute) { 2665 classfile_parse_error("Multiple Exceptions attributes in class file %s", 2666 CHECK_NULL); 2667 } 2668 parsed_checked_exceptions_attribute = true; 2669 checked_exceptions_start = 2670 parse_checked_exceptions(cfs, 2671 &checked_exceptions_length, 2672 method_attribute_length, 2673 CHECK_NULL); 2674 } else if (method_attribute_name == vmSymbols::tag_method_parameters()) { 2675 // reject multiple method parameters 2676 if (method_parameters_seen) { 2677 classfile_parse_error("Multiple MethodParameters attributes in class file %s", 2678 CHECK_NULL); 2679 } 2680 method_parameters_seen = true; 2681 method_parameters_length = cfs->get_u1_fast(); 2682 const u2 real_length = (method_parameters_length * 4u) + 1u; 2683 if (method_attribute_length != real_length) { 2684 classfile_parse_error( 2685 "Invalid MethodParameters method attribute length %u in class file", 2686 method_attribute_length, CHECK_NULL); 2687 } 2688 method_parameters_data = cfs->current(); 2689 cfs->skip_u2_fast(method_parameters_length); 2690 cfs->skip_u2_fast(method_parameters_length); 2691 // ignore this attribute if it cannot be reflected 2692 if (!SystemDictionary::Parameter_klass_loaded()) 2693 method_parameters_length = -1; 2694 } else if (method_attribute_name == vmSymbols::tag_synthetic()) { 2695 if (method_attribute_length != 0) { 2696 classfile_parse_error( 2697 "Invalid Synthetic method attribute length %u in class file %s", 2698 method_attribute_length, CHECK_NULL); 2699 } 2700 // Should we check that there hasn't already been a synthetic attribute? 2701 access_flags.set_is_synthetic(); 2702 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120 2703 if (method_attribute_length != 0) { 2704 classfile_parse_error( 2705 "Invalid Deprecated method attribute length %u in class file %s", 2706 method_attribute_length, CHECK_NULL); 2707 } 2708 } else if (_major_version >= JAVA_1_5_VERSION) { 2709 if (method_attribute_name == vmSymbols::tag_signature()) { 2710 if (generic_signature_index != 0) { 2711 classfile_parse_error( 2712 "Multiple Signature attributes for method in class file %s", 2713 CHECK_NULL); 2714 } 2715 if (method_attribute_length != 2) { 2716 classfile_parse_error( 2717 "Invalid Signature attribute length %u in class file %s", 2718 method_attribute_length, CHECK_NULL); 2719 } 2720 generic_signature_index = parse_generic_signature_attribute(cfs, CHECK_NULL); 2721 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2722 if (runtime_visible_annotations != NULL) { 2723 classfile_parse_error( 2724 "Multiple RuntimeVisibleAnnotations attributes for method in class file %s", 2725 CHECK_NULL); 2726 } 2727 runtime_visible_annotations_length = method_attribute_length; 2728 runtime_visible_annotations = cfs->current(); 2729 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2730 cfs->guarantee_more(runtime_visible_annotations_length, CHECK_NULL); 2731 parse_annotations(cp, 2732 runtime_visible_annotations, 2733 runtime_visible_annotations_length, 2734 &parsed_annotations, 2735 _loader_data, 2736 CHECK_NULL); 2737 cfs->skip_u1_fast(runtime_visible_annotations_length); 2738 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2739 if (runtime_invisible_annotations_exists) { 2740 classfile_parse_error( 2741 "Multiple RuntimeInvisibleAnnotations attributes for method in class file %s", 2742 CHECK_NULL); 2743 } 2744 runtime_invisible_annotations_exists = true; 2745 if (PreserveAllAnnotations) { 2746 runtime_invisible_annotations_length = method_attribute_length; 2747 runtime_invisible_annotations = cfs->current(); 2748 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 2749 } 2750 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2751 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) { 2752 if (runtime_visible_parameter_annotations != NULL) { 2753 classfile_parse_error( 2754 "Multiple RuntimeVisibleParameterAnnotations attributes for method in class file %s", 2755 CHECK_NULL); 2756 } 2757 runtime_visible_parameter_annotations_length = method_attribute_length; 2758 runtime_visible_parameter_annotations = cfs->current(); 2759 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations"); 2760 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_NULL); 2761 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) { 2762 if (runtime_invisible_parameter_annotations_exists) { 2763 classfile_parse_error( 2764 "Multiple RuntimeInvisibleParameterAnnotations attributes for method in class file %s", 2765 CHECK_NULL); 2766 } 2767 runtime_invisible_parameter_annotations_exists = true; 2768 if (PreserveAllAnnotations) { 2769 runtime_invisible_parameter_annotations_length = method_attribute_length; 2770 runtime_invisible_parameter_annotations = cfs->current(); 2771 assert(runtime_invisible_parameter_annotations != NULL, 2772 "null invisible parameter annotations"); 2773 } 2774 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2775 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) { 2776 if (annotation_default != NULL) { 2777 classfile_parse_error( 2778 "Multiple AnnotationDefault attributes for method in class file %s", 2779 CHECK_NULL); 2780 } 2781 annotation_default_length = method_attribute_length; 2782 annotation_default = cfs->current(); 2783 assert(annotation_default != NULL, "null annotation default"); 2784 cfs->skip_u1(annotation_default_length, CHECK_NULL); 2785 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_type_annotations()) { 2786 if (runtime_visible_type_annotations != NULL) { 2787 classfile_parse_error( 2788 "Multiple RuntimeVisibleTypeAnnotations attributes for method in class file %s", 2789 CHECK_NULL); 2790 } 2791 runtime_visible_type_annotations_length = method_attribute_length; 2792 runtime_visible_type_annotations = cfs->current(); 2793 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 2794 // No need for the VM to parse Type annotations 2795 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK_NULL); 2796 } else if (method_attribute_name == vmSymbols::tag_runtime_invisible_type_annotations()) { 2797 if (runtime_invisible_type_annotations_exists) { 2798 classfile_parse_error( 2799 "Multiple RuntimeInvisibleTypeAnnotations attributes for method in class file %s", 2800 CHECK_NULL); 2801 } else { 2802 runtime_invisible_type_annotations_exists = true; 2803 } 2804 if (PreserveAllAnnotations) { 2805 runtime_invisible_type_annotations_length = method_attribute_length; 2806 runtime_invisible_type_annotations = cfs->current(); 2807 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 2808 } 2809 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2810 } else { 2811 // Skip unknown attributes 2812 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2813 } 2814 } else { 2815 // Skip unknown attributes 2816 cfs->skip_u1(method_attribute_length, CHECK_NULL); 2817 } 2818 } 2819 2820 if (linenumber_table != NULL) { 2821 linenumber_table->write_terminator(); 2822 linenumber_table_length = linenumber_table->position(); 2823 } 2824 2825 // Make sure there's at least one Code attribute in non-native/non-abstract method 2826 if (_need_verify) { 2827 guarantee_property(access_flags.is_native() || 2828 access_flags.is_abstract() || 2829 parsed_code_attribute, 2830 "Absent Code attribute in method that is not native or abstract in class file %s", 2831 CHECK_NULL); 2832 } 2833 2834 // All sizing information for a Method* is finally available, now create it 2835 InlineTableSizes sizes( 2836 total_lvt_length, 2837 linenumber_table_length, 2838 exception_table_length, 2839 checked_exceptions_length, 2840 method_parameters_length, 2841 generic_signature_index, 2842 runtime_visible_annotations_length + 2843 runtime_invisible_annotations_length, 2844 runtime_visible_parameter_annotations_length + 2845 runtime_invisible_parameter_annotations_length, 2846 runtime_visible_type_annotations_length + 2847 runtime_invisible_type_annotations_length, 2848 annotation_default_length, 2849 0); 2850 2851 Method* const m = Method::allocate(_loader_data, 2852 code_length, 2853 access_flags, 2854 &sizes, 2855 ConstMethod::NORMAL, 2856 CHECK_NULL); 2857 2858 ClassLoadingService::add_class_method_size(m->size()*wordSize); 2859 2860 // Fill in information from fixed part (access_flags already set) 2861 m->set_constants(_cp); 2862 m->set_name_index(name_index); 2863 m->set_signature_index(signature_index); 2864 2865 ResultTypeFinder rtf(cp->symbol_at(signature_index)); 2866 m->constMethod()->set_result_type(rtf.type()); 2867 2868 if (args_size >= 0) { 2869 m->set_size_of_parameters(args_size); 2870 } else { 2871 m->compute_size_of_parameters(THREAD); 2872 } 2873 #ifdef ASSERT 2874 if (args_size >= 0) { 2875 m->compute_size_of_parameters(THREAD); 2876 assert(args_size == m->size_of_parameters(), ""); 2877 } 2878 #endif 2879 2880 // Fill in code attribute information 2881 m->set_max_stack(max_stack); 2882 m->set_max_locals(max_locals); 2883 if (stackmap_data != NULL) { 2884 m->constMethod()->copy_stackmap_data(_loader_data, 2885 (u1*)stackmap_data, 2886 stackmap_data_length, 2887 CHECK_NULL); 2888 } 2889 2890 // Copy byte codes 2891 m->set_code((u1*)code_start); 2892 2893 // Copy line number table 2894 if (linenumber_table != NULL) { 2895 memcpy(m->compressed_linenumber_table(), 2896 linenumber_table->buffer(), 2897 linenumber_table_length); 2898 } 2899 2900 // Copy exception table 2901 if (exception_table_length > 0) { 2902 Copy::conjoint_swap_if_needed<Endian::JAVA>(exception_table_start, 2903 m->exception_table_start(), 2904 exception_table_length * sizeof(ExceptionTableElement), 2905 sizeof(u2)); 2906 } 2907 2908 // Copy method parameters 2909 if (method_parameters_length > 0) { 2910 MethodParametersElement* elem = m->constMethod()->method_parameters_start(); 2911 for (int i = 0; i < method_parameters_length; i++) { 2912 elem[i].name_cp_index = Bytes::get_Java_u2((address)method_parameters_data); 2913 method_parameters_data += 2; 2914 elem[i].flags = Bytes::get_Java_u2((address)method_parameters_data); 2915 method_parameters_data += 2; 2916 } 2917 } 2918 2919 // Copy checked exceptions 2920 if (checked_exceptions_length > 0) { 2921 Copy::conjoint_swap_if_needed<Endian::JAVA>(checked_exceptions_start, 2922 m->checked_exceptions_start(), 2923 checked_exceptions_length * sizeof(CheckedExceptionElement), 2924 sizeof(u2)); 2925 } 2926 2927 // Copy class file LVT's/LVTT's into the HotSpot internal LVT. 2928 if (total_lvt_length > 0) { 2929 promoted_flags->set_has_localvariable_table(); 2930 copy_localvariable_table(m->constMethod(), 2931 lvt_cnt, 2932 localvariable_table_length, 2933 localvariable_table_start, 2934 lvtt_cnt, 2935 localvariable_type_table_length, 2936 localvariable_type_table_start, 2937 CHECK_NULL); 2938 } 2939 2940 if (parsed_annotations.has_any_annotations()) 2941 parsed_annotations.apply_to(m); 2942 2943 // Copy annotations 2944 copy_method_annotations(m->constMethod(), 2945 runtime_visible_annotations, 2946 runtime_visible_annotations_length, 2947 runtime_invisible_annotations, 2948 runtime_invisible_annotations_length, 2949 runtime_visible_parameter_annotations, 2950 runtime_visible_parameter_annotations_length, 2951 runtime_invisible_parameter_annotations, 2952 runtime_invisible_parameter_annotations_length, 2953 runtime_visible_type_annotations, 2954 runtime_visible_type_annotations_length, 2955 runtime_invisible_type_annotations, 2956 runtime_invisible_type_annotations_length, 2957 annotation_default, 2958 annotation_default_length, 2959 CHECK_NULL); 2960 2961 if (name == vmSymbols::finalize_method_name() && 2962 signature == vmSymbols::void_method_signature()) { 2963 if (m->is_empty_method()) { 2964 _has_empty_finalizer = true; 2965 } else { 2966 _has_finalizer = true; 2967 } 2968 } 2969 if (name == vmSymbols::object_initializer_name() && 2970 signature == vmSymbols::void_method_signature() && 2971 m->is_vanilla_constructor()) { 2972 _has_vanilla_constructor = true; 2973 } 2974 2975 NOT_PRODUCT(m->verify()); 2976 return m; 2977 } 2978 2979 2980 // The promoted_flags parameter is used to pass relevant access_flags 2981 // from the methods back up to the containing klass. These flag values 2982 // are added to klass's access_flags. 2983 // Side-effects: populates the _methods field in the parser 2984 void ClassFileParser::parse_methods(const ClassFileStream* const cfs, 2985 bool is_interface, 2986 bool is_value_type, 2987 AccessFlags* promoted_flags, 2988 bool* has_final_method, 2989 bool* declares_nonstatic_concrete_methods, 2990 TRAPS) { 2991 assert(cfs != NULL, "invariant"); 2992 assert(promoted_flags != NULL, "invariant"); 2993 assert(has_final_method != NULL, "invariant"); 2994 assert(declares_nonstatic_concrete_methods != NULL, "invariant"); 2995 2996 assert(NULL == _methods, "invariant"); 2997 2998 cfs->guarantee_more(2, CHECK); // length 2999 const u2 length = cfs->get_u2_fast(); 3000 if (length == 0) { 3001 _methods = Universe::the_empty_method_array(); 3002 } else { 3003 _methods = MetadataFactory::new_array<Method*>(_loader_data, 3004 length, 3005 NULL, 3006 CHECK); 3007 3008 for (int index = 0; index < length; index++) { 3009 Method* method = parse_method(cfs, 3010 is_interface, 3011 is_value_type, 3012 _cp, 3013 promoted_flags, 3014 CHECK); 3015 3016 if (method->is_final()) { 3017 *has_final_method = true; 3018 } 3019 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags 3020 // used for interface initialization, and default method inheritance analysis 3021 if (is_interface && !(*declares_nonstatic_concrete_methods) 3022 && !method->is_abstract() && !method->is_static()) { 3023 *declares_nonstatic_concrete_methods = true; 3024 } 3025 _methods->at_put(index, method); 3026 } 3027 3028 if (_need_verify && length > 1) { 3029 // Check duplicated methods 3030 ResourceMark rm(THREAD); 3031 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD( 3032 THREAD, NameSigHash*, HASH_ROW_SIZE); 3033 initialize_hashtable(names_and_sigs); 3034 bool dup = false; 3035 const Symbol* name = NULL; 3036 const Symbol* sig = NULL; 3037 { 3038 debug_only(NoSafepointVerifier nsv;) 3039 for (int i = 0; i < length; i++) { 3040 const Method* const m = _methods->at(i); 3041 name = m->name(); 3042 sig = m->signature(); 3043 // If no duplicates, add name/signature in hashtable names_and_sigs. 3044 if (!put_after_lookup(name, sig, names_and_sigs)) { 3045 dup = true; 3046 break; 3047 } 3048 } 3049 } 3050 if (dup) { 3051 classfile_parse_error("Duplicate method name \"%s\" with signature \"%s\" in class file %s", 3052 name->as_C_string(), sig->as_klass_external_name(), CHECK); 3053 } 3054 } 3055 } 3056 } 3057 3058 static const intArray* sort_methods(Array<Method*>* methods) { 3059 const int length = methods->length(); 3060 // If JVMTI original method ordering or sharing is enabled we have to 3061 // remember the original class file ordering. 3062 // We temporarily use the vtable_index field in the Method* to store the 3063 // class file index, so we can read in after calling qsort. 3064 // Put the method ordering in the shared archive. 3065 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 3066 for (int index = 0; index < length; index++) { 3067 Method* const m = methods->at(index); 3068 assert(!m->valid_vtable_index(), "vtable index should not be set"); 3069 m->set_vtable_index(index); 3070 } 3071 } 3072 // Sort method array by ascending method name (for faster lookups & vtable construction) 3073 // Note that the ordering is not alphabetical, see Symbol::fast_compare 3074 Method::sort_methods(methods); 3075 3076 intArray* method_ordering = NULL; 3077 // If JVMTI original method ordering or sharing is enabled construct int 3078 // array remembering the original ordering 3079 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) { 3080 method_ordering = new intArray(length, length, -1); 3081 for (int index = 0; index < length; index++) { 3082 Method* const m = methods->at(index); 3083 const int old_index = m->vtable_index(); 3084 assert(old_index >= 0 && old_index < length, "invalid method index"); 3085 method_ordering->at_put(index, old_index); 3086 m->set_vtable_index(Method::invalid_vtable_index); 3087 } 3088 } 3089 return method_ordering; 3090 } 3091 3092 // Parse generic_signature attribute for methods and fields 3093 u2 ClassFileParser::parse_generic_signature_attribute(const ClassFileStream* const cfs, 3094 TRAPS) { 3095 assert(cfs != NULL, "invariant"); 3096 3097 cfs->guarantee_more(2, CHECK_0); // generic_signature_index 3098 const u2 generic_signature_index = cfs->get_u2_fast(); 3099 check_property( 3100 valid_symbol_at(generic_signature_index), 3101 "Invalid Signature attribute at constant pool index %u in class file %s", 3102 generic_signature_index, CHECK_0); 3103 return generic_signature_index; 3104 } 3105 3106 void ClassFileParser::parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, 3107 TRAPS) { 3108 3109 assert(cfs != NULL, "invariant"); 3110 3111 cfs->guarantee_more(2, CHECK); // sourcefile_index 3112 const u2 sourcefile_index = cfs->get_u2_fast(); 3113 check_property( 3114 valid_symbol_at(sourcefile_index), 3115 "Invalid SourceFile attribute at constant pool index %u in class file %s", 3116 sourcefile_index, CHECK); 3117 set_class_sourcefile_index(sourcefile_index); 3118 } 3119 3120 void ClassFileParser::parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs, 3121 int length, 3122 TRAPS) { 3123 assert(cfs != NULL, "invariant"); 3124 3125 const u1* const sde_buffer = cfs->current(); 3126 assert(sde_buffer != NULL, "null sde buffer"); 3127 3128 // Don't bother storing it if there is no way to retrieve it 3129 if (JvmtiExport::can_get_source_debug_extension()) { 3130 assert((length+1) > length, "Overflow checking"); 3131 u1* const sde = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, u1, length+1); 3132 for (int i = 0; i < length; i++) { 3133 sde[i] = sde_buffer[i]; 3134 } 3135 sde[length] = '\0'; 3136 set_class_sde_buffer((const char*)sde, length); 3137 } 3138 // Got utf8 string, set stream position forward 3139 cfs->skip_u1(length, CHECK); 3140 } 3141 3142 3143 // Inner classes can be static, private or protected (classic VM does this) 3144 #define RECOGNIZED_INNER_CLASS_MODIFIERS ( JVM_RECOGNIZED_CLASS_MODIFIERS | \ 3145 JVM_ACC_PRIVATE | \ 3146 JVM_ACC_PROTECTED | \ 3147 JVM_ACC_STATIC \ 3148 ) 3149 3150 // Return number of classes in the inner classes attribute table 3151 u2 ClassFileParser::parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs, 3152 const u1* const inner_classes_attribute_start, 3153 bool parsed_enclosingmethod_attribute, 3154 u2 enclosing_method_class_index, 3155 u2 enclosing_method_method_index, 3156 TRAPS) { 3157 const u1* const current_mark = cfs->current(); 3158 u2 length = 0; 3159 if (inner_classes_attribute_start != NULL) { 3160 cfs->set_current(inner_classes_attribute_start); 3161 cfs->guarantee_more(2, CHECK_0); // length 3162 length = cfs->get_u2_fast(); 3163 } 3164 3165 // 4-tuples of shorts of inner classes data and 2 shorts of enclosing 3166 // method data: 3167 // [inner_class_info_index, 3168 // outer_class_info_index, 3169 // inner_name_index, 3170 // inner_class_access_flags, 3171 // ... 3172 // enclosing_method_class_index, 3173 // enclosing_method_method_index] 3174 const int size = length * 4 + (parsed_enclosingmethod_attribute ? 2 : 0); 3175 Array<u2>* const inner_classes = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3176 _inner_classes = inner_classes; 3177 3178 int index = 0; 3179 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2 3180 for (int n = 0; n < length; n++) { 3181 // Inner class index 3182 const u2 inner_class_info_index = cfs->get_u2_fast(); 3183 check_property( 3184 valid_klass_reference_at(inner_class_info_index), 3185 "inner_class_info_index %u has bad constant type in class file %s", 3186 inner_class_info_index, CHECK_0); 3187 // Outer class index 3188 const u2 outer_class_info_index = cfs->get_u2_fast(); 3189 check_property( 3190 outer_class_info_index == 0 || 3191 valid_klass_reference_at(outer_class_info_index), 3192 "outer_class_info_index %u has bad constant type in class file %s", 3193 outer_class_info_index, CHECK_0); 3194 // Inner class name 3195 const u2 inner_name_index = cfs->get_u2_fast(); 3196 check_property( 3197 inner_name_index == 0 || valid_symbol_at(inner_name_index), 3198 "inner_name_index %u has bad constant type in class file %s", 3199 inner_name_index, CHECK_0); 3200 if (_need_verify) { 3201 guarantee_property(inner_class_info_index != outer_class_info_index, 3202 "Class is both outer and inner class in class file %s", CHECK_0); 3203 } 3204 3205 jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS; 3206 // JVM_ACC_MODULE is defined in JDK-9 and later. 3207 if (_major_version >= JAVA_9_VERSION) { 3208 recognized_modifiers |= JVM_ACC_MODULE; 3209 } 3210 // JVM_ACC_VALUE is defined for class file version 55 and later 3211 if (supports_value_types()) { 3212 recognized_modifiers |= JVM_ACC_VALUE; 3213 } 3214 3215 // Access flags 3216 jint flags = cfs->get_u2_fast() & recognized_modifiers; 3217 3218 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 3219 // Set abstract bit for old class files for backward compatibility 3220 flags |= JVM_ACC_ABSTRACT; 3221 } 3222 verify_legal_class_modifiers(flags, CHECK_0); 3223 AccessFlags inner_access_flags(flags); 3224 3225 inner_classes->at_put(index++, inner_class_info_index); 3226 inner_classes->at_put(index++, outer_class_info_index); 3227 inner_classes->at_put(index++, inner_name_index); 3228 inner_classes->at_put(index++, inner_access_flags.as_short()); 3229 } 3230 3231 // 4347400: make sure there's no duplicate entry in the classes array 3232 if (_need_verify && _major_version >= JAVA_1_5_VERSION) { 3233 for(int i = 0; i < length * 4; i += 4) { 3234 for(int j = i + 4; j < length * 4; j += 4) { 3235 guarantee_property((inner_classes->at(i) != inner_classes->at(j) || 3236 inner_classes->at(i+1) != inner_classes->at(j+1) || 3237 inner_classes->at(i+2) != inner_classes->at(j+2) || 3238 inner_classes->at(i+3) != inner_classes->at(j+3)), 3239 "Duplicate entry in InnerClasses in class file %s", 3240 CHECK_0); 3241 } 3242 } 3243 } 3244 3245 // Set EnclosingMethod class and method indexes. 3246 if (parsed_enclosingmethod_attribute) { 3247 inner_classes->at_put(index++, enclosing_method_class_index); 3248 inner_classes->at_put(index++, enclosing_method_method_index); 3249 } 3250 assert(index == size, "wrong size"); 3251 3252 // Restore buffer's current position. 3253 cfs->set_current(current_mark); 3254 3255 return length; 3256 } 3257 3258 u2 ClassFileParser::parse_classfile_nest_members_attribute(const ClassFileStream* const cfs, 3259 const u1* const nest_members_attribute_start, 3260 TRAPS) { 3261 const u1* const current_mark = cfs->current(); 3262 u2 length = 0; 3263 if (nest_members_attribute_start != NULL) { 3264 cfs->set_current(nest_members_attribute_start); 3265 cfs->guarantee_more(2, CHECK_0); // length 3266 length = cfs->get_u2_fast(); 3267 } 3268 const int size = length; 3269 Array<u2>* const nest_members = MetadataFactory::new_array<u2>(_loader_data, size, CHECK_0); 3270 _nest_members = nest_members; 3271 3272 int index = 0; 3273 cfs->guarantee_more(2 * length, CHECK_0); 3274 for (int n = 0; n < length; n++) { 3275 const u2 class_info_index = cfs->get_u2_fast(); 3276 check_property( 3277 valid_klass_reference_at(class_info_index), 3278 "Nest member class_info_index %u has bad constant type in class file %s", 3279 class_info_index, CHECK_0); 3280 nest_members->at_put(index++, class_info_index); 3281 } 3282 assert(index == size, "wrong size"); 3283 3284 // Restore buffer's current position. 3285 cfs->set_current(current_mark); 3286 3287 return length; 3288 } 3289 3290 void ClassFileParser::parse_classfile_synthetic_attribute(TRAPS) { 3291 set_class_synthetic_flag(true); 3292 } 3293 3294 void ClassFileParser::parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS) { 3295 assert(cfs != NULL, "invariant"); 3296 3297 const u2 signature_index = cfs->get_u2(CHECK); 3298 check_property( 3299 valid_symbol_at(signature_index), 3300 "Invalid constant pool index %u in Signature attribute in class file %s", 3301 signature_index, CHECK); 3302 set_class_generic_signature_index(signature_index); 3303 } 3304 3305 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs, 3306 ConstantPool* cp, 3307 u4 attribute_byte_length, 3308 TRAPS) { 3309 assert(cfs != NULL, "invariant"); 3310 assert(cp != NULL, "invariant"); 3311 3312 const u1* const current_start = cfs->current(); 3313 3314 guarantee_property(attribute_byte_length >= sizeof(u2), 3315 "Invalid BootstrapMethods attribute length %u in class file %s", 3316 attribute_byte_length, 3317 CHECK); 3318 3319 cfs->guarantee_more(attribute_byte_length, CHECK); 3320 3321 const int attribute_array_length = cfs->get_u2_fast(); 3322 3323 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length, 3324 "Short length on BootstrapMethods in class file %s", 3325 CHECK); 3326 3327 3328 // The attribute contains a counted array of counted tuples of shorts, 3329 // represending bootstrap specifiers: 3330 // length*{bootstrap_method_index, argument_count*{argument_index}} 3331 const int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2); 3332 // operand_count = number of shorts in attr, except for leading length 3333 3334 // The attribute is copied into a short[] array. 3335 // The array begins with a series of short[2] pairs, one for each tuple. 3336 const int index_size = (attribute_array_length * 2); 3337 3338 Array<u2>* const operands = 3339 MetadataFactory::new_array<u2>(_loader_data, index_size + operand_count, CHECK); 3340 3341 // Eagerly assign operands so they will be deallocated with the constant 3342 // pool if there is an error. 3343 cp->set_operands(operands); 3344 3345 int operand_fill_index = index_size; 3346 const int cp_size = cp->length(); 3347 3348 for (int n = 0; n < attribute_array_length; n++) { 3349 // Store a 32-bit offset into the header of the operand array. 3350 ConstantPool::operand_offset_at_put(operands, n, operand_fill_index); 3351 3352 // Read a bootstrap specifier. 3353 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc 3354 const u2 bootstrap_method_index = cfs->get_u2_fast(); 3355 const u2 argument_count = cfs->get_u2_fast(); 3356 check_property( 3357 valid_cp_range(bootstrap_method_index, cp_size) && 3358 cp->tag_at(bootstrap_method_index).is_method_handle(), 3359 "bootstrap_method_index %u has bad constant type in class file %s", 3360 bootstrap_method_index, 3361 CHECK); 3362 3363 guarantee_property((operand_fill_index + 1 + argument_count) < operands->length(), 3364 "Invalid BootstrapMethods num_bootstrap_methods or num_bootstrap_arguments value in class file %s", 3365 CHECK); 3366 3367 operands->at_put(operand_fill_index++, bootstrap_method_index); 3368 operands->at_put(operand_fill_index++, argument_count); 3369 3370 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc] 3371 for (int j = 0; j < argument_count; j++) { 3372 const u2 argument_index = cfs->get_u2_fast(); 3373 check_property( 3374 valid_cp_range(argument_index, cp_size) && 3375 cp->tag_at(argument_index).is_loadable_constant(), 3376 "argument_index %u has bad constant type in class file %s", 3377 argument_index, 3378 CHECK); 3379 operands->at_put(operand_fill_index++, argument_index); 3380 } 3381 } 3382 guarantee_property(current_start + attribute_byte_length == cfs->current(), 3383 "Bad length on BootstrapMethods in class file %s", 3384 CHECK); 3385 } 3386 3387 void ClassFileParser::parse_classfile_attributes(const ClassFileStream* const cfs, 3388 ConstantPool* cp, 3389 ClassFileParser::ClassAnnotationCollector* parsed_annotations, 3390 TRAPS) { 3391 assert(cfs != NULL, "invariant"); 3392 assert(cp != NULL, "invariant"); 3393 assert(parsed_annotations != NULL, "invariant"); 3394 3395 // Set inner classes attribute to default sentinel 3396 _inner_classes = Universe::the_empty_short_array(); 3397 // Set nest members attribute to default sentinel 3398 _nest_members = Universe::the_empty_short_array(); 3399 cfs->guarantee_more(2, CHECK); // attributes_count 3400 u2 attributes_count = cfs->get_u2_fast(); 3401 bool parsed_sourcefile_attribute = false; 3402 bool parsed_innerclasses_attribute = false; 3403 bool parsed_nest_members_attribute = false; 3404 bool parsed_nest_host_attribute = false; 3405 bool parsed_enclosingmethod_attribute = false; 3406 bool parsed_bootstrap_methods_attribute = false; 3407 const u1* runtime_visible_annotations = NULL; 3408 int runtime_visible_annotations_length = 0; 3409 const u1* runtime_invisible_annotations = NULL; 3410 int runtime_invisible_annotations_length = 0; 3411 const u1* runtime_visible_type_annotations = NULL; 3412 int runtime_visible_type_annotations_length = 0; 3413 const u1* runtime_invisible_type_annotations = NULL; 3414 int runtime_invisible_type_annotations_length = 0; 3415 bool runtime_invisible_type_annotations_exists = false; 3416 bool runtime_invisible_annotations_exists = false; 3417 bool parsed_source_debug_ext_annotations_exist = false; 3418 const u1* inner_classes_attribute_start = NULL; 3419 u4 inner_classes_attribute_length = 0; 3420 const u1* value_types_attribute_start = NULL; 3421 u4 value_types_attribute_length = 0; 3422 u2 enclosing_method_class_index = 0; 3423 u2 enclosing_method_method_index = 0; 3424 const u1* nest_members_attribute_start = NULL; 3425 u4 nest_members_attribute_length = 0; 3426 3427 // Iterate over attributes 3428 while (attributes_count--) { 3429 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length 3430 const u2 attribute_name_index = cfs->get_u2_fast(); 3431 const u4 attribute_length = cfs->get_u4_fast(); 3432 check_property( 3433 valid_symbol_at(attribute_name_index), 3434 "Attribute name has bad constant pool index %u in class file %s", 3435 attribute_name_index, CHECK); 3436 const Symbol* const tag = cp->symbol_at(attribute_name_index); 3437 if (tag == vmSymbols::tag_source_file()) { 3438 // Check for SourceFile tag 3439 if (_need_verify) { 3440 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK); 3441 } 3442 if (parsed_sourcefile_attribute) { 3443 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK); 3444 } else { 3445 parsed_sourcefile_attribute = true; 3446 } 3447 parse_classfile_sourcefile_attribute(cfs, CHECK); 3448 } else if (tag == vmSymbols::tag_source_debug_extension()) { 3449 // Check for SourceDebugExtension tag 3450 if (parsed_source_debug_ext_annotations_exist) { 3451 classfile_parse_error( 3452 "Multiple SourceDebugExtension attributes in class file %s", CHECK); 3453 } 3454 parsed_source_debug_ext_annotations_exist = true; 3455 parse_classfile_source_debug_extension_attribute(cfs, (int)attribute_length, CHECK); 3456 } else if (tag == vmSymbols::tag_inner_classes()) { 3457 // Check for InnerClasses tag 3458 if (parsed_innerclasses_attribute) { 3459 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK); 3460 } else { 3461 parsed_innerclasses_attribute = true; 3462 } 3463 inner_classes_attribute_start = cfs->current(); 3464 inner_classes_attribute_length = attribute_length; 3465 cfs->skip_u1(inner_classes_attribute_length, CHECK); 3466 } else if (tag == vmSymbols::tag_synthetic()) { 3467 // Check for Synthetic tag 3468 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec 3469 if (attribute_length != 0) { 3470 classfile_parse_error( 3471 "Invalid Synthetic classfile attribute length %u in class file %s", 3472 attribute_length, CHECK); 3473 } 3474 parse_classfile_synthetic_attribute(CHECK); 3475 } else if (tag == vmSymbols::tag_deprecated()) { 3476 // Check for Deprecatd tag - 4276120 3477 if (attribute_length != 0) { 3478 classfile_parse_error( 3479 "Invalid Deprecated classfile attribute length %u in class file %s", 3480 attribute_length, CHECK); 3481 } 3482 } else if (_major_version >= JAVA_1_5_VERSION) { 3483 if (tag == vmSymbols::tag_signature()) { 3484 if (_generic_signature_index != 0) { 3485 classfile_parse_error( 3486 "Multiple Signature attributes in class file %s", CHECK); 3487 } 3488 if (attribute_length != 2) { 3489 classfile_parse_error( 3490 "Wrong Signature attribute length %u in class file %s", 3491 attribute_length, CHECK); 3492 } 3493 parse_classfile_signature_attribute(cfs, CHECK); 3494 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { 3495 if (runtime_visible_annotations != NULL) { 3496 classfile_parse_error( 3497 "Multiple RuntimeVisibleAnnotations attributes in class file %s", CHECK); 3498 } 3499 runtime_visible_annotations_length = attribute_length; 3500 runtime_visible_annotations = cfs->current(); 3501 assert(runtime_visible_annotations != NULL, "null visible annotations"); 3502 cfs->guarantee_more(runtime_visible_annotations_length, CHECK); 3503 parse_annotations(cp, 3504 runtime_visible_annotations, 3505 runtime_visible_annotations_length, 3506 parsed_annotations, 3507 _loader_data, 3508 CHECK); 3509 cfs->skip_u1_fast(runtime_visible_annotations_length); 3510 } else if (tag == vmSymbols::tag_runtime_invisible_annotations()) { 3511 if (runtime_invisible_annotations_exists) { 3512 classfile_parse_error( 3513 "Multiple RuntimeInvisibleAnnotations attributes in class file %s", CHECK); 3514 } 3515 runtime_invisible_annotations_exists = true; 3516 if (PreserveAllAnnotations) { 3517 runtime_invisible_annotations_length = attribute_length; 3518 runtime_invisible_annotations = cfs->current(); 3519 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 3520 } 3521 cfs->skip_u1(attribute_length, CHECK); 3522 } else if (tag == vmSymbols::tag_enclosing_method()) { 3523 if (parsed_enclosingmethod_attribute) { 3524 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK); 3525 } else { 3526 parsed_enclosingmethod_attribute = true; 3527 } 3528 guarantee_property(attribute_length == 4, 3529 "Wrong EnclosingMethod attribute length %u in class file %s", 3530 attribute_length, CHECK); 3531 cfs->guarantee_more(4, CHECK); // class_index, method_index 3532 enclosing_method_class_index = cfs->get_u2_fast(); 3533 enclosing_method_method_index = cfs->get_u2_fast(); 3534 if (enclosing_method_class_index == 0) { 3535 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK); 3536 } 3537 // Validate the constant pool indices and types 3538 check_property(valid_klass_reference_at(enclosing_method_class_index), 3539 "Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK); 3540 if (enclosing_method_method_index != 0 && 3541 (!cp->is_within_bounds(enclosing_method_method_index) || 3542 !cp->tag_at(enclosing_method_method_index).is_name_and_type())) { 3543 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK); 3544 } 3545 } else if (tag == vmSymbols::tag_bootstrap_methods() && 3546 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 3547 if (parsed_bootstrap_methods_attribute) { 3548 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK); 3549 } 3550 parsed_bootstrap_methods_attribute = true; 3551 parse_classfile_bootstrap_methods_attribute(cfs, cp, attribute_length, CHECK); 3552 } else if (tag == vmSymbols::tag_runtime_visible_type_annotations()) { 3553 if (runtime_visible_type_annotations != NULL) { 3554 classfile_parse_error( 3555 "Multiple RuntimeVisibleTypeAnnotations attributes in class file %s", CHECK); 3556 } 3557 runtime_visible_type_annotations_length = attribute_length; 3558 runtime_visible_type_annotations = cfs->current(); 3559 assert(runtime_visible_type_annotations != NULL, "null visible type annotations"); 3560 // No need for the VM to parse Type annotations 3561 cfs->skip_u1(runtime_visible_type_annotations_length, CHECK); 3562 } else if (tag == vmSymbols::tag_runtime_invisible_type_annotations()) { 3563 if (runtime_invisible_type_annotations_exists) { 3564 classfile_parse_error( 3565 "Multiple RuntimeInvisibleTypeAnnotations attributes in class file %s", CHECK); 3566 } else { 3567 runtime_invisible_type_annotations_exists = true; 3568 } 3569 if (PreserveAllAnnotations) { 3570 runtime_invisible_type_annotations_length = attribute_length; 3571 runtime_invisible_type_annotations = cfs->current(); 3572 assert(runtime_invisible_type_annotations != NULL, "null invisible type annotations"); 3573 } 3574 cfs->skip_u1(attribute_length, CHECK); 3575 } else if (_major_version >= JAVA_11_VERSION) { 3576 if (tag == vmSymbols::tag_nest_members()) { 3577 // Check for NestMembers tag 3578 if (parsed_nest_members_attribute) { 3579 classfile_parse_error("Multiple NestMembers attributes in class file %s", CHECK); 3580 } else { 3581 parsed_nest_members_attribute = true; 3582 } 3583 if (parsed_nest_host_attribute) { 3584 classfile_parse_error("Conflicting NestHost and NestMembers attributes in class file %s", CHECK); 3585 } 3586 nest_members_attribute_start = cfs->current(); 3587 nest_members_attribute_length = attribute_length; 3588 cfs->skip_u1(nest_members_attribute_length, CHECK); 3589 } else if (tag == vmSymbols::tag_nest_host()) { 3590 if (parsed_nest_host_attribute) { 3591 classfile_parse_error("Multiple NestHost attributes in class file %s", CHECK); 3592 } else { 3593 parsed_nest_host_attribute = true; 3594 } 3595 if (parsed_nest_members_attribute) { 3596 classfile_parse_error("Conflicting NestMembers and NestHost attributes in class file %s", CHECK); 3597 } 3598 if (_need_verify) { 3599 guarantee_property(attribute_length == 2, "Wrong NestHost attribute length in class file %s", CHECK); 3600 } 3601 cfs->guarantee_more(2, CHECK); 3602 u2 class_info_index = cfs->get_u2_fast(); 3603 check_property( 3604 valid_klass_reference_at(class_info_index), 3605 "Nest-host class_info_index %u has bad constant type in class file %s", 3606 class_info_index, CHECK); 3607 _nest_host = class_info_index; 3608 } else { 3609 // Unknown attribute 3610 cfs->skip_u1(attribute_length, CHECK); 3611 } 3612 } else { 3613 // Unknown attribute 3614 cfs->skip_u1(attribute_length, CHECK); 3615 } 3616 } else { 3617 // Unknown attribute 3618 cfs->skip_u1(attribute_length, CHECK); 3619 } 3620 } 3621 _annotations = assemble_annotations(runtime_visible_annotations, 3622 runtime_visible_annotations_length, 3623 runtime_invisible_annotations, 3624 runtime_invisible_annotations_length, 3625 CHECK); 3626 _type_annotations = assemble_annotations(runtime_visible_type_annotations, 3627 runtime_visible_type_annotations_length, 3628 runtime_invisible_type_annotations, 3629 runtime_invisible_type_annotations_length, 3630 CHECK); 3631 3632 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 3633 const u2 num_of_classes = parse_classfile_inner_classes_attribute( 3634 cfs, 3635 inner_classes_attribute_start, 3636 parsed_innerclasses_attribute, 3637 enclosing_method_class_index, 3638 enclosing_method_method_index, 3639 CHECK); 3640 if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) { 3641 guarantee_property( 3642 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 3643 "Wrong InnerClasses attribute length in class file %s", CHECK); 3644 } 3645 } 3646 3647 if (parsed_nest_members_attribute) { 3648 const u2 num_of_classes = parse_classfile_nest_members_attribute( 3649 cfs, 3650 nest_members_attribute_start, 3651 CHECK); 3652 if (_need_verify) { 3653 guarantee_property( 3654 nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes, 3655 "Wrong NestMembers attribute length in class file %s", CHECK); 3656 } 3657 } 3658 3659 if (_max_bootstrap_specifier_index >= 0) { 3660 guarantee_property(parsed_bootstrap_methods_attribute, 3661 "Missing BootstrapMethods attribute in class file %s", CHECK); 3662 } 3663 } 3664 3665 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) { 3666 assert(k != NULL, "invariant"); 3667 3668 if (_synthetic_flag) 3669 k->set_is_synthetic(); 3670 if (_sourcefile_index != 0) { 3671 k->set_source_file_name_index(_sourcefile_index); 3672 } 3673 if (_generic_signature_index != 0) { 3674 k->set_generic_signature_index(_generic_signature_index); 3675 } 3676 if (_sde_buffer != NULL) { 3677 k->set_source_debug_extension(_sde_buffer, _sde_length); 3678 } 3679 } 3680 3681 // Create the Annotations object that will 3682 // hold the annotations array for the Klass. 3683 void ClassFileParser::create_combined_annotations(TRAPS) { 3684 if (_annotations == NULL && 3685 _type_annotations == NULL && 3686 _fields_annotations == NULL && 3687 _fields_type_annotations == NULL) { 3688 // Don't create the Annotations object unnecessarily. 3689 return; 3690 } 3691 3692 Annotations* const annotations = Annotations::allocate(_loader_data, CHECK); 3693 annotations->set_class_annotations(_annotations); 3694 annotations->set_class_type_annotations(_type_annotations); 3695 annotations->set_fields_annotations(_fields_annotations); 3696 annotations->set_fields_type_annotations(_fields_type_annotations); 3697 3698 // This is the Annotations object that will be 3699 // assigned to InstanceKlass being constructed. 3700 _combined_annotations = annotations; 3701 3702 // The annotations arrays below has been transfered the 3703 // _combined_annotations so these fields can now be cleared. 3704 _annotations = NULL; 3705 _type_annotations = NULL; 3706 _fields_annotations = NULL; 3707 _fields_type_annotations = NULL; 3708 } 3709 3710 // Transfer ownership of metadata allocated to the InstanceKlass. 3711 void ClassFileParser::apply_parsed_class_metadata( 3712 InstanceKlass* this_klass, 3713 int java_fields_count, TRAPS) { 3714 assert(this_klass != NULL, "invariant"); 3715 3716 _cp->set_pool_holder(this_klass); 3717 this_klass->set_constants(_cp); 3718 this_klass->set_fields(_fields, java_fields_count); 3719 this_klass->set_methods(_methods); 3720 this_klass->set_inner_classes(_inner_classes); 3721 this_klass->set_nest_members(_nest_members); 3722 this_klass->set_nest_host_index(_nest_host); 3723 this_klass->set_local_interfaces(_local_interfaces); 3724 this_klass->set_annotations(_combined_annotations); 3725 // Delay the setting of _transitive_interfaces until after initialize_supers() in 3726 // fill_instance_klass(). It is because the _transitive_interfaces may be shared with 3727 // its _super. If an OOM occurs while loading the current klass, its _super field 3728 // may not have been set. When GC tries to free the klass, the _transitive_interfaces 3729 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent 3730 // dereferences to the deallocated _transitive_interfaces will result in a crash. 3731 3732 // Clear out these fields so they don't get deallocated by the destructor 3733 clear_class_metadata(); 3734 } 3735 3736 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations, 3737 int runtime_visible_annotations_length, 3738 const u1* const runtime_invisible_annotations, 3739 int runtime_invisible_annotations_length, 3740 TRAPS) { 3741 AnnotationArray* annotations = NULL; 3742 if (runtime_visible_annotations != NULL || 3743 runtime_invisible_annotations != NULL) { 3744 annotations = MetadataFactory::new_array<u1>(_loader_data, 3745 runtime_visible_annotations_length + 3746 runtime_invisible_annotations_length, 3747 CHECK_(annotations)); 3748 if (runtime_visible_annotations != NULL) { 3749 for (int i = 0; i < runtime_visible_annotations_length; i++) { 3750 annotations->at_put(i, runtime_visible_annotations[i]); 3751 } 3752 } 3753 if (runtime_invisible_annotations != NULL) { 3754 for (int i = 0; i < runtime_invisible_annotations_length; i++) { 3755 int append = runtime_visible_annotations_length+i; 3756 annotations->at_put(append, runtime_invisible_annotations[i]); 3757 } 3758 } 3759 } 3760 return annotations; 3761 } 3762 3763 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp, 3764 const int super_class_index, 3765 const bool need_verify, 3766 TRAPS) { 3767 assert(cp != NULL, "invariant"); 3768 const InstanceKlass* super_klass = NULL; 3769 3770 if (super_class_index == 0) { 3771 check_property(_class_name == vmSymbols::java_lang_Object() 3772 || (_access_flags.get_flags() & JVM_ACC_VALUE), 3773 "Invalid superclass index %u in class file %s", 3774 super_class_index, 3775 CHECK_NULL); 3776 } else { 3777 check_property(valid_klass_reference_at(super_class_index), 3778 "Invalid superclass index %u in class file %s", 3779 super_class_index, 3780 CHECK_NULL); 3781 // The class name should be legal because it is checked when parsing constant pool. 3782 // However, make sure it is not an array type. 3783 bool is_array = false; 3784 if (cp->tag_at(super_class_index).is_klass()) { 3785 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index)); 3786 if (need_verify) 3787 is_array = super_klass->is_array_klass(); 3788 } else if (need_verify) { 3789 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY); 3790 } 3791 if (need_verify) { 3792 guarantee_property(!is_array, 3793 "Bad superclass name in class file %s", CHECK_NULL); 3794 } 3795 } 3796 return super_klass; 3797 } 3798 3799 #ifndef PRODUCT 3800 static void print_field_layout(const Symbol* name, 3801 Array<u2>* fields, 3802 const constantPoolHandle& cp, 3803 int instance_size, 3804 int instance_fields_start, 3805 int instance_fields_end, 3806 int static_fields_end) { 3807 3808 assert(name != NULL, "invariant"); 3809 3810 tty->print("%s: field layout\n", name->as_klass_external_name()); 3811 tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---"); 3812 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 3813 if (!fs.access_flags().is_static()) { 3814 tty->print(" @%3d \"%s\" %s\n", 3815 fs.offset(), 3816 fs.name()->as_klass_external_name(), 3817 fs.signature()->as_klass_external_name()); 3818 } 3819 } 3820 tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---"); 3821 tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---"); 3822 tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---"); 3823 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 3824 if (fs.access_flags().is_static()) { 3825 tty->print(" @%3d \"%s\" %s\n", 3826 fs.offset(), 3827 fs.name()->as_klass_external_name(), 3828 fs.signature()->as_klass_external_name()); 3829 } 3830 } 3831 tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---"); 3832 tty->print("\n"); 3833 } 3834 #endif 3835 3836 // Values needed for oopmap and InstanceKlass creation 3837 class ClassFileParser::FieldLayoutInfo : public ResourceObj { 3838 public: 3839 OopMapBlocksBuilder* oop_map_blocks; 3840 int instance_size; 3841 int nonstatic_field_size; 3842 int static_field_size; 3843 bool has_nonstatic_fields; 3844 }; 3845 3846 // Utility to collect and compact oop maps during layout 3847 class ClassFileParser::OopMapBlocksBuilder : public ResourceObj { 3848 public: 3849 OopMapBlock* nonstatic_oop_maps; 3850 unsigned int nonstatic_oop_map_count; 3851 unsigned int max_nonstatic_oop_maps; 3852 3853 public: 3854 OopMapBlocksBuilder(unsigned int max_blocks, TRAPS) { 3855 max_nonstatic_oop_maps = max_blocks; 3856 nonstatic_oop_map_count = 0; 3857 if (max_blocks == 0) { 3858 nonstatic_oop_maps = NULL; 3859 } else { 3860 nonstatic_oop_maps = NEW_RESOURCE_ARRAY_IN_THREAD( 3861 THREAD, OopMapBlock, max_nonstatic_oop_maps); 3862 memset(nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks); 3863 } 3864 } 3865 3866 OopMapBlock* last_oop_map() const { 3867 assert(nonstatic_oop_map_count > 0, "Has no oop maps"); 3868 return nonstatic_oop_maps + (nonstatic_oop_map_count - 1); 3869 } 3870 3871 // addition of super oop maps 3872 void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) { 3873 assert(nof_blocks && nonstatic_oop_map_count == 0 && 3874 nof_blocks <= max_nonstatic_oop_maps, "invariant"); 3875 3876 memcpy(nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks); 3877 nonstatic_oop_map_count += nof_blocks; 3878 } 3879 3880 // collection of oops 3881 void add(int offset, int count) { 3882 if (nonstatic_oop_map_count == 0) { 3883 nonstatic_oop_map_count++; 3884 } 3885 OopMapBlock* nonstatic_oop_map = last_oop_map(); 3886 if (nonstatic_oop_map->count() == 0) { // Unused map, set it up 3887 nonstatic_oop_map->set_offset(offset); 3888 nonstatic_oop_map->set_count(count); 3889 } else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add 3890 nonstatic_oop_map->increment_count(count); 3891 } else { // Need a new one... 3892 nonstatic_oop_map_count++; 3893 assert(nonstatic_oop_map_count <= max_nonstatic_oop_maps, "range check"); 3894 nonstatic_oop_map = last_oop_map(); 3895 nonstatic_oop_map->set_offset(offset); 3896 nonstatic_oop_map->set_count(count); 3897 } 3898 } 3899 3900 // general purpose copy, e.g. into allocated instanceKlass 3901 void copy(OopMapBlock* dst) { 3902 if (nonstatic_oop_map_count != 0) { 3903 memcpy(dst, nonstatic_oop_maps, sizeof(OopMapBlock) * nonstatic_oop_map_count); 3904 } 3905 } 3906 3907 // Sort and compact adjacent blocks 3908 void compact(TRAPS) { 3909 if (nonstatic_oop_map_count <= 1) { 3910 return; 3911 } 3912 /* 3913 * Since field layout sneeks in oops before values, we will be able to condense 3914 * blocks. There is potential to compact between super, own refs and values 3915 * containing refs. 3916 * 3917 * Currently compaction is slightly limited due to values being 8 byte aligned. 3918 * This may well change: FixMe if doesn't, the code below is fairly general purpose 3919 * and maybe it doesn't need to be. 3920 */ 3921 qsort(nonstatic_oop_maps, nonstatic_oop_map_count, sizeof(OopMapBlock), 3922 (_sort_Fn)OopMapBlock::compare_offset); 3923 if (nonstatic_oop_map_count < 2) { 3924 return; 3925 } 3926 3927 //Make a temp copy, and iterate through and copy back into the orig 3928 ResourceMark rm(THREAD); 3929 OopMapBlock* oop_maps_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, OopMapBlock, 3930 nonstatic_oop_map_count); 3931 OopMapBlock* oop_maps_copy_end = oop_maps_copy + nonstatic_oop_map_count; 3932 copy(oop_maps_copy); 3933 OopMapBlock* nonstatic_oop_map = nonstatic_oop_maps; 3934 unsigned int new_count = 1; 3935 oop_maps_copy++; 3936 while(oop_maps_copy < oop_maps_copy_end) { 3937 assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant"); 3938 if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) { 3939 nonstatic_oop_map->increment_count(oop_maps_copy->count()); 3940 } else { 3941 nonstatic_oop_map++; 3942 new_count++; 3943 nonstatic_oop_map->set_offset(oop_maps_copy->offset()); 3944 nonstatic_oop_map->set_count(oop_maps_copy->count()); 3945 } 3946 oop_maps_copy++; 3947 } 3948 assert(new_count <= nonstatic_oop_map_count, "end up with more maps after compact() ?"); 3949 nonstatic_oop_map_count = new_count; 3950 } 3951 3952 void print_on(outputStream* st) const { 3953 st->print_cr(" OopMapBlocks: %3d /%3d", nonstatic_oop_map_count, max_nonstatic_oop_maps); 3954 if (nonstatic_oop_map_count > 0) { 3955 OopMapBlock* map = nonstatic_oop_maps; 3956 OopMapBlock* last_map = last_oop_map(); 3957 assert(map <= last_map, "Last less than first"); 3958 while (map <= last_map) { 3959 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(), 3960 map->offset() + map->offset_span() - heapOopSize, map->count()); 3961 map++; 3962 } 3963 } 3964 } 3965 3966 void print_value_on(outputStream* st) const { 3967 print_on(st); 3968 } 3969 3970 }; 3971 3972 void ClassFileParser::throwValueTypeLimitation(THREAD_AND_LOCATION_DECL, 3973 const char* msg, 3974 const Symbol* name, 3975 const Symbol* sig) const { 3976 3977 ResourceMark rm(THREAD); 3978 if (name == NULL || sig == NULL) { 3979 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS, 3980 vmSymbols::java_lang_ClassFormatError(), 3981 "class: %s - %s", _class_name->as_C_string(), msg); 3982 } 3983 else { 3984 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS, 3985 vmSymbols::java_lang_ClassFormatError(), 3986 "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(), 3987 _class_name->as_C_string(), msg); 3988 } 3989 } 3990 3991 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring! 3992 void ClassFileParser::layout_fields(ConstantPool* cp, 3993 const FieldAllocationCount* fac, 3994 const ClassAnnotationCollector* parsed_annotations, 3995 FieldLayoutInfo* info, 3996 TRAPS) { 3997 3998 assert(cp != NULL, "invariant"); 3999 4000 // Field size and offset computation 4001 int nonstatic_field_size = _super_klass == NULL ? 0 : 4002 _super_klass->nonstatic_field_size(); 4003 int next_nonstatic_valuetype_offset = 0; 4004 int first_nonstatic_valuetype_offset = 0; 4005 4006 // Fields that are value types are handled differently depending if they are static or not: 4007 // - static fields are oops 4008 // - non-static fields are embedded 4009 4010 // Count the contended fields by type. 4011 // 4012 // We ignore static fields, because @Contended is not supported for them. 4013 // The layout code below will also ignore the static fields. 4014 int nonstatic_contended_count = 0; 4015 FieldAllocationCount fac_contended; 4016 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4017 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 4018 if (fs.is_contended()) { 4019 fac_contended.count[atype]++; 4020 if (!fs.access_flags().is_static()) { 4021 nonstatic_contended_count++; 4022 } 4023 } 4024 } 4025 4026 4027 // Calculate the starting byte offsets 4028 int next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields(); 4029 // Value types in static fields are not embedded, they are handled with oops 4030 int next_static_double_offset = next_static_oop_offset + 4031 ((fac->count[STATIC_OOP] + fac->count[STATIC_FLATTENABLE]) * heapOopSize); 4032 if (fac->count[STATIC_DOUBLE]) { 4033 next_static_double_offset = align_up(next_static_double_offset, BytesPerLong); 4034 } 4035 4036 int next_static_word_offset = next_static_double_offset + 4037 ((fac->count[STATIC_DOUBLE]) * BytesPerLong); 4038 int next_static_short_offset = next_static_word_offset + 4039 ((fac->count[STATIC_WORD]) * BytesPerInt); 4040 int next_static_byte_offset = next_static_short_offset + 4041 ((fac->count[STATIC_SHORT]) * BytesPerShort); 4042 4043 int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() + 4044 nonstatic_field_size * heapOopSize; 4045 4046 // First field of value types is aligned on a long boundary in order to ease 4047 // in-lining of value types (with header removal) in packed arrays and 4048 // flatten value types 4049 int initial_value_type_padding = 0; 4050 if (is_value_type()) { 4051 int old = nonstatic_fields_start; 4052 nonstatic_fields_start = align_up(nonstatic_fields_start, BytesPerLong); 4053 initial_value_type_padding = nonstatic_fields_start - old; 4054 } 4055 4056 int next_nonstatic_field_offset = nonstatic_fields_start; 4057 4058 const bool is_contended_class = parsed_annotations->is_contended(); 4059 4060 // Class is contended, pad before all the fields 4061 if (is_contended_class) { 4062 next_nonstatic_field_offset += ContendedPaddingWidth; 4063 } 4064 4065 // Temporary value types restrictions 4066 if (is_value_type()) { 4067 if (is_contended_class) { 4068 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support @Contended annotation yet"); 4069 return; 4070 } 4071 } 4072 4073 // Compute the non-contended fields count. 4074 // The packing code below relies on these counts to determine if some field 4075 // can be squeezed into the alignment gap. Contended fields are obviously 4076 // exempt from that. 4077 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE]; 4078 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD]; 4079 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT]; 4080 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE]; 4081 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP]; 4082 4083 int static_value_type_count = 0; 4084 int nonstatic_value_type_count = 0; 4085 int* nonstatic_value_type_indexes = NULL; 4086 Klass** nonstatic_value_type_klasses = NULL; 4087 unsigned int value_type_oop_map_count = 0; 4088 int not_flattened_value_types = 0; 4089 4090 int max_nonstatic_value_type = fac->count[NONSTATIC_FLATTENABLE] + 1; 4091 4092 nonstatic_value_type_indexes = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, int, 4093 max_nonstatic_value_type); 4094 for (int i = 0; i < max_nonstatic_value_type; i++) { 4095 nonstatic_value_type_indexes[i] = -1; 4096 } 4097 nonstatic_value_type_klasses = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, Klass*, 4098 max_nonstatic_value_type); 4099 4100 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) { 4101 if (fs.allocation_type() == STATIC_FLATTENABLE) { 4102 // Pre-resolve the flattenable field and check for value type circularity 4103 // issues. Note that super-class circularity checks are not needed here 4104 // because flattenable fields can only be in value types and value types 4105 // only have java.lang.Object as their super class. 4106 // Also, note that super-interface circularity checks are not needed 4107 // because interfaces cannot be value types. 4108 ResourceMark rm; 4109 if (!fs.signature()->is_Q_signature()) { 4110 THROW(vmSymbols::java_lang_ClassFormatError()); 4111 } 4112 Klass* klass = 4113 SystemDictionary::resolve_flattenable_field_or_fail(&fs, 4114 Handle(THREAD, _loader_data->class_loader()), 4115 _protection_domain, true, CHECK); 4116 assert(klass != NULL, "Sanity check"); 4117 if (!klass->access_flags().is_value_type()) { 4118 THROW(vmSymbols::java_lang_IncompatibleClassChangeError()); 4119 } 4120 static_value_type_count++; 4121 } else if (fs.allocation_type() == NONSTATIC_FLATTENABLE) { 4122 // Pre-resolve the flattenable field and check for value type circularity issues. 4123 ResourceMark rm; 4124 if (!fs.signature()->is_Q_signature()) { 4125 THROW(vmSymbols::java_lang_ClassFormatError()); 4126 } 4127 Klass* klass = 4128 SystemDictionary::resolve_flattenable_field_or_fail(&fs, 4129 Handle(THREAD, _loader_data->class_loader()), 4130 _protection_domain, true, CHECK); 4131 assert(klass != NULL, "Sanity check"); 4132 if (!klass->access_flags().is_value_type()) { 4133 THROW(vmSymbols::java_lang_IncompatibleClassChangeError()); 4134 } 4135 ValueKlass* vk = ValueKlass::cast(klass); 4136 // Conditions to apply flattening or not should be defined in a single place 4137 if ((ValueFieldMaxFlatSize < 0) || (vk->size_helper() * HeapWordSize) <= ValueFieldMaxFlatSize) { 4138 nonstatic_value_type_indexes[nonstatic_value_type_count] = fs.index(); 4139 nonstatic_value_type_klasses[nonstatic_value_type_count] = klass; 4140 nonstatic_value_type_count++; 4141 4142 ValueKlass* vklass = ValueKlass::cast(klass); 4143 if (vklass->contains_oops()) { 4144 value_type_oop_map_count += vklass->nonstatic_oop_map_count(); 4145 } 4146 fs.set_flattened(true); 4147 } else { 4148 not_flattened_value_types++; 4149 fs.set_flattened(false); 4150 } 4151 } 4152 } 4153 4154 // Adjusting non_static_oop_count to take into account not flattened value types; 4155 nonstatic_oop_count += not_flattened_value_types; 4156 4157 // Total non-static fields count, including every contended field 4158 unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] + 4159 fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] + 4160 fac->count[NONSTATIC_OOP] + fac->count[NONSTATIC_FLATTENABLE]; 4161 4162 const bool super_has_nonstatic_fields = 4163 (_super_klass != NULL && _super_klass->has_nonstatic_fields()); 4164 const bool has_nonstatic_fields = 4165 super_has_nonstatic_fields || (nonstatic_fields_count != 0); 4166 const bool has_nonstatic_value_fields = nonstatic_value_type_count > 0; 4167 4168 if (is_value_type() && (!has_nonstatic_fields)) { 4169 // There are a number of fixes required throughout the type system and JIT 4170 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support zero instance size yet"); 4171 return; 4172 } 4173 4174 // Prepare list of oops for oop map generation. 4175 // 4176 // "offset" and "count" lists are describing the set of contiguous oop 4177 // regions. offset[i] is the start of the i-th region, which then has 4178 // count[i] oops following. Before we know how many regions are required, 4179 // we pessimistically allocate the maps to fit all the oops into the 4180 // distinct regions. 4181 // 4182 int super_oop_map_count = (_super_klass == NULL) ? 0 :_super_klass->nonstatic_oop_map_count(); 4183 int max_oop_map_count = 4184 super_oop_map_count + 4185 fac->count[NONSTATIC_OOP] + 4186 value_type_oop_map_count + 4187 not_flattened_value_types; 4188 4189 OopMapBlocksBuilder* nonstatic_oop_maps = new OopMapBlocksBuilder(max_oop_map_count, THREAD); 4190 if (super_oop_map_count > 0) { 4191 nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(), 4192 _super_klass->nonstatic_oop_map_count()); 4193 } 4194 4195 int first_nonstatic_oop_offset = 0; // will be set for first oop field 4196 4197 bool compact_fields = CompactFields; 4198 int allocation_style = FieldsAllocationStyle; 4199 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range? 4200 assert(false, "0 <= FieldsAllocationStyle <= 2"); 4201 allocation_style = 1; // Optimistic 4202 } 4203 4204 // The next classes have predefined hard-coded fields offsets 4205 // (see in JavaClasses::compute_hard_coded_offsets()). 4206 // Use default fields allocation order for them. 4207 if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL && 4208 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() || 4209 _class_name == vmSymbols::java_lang_Class() || 4210 _class_name == vmSymbols::java_lang_ClassLoader() || 4211 _class_name == vmSymbols::java_lang_ref_Reference() || 4212 _class_name == vmSymbols::java_lang_ref_SoftReference() || 4213 _class_name == vmSymbols::java_lang_StackTraceElement() || 4214 _class_name == vmSymbols::java_lang_String() || 4215 _class_name == vmSymbols::java_lang_Throwable() || 4216 _class_name == vmSymbols::java_lang_Boolean() || 4217 _class_name == vmSymbols::java_lang_Character() || 4218 _class_name == vmSymbols::java_lang_Float() || 4219 _class_name == vmSymbols::java_lang_Double() || 4220 _class_name == vmSymbols::java_lang_Byte() || 4221 _class_name == vmSymbols::java_lang_Short() || 4222 _class_name == vmSymbols::java_lang_Integer() || 4223 _class_name == vmSymbols::java_lang_Long())) { 4224 allocation_style = 0; // Allocate oops first 4225 compact_fields = false; // Don't compact fields 4226 } 4227 4228 int next_nonstatic_oop_offset = 0; 4229 int next_nonstatic_double_offset = 0; 4230 4231 // Rearrange fields for a given allocation style 4232 if( allocation_style == 0 ) { 4233 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields 4234 next_nonstatic_oop_offset = next_nonstatic_field_offset; 4235 next_nonstatic_double_offset = next_nonstatic_oop_offset + 4236 (nonstatic_oop_count * heapOopSize); 4237 } else if( allocation_style == 1 ) { 4238 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields 4239 next_nonstatic_double_offset = next_nonstatic_field_offset; 4240 } else if( allocation_style == 2 ) { 4241 // Fields allocation: oops fields in super and sub classes are together. 4242 if( nonstatic_field_size > 0 && super_oop_map_count > 0 ) { 4243 if (next_nonstatic_field_offset == nonstatic_oop_maps->last_oop_map()->end_offset()) { 4244 allocation_style = 0; // allocate oops first 4245 next_nonstatic_oop_offset = next_nonstatic_field_offset; 4246 next_nonstatic_double_offset = next_nonstatic_oop_offset + 4247 (nonstatic_oop_count * heapOopSize); 4248 } 4249 } 4250 if( allocation_style == 2 ) { 4251 allocation_style = 1; // allocate oops last 4252 next_nonstatic_double_offset = next_nonstatic_field_offset; 4253 } 4254 } else { 4255 ShouldNotReachHere(); 4256 } 4257 4258 int nonstatic_oop_space_count = 0; 4259 int nonstatic_word_space_count = 0; 4260 int nonstatic_short_space_count = 0; 4261 int nonstatic_byte_space_count = 0; 4262 int nonstatic_oop_space_offset = 0; 4263 int nonstatic_word_space_offset = 0; 4264 int nonstatic_short_space_offset = 0; 4265 int nonstatic_byte_space_offset = 0; 4266 4267 // Try to squeeze some of the fields into the gaps due to 4268 // long/double alignment. 4269 if (nonstatic_double_count > 0) { 4270 int offset = next_nonstatic_double_offset; 4271 next_nonstatic_double_offset = align_up(offset, BytesPerLong); 4272 if (compact_fields && offset != next_nonstatic_double_offset) { 4273 // Allocate available fields into the gap before double field. 4274 int length = next_nonstatic_double_offset - offset; 4275 assert(length == BytesPerInt, ""); 4276 nonstatic_word_space_offset = offset; 4277 if (nonstatic_word_count > 0) { 4278 nonstatic_word_count -= 1; 4279 nonstatic_word_space_count = 1; // Only one will fit 4280 length -= BytesPerInt; 4281 offset += BytesPerInt; 4282 } 4283 nonstatic_short_space_offset = offset; 4284 while (length >= BytesPerShort && nonstatic_short_count > 0) { 4285 nonstatic_short_count -= 1; 4286 nonstatic_short_space_count += 1; 4287 length -= BytesPerShort; 4288 offset += BytesPerShort; 4289 } 4290 nonstatic_byte_space_offset = offset; 4291 while (length > 0 && nonstatic_byte_count > 0) { 4292 nonstatic_byte_count -= 1; 4293 nonstatic_byte_space_count += 1; 4294 length -= 1; 4295 } 4296 // Allocate oop field in the gap if there are no other fields for that. 4297 nonstatic_oop_space_offset = offset; 4298 if (length >= heapOopSize && nonstatic_oop_count > 0 && 4299 allocation_style != 0) { // when oop fields not first 4300 nonstatic_oop_count -= 1; 4301 nonstatic_oop_space_count = 1; // Only one will fit 4302 length -= heapOopSize; 4303 offset += heapOopSize; 4304 } 4305 } 4306 } 4307 4308 int next_nonstatic_word_offset = next_nonstatic_double_offset + 4309 (nonstatic_double_count * BytesPerLong); 4310 int next_nonstatic_short_offset = next_nonstatic_word_offset + 4311 (nonstatic_word_count * BytesPerInt); 4312 int next_nonstatic_byte_offset = next_nonstatic_short_offset + 4313 (nonstatic_short_count * BytesPerShort); 4314 int next_nonstatic_padded_offset = next_nonstatic_byte_offset + 4315 nonstatic_byte_count; 4316 4317 // let oops jump before padding with this allocation style 4318 if( allocation_style == 1 ) { 4319 next_nonstatic_oop_offset = next_nonstatic_padded_offset; 4320 if( nonstatic_oop_count > 0 ) { 4321 next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize); 4322 } 4323 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize); 4324 } 4325 4326 // Aligning embedded value types 4327 // bug below, the current algorithm to layout embedded value types always put them at the 4328 // end of the layout, which doesn't match the different allocation policies the VM is 4329 // supposed to provide => FixMe 4330 // Note also that the current alignment policy is to make each value type starting on a 4331 // 64 bits boundary. This could be optimized later. For instance, it could be nice to 4332 // align value types according to their most constrained internal type. 4333 next_nonstatic_valuetype_offset = align_up(next_nonstatic_padded_offset, BytesPerLong); 4334 int next_value_type_index = 0; 4335 4336 // Iterate over fields again and compute correct offsets. 4337 // The field allocation type was temporarily stored in the offset slot. 4338 // oop fields are located before non-oop fields (static and non-static). 4339 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4340 4341 // skip already laid out fields 4342 if (fs.is_offset_set()) continue; 4343 4344 // contended instance fields are handled below 4345 if (fs.is_contended() && !fs.access_flags().is_static()) continue; 4346 4347 int real_offset = 0; 4348 const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type(); 4349 4350 // pack the rest of the fields 4351 switch (atype) { 4352 // Value types in static fields are handled with oops 4353 case STATIC_FLATTENABLE: // Fallthrough 4354 case STATIC_OOP: 4355 real_offset = next_static_oop_offset; 4356 next_static_oop_offset += heapOopSize; 4357 break; 4358 case STATIC_BYTE: 4359 real_offset = next_static_byte_offset; 4360 next_static_byte_offset += 1; 4361 break; 4362 case STATIC_SHORT: 4363 real_offset = next_static_short_offset; 4364 next_static_short_offset += BytesPerShort; 4365 break; 4366 case STATIC_WORD: 4367 real_offset = next_static_word_offset; 4368 next_static_word_offset += BytesPerInt; 4369 break; 4370 case STATIC_DOUBLE: 4371 real_offset = next_static_double_offset; 4372 next_static_double_offset += BytesPerLong; 4373 break; 4374 case NONSTATIC_FLATTENABLE: 4375 if (fs.is_flattened()) { 4376 Klass* klass = nonstatic_value_type_klasses[next_value_type_index]; 4377 assert(klass != NULL, "Klass should have been loaded and resolved earlier"); 4378 assert(klass->access_flags().is_value_type(),"Must be a value type"); 4379 ValueKlass* vklass = ValueKlass::cast(klass); 4380 real_offset = next_nonstatic_valuetype_offset; 4381 next_nonstatic_valuetype_offset += (vklass->size_helper()) * wordSize - vklass->first_field_offset(); 4382 // aligning next value type on a 64 bits boundary 4383 next_nonstatic_valuetype_offset = align_up(next_nonstatic_valuetype_offset, BytesPerLong); 4384 next_value_type_index += 1; 4385 4386 if (vklass->contains_oops()) { // add flatten oop maps 4387 int diff = real_offset - vklass->first_field_offset(); 4388 const OopMapBlock* map = vklass->start_of_nonstatic_oop_maps(); 4389 const OopMapBlock* const last_map = map + vklass->nonstatic_oop_map_count(); 4390 while (map < last_map) { 4391 nonstatic_oop_maps->add(map->offset() + diff, map->count()); 4392 map++; 4393 } 4394 } 4395 break; 4396 } else { 4397 // Fall through 4398 } 4399 case NONSTATIC_OOP: 4400 if( nonstatic_oop_space_count > 0 ) { 4401 real_offset = nonstatic_oop_space_offset; 4402 nonstatic_oop_space_offset += heapOopSize; 4403 nonstatic_oop_space_count -= 1; 4404 } else { 4405 real_offset = next_nonstatic_oop_offset; 4406 next_nonstatic_oop_offset += heapOopSize; 4407 } 4408 nonstatic_oop_maps->add(real_offset, 1); 4409 break; 4410 case NONSTATIC_BYTE: 4411 if( nonstatic_byte_space_count > 0 ) { 4412 real_offset = nonstatic_byte_space_offset; 4413 nonstatic_byte_space_offset += 1; 4414 nonstatic_byte_space_count -= 1; 4415 } else { 4416 real_offset = next_nonstatic_byte_offset; 4417 next_nonstatic_byte_offset += 1; 4418 } 4419 break; 4420 case NONSTATIC_SHORT: 4421 if( nonstatic_short_space_count > 0 ) { 4422 real_offset = nonstatic_short_space_offset; 4423 nonstatic_short_space_offset += BytesPerShort; 4424 nonstatic_short_space_count -= 1; 4425 } else { 4426 real_offset = next_nonstatic_short_offset; 4427 next_nonstatic_short_offset += BytesPerShort; 4428 } 4429 break; 4430 case NONSTATIC_WORD: 4431 if( nonstatic_word_space_count > 0 ) { 4432 real_offset = nonstatic_word_space_offset; 4433 nonstatic_word_space_offset += BytesPerInt; 4434 nonstatic_word_space_count -= 1; 4435 } else { 4436 real_offset = next_nonstatic_word_offset; 4437 next_nonstatic_word_offset += BytesPerInt; 4438 } 4439 break; 4440 case NONSTATIC_DOUBLE: 4441 real_offset = next_nonstatic_double_offset; 4442 next_nonstatic_double_offset += BytesPerLong; 4443 break; 4444 default: 4445 ShouldNotReachHere(); 4446 } 4447 fs.set_offset(real_offset); 4448 } 4449 4450 4451 // Handle the contended cases. 4452 // 4453 // Each contended field should not intersect the cache line with another contended field. 4454 // In the absence of alignment information, we end up with pessimistically separating 4455 // the fields with full-width padding. 4456 // 4457 // Additionally, this should not break alignment for the fields, so we round the alignment up 4458 // for each field. 4459 if (nonstatic_contended_count > 0) { 4460 4461 // if there is at least one contended field, we need to have pre-padding for them 4462 next_nonstatic_padded_offset += ContendedPaddingWidth; 4463 4464 // collect all contended groups 4465 ResourceBitMap bm(cp->size()); 4466 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4467 // skip already laid out fields 4468 if (fs.is_offset_set()) continue; 4469 4470 if (fs.is_contended()) { 4471 bm.set_bit(fs.contended_group()); 4472 } 4473 } 4474 4475 int current_group = -1; 4476 while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) { 4477 4478 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4479 4480 // skip already laid out fields 4481 if (fs.is_offset_set()) continue; 4482 4483 // skip non-contended fields and fields from different group 4484 if (!fs.is_contended() || (fs.contended_group() != current_group)) continue; 4485 4486 // handle statics below 4487 if (fs.access_flags().is_static()) continue; 4488 4489 int real_offset = 0; 4490 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 4491 4492 switch (atype) { 4493 case NONSTATIC_BYTE: 4494 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, 1); 4495 real_offset = next_nonstatic_padded_offset; 4496 next_nonstatic_padded_offset += 1; 4497 break; 4498 4499 case NONSTATIC_SHORT: 4500 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort); 4501 real_offset = next_nonstatic_padded_offset; 4502 next_nonstatic_padded_offset += BytesPerShort; 4503 break; 4504 4505 case NONSTATIC_WORD: 4506 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt); 4507 real_offset = next_nonstatic_padded_offset; 4508 next_nonstatic_padded_offset += BytesPerInt; 4509 break; 4510 4511 case NONSTATIC_DOUBLE: 4512 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong); 4513 real_offset = next_nonstatic_padded_offset; 4514 next_nonstatic_padded_offset += BytesPerLong; 4515 break; 4516 4517 // Value types in static fields are handled with oops 4518 case NONSTATIC_FLATTENABLE: 4519 throwValueTypeLimitation(THREAD_AND_LOCATION, 4520 "@Contended annotation not supported for value types yet", fs.name(), fs.signature()); 4521 return; 4522 4523 case NONSTATIC_OOP: 4524 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize); 4525 real_offset = next_nonstatic_padded_offset; 4526 next_nonstatic_padded_offset += heapOopSize; 4527 nonstatic_oop_maps->add(real_offset, 1); 4528 break; 4529 4530 default: 4531 ShouldNotReachHere(); 4532 } 4533 4534 if (fs.contended_group() == 0) { 4535 // Contended group defines the equivalence class over the fields: 4536 // the fields within the same contended group are not inter-padded. 4537 // The only exception is default group, which does not incur the 4538 // equivalence, and so requires intra-padding. 4539 next_nonstatic_padded_offset += ContendedPaddingWidth; 4540 } 4541 4542 fs.set_offset(real_offset); 4543 } // for 4544 4545 // Start laying out the next group. 4546 // Note that this will effectively pad the last group in the back; 4547 // this is expected to alleviate memory contention effects for 4548 // subclass fields and/or adjacent object. 4549 // If this was the default group, the padding is already in place. 4550 if (current_group != 0) { 4551 next_nonstatic_padded_offset += ContendedPaddingWidth; 4552 } 4553 } 4554 4555 // handle static fields 4556 } 4557 4558 // Entire class is contended, pad in the back. 4559 // This helps to alleviate memory contention effects for subclass fields 4560 // and/or adjacent object. 4561 if (is_contended_class) { 4562 assert(!is_value_type(), "@Contended not supported for value types yet"); 4563 next_nonstatic_padded_offset += ContendedPaddingWidth; 4564 } 4565 4566 int notaligned_nonstatic_fields_end; 4567 if (nonstatic_value_type_count != 0) { 4568 notaligned_nonstatic_fields_end = next_nonstatic_valuetype_offset; 4569 } else { 4570 notaligned_nonstatic_fields_end = next_nonstatic_padded_offset; 4571 } 4572 4573 int nonstatic_field_sz_align = heapOopSize; 4574 if (is_value_type()) { 4575 if ((notaligned_nonstatic_fields_end - nonstatic_fields_start) > heapOopSize) { 4576 nonstatic_field_sz_align = BytesPerLong; // value copy of fields only uses jlong copy 4577 } 4578 } 4579 int nonstatic_fields_end = align_up(notaligned_nonstatic_fields_end, nonstatic_field_sz_align); 4580 int instance_end = align_up(notaligned_nonstatic_fields_end, wordSize); 4581 int static_fields_end = align_up(next_static_byte_offset, wordSize); 4582 4583 int static_field_size = (static_fields_end - 4584 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 4585 nonstatic_field_size = nonstatic_field_size + 4586 (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize; 4587 4588 int instance_size = align_object_size(instance_end / wordSize); 4589 4590 assert(instance_size == align_object_size(align_up( 4591 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize) 4592 + initial_value_type_padding, wordSize) / wordSize), "consistent layout helper value"); 4593 4594 4595 // Invariant: nonstatic_field end/start should only change if there are 4596 // nonstatic fields in the class, or if the class is contended. We compare 4597 // against the non-aligned value, so that end alignment will not fail the 4598 // assert without actually having the fields. 4599 assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) || 4600 is_contended_class || 4601 (nonstatic_fields_count > 0), "double-check nonstatic start/end"); 4602 4603 // Number of non-static oop map blocks allocated at end of klass. 4604 nonstatic_oop_maps->compact(THREAD); 4605 4606 #ifndef PRODUCT 4607 if ((PrintFieldLayout && !is_value_type()) || 4608 (PrintValueLayout && (is_value_type() || has_nonstatic_value_fields))) { 4609 print_field_layout(_class_name, 4610 _fields, 4611 cp, 4612 instance_size, 4613 nonstatic_fields_start, 4614 nonstatic_fields_end, 4615 static_fields_end); 4616 nonstatic_oop_maps->print_on(tty); 4617 tty->print("\n"); 4618 } 4619 4620 #endif 4621 // Pass back information needed for InstanceKlass creation 4622 info->oop_map_blocks = nonstatic_oop_maps; 4623 info->instance_size = instance_size; 4624 info->static_field_size = static_field_size; 4625 info->nonstatic_field_size = nonstatic_field_size; 4626 info->has_nonstatic_fields = has_nonstatic_fields; 4627 } 4628 4629 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik, TRAPS) { 4630 assert(ik != NULL, "invariant"); 4631 4632 const Klass* const super = ik->super(); 4633 4634 // Check if this klass has an empty finalize method (i.e. one with return bytecode only), 4635 // in which case we don't have to register objects as finalizable 4636 if (!_has_empty_finalizer) { 4637 if (_has_finalizer || 4638 (super != NULL && super->has_finalizer())) { 4639 ik->set_has_finalizer(); 4640 } 4641 } 4642 4643 #ifdef ASSERT 4644 bool f = false; 4645 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(), 4646 vmSymbols::void_method_signature()); 4647 if (m != NULL && !m->is_empty_method()) { 4648 f = true; 4649 } 4650 4651 // Spec doesn't prevent agent from redefinition of empty finalizer. 4652 // Despite the fact that it's generally bad idea and redefined finalizer 4653 // will not work as expected we shouldn't abort vm in this case 4654 if (!ik->has_redefined_this_or_super()) { 4655 assert(ik->has_finalizer() == f, "inconsistent has_finalizer"); 4656 } 4657 #endif 4658 4659 // Check if this klass supports the java.lang.Cloneable interface 4660 if (SystemDictionary::Cloneable_klass_loaded()) { 4661 if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) { 4662 if (ik->is_value()) { 4663 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support Cloneable"); 4664 return; 4665 } 4666 ik->set_is_cloneable(); 4667 } 4668 } 4669 4670 // Check if this klass has a vanilla default constructor 4671 if (super == NULL) { 4672 // java.lang.Object has empty default constructor 4673 ik->set_has_vanilla_constructor(); 4674 } else { 4675 if (super->has_vanilla_constructor() && 4676 _has_vanilla_constructor) { 4677 ik->set_has_vanilla_constructor(); 4678 } 4679 #ifdef ASSERT 4680 bool v = false; 4681 if (super->has_vanilla_constructor()) { 4682 const Method* const constructor = 4683 ik->find_method(vmSymbols::object_initializer_name(), 4684 vmSymbols::void_method_signature()); 4685 if (constructor != NULL && constructor->is_vanilla_constructor()) { 4686 v = true; 4687 } 4688 } 4689 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor"); 4690 #endif 4691 } 4692 4693 // If it cannot be fast-path allocated, set a bit in the layout helper. 4694 // See documentation of InstanceKlass::can_be_fastpath_allocated(). 4695 assert(ik->size_helper() > 0, "layout_helper is initialized"); 4696 if ((!RegisterFinalizersAtInit && ik->has_finalizer()) 4697 || ik->is_abstract() || ik->is_interface() 4698 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL) 4699 || ik->size_helper() >= FastAllocateSizeLimit) { 4700 // Forbid fast-path allocation. 4701 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true); 4702 ik->set_layout_helper(lh); 4703 } 4704 } 4705 4706 bool ClassFileParser::supports_value_types() const { 4707 // Value types are only supported by class file version 55 and later 4708 return _major_version >= JAVA_11_VERSION; 4709 } 4710 4711 // utility methods for appending an array with check for duplicates 4712 4713 static void append_interfaces(GrowableArray<InstanceKlass*>* result, 4714 const Array<InstanceKlass*>* const ifs) { 4715 // iterate over new interfaces 4716 for (int i = 0; i < ifs->length(); i++) { 4717 InstanceKlass* const e = ifs->at(i); 4718 assert(e->is_klass() && e->is_interface(), "just checking"); 4719 // add new interface 4720 result->append_if_missing(e); 4721 } 4722 } 4723 4724 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super, 4725 Array<InstanceKlass*>* local_ifs, 4726 ClassLoaderData* loader_data, 4727 TRAPS) { 4728 assert(local_ifs != NULL, "invariant"); 4729 assert(loader_data != NULL, "invariant"); 4730 4731 // Compute maximum size for transitive interfaces 4732 int max_transitive_size = 0; 4733 int super_size = 0; 4734 // Add superclass transitive interfaces size 4735 if (super != NULL) { 4736 super_size = super->transitive_interfaces()->length(); 4737 max_transitive_size += super_size; 4738 } 4739 // Add local interfaces' super interfaces 4740 const int local_size = local_ifs->length(); 4741 for (int i = 0; i < local_size; i++) { 4742 InstanceKlass* const l = local_ifs->at(i); 4743 max_transitive_size += l->transitive_interfaces()->length(); 4744 } 4745 // Finally add local interfaces 4746 max_transitive_size += local_size; 4747 // Construct array 4748 if (max_transitive_size == 0) { 4749 // no interfaces, use canonicalized array 4750 return Universe::the_empty_instance_klass_array(); 4751 } else if (max_transitive_size == super_size) { 4752 // no new local interfaces added, share superklass' transitive interface array 4753 return super->transitive_interfaces(); 4754 } else if (max_transitive_size == local_size) { 4755 // only local interfaces added, share local interface array 4756 return local_ifs; 4757 } else { 4758 ResourceMark rm; 4759 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size); 4760 4761 // Copy down from superclass 4762 if (super != NULL) { 4763 append_interfaces(result, super->transitive_interfaces()); 4764 } 4765 4766 // Copy down from local interfaces' superinterfaces 4767 for (int i = 0; i < local_size; i++) { 4768 InstanceKlass* const l = local_ifs->at(i); 4769 append_interfaces(result, l->transitive_interfaces()); 4770 } 4771 // Finally add local interfaces 4772 append_interfaces(result, local_ifs); 4773 4774 // length will be less than the max_transitive_size if duplicates were removed 4775 const int length = result->length(); 4776 assert(length <= max_transitive_size, "just checking"); 4777 Array<InstanceKlass*>* const new_result = 4778 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL); 4779 for (int i = 0; i < length; i++) { 4780 InstanceKlass* const e = result->at(i); 4781 assert(e != NULL, "just checking"); 4782 new_result->at_put(i, e); 4783 } 4784 return new_result; 4785 } 4786 } 4787 4788 static void check_super_class_access(const InstanceKlass* this_klass, TRAPS) { 4789 assert(this_klass != NULL, "invariant"); 4790 const Klass* const super = this_klass->super(); 4791 if (super != NULL) { 4792 4793 // If the loader is not the boot loader then throw an exception if its 4794 // superclass is in package jdk.internal.reflect and its loader is not a 4795 // special reflection class loader 4796 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) { 4797 assert(super->is_instance_klass(), "super is not instance klass"); 4798 PackageEntry* super_package = super->package(); 4799 if (super_package != NULL && 4800 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 && 4801 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) { 4802 ResourceMark rm(THREAD); 4803 Exceptions::fthrow( 4804 THREAD_AND_LOCATION, 4805 vmSymbols::java_lang_IllegalAccessError(), 4806 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s", 4807 this_klass->external_name(), 4808 this_klass->class_loader_data()->loader_name_and_id(), 4809 super->external_name()); 4810 return; 4811 } 4812 } 4813 4814 Reflection::VerifyClassAccessResults vca_result = 4815 Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false); 4816 if (vca_result != Reflection::ACCESS_OK) { 4817 ResourceMark rm(THREAD); 4818 char* msg = Reflection::verify_class_access_msg(this_klass, 4819 InstanceKlass::cast(super), 4820 vca_result); 4821 if (msg == NULL) { 4822 bool same_module = (this_klass->module() == super->module()); 4823 Exceptions::fthrow( 4824 THREAD_AND_LOCATION, 4825 vmSymbols::java_lang_IllegalAccessError(), 4826 "class %s cannot access its %ssuperclass %s (%s%s%s)", 4827 this_klass->external_name(), 4828 super->is_abstract() ? "abstract " : "", 4829 super->external_name(), 4830 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(), 4831 (same_module) ? "" : "; ", 4832 (same_module) ? "" : super->class_in_module_of_loader()); 4833 } else { 4834 // Add additional message content. 4835 Exceptions::fthrow( 4836 THREAD_AND_LOCATION, 4837 vmSymbols::java_lang_IllegalAccessError(), 4838 "superclass access check failed: %s", 4839 msg); 4840 } 4841 } 4842 } 4843 } 4844 4845 4846 static void check_super_interface_access(const InstanceKlass* this_klass, TRAPS) { 4847 assert(this_klass != NULL, "invariant"); 4848 const Array<InstanceKlass*>* const local_interfaces = this_klass->local_interfaces(); 4849 const int lng = local_interfaces->length(); 4850 for (int i = lng - 1; i >= 0; i--) { 4851 InstanceKlass* const k = local_interfaces->at(i); 4852 assert (k != NULL && k->is_interface(), "invalid interface"); 4853 Reflection::VerifyClassAccessResults vca_result = 4854 Reflection::verify_class_access(this_klass, k, false); 4855 if (vca_result != Reflection::ACCESS_OK) { 4856 ResourceMark rm(THREAD); 4857 char* msg = Reflection::verify_class_access_msg(this_klass, 4858 k, 4859 vca_result); 4860 if (msg == NULL) { 4861 bool same_module = (this_klass->module() == k->module()); 4862 Exceptions::fthrow( 4863 THREAD_AND_LOCATION, 4864 vmSymbols::java_lang_IllegalAccessError(), 4865 "class %s cannot access its superinterface %s (%s%s%s)", 4866 this_klass->external_name(), 4867 k->external_name(), 4868 (same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(), 4869 (same_module) ? "" : "; ", 4870 (same_module) ? "" : k->class_in_module_of_loader()); 4871 } else { 4872 // Add additional message content. 4873 Exceptions::fthrow( 4874 THREAD_AND_LOCATION, 4875 vmSymbols::java_lang_IllegalAccessError(), 4876 "superinterface check failed: %s", 4877 msg); 4878 } 4879 } 4880 } 4881 } 4882 4883 4884 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) { 4885 assert(this_klass != NULL, "invariant"); 4886 const Array<Method*>* const methods = this_klass->methods(); 4887 const int num_methods = methods->length(); 4888 4889 // go thru each method and check if it overrides a final method 4890 for (int index = 0; index < num_methods; index++) { 4891 const Method* const m = methods->at(index); 4892 4893 // skip private, static, and <init> methods 4894 if ((!m->is_private() && !m->is_static()) && 4895 (m->name() != vmSymbols::object_initializer_name())) { 4896 4897 const Symbol* const name = m->name(); 4898 const Symbol* const signature = m->signature(); 4899 const Klass* k = this_klass->super(); 4900 const Method* super_m = NULL; 4901 while (k != NULL) { 4902 // skip supers that don't have final methods. 4903 if (k->has_final_method()) { 4904 // lookup a matching method in the super class hierarchy 4905 super_m = InstanceKlass::cast(k)->lookup_method(name, signature); 4906 if (super_m == NULL) { 4907 break; // didn't find any match; get out 4908 } 4909 4910 if (super_m->is_final() && !super_m->is_static() && 4911 !super_m->access_flags().is_private()) { 4912 // matching method in super is final, and not static or private 4913 bool can_access = Reflection::verify_member_access(this_klass, 4914 super_m->method_holder(), 4915 super_m->method_holder(), 4916 super_m->access_flags(), 4917 false, false, CHECK); 4918 if (can_access) { 4919 // this class can access super final method and therefore override 4920 ResourceMark rm(THREAD); 4921 Exceptions::fthrow(THREAD_AND_LOCATION, 4922 vmSymbols::java_lang_VerifyError(), 4923 "class %s overrides final method %s.%s%s", 4924 this_klass->external_name(), 4925 super_m->method_holder()->external_name(), 4926 name->as_C_string(), 4927 signature->as_C_string() 4928 ); 4929 return; 4930 } 4931 } 4932 4933 // continue to look from super_m's holder's super. 4934 k = super_m->method_holder()->super(); 4935 continue; 4936 } 4937 4938 k = k->super(); 4939 } 4940 } 4941 } 4942 } 4943 4944 4945 // assumes that this_klass is an interface 4946 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) { 4947 assert(this_klass != NULL, "invariant"); 4948 assert(this_klass->is_interface(), "not an interface"); 4949 const Array<Method*>* methods = this_klass->methods(); 4950 const int num_methods = methods->length(); 4951 4952 for (int index = 0; index < num_methods; index++) { 4953 const Method* const m = methods->at(index); 4954 // if m is static and not the init method, throw a verify error 4955 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) { 4956 ResourceMark rm(THREAD); 4957 Exceptions::fthrow( 4958 THREAD_AND_LOCATION, 4959 vmSymbols::java_lang_VerifyError(), 4960 "Illegal static method %s in interface %s", 4961 m->name()->as_C_string(), 4962 this_klass->external_name() 4963 ); 4964 return; 4965 } 4966 } 4967 } 4968 4969 // utility methods for format checking 4970 4971 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const { 4972 const bool is_module = (flags & JVM_ACC_MODULE) != 0; 4973 const bool is_value_type = (flags & JVM_ACC_VALUE) != 0; 4974 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set"); 4975 assert(supports_value_types() || !is_value_type, "JVM_ACC_VALUE should not be set"); 4976 if (is_module) { 4977 ResourceMark rm(THREAD); 4978 Exceptions::fthrow( 4979 THREAD_AND_LOCATION, 4980 vmSymbols::java_lang_NoClassDefFoundError(), 4981 "%s is not a class because access_flag ACC_MODULE is set", 4982 _class_name->as_C_string()); 4983 return; 4984 } 4985 4986 if (is_value_type && !EnableValhalla) { 4987 ResourceMark rm(THREAD); 4988 Exceptions::fthrow( 4989 THREAD_AND_LOCATION, 4990 vmSymbols::java_lang_ClassFormatError(), 4991 "Class modifier ACC_VALUE in class %s requires option -XX:+EnableValhalla", 4992 _class_name->as_C_string() 4993 ); 4994 } 4995 4996 if (!_need_verify) { return; } 4997 4998 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0; 4999 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 5000 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 5001 const bool is_super = (flags & JVM_ACC_SUPER) != 0; 5002 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 5003 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0; 5004 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 5005 5006 if ((is_abstract && is_final) || 5007 (is_interface && !is_abstract) || 5008 (is_interface && major_gte_15 && (is_super || is_enum)) || 5009 (!is_interface && major_gte_15 && is_annotation) || 5010 (is_value_type && (is_interface || is_abstract || is_enum || !is_final))) { 5011 ResourceMark rm(THREAD); 5012 Exceptions::fthrow( 5013 THREAD_AND_LOCATION, 5014 vmSymbols::java_lang_ClassFormatError(), 5015 "Illegal class modifiers in class %s: 0x%X", 5016 _class_name->as_C_string(), flags 5017 ); 5018 return; 5019 } 5020 } 5021 5022 static bool has_illegal_visibility(jint flags) { 5023 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 5024 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 5025 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 5026 5027 return ((is_public && is_protected) || 5028 (is_public && is_private) || 5029 (is_protected && is_private)); 5030 } 5031 5032 // A legal major_version.minor_version must be one of the following: 5033 // 5034 // Major_version = 45, any minor_version. 5035 // Major_version >= 46 and major_version <= current_major_version and minor_version = 0. 5036 // Major_version = current_major_version and minor_version = 65535 and --enable-preview is present. 5037 // 5038 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){ 5039 const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION; 5040 if (major != JAVA_MIN_SUPPORTED_VERSION) { // All 45.* are ok including 45.65535 5041 if (minor == JAVA_PREVIEW_MINOR_VERSION) { 5042 if (major != max_version) { 5043 ResourceMark rm(THREAD); 5044 Exceptions::fthrow( 5045 THREAD_AND_LOCATION, 5046 vmSymbols::java_lang_UnsupportedClassVersionError(), 5047 "%s (class file version %u.%u) was compiled with preview features that are unsupported. " 5048 "This version of the Java Runtime only recognizes preview features for class file version %u.%u", 5049 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION); 5050 return; 5051 } 5052 5053 if (!Arguments::enable_preview()) { 5054 ResourceMark rm(THREAD); 5055 Exceptions::fthrow( 5056 THREAD_AND_LOCATION, 5057 vmSymbols::java_lang_UnsupportedClassVersionError(), 5058 "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'", 5059 class_name->as_C_string(), major, minor); 5060 return; 5061 } 5062 5063 } else { // minor != JAVA_PREVIEW_MINOR_VERSION 5064 if (major > max_version) { 5065 ResourceMark rm(THREAD); 5066 Exceptions::fthrow( 5067 THREAD_AND_LOCATION, 5068 vmSymbols::java_lang_UnsupportedClassVersionError(), 5069 "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), " 5070 "this version of the Java Runtime only recognizes class file versions up to %u.0", 5071 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION); 5072 } else if (major < JAVA_MIN_SUPPORTED_VERSION) { 5073 ResourceMark rm(THREAD); 5074 Exceptions::fthrow( 5075 THREAD_AND_LOCATION, 5076 vmSymbols::java_lang_UnsupportedClassVersionError(), 5077 "%s (class file version %u.%u) was compiled with an invalid major version", 5078 class_name->as_C_string(), major, minor); 5079 } else if (minor != 0) { 5080 ResourceMark rm(THREAD); 5081 Exceptions::fthrow( 5082 THREAD_AND_LOCATION, 5083 vmSymbols::java_lang_UnsupportedClassVersionError(), 5084 "%s (class file version %u.%u) was compiled with an invalid non-zero minor version", 5085 class_name->as_C_string(), major, minor); 5086 } 5087 } 5088 } 5089 } 5090 5091 void ClassFileParser::verify_legal_field_modifiers(jint flags, 5092 bool is_interface, 5093 bool is_value_type, 5094 TRAPS) const { 5095 if (!_need_verify) { return; } 5096 5097 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 5098 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 5099 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 5100 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 5101 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 5102 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0; 5103 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0; 5104 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 5105 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 5106 5107 bool is_illegal = false; 5108 5109 if (is_interface) { 5110 if (!is_public || !is_static || !is_final || is_private || 5111 is_protected || is_volatile || is_transient || 5112 (major_gte_15 && is_enum)) { 5113 is_illegal = true; 5114 } 5115 } else { // not interface 5116 if (has_illegal_visibility(flags) || (is_final && is_volatile)) { 5117 is_illegal = true; 5118 } else { 5119 if (is_value_type && !is_static && !is_final) { 5120 is_illegal = true; 5121 } 5122 } 5123 } 5124 5125 if (is_illegal) { 5126 ResourceMark rm(THREAD); 5127 Exceptions::fthrow( 5128 THREAD_AND_LOCATION, 5129 vmSymbols::java_lang_ClassFormatError(), 5130 "Illegal field modifiers in class %s: 0x%X", 5131 _class_name->as_C_string(), flags); 5132 return; 5133 } 5134 } 5135 5136 void ClassFileParser::verify_legal_method_modifiers(jint flags, 5137 bool is_interface, 5138 bool is_value_type, 5139 const Symbol* name, 5140 TRAPS) const { 5141 if (!_need_verify) { return; } 5142 5143 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 5144 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 5145 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 5146 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 5147 const bool is_native = (flags & JVM_ACC_NATIVE) != 0; 5148 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 5149 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0; 5150 const bool is_strict = (flags & JVM_ACC_STRICT) != 0; 5151 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0; 5152 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 5153 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION; 5154 const bool major_gte_8 = _major_version >= JAVA_8_VERSION; 5155 const bool is_initializer = (name == vmSymbols::object_initializer_name()); 5156 5157 bool is_illegal = false; 5158 5159 if (is_interface) { 5160 if (major_gte_8) { 5161 // Class file version is JAVA_8_VERSION or later Methods of 5162 // interfaces may set any of the flags except ACC_PROTECTED, 5163 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must 5164 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. 5165 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */ 5166 (is_native || is_protected || is_final || is_synchronized) || 5167 // If a specific method of a class or interface has its 5168 // ACC_ABSTRACT flag set, it must not have any of its 5169 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, 5170 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to 5171 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as 5172 // those flags are illegal irrespective of ACC_ABSTRACT being set or not. 5173 (is_abstract && (is_private || is_static || is_strict))) { 5174 is_illegal = true; 5175 } 5176 } else if (major_gte_15) { 5177 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION) 5178 if (!is_public || is_private || is_protected || is_static || is_final || 5179 is_synchronized || is_native || !is_abstract || is_strict) { 5180 is_illegal = true; 5181 } 5182 } else { 5183 // Class file version is pre-JAVA_1_5_VERSION 5184 if (!is_public || is_static || is_final || is_native || !is_abstract) { 5185 is_illegal = true; 5186 } 5187 } 5188 } else { // not interface 5189 if (has_illegal_visibility(flags)) { 5190 is_illegal = true; 5191 } else { 5192 if (is_initializer) { 5193 if (is_static || is_final || is_synchronized || is_native || 5194 is_abstract || (major_gte_15 && is_bridge)) { 5195 is_illegal = true; 5196 } 5197 } else { // not initializer 5198 if (is_value_type && is_synchronized && !is_static) { 5199 is_illegal = true; 5200 } else { 5201 if (is_abstract) { 5202 if ((is_final || is_native || is_private || is_static || 5203 (major_gte_15 && (is_synchronized || is_strict)))) { 5204 is_illegal = true; 5205 } 5206 } 5207 } 5208 } 5209 } 5210 } 5211 5212 if (is_illegal) { 5213 ResourceMark rm(THREAD); 5214 Exceptions::fthrow( 5215 THREAD_AND_LOCATION, 5216 vmSymbols::java_lang_ClassFormatError(), 5217 "Method %s in class %s has illegal modifiers: 0x%X", 5218 name->as_C_string(), _class_name->as_C_string(), flags); 5219 return; 5220 } 5221 } 5222 5223 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, 5224 int length, 5225 TRAPS) const { 5226 assert(_need_verify, "only called when _need_verify is true"); 5227 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) { 5228 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); 5229 } 5230 } 5231 5232 // Unqualified names may not contain the characters '.', ';', '[', or '/'. 5233 // In class names, '/' separates unqualified names. This is verified in this function also. 5234 // Method names also may not contain the characters '<' or '>', unless <init> 5235 // or <clinit>. Note that method names may not be <init> or <clinit> in this 5236 // method. Because these names have been checked as special cases before 5237 // calling this method in verify_legal_method_name. 5238 // 5239 // This method is also called from the modular system APIs in modules.cpp 5240 // to verify the validity of module and package names. 5241 bool ClassFileParser::verify_unqualified_name(const char* name, 5242 unsigned int length, 5243 int type) { 5244 for (const char* p = name; p != name + length; p++) { 5245 switch(*p) { 5246 case '.': 5247 case ';': 5248 case '[': 5249 // do not permit '.', ';', or '[' 5250 return false; 5251 case '/': 5252 // check for '//' or leading or trailing '/' which are not legal 5253 // unqualified name must not be empty 5254 if (type == ClassFileParser::LegalClass) { 5255 if (p == name || p+1 >= name+length || *(p+1) == '/') { 5256 return false; 5257 } 5258 } else { 5259 return false; // do not permit '/' unless it's class name 5260 } 5261 break; 5262 case '<': 5263 case '>': 5264 // do not permit '<' or '>' in method names 5265 if (type == ClassFileParser::LegalMethod) { 5266 return false; 5267 } 5268 } 5269 } 5270 return true; 5271 } 5272 5273 // Take pointer to a UTF8 byte string (not NUL-terminated). 5274 // Skip over the longest part of the string that could 5275 // be taken as a fieldname. Allow '/' if slash_ok is true. 5276 // Return a pointer to just past the fieldname. 5277 // Return NULL if no fieldname at all was found, or in the case of slash_ok 5278 // being true, we saw consecutive slashes (meaning we were looking for a 5279 // qualified path but found something that was badly-formed). 5280 static const char* skip_over_field_name(const char* const name, 5281 bool slash_ok, 5282 unsigned int length) { 5283 const char* p; 5284 jboolean last_is_slash = false; 5285 jboolean not_first_ch = false; 5286 5287 for (p = name; p != name + length; not_first_ch = true) { 5288 const char* old_p = p; 5289 jchar ch = *p; 5290 if (ch < 128) { 5291 p++; 5292 // quick check for ascii 5293 if ((ch >= 'a' && ch <= 'z') || 5294 (ch >= 'A' && ch <= 'Z') || 5295 (ch == '_' || ch == '$') || 5296 (not_first_ch && ch >= '0' && ch <= '9')) { 5297 last_is_slash = false; 5298 continue; 5299 } 5300 if (slash_ok && ch == '/') { 5301 if (last_is_slash) { 5302 return NULL; // Don't permit consecutive slashes 5303 } 5304 last_is_slash = true; 5305 continue; 5306 } 5307 } 5308 else { 5309 jint unicode_ch; 5310 char* tmp_p = UTF8::next_character(p, &unicode_ch); 5311 p = tmp_p; 5312 last_is_slash = false; 5313 // Check if ch is Java identifier start or is Java identifier part 5314 // 4672820: call java.lang.Character methods directly without generating separate tables. 5315 EXCEPTION_MARK; 5316 // return value 5317 JavaValue result(T_BOOLEAN); 5318 // Set up the arguments to isJavaIdentifierStart or isJavaIdentifierPart 5319 JavaCallArguments args; 5320 args.push_int(unicode_ch); 5321 5322 if (not_first_ch) { 5323 // public static boolean isJavaIdentifierPart(char ch); 5324 JavaCalls::call_static(&result, 5325 SystemDictionary::Character_klass(), 5326 vmSymbols::isJavaIdentifierPart_name(), 5327 vmSymbols::int_bool_signature(), 5328 &args, 5329 THREAD); 5330 } else { 5331 // public static boolean isJavaIdentifierStart(char ch); 5332 JavaCalls::call_static(&result, 5333 SystemDictionary::Character_klass(), 5334 vmSymbols::isJavaIdentifierStart_name(), 5335 vmSymbols::int_bool_signature(), 5336 &args, 5337 THREAD); 5338 } 5339 if (HAS_PENDING_EXCEPTION) { 5340 CLEAR_PENDING_EXCEPTION; 5341 return NULL; 5342 } 5343 if(result.get_jboolean()) { 5344 continue; 5345 } 5346 } 5347 return (not_first_ch) ? old_p : NULL; 5348 } 5349 return (not_first_ch) ? p : NULL; 5350 } 5351 5352 // Take pointer to a UTF8 byte string (not NUL-terminated). 5353 // Skip over the longest part of the string that could 5354 // be taken as a field signature. Allow "void" if void_ok. 5355 // Return a pointer to just past the signature. 5356 // Return NULL if no legal signature is found. 5357 const char* ClassFileParser::skip_over_field_signature(const char* signature, 5358 bool void_ok, 5359 unsigned int length, 5360 TRAPS) const { 5361 unsigned int array_dim = 0; 5362 while (length > 0) { 5363 switch (signature[0]) { 5364 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; } 5365 case JVM_SIGNATURE_BOOLEAN: 5366 case JVM_SIGNATURE_BYTE: 5367 case JVM_SIGNATURE_CHAR: 5368 case JVM_SIGNATURE_SHORT: 5369 case JVM_SIGNATURE_INT: 5370 case JVM_SIGNATURE_FLOAT: 5371 case JVM_SIGNATURE_LONG: 5372 case JVM_SIGNATURE_DOUBLE: 5373 return signature + 1; 5374 case JVM_SIGNATURE_VALUETYPE: 5375 // Can't enable this check until JDK upgrades the bytecode generators 5376 // if (_major_version < CONSTANT_CLASS_DESCRIPTORS ) { 5377 // classfile_parse_error("Class name contains illegal Q-signature " 5378 // "in descriptor in class file %s", 5379 // CHECK_0); 5380 // } 5381 // fall through 5382 case JVM_SIGNATURE_CLASS: 5383 { 5384 if (_major_version < JAVA_1_5_VERSION) { 5385 // Skip over the class name if one is there 5386 const char* const p = skip_over_field_name(signature + 1, true, --length); 5387 5388 // The next character better be a semicolon 5389 if (p && (p - signature) > 1 && p[0] == ';') { 5390 return p + 1; 5391 } 5392 } 5393 else { 5394 // Skip leading 'L' or 'Q' and ignore first appearance of ';' 5395 signature++; 5396 const char* c = (const char*) memchr(signature, ';', length - 1); 5397 // Format check signature 5398 if (c != NULL) { 5399 int newlen = c - (char*) signature; 5400 bool legal = verify_unqualified_name(signature, newlen, LegalClass); 5401 if (!legal) { 5402 classfile_parse_error("Class name contains illegal character " 5403 "in descriptor in class file %s", 5404 CHECK_0); 5405 return NULL; 5406 } 5407 return signature + newlen + 1; 5408 } 5409 } 5410 return NULL; 5411 } 5412 case JVM_SIGNATURE_ARRAY: 5413 array_dim++; 5414 if (array_dim > 255) { 5415 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions. 5416 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0); 5417 } 5418 // The rest of what's there better be a legal signature 5419 signature++; 5420 length--; 5421 void_ok = false; 5422 break; 5423 default: 5424 return NULL; 5425 } 5426 } 5427 return NULL; 5428 } 5429 5430 // Checks if name is a legal class name. 5431 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const { 5432 if (!_need_verify || _relax_verify) { return; } 5433 5434 assert(name->refcount() > 0, "symbol must be kept alive"); 5435 char* bytes = (char*)name->bytes(); 5436 unsigned int length = name->utf8_length(); 5437 bool legal = false; 5438 5439 if (length > 0) { 5440 const char* p; 5441 if (bytes[0] == JVM_SIGNATURE_ARRAY) { 5442 p = skip_over_field_signature(bytes, false, length, CHECK); 5443 legal = (p != NULL) && ((p - bytes) == (int)length); 5444 } else if (_major_version < JAVA_1_5_VERSION) { 5445 if (bytes[0] != '<') { 5446 p = skip_over_field_name(bytes, true, length); 5447 legal = (p != NULL) && ((p - bytes) == (int)length); 5448 } 5449 } else if (_major_version >= CONSTANT_CLASS_DESCRIPTORS && bytes[length - 1] == ';' ) { 5450 // Support for L...; and Q...; descriptors 5451 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass); 5452 } else { 5453 // 4900761: relax the constraints based on JSR202 spec 5454 // Class names may be drawn from the entire Unicode character set. 5455 // Identifiers between '/' must be unqualified names. 5456 // The utf8 string has been verified when parsing cpool entries. 5457 legal = verify_unqualified_name(bytes, length, LegalClass); 5458 } 5459 } 5460 if (!legal) { 5461 ResourceMark rm(THREAD); 5462 assert(_class_name != NULL, "invariant"); 5463 Exceptions::fthrow( 5464 THREAD_AND_LOCATION, 5465 vmSymbols::java_lang_ClassFormatError(), 5466 "Illegal class name \"%.*s\" in class file %s", length, bytes, 5467 _class_name->as_C_string() 5468 ); 5469 return; 5470 } 5471 } 5472 5473 // Checks if name is a legal field name. 5474 void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const { 5475 if (!_need_verify || _relax_verify) { return; } 5476 5477 char* bytes = (char*)name->bytes(); 5478 unsigned int length = name->utf8_length(); 5479 bool legal = false; 5480 5481 if (length > 0) { 5482 if (_major_version < JAVA_1_5_VERSION) { 5483 if (bytes[0] != '<') { 5484 const char* p = skip_over_field_name(bytes, false, length); 5485 legal = (p != NULL) && ((p - bytes) == (int)length); 5486 } 5487 } else { 5488 // 4881221: relax the constraints based on JSR202 spec 5489 legal = verify_unqualified_name(bytes, length, LegalField); 5490 } 5491 } 5492 5493 if (!legal) { 5494 ResourceMark rm(THREAD); 5495 assert(_class_name != NULL, "invariant"); 5496 Exceptions::fthrow( 5497 THREAD_AND_LOCATION, 5498 vmSymbols::java_lang_ClassFormatError(), 5499 "Illegal field name \"%.*s\" in class %s", length, bytes, 5500 _class_name->as_C_string() 5501 ); 5502 return; 5503 } 5504 } 5505 5506 // Checks if name is a legal method name. 5507 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const { 5508 if (!_need_verify || _relax_verify) { return; } 5509 5510 assert(name != NULL, "method name is null"); 5511 char* bytes = (char*)name->bytes(); 5512 unsigned int length = name->utf8_length(); 5513 bool legal = false; 5514 5515 if (length > 0) { 5516 if (bytes[0] == '<') { 5517 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) { 5518 legal = true; 5519 } 5520 } else if (_major_version < JAVA_1_5_VERSION) { 5521 const char* p; 5522 p = skip_over_field_name(bytes, false, length); 5523 legal = (p != NULL) && ((p - bytes) == (int)length); 5524 } else { 5525 // 4881221: relax the constraints based on JSR202 spec 5526 legal = verify_unqualified_name(bytes, length, LegalMethod); 5527 } 5528 } 5529 5530 if (!legal) { 5531 ResourceMark rm(THREAD); 5532 assert(_class_name != NULL, "invariant"); 5533 Exceptions::fthrow( 5534 THREAD_AND_LOCATION, 5535 vmSymbols::java_lang_ClassFormatError(), 5536 "Illegal method name \"%.*s\" in class %s", length, bytes, 5537 _class_name->as_C_string() 5538 ); 5539 return; 5540 } 5541 } 5542 5543 5544 // Checks if signature is a legal field signature. 5545 void ClassFileParser::verify_legal_field_signature(const Symbol* name, 5546 const Symbol* signature, 5547 TRAPS) const { 5548 if (!_need_verify) { return; } 5549 5550 const char* const bytes = (const char* const)signature->bytes(); 5551 const unsigned int length = signature->utf8_length(); 5552 const char* const p = skip_over_field_signature(bytes, false, length, CHECK); 5553 5554 if (p == NULL || (p - bytes) != (int)length) { 5555 throwIllegalSignature("Field", name, signature, CHECK); 5556 } 5557 } 5558 5559 // Checks if signature is a legal method signature. 5560 // Returns number of parameters 5561 int ClassFileParser::verify_legal_method_signature(const Symbol* name, 5562 const Symbol* signature, 5563 TRAPS) const { 5564 if (!_need_verify) { 5565 // make sure caller's args_size will be less than 0 even for non-static 5566 // method so it will be recomputed in compute_size_of_parameters(). 5567 return -2; 5568 } 5569 5570 // Class initializers cannot have args for class format version >= 51. 5571 if (name == vmSymbols::class_initializer_name() && 5572 signature != vmSymbols::void_method_signature() && 5573 _major_version >= JAVA_7_VERSION) { 5574 throwIllegalSignature("Method", name, signature, CHECK_0); 5575 return 0; 5576 } 5577 5578 unsigned int args_size = 0; 5579 const char* p = (const char*)signature->bytes(); 5580 unsigned int length = signature->utf8_length(); 5581 const char* nextp; 5582 5583 // The first character must be a '(' 5584 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) { 5585 length--; 5586 // Skip over legal field signatures 5587 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5588 while ((length > 0) && (nextp != NULL)) { 5589 args_size++; 5590 if (p[0] == 'J' || p[0] == 'D') { 5591 args_size++; 5592 } 5593 length -= nextp - p; 5594 p = nextp; 5595 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5596 } 5597 // The first non-signature thing better be a ')' 5598 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) { 5599 length--; 5600 if (name->utf8_length() > 0 && name->char_at(0) == '<') { 5601 // All internal methods must return void 5602 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) { 5603 return args_size; 5604 } 5605 } else { 5606 // Now we better just have a return value 5607 nextp = skip_over_field_signature(p, true, length, CHECK_0); 5608 if (nextp && ((int)length == (nextp - p))) { 5609 return args_size; 5610 } 5611 } 5612 } 5613 } 5614 // Report error 5615 throwIllegalSignature("Method", name, signature, CHECK_0); 5616 return 0; 5617 } 5618 5619 int ClassFileParser::static_field_size() const { 5620 assert(_field_info != NULL, "invariant"); 5621 return _field_info->static_field_size; 5622 } 5623 5624 int ClassFileParser::total_oop_map_count() const { 5625 assert(_field_info != NULL, "invariant"); 5626 return _field_info->oop_map_blocks->nonstatic_oop_map_count; 5627 } 5628 5629 jint ClassFileParser::layout_size() const { 5630 assert(_field_info != NULL, "invariant"); 5631 return _field_info->instance_size; 5632 } 5633 5634 static void check_methods_for_intrinsics(const InstanceKlass* ik, 5635 const Array<Method*>* methods) { 5636 assert(ik != NULL, "invariant"); 5637 assert(methods != NULL, "invariant"); 5638 5639 // Set up Method*::intrinsic_id as soon as we know the names of methods. 5640 // (We used to do this lazily, but now we query it in Rewriter, 5641 // which is eagerly done for every method, so we might as well do it now, 5642 // when everything is fresh in memory.) 5643 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik); 5644 5645 if (klass_id != vmSymbols::NO_SID) { 5646 for (int j = 0; j < methods->length(); ++j) { 5647 Method* method = methods->at(j); 5648 method->init_intrinsic_id(); 5649 5650 if (CheckIntrinsics) { 5651 // Check if an intrinsic is defined for method 'method', 5652 // but the method is not annotated with @HotSpotIntrinsicCandidate. 5653 if (method->intrinsic_id() != vmIntrinsics::_none && 5654 !method->intrinsic_candidate()) { 5655 tty->print("Compiler intrinsic is defined for method [%s], " 5656 "but the method is not annotated with @HotSpotIntrinsicCandidate.%s", 5657 method->name_and_sig_as_C_string(), 5658 NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.") 5659 ); 5660 tty->cr(); 5661 DEBUG_ONLY(vm_exit(1)); 5662 } 5663 // Check is the method 'method' is annotated with @HotSpotIntrinsicCandidate, 5664 // but there is no intrinsic available for it. 5665 if (method->intrinsic_candidate() && 5666 method->intrinsic_id() == vmIntrinsics::_none) { 5667 tty->print("Method [%s] is annotated with @HotSpotIntrinsicCandidate, " 5668 "but no compiler intrinsic is defined for the method.%s", 5669 method->name_and_sig_as_C_string(), 5670 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5671 ); 5672 tty->cr(); 5673 DEBUG_ONLY(vm_exit(1)); 5674 } 5675 } 5676 } // end for 5677 5678 #ifdef ASSERT 5679 if (CheckIntrinsics) { 5680 // Check for orphan methods in the current class. A method m 5681 // of a class C is orphan if an intrinsic is defined for method m, 5682 // but class C does not declare m. 5683 // The check is potentially expensive, therefore it is available 5684 // only in debug builds. 5685 5686 for (int id = vmIntrinsics::FIRST_ID; id < (int)vmIntrinsics::ID_LIMIT; ++id) { 5687 if (vmIntrinsics::_compiledLambdaForm == id) { 5688 // The _compiledLamdbdaForm intrinsic is a special marker for bytecode 5689 // generated for the JVM from a LambdaForm and therefore no method 5690 // is defined for it. 5691 continue; 5692 } 5693 5694 if (vmIntrinsics::class_for(vmIntrinsics::ID_from(id)) == klass_id) { 5695 // Check if the current class contains a method with the same 5696 // name, flags, signature. 5697 bool match = false; 5698 for (int j = 0; j < methods->length(); ++j) { 5699 const Method* method = methods->at(j); 5700 if (method->intrinsic_id() == id) { 5701 match = true; 5702 break; 5703 } 5704 } 5705 5706 if (!match) { 5707 char buf[1000]; 5708 tty->print("Compiler intrinsic is defined for method [%s], " 5709 "but the method is not available in class [%s].%s", 5710 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id), 5711 buf, sizeof(buf)), 5712 ik->name()->as_C_string(), 5713 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5714 ); 5715 tty->cr(); 5716 DEBUG_ONLY(vm_exit(1)); 5717 } 5718 } 5719 } // end for 5720 } // CheckIntrinsics 5721 #endif // ASSERT 5722 } 5723 } 5724 5725 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) { 5726 if (_klass != NULL) { 5727 return _klass; 5728 } 5729 5730 InstanceKlass* const ik = 5731 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL); 5732 5733 fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL); 5734 5735 assert(_klass == ik, "invariant"); 5736 5737 5738 if (ik->should_store_fingerprint()) { 5739 ik->store_fingerprint(_stream->compute_fingerprint()); 5740 } 5741 5742 ik->set_has_passed_fingerprint_check(false); 5743 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) { 5744 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik); 5745 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint(); 5746 if (aot_fp != 0 && aot_fp == fp) { 5747 // This class matches with a class saved in an AOT library 5748 ik->set_has_passed_fingerprint_check(true); 5749 } else { 5750 ResourceMark rm; 5751 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, 5752 ik->external_name(), aot_fp, _stream->compute_fingerprint()); 5753 } 5754 } 5755 5756 if (ik->is_value()) { 5757 ValueKlass* vk = ValueKlass::cast(ik); 5758 oop val = ik->allocate_instance(CHECK_NULL); 5759 vk->set_default_value(val); 5760 } 5761 5762 return ik; 5763 } 5764 5765 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) { 5766 assert(ik != NULL, "invariant"); 5767 5768 // Set name and CLD before adding to CLD 5769 ik->set_class_loader_data(_loader_data); 5770 ik->set_name(_class_name); 5771 5772 // Add all classes to our internal class loader list here, 5773 // including classes in the bootstrap (NULL) class loader. 5774 const bool publicize = !is_internal(); 5775 5776 _loader_data->add_class(ik, publicize); 5777 5778 set_klass_to_deallocate(ik); 5779 5780 assert(_field_info != NULL, "invariant"); 5781 assert(ik->static_field_size() == _field_info->static_field_size, "sanity"); 5782 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->nonstatic_oop_map_count, 5783 "sanity"); 5784 5785 assert(ik->is_instance_klass(), "sanity"); 5786 assert(ik->size_helper() == _field_info->instance_size, "sanity"); 5787 5788 // Fill in information already parsed 5789 ik->set_should_verify_class(_need_verify); 5790 5791 // Not yet: supers are done below to support the new subtype-checking fields 5792 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size); 5793 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields); 5794 assert(_fac != NULL, "invariant"); 5795 ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_FLATTENABLE]); 5796 5797 // this transfers ownership of a lot of arrays from 5798 // the parser onto the InstanceKlass* 5799 apply_parsed_class_metadata(ik, _java_fields_count, CHECK); 5800 5801 // note that is not safe to use the fields in the parser from this point on 5802 assert(NULL == _cp, "invariant"); 5803 assert(NULL == _fields, "invariant"); 5804 assert(NULL == _methods, "invariant"); 5805 assert(NULL == _inner_classes, "invariant"); 5806 assert(NULL == _nest_members, "invariant"); 5807 assert(NULL == _local_interfaces, "invariant"); 5808 assert(NULL == _combined_annotations, "invariant"); 5809 5810 if (_has_final_method) { 5811 ik->set_has_final_method(); 5812 } 5813 5814 ik->copy_method_ordering(_method_ordering, CHECK); 5815 // The InstanceKlass::_methods_jmethod_ids cache 5816 // is managed on the assumption that the initial cache 5817 // size is equal to the number of methods in the class. If 5818 // that changes, then InstanceKlass::idnum_can_increment() 5819 // has to be changed accordingly. 5820 ik->set_initial_method_idnum(ik->methods()->length()); 5821 5822 ik->set_this_class_index(_this_class_index); 5823 5824 if (is_unsafe_anonymous()) { 5825 // _this_class_index is a CONSTANT_Class entry that refers to this 5826 // anonymous class itself. If this class needs to refer to its own methods or 5827 // fields, it would use a CONSTANT_MethodRef, etc, which would reference 5828 // _this_class_index. However, because this class is anonymous (it's 5829 // not stored in SystemDictionary), _this_class_index cannot be resolved 5830 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup. 5831 // Therefore, we must eagerly resolve _this_class_index now. 5832 ik->constants()->klass_at_put(_this_class_index, ik); 5833 } 5834 5835 ik->set_minor_version(_minor_version); 5836 ik->set_major_version(_major_version); 5837 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods); 5838 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods); 5839 5840 if (_unsafe_anonymous_host != NULL) { 5841 assert (ik->is_unsafe_anonymous(), "should be the same"); 5842 ik->set_unsafe_anonymous_host(_unsafe_anonymous_host); 5843 } 5844 5845 // Set PackageEntry for this_klass 5846 oop cl = ik->class_loader(); 5847 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl)); 5848 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh()); 5849 ik->set_package(cld, CHECK); 5850 5851 const Array<Method*>* const methods = ik->methods(); 5852 assert(methods != NULL, "invariant"); 5853 const int methods_len = methods->length(); 5854 5855 check_methods_for_intrinsics(ik, methods); 5856 5857 // Fill in field values obtained by parse_classfile_attributes 5858 if (_parsed_annotations->has_any_annotations()) { 5859 _parsed_annotations->apply_to(ik); 5860 } 5861 5862 apply_parsed_class_attributes(ik); 5863 5864 // Miranda methods 5865 if ((_num_miranda_methods > 0) || 5866 // if this class introduced new miranda methods or 5867 (_super_klass != NULL && _super_klass->has_miranda_methods()) 5868 // super class exists and this class inherited miranda methods 5869 ) { 5870 ik->set_has_miranda_methods(); // then set a flag 5871 } 5872 5873 // Fill in information needed to compute superclasses. 5874 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK); 5875 ik->set_transitive_interfaces(_transitive_interfaces); 5876 _transitive_interfaces = NULL; 5877 5878 // Initialize itable offset tables 5879 klassItable::setup_itable_offset_table(ik); 5880 5881 // Compute transitive closure of interfaces this class implements 5882 // Do final class setup 5883 OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks; 5884 if (oop_map_blocks->nonstatic_oop_map_count > 0) { 5885 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps()); 5886 } 5887 5888 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper 5889 set_precomputed_flags(ik, CHECK); 5890 5891 // check if this class can access its super class 5892 check_super_class_access(ik, CHECK); 5893 5894 // check if this class can access its superinterfaces 5895 check_super_interface_access(ik, CHECK); 5896 5897 // check if this class overrides any final method 5898 check_final_method_override(ik, CHECK); 5899 5900 // reject static interface methods prior to Java 8 5901 if (ik->is_interface() && _major_version < JAVA_8_VERSION) { 5902 check_illegal_static_method(ik, CHECK); 5903 } 5904 5905 // Obtain this_klass' module entry 5906 ModuleEntry* module_entry = ik->module(); 5907 assert(module_entry != NULL, "module_entry should always be set"); 5908 5909 // Obtain java.lang.Module 5910 Handle module_handle(THREAD, module_entry->module()); 5911 5912 // Allocate mirror and initialize static fields 5913 // The create_mirror() call will also call compute_modifiers() 5914 java_lang_Class::create_mirror(ik, 5915 Handle(THREAD, _loader_data->class_loader()), 5916 module_handle, 5917 _protection_domain, 5918 CHECK); 5919 5920 assert(_all_mirandas != NULL, "invariant"); 5921 5922 // Generate any default methods - default methods are public interface methods 5923 // that have a default implementation. This is new with Java 8. 5924 if (_has_nonstatic_concrete_methods) { 5925 DefaultMethods::generate_default_methods(ik, 5926 _all_mirandas, 5927 CHECK); 5928 } 5929 5930 // Add read edges to the unnamed modules of the bootstrap and app class loaders. 5931 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() && 5932 !module_entry->has_default_read_edges()) { 5933 if (!module_entry->set_has_default_read_edges()) { 5934 // We won a potential race 5935 JvmtiExport::add_default_read_edges(module_handle, THREAD); 5936 } 5937 } 5938 5939 int nfields = ik->java_fields_count(); 5940 if (ik->is_value()) nfields++; 5941 for (int i = 0; i < nfields; i++) { 5942 if (ik->field_access_flags(i) & JVM_ACC_FLATTENABLE) { 5943 Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK); 5944 // Value classes must have been pre-loaded 5945 Klass* klass = SystemDictionary::find(klass_name, 5946 Handle(THREAD, ik->class_loader()), 5947 Handle(THREAD, ik->protection_domain()), CHECK); 5948 assert(klass != NULL, "Sanity check"); 5949 assert(klass->access_flags().is_value_type(), "Value type expected"); 5950 ik->set_value_field_klass(i, klass); 5951 klass_name->decrement_refcount(); 5952 } else if (is_value_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0) 5953 && ((ik->field_access_flags(i) & JVM_ACC_STATIC) != 0)) { 5954 ValueKlass::cast(ik)->set_default_value_offset(ik->field_offset(i)); 5955 } 5956 } 5957 5958 if (is_value_type()) { 5959 ValueKlass::cast(ik)->initialize_calling_convention(CHECK); 5960 } 5961 5962 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */); 5963 5964 if (!is_internal()) { 5965 if (log_is_enabled(Info, class, load)) { 5966 ResourceMark rm; 5967 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string(); 5968 ik->print_class_load_logging(_loader_data, module_name, _stream); 5969 } 5970 5971 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION && 5972 ik->major_version() != JAVA_MIN_SUPPORTED_VERSION && 5973 log_is_enabled(Info, class, preview)) { 5974 ResourceMark rm; 5975 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)", 5976 ik->external_name(), ik->major_version()); 5977 } 5978 5979 if (log_is_enabled(Debug, class, resolve)) { 5980 ResourceMark rm; 5981 // print out the superclass. 5982 const char * from = ik->external_name(); 5983 if (ik->java_super() != NULL) { 5984 log_debug(class, resolve)("%s %s (super)", 5985 from, 5986 ik->java_super()->external_name()); 5987 } 5988 // print out each of the interface classes referred to by this class. 5989 const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces(); 5990 if (local_interfaces != NULL) { 5991 const int length = local_interfaces->length(); 5992 for (int i = 0; i < length; i++) { 5993 const InstanceKlass* const k = local_interfaces->at(i); 5994 const char * to = k->external_name(); 5995 log_debug(class, resolve)("%s %s (interface)", from, to); 5996 } 5997 } 5998 } 5999 } 6000 6001 JFR_ONLY(INIT_ID(ik);) 6002 6003 // If we reach here, all is well. 6004 // Now remove the InstanceKlass* from the _klass_to_deallocate field 6005 // in order for it to not be destroyed in the ClassFileParser destructor. 6006 set_klass_to_deallocate(NULL); 6007 6008 // it's official 6009 set_klass(ik); 6010 6011 debug_only(ik->verify();) 6012 } 6013 6014 void ClassFileParser::update_class_name(Symbol* new_class_name) { 6015 // Decrement the refcount in the old name, since we're clobbering it. 6016 _class_name->decrement_refcount(); 6017 6018 _class_name = new_class_name; 6019 // Increment the refcount of the new name. 6020 // Now the ClassFileParser owns this name and will decrement in 6021 // the destructor. 6022 _class_name->increment_refcount(); 6023 } 6024 6025 6026 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's 6027 // package by prepending its host class's package name to its class name and setting 6028 // its _class_name field. 6029 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) { 6030 ResourceMark rm(THREAD); 6031 assert(strrchr(_class_name->as_C_string(), '/') == NULL, 6032 "Unsafe anonymous class should not be in a package"); 6033 const char* host_pkg_name = 6034 ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL); 6035 6036 if (host_pkg_name != NULL) { 6037 int host_pkg_len = (int)strlen(host_pkg_name); 6038 int class_name_len = _class_name->utf8_length(); 6039 int symbol_len = host_pkg_len + 1 + class_name_len; 6040 char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1); 6041 int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s", 6042 host_pkg_name, class_name_len, _class_name->base()); 6043 assert(n == symbol_len, "Unexpected number of characters in string"); 6044 6045 // Decrement old _class_name to avoid leaking. 6046 _class_name->decrement_refcount(); 6047 6048 // Create a symbol and update the anonymous class name. 6049 // The new class name is created with a refcount of one. When installed into the InstanceKlass, 6050 // it'll be two and when the ClassFileParser destructor runs, it'll go back to one and get deleted 6051 // when the class is unloaded. 6052 _class_name = SymbolTable::new_symbol(new_anon_name, symbol_len, CHECK); 6053 } 6054 } 6055 6056 // If the host class and the anonymous class are in the same package then do 6057 // nothing. If the anonymous class is in the unnamed package then move it to its 6058 // host's package. If the classes are in different packages then throw an IAE 6059 // exception. 6060 void ClassFileParser::fix_unsafe_anonymous_class_name(TRAPS) { 6061 assert(_unsafe_anonymous_host != NULL, "Expected an unsafe anonymous class"); 6062 6063 const jbyte* anon_last_slash = UTF8::strrchr((const jbyte*)_class_name->base(), 6064 _class_name->utf8_length(), '/'); 6065 if (anon_last_slash == NULL) { // Unnamed package 6066 prepend_host_package_name(_unsafe_anonymous_host, CHECK); 6067 } else { 6068 if (!_unsafe_anonymous_host->is_same_class_package(_unsafe_anonymous_host->class_loader(), _class_name)) { 6069 ResourceMark rm(THREAD); 6070 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 6071 err_msg("Host class %s and anonymous class %s are in different packages", 6072 _unsafe_anonymous_host->name()->as_C_string(), _class_name->as_C_string())); 6073 } 6074 } 6075 } 6076 6077 static bool relax_format_check_for(ClassLoaderData* loader_data) { 6078 bool trusted = (loader_data->is_the_null_class_loader_data() || 6079 SystemDictionary::is_platform_class_loader(loader_data->class_loader())); 6080 bool need_verify = 6081 // verifyAll 6082 (BytecodeVerificationLocal && BytecodeVerificationRemote) || 6083 // verifyRemote 6084 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted); 6085 return !need_verify; 6086 } 6087 6088 ClassFileParser::ClassFileParser(ClassFileStream* stream, 6089 Symbol* name, 6090 ClassLoaderData* loader_data, 6091 Handle protection_domain, 6092 const InstanceKlass* unsafe_anonymous_host, 6093 GrowableArray<Handle>* cp_patches, 6094 Publicity pub_level, 6095 TRAPS) : 6096 _stream(stream), 6097 _requested_name(name), 6098 _class_name(NULL), 6099 _loader_data(loader_data), 6100 _unsafe_anonymous_host(unsafe_anonymous_host), 6101 _cp_patches(cp_patches), 6102 _num_patched_klasses(0), 6103 _max_num_patched_klasses(0), 6104 _orig_cp_size(0), 6105 _first_patched_klass_resolved_index(0), 6106 _super_klass(), 6107 _cp(NULL), 6108 _fields(NULL), 6109 _methods(NULL), 6110 _inner_classes(NULL), 6111 _nest_members(NULL), 6112 _nest_host(0), 6113 _local_interfaces(NULL), 6114 _transitive_interfaces(NULL), 6115 _combined_annotations(NULL), 6116 _annotations(NULL), 6117 _type_annotations(NULL), 6118 _fields_annotations(NULL), 6119 _fields_type_annotations(NULL), 6120 _klass(NULL), 6121 _klass_to_deallocate(NULL), 6122 _parsed_annotations(NULL), 6123 _fac(NULL), 6124 _field_info(NULL), 6125 _method_ordering(NULL), 6126 _all_mirandas(NULL), 6127 _vtable_size(0), 6128 _itable_size(0), 6129 _num_miranda_methods(0), 6130 _rt(REF_NONE), 6131 _protection_domain(protection_domain), 6132 _access_flags(), 6133 _pub_level(pub_level), 6134 _bad_constant_seen(0), 6135 _synthetic_flag(false), 6136 _sde_length(false), 6137 _sde_buffer(NULL), 6138 _sourcefile_index(0), 6139 _generic_signature_index(0), 6140 _major_version(0), 6141 _minor_version(0), 6142 _this_class_index(0), 6143 _super_class_index(0), 6144 _itfs_len(0), 6145 _java_fields_count(0), 6146 _need_verify(false), 6147 _relax_verify(false), 6148 _has_nonstatic_concrete_methods(false), 6149 _declares_nonstatic_concrete_methods(false), 6150 _has_final_method(false), 6151 _has_flattenable_fields(false), 6152 _has_finalizer(false), 6153 _has_empty_finalizer(false), 6154 _has_vanilla_constructor(false), 6155 _max_bootstrap_specifier_index(-1) { 6156 6157 _class_name = name != NULL ? name : vmSymbols::unknown_class_name(); 6158 _class_name->increment_refcount(); 6159 6160 assert(THREAD->is_Java_thread(), "invariant"); 6161 assert(_loader_data != NULL, "invariant"); 6162 assert(stream != NULL, "invariant"); 6163 assert(_stream != NULL, "invariant"); 6164 assert(_stream->buffer() == _stream->current(), "invariant"); 6165 assert(_class_name != NULL, "invariant"); 6166 assert(0 == _access_flags.as_int(), "invariant"); 6167 6168 // Figure out whether we can skip format checking (matching classic VM behavior) 6169 if (DumpSharedSpaces) { 6170 // verify == true means it's a 'remote' class (i.e., non-boot class) 6171 // Verification decision is based on BytecodeVerificationRemote flag 6172 // for those classes. 6173 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote : 6174 BytecodeVerificationLocal; 6175 } 6176 else { 6177 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(), 6178 stream->need_verify()); 6179 } 6180 if (_cp_patches != NULL) { 6181 int len = _cp_patches->length(); 6182 for (int i=0; i<len; i++) { 6183 if (has_cp_patch_at(i)) { 6184 Handle patch = cp_patch_at(i); 6185 if (java_lang_String::is_instance(patch()) || java_lang_Class::is_instance(patch())) { 6186 // We need to append the names of the patched classes to the end of the constant pool, 6187 // because a patched class may have a Utf8 name that's not already included in the 6188 // original constant pool. These class names are used when patch_constant_pool() 6189 // calls patch_class(). 6190 // 6191 // Note that a String in cp_patch_at(i) may be used to patch a Utf8, a String, or a Class. 6192 // At this point, we don't know the tag for index i yet, because we haven't parsed the 6193 // constant pool. So we can only assume the worst -- every String is used to patch a Class. 6194 _max_num_patched_klasses++; 6195 } 6196 } 6197 } 6198 } 6199 6200 // synch back verification state to stream 6201 stream->set_verify(_need_verify); 6202 6203 // Check if verification needs to be relaxed for this class file 6204 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) 6205 _relax_verify = relax_format_check_for(_loader_data); 6206 6207 parse_stream(stream, CHECK); 6208 6209 post_process_parsed_stream(stream, _cp, CHECK); 6210 } 6211 6212 void ClassFileParser::clear_class_metadata() { 6213 // metadata created before the instance klass is created. Must be 6214 // deallocated if classfile parsing returns an error. 6215 _cp = NULL; 6216 _fields = NULL; 6217 _methods = NULL; 6218 _inner_classes = NULL; 6219 _nest_members = NULL; 6220 _local_interfaces = NULL; 6221 _combined_annotations = NULL; 6222 _annotations = _type_annotations = NULL; 6223 _fields_annotations = _fields_type_annotations = NULL; 6224 } 6225 6226 // Destructor to clean up 6227 ClassFileParser::~ClassFileParser() { 6228 _class_name->decrement_refcount(); 6229 6230 if (_cp != NULL) { 6231 MetadataFactory::free_metadata(_loader_data, _cp); 6232 } 6233 if (_fields != NULL) { 6234 MetadataFactory::free_array<u2>(_loader_data, _fields); 6235 } 6236 6237 if (_methods != NULL) { 6238 // Free methods 6239 InstanceKlass::deallocate_methods(_loader_data, _methods); 6240 } 6241 6242 // beware of the Universe::empty_blah_array!! 6243 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) { 6244 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 6245 } 6246 6247 if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) { 6248 MetadataFactory::free_array<u2>(_loader_data, _nest_members); 6249 } 6250 6251 // Free interfaces 6252 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass, 6253 _local_interfaces, _transitive_interfaces); 6254 6255 if (_combined_annotations != NULL) { 6256 // After all annotations arrays have been created, they are installed into the 6257 // Annotations object that will be assigned to the InstanceKlass being created. 6258 6259 // Deallocate the Annotations object and the installed annotations arrays. 6260 _combined_annotations->deallocate_contents(_loader_data); 6261 6262 // If the _combined_annotations pointer is non-NULL, 6263 // then the other annotations fields should have been cleared. 6264 assert(_annotations == NULL, "Should have been cleared"); 6265 assert(_type_annotations == NULL, "Should have been cleared"); 6266 assert(_fields_annotations == NULL, "Should have been cleared"); 6267 assert(_fields_type_annotations == NULL, "Should have been cleared"); 6268 } else { 6269 // If the annotations arrays were not installed into the Annotations object, 6270 // then they have to be deallocated explicitly. 6271 MetadataFactory::free_array<u1>(_loader_data, _annotations); 6272 MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 6273 Annotations::free_contents(_loader_data, _fields_annotations); 6274 Annotations::free_contents(_loader_data, _fields_type_annotations); 6275 } 6276 6277 clear_class_metadata(); 6278 _transitive_interfaces = NULL; 6279 6280 // deallocate the klass if already created. Don't directly deallocate, but add 6281 // to the deallocate list so that the klass is removed from the CLD::_klasses list 6282 // at a safepoint. 6283 if (_klass_to_deallocate != NULL) { 6284 _loader_data->add_to_deallocate_list(_klass_to_deallocate); 6285 } 6286 } 6287 6288 void ClassFileParser::parse_stream(const ClassFileStream* const stream, 6289 TRAPS) { 6290 6291 assert(stream != NULL, "invariant"); 6292 assert(_class_name != NULL, "invariant"); 6293 6294 // BEGIN STREAM PARSING 6295 stream->guarantee_more(8, CHECK); // magic, major, minor 6296 // Magic value 6297 const u4 magic = stream->get_u4_fast(); 6298 guarantee_property(magic == JAVA_CLASSFILE_MAGIC, 6299 "Incompatible magic value %u in class file %s", 6300 magic, CHECK); 6301 6302 // Version numbers 6303 _minor_version = stream->get_u2_fast(); 6304 _major_version = stream->get_u2_fast(); 6305 6306 if (DumpSharedSpaces && _major_version < JAVA_1_5_VERSION) { 6307 ResourceMark rm; 6308 warning("Pre JDK 1.5 class not supported by CDS: %u.%u %s", 6309 _major_version, _minor_version, _class_name->as_C_string()); 6310 Exceptions::fthrow( 6311 THREAD_AND_LOCATION, 6312 vmSymbols::java_lang_UnsupportedClassVersionError(), 6313 "Unsupported major.minor version for dump time %u.%u", 6314 _major_version, 6315 _minor_version); 6316 } 6317 6318 // Check version numbers - we check this even with verifier off 6319 verify_class_version(_major_version, _minor_version, _class_name, CHECK); 6320 6321 stream->guarantee_more(3, CHECK); // length, first cp tag 6322 u2 cp_size = stream->get_u2_fast(); 6323 6324 guarantee_property( 6325 cp_size >= 1, "Illegal constant pool size %u in class file %s", 6326 cp_size, CHECK); 6327 6328 _orig_cp_size = cp_size; 6329 if (int(cp_size) + _max_num_patched_klasses > 0xffff) { 6330 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes"); 6331 } 6332 cp_size += _max_num_patched_klasses; 6333 6334 _cp = ConstantPool::allocate(_loader_data, 6335 cp_size, 6336 CHECK); 6337 6338 ConstantPool* const cp = _cp; 6339 6340 parse_constant_pool(stream, cp, _orig_cp_size, CHECK); 6341 6342 assert(cp_size == (const u2)cp->length(), "invariant"); 6343 6344 // ACCESS FLAGS 6345 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 6346 6347 jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS; 6348 // JVM_ACC_MODULE is defined in JDK-9 and later. 6349 if (_major_version >= JAVA_9_VERSION) { 6350 recognized_modifiers |= JVM_ACC_MODULE; 6351 } 6352 // JVM_ACC_VALUE is defined for class file version 55 and later 6353 if (supports_value_types()) { 6354 recognized_modifiers |= JVM_ACC_VALUE; 6355 } 6356 6357 // Access flags 6358 jint flags = stream->get_u2_fast() & recognized_modifiers; 6359 6360 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 6361 // Set abstract bit for old class files for backward compatibility 6362 flags |= JVM_ACC_ABSTRACT; 6363 } 6364 6365 verify_legal_class_modifiers(flags, CHECK); 6366 6367 short bad_constant = class_bad_constant_seen(); 6368 if (bad_constant != 0) { 6369 // Do not throw CFE until after the access_flags are checked because if 6370 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE. 6371 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK); 6372 } 6373 6374 _access_flags.set_flags(flags); 6375 6376 // This class and superclass 6377 _this_class_index = stream->get_u2_fast(); 6378 check_property( 6379 valid_cp_range(_this_class_index, cp_size) && 6380 cp->tag_at(_this_class_index).is_unresolved_klass(), 6381 "Invalid this class index %u in constant pool in class file %s", 6382 _this_class_index, CHECK); 6383 6384 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index); 6385 assert(class_name_in_cp != NULL, "class_name can't be null"); 6386 6387 // Update _class_name to reflect the name in the constant pool 6388 update_class_name(class_name_in_cp); 6389 6390 // Don't need to check whether this class name is legal or not. 6391 // It has been checked when constant pool is parsed. 6392 // However, make sure it is not an array type. 6393 if (_need_verify) { 6394 guarantee_property(_class_name->char_at(0) != JVM_SIGNATURE_ARRAY, 6395 "Bad class name in class file %s", 6396 CHECK); 6397 } 6398 6399 // Checks if name in class file matches requested name 6400 if (_requested_name != NULL && _requested_name != _class_name) { 6401 ResourceMark rm(THREAD); 6402 Exceptions::fthrow( 6403 THREAD_AND_LOCATION, 6404 vmSymbols::java_lang_NoClassDefFoundError(), 6405 "%s (wrong name: %s)", 6406 _class_name->as_C_string(), 6407 _requested_name != NULL ? _requested_name->as_C_string() : "NoName" 6408 ); 6409 return; 6410 } 6411 6412 // if this is an anonymous class fix up its name if it's in the unnamed 6413 // package. Otherwise, throw IAE if it is in a different package than 6414 // its host class. 6415 if (_unsafe_anonymous_host != NULL) { 6416 fix_unsafe_anonymous_class_name(CHECK); 6417 } 6418 6419 // Verification prevents us from creating names with dots in them, this 6420 // asserts that that's the case. 6421 assert(is_internal_format(_class_name), "external class name format used internally"); 6422 6423 if (!is_internal()) { 6424 LogTarget(Debug, class, preorder) lt; 6425 if (lt.is_enabled()){ 6426 ResourceMark rm(THREAD); 6427 LogStream ls(lt); 6428 ls.print("%s", _class_name->as_klass_external_name()); 6429 if (stream->source() != NULL) { 6430 ls.print(" source: %s", stream->source()); 6431 } 6432 ls.cr(); 6433 } 6434 6435 #if INCLUDE_CDS 6436 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) { 6437 if (!ClassLoader::has_jrt_entry()) { 6438 warning("DumpLoadedClassList and CDS are not supported in exploded build"); 6439 DumpLoadedClassList = NULL; 6440 } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) && 6441 _unsafe_anonymous_host == NULL) { 6442 // Only dump the classes that can be stored into CDS archive. 6443 // Unsafe anonymous classes such as generated LambdaForm classes are also not included. 6444 oop class_loader = _loader_data->class_loader(); 6445 ResourceMark rm(THREAD); 6446 bool skip = false; 6447 if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) { 6448 // For the boot and platform class loaders, skip classes that are not found in the 6449 // java runtime image, such as those found in the --patch-module entries. 6450 // These classes can't be loaded from the archive during runtime. 6451 if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) { 6452 skip = true; 6453 } 6454 6455 if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) { 6456 // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a 6457 // as they can be loaded from the archive during runtime. 6458 skip = false; 6459 } 6460 } 6461 if (skip) { 6462 tty->print_cr("skip writing class %s from source %s to classlist file", 6463 _class_name->as_C_string(), stream->source()); 6464 } else { 6465 classlist_file->print_cr("%s", _class_name->as_C_string()); 6466 classlist_file->flush(); 6467 } 6468 } 6469 } 6470 #endif 6471 } 6472 6473 // SUPERKLASS 6474 _super_class_index = stream->get_u2_fast(); 6475 _super_klass = parse_super_class(cp, 6476 _super_class_index, 6477 _need_verify, 6478 CHECK); 6479 6480 // Interfaces 6481 _itfs_len = stream->get_u2_fast(); 6482 parse_interfaces(stream, 6483 _itfs_len, 6484 cp, 6485 &_has_nonstatic_concrete_methods, 6486 CHECK); 6487 6488 assert(_local_interfaces != NULL, "invariant"); 6489 6490 // Fields (offsets are filled in later) 6491 _fac = new FieldAllocationCount(); 6492 parse_fields(stream, 6493 _access_flags.is_interface(), 6494 _access_flags.is_value_type(), 6495 _fac, 6496 cp, 6497 cp_size, 6498 &_java_fields_count, 6499 CHECK); 6500 6501 assert(_fields != NULL, "invariant"); 6502 6503 // Methods 6504 AccessFlags promoted_flags; 6505 parse_methods(stream, 6506 _access_flags.is_interface(), 6507 _access_flags.is_value_type(), 6508 &promoted_flags, 6509 &_has_final_method, 6510 &_declares_nonstatic_concrete_methods, 6511 CHECK); 6512 6513 assert(_methods != NULL, "invariant"); 6514 6515 // promote flags from parse_methods() to the klass' flags 6516 _access_flags.add_promoted_flags(promoted_flags.as_int()); 6517 6518 if (_declares_nonstatic_concrete_methods) { 6519 _has_nonstatic_concrete_methods = true; 6520 } 6521 6522 // Additional attributes/annotations 6523 _parsed_annotations = new ClassAnnotationCollector(); 6524 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK); 6525 6526 assert(_inner_classes != NULL, "invariant"); 6527 6528 // Finalize the Annotations metadata object, 6529 // now that all annotation arrays have been created. 6530 create_combined_annotations(CHECK); 6531 6532 // Make sure this is the end of class file stream 6533 guarantee_property(stream->at_eos(), 6534 "Extra bytes at the end of class file %s", 6535 CHECK); 6536 6537 // all bytes in stream read and parsed 6538 } 6539 6540 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream, 6541 ConstantPool* cp, 6542 TRAPS) { 6543 assert(stream != NULL, "invariant"); 6544 assert(stream->at_eos(), "invariant"); 6545 assert(cp != NULL, "invariant"); 6546 assert(_loader_data != NULL, "invariant"); 6547 6548 if (_class_name == vmSymbols::java_lang_Object()) { 6549 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(), 6550 "java.lang.Object cannot implement an interface in class file %s", 6551 CHECK); 6552 } 6553 // We check super class after class file is parsed and format is checked 6554 if (_super_class_index > 0 && NULL ==_super_klass) { 6555 Symbol* const super_class_name = cp->klass_name_at(_super_class_index); 6556 if (_access_flags.is_interface()) { 6557 // Before attempting to resolve the superclass, check for class format 6558 // errors not checked yet. 6559 guarantee_property(super_class_name == vmSymbols::java_lang_Object(), 6560 "Interfaces must have java.lang.Object as superclass in class file %s", 6561 CHECK); 6562 } 6563 Handle loader(THREAD, _loader_data->class_loader()); 6564 _super_klass = (const InstanceKlass*) 6565 SystemDictionary::resolve_super_or_fail(_class_name, 6566 super_class_name, 6567 loader, 6568 _protection_domain, 6569 true, 6570 CHECK); 6571 } 6572 6573 if (_super_klass != NULL) { 6574 if (_super_klass->has_nonstatic_concrete_methods()) { 6575 _has_nonstatic_concrete_methods = true; 6576 } 6577 6578 if (_super_klass->is_interface()) { 6579 ResourceMark rm(THREAD); 6580 Exceptions::fthrow( 6581 THREAD_AND_LOCATION, 6582 vmSymbols::java_lang_IncompatibleClassChangeError(), 6583 "class %s has interface %s as super class", 6584 _class_name->as_klass_external_name(), 6585 _super_klass->external_name() 6586 ); 6587 return; 6588 } 6589 6590 // For a value class, only java/lang/Object is an acceptable super class 6591 if (_access_flags.get_flags() & JVM_ACC_VALUE) { 6592 guarantee_property(_super_klass->name() == vmSymbols::java_lang_Object(), 6593 "Value type must have java.lang.Object as superclass in class file %s", 6594 CHECK); 6595 } 6596 6597 // Make sure super class is not final 6598 if (_super_klass->is_final()) { 6599 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class"); 6600 } 6601 } 6602 6603 // Compute the transitive list of all unique interfaces implemented by this class 6604 _transitive_interfaces = 6605 compute_transitive_interfaces(_super_klass, 6606 _local_interfaces, 6607 _loader_data, 6608 CHECK); 6609 6610 assert(_transitive_interfaces != NULL, "invariant"); 6611 6612 // sort methods 6613 _method_ordering = sort_methods(_methods); 6614 6615 _all_mirandas = new GrowableArray<Method*>(20); 6616 6617 Handle loader(THREAD, _loader_data->class_loader()); 6618 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size, 6619 &_num_miranda_methods, 6620 _all_mirandas, 6621 _super_klass, 6622 _methods, 6623 _access_flags, 6624 _major_version, 6625 loader, 6626 _class_name, 6627 _local_interfaces, 6628 CHECK); 6629 6630 // Size of Java itable (in words) 6631 _itable_size = _access_flags.is_interface() ? 0 : 6632 klassItable::compute_itable_size(_transitive_interfaces); 6633 6634 assert(_fac != NULL, "invariant"); 6635 assert(_parsed_annotations != NULL, "invariant"); 6636 6637 6638 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 6639 if (fs.is_flattenable()) { 6640 // Pre-load value class 6641 Klass* klass = SystemDictionary::resolve_flattenable_field_or_fail(&fs, 6642 Handle(THREAD, _loader_data->class_loader()), 6643 _protection_domain, true, CHECK); 6644 assert(klass != NULL, "Sanity check"); 6645 assert(klass->access_flags().is_value_type(), "Value type expected"); 6646 _has_flattenable_fields = true; 6647 } 6648 } 6649 6650 _field_info = new FieldLayoutInfo(); 6651 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK); 6652 6653 // Compute reference typ 6654 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type(); 6655 6656 } 6657 6658 void ClassFileParser::set_klass(InstanceKlass* klass) { 6659 6660 #ifdef ASSERT 6661 if (klass != NULL) { 6662 assert(NULL == _klass, "leaking?"); 6663 } 6664 #endif 6665 6666 _klass = klass; 6667 } 6668 6669 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) { 6670 6671 #ifdef ASSERT 6672 if (klass != NULL) { 6673 assert(NULL == _klass_to_deallocate, "leaking?"); 6674 } 6675 #endif 6676 6677 _klass_to_deallocate = klass; 6678 } 6679 6680 // Caller responsible for ResourceMark 6681 // clone stream with rewound position 6682 const ClassFileStream* ClassFileParser::clone_stream() const { 6683 assert(_stream != NULL, "invariant"); 6684 6685 return _stream->clone(); 6686 } 6687 6688 // ---------------------------------------------------------------------------- 6689 // debugging 6690 6691 #ifdef ASSERT 6692 6693 // return true if class_name contains no '.' (internal format is '/') 6694 bool ClassFileParser::is_internal_format(Symbol* class_name) { 6695 if (class_name != NULL) { 6696 ResourceMark rm; 6697 char* name = class_name->as_C_string(); 6698 return strchr(name, '.') == NULL; 6699 } else { 6700 return true; 6701 } 6702 } 6703 6704 #endif