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