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