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