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