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