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