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