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