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