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