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