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