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