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