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