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