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