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