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