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