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