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