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