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