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