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