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