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 == JVM_CLASSFILE_MAJOR_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_14_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 JVM_CLASSFILE_MAJOR_VERSION.65535 and 3728 // --enable-preview wasn't specified then a java.lang.UnsupportedClassVersionError 3729 // exception would have been thrown. 3730 ResourceMark rm(THREAD); 3731 if (supports_records()) { 3732 log_info(class, record)( 3733 "Ignoring Record attribute in class %s because super type is not java.lang.Record", 3734 _class_name->as_C_string()); 3735 } else { 3736 log_info(class, record)( 3737 "Ignoring Record attribute in class %s because class file version is not %d.65535", 3738 _class_name->as_C_string(), JVM_CLASSFILE_MAJOR_VERSION); 3739 } 3740 } 3741 cfs->skip_u1(attribute_length, CHECK); 3742 } else { 3743 // Unknown attribute 3744 cfs->skip_u1(attribute_length, CHECK); 3745 } 3746 } else { 3747 // Unknown attribute 3748 cfs->skip_u1(attribute_length, CHECK); 3749 } 3750 } else { 3751 // Unknown attribute 3752 cfs->skip_u1(attribute_length, CHECK); 3753 } 3754 } else { 3755 // Unknown attribute 3756 cfs->skip_u1(attribute_length, CHECK); 3757 } 3758 } 3759 _class_annotations = assemble_annotations(runtime_visible_annotations, 3760 runtime_visible_annotations_length, 3761 runtime_invisible_annotations, 3762 runtime_invisible_annotations_length, 3763 CHECK); 3764 _class_type_annotations = assemble_annotations(runtime_visible_type_annotations, 3765 runtime_visible_type_annotations_length, 3766 runtime_invisible_type_annotations, 3767 runtime_invisible_type_annotations_length, 3768 CHECK); 3769 3770 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 3771 const u2 num_of_classes = parse_classfile_inner_classes_attribute( 3772 cfs, 3773 inner_classes_attribute_start, 3774 parsed_innerclasses_attribute, 3775 enclosing_method_class_index, 3776 enclosing_method_method_index, 3777 CHECK); 3778 if (parsed_innerclasses_attribute && _need_verify && _major_version >= JAVA_1_5_VERSION) { 3779 guarantee_property( 3780 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 3781 "Wrong InnerClasses attribute length in class file %s", CHECK); 3782 } 3783 } 3784 3785 if (parsed_nest_members_attribute) { 3786 const u2 num_of_classes = parse_classfile_nest_members_attribute( 3787 cfs, 3788 nest_members_attribute_start, 3789 CHECK); 3790 if (_need_verify) { 3791 guarantee_property( 3792 nest_members_attribute_length == sizeof(num_of_classes) + sizeof(u2) * num_of_classes, 3793 "Wrong NestMembers attribute length in class file %s", CHECK); 3794 } 3795 } 3796 3797 if (parsed_record_attribute) { 3798 const unsigned int calculated_attr_length = parse_classfile_record_attribute( 3799 cfs, 3800 cp, 3801 record_attribute_start, 3802 CHECK); 3803 if (_need_verify) { 3804 guarantee_property(record_attribute_length == calculated_attr_length, 3805 "Record attribute has wrong length in class file %s", 3806 CHECK); 3807 } 3808 } 3809 3810 if (_max_bootstrap_specifier_index >= 0) { 3811 guarantee_property(parsed_bootstrap_methods_attribute, 3812 "Missing BootstrapMethods attribute in class file %s", CHECK); 3813 } 3814 } 3815 3816 void ClassFileParser::apply_parsed_class_attributes(InstanceKlass* k) { 3817 assert(k != NULL, "invariant"); 3818 3819 if (_synthetic_flag) 3820 k->set_is_synthetic(); 3821 if (_sourcefile_index != 0) { 3822 k->set_source_file_name_index(_sourcefile_index); 3823 } 3824 if (_generic_signature_index != 0) { 3825 k->set_generic_signature_index(_generic_signature_index); 3826 } 3827 if (_sde_buffer != NULL) { 3828 k->set_source_debug_extension(_sde_buffer, _sde_length); 3829 } 3830 } 3831 3832 // Create the Annotations object that will 3833 // hold the annotations array for the Klass. 3834 void ClassFileParser::create_combined_annotations(TRAPS) { 3835 if (_class_annotations == NULL && 3836 _class_type_annotations == NULL && 3837 _fields_annotations == NULL && 3838 _fields_type_annotations == NULL) { 3839 // Don't create the Annotations object unnecessarily. 3840 return; 3841 } 3842 3843 Annotations* const annotations = Annotations::allocate(_loader_data, CHECK); 3844 annotations->set_class_annotations(_class_annotations); 3845 annotations->set_class_type_annotations(_class_type_annotations); 3846 annotations->set_fields_annotations(_fields_annotations); 3847 annotations->set_fields_type_annotations(_fields_type_annotations); 3848 3849 // This is the Annotations object that will be 3850 // assigned to InstanceKlass being constructed. 3851 _combined_annotations = annotations; 3852 3853 // The annotations arrays below has been transfered the 3854 // _combined_annotations so these fields can now be cleared. 3855 _class_annotations = NULL; 3856 _class_type_annotations = NULL; 3857 _fields_annotations = NULL; 3858 _fields_type_annotations = NULL; 3859 } 3860 3861 // Transfer ownership of metadata allocated to the InstanceKlass. 3862 void ClassFileParser::apply_parsed_class_metadata( 3863 InstanceKlass* this_klass, 3864 int java_fields_count, 3865 TRAPS) { 3866 assert(this_klass != NULL, "invariant"); 3867 3868 _cp->set_pool_holder(this_klass); 3869 this_klass->set_constants(_cp); 3870 this_klass->set_fields(_fields, java_fields_count); 3871 this_klass->set_methods(_methods); 3872 this_klass->set_inner_classes(_inner_classes); 3873 this_klass->set_nest_members(_nest_members); 3874 this_klass->set_nest_host_index(_nest_host); 3875 this_klass->set_local_interfaces(_local_interfaces); 3876 this_klass->set_annotations(_combined_annotations); 3877 this_klass->set_record_components(_record_components); 3878 // Delay the setting of _transitive_interfaces until after initialize_supers() in 3879 // fill_instance_klass(). It is because the _transitive_interfaces may be shared with 3880 // its _super. If an OOM occurs while loading the current klass, its _super field 3881 // may not have been set. When GC tries to free the klass, the _transitive_interfaces 3882 // may be deallocated mistakenly in InstanceKlass::deallocate_interfaces(). Subsequent 3883 // dereferences to the deallocated _transitive_interfaces will result in a crash. 3884 3885 // Clear out these fields so they don't get deallocated by the destructor 3886 clear_class_metadata(); 3887 } 3888 3889 AnnotationArray* ClassFileParser::assemble_annotations(const u1* const runtime_visible_annotations, 3890 int runtime_visible_annotations_length, 3891 const u1* const runtime_invisible_annotations, 3892 int runtime_invisible_annotations_length, 3893 TRAPS) { 3894 AnnotationArray* annotations = NULL; 3895 if (runtime_visible_annotations != NULL || 3896 runtime_invisible_annotations != NULL) { 3897 annotations = MetadataFactory::new_array<u1>(_loader_data, 3898 runtime_visible_annotations_length + 3899 runtime_invisible_annotations_length, 3900 CHECK_(annotations)); 3901 if (runtime_visible_annotations != NULL) { 3902 for (int i = 0; i < runtime_visible_annotations_length; i++) { 3903 annotations->at_put(i, runtime_visible_annotations[i]); 3904 } 3905 } 3906 if (runtime_invisible_annotations != NULL) { 3907 for (int i = 0; i < runtime_invisible_annotations_length; i++) { 3908 int append = runtime_visible_annotations_length+i; 3909 annotations->at_put(append, runtime_invisible_annotations[i]); 3910 } 3911 } 3912 } 3913 return annotations; 3914 } 3915 3916 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp, 3917 const int super_class_index, 3918 const bool need_verify, 3919 TRAPS) { 3920 assert(cp != NULL, "invariant"); 3921 const InstanceKlass* super_klass = NULL; 3922 3923 if (super_class_index == 0) { 3924 check_property(_class_name == vmSymbols::java_lang_Object(), 3925 "Invalid superclass index %u in class file %s", 3926 super_class_index, 3927 CHECK_NULL); 3928 } else { 3929 check_property(valid_klass_reference_at(super_class_index), 3930 "Invalid superclass index %u in class file %s", 3931 super_class_index, 3932 CHECK_NULL); 3933 // The class name should be legal because it is checked when parsing constant pool. 3934 // However, make sure it is not an array type. 3935 bool is_array = false; 3936 if (cp->tag_at(super_class_index).is_klass()) { 3937 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index)); 3938 if (need_verify) 3939 is_array = super_klass->is_array_klass(); 3940 } else if (need_verify) { 3941 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY); 3942 } 3943 if (need_verify) { 3944 guarantee_property(!is_array, 3945 "Bad superclass name in class file %s", CHECK_NULL); 3946 } 3947 } 3948 return super_klass; 3949 } 3950 3951 static unsigned int compute_oop_map_count(const InstanceKlass* super, 3952 unsigned int nonstatic_oop_map_count, 3953 int first_nonstatic_oop_offset) { 3954 3955 unsigned int map_count = 3956 NULL == super ? 0 : super->nonstatic_oop_map_count(); 3957 if (nonstatic_oop_map_count > 0) { 3958 // We have oops to add to map 3959 if (map_count == 0) { 3960 map_count = nonstatic_oop_map_count; 3961 } 3962 else { 3963 // Check whether we should add a new map block or whether the last one can 3964 // be extended 3965 const OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps(); 3966 const OopMapBlock* const last_map = first_map + map_count - 1; 3967 3968 const int next_offset = last_map->offset() + last_map->count() * heapOopSize; 3969 if (next_offset == first_nonstatic_oop_offset) { 3970 // There is no gap bettwen superklass's last oop field and first 3971 // local oop field, merge maps. 3972 nonstatic_oop_map_count -= 1; 3973 } 3974 else { 3975 // Superklass didn't end with a oop field, add extra maps 3976 assert(next_offset < first_nonstatic_oop_offset, "just checking"); 3977 } 3978 map_count += nonstatic_oop_map_count; 3979 } 3980 } 3981 return map_count; 3982 } 3983 3984 #ifndef PRODUCT 3985 static void print_field_layout(const Symbol* name, 3986 Array<u2>* fields, 3987 ConstantPool* cp, 3988 int instance_size, 3989 int instance_fields_start, 3990 int instance_fields_end, 3991 int static_fields_end) { 3992 3993 assert(name != NULL, "invariant"); 3994 3995 tty->print("%s: field layout\n", name->as_klass_external_name()); 3996 tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---"); 3997 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 3998 if (!fs.access_flags().is_static()) { 3999 tty->print(" @%3d \"%s\" %s\n", 4000 fs.offset(), 4001 fs.name()->as_klass_external_name(), 4002 fs.signature()->as_klass_external_name()); 4003 } 4004 } 4005 tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---"); 4006 tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---"); 4007 tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---"); 4008 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) { 4009 if (fs.access_flags().is_static()) { 4010 tty->print(" @%3d \"%s\" %s\n", 4011 fs.offset(), 4012 fs.name()->as_klass_external_name(), 4013 fs.signature()->as_klass_external_name()); 4014 } 4015 } 4016 tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---"); 4017 tty->print("\n"); 4018 } 4019 #endif 4020 4021 // Values needed for oopmap and InstanceKlass creation 4022 class ClassFileParser::FieldLayoutInfo : public ResourceObj { 4023 public: 4024 int* nonstatic_oop_offsets; 4025 unsigned int* nonstatic_oop_counts; 4026 unsigned int nonstatic_oop_map_count; 4027 unsigned int total_oop_map_count; 4028 int instance_size; 4029 int nonstatic_field_size; 4030 int static_field_size; 4031 bool has_nonstatic_fields; 4032 }; 4033 4034 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring! 4035 void ClassFileParser::layout_fields(ConstantPool* cp, 4036 const FieldAllocationCount* fac, 4037 const ClassAnnotationCollector* parsed_annotations, 4038 FieldLayoutInfo* info, 4039 TRAPS) { 4040 4041 assert(cp != NULL, "invariant"); 4042 4043 // Field size and offset computation 4044 int nonstatic_field_size = _super_klass == NULL ? 0 : 4045 _super_klass->nonstatic_field_size(); 4046 4047 // Count the contended fields by type. 4048 // 4049 // We ignore static fields, because @Contended is not supported for them. 4050 // The layout code below will also ignore the static fields. 4051 int nonstatic_contended_count = 0; 4052 FieldAllocationCount fac_contended; 4053 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4054 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 4055 if (fs.is_contended()) { 4056 fac_contended.count[atype]++; 4057 if (!fs.access_flags().is_static()) { 4058 nonstatic_contended_count++; 4059 } 4060 } 4061 } 4062 4063 4064 // Calculate the starting byte offsets 4065 int next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields(); 4066 int next_static_double_offset = next_static_oop_offset + 4067 ((fac->count[STATIC_OOP]) * heapOopSize); 4068 if (fac->count[STATIC_DOUBLE]) { 4069 next_static_double_offset = align_up(next_static_double_offset, BytesPerLong); 4070 } 4071 4072 int next_static_word_offset = next_static_double_offset + 4073 ((fac->count[STATIC_DOUBLE]) * BytesPerLong); 4074 int next_static_short_offset = next_static_word_offset + 4075 ((fac->count[STATIC_WORD]) * BytesPerInt); 4076 int next_static_byte_offset = next_static_short_offset + 4077 ((fac->count[STATIC_SHORT]) * BytesPerShort); 4078 4079 int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() + 4080 nonstatic_field_size * heapOopSize; 4081 4082 int next_nonstatic_field_offset = nonstatic_fields_start; 4083 4084 const bool is_contended_class = parsed_annotations->is_contended(); 4085 4086 // Class is contended, pad before all the fields 4087 if (is_contended_class) { 4088 next_nonstatic_field_offset += ContendedPaddingWidth; 4089 } 4090 4091 // Compute the non-contended fields count. 4092 // The packing code below relies on these counts to determine if some field 4093 // can be squeezed into the alignment gap. Contended fields are obviously 4094 // exempt from that. 4095 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE]; 4096 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD]; 4097 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT]; 4098 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE]; 4099 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP]; 4100 4101 // Total non-static fields count, including every contended field 4102 unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] + 4103 fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] + 4104 fac->count[NONSTATIC_OOP]; 4105 4106 const bool super_has_nonstatic_fields = 4107 (_super_klass != NULL && _super_klass->has_nonstatic_fields()); 4108 const bool has_nonstatic_fields = 4109 super_has_nonstatic_fields || (nonstatic_fields_count != 0); 4110 4111 4112 // Prepare list of oops for oop map generation. 4113 // 4114 // "offset" and "count" lists are describing the set of contiguous oop 4115 // regions. offset[i] is the start of the i-th region, which then has 4116 // count[i] oops following. Before we know how many regions are required, 4117 // we pessimistically allocate the maps to fit all the oops into the 4118 // distinct regions. 4119 // 4120 // TODO: We add +1 to always allocate non-zero resource arrays; we need 4121 // to figure out if we still need to do this. 4122 unsigned int nonstatic_oop_map_count = 0; 4123 unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1; 4124 4125 int* nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD( 4126 THREAD, int, max_nonstatic_oop_maps); 4127 unsigned int* const nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD( 4128 THREAD, unsigned int, max_nonstatic_oop_maps); 4129 4130 int first_nonstatic_oop_offset = 0; // will be set for first oop field 4131 4132 bool compact_fields = CompactFields; 4133 int allocation_style = FieldsAllocationStyle; 4134 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range? 4135 assert(false, "0 <= FieldsAllocationStyle <= 2"); 4136 allocation_style = 1; // Optimistic 4137 } 4138 4139 // The next classes have predefined hard-coded fields offsets 4140 // (see in JavaClasses::compute_hard_coded_offsets()). 4141 // Use default fields allocation order for them. 4142 if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL && 4143 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() || 4144 _class_name == vmSymbols::java_lang_Class() || 4145 _class_name == vmSymbols::java_lang_ClassLoader() || 4146 _class_name == vmSymbols::java_lang_ref_Reference() || 4147 _class_name == vmSymbols::java_lang_ref_SoftReference() || 4148 _class_name == vmSymbols::java_lang_StackTraceElement() || 4149 _class_name == vmSymbols::java_lang_String() || 4150 _class_name == vmSymbols::java_lang_Throwable() || 4151 _class_name == vmSymbols::java_lang_Boolean() || 4152 _class_name == vmSymbols::java_lang_Character() || 4153 _class_name == vmSymbols::java_lang_Float() || 4154 _class_name == vmSymbols::java_lang_Double() || 4155 _class_name == vmSymbols::java_lang_Byte() || 4156 _class_name == vmSymbols::java_lang_Short() || 4157 _class_name == vmSymbols::java_lang_Integer() || 4158 _class_name == vmSymbols::java_lang_Long())) { 4159 allocation_style = 0; // Allocate oops first 4160 compact_fields = false; // Don't compact fields 4161 } 4162 4163 int next_nonstatic_oop_offset = 0; 4164 int next_nonstatic_double_offset = 0; 4165 4166 // Rearrange fields for a given allocation style 4167 if( allocation_style == 0 ) { 4168 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields 4169 next_nonstatic_oop_offset = next_nonstatic_field_offset; 4170 next_nonstatic_double_offset = next_nonstatic_oop_offset + 4171 (nonstatic_oop_count * heapOopSize); 4172 } else if( allocation_style == 1 ) { 4173 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields 4174 next_nonstatic_double_offset = next_nonstatic_field_offset; 4175 } else if( allocation_style == 2 ) { 4176 // Fields allocation: oops fields in super and sub classes are together. 4177 if( nonstatic_field_size > 0 && _super_klass != NULL && 4178 _super_klass->nonstatic_oop_map_size() > 0 ) { 4179 const unsigned int map_count = _super_klass->nonstatic_oop_map_count(); 4180 const OopMapBlock* const first_map = _super_klass->start_of_nonstatic_oop_maps(); 4181 const OopMapBlock* const last_map = first_map + map_count - 1; 4182 const int next_offset = last_map->offset() + (last_map->count() * heapOopSize); 4183 if (next_offset == next_nonstatic_field_offset) { 4184 allocation_style = 0; // allocate oops first 4185 next_nonstatic_oop_offset = next_nonstatic_field_offset; 4186 next_nonstatic_double_offset = next_nonstatic_oop_offset + 4187 (nonstatic_oop_count * heapOopSize); 4188 } 4189 } 4190 if( allocation_style == 2 ) { 4191 allocation_style = 1; // allocate oops last 4192 next_nonstatic_double_offset = next_nonstatic_field_offset; 4193 } 4194 } else { 4195 ShouldNotReachHere(); 4196 } 4197 4198 int nonstatic_oop_space_count = 0; 4199 int nonstatic_word_space_count = 0; 4200 int nonstatic_short_space_count = 0; 4201 int nonstatic_byte_space_count = 0; 4202 int nonstatic_oop_space_offset = 0; 4203 int nonstatic_word_space_offset = 0; 4204 int nonstatic_short_space_offset = 0; 4205 int nonstatic_byte_space_offset = 0; 4206 4207 // Try to squeeze some of the fields into the gaps due to 4208 // long/double alignment. 4209 if (nonstatic_double_count > 0) { 4210 int offset = next_nonstatic_double_offset; 4211 next_nonstatic_double_offset = align_up(offset, BytesPerLong); 4212 if (compact_fields && offset != next_nonstatic_double_offset) { 4213 // Allocate available fields into the gap before double field. 4214 int length = next_nonstatic_double_offset - offset; 4215 assert(length == BytesPerInt, ""); 4216 nonstatic_word_space_offset = offset; 4217 if (nonstatic_word_count > 0) { 4218 nonstatic_word_count -= 1; 4219 nonstatic_word_space_count = 1; // Only one will fit 4220 length -= BytesPerInt; 4221 offset += BytesPerInt; 4222 } 4223 nonstatic_short_space_offset = offset; 4224 while (length >= BytesPerShort && nonstatic_short_count > 0) { 4225 nonstatic_short_count -= 1; 4226 nonstatic_short_space_count += 1; 4227 length -= BytesPerShort; 4228 offset += BytesPerShort; 4229 } 4230 nonstatic_byte_space_offset = offset; 4231 while (length > 0 && nonstatic_byte_count > 0) { 4232 nonstatic_byte_count -= 1; 4233 nonstatic_byte_space_count += 1; 4234 length -= 1; 4235 } 4236 // Allocate oop field in the gap if there are no other fields for that. 4237 nonstatic_oop_space_offset = offset; 4238 if (length >= heapOopSize && nonstatic_oop_count > 0 && 4239 allocation_style != 0) { // when oop fields not first 4240 nonstatic_oop_count -= 1; 4241 nonstatic_oop_space_count = 1; // Only one will fit 4242 length -= heapOopSize; 4243 offset += heapOopSize; 4244 } 4245 } 4246 } 4247 4248 int next_nonstatic_word_offset = next_nonstatic_double_offset + 4249 (nonstatic_double_count * BytesPerLong); 4250 int next_nonstatic_short_offset = next_nonstatic_word_offset + 4251 (nonstatic_word_count * BytesPerInt); 4252 int next_nonstatic_byte_offset = next_nonstatic_short_offset + 4253 (nonstatic_short_count * BytesPerShort); 4254 int next_nonstatic_padded_offset = next_nonstatic_byte_offset + 4255 nonstatic_byte_count; 4256 4257 // let oops jump before padding with this allocation style 4258 if( allocation_style == 1 ) { 4259 next_nonstatic_oop_offset = next_nonstatic_padded_offset; 4260 if( nonstatic_oop_count > 0 ) { 4261 next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize); 4262 } 4263 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize); 4264 } 4265 4266 // Iterate over fields again and compute correct offsets. 4267 // The field allocation type was temporarily stored in the offset slot. 4268 // oop fields are located before non-oop fields (static and non-static). 4269 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4270 4271 // skip already laid out fields 4272 if (fs.is_offset_set()) continue; 4273 4274 // contended instance fields are handled below 4275 if (fs.is_contended() && !fs.access_flags().is_static()) continue; 4276 4277 int real_offset = 0; 4278 const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type(); 4279 4280 // pack the rest of the fields 4281 switch (atype) { 4282 case STATIC_OOP: 4283 real_offset = next_static_oop_offset; 4284 next_static_oop_offset += heapOopSize; 4285 break; 4286 case STATIC_BYTE: 4287 real_offset = next_static_byte_offset; 4288 next_static_byte_offset += 1; 4289 break; 4290 case STATIC_SHORT: 4291 real_offset = next_static_short_offset; 4292 next_static_short_offset += BytesPerShort; 4293 break; 4294 case STATIC_WORD: 4295 real_offset = next_static_word_offset; 4296 next_static_word_offset += BytesPerInt; 4297 break; 4298 case STATIC_DOUBLE: 4299 real_offset = next_static_double_offset; 4300 next_static_double_offset += BytesPerLong; 4301 break; 4302 case NONSTATIC_OOP: 4303 if( nonstatic_oop_space_count > 0 ) { 4304 real_offset = nonstatic_oop_space_offset; 4305 nonstatic_oop_space_offset += heapOopSize; 4306 nonstatic_oop_space_count -= 1; 4307 } else { 4308 real_offset = next_nonstatic_oop_offset; 4309 next_nonstatic_oop_offset += heapOopSize; 4310 } 4311 4312 // Record this oop in the oop maps 4313 if( nonstatic_oop_map_count > 0 && 4314 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == 4315 real_offset - 4316 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * 4317 heapOopSize ) { 4318 // This oop is adjacent to the previous one, add to current oop map 4319 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check"); 4320 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; 4321 } else { 4322 // This oop is not adjacent to the previous one, create new oop map 4323 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); 4324 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; 4325 nonstatic_oop_counts [nonstatic_oop_map_count] = 1; 4326 nonstatic_oop_map_count += 1; 4327 if( first_nonstatic_oop_offset == 0 ) { // Undefined 4328 first_nonstatic_oop_offset = real_offset; 4329 } 4330 } 4331 break; 4332 case NONSTATIC_BYTE: 4333 if( nonstatic_byte_space_count > 0 ) { 4334 real_offset = nonstatic_byte_space_offset; 4335 nonstatic_byte_space_offset += 1; 4336 nonstatic_byte_space_count -= 1; 4337 } else { 4338 real_offset = next_nonstatic_byte_offset; 4339 next_nonstatic_byte_offset += 1; 4340 } 4341 break; 4342 case NONSTATIC_SHORT: 4343 if( nonstatic_short_space_count > 0 ) { 4344 real_offset = nonstatic_short_space_offset; 4345 nonstatic_short_space_offset += BytesPerShort; 4346 nonstatic_short_space_count -= 1; 4347 } else { 4348 real_offset = next_nonstatic_short_offset; 4349 next_nonstatic_short_offset += BytesPerShort; 4350 } 4351 break; 4352 case NONSTATIC_WORD: 4353 if( nonstatic_word_space_count > 0 ) { 4354 real_offset = nonstatic_word_space_offset; 4355 nonstatic_word_space_offset += BytesPerInt; 4356 nonstatic_word_space_count -= 1; 4357 } else { 4358 real_offset = next_nonstatic_word_offset; 4359 next_nonstatic_word_offset += BytesPerInt; 4360 } 4361 break; 4362 case NONSTATIC_DOUBLE: 4363 real_offset = next_nonstatic_double_offset; 4364 next_nonstatic_double_offset += BytesPerLong; 4365 break; 4366 default: 4367 ShouldNotReachHere(); 4368 } 4369 fs.set_offset(real_offset); 4370 } 4371 4372 4373 // Handle the contended cases. 4374 // 4375 // Each contended field should not intersect the cache line with another contended field. 4376 // In the absence of alignment information, we end up with pessimistically separating 4377 // the fields with full-width padding. 4378 // 4379 // Additionally, this should not break alignment for the fields, so we round the alignment up 4380 // for each field. 4381 if (nonstatic_contended_count > 0) { 4382 4383 // if there is at least one contended field, we need to have pre-padding for them 4384 next_nonstatic_padded_offset += ContendedPaddingWidth; 4385 4386 // collect all contended groups 4387 ResourceBitMap bm(cp->size()); 4388 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4389 // skip already laid out fields 4390 if (fs.is_offset_set()) continue; 4391 4392 if (fs.is_contended()) { 4393 bm.set_bit(fs.contended_group()); 4394 } 4395 } 4396 4397 int current_group = -1; 4398 while ((current_group = (int)bm.get_next_one_offset(current_group + 1)) != (int)bm.size()) { 4399 4400 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) { 4401 4402 // skip already laid out fields 4403 if (fs.is_offset_set()) continue; 4404 4405 // skip non-contended fields and fields from different group 4406 if (!fs.is_contended() || (fs.contended_group() != current_group)) continue; 4407 4408 // handle statics below 4409 if (fs.access_flags().is_static()) continue; 4410 4411 int real_offset = 0; 4412 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type(); 4413 4414 switch (atype) { 4415 case NONSTATIC_BYTE: 4416 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, 1); 4417 real_offset = next_nonstatic_padded_offset; 4418 next_nonstatic_padded_offset += 1; 4419 break; 4420 4421 case NONSTATIC_SHORT: 4422 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort); 4423 real_offset = next_nonstatic_padded_offset; 4424 next_nonstatic_padded_offset += BytesPerShort; 4425 break; 4426 4427 case NONSTATIC_WORD: 4428 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt); 4429 real_offset = next_nonstatic_padded_offset; 4430 next_nonstatic_padded_offset += BytesPerInt; 4431 break; 4432 4433 case NONSTATIC_DOUBLE: 4434 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong); 4435 real_offset = next_nonstatic_padded_offset; 4436 next_nonstatic_padded_offset += BytesPerLong; 4437 break; 4438 4439 case NONSTATIC_OOP: 4440 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize); 4441 real_offset = next_nonstatic_padded_offset; 4442 next_nonstatic_padded_offset += heapOopSize; 4443 4444 // Record this oop in the oop maps 4445 if( nonstatic_oop_map_count > 0 && 4446 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] == 4447 real_offset - 4448 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) * 4449 heapOopSize ) { 4450 // This oop is adjacent to the previous one, add to current oop map 4451 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check"); 4452 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1; 4453 } else { 4454 // This oop is not adjacent to the previous one, create new oop map 4455 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check"); 4456 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset; 4457 nonstatic_oop_counts [nonstatic_oop_map_count] = 1; 4458 nonstatic_oop_map_count += 1; 4459 if( first_nonstatic_oop_offset == 0 ) { // Undefined 4460 first_nonstatic_oop_offset = real_offset; 4461 } 4462 } 4463 break; 4464 4465 default: 4466 ShouldNotReachHere(); 4467 } 4468 4469 if (fs.contended_group() == 0) { 4470 // Contended group defines the equivalence class over the fields: 4471 // the fields within the same contended group are not inter-padded. 4472 // The only exception is default group, which does not incur the 4473 // equivalence, and so requires intra-padding. 4474 next_nonstatic_padded_offset += ContendedPaddingWidth; 4475 } 4476 4477 fs.set_offset(real_offset); 4478 } // for 4479 4480 // Start laying out the next group. 4481 // Note that this will effectively pad the last group in the back; 4482 // this is expected to alleviate memory contention effects for 4483 // subclass fields and/or adjacent object. 4484 // If this was the default group, the padding is already in place. 4485 if (current_group != 0) { 4486 next_nonstatic_padded_offset += ContendedPaddingWidth; 4487 } 4488 } 4489 4490 // handle static fields 4491 } 4492 4493 // Entire class is contended, pad in the back. 4494 // This helps to alleviate memory contention effects for subclass fields 4495 // and/or adjacent object. 4496 if (is_contended_class) { 4497 next_nonstatic_padded_offset += ContendedPaddingWidth; 4498 } 4499 4500 int notaligned_nonstatic_fields_end = next_nonstatic_padded_offset; 4501 4502 int nonstatic_fields_end = align_up(notaligned_nonstatic_fields_end, heapOopSize); 4503 int instance_end = align_up(notaligned_nonstatic_fields_end, wordSize); 4504 int static_fields_end = align_up(next_static_byte_offset, wordSize); 4505 4506 int static_field_size = (static_fields_end - 4507 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 4508 nonstatic_field_size = nonstatic_field_size + 4509 (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize; 4510 4511 int instance_size = align_object_size(instance_end / wordSize); 4512 4513 assert(instance_size == align_object_size(align_up( 4514 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), 4515 wordSize) / wordSize), "consistent layout helper value"); 4516 4517 // Invariant: nonstatic_field end/start should only change if there are 4518 // nonstatic fields in the class, or if the class is contended. We compare 4519 // against the non-aligned value, so that end alignment will not fail the 4520 // assert without actually having the fields. 4521 assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) || 4522 is_contended_class || 4523 (nonstatic_fields_count > 0), "double-check nonstatic start/end"); 4524 4525 // Number of non-static oop map blocks allocated at end of klass. 4526 const unsigned int total_oop_map_count = 4527 compute_oop_map_count(_super_klass, nonstatic_oop_map_count, 4528 first_nonstatic_oop_offset); 4529 4530 #ifndef PRODUCT 4531 if (PrintFieldLayout) { 4532 print_field_layout(_class_name, 4533 _fields, 4534 cp, 4535 instance_size, 4536 nonstatic_fields_start, 4537 nonstatic_fields_end, 4538 static_fields_end); 4539 } 4540 4541 #endif 4542 // Pass back information needed for InstanceKlass creation 4543 info->nonstatic_oop_offsets = nonstatic_oop_offsets; 4544 info->nonstatic_oop_counts = nonstatic_oop_counts; 4545 info->nonstatic_oop_map_count = nonstatic_oop_map_count; 4546 info->total_oop_map_count = total_oop_map_count; 4547 info->instance_size = instance_size; 4548 info->static_field_size = static_field_size; 4549 info->nonstatic_field_size = nonstatic_field_size; 4550 info->has_nonstatic_fields = has_nonstatic_fields; 4551 } 4552 4553 static void fill_oop_maps(const InstanceKlass* k, 4554 unsigned int nonstatic_oop_map_count, 4555 const int* nonstatic_oop_offsets, 4556 const unsigned int* nonstatic_oop_counts) { 4557 4558 assert(k != NULL, "invariant"); 4559 4560 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps(); 4561 const InstanceKlass* const super = k->superklass(); 4562 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0; 4563 if (super_count > 0) { 4564 // Copy maps from superklass 4565 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps(); 4566 for (unsigned int i = 0; i < super_count; ++i) { 4567 *this_oop_map++ = *super_oop_map++; 4568 } 4569 } 4570 4571 if (nonstatic_oop_map_count > 0) { 4572 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) { 4573 // The counts differ because there is no gap between superklass's last oop 4574 // field and the first local oop field. Extend the last oop map copied 4575 // from the superklass instead of creating new one. 4576 nonstatic_oop_map_count--; 4577 nonstatic_oop_offsets++; 4578 this_oop_map--; 4579 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++); 4580 this_oop_map++; 4581 } 4582 4583 // Add new map blocks, fill them 4584 while (nonstatic_oop_map_count-- > 0) { 4585 this_oop_map->set_offset(*nonstatic_oop_offsets++); 4586 this_oop_map->set_count(*nonstatic_oop_counts++); 4587 this_oop_map++; 4588 } 4589 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() == 4590 this_oop_map, "sanity"); 4591 } 4592 } 4593 4594 4595 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) { 4596 assert(ik != NULL, "invariant"); 4597 4598 const Klass* const super = ik->super(); 4599 4600 // Check if this klass has an empty finalize method (i.e. one with return bytecode only), 4601 // in which case we don't have to register objects as finalizable 4602 if (!_has_empty_finalizer) { 4603 if (_has_finalizer || 4604 (super != NULL && super->has_finalizer())) { 4605 ik->set_has_finalizer(); 4606 } 4607 } 4608 4609 #ifdef ASSERT 4610 bool f = false; 4611 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(), 4612 vmSymbols::void_method_signature()); 4613 if (m != NULL && !m->is_empty_method()) { 4614 f = true; 4615 } 4616 4617 // Spec doesn't prevent agent from redefinition of empty finalizer. 4618 // Despite the fact that it's generally bad idea and redefined finalizer 4619 // will not work as expected we shouldn't abort vm in this case 4620 if (!ik->has_redefined_this_or_super()) { 4621 assert(ik->has_finalizer() == f, "inconsistent has_finalizer"); 4622 } 4623 #endif 4624 4625 // Check if this klass supports the java.lang.Cloneable interface 4626 if (SystemDictionary::Cloneable_klass_loaded()) { 4627 if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) { 4628 ik->set_is_cloneable(); 4629 } 4630 } 4631 4632 // Check if this klass has a vanilla default constructor 4633 if (super == NULL) { 4634 // java.lang.Object has empty default constructor 4635 ik->set_has_vanilla_constructor(); 4636 } else { 4637 if (super->has_vanilla_constructor() && 4638 _has_vanilla_constructor) { 4639 ik->set_has_vanilla_constructor(); 4640 } 4641 #ifdef ASSERT 4642 bool v = false; 4643 if (super->has_vanilla_constructor()) { 4644 const Method* const constructor = 4645 ik->find_method(vmSymbols::object_initializer_name(), 4646 vmSymbols::void_method_signature()); 4647 if (constructor != NULL && constructor->is_vanilla_constructor()) { 4648 v = true; 4649 } 4650 } 4651 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor"); 4652 #endif 4653 } 4654 4655 // If it cannot be fast-path allocated, set a bit in the layout helper. 4656 // See documentation of InstanceKlass::can_be_fastpath_allocated(). 4657 assert(ik->size_helper() > 0, "layout_helper is initialized"); 4658 if ((!RegisterFinalizersAtInit && ik->has_finalizer()) 4659 || ik->is_abstract() || ik->is_interface() 4660 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL) 4661 || ik->size_helper() >= FastAllocateSizeLimit) { 4662 // Forbid fast-path allocation. 4663 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true); 4664 ik->set_layout_helper(lh); 4665 } 4666 } 4667 4668 // utility methods for appending an array with check for duplicates 4669 4670 static void append_interfaces(GrowableArray<InstanceKlass*>* result, 4671 const Array<InstanceKlass*>* const ifs) { 4672 // iterate over new interfaces 4673 for (int i = 0; i < ifs->length(); i++) { 4674 InstanceKlass* const e = ifs->at(i); 4675 assert(e->is_klass() && e->is_interface(), "just checking"); 4676 // add new interface 4677 result->append_if_missing(e); 4678 } 4679 } 4680 4681 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super, 4682 Array<InstanceKlass*>* local_ifs, 4683 ClassLoaderData* loader_data, 4684 TRAPS) { 4685 assert(local_ifs != NULL, "invariant"); 4686 assert(loader_data != NULL, "invariant"); 4687 4688 // Compute maximum size for transitive interfaces 4689 int max_transitive_size = 0; 4690 int super_size = 0; 4691 // Add superclass transitive interfaces size 4692 if (super != NULL) { 4693 super_size = super->transitive_interfaces()->length(); 4694 max_transitive_size += super_size; 4695 } 4696 // Add local interfaces' super interfaces 4697 const int local_size = local_ifs->length(); 4698 for (int i = 0; i < local_size; i++) { 4699 InstanceKlass* const l = local_ifs->at(i); 4700 max_transitive_size += l->transitive_interfaces()->length(); 4701 } 4702 // Finally add local interfaces 4703 max_transitive_size += local_size; 4704 // Construct array 4705 if (max_transitive_size == 0) { 4706 // no interfaces, use canonicalized array 4707 return Universe::the_empty_instance_klass_array(); 4708 } else if (max_transitive_size == super_size) { 4709 // no new local interfaces added, share superklass' transitive interface array 4710 return super->transitive_interfaces(); 4711 } else if (max_transitive_size == local_size) { 4712 // only local interfaces added, share local interface array 4713 return local_ifs; 4714 } else { 4715 ResourceMark rm; 4716 GrowableArray<InstanceKlass*>* const result = new GrowableArray<InstanceKlass*>(max_transitive_size); 4717 4718 // Copy down from superclass 4719 if (super != NULL) { 4720 append_interfaces(result, super->transitive_interfaces()); 4721 } 4722 4723 // Copy down from local interfaces' superinterfaces 4724 for (int i = 0; i < local_size; i++) { 4725 InstanceKlass* const l = local_ifs->at(i); 4726 append_interfaces(result, l->transitive_interfaces()); 4727 } 4728 // Finally add local interfaces 4729 append_interfaces(result, local_ifs); 4730 4731 // length will be less than the max_transitive_size if duplicates were removed 4732 const int length = result->length(); 4733 assert(length <= max_transitive_size, "just checking"); 4734 Array<InstanceKlass*>* const new_result = 4735 MetadataFactory::new_array<InstanceKlass*>(loader_data, length, CHECK_NULL); 4736 for (int i = 0; i < length; i++) { 4737 InstanceKlass* const e = result->at(i); 4738 assert(e != NULL, "just checking"); 4739 new_result->at_put(i, e); 4740 } 4741 return new_result; 4742 } 4743 } 4744 4745 static void check_super_class_access(const InstanceKlass* this_klass, TRAPS) { 4746 assert(this_klass != NULL, "invariant"); 4747 const Klass* const super = this_klass->super(); 4748 4749 if (super != NULL) { 4750 4751 // If the loader is not the boot loader then throw an exception if its 4752 // superclass is in package jdk.internal.reflect and its loader is not a 4753 // special reflection class loader 4754 if (!this_klass->class_loader_data()->is_the_null_class_loader_data()) { 4755 assert(super->is_instance_klass(), "super is not instance klass"); 4756 PackageEntry* super_package = super->package(); 4757 if (super_package != NULL && 4758 super_package->name()->fast_compare(vmSymbols::jdk_internal_reflect()) == 0 && 4759 !java_lang_ClassLoader::is_reflection_class_loader(this_klass->class_loader())) { 4760 ResourceMark rm(THREAD); 4761 Exceptions::fthrow( 4762 THREAD_AND_LOCATION, 4763 vmSymbols::java_lang_IllegalAccessError(), 4764 "class %s loaded by %s cannot access jdk/internal/reflect superclass %s", 4765 this_klass->external_name(), 4766 this_klass->class_loader_data()->loader_name_and_id(), 4767 super->external_name()); 4768 return; 4769 } 4770 } 4771 4772 Reflection::VerifyClassAccessResults vca_result = 4773 Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false); 4774 if (vca_result != Reflection::ACCESS_OK) { 4775 ResourceMark rm(THREAD); 4776 char* msg = Reflection::verify_class_access_msg(this_klass, 4777 InstanceKlass::cast(super), 4778 vca_result); 4779 if (msg == NULL) { 4780 bool same_module = (this_klass->module() == super->module()); 4781 Exceptions::fthrow( 4782 THREAD_AND_LOCATION, 4783 vmSymbols::java_lang_IllegalAccessError(), 4784 "class %s cannot access its %ssuperclass %s (%s%s%s)", 4785 this_klass->external_name(), 4786 super->is_abstract() ? "abstract " : "", 4787 super->external_name(), 4788 (same_module) ? this_klass->joint_in_module_of_loader(super) : this_klass->class_in_module_of_loader(), 4789 (same_module) ? "" : "; ", 4790 (same_module) ? "" : super->class_in_module_of_loader()); 4791 } else { 4792 // Add additional message content. 4793 Exceptions::fthrow( 4794 THREAD_AND_LOCATION, 4795 vmSymbols::java_lang_IllegalAccessError(), 4796 "superclass access check failed: %s", 4797 msg); 4798 } 4799 } 4800 } 4801 } 4802 4803 4804 static void check_super_interface_access(const InstanceKlass* this_klass, TRAPS) { 4805 assert(this_klass != NULL, "invariant"); 4806 const Array<InstanceKlass*>* const local_interfaces = this_klass->local_interfaces(); 4807 const int lng = local_interfaces->length(); 4808 for (int i = lng - 1; i >= 0; i--) { 4809 InstanceKlass* const k = local_interfaces->at(i); 4810 assert (k != NULL && k->is_interface(), "invalid interface"); 4811 Reflection::VerifyClassAccessResults vca_result = 4812 Reflection::verify_class_access(this_klass, k, false); 4813 if (vca_result != Reflection::ACCESS_OK) { 4814 ResourceMark rm(THREAD); 4815 char* msg = Reflection::verify_class_access_msg(this_klass, 4816 k, 4817 vca_result); 4818 if (msg == NULL) { 4819 bool same_module = (this_klass->module() == k->module()); 4820 Exceptions::fthrow( 4821 THREAD_AND_LOCATION, 4822 vmSymbols::java_lang_IllegalAccessError(), 4823 "class %s cannot access its superinterface %s (%s%s%s)", 4824 this_klass->external_name(), 4825 k->external_name(), 4826 (same_module) ? this_klass->joint_in_module_of_loader(k) : this_klass->class_in_module_of_loader(), 4827 (same_module) ? "" : "; ", 4828 (same_module) ? "" : k->class_in_module_of_loader()); 4829 } else { 4830 // Add additional message content. 4831 Exceptions::fthrow( 4832 THREAD_AND_LOCATION, 4833 vmSymbols::java_lang_IllegalAccessError(), 4834 "superinterface check failed: %s", 4835 msg); 4836 } 4837 } 4838 } 4839 } 4840 4841 4842 static void check_final_method_override(const InstanceKlass* this_klass, TRAPS) { 4843 assert(this_klass != NULL, "invariant"); 4844 const Array<Method*>* const methods = this_klass->methods(); 4845 const int num_methods = methods->length(); 4846 4847 // go thru each method and check if it overrides a final method 4848 for (int index = 0; index < num_methods; index++) { 4849 const Method* const m = methods->at(index); 4850 4851 // skip private, static, and <init> methods 4852 if ((!m->is_private() && !m->is_static()) && 4853 (m->name() != vmSymbols::object_initializer_name())) { 4854 4855 const Symbol* const name = m->name(); 4856 const Symbol* const signature = m->signature(); 4857 const Klass* k = this_klass->super(); 4858 const Method* super_m = NULL; 4859 while (k != NULL) { 4860 // skip supers that don't have final methods. 4861 if (k->has_final_method()) { 4862 // lookup a matching method in the super class hierarchy 4863 super_m = InstanceKlass::cast(k)->lookup_method(name, signature); 4864 if (super_m == NULL) { 4865 break; // didn't find any match; get out 4866 } 4867 4868 if (super_m->is_final() && !super_m->is_static() && 4869 !super_m->access_flags().is_private()) { 4870 // matching method in super is final, and not static or private 4871 bool can_access = Reflection::verify_member_access(this_klass, 4872 super_m->method_holder(), 4873 super_m->method_holder(), 4874 super_m->access_flags(), 4875 false, false, CHECK); 4876 if (can_access) { 4877 // this class can access super final method and therefore override 4878 ResourceMark rm(THREAD); 4879 Exceptions::fthrow(THREAD_AND_LOCATION, 4880 vmSymbols::java_lang_VerifyError(), 4881 "class %s overrides final method %s.%s%s", 4882 this_klass->external_name(), 4883 super_m->method_holder()->external_name(), 4884 name->as_C_string(), 4885 signature->as_C_string() 4886 ); 4887 return; 4888 } 4889 } 4890 4891 // continue to look from super_m's holder's super. 4892 k = super_m->method_holder()->super(); 4893 continue; 4894 } 4895 4896 k = k->super(); 4897 } 4898 } 4899 } 4900 } 4901 4902 4903 // assumes that this_klass is an interface 4904 static void check_illegal_static_method(const InstanceKlass* this_klass, TRAPS) { 4905 assert(this_klass != NULL, "invariant"); 4906 assert(this_klass->is_interface(), "not an interface"); 4907 const Array<Method*>* methods = this_klass->methods(); 4908 const int num_methods = methods->length(); 4909 4910 for (int index = 0; index < num_methods; index++) { 4911 const Method* const m = methods->at(index); 4912 // if m is static and not the init method, throw a verify error 4913 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) { 4914 ResourceMark rm(THREAD); 4915 Exceptions::fthrow( 4916 THREAD_AND_LOCATION, 4917 vmSymbols::java_lang_VerifyError(), 4918 "Illegal static method %s in interface %s", 4919 m->name()->as_C_string(), 4920 this_klass->external_name() 4921 ); 4922 return; 4923 } 4924 } 4925 } 4926 4927 // utility methods for format checking 4928 4929 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const { 4930 const bool is_module = (flags & JVM_ACC_MODULE) != 0; 4931 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set"); 4932 if (is_module) { 4933 ResourceMark rm(THREAD); 4934 Exceptions::fthrow( 4935 THREAD_AND_LOCATION, 4936 vmSymbols::java_lang_NoClassDefFoundError(), 4937 "%s is not a class because access_flag ACC_MODULE is set", 4938 _class_name->as_C_string()); 4939 return; 4940 } 4941 4942 if (!_need_verify) { return; } 4943 4944 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0; 4945 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 4946 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 4947 const bool is_super = (flags & JVM_ACC_SUPER) != 0; 4948 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 4949 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0; 4950 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION; 4951 const bool major_gte_14 = _major_version >= JAVA_14_VERSION; 4952 4953 if ((is_abstract && is_final) || 4954 (is_interface && !is_abstract) || 4955 (is_interface && major_gte_1_5 && (is_super || is_enum)) || 4956 (!is_interface && major_gte_1_5 && is_annotation)) { 4957 ResourceMark rm(THREAD); 4958 Exceptions::fthrow( 4959 THREAD_AND_LOCATION, 4960 vmSymbols::java_lang_ClassFormatError(), 4961 "Illegal class modifiers in class %s: 0x%X", 4962 _class_name->as_C_string(), flags 4963 ); 4964 return; 4965 } 4966 } 4967 4968 static bool has_illegal_visibility(jint flags) { 4969 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 4970 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 4971 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 4972 4973 return ((is_public && is_protected) || 4974 (is_public && is_private) || 4975 (is_protected && is_private)); 4976 } 4977 4978 // A legal major_version.minor_version must be one of the following: 4979 // 4980 // Major_version >= 45 and major_version < 56, any minor_version. 4981 // Major_version >= 56 and major_version <= JVM_CLASSFILE_MAJOR_VERSION and minor_version = 0. 4982 // Major_version = JVM_CLASSFILE_MAJOR_VERSION and minor_version = 65535 and --enable-preview is present. 4983 // 4984 static void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS){ 4985 ResourceMark rm(THREAD); 4986 const u2 max_version = JVM_CLASSFILE_MAJOR_VERSION; 4987 if (major < JAVA_MIN_SUPPORTED_VERSION) { 4988 Exceptions::fthrow( 4989 THREAD_AND_LOCATION, 4990 vmSymbols::java_lang_UnsupportedClassVersionError(), 4991 "%s (class file version %u.%u) was compiled with an invalid major version", 4992 class_name->as_C_string(), major, minor); 4993 return; 4994 } 4995 4996 if (major > max_version) { 4997 Exceptions::fthrow( 4998 THREAD_AND_LOCATION, 4999 vmSymbols::java_lang_UnsupportedClassVersionError(), 5000 "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), " 5001 "this version of the Java Runtime only recognizes class file versions up to %u.0", 5002 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION); 5003 return; 5004 } 5005 5006 if (major < JAVA_12_VERSION || minor == 0) { 5007 return; 5008 } 5009 5010 if (minor == JAVA_PREVIEW_MINOR_VERSION) { 5011 if (major != max_version) { 5012 Exceptions::fthrow( 5013 THREAD_AND_LOCATION, 5014 vmSymbols::java_lang_UnsupportedClassVersionError(), 5015 "%s (class file version %u.%u) was compiled with preview features that are unsupported. " 5016 "This version of the Java Runtime only recognizes preview features for class file version %u.%u", 5017 class_name->as_C_string(), major, minor, JVM_CLASSFILE_MAJOR_VERSION, JAVA_PREVIEW_MINOR_VERSION); 5018 return; 5019 } 5020 5021 if (!Arguments::enable_preview()) { 5022 Exceptions::fthrow( 5023 THREAD_AND_LOCATION, 5024 vmSymbols::java_lang_UnsupportedClassVersionError(), 5025 "Preview features are not enabled for %s (class file version %u.%u). Try running with '--enable-preview'", 5026 class_name->as_C_string(), major, minor); 5027 return; 5028 } 5029 5030 } else { // minor != JAVA_PREVIEW_MINOR_VERSION 5031 Exceptions::fthrow( 5032 THREAD_AND_LOCATION, 5033 vmSymbols::java_lang_UnsupportedClassVersionError(), 5034 "%s (class file version %u.%u) was compiled with an invalid non-zero minor version", 5035 class_name->as_C_string(), major, minor); 5036 } 5037 } 5038 5039 void ClassFileParser::verify_legal_field_modifiers(jint flags, 5040 bool is_interface, 5041 TRAPS) const { 5042 if (!_need_verify) { return; } 5043 5044 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 5045 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 5046 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 5047 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 5048 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 5049 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0; 5050 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0; 5051 const bool is_enum = (flags & JVM_ACC_ENUM) != 0; 5052 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION; 5053 5054 bool is_illegal = false; 5055 5056 if (is_interface) { 5057 if (!is_public || !is_static || !is_final || is_private || 5058 is_protected || is_volatile || is_transient || 5059 (major_gte_1_5 && is_enum)) { 5060 is_illegal = true; 5061 } 5062 } else { // not interface 5063 if (has_illegal_visibility(flags) || (is_final && is_volatile)) { 5064 is_illegal = true; 5065 } 5066 } 5067 5068 if (is_illegal) { 5069 ResourceMark rm(THREAD); 5070 Exceptions::fthrow( 5071 THREAD_AND_LOCATION, 5072 vmSymbols::java_lang_ClassFormatError(), 5073 "Illegal field modifiers in class %s: 0x%X", 5074 _class_name->as_C_string(), flags); 5075 return; 5076 } 5077 } 5078 5079 void ClassFileParser::verify_legal_method_modifiers(jint flags, 5080 bool is_interface, 5081 const Symbol* name, 5082 TRAPS) const { 5083 if (!_need_verify) { return; } 5084 5085 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0; 5086 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0; 5087 const bool is_static = (flags & JVM_ACC_STATIC) != 0; 5088 const bool is_final = (flags & JVM_ACC_FINAL) != 0; 5089 const bool is_native = (flags & JVM_ACC_NATIVE) != 0; 5090 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0; 5091 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0; 5092 const bool is_strict = (flags & JVM_ACC_STRICT) != 0; 5093 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0; 5094 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0; 5095 const bool major_gte_1_5 = _major_version >= JAVA_1_5_VERSION; 5096 const bool major_gte_8 = _major_version >= JAVA_8_VERSION; 5097 const bool is_initializer = (name == vmSymbols::object_initializer_name()); 5098 5099 bool is_illegal = false; 5100 5101 if (is_interface) { 5102 if (major_gte_8) { 5103 // Class file version is JAVA_8_VERSION or later Methods of 5104 // interfaces may set any of the flags except ACC_PROTECTED, 5105 // ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED; they must 5106 // have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. 5107 if ((is_public == is_private) || /* Only one of private and public should be true - XNOR */ 5108 (is_native || is_protected || is_final || is_synchronized) || 5109 // If a specific method of a class or interface has its 5110 // ACC_ABSTRACT flag set, it must not have any of its 5111 // ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, 5112 // ACC_STRICT, or ACC_SYNCHRONIZED flags set. No need to 5113 // check for ACC_FINAL, ACC_NATIVE or ACC_SYNCHRONIZED as 5114 // those flags are illegal irrespective of ACC_ABSTRACT being set or not. 5115 (is_abstract && (is_private || is_static || is_strict))) { 5116 is_illegal = true; 5117 } 5118 } else if (major_gte_1_5) { 5119 // Class file version in the interval [JAVA_1_5_VERSION, JAVA_8_VERSION) 5120 if (!is_public || is_private || is_protected || is_static || is_final || 5121 is_synchronized || is_native || !is_abstract || is_strict) { 5122 is_illegal = true; 5123 } 5124 } else { 5125 // Class file version is pre-JAVA_1_5_VERSION 5126 if (!is_public || is_static || is_final || is_native || !is_abstract) { 5127 is_illegal = true; 5128 } 5129 } 5130 } else { // not interface 5131 if (has_illegal_visibility(flags)) { 5132 is_illegal = true; 5133 } else { 5134 if (is_initializer) { 5135 if (is_static || is_final || is_synchronized || is_native || 5136 is_abstract || (major_gte_1_5 && is_bridge)) { 5137 is_illegal = true; 5138 } 5139 } else { // not initializer 5140 if (is_abstract) { 5141 if ((is_final || is_native || is_private || is_static || 5142 (major_gte_1_5 && (is_synchronized || is_strict)))) { 5143 is_illegal = true; 5144 } 5145 } 5146 } 5147 } 5148 } 5149 5150 if (is_illegal) { 5151 ResourceMark rm(THREAD); 5152 Exceptions::fthrow( 5153 THREAD_AND_LOCATION, 5154 vmSymbols::java_lang_ClassFormatError(), 5155 "Method %s in class %s has illegal modifiers: 0x%X", 5156 name->as_C_string(), _class_name->as_C_string(), flags); 5157 return; 5158 } 5159 } 5160 5161 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, 5162 int length, 5163 TRAPS) const { 5164 assert(_need_verify, "only called when _need_verify is true"); 5165 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) { 5166 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK); 5167 } 5168 } 5169 5170 // Unqualified names may not contain the characters '.', ';', '[', or '/'. 5171 // In class names, '/' separates unqualified names. This is verified in this function also. 5172 // Method names also may not contain the characters '<' or '>', unless <init> 5173 // or <clinit>. Note that method names may not be <init> or <clinit> in this 5174 // method. Because these names have been checked as special cases before 5175 // calling this method in verify_legal_method_name. 5176 // 5177 // This method is also called from the modular system APIs in modules.cpp 5178 // to verify the validity of module and package names. 5179 bool ClassFileParser::verify_unqualified_name(const char* name, 5180 unsigned int length, 5181 int type) { 5182 if (length == 0) return false; // Must have at least one char. 5183 for (const char* p = name; p != name + length; p++) { 5184 switch(*p) { 5185 case JVM_SIGNATURE_DOT: 5186 case JVM_SIGNATURE_ENDCLASS: 5187 case JVM_SIGNATURE_ARRAY: 5188 // do not permit '.', ';', or '[' 5189 return false; 5190 case JVM_SIGNATURE_SLASH: 5191 // check for '//' or leading or trailing '/' which are not legal 5192 // unqualified name must not be empty 5193 if (type == ClassFileParser::LegalClass) { 5194 if (p == name || p+1 >= name+length || 5195 *(p+1) == JVM_SIGNATURE_SLASH) { 5196 return false; 5197 } 5198 } else { 5199 return false; // do not permit '/' unless it's class name 5200 } 5201 break; 5202 case JVM_SIGNATURE_SPECIAL: 5203 case JVM_SIGNATURE_ENDSPECIAL: 5204 // do not permit '<' or '>' in method names 5205 if (type == ClassFileParser::LegalMethod) { 5206 return false; 5207 } 5208 } 5209 } 5210 return true; 5211 } 5212 5213 // Take pointer to a UTF8 byte string (not NUL-terminated). 5214 // Skip over the longest part of the string that could 5215 // be taken as a fieldname. Allow '/' if slash_ok is true. 5216 // Return a pointer to just past the fieldname. 5217 // Return NULL if no fieldname at all was found, or in the case of slash_ok 5218 // being true, we saw consecutive slashes (meaning we were looking for a 5219 // qualified path but found something that was badly-formed). 5220 static const char* skip_over_field_name(const char* const name, 5221 bool slash_ok, 5222 unsigned int length) { 5223 const char* p; 5224 jboolean last_is_slash = false; 5225 jboolean not_first_ch = false; 5226 5227 for (p = name; p != name + length; not_first_ch = true) { 5228 const char* old_p = p; 5229 jchar ch = *p; 5230 if (ch < 128) { 5231 p++; 5232 // quick check for ascii 5233 if ((ch >= 'a' && ch <= 'z') || 5234 (ch >= 'A' && ch <= 'Z') || 5235 (ch == '_' || ch == '$') || 5236 (not_first_ch && ch >= '0' && ch <= '9')) { 5237 last_is_slash = false; 5238 continue; 5239 } 5240 if (slash_ok && ch == JVM_SIGNATURE_SLASH) { 5241 if (last_is_slash) { 5242 return NULL; // Don't permit consecutive slashes 5243 } 5244 last_is_slash = true; 5245 continue; 5246 } 5247 } 5248 else { 5249 jint unicode_ch; 5250 char* tmp_p = UTF8::next_character(p, &unicode_ch); 5251 p = tmp_p; 5252 last_is_slash = false; 5253 // Check if ch is Java identifier start or is Java identifier part 5254 // 4672820: call java.lang.Character methods directly without generating separate tables. 5255 EXCEPTION_MARK; 5256 // return value 5257 JavaValue result(T_BOOLEAN); 5258 // Set up the arguments to isJavaIdentifierStart or isJavaIdentifierPart 5259 JavaCallArguments args; 5260 args.push_int(unicode_ch); 5261 5262 if (not_first_ch) { 5263 // public static boolean isJavaIdentifierPart(char ch); 5264 JavaCalls::call_static(&result, 5265 SystemDictionary::Character_klass(), 5266 vmSymbols::isJavaIdentifierPart_name(), 5267 vmSymbols::int_bool_signature(), 5268 &args, 5269 THREAD); 5270 } else { 5271 // public static boolean isJavaIdentifierStart(char ch); 5272 JavaCalls::call_static(&result, 5273 SystemDictionary::Character_klass(), 5274 vmSymbols::isJavaIdentifierStart_name(), 5275 vmSymbols::int_bool_signature(), 5276 &args, 5277 THREAD); 5278 } 5279 if (HAS_PENDING_EXCEPTION) { 5280 CLEAR_PENDING_EXCEPTION; 5281 return NULL; 5282 } 5283 if(result.get_jboolean()) { 5284 continue; 5285 } 5286 } 5287 return (not_first_ch) ? old_p : NULL; 5288 } 5289 return (not_first_ch) ? p : NULL; 5290 } 5291 5292 // Take pointer to a UTF8 byte string (not NUL-terminated). 5293 // Skip over the longest part of the string that could 5294 // be taken as a field signature. Allow "void" if void_ok. 5295 // Return a pointer to just past the signature. 5296 // Return NULL if no legal signature is found. 5297 const char* ClassFileParser::skip_over_field_signature(const char* signature, 5298 bool void_ok, 5299 unsigned int length, 5300 TRAPS) const { 5301 unsigned int array_dim = 0; 5302 while (length > 0) { 5303 switch (signature[0]) { 5304 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; } 5305 case JVM_SIGNATURE_BOOLEAN: 5306 case JVM_SIGNATURE_BYTE: 5307 case JVM_SIGNATURE_CHAR: 5308 case JVM_SIGNATURE_SHORT: 5309 case JVM_SIGNATURE_INT: 5310 case JVM_SIGNATURE_FLOAT: 5311 case JVM_SIGNATURE_LONG: 5312 case JVM_SIGNATURE_DOUBLE: 5313 return signature + 1; 5314 case JVM_SIGNATURE_CLASS: { 5315 if (_major_version < JAVA_1_5_VERSION) { 5316 // Skip over the class name if one is there 5317 const char* const p = skip_over_field_name(signature + 1, true, --length); 5318 5319 // The next character better be a semicolon 5320 if (p && (p - signature) > 1 && p[0] == JVM_SIGNATURE_ENDCLASS) { 5321 return p + 1; 5322 } 5323 } 5324 else { 5325 // Skip leading 'L' and ignore first appearance of ';' 5326 signature++; 5327 const char* c = (const char*) memchr(signature, JVM_SIGNATURE_ENDCLASS, length - 1); 5328 // Format check signature 5329 if (c != NULL) { 5330 int newlen = c - (char*) signature; 5331 bool legal = verify_unqualified_name(signature, newlen, LegalClass); 5332 if (!legal) { 5333 classfile_parse_error("Class name is empty or contains illegal character " 5334 "in descriptor in class file %s", 5335 CHECK_0); 5336 return NULL; 5337 } 5338 return signature + newlen + 1; 5339 } 5340 } 5341 return NULL; 5342 } 5343 case JVM_SIGNATURE_ARRAY: 5344 array_dim++; 5345 if (array_dim > 255) { 5346 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions. 5347 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0); 5348 } 5349 // The rest of what's there better be a legal signature 5350 signature++; 5351 length--; 5352 void_ok = false; 5353 break; 5354 default: 5355 return NULL; 5356 } 5357 } 5358 return NULL; 5359 } 5360 5361 // Checks if name is a legal class name. 5362 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const { 5363 if (!_need_verify || _relax_verify) { return; } 5364 5365 assert(name->refcount() > 0, "symbol must be kept alive"); 5366 char* bytes = (char*)name->bytes(); 5367 unsigned int length = name->utf8_length(); 5368 bool legal = false; 5369 5370 if (length > 0) { 5371 const char* p; 5372 if (bytes[0] == JVM_SIGNATURE_ARRAY) { 5373 p = skip_over_field_signature(bytes, false, length, CHECK); 5374 legal = (p != NULL) && ((p - bytes) == (int)length); 5375 } else if (_major_version < JAVA_1_5_VERSION) { 5376 if (bytes[0] != JVM_SIGNATURE_SPECIAL) { 5377 p = skip_over_field_name(bytes, true, length); 5378 legal = (p != NULL) && ((p - bytes) == (int)length); 5379 } 5380 } else { 5381 // 4900761: relax the constraints based on JSR202 spec 5382 // Class names may be drawn from the entire Unicode character set. 5383 // Identifiers between '/' must be unqualified names. 5384 // The utf8 string has been verified when parsing cpool entries. 5385 legal = verify_unqualified_name(bytes, length, LegalClass); 5386 } 5387 } 5388 if (!legal) { 5389 ResourceMark rm(THREAD); 5390 assert(_class_name != NULL, "invariant"); 5391 Exceptions::fthrow( 5392 THREAD_AND_LOCATION, 5393 vmSymbols::java_lang_ClassFormatError(), 5394 "Illegal class name \"%.*s\" in class file %s", length, bytes, 5395 _class_name->as_C_string() 5396 ); 5397 return; 5398 } 5399 } 5400 5401 // Checks if name is a legal field name. 5402 void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const { 5403 if (!_need_verify || _relax_verify) { return; } 5404 5405 char* bytes = (char*)name->bytes(); 5406 unsigned int length = name->utf8_length(); 5407 bool legal = false; 5408 5409 if (length > 0) { 5410 if (_major_version < JAVA_1_5_VERSION) { 5411 if (bytes[0] != JVM_SIGNATURE_SPECIAL) { 5412 const char* p = skip_over_field_name(bytes, false, length); 5413 legal = (p != NULL) && ((p - bytes) == (int)length); 5414 } 5415 } else { 5416 // 4881221: relax the constraints based on JSR202 spec 5417 legal = verify_unqualified_name(bytes, length, LegalField); 5418 } 5419 } 5420 5421 if (!legal) { 5422 ResourceMark rm(THREAD); 5423 assert(_class_name != NULL, "invariant"); 5424 Exceptions::fthrow( 5425 THREAD_AND_LOCATION, 5426 vmSymbols::java_lang_ClassFormatError(), 5427 "Illegal field name \"%.*s\" in class %s", length, bytes, 5428 _class_name->as_C_string() 5429 ); 5430 return; 5431 } 5432 } 5433 5434 // Checks if name is a legal method name. 5435 void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const { 5436 if (!_need_verify || _relax_verify) { return; } 5437 5438 assert(name != NULL, "method name is null"); 5439 char* bytes = (char*)name->bytes(); 5440 unsigned int length = name->utf8_length(); 5441 bool legal = false; 5442 5443 if (length > 0) { 5444 if (bytes[0] == JVM_SIGNATURE_SPECIAL) { 5445 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) { 5446 legal = true; 5447 } 5448 } else if (_major_version < JAVA_1_5_VERSION) { 5449 const char* p; 5450 p = skip_over_field_name(bytes, false, length); 5451 legal = (p != NULL) && ((p - bytes) == (int)length); 5452 } else { 5453 // 4881221: relax the constraints based on JSR202 spec 5454 legal = verify_unqualified_name(bytes, length, LegalMethod); 5455 } 5456 } 5457 5458 if (!legal) { 5459 ResourceMark rm(THREAD); 5460 assert(_class_name != NULL, "invariant"); 5461 Exceptions::fthrow( 5462 THREAD_AND_LOCATION, 5463 vmSymbols::java_lang_ClassFormatError(), 5464 "Illegal method name \"%.*s\" in class %s", length, bytes, 5465 _class_name->as_C_string() 5466 ); 5467 return; 5468 } 5469 } 5470 5471 5472 // Checks if signature is a legal field signature. 5473 void ClassFileParser::verify_legal_field_signature(const Symbol* name, 5474 const Symbol* signature, 5475 TRAPS) const { 5476 if (!_need_verify) { return; } 5477 5478 const char* const bytes = (const char* const)signature->bytes(); 5479 const unsigned int length = signature->utf8_length(); 5480 const char* const p = skip_over_field_signature(bytes, false, length, CHECK); 5481 5482 if (p == NULL || (p - bytes) != (int)length) { 5483 throwIllegalSignature("Field", name, signature, CHECK); 5484 } 5485 } 5486 5487 // Checks if signature is a legal method signature. 5488 // Returns number of parameters 5489 int ClassFileParser::verify_legal_method_signature(const Symbol* name, 5490 const Symbol* signature, 5491 TRAPS) const { 5492 if (!_need_verify) { 5493 // make sure caller's args_size will be less than 0 even for non-static 5494 // method so it will be recomputed in compute_size_of_parameters(). 5495 return -2; 5496 } 5497 5498 // Class initializers cannot have args for class format version >= 51. 5499 if (name == vmSymbols::class_initializer_name() && 5500 signature != vmSymbols::void_method_signature() && 5501 _major_version >= JAVA_7_VERSION) { 5502 throwIllegalSignature("Method", name, signature, CHECK_0); 5503 return 0; 5504 } 5505 5506 unsigned int args_size = 0; 5507 const char* p = (const char*)signature->bytes(); 5508 unsigned int length = signature->utf8_length(); 5509 const char* nextp; 5510 5511 // The first character must be a '(' 5512 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) { 5513 length--; 5514 // Skip over legal field signatures 5515 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5516 while ((length > 0) && (nextp != NULL)) { 5517 args_size++; 5518 if (p[0] == 'J' || p[0] == 'D') { 5519 args_size++; 5520 } 5521 length -= nextp - p; 5522 p = nextp; 5523 nextp = skip_over_field_signature(p, false, length, CHECK_0); 5524 } 5525 // The first non-signature thing better be a ')' 5526 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) { 5527 length--; 5528 if (name->utf8_length() > 0 && name->char_at(0) == JVM_SIGNATURE_SPECIAL) { 5529 // All internal methods must return void 5530 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) { 5531 return args_size; 5532 } 5533 } else { 5534 // Now we better just have a return value 5535 nextp = skip_over_field_signature(p, true, length, CHECK_0); 5536 if (nextp && ((int)length == (nextp - p))) { 5537 return args_size; 5538 } 5539 } 5540 } 5541 } 5542 // Report error 5543 throwIllegalSignature("Method", name, signature, CHECK_0); 5544 return 0; 5545 } 5546 5547 int ClassFileParser::static_field_size() const { 5548 assert(_field_info != NULL, "invariant"); 5549 return _field_info->static_field_size; 5550 } 5551 5552 int ClassFileParser::total_oop_map_count() const { 5553 assert(_field_info != NULL, "invariant"); 5554 return _field_info->total_oop_map_count; 5555 } 5556 5557 jint ClassFileParser::layout_size() const { 5558 assert(_field_info != NULL, "invariant"); 5559 return _field_info->instance_size; 5560 } 5561 5562 static void check_methods_for_intrinsics(const InstanceKlass* ik, 5563 const Array<Method*>* methods) { 5564 assert(ik != NULL, "invariant"); 5565 assert(methods != NULL, "invariant"); 5566 5567 // Set up Method*::intrinsic_id as soon as we know the names of methods. 5568 // (We used to do this lazily, but now we query it in Rewriter, 5569 // which is eagerly done for every method, so we might as well do it now, 5570 // when everything is fresh in memory.) 5571 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik); 5572 5573 if (klass_id != vmSymbols::NO_SID) { 5574 for (int j = 0; j < methods->length(); ++j) { 5575 Method* method = methods->at(j); 5576 method->init_intrinsic_id(); 5577 5578 if (CheckIntrinsics) { 5579 // Check if an intrinsic is defined for method 'method', 5580 // but the method is not annotated with @HotSpotIntrinsicCandidate. 5581 if (method->intrinsic_id() != vmIntrinsics::_none && 5582 !method->intrinsic_candidate()) { 5583 tty->print("Compiler intrinsic is defined for method [%s], " 5584 "but the method is not annotated with @HotSpotIntrinsicCandidate.%s", 5585 method->name_and_sig_as_C_string(), 5586 NOT_DEBUG(" Method will not be inlined.") DEBUG_ONLY(" Exiting.") 5587 ); 5588 tty->cr(); 5589 DEBUG_ONLY(vm_exit(1)); 5590 } 5591 // Check is the method 'method' is annotated with @HotSpotIntrinsicCandidate, 5592 // but there is no intrinsic available for it. 5593 if (method->intrinsic_candidate() && 5594 method->intrinsic_id() == vmIntrinsics::_none) { 5595 tty->print("Method [%s] is annotated with @HotSpotIntrinsicCandidate, " 5596 "but no compiler intrinsic is defined for the method.%s", 5597 method->name_and_sig_as_C_string(), 5598 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5599 ); 5600 tty->cr(); 5601 DEBUG_ONLY(vm_exit(1)); 5602 } 5603 } 5604 } // end for 5605 5606 #ifdef ASSERT 5607 if (CheckIntrinsics) { 5608 // Check for orphan methods in the current class. A method m 5609 // of a class C is orphan if an intrinsic is defined for method m, 5610 // but class C does not declare m. 5611 // The check is potentially expensive, therefore it is available 5612 // only in debug builds. 5613 5614 for (int id = vmIntrinsics::FIRST_ID; id < (int)vmIntrinsics::ID_LIMIT; ++id) { 5615 if (vmIntrinsics::_compiledLambdaForm == id) { 5616 // The _compiledLamdbdaForm intrinsic is a special marker for bytecode 5617 // generated for the JVM from a LambdaForm and therefore no method 5618 // is defined for it. 5619 continue; 5620 } 5621 5622 if (vmIntrinsics::class_for(vmIntrinsics::ID_from(id)) == klass_id) { 5623 // Check if the current class contains a method with the same 5624 // name, flags, signature. 5625 bool match = false; 5626 for (int j = 0; j < methods->length(); ++j) { 5627 const Method* method = methods->at(j); 5628 if (method->intrinsic_id() == id) { 5629 match = true; 5630 break; 5631 } 5632 } 5633 5634 if (!match) { 5635 char buf[1000]; 5636 tty->print("Compiler intrinsic is defined for method [%s], " 5637 "but the method is not available in class [%s].%s", 5638 vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID_from(id), 5639 buf, sizeof(buf)), 5640 ik->name()->as_C_string(), 5641 NOT_DEBUG("") DEBUG_ONLY(" Exiting.") 5642 ); 5643 tty->cr(); 5644 DEBUG_ONLY(vm_exit(1)); 5645 } 5646 } 5647 } // end for 5648 } // CheckIntrinsics 5649 #endif // ASSERT 5650 } 5651 } 5652 5653 InstanceKlass* ClassFileParser::create_instance_klass(bool changed_by_loadhook, TRAPS) { 5654 if (_klass != NULL) { 5655 return _klass; 5656 } 5657 5658 InstanceKlass* const ik = 5659 InstanceKlass::allocate_instance_klass(*this, CHECK_NULL); 5660 5661 fill_instance_klass(ik, changed_by_loadhook, CHECK_NULL); 5662 5663 assert(_klass == ik, "invariant"); 5664 5665 5666 if (ik->should_store_fingerprint()) { 5667 ik->store_fingerprint(_stream->compute_fingerprint()); 5668 } 5669 5670 ik->set_has_passed_fingerprint_check(false); 5671 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) { 5672 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik); 5673 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint(); 5674 if (aot_fp != 0 && aot_fp == fp) { 5675 // This class matches with a class saved in an AOT library 5676 ik->set_has_passed_fingerprint_check(true); 5677 } else { 5678 ResourceMark rm; 5679 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT, 5680 ik->external_name(), aot_fp, _stream->compute_fingerprint()); 5681 } 5682 } 5683 5684 return ik; 5685 } 5686 5687 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) { 5688 assert(ik != NULL, "invariant"); 5689 5690 // Set name and CLD before adding to CLD 5691 ik->set_class_loader_data(_loader_data); 5692 ik->set_name(_class_name); 5693 5694 // Add all classes to our internal class loader list here, 5695 // including classes in the bootstrap (NULL) class loader. 5696 const bool publicize = !is_internal(); 5697 5698 _loader_data->add_class(ik, publicize); 5699 5700 set_klass_to_deallocate(ik); 5701 5702 assert(_field_info != NULL, "invariant"); 5703 assert(ik->static_field_size() == _field_info->static_field_size, "sanity"); 5704 assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count, 5705 "sanity"); 5706 5707 assert(ik->is_instance_klass(), "sanity"); 5708 assert(ik->size_helper() == _field_info->instance_size, "sanity"); 5709 5710 // Fill in information already parsed 5711 ik->set_should_verify_class(_need_verify); 5712 5713 // Not yet: supers are done below to support the new subtype-checking fields 5714 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size); 5715 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields); 5716 assert(_fac != NULL, "invariant"); 5717 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]); 5718 5719 // this transfers ownership of a lot of arrays from 5720 // the parser onto the InstanceKlass* 5721 apply_parsed_class_metadata(ik, _java_fields_count, CHECK); 5722 5723 // note that is not safe to use the fields in the parser from this point on 5724 assert(NULL == _cp, "invariant"); 5725 assert(NULL == _fields, "invariant"); 5726 assert(NULL == _methods, "invariant"); 5727 assert(NULL == _inner_classes, "invariant"); 5728 assert(NULL == _nest_members, "invariant"); 5729 assert(NULL == _local_interfaces, "invariant"); 5730 assert(NULL == _combined_annotations, "invariant"); 5731 assert(NULL == _record_components, "invariant"); 5732 5733 if (_has_final_method) { 5734 ik->set_has_final_method(); 5735 } 5736 5737 ik->copy_method_ordering(_method_ordering, CHECK); 5738 // The InstanceKlass::_methods_jmethod_ids cache 5739 // is managed on the assumption that the initial cache 5740 // size is equal to the number of methods in the class. If 5741 // that changes, then InstanceKlass::idnum_can_increment() 5742 // has to be changed accordingly. 5743 ik->set_initial_method_idnum(ik->methods()->length()); 5744 5745 ik->set_this_class_index(_this_class_index); 5746 5747 if (is_unsafe_anonymous()) { 5748 // _this_class_index is a CONSTANT_Class entry that refers to this 5749 // anonymous class itself. If this class needs to refer to its own methods or 5750 // fields, it would use a CONSTANT_MethodRef, etc, which would reference 5751 // _this_class_index. However, because this class is anonymous (it's 5752 // not stored in SystemDictionary), _this_class_index cannot be resolved 5753 // with ConstantPool::klass_at_impl, which does a SystemDictionary lookup. 5754 // Therefore, we must eagerly resolve _this_class_index now. 5755 ik->constants()->klass_at_put(_this_class_index, ik); 5756 } 5757 5758 ik->set_minor_version(_minor_version); 5759 ik->set_major_version(_major_version); 5760 ik->set_has_nonstatic_concrete_methods(_has_nonstatic_concrete_methods); 5761 ik->set_declares_nonstatic_concrete_methods(_declares_nonstatic_concrete_methods); 5762 5763 if (_unsafe_anonymous_host != NULL) { 5764 assert (ik->is_unsafe_anonymous(), "should be the same"); 5765 ik->set_unsafe_anonymous_host(_unsafe_anonymous_host); 5766 } 5767 5768 // Set PackageEntry for this_klass 5769 oop cl = ik->class_loader(); 5770 Handle clh = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(cl)); 5771 ClassLoaderData* cld = ClassLoaderData::class_loader_data_or_null(clh()); 5772 ik->set_package(cld, CHECK); 5773 5774 const Array<Method*>* const methods = ik->methods(); 5775 assert(methods != NULL, "invariant"); 5776 const int methods_len = methods->length(); 5777 5778 check_methods_for_intrinsics(ik, methods); 5779 5780 // Fill in field values obtained by parse_classfile_attributes 5781 if (_parsed_annotations->has_any_annotations()) { 5782 _parsed_annotations->apply_to(ik); 5783 } 5784 5785 apply_parsed_class_attributes(ik); 5786 5787 // Miranda methods 5788 if ((_num_miranda_methods > 0) || 5789 // if this class introduced new miranda methods or 5790 (_super_klass != NULL && _super_klass->has_miranda_methods()) 5791 // super class exists and this class inherited miranda methods 5792 ) { 5793 ik->set_has_miranda_methods(); // then set a flag 5794 } 5795 5796 // Fill in information needed to compute superclasses. 5797 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK); 5798 ik->set_transitive_interfaces(_transitive_interfaces); 5799 _transitive_interfaces = NULL; 5800 5801 // Initialize itable offset tables 5802 klassItable::setup_itable_offset_table(ik); 5803 5804 // Compute transitive closure of interfaces this class implements 5805 // Do final class setup 5806 fill_oop_maps(ik, 5807 _field_info->nonstatic_oop_map_count, 5808 _field_info->nonstatic_oop_offsets, 5809 _field_info->nonstatic_oop_counts); 5810 5811 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper 5812 set_precomputed_flags(ik); 5813 5814 // check if this class can access its super class 5815 check_super_class_access(ik, CHECK); 5816 5817 // check if this class can access its superinterfaces 5818 check_super_interface_access(ik, CHECK); 5819 5820 // check if this class overrides any final method 5821 check_final_method_override(ik, CHECK); 5822 5823 // reject static interface methods prior to Java 8 5824 if (ik->is_interface() && _major_version < JAVA_8_VERSION) { 5825 check_illegal_static_method(ik, CHECK); 5826 } 5827 5828 // Obtain this_klass' module entry 5829 ModuleEntry* module_entry = ik->module(); 5830 assert(module_entry != NULL, "module_entry should always be set"); 5831 5832 // Obtain java.lang.Module 5833 Handle module_handle(THREAD, module_entry->module()); 5834 5835 // Allocate mirror and initialize static fields 5836 // The create_mirror() call will also call compute_modifiers() 5837 java_lang_Class::create_mirror(ik, 5838 Handle(THREAD, _loader_data->class_loader()), 5839 module_handle, 5840 _protection_domain, 5841 CHECK); 5842 5843 assert(_all_mirandas != NULL, "invariant"); 5844 5845 // Generate any default methods - default methods are public interface methods 5846 // that have a default implementation. This is new with Java 8. 5847 if (_has_nonstatic_concrete_methods) { 5848 DefaultMethods::generate_default_methods(ik, 5849 _all_mirandas, 5850 CHECK); 5851 } 5852 5853 // Add read edges to the unnamed modules of the bootstrap and app class loaders. 5854 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() && 5855 !module_entry->has_default_read_edges()) { 5856 if (!module_entry->set_has_default_read_edges()) { 5857 // We won a potential race 5858 JvmtiExport::add_default_read_edges(module_handle, THREAD); 5859 } 5860 } 5861 5862 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */); 5863 5864 if (!is_internal()) { 5865 if (log_is_enabled(Info, class, load)) { 5866 ResourceMark rm; 5867 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string(); 5868 ik->print_class_load_logging(_loader_data, module_name, _stream); 5869 } 5870 5871 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION && 5872 ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION && 5873 log_is_enabled(Info, class, preview)) { 5874 ResourceMark rm; 5875 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)", 5876 ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION); 5877 } 5878 5879 if (log_is_enabled(Debug, class, resolve)) { 5880 ResourceMark rm; 5881 // print out the superclass. 5882 const char * from = ik->external_name(); 5883 if (ik->java_super() != NULL) { 5884 log_debug(class, resolve)("%s %s (super)", 5885 from, 5886 ik->java_super()->external_name()); 5887 } 5888 // print out each of the interface classes referred to by this class. 5889 const Array<InstanceKlass*>* const local_interfaces = ik->local_interfaces(); 5890 if (local_interfaces != NULL) { 5891 const int length = local_interfaces->length(); 5892 for (int i = 0; i < length; i++) { 5893 const InstanceKlass* const k = local_interfaces->at(i); 5894 const char * to = k->external_name(); 5895 log_debug(class, resolve)("%s %s (interface)", from, to); 5896 } 5897 } 5898 } 5899 } 5900 5901 JFR_ONLY(INIT_ID(ik);) 5902 5903 // If we reach here, all is well. 5904 // Now remove the InstanceKlass* from the _klass_to_deallocate field 5905 // in order for it to not be destroyed in the ClassFileParser destructor. 5906 set_klass_to_deallocate(NULL); 5907 5908 // it's official 5909 set_klass(ik); 5910 5911 debug_only(ik->verify();) 5912 } 5913 5914 void ClassFileParser::update_class_name(Symbol* new_class_name) { 5915 // Decrement the refcount in the old name, since we're clobbering it. 5916 _class_name->decrement_refcount(); 5917 5918 _class_name = new_class_name; 5919 // Increment the refcount of the new name. 5920 // Now the ClassFileParser owns this name and will decrement in 5921 // the destructor. 5922 _class_name->increment_refcount(); 5923 } 5924 5925 5926 // For an unsafe anonymous class that is in the unnamed package, move it to its host class's 5927 // package by prepending its host class's package name to its class name and setting 5928 // its _class_name field. 5929 void ClassFileParser::prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS) { 5930 ResourceMark rm(THREAD); 5931 assert(strrchr(_class_name->as_C_string(), JVM_SIGNATURE_SLASH) == NULL, 5932 "Unsafe anonymous class should not be in a package"); 5933 const char* host_pkg_name = 5934 ClassLoader::package_from_name(unsafe_anonymous_host->name()->as_C_string(), NULL); 5935 5936 if (host_pkg_name != NULL) { 5937 int host_pkg_len = (int)strlen(host_pkg_name); 5938 int class_name_len = _class_name->utf8_length(); 5939 int symbol_len = host_pkg_len + 1 + class_name_len; 5940 char* new_anon_name = NEW_RESOURCE_ARRAY(char, symbol_len + 1); 5941 int n = os::snprintf(new_anon_name, symbol_len + 1, "%s/%.*s", 5942 host_pkg_name, class_name_len, _class_name->base()); 5943 assert(n == symbol_len, "Unexpected number of characters in string"); 5944 5945 // Decrement old _class_name to avoid leaking. 5946 _class_name->decrement_refcount(); 5947 5948 // Create a symbol and update the anonymous class name. 5949 // The new class name is created with a refcount of one. When installed into the InstanceKlass, 5950 // it'll be two and when the ClassFileParser destructor runs, it'll go back to one and get deleted 5951 // when the class is unloaded. 5952 _class_name = SymbolTable::new_symbol(new_anon_name, symbol_len); 5953 } 5954 } 5955 5956 // If the host class and the anonymous class are in the same package then do 5957 // nothing. If the anonymous class is in the unnamed package then move it to its 5958 // host's package. If the classes are in different packages then throw an IAE 5959 // exception. 5960 void ClassFileParser::fix_unsafe_anonymous_class_name(TRAPS) { 5961 assert(_unsafe_anonymous_host != NULL, "Expected an unsafe anonymous class"); 5962 5963 const jbyte* anon_last_slash = UTF8::strrchr((const jbyte*)_class_name->base(), 5964 _class_name->utf8_length(), JVM_SIGNATURE_SLASH); 5965 if (anon_last_slash == NULL) { // Unnamed package 5966 prepend_host_package_name(_unsafe_anonymous_host, CHECK); 5967 } else { 5968 if (!_unsafe_anonymous_host->is_same_class_package(_unsafe_anonymous_host->class_loader(), _class_name)) { 5969 ResourceMark rm(THREAD); 5970 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 5971 err_msg("Host class %s and anonymous class %s are in different packages", 5972 _unsafe_anonymous_host->name()->as_C_string(), _class_name->as_C_string())); 5973 } 5974 } 5975 } 5976 5977 static bool relax_format_check_for(ClassLoaderData* loader_data) { 5978 bool trusted = (loader_data->is_the_null_class_loader_data() || 5979 SystemDictionary::is_platform_class_loader(loader_data->class_loader())); 5980 bool need_verify = 5981 // verifyAll 5982 (BytecodeVerificationLocal && BytecodeVerificationRemote) || 5983 // verifyRemote 5984 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted); 5985 return !need_verify; 5986 } 5987 5988 ClassFileParser::ClassFileParser(ClassFileStream* stream, 5989 Symbol* name, 5990 ClassLoaderData* loader_data, 5991 Handle protection_domain, 5992 const InstanceKlass* unsafe_anonymous_host, 5993 GrowableArray<Handle>* cp_patches, 5994 Publicity pub_level, 5995 TRAPS) : 5996 _stream(stream), 5997 _requested_name(name), 5998 _class_name(NULL), 5999 _loader_data(loader_data), 6000 _unsafe_anonymous_host(unsafe_anonymous_host), 6001 _cp_patches(cp_patches), 6002 _num_patched_klasses(0), 6003 _max_num_patched_klasses(0), 6004 _orig_cp_size(0), 6005 _first_patched_klass_resolved_index(0), 6006 _super_klass(), 6007 _cp(NULL), 6008 _fields(NULL), 6009 _methods(NULL), 6010 _inner_classes(NULL), 6011 _nest_members(NULL), 6012 _nest_host(0), 6013 _record_components(NULL), 6014 _local_interfaces(NULL), 6015 _transitive_interfaces(NULL), 6016 _combined_annotations(NULL), 6017 _class_annotations(NULL), 6018 _class_type_annotations(NULL), 6019 _fields_annotations(NULL), 6020 _fields_type_annotations(NULL), 6021 _klass(NULL), 6022 _klass_to_deallocate(NULL), 6023 _parsed_annotations(NULL), 6024 _fac(NULL), 6025 _field_info(NULL), 6026 _method_ordering(NULL), 6027 _all_mirandas(NULL), 6028 _vtable_size(0), 6029 _itable_size(0), 6030 _num_miranda_methods(0), 6031 _rt(REF_NONE), 6032 _protection_domain(protection_domain), 6033 _access_flags(), 6034 _pub_level(pub_level), 6035 _bad_constant_seen(0), 6036 _synthetic_flag(false), 6037 _sde_length(false), 6038 _sde_buffer(NULL), 6039 _sourcefile_index(0), 6040 _generic_signature_index(0), 6041 _major_version(0), 6042 _minor_version(0), 6043 _this_class_index(0), 6044 _super_class_index(0), 6045 _itfs_len(0), 6046 _java_fields_count(0), 6047 _need_verify(false), 6048 _relax_verify(false), 6049 _has_nonstatic_concrete_methods(false), 6050 _declares_nonstatic_concrete_methods(false), 6051 _has_final_method(false), 6052 _has_finalizer(false), 6053 _has_empty_finalizer(false), 6054 _has_vanilla_constructor(false), 6055 _max_bootstrap_specifier_index(-1) { 6056 6057 _class_name = name != NULL ? name : vmSymbols::unknown_class_name(); 6058 _class_name->increment_refcount(); 6059 6060 assert(THREAD->is_Java_thread(), "invariant"); 6061 assert(_loader_data != NULL, "invariant"); 6062 assert(stream != NULL, "invariant"); 6063 assert(_stream != NULL, "invariant"); 6064 assert(_stream->buffer() == _stream->current(), "invariant"); 6065 assert(_class_name != NULL, "invariant"); 6066 assert(0 == _access_flags.as_int(), "invariant"); 6067 6068 // Figure out whether we can skip format checking (matching classic VM behavior) 6069 if (DumpSharedSpaces) { 6070 // verify == true means it's a 'remote' class (i.e., non-boot class) 6071 // Verification decision is based on BytecodeVerificationRemote flag 6072 // for those classes. 6073 _need_verify = (stream->need_verify()) ? BytecodeVerificationRemote : 6074 BytecodeVerificationLocal; 6075 } 6076 else { 6077 _need_verify = Verifier::should_verify_for(_loader_data->class_loader(), 6078 stream->need_verify()); 6079 } 6080 if (_cp_patches != NULL) { 6081 int len = _cp_patches->length(); 6082 for (int i=0; i<len; i++) { 6083 if (has_cp_patch_at(i)) { 6084 Handle patch = cp_patch_at(i); 6085 if (java_lang_String::is_instance(patch()) || java_lang_Class::is_instance(patch())) { 6086 // We need to append the names of the patched classes to the end of the constant pool, 6087 // because a patched class may have a Utf8 name that's not already included in the 6088 // original constant pool. These class names are used when patch_constant_pool() 6089 // calls patch_class(). 6090 // 6091 // Note that a String in cp_patch_at(i) may be used to patch a Utf8, a String, or a Class. 6092 // At this point, we don't know the tag for index i yet, because we haven't parsed the 6093 // constant pool. So we can only assume the worst -- every String is used to patch a Class. 6094 _max_num_patched_klasses++; 6095 } 6096 } 6097 } 6098 } 6099 6100 // synch back verification state to stream 6101 stream->set_verify(_need_verify); 6102 6103 // Check if verification needs to be relaxed for this class file 6104 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376) 6105 _relax_verify = relax_format_check_for(_loader_data); 6106 6107 parse_stream(stream, CHECK); 6108 6109 post_process_parsed_stream(stream, _cp, CHECK); 6110 } 6111 6112 void ClassFileParser::clear_class_metadata() { 6113 // metadata created before the instance klass is created. Must be 6114 // deallocated if classfile parsing returns an error. 6115 _cp = NULL; 6116 _fields = NULL; 6117 _methods = NULL; 6118 _inner_classes = NULL; 6119 _nest_members = NULL; 6120 _local_interfaces = NULL; 6121 _combined_annotations = NULL; 6122 _class_annotations = _class_type_annotations = NULL; 6123 _fields_annotations = _fields_type_annotations = NULL; 6124 _record_components = NULL; 6125 } 6126 6127 // Destructor to clean up 6128 ClassFileParser::~ClassFileParser() { 6129 _class_name->decrement_refcount(); 6130 6131 if (_cp != NULL) { 6132 MetadataFactory::free_metadata(_loader_data, _cp); 6133 } 6134 if (_fields != NULL) { 6135 MetadataFactory::free_array<u2>(_loader_data, _fields); 6136 } 6137 6138 if (_methods != NULL) { 6139 // Free methods 6140 InstanceKlass::deallocate_methods(_loader_data, _methods); 6141 } 6142 6143 // beware of the Universe::empty_blah_array!! 6144 if (_inner_classes != NULL && _inner_classes != Universe::the_empty_short_array()) { 6145 MetadataFactory::free_array<u2>(_loader_data, _inner_classes); 6146 } 6147 6148 if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) { 6149 MetadataFactory::free_array<u2>(_loader_data, _nest_members); 6150 } 6151 6152 if (_record_components != NULL) { 6153 InstanceKlass::deallocate_record_components(_loader_data, _record_components); 6154 } 6155 6156 // Free interfaces 6157 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass, 6158 _local_interfaces, _transitive_interfaces); 6159 6160 if (_combined_annotations != NULL) { 6161 // After all annotations arrays have been created, they are installed into the 6162 // Annotations object that will be assigned to the InstanceKlass being created. 6163 6164 // Deallocate the Annotations object and the installed annotations arrays. 6165 _combined_annotations->deallocate_contents(_loader_data); 6166 6167 // If the _combined_annotations pointer is non-NULL, 6168 // then the other annotations fields should have been cleared. 6169 assert(_class_annotations == NULL, "Should have been cleared"); 6170 assert(_class_type_annotations == NULL, "Should have been cleared"); 6171 assert(_fields_annotations == NULL, "Should have been cleared"); 6172 assert(_fields_type_annotations == NULL, "Should have been cleared"); 6173 } else { 6174 // If the annotations arrays were not installed into the Annotations object, 6175 // then they have to be deallocated explicitly. 6176 MetadataFactory::free_array<u1>(_loader_data, _class_annotations); 6177 MetadataFactory::free_array<u1>(_loader_data, _class_type_annotations); 6178 Annotations::free_contents(_loader_data, _fields_annotations); 6179 Annotations::free_contents(_loader_data, _fields_type_annotations); 6180 } 6181 6182 clear_class_metadata(); 6183 _transitive_interfaces = NULL; 6184 6185 // deallocate the klass if already created. Don't directly deallocate, but add 6186 // to the deallocate list so that the klass is removed from the CLD::_klasses list 6187 // at a safepoint. 6188 if (_klass_to_deallocate != NULL) { 6189 _loader_data->add_to_deallocate_list(_klass_to_deallocate); 6190 } 6191 } 6192 6193 void ClassFileParser::parse_stream(const ClassFileStream* const stream, 6194 TRAPS) { 6195 6196 assert(stream != NULL, "invariant"); 6197 assert(_class_name != NULL, "invariant"); 6198 6199 // BEGIN STREAM PARSING 6200 stream->guarantee_more(8, CHECK); // magic, major, minor 6201 // Magic value 6202 const u4 magic = stream->get_u4_fast(); 6203 guarantee_property(magic == JAVA_CLASSFILE_MAGIC, 6204 "Incompatible magic value %u in class file %s", 6205 magic, CHECK); 6206 6207 // Version numbers 6208 _minor_version = stream->get_u2_fast(); 6209 _major_version = stream->get_u2_fast(); 6210 6211 if (DumpSharedSpaces && _major_version < JAVA_6_VERSION) { 6212 ResourceMark rm; 6213 warning("Pre JDK 6 class not supported by CDS: %u.%u %s", 6214 _major_version, _minor_version, _class_name->as_C_string()); 6215 Exceptions::fthrow( 6216 THREAD_AND_LOCATION, 6217 vmSymbols::java_lang_UnsupportedClassVersionError(), 6218 "Unsupported major.minor version for dump time %u.%u", 6219 _major_version, 6220 _minor_version); 6221 } 6222 6223 // Check version numbers - we check this even with verifier off 6224 verify_class_version(_major_version, _minor_version, _class_name, CHECK); 6225 6226 stream->guarantee_more(3, CHECK); // length, first cp tag 6227 u2 cp_size = stream->get_u2_fast(); 6228 6229 guarantee_property( 6230 cp_size >= 1, "Illegal constant pool size %u in class file %s", 6231 cp_size, CHECK); 6232 6233 _orig_cp_size = cp_size; 6234 if (int(cp_size) + _max_num_patched_klasses > 0xffff) { 6235 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes"); 6236 } 6237 cp_size += _max_num_patched_klasses; 6238 6239 _cp = ConstantPool::allocate(_loader_data, 6240 cp_size, 6241 CHECK); 6242 6243 ConstantPool* const cp = _cp; 6244 6245 parse_constant_pool(stream, cp, _orig_cp_size, CHECK); 6246 6247 assert(cp_size == (const u2)cp->length(), "invariant"); 6248 6249 // ACCESS FLAGS 6250 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len 6251 6252 // Access flags 6253 jint flags; 6254 // JVM_ACC_MODULE is defined in JDK-9 and later. 6255 if (_major_version >= JAVA_9_VERSION) { 6256 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE); 6257 } else { 6258 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS; 6259 } 6260 6261 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) { 6262 // Set abstract bit for old class files for backward compatibility 6263 flags |= JVM_ACC_ABSTRACT; 6264 } 6265 6266 verify_legal_class_modifiers(flags, CHECK); 6267 6268 short bad_constant = class_bad_constant_seen(); 6269 if (bad_constant != 0) { 6270 // Do not throw CFE until after the access_flags are checked because if 6271 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE. 6272 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK); 6273 } 6274 6275 _access_flags.set_flags(flags); 6276 6277 // This class and superclass 6278 _this_class_index = stream->get_u2_fast(); 6279 check_property( 6280 valid_cp_range(_this_class_index, cp_size) && 6281 cp->tag_at(_this_class_index).is_unresolved_klass(), 6282 "Invalid this class index %u in constant pool in class file %s", 6283 _this_class_index, CHECK); 6284 6285 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index); 6286 assert(class_name_in_cp != NULL, "class_name can't be null"); 6287 6288 // Update _class_name to reflect the name in the constant pool 6289 update_class_name(class_name_in_cp); 6290 6291 // Don't need to check whether this class name is legal or not. 6292 // It has been checked when constant pool is parsed. 6293 // However, make sure it is not an array type. 6294 if (_need_verify) { 6295 guarantee_property(_class_name->char_at(0) != JVM_SIGNATURE_ARRAY, 6296 "Bad class name in class file %s", 6297 CHECK); 6298 } 6299 6300 // Checks if name in class file matches requested name 6301 if (_requested_name != NULL && _requested_name != _class_name) { 6302 ResourceMark rm(THREAD); 6303 Exceptions::fthrow( 6304 THREAD_AND_LOCATION, 6305 vmSymbols::java_lang_NoClassDefFoundError(), 6306 "%s (wrong name: %s)", 6307 _class_name->as_C_string(), 6308 _requested_name != NULL ? _requested_name->as_C_string() : "NoName" 6309 ); 6310 return; 6311 } 6312 6313 // if this is an anonymous class fix up its name if it's in the unnamed 6314 // package. Otherwise, throw IAE if it is in a different package than 6315 // its host class. 6316 if (_unsafe_anonymous_host != NULL) { 6317 fix_unsafe_anonymous_class_name(CHECK); 6318 } 6319 6320 // Verification prevents us from creating names with dots in them, this 6321 // asserts that that's the case. 6322 assert(is_internal_format(_class_name), "external class name format used internally"); 6323 6324 if (!is_internal()) { 6325 LogTarget(Debug, class, preorder) lt; 6326 if (lt.is_enabled()){ 6327 ResourceMark rm(THREAD); 6328 LogStream ls(lt); 6329 ls.print("%s", _class_name->as_klass_external_name()); 6330 if (stream->source() != NULL) { 6331 ls.print(" source: %s", stream->source()); 6332 } 6333 ls.cr(); 6334 } 6335 6336 #if INCLUDE_CDS 6337 if (DumpLoadedClassList != NULL && stream->source() != NULL && classlist_file->is_open()) { 6338 if (!ClassLoader::has_jrt_entry()) { 6339 warning("DumpLoadedClassList and CDS are not supported in exploded build"); 6340 DumpLoadedClassList = NULL; 6341 } else if (SystemDictionaryShared::is_sharing_possible(_loader_data) && 6342 _unsafe_anonymous_host == NULL) { 6343 // Only dump the classes that can be stored into CDS archive. 6344 // Unsafe anonymous classes such as generated LambdaForm classes are also not included. 6345 oop class_loader = _loader_data->class_loader(); 6346 ResourceMark rm(THREAD); 6347 bool skip = false; 6348 if (class_loader == NULL || SystemDictionary::is_platform_class_loader(class_loader)) { 6349 // For the boot and platform class loaders, skip classes that are not found in the 6350 // java runtime image, such as those found in the --patch-module entries. 6351 // These classes can't be loaded from the archive during runtime. 6352 if (!stream->from_boot_loader_modules_image() && strncmp(stream->source(), "jrt:", 4) != 0) { 6353 skip = true; 6354 } 6355 6356 if (class_loader == NULL && ClassLoader::contains_append_entry(stream->source())) { 6357 // .. but don't skip the boot classes that are loaded from -Xbootclasspath/a 6358 // as they can be loaded from the archive during runtime. 6359 skip = false; 6360 } 6361 } 6362 if (skip) { 6363 tty->print_cr("skip writing class %s from source %s to classlist file", 6364 _class_name->as_C_string(), stream->source()); 6365 } else { 6366 classlist_file->print_cr("%s", _class_name->as_C_string()); 6367 classlist_file->flush(); 6368 } 6369 } 6370 } 6371 #endif 6372 } 6373 6374 // SUPERKLASS 6375 _super_class_index = stream->get_u2_fast(); 6376 _super_klass = parse_super_class(cp, 6377 _super_class_index, 6378 _need_verify, 6379 CHECK); 6380 6381 // Interfaces 6382 _itfs_len = stream->get_u2_fast(); 6383 parse_interfaces(stream, 6384 _itfs_len, 6385 cp, 6386 &_has_nonstatic_concrete_methods, 6387 CHECK); 6388 6389 assert(_local_interfaces != NULL, "invariant"); 6390 6391 // Fields (offsets are filled in later) 6392 _fac = new FieldAllocationCount(); 6393 parse_fields(stream, 6394 _access_flags.is_interface(), 6395 _fac, 6396 cp, 6397 cp_size, 6398 &_java_fields_count, 6399 CHECK); 6400 6401 assert(_fields != NULL, "invariant"); 6402 6403 // Methods 6404 AccessFlags promoted_flags; 6405 parse_methods(stream, 6406 _access_flags.is_interface(), 6407 &promoted_flags, 6408 &_has_final_method, 6409 &_declares_nonstatic_concrete_methods, 6410 CHECK); 6411 6412 assert(_methods != NULL, "invariant"); 6413 6414 // promote flags from parse_methods() to the klass' flags 6415 _access_flags.add_promoted_flags(promoted_flags.as_int()); 6416 6417 if (_declares_nonstatic_concrete_methods) { 6418 _has_nonstatic_concrete_methods = true; 6419 } 6420 6421 // Additional attributes/annotations 6422 _parsed_annotations = new ClassAnnotationCollector(); 6423 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK); 6424 6425 assert(_inner_classes != NULL, "invariant"); 6426 6427 // Finalize the Annotations metadata object, 6428 // now that all annotation arrays have been created. 6429 create_combined_annotations(CHECK); 6430 6431 // Make sure this is the end of class file stream 6432 guarantee_property(stream->at_eos(), 6433 "Extra bytes at the end of class file %s", 6434 CHECK); 6435 6436 // all bytes in stream read and parsed 6437 } 6438 6439 void ClassFileParser::post_process_parsed_stream(const ClassFileStream* const stream, 6440 ConstantPool* cp, 6441 TRAPS) { 6442 assert(stream != NULL, "invariant"); 6443 assert(stream->at_eos(), "invariant"); 6444 assert(cp != NULL, "invariant"); 6445 assert(_loader_data != NULL, "invariant"); 6446 6447 if (_class_name == vmSymbols::java_lang_Object()) { 6448 check_property(_local_interfaces == Universe::the_empty_instance_klass_array(), 6449 "java.lang.Object cannot implement an interface in class file %s", 6450 CHECK); 6451 } 6452 // We check super class after class file is parsed and format is checked 6453 if (_super_class_index > 0 && NULL ==_super_klass) { 6454 Symbol* const super_class_name = cp->klass_name_at(_super_class_index); 6455 if (_access_flags.is_interface()) { 6456 // Before attempting to resolve the superclass, check for class format 6457 // errors not checked yet. 6458 guarantee_property(super_class_name == vmSymbols::java_lang_Object(), 6459 "Interfaces must have java.lang.Object as superclass in class file %s", 6460 CHECK); 6461 } 6462 Handle loader(THREAD, _loader_data->class_loader()); 6463 _super_klass = (const InstanceKlass*) 6464 SystemDictionary::resolve_super_or_fail(_class_name, 6465 super_class_name, 6466 loader, 6467 _protection_domain, 6468 true, 6469 CHECK); 6470 } 6471 6472 if (_super_klass != NULL) { 6473 if (_super_klass->has_nonstatic_concrete_methods()) { 6474 _has_nonstatic_concrete_methods = true; 6475 } 6476 6477 if (_super_klass->is_interface()) { 6478 ResourceMark rm(THREAD); 6479 Exceptions::fthrow( 6480 THREAD_AND_LOCATION, 6481 vmSymbols::java_lang_IncompatibleClassChangeError(), 6482 "class %s has interface %s as super class", 6483 _class_name->as_klass_external_name(), 6484 _super_klass->external_name() 6485 ); 6486 return; 6487 } 6488 // Make sure super class is not final 6489 if (_super_klass->is_final()) { 6490 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class"); 6491 } 6492 } 6493 6494 // Compute the transitive list of all unique interfaces implemented by this class 6495 _transitive_interfaces = 6496 compute_transitive_interfaces(_super_klass, 6497 _local_interfaces, 6498 _loader_data, 6499 CHECK); 6500 6501 assert(_transitive_interfaces != NULL, "invariant"); 6502 6503 // sort methods 6504 _method_ordering = sort_methods(_methods); 6505 6506 _all_mirandas = new GrowableArray<Method*>(20); 6507 6508 Handle loader(THREAD, _loader_data->class_loader()); 6509 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size, 6510 &_num_miranda_methods, 6511 _all_mirandas, 6512 _super_klass, 6513 _methods, 6514 _access_flags, 6515 _major_version, 6516 loader, 6517 _class_name, 6518 _local_interfaces, 6519 CHECK); 6520 6521 // Size of Java itable (in words) 6522 _itable_size = _access_flags.is_interface() ? 0 : 6523 klassItable::compute_itable_size(_transitive_interfaces); 6524 6525 assert(_fac != NULL, "invariant"); 6526 assert(_parsed_annotations != NULL, "invariant"); 6527 6528 _field_info = new FieldLayoutInfo(); 6529 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK); 6530 6531 // Compute reference typ 6532 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type(); 6533 6534 } 6535 6536 void ClassFileParser::set_klass(InstanceKlass* klass) { 6537 6538 #ifdef ASSERT 6539 if (klass != NULL) { 6540 assert(NULL == _klass, "leaking?"); 6541 } 6542 #endif 6543 6544 _klass = klass; 6545 } 6546 6547 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) { 6548 6549 #ifdef ASSERT 6550 if (klass != NULL) { 6551 assert(NULL == _klass_to_deallocate, "leaking?"); 6552 } 6553 #endif 6554 6555 _klass_to_deallocate = klass; 6556 } 6557 6558 // Caller responsible for ResourceMark 6559 // clone stream with rewound position 6560 const ClassFileStream* ClassFileParser::clone_stream() const { 6561 assert(_stream != NULL, "invariant"); 6562 6563 return _stream->clone(); 6564 } 6565 // ---------------------------------------------------------------------------- 6566 // debugging 6567 6568 #ifdef ASSERT 6569 6570 // return true if class_name contains no '.' (internal format is '/') 6571 bool ClassFileParser::is_internal_format(Symbol* class_name) { 6572 if (class_name != NULL) { 6573 ResourceMark rm; 6574 char* name = class_name->as_C_string(); 6575 return strchr(name, JVM_SIGNATURE_DOT) == NULL; 6576 } else { 6577 return true; 6578 } 6579 } 6580 6581 #endif