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