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