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