38 #include "classfile/verifier.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "memory/allocation.hpp"
43 #include "memory/metadataFactory.hpp"
44 #include "memory/oopFactory.hpp"
45 #include "memory/resourceArea.hpp"
46 #include "memory/universe.hpp"
47 #include "oops/annotations.hpp"
48 #include "oops/constantPool.inline.hpp"
49 #include "oops/fieldStreams.hpp"
50 #include "oops/instanceKlass.hpp"
51 #include "oops/instanceMirrorKlass.hpp"
52 #include "oops/klass.inline.hpp"
53 #include "oops/klassVtable.hpp"
54 #include "oops/metadata.hpp"
55 #include "oops/method.hpp"
56 #include "oops/oop.inline.hpp"
57 #include "oops/symbol.hpp"
58 #include "prims/jvmtiExport.hpp"
59 #include "prims/jvmtiThreadState.hpp"
60 #include "runtime/arguments.hpp"
61 #include "runtime/handles.inline.hpp"
62 #include "runtime/javaCalls.hpp"
63 #include "runtime/os.hpp"
64 #include "runtime/perfData.hpp"
65 #include "runtime/reflection.hpp"
66 #include "runtime/safepointVerifiers.hpp"
67 #include "runtime/signature.hpp"
68 #include "runtime/timer.hpp"
69 #include "services/classLoadingService.hpp"
70 #include "services/threadService.hpp"
71 #include "utilities/align.hpp"
72 #include "utilities/bitMap.inline.hpp"
73 #include "utilities/copy.hpp"
74 #include "utilities/exceptions.hpp"
75 #include "utilities/globalDefinitions.hpp"
76 #include "utilities/growableArray.hpp"
77 #include "utilities/macros.hpp"
105 // - also used as the max version when running in jdk6
106 #define JAVA_6_VERSION 50
107
108 // Used for backward compatibility reasons:
109 // - to disallow argument and require ACC_STATIC for <clinit> methods
110 #define JAVA_7_VERSION 51
111
112 // Extension method support.
113 #define JAVA_8_VERSION 52
114
115 #define JAVA_9_VERSION 53
116
117 #define JAVA_10_VERSION 54
118
119 #define JAVA_11_VERSION 55
120
121 #define JAVA_12_VERSION 56
122
123 #define JAVA_13_VERSION 57
124
125 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
126 assert((bad_constant == 19 || bad_constant == 20) && _major_version >= JAVA_9_VERSION,
127 "Unexpected bad constant pool entry");
128 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
129 }
130
131 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
132 ConstantPool* cp,
133 const int length,
134 TRAPS) {
135 assert(stream != NULL, "invariant");
136 assert(cp != NULL, "invariant");
137
138 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
139 // this function (_current can be allocated in a register, with scalar
140 // replacement of aggregates). The _current pointer is copied back to
141 // stream() when this function returns. DON'T call another method within
142 // this method that uses stream().
143 const ClassFileStream cfs1 = *stream;
144 const ClassFileStream* const cfs = &cfs1;
145
146 assert(cfs->allocated_on_stack(), "should be local");
147 debug_only(const u1* const old_current = stream->current();)
148
149 // Used for batching symbol allocations.
150 const char* names[SymbolTable::symbol_alloc_batch_size];
151 int lengths[SymbolTable::symbol_alloc_batch_size];
152 int indices[SymbolTable::symbol_alloc_batch_size];
153 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
154 int names_count = 0;
155
156 // parsing Index 0 is unused
157 for (int index = 1; index < length; index++) {
158 // Each of the following case guarantees one more byte in the stream
159 // for the following tag or the access_flags following constant pool,
160 // so we don't need bounds-check for reading tag.
161 const u1 tag = cfs->get_u1_fast();
162 switch (tag) {
163 case JVM_CONSTANT_Class : {
164 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
165 const u2 name_index = cfs->get_u2_fast();
166 cp->klass_index_at_put(index, name_index);
167 break;
168 }
169 case JVM_CONSTANT_Fieldref: {
170 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
171 const u2 class_index = cfs->get_u2_fast();
172 const u2 name_and_type_index = cfs->get_u2_fast();
173 cp->field_at_put(index, class_index, name_and_type_index);
174 break;
175 }
176 case JVM_CONSTANT_Methodref: {
177 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
178 const u2 class_index = cfs->get_u2_fast();
179 const u2 name_and_type_index = cfs->get_u2_fast();
180 cp->method_at_put(index, class_index, name_and_type_index);
181 break;
182 }
183 case JVM_CONSTANT_InterfaceMethodref: {
475 check_property(valid_symbol_at(name_ref_index),
476 "Invalid constant pool index %u in class file %s",
477 name_ref_index, CHECK);
478 check_property(valid_symbol_at(signature_ref_index),
479 "Invalid constant pool index %u in class file %s",
480 signature_ref_index, CHECK);
481 break;
482 }
483 case JVM_CONSTANT_Utf8:
484 break;
485 case JVM_CONSTANT_UnresolvedClass: // fall-through
486 case JVM_CONSTANT_UnresolvedClassInError: {
487 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
488 break;
489 }
490 case JVM_CONSTANT_ClassIndex: {
491 const int class_index = cp->klass_index_at(index);
492 check_property(valid_symbol_at(class_index),
493 "Invalid constant pool index %u in class file %s",
494 class_index, CHECK);
495 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
496 break;
497 }
498 case JVM_CONSTANT_StringIndex: {
499 const int string_index = cp->string_index_at(index);
500 check_property(valid_symbol_at(string_index),
501 "Invalid constant pool index %u in class file %s",
502 string_index, CHECK);
503 Symbol* const sym = cp->symbol_at(string_index);
504 cp->unresolved_string_at_put(index, sym);
505 break;
506 }
507 case JVM_CONSTANT_MethodHandle: {
508 const int ref_index = cp->method_handle_index_at(index);
509 check_property(valid_cp_range(ref_index, length),
510 "Invalid constant pool index %u in class file %s",
511 ref_index, CHECK);
512 const constantTag tag = cp->tag_at(ref_index);
513 const int ref_kind = cp->method_handle_ref_kind_at(index);
514
515 switch (ref_kind) {
1444 CHECK);
1445 parsed_annotations->set_field_annotations(a);
1446 a = assemble_annotations(runtime_visible_type_annotations,
1447 runtime_visible_type_annotations_length,
1448 runtime_invisible_type_annotations,
1449 runtime_invisible_type_annotations_length,
1450 CHECK);
1451 parsed_annotations->set_field_type_annotations(a);
1452 return;
1453 }
1454
1455
1456 // Field allocation types. Used for computing field offsets.
1457
1458 enum FieldAllocationType {
1459 STATIC_OOP, // Oops
1460 STATIC_BYTE, // Boolean, Byte, char
1461 STATIC_SHORT, // shorts
1462 STATIC_WORD, // ints
1463 STATIC_DOUBLE, // aligned long or double
1464 NONSTATIC_OOP,
1465 NONSTATIC_BYTE,
1466 NONSTATIC_SHORT,
1467 NONSTATIC_WORD,
1468 NONSTATIC_DOUBLE,
1469 MAX_FIELD_ALLOCATION_TYPE,
1470 BAD_ALLOCATION_TYPE = -1
1471 };
1472
1473 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1474 BAD_ALLOCATION_TYPE, // 0
1475 BAD_ALLOCATION_TYPE, // 1
1476 BAD_ALLOCATION_TYPE, // 2
1477 BAD_ALLOCATION_TYPE, // 3
1478 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1479 NONSTATIC_SHORT, // T_CHAR = 5,
1480 NONSTATIC_WORD, // T_FLOAT = 6,
1481 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1482 NONSTATIC_BYTE, // T_BYTE = 8,
1483 NONSTATIC_SHORT, // T_SHORT = 9,
1484 NONSTATIC_WORD, // T_INT = 10,
1485 NONSTATIC_DOUBLE, // T_LONG = 11,
1486 NONSTATIC_OOP, // T_OBJECT = 12,
1487 NONSTATIC_OOP, // T_ARRAY = 13,
1488 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1489 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1490 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1491 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1492 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1493 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1494 BAD_ALLOCATION_TYPE, // 0
1495 BAD_ALLOCATION_TYPE, // 1
1496 BAD_ALLOCATION_TYPE, // 2
1497 BAD_ALLOCATION_TYPE, // 3
1498 STATIC_BYTE , // T_BOOLEAN = 4,
1499 STATIC_SHORT, // T_CHAR = 5,
1500 STATIC_WORD, // T_FLOAT = 6,
1501 STATIC_DOUBLE, // T_DOUBLE = 7,
1502 STATIC_BYTE, // T_BYTE = 8,
1503 STATIC_SHORT, // T_SHORT = 9,
1504 STATIC_WORD, // T_INT = 10,
1505 STATIC_DOUBLE, // T_LONG = 11,
1506 STATIC_OOP, // T_OBJECT = 12,
1507 STATIC_OOP, // T_ARRAY = 13,
1508 BAD_ALLOCATION_TYPE, // T_VOID = 14,
1509 BAD_ALLOCATION_TYPE, // T_ADDRESS = 15,
1510 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 16,
1511 BAD_ALLOCATION_TYPE, // T_METADATA = 17,
1512 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 18,
1513 BAD_ALLOCATION_TYPE, // T_CONFLICT = 19,
1514 };
1515
1516 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type) {
1517 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1518 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1519 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1520 return result;
1521 }
1522
1523 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1524 public:
1525 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1526
1527 FieldAllocationCount() {
1528 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1529 count[i] = 0;
1530 }
1531 }
1532
1533 FieldAllocationType update(bool is_static, BasicType type) {
1534 FieldAllocationType atype = basic_type_to_atype(is_static, type);
1535 if (atype != BAD_ALLOCATION_TYPE) {
1536 // Make sure there is no overflow with injected fields.
1537 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1538 count[atype]++;
1539 }
1540 return atype;
1541 }
1542 };
1543
1544 // Side-effects: populates the _fields, _fields_annotations,
1545 // _fields_type_annotations fields
1546 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1547 bool is_interface,
1548 FieldAllocationCount* const fac,
1549 ConstantPool* cp,
1550 const int cp_size,
1551 u2* const java_fields_count_ptr,
1552 TRAPS) {
1553
1554 assert(cfs != NULL, "invariant");
1555 assert(fac != NULL, "invariant");
1556 assert(cp != NULL, "invariant");
1557 assert(java_fields_count_ptr != NULL, "invariant");
1558
1559 assert(NULL == _fields, "invariant");
1560 assert(NULL == _fields_annotations, "invariant");
1561 assert(NULL == _fields_type_annotations, "invariant");
1562
1563 cfs->guarantee_more(2, CHECK); // length
1564 const u2 length = cfs->get_u2_fast();
1565 *java_fields_count_ptr = length;
1566
1567 int num_injected = 0;
1568 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1569 &num_injected);
1570 const int total_fields = length + num_injected;
1571
1572 // The field array starts with tuples of shorts
1573 // [access, name index, sig index, initial value index, byte offset].
1574 // A generic signature slot only exists for field with generic
1575 // signature attribute. And the access flag is set with
1576 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1577 // signature slots are at the end of the field array and after all
1578 // other fields data.
1579 //
1580 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1581 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1582 // ...
1583 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1584 // [generic signature index]
1585 // [generic signature index]
1586 // ...
1587 //
1588 // Allocate a temporary resource array for field data. For each field,
1589 // a slot is reserved in the temporary array for the generic signature
1590 // index. After parsing all fields, the data are copied to a permanent
1591 // array and any unused slots will be discarded.
1592 ResourceMark rm(THREAD);
1593 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1594 u2,
1595 total_fields * (FieldInfo::field_slots + 1));
1596
1597 // The generic signature slots start after all other fields' data.
1598 int generic_signature_slot = total_fields * FieldInfo::field_slots;
1599 int num_generic_signature = 0;
1600 for (int n = 0; n < length; n++) {
1601 // access_flags, name_index, descriptor_index, attributes_count
1602 cfs->guarantee_more(8, CHECK);
1603
1604 AccessFlags access_flags;
1605 const jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1606 verify_legal_field_modifiers(flags, is_interface, CHECK);
1607 access_flags.set_flags(flags);
1608
1609 const u2 name_index = cfs->get_u2_fast();
1610 check_property(valid_symbol_at(name_index),
1611 "Invalid constant pool index %u for field name in class file %s",
1612 name_index, CHECK);
1613 const Symbol* const name = cp->symbol_at(name_index);
1614 verify_legal_field_name(name, CHECK);
1615
1616 const u2 signature_index = cfs->get_u2_fast();
1617 check_property(valid_symbol_at(signature_index),
1618 "Invalid constant pool index %u for field signature in class file %s",
1619 signature_index, CHECK);
1620 const Symbol* const sig = cp->symbol_at(signature_index);
1621 verify_legal_field_signature(name, sig, CHECK);
1622
1623 u2 constantvalue_index = 0;
1624 bool is_synthetic = false;
1625 u2 generic_signature_index = 0;
1626 const bool is_static = access_flags.is_static();
1627 FieldAnnotationCollector parsed_annotations(_loader_data);
1628
1629 const u2 attributes_count = cfs->get_u2_fast();
1630 if (attributes_count > 0) {
1631 parse_field_attributes(cfs,
1632 attributes_count,
1633 is_static,
1634 signature_index,
1635 &constantvalue_index,
1636 &is_synthetic,
1637 &generic_signature_index,
1638 &parsed_annotations,
1639 CHECK);
1640
1641 if (parsed_annotations.field_annotations() != NULL) {
1661
1662 if (is_synthetic) {
1663 access_flags.set_is_synthetic();
1664 }
1665 if (generic_signature_index != 0) {
1666 access_flags.set_field_has_generic_signature();
1667 fa[generic_signature_slot] = generic_signature_index;
1668 generic_signature_slot ++;
1669 num_generic_signature ++;
1670 }
1671 }
1672
1673 FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1674 field->initialize(access_flags.as_short(),
1675 name_index,
1676 signature_index,
1677 constantvalue_index);
1678 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1679
1680 // Remember how many oops we encountered and compute allocation type
1681 const FieldAllocationType atype = fac->update(is_static, type);
1682 field->set_allocation_type(atype);
1683
1684 // After field is initialized with type, we can augment it with aux info
1685 if (parsed_annotations.has_any_annotations())
1686 parsed_annotations.apply_to(field);
1687 }
1688
1689 int index = length;
1690 if (num_injected != 0) {
1691 for (int n = 0; n < num_injected; n++) {
1692 // Check for duplicates
1693 if (injected[n].may_be_java) {
1694 const Symbol* const name = injected[n].name();
1695 const Symbol* const signature = injected[n].signature();
1696 bool duplicate = false;
1697 for (int i = 0; i < length; i++) {
1698 const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1699 if (name == cp->symbol_at(f->name_index()) &&
1700 signature == cp->symbol_at(f->signature_index())) {
1701 // Symbol is desclared in Java so skip this one
1702 duplicate = true;
1703 break;
1704 }
1705 }
1706 if (duplicate) {
1707 // These will be removed from the field array at the end
1708 continue;
1709 }
1710 }
1711
1712 // Injected field
1713 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1714 field->initialize(JVM_ACC_FIELD_INTERNAL,
1715 injected[n].name_index,
1716 injected[n].signature_index,
1717 0);
1718
1719 const BasicType type = FieldType::basic_type(injected[n].signature());
1720
1721 // Remember how many oops we encountered and compute allocation type
1722 const FieldAllocationType atype = fac->update(false, type);
1723 field->set_allocation_type(atype);
1724 index++;
1725 }
1726 }
1727
1728 assert(NULL == _fields, "invariant");
1729
1730 _fields =
1731 MetadataFactory::new_array<u2>(_loader_data,
1732 index * FieldInfo::field_slots + num_generic_signature,
1733 CHECK);
1734 // Sometimes injected fields already exist in the Java source so
1735 // the fields array could be too long. In that case the
1736 // fields array is trimed. Also unused slots that were reserved
1737 // for generic signature indexes are discarded.
1738 {
1739 int i = 0;
1740 for (; i < index * FieldInfo::field_slots; i++) {
1741 _fields->at_put(i, fa[i]);
1742 }
1743 for (int j = total_fields * FieldInfo::field_slots;
1744 j < generic_signature_slot; j++) {
1745 _fields->at_put(i++, fa[j]);
1746 }
1747 assert(_fields->length() == i, "");
2331 runtime_visible_type_annotations_length,
2332 runtime_invisible_type_annotations,
2333 runtime_invisible_type_annotations_length,
2334 CHECK);
2335 cm->set_type_annotations(a);
2336 }
2337 }
2338
2339
2340 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2341 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2342 // Method* to save footprint, so we only know the size of the resulting Method* when the
2343 // entire method attribute is parsed.
2344 //
2345 // The promoted_flags parameter is used to pass relevant access_flags
2346 // from the method back up to the containing klass. These flag values
2347 // are added to klass's access_flags.
2348
2349 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2350 bool is_interface,
2351 const ConstantPool* cp,
2352 AccessFlags* const promoted_flags,
2353 TRAPS) {
2354 assert(cfs != NULL, "invariant");
2355 assert(cp != NULL, "invariant");
2356 assert(promoted_flags != NULL, "invariant");
2357
2358 ResourceMark rm(THREAD);
2359 // Parse fixed parts:
2360 // access_flags, name_index, descriptor_index, attributes_count
2361 cfs->guarantee_more(8, CHECK_NULL);
2362
2363 int flags = cfs->get_u2_fast();
2364 const u2 name_index = cfs->get_u2_fast();
2365 const int cp_size = cp->length();
2366 check_property(
2367 valid_symbol_at(name_index),
2368 "Illegal constant pool index %u for method name in class file %s",
2369 name_index, CHECK_NULL);
2370 const Symbol* const name = cp->symbol_at(name_index);
2371 verify_legal_method_name(name, CHECK_NULL);
2372
2373 const u2 signature_index = cfs->get_u2_fast();
2374 guarantee_property(
2375 valid_symbol_at(signature_index),
2376 "Illegal constant pool index %u for method signature in class file %s",
2377 signature_index, CHECK_NULL);
2378 const Symbol* const signature = cp->symbol_at(signature_index);
2379
2380 if (name == vmSymbols::class_initializer_name()) {
2381 // We ignore the other access flags for a valid class initializer.
2382 // (JVM Spec 2nd ed., chapter 4.6)
2383 if (_major_version < 51) { // backward compatibility
2384 flags = JVM_ACC_STATIC;
2385 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2386 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
2387 } else {
2388 classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL);
2389 }
2390 } else {
2391 verify_legal_method_modifiers(flags, is_interface, name, CHECK_NULL);
2392 }
2393
2394 if (name == vmSymbols::object_initializer_name() && is_interface) {
2395 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);
2396 }
2397
2398 int args_size = -1; // only used when _need_verify is true
2399 if (_need_verify) {
2400 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2401 verify_legal_method_signature(name, signature, CHECK_NULL);
2402 if (args_size > MAX_ARGS_SIZE) {
2403 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL);
2404 }
2405 }
2406
2407 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2408
2409 // Default values for code and exceptions attribute elements
2410 u2 max_stack = 0;
2411 u2 max_locals = 0;
2412 u4 code_length = 0;
2413 const u1* code_start = 0;
2414 u2 exception_table_length = 0;
2415 const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements
2946 _has_finalizer = true;
2947 }
2948 }
2949 if (name == vmSymbols::object_initializer_name() &&
2950 signature == vmSymbols::void_method_signature() &&
2951 m->is_vanilla_constructor()) {
2952 _has_vanilla_constructor = true;
2953 }
2954
2955 NOT_PRODUCT(m->verify());
2956 return m;
2957 }
2958
2959
2960 // The promoted_flags parameter is used to pass relevant access_flags
2961 // from the methods back up to the containing klass. These flag values
2962 // are added to klass's access_flags.
2963 // Side-effects: populates the _methods field in the parser
2964 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
2965 bool is_interface,
2966 AccessFlags* promoted_flags,
2967 bool* has_final_method,
2968 bool* declares_nonstatic_concrete_methods,
2969 TRAPS) {
2970 assert(cfs != NULL, "invariant");
2971 assert(promoted_flags != NULL, "invariant");
2972 assert(has_final_method != NULL, "invariant");
2973 assert(declares_nonstatic_concrete_methods != NULL, "invariant");
2974
2975 assert(NULL == _methods, "invariant");
2976
2977 cfs->guarantee_more(2, CHECK); // length
2978 const u2 length = cfs->get_u2_fast();
2979 if (length == 0) {
2980 _methods = Universe::the_empty_method_array();
2981 } else {
2982 _methods = MetadataFactory::new_array<Method*>(_loader_data,
2983 length,
2984 NULL,
2985 CHECK);
2986
2987 for (int index = 0; index < length; index++) {
2988 Method* method = parse_method(cfs,
2989 is_interface,
2990 _cp,
2991 promoted_flags,
2992 CHECK);
2993
2994 if (method->is_final()) {
2995 *has_final_method = true;
2996 }
2997 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
2998 // used for interface initialization, and default method inheritance analysis
2999 if (is_interface && !(*declares_nonstatic_concrete_methods)
3000 && !method->is_abstract() && !method->is_static()) {
3001 *declares_nonstatic_concrete_methods = true;
3002 }
3003 _methods->at_put(index, method);
3004 }
3005
3006 if (_need_verify && length > 1) {
3007 // Check duplicated methods
3008 ResourceMark rm(THREAD);
3009 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
3162 valid_klass_reference_at(inner_class_info_index),
3163 "inner_class_info_index %u has bad constant type in class file %s",
3164 inner_class_info_index, CHECK_0);
3165 // Outer class index
3166 const u2 outer_class_info_index = cfs->get_u2_fast();
3167 check_property(
3168 outer_class_info_index == 0 ||
3169 valid_klass_reference_at(outer_class_info_index),
3170 "outer_class_info_index %u has bad constant type in class file %s",
3171 outer_class_info_index, CHECK_0);
3172 // Inner class name
3173 const u2 inner_name_index = cfs->get_u2_fast();
3174 check_property(
3175 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3176 "inner_name_index %u has bad constant type in class file %s",
3177 inner_name_index, CHECK_0);
3178 if (_need_verify) {
3179 guarantee_property(inner_class_info_index != outer_class_info_index,
3180 "Class is both outer and inner class in class file %s", CHECK_0);
3181 }
3182 // Access flags
3183 jint flags;
3184 // JVM_ACC_MODULE is defined in JDK-9 and later.
3185 if (_major_version >= JAVA_9_VERSION) {
3186 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3187 } else {
3188 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3189 }
3190 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3191 // Set abstract bit for old class files for backward compatibility
3192 flags |= JVM_ACC_ABSTRACT;
3193 }
3194 verify_legal_class_modifiers(flags, CHECK_0);
3195 AccessFlags inner_access_flags(flags);
3196
3197 inner_classes->at_put(index++, inner_class_info_index);
3198 inner_classes->at_put(index++, outer_class_info_index);
3199 inner_classes->at_put(index++, inner_name_index);
3200 inner_classes->at_put(index++, inner_access_flags.as_short());
3201 }
3202
3203 // 4347400: make sure there's no duplicate entry in the classes array
3204 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3205 for(int i = 0; i < length * 4; i += 4) {
3206 for(int j = i + 4; j < length * 4; j += 4) {
3207 guarantee_property((inner_classes->at(i) != inner_classes->at(j) ||
3208 inner_classes->at(i+1) != inner_classes->at(j+1) ||
3209 inner_classes->at(i+2) != inner_classes->at(j+2) ||
3372 u2 attributes_count = cfs->get_u2_fast();
3373 bool parsed_sourcefile_attribute = false;
3374 bool parsed_innerclasses_attribute = false;
3375 bool parsed_nest_members_attribute = false;
3376 bool parsed_nest_host_attribute = false;
3377 bool parsed_enclosingmethod_attribute = false;
3378 bool parsed_bootstrap_methods_attribute = false;
3379 const u1* runtime_visible_annotations = NULL;
3380 int runtime_visible_annotations_length = 0;
3381 const u1* runtime_invisible_annotations = NULL;
3382 int runtime_invisible_annotations_length = 0;
3383 const u1* runtime_visible_type_annotations = NULL;
3384 int runtime_visible_type_annotations_length = 0;
3385 const u1* runtime_invisible_type_annotations = NULL;
3386 int runtime_invisible_type_annotations_length = 0;
3387 bool runtime_invisible_type_annotations_exists = false;
3388 bool runtime_invisible_annotations_exists = false;
3389 bool parsed_source_debug_ext_annotations_exist = false;
3390 const u1* inner_classes_attribute_start = NULL;
3391 u4 inner_classes_attribute_length = 0;
3392 u2 enclosing_method_class_index = 0;
3393 u2 enclosing_method_method_index = 0;
3394 const u1* nest_members_attribute_start = NULL;
3395 u4 nest_members_attribute_length = 0;
3396
3397 // Iterate over attributes
3398 while (attributes_count--) {
3399 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3400 const u2 attribute_name_index = cfs->get_u2_fast();
3401 const u4 attribute_length = cfs->get_u4_fast();
3402 check_property(
3403 valid_symbol_at(attribute_name_index),
3404 "Attribute name has bad constant pool index %u in class file %s",
3405 attribute_name_index, CHECK);
3406 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3407 if (tag == vmSymbols::tag_source_file()) {
3408 // Check for SourceFile tag
3409 if (_need_verify) {
3410 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3411 }
3721 }
3722 }
3723 if (runtime_invisible_annotations != NULL) {
3724 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3725 int append = runtime_visible_annotations_length+i;
3726 annotations->at_put(append, runtime_invisible_annotations[i]);
3727 }
3728 }
3729 }
3730 return annotations;
3731 }
3732
3733 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3734 const int super_class_index,
3735 const bool need_verify,
3736 TRAPS) {
3737 assert(cp != NULL, "invariant");
3738 const InstanceKlass* super_klass = NULL;
3739
3740 if (super_class_index == 0) {
3741 check_property(_class_name == vmSymbols::java_lang_Object(),
3742 "Invalid superclass index %u in class file %s",
3743 super_class_index,
3744 CHECK_NULL);
3745 } else {
3746 check_property(valid_klass_reference_at(super_class_index),
3747 "Invalid superclass index %u in class file %s",
3748 super_class_index,
3749 CHECK_NULL);
3750 // The class name should be legal because it is checked when parsing constant pool.
3751 // However, make sure it is not an array type.
3752 bool is_array = false;
3753 if (cp->tag_at(super_class_index).is_klass()) {
3754 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3755 if (need_verify)
3756 is_array = super_klass->is_array_klass();
3757 } else if (need_verify) {
3758 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3759 }
3760 if (need_verify) {
3761 guarantee_property(!is_array,
3762 "Bad superclass name in class file %s", CHECK_NULL);
3763 }
3764 }
3765 return super_klass;
3766 }
3767
3768 static unsigned int compute_oop_map_count(const InstanceKlass* super,
3769 unsigned int nonstatic_oop_map_count,
3770 int first_nonstatic_oop_offset) {
3771
3772 unsigned int map_count =
3773 NULL == super ? 0 : super->nonstatic_oop_map_count();
3774 if (nonstatic_oop_map_count > 0) {
3775 // We have oops to add to map
3776 if (map_count == 0) {
3777 map_count = nonstatic_oop_map_count;
3778 }
3779 else {
3780 // Check whether we should add a new map block or whether the last one can
3781 // be extended
3782 const OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps();
3783 const OopMapBlock* const last_map = first_map + map_count - 1;
3784
3785 const int next_offset = last_map->offset() + last_map->count() * heapOopSize;
3786 if (next_offset == first_nonstatic_oop_offset) {
3787 // There is no gap bettwen superklass's last oop field and first
3788 // local oop field, merge maps.
3789 nonstatic_oop_map_count -= 1;
3790 }
3791 else {
3792 // Superklass didn't end with a oop field, add extra maps
3793 assert(next_offset < first_nonstatic_oop_offset, "just checking");
3794 }
3795 map_count += nonstatic_oop_map_count;
3796 }
3797 }
3798 return map_count;
3799 }
3800
3801 #ifndef PRODUCT
3802 static void print_field_layout(const Symbol* name,
3803 Array<u2>* fields,
3804 const constantPoolHandle& cp,
3805 int instance_size,
3806 int instance_fields_start,
3807 int instance_fields_end,
3808 int static_fields_end) {
3809
3810 assert(name != NULL, "invariant");
3811
3812 tty->print("%s: field layout\n", name->as_klass_external_name());
3813 tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---");
3814 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3815 if (!fs.access_flags().is_static()) {
3816 tty->print(" @%3d \"%s\" %s\n",
3817 fs.offset(),
3818 fs.name()->as_klass_external_name(),
3819 fs.signature()->as_klass_external_name());
3820 }
3821 }
3822 tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3823 tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3824 tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3825 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3826 if (fs.access_flags().is_static()) {
3827 tty->print(" @%3d \"%s\" %s\n",
3828 fs.offset(),
3829 fs.name()->as_klass_external_name(),
3830 fs.signature()->as_klass_external_name());
3831 }
3832 }
3833 tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---");
3834 tty->print("\n");
3835 }
3836 #endif
3837
3838 // Values needed for oopmap and InstanceKlass creation
3839 class ClassFileParser::FieldLayoutInfo : public ResourceObj {
3840 public:
3841 int* nonstatic_oop_offsets;
3842 unsigned int* nonstatic_oop_counts;
3843 unsigned int nonstatic_oop_map_count;
3844 unsigned int total_oop_map_count;
3845 int instance_size;
3846 int nonstatic_field_size;
3847 int static_field_size;
3848 bool has_nonstatic_fields;
3849 };
3850
3851 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring!
3852 void ClassFileParser::layout_fields(ConstantPool* cp,
3853 const FieldAllocationCount* fac,
3854 const ClassAnnotationCollector* parsed_annotations,
3855 FieldLayoutInfo* info,
3856 TRAPS) {
3857
3858 assert(cp != NULL, "invariant");
3859
3860 // Field size and offset computation
3861 int nonstatic_field_size = _super_klass == NULL ? 0 :
3862 _super_klass->nonstatic_field_size();
3863
3864 // Count the contended fields by type.
3865 //
3866 // We ignore static fields, because @Contended is not supported for them.
3867 // The layout code below will also ignore the static fields.
3868 int nonstatic_contended_count = 0;
3869 FieldAllocationCount fac_contended;
3870 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
3871 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
3872 if (fs.is_contended()) {
3873 fac_contended.count[atype]++;
3874 if (!fs.access_flags().is_static()) {
3875 nonstatic_contended_count++;
3876 }
3877 }
3878 }
3879
3880
3881 // Calculate the starting byte offsets
3882 int next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields();
3883 int next_static_double_offset = next_static_oop_offset +
3884 ((fac->count[STATIC_OOP]) * heapOopSize);
3885 if (fac->count[STATIC_DOUBLE]) {
3886 next_static_double_offset = align_up(next_static_double_offset, BytesPerLong);
3887 }
3888
3889 int next_static_word_offset = next_static_double_offset +
3890 ((fac->count[STATIC_DOUBLE]) * BytesPerLong);
3891 int next_static_short_offset = next_static_word_offset +
3892 ((fac->count[STATIC_WORD]) * BytesPerInt);
3893 int next_static_byte_offset = next_static_short_offset +
3894 ((fac->count[STATIC_SHORT]) * BytesPerShort);
3895
3896 int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() +
3897 nonstatic_field_size * heapOopSize;
3898
3899 int next_nonstatic_field_offset = nonstatic_fields_start;
3900
3901 const bool is_contended_class = parsed_annotations->is_contended();
3902
3903 // Class is contended, pad before all the fields
3904 if (is_contended_class) {
3905 next_nonstatic_field_offset += ContendedPaddingWidth;
3906 }
3907
3908 // Compute the non-contended fields count.
3909 // The packing code below relies on these counts to determine if some field
3910 // can be squeezed into the alignment gap. Contended fields are obviously
3911 // exempt from that.
3912 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
3913 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD];
3914 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT];
3915 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE];
3916 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP];
3917
3918 // Total non-static fields count, including every contended field
3919 unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] +
3920 fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] +
3921 fac->count[NONSTATIC_OOP];
3922
3923 const bool super_has_nonstatic_fields =
3924 (_super_klass != NULL && _super_klass->has_nonstatic_fields());
3925 const bool has_nonstatic_fields =
3926 super_has_nonstatic_fields || (nonstatic_fields_count != 0);
3927
3928
3929 // Prepare list of oops for oop map generation.
3930 //
3931 // "offset" and "count" lists are describing the set of contiguous oop
3932 // regions. offset[i] is the start of the i-th region, which then has
3933 // count[i] oops following. Before we know how many regions are required,
3934 // we pessimistically allocate the maps to fit all the oops into the
3935 // distinct regions.
3936 //
3937 // TODO: We add +1 to always allocate non-zero resource arrays; we need
3938 // to figure out if we still need to do this.
3939 unsigned int nonstatic_oop_map_count = 0;
3940 unsigned int max_nonstatic_oop_maps = fac->count[NONSTATIC_OOP] + 1;
3941
3942 int* nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3943 THREAD, int, max_nonstatic_oop_maps);
3944 unsigned int* const nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3945 THREAD, unsigned int, max_nonstatic_oop_maps);
3946
3947 int first_nonstatic_oop_offset = 0; // will be set for first oop field
3948
3949 bool compact_fields = CompactFields;
3950 int allocation_style = FieldsAllocationStyle;
3951 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3952 assert(false, "0 <= FieldsAllocationStyle <= 2");
3953 allocation_style = 1; // Optimistic
3954 }
3955
3956 // The next classes have predefined hard-coded fields offsets
3957 // (see in JavaClasses::compute_hard_coded_offsets()).
3958 // Use default fields allocation order for them.
3959 if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL &&
3960 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3961 _class_name == vmSymbols::java_lang_Class() ||
3962 _class_name == vmSymbols::java_lang_ClassLoader() ||
3963 _class_name == vmSymbols::java_lang_ref_Reference() ||
3964 _class_name == vmSymbols::java_lang_ref_SoftReference() ||
3965 _class_name == vmSymbols::java_lang_StackTraceElement() ||
3974 _class_name == vmSymbols::java_lang_Integer() ||
3975 _class_name == vmSymbols::java_lang_Long())) {
3976 allocation_style = 0; // Allocate oops first
3977 compact_fields = false; // Don't compact fields
3978 }
3979
3980 int next_nonstatic_oop_offset = 0;
3981 int next_nonstatic_double_offset = 0;
3982
3983 // Rearrange fields for a given allocation style
3984 if( allocation_style == 0 ) {
3985 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
3986 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3987 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3988 (nonstatic_oop_count * heapOopSize);
3989 } else if( allocation_style == 1 ) {
3990 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
3991 next_nonstatic_double_offset = next_nonstatic_field_offset;
3992 } else if( allocation_style == 2 ) {
3993 // Fields allocation: oops fields in super and sub classes are together.
3994 if( nonstatic_field_size > 0 && _super_klass != NULL &&
3995 _super_klass->nonstatic_oop_map_size() > 0 ) {
3996 const unsigned int map_count = _super_klass->nonstatic_oop_map_count();
3997 const OopMapBlock* const first_map = _super_klass->start_of_nonstatic_oop_maps();
3998 const OopMapBlock* const last_map = first_map + map_count - 1;
3999 const int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
4000 if (next_offset == next_nonstatic_field_offset) {
4001 allocation_style = 0; // allocate oops first
4002 next_nonstatic_oop_offset = next_nonstatic_field_offset;
4003 next_nonstatic_double_offset = next_nonstatic_oop_offset +
4004 (nonstatic_oop_count * heapOopSize);
4005 }
4006 }
4007 if( allocation_style == 2 ) {
4008 allocation_style = 1; // allocate oops last
4009 next_nonstatic_double_offset = next_nonstatic_field_offset;
4010 }
4011 } else {
4012 ShouldNotReachHere();
4013 }
4014
4015 int nonstatic_oop_space_count = 0;
4016 int nonstatic_word_space_count = 0;
4017 int nonstatic_short_space_count = 0;
4018 int nonstatic_byte_space_count = 0;
4019 int nonstatic_oop_space_offset = 0;
4020 int nonstatic_word_space_offset = 0;
4063 }
4064
4065 int next_nonstatic_word_offset = next_nonstatic_double_offset +
4066 (nonstatic_double_count * BytesPerLong);
4067 int next_nonstatic_short_offset = next_nonstatic_word_offset +
4068 (nonstatic_word_count * BytesPerInt);
4069 int next_nonstatic_byte_offset = next_nonstatic_short_offset +
4070 (nonstatic_short_count * BytesPerShort);
4071 int next_nonstatic_padded_offset = next_nonstatic_byte_offset +
4072 nonstatic_byte_count;
4073
4074 // let oops jump before padding with this allocation style
4075 if( allocation_style == 1 ) {
4076 next_nonstatic_oop_offset = next_nonstatic_padded_offset;
4077 if( nonstatic_oop_count > 0 ) {
4078 next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize);
4079 }
4080 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
4081 }
4082
4083 // Iterate over fields again and compute correct offsets.
4084 // The field allocation type was temporarily stored in the offset slot.
4085 // oop fields are located before non-oop fields (static and non-static).
4086 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4087
4088 // skip already laid out fields
4089 if (fs.is_offset_set()) continue;
4090
4091 // contended instance fields are handled below
4092 if (fs.is_contended() && !fs.access_flags().is_static()) continue;
4093
4094 int real_offset = 0;
4095 const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type();
4096
4097 // pack the rest of the fields
4098 switch (atype) {
4099 case STATIC_OOP:
4100 real_offset = next_static_oop_offset;
4101 next_static_oop_offset += heapOopSize;
4102 break;
4103 case STATIC_BYTE:
4104 real_offset = next_static_byte_offset;
4105 next_static_byte_offset += 1;
4106 break;
4107 case STATIC_SHORT:
4108 real_offset = next_static_short_offset;
4109 next_static_short_offset += BytesPerShort;
4110 break;
4111 case STATIC_WORD:
4112 real_offset = next_static_word_offset;
4113 next_static_word_offset += BytesPerInt;
4114 break;
4115 case STATIC_DOUBLE:
4116 real_offset = next_static_double_offset;
4117 next_static_double_offset += BytesPerLong;
4118 break;
4119 case NONSTATIC_OOP:
4120 if( nonstatic_oop_space_count > 0 ) {
4121 real_offset = nonstatic_oop_space_offset;
4122 nonstatic_oop_space_offset += heapOopSize;
4123 nonstatic_oop_space_count -= 1;
4124 } else {
4125 real_offset = next_nonstatic_oop_offset;
4126 next_nonstatic_oop_offset += heapOopSize;
4127 }
4128
4129 // Record this oop in the oop maps
4130 if( nonstatic_oop_map_count > 0 &&
4131 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
4132 real_offset -
4133 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
4134 heapOopSize ) {
4135 // This oop is adjacent to the previous one, add to current oop map
4136 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check");
4137 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
4138 } else {
4139 // This oop is not adjacent to the previous one, create new oop map
4140 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check");
4141 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
4142 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
4143 nonstatic_oop_map_count += 1;
4144 if( first_nonstatic_oop_offset == 0 ) { // Undefined
4145 first_nonstatic_oop_offset = real_offset;
4146 }
4147 }
4148 break;
4149 case NONSTATIC_BYTE:
4150 if( nonstatic_byte_space_count > 0 ) {
4151 real_offset = nonstatic_byte_space_offset;
4152 nonstatic_byte_space_offset += 1;
4153 nonstatic_byte_space_count -= 1;
4154 } else {
4155 real_offset = next_nonstatic_byte_offset;
4156 next_nonstatic_byte_offset += 1;
4157 }
4158 break;
4159 case NONSTATIC_SHORT:
4160 if( nonstatic_short_space_count > 0 ) {
4161 real_offset = nonstatic_short_space_offset;
4162 nonstatic_short_space_offset += BytesPerShort;
4163 nonstatic_short_space_count -= 1;
4164 } else {
4165 real_offset = next_nonstatic_short_offset;
4166 next_nonstatic_short_offset += BytesPerShort;
4167 }
4236 break;
4237
4238 case NONSTATIC_SHORT:
4239 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort);
4240 real_offset = next_nonstatic_padded_offset;
4241 next_nonstatic_padded_offset += BytesPerShort;
4242 break;
4243
4244 case NONSTATIC_WORD:
4245 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt);
4246 real_offset = next_nonstatic_padded_offset;
4247 next_nonstatic_padded_offset += BytesPerInt;
4248 break;
4249
4250 case NONSTATIC_DOUBLE:
4251 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4252 real_offset = next_nonstatic_padded_offset;
4253 next_nonstatic_padded_offset += BytesPerLong;
4254 break;
4255
4256 case NONSTATIC_OOP:
4257 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize);
4258 real_offset = next_nonstatic_padded_offset;
4259 next_nonstatic_padded_offset += heapOopSize;
4260
4261 // Record this oop in the oop maps
4262 if( nonstatic_oop_map_count > 0 &&
4263 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
4264 real_offset -
4265 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
4266 heapOopSize ) {
4267 // This oop is adjacent to the previous one, add to current oop map
4268 assert(nonstatic_oop_map_count - 1 < max_nonstatic_oop_maps, "range check");
4269 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
4270 } else {
4271 // This oop is not adjacent to the previous one, create new oop map
4272 assert(nonstatic_oop_map_count < max_nonstatic_oop_maps, "range check");
4273 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
4274 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
4275 nonstatic_oop_map_count += 1;
4276 if( first_nonstatic_oop_offset == 0 ) { // Undefined
4277 first_nonstatic_oop_offset = real_offset;
4278 }
4279 }
4280 break;
4281
4282 default:
4283 ShouldNotReachHere();
4284 }
4285
4286 if (fs.contended_group() == 0) {
4287 // Contended group defines the equivalence class over the fields:
4288 // the fields within the same contended group are not inter-padded.
4289 // The only exception is default group, which does not incur the
4290 // equivalence, and so requires intra-padding.
4291 next_nonstatic_padded_offset += ContendedPaddingWidth;
4292 }
4293
4294 fs.set_offset(real_offset);
4295 } // for
4296
4297 // Start laying out the next group.
4298 // Note that this will effectively pad the last group in the back;
4299 // this is expected to alleviate memory contention effects for
4300 // subclass fields and/or adjacent object.
4301 // If this was the default group, the padding is already in place.
4302 if (current_group != 0) {
4303 next_nonstatic_padded_offset += ContendedPaddingWidth;
4304 }
4305 }
4306
4307 // handle static fields
4308 }
4309
4310 // Entire class is contended, pad in the back.
4311 // This helps to alleviate memory contention effects for subclass fields
4312 // and/or adjacent object.
4313 if (is_contended_class) {
4314 next_nonstatic_padded_offset += ContendedPaddingWidth;
4315 }
4316
4317 int notaligned_nonstatic_fields_end = next_nonstatic_padded_offset;
4318
4319 int nonstatic_fields_end = align_up(notaligned_nonstatic_fields_end, heapOopSize);
4320 int instance_end = align_up(notaligned_nonstatic_fields_end, wordSize);
4321 int static_fields_end = align_up(next_static_byte_offset, wordSize);
4322
4323 int static_field_size = (static_fields_end -
4324 InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
4325 nonstatic_field_size = nonstatic_field_size +
4326 (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize;
4327
4328 int instance_size = align_object_size(instance_end / wordSize);
4329
4330 assert(instance_size == align_object_size(align_up(
4331 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize),
4332 wordSize) / wordSize), "consistent layout helper value");
4333
4334 // Invariant: nonstatic_field end/start should only change if there are
4335 // nonstatic fields in the class, or if the class is contended. We compare
4336 // against the non-aligned value, so that end alignment will not fail the
4337 // assert without actually having the fields.
4338 assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4339 is_contended_class ||
4340 (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4341
4342 // Number of non-static oop map blocks allocated at end of klass.
4343 const unsigned int total_oop_map_count =
4344 compute_oop_map_count(_super_klass, nonstatic_oop_map_count,
4345 first_nonstatic_oop_offset);
4346
4347 #ifndef PRODUCT
4348 if (PrintFieldLayout) {
4349 print_field_layout(_class_name,
4350 _fields,
4351 cp,
4352 instance_size,
4353 nonstatic_fields_start,
4354 nonstatic_fields_end,
4355 static_fields_end);
4356 }
4357
4358 #endif
4359 // Pass back information needed for InstanceKlass creation
4360 info->nonstatic_oop_offsets = nonstatic_oop_offsets;
4361 info->nonstatic_oop_counts = nonstatic_oop_counts;
4362 info->nonstatic_oop_map_count = nonstatic_oop_map_count;
4363 info->total_oop_map_count = total_oop_map_count;
4364 info->instance_size = instance_size;
4365 info->static_field_size = static_field_size;
4366 info->nonstatic_field_size = nonstatic_field_size;
4367 info->has_nonstatic_fields = has_nonstatic_fields;
4368 }
4369
4370 static void fill_oop_maps(const InstanceKlass* k,
4371 unsigned int nonstatic_oop_map_count,
4372 const int* nonstatic_oop_offsets,
4373 const unsigned int* nonstatic_oop_counts) {
4374
4375 assert(k != NULL, "invariant");
4376
4377 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
4378 const InstanceKlass* const super = k->superklass();
4379 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0;
4380 if (super_count > 0) {
4381 // Copy maps from superklass
4382 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps();
4383 for (unsigned int i = 0; i < super_count; ++i) {
4384 *this_oop_map++ = *super_oop_map++;
4385 }
4386 }
4387
4388 if (nonstatic_oop_map_count > 0) {
4389 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) {
4390 // The counts differ because there is no gap between superklass's last oop
4391 // field and the first local oop field. Extend the last oop map copied
4392 // from the superklass instead of creating new one.
4393 nonstatic_oop_map_count--;
4394 nonstatic_oop_offsets++;
4395 this_oop_map--;
4396 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++);
4397 this_oop_map++;
4398 }
4399
4400 // Add new map blocks, fill them
4401 while (nonstatic_oop_map_count-- > 0) {
4402 this_oop_map->set_offset(*nonstatic_oop_offsets++);
4403 this_oop_map->set_count(*nonstatic_oop_counts++);
4404 this_oop_map++;
4405 }
4406 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() ==
4407 this_oop_map, "sanity");
4408 }
4409 }
4410
4411
4412 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik) {
4413 assert(ik != NULL, "invariant");
4414
4415 const Klass* const super = ik->super();
4416
4417 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4418 // in which case we don't have to register objects as finalizable
4419 if (!_has_empty_finalizer) {
4420 if (_has_finalizer ||
4421 (super != NULL && super->has_finalizer())) {
4422 ik->set_has_finalizer();
4423 }
4424 }
4425
4426 #ifdef ASSERT
4427 bool f = false;
4428 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4429 vmSymbols::void_method_signature());
4430 if (m != NULL && !m->is_empty_method()) {
4431 f = true;
4432 }
4433
4434 // Spec doesn't prevent agent from redefinition of empty finalizer.
4435 // Despite the fact that it's generally bad idea and redefined finalizer
4436 // will not work as expected we shouldn't abort vm in this case
4437 if (!ik->has_redefined_this_or_super()) {
4438 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4439 }
4440 #endif
4441
4442 // Check if this klass supports the java.lang.Cloneable interface
4443 if (SystemDictionary::Cloneable_klass_loaded()) {
4444 if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) {
4445 ik->set_is_cloneable();
4446 }
4447 }
4448
4449 // Check if this klass has a vanilla default constructor
4450 if (super == NULL) {
4451 // java.lang.Object has empty default constructor
4452 ik->set_has_vanilla_constructor();
4453 } else {
4454 if (super->has_vanilla_constructor() &&
4455 _has_vanilla_constructor) {
4456 ik->set_has_vanilla_constructor();
4457 }
4458 #ifdef ASSERT
4459 bool v = false;
4460 if (super->has_vanilla_constructor()) {
4461 const Method* const constructor =
4462 ik->find_method(vmSymbols::object_initializer_name(),
4463 vmSymbols::void_method_signature());
4464 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4465 v = true;
4466 }
4467 }
4468 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4469 #endif
4470 }
4471
4472 // If it cannot be fast-path allocated, set a bit in the layout helper.
4473 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4474 assert(ik->size_helper() > 0, "layout_helper is initialized");
4475 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4476 || ik->is_abstract() || ik->is_interface()
4477 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4478 || ik->size_helper() >= FastAllocateSizeLimit) {
4479 // Forbid fast-path allocation.
4480 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4481 ik->set_layout_helper(lh);
4482 }
4483 }
4484
4485 // utility methods for appending an array with check for duplicates
4486
4487 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4488 const Array<InstanceKlass*>* const ifs) {
4489 // iterate over new interfaces
4490 for (int i = 0; i < ifs->length(); i++) {
4491 InstanceKlass* const e = ifs->at(i);
4492 assert(e->is_klass() && e->is_interface(), "just checking");
4493 // add new interface
4494 result->append_if_missing(e);
4495 }
4496 }
4497
4498 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4499 Array<InstanceKlass*>* local_ifs,
4500 ClassLoaderData* loader_data,
4501 TRAPS) {
4502 assert(local_ifs != NULL, "invariant");
4503 assert(loader_data != NULL, "invariant");
4504
4727 const Method* const m = methods->at(index);
4728 // if m is static and not the init method, throw a verify error
4729 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4730 ResourceMark rm(THREAD);
4731 Exceptions::fthrow(
4732 THREAD_AND_LOCATION,
4733 vmSymbols::java_lang_VerifyError(),
4734 "Illegal static method %s in interface %s",
4735 m->name()->as_C_string(),
4736 this_klass->external_name()
4737 );
4738 return;
4739 }
4740 }
4741 }
4742
4743 // utility methods for format checking
4744
4745 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4746 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4747 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4748 if (is_module) {
4749 ResourceMark rm(THREAD);
4750 Exceptions::fthrow(
4751 THREAD_AND_LOCATION,
4752 vmSymbols::java_lang_NoClassDefFoundError(),
4753 "%s is not a class because access_flag ACC_MODULE is set",
4754 _class_name->as_C_string());
4755 return;
4756 }
4757
4758 if (!_need_verify) { return; }
4759
4760 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4761 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4762 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4763 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4764 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4765 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4766 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4767
4768 if ((is_abstract && is_final) ||
4769 (is_interface && !is_abstract) ||
4770 (is_interface && major_gte_15 && (is_super || is_enum)) ||
4771 (!is_interface && major_gte_15 && is_annotation)) {
4772 ResourceMark rm(THREAD);
4773 Exceptions::fthrow(
4774 THREAD_AND_LOCATION,
4775 vmSymbols::java_lang_ClassFormatError(),
4776 "Illegal class modifiers in class %s: 0x%X",
4777 _class_name->as_C_string(), flags
4778 );
4779 return;
4780 }
4781 }
4782
4783 static bool has_illegal_visibility(jint flags) {
4784 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4785 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4786 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4787
4788 return ((is_public && is_protected) ||
4789 (is_public && is_private) ||
4790 (is_protected && is_private));
4791 }
4834 ResourceMark rm(THREAD);
4835 Exceptions::fthrow(
4836 THREAD_AND_LOCATION,
4837 vmSymbols::java_lang_UnsupportedClassVersionError(),
4838 "%s (class file version %u.%u) was compiled with an invalid major version",
4839 class_name->as_C_string(), major, minor);
4840 } else if (minor != 0) {
4841 ResourceMark rm(THREAD);
4842 Exceptions::fthrow(
4843 THREAD_AND_LOCATION,
4844 vmSymbols::java_lang_UnsupportedClassVersionError(),
4845 "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
4846 class_name->as_C_string(), major, minor);
4847 }
4848 }
4849 }
4850 }
4851
4852 void ClassFileParser::verify_legal_field_modifiers(jint flags,
4853 bool is_interface,
4854 TRAPS) const {
4855 if (!_need_verify) { return; }
4856
4857 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4858 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4859 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4860 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4861 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4862 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4863 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4864 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4865 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4866
4867 bool is_illegal = false;
4868
4869 if (is_interface) {
4870 if (!is_public || !is_static || !is_final || is_private ||
4871 is_protected || is_volatile || is_transient ||
4872 (major_gte_15 && is_enum)) {
4873 is_illegal = true;
4874 }
4875 } else { // not interface
4876 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4877 is_illegal = true;
4878 }
4879 }
4880
4881 if (is_illegal) {
4882 ResourceMark rm(THREAD);
4883 Exceptions::fthrow(
4884 THREAD_AND_LOCATION,
4885 vmSymbols::java_lang_ClassFormatError(),
4886 "Illegal field modifiers in class %s: 0x%X",
4887 _class_name->as_C_string(), flags);
4888 return;
4889 }
4890 }
4891
4892 void ClassFileParser::verify_legal_method_modifiers(jint flags,
4893 bool is_interface,
4894 const Symbol* name,
4895 TRAPS) const {
4896 if (!_need_verify) { return; }
4897
4898 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4899 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4900 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4901 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4902 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4903 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4904 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4905 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4906 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4907 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4908 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4909 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
4910 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4911
4912 bool is_illegal = false;
4913
4933 if (!is_public || is_private || is_protected || is_static || is_final ||
4934 is_synchronized || is_native || !is_abstract || is_strict) {
4935 is_illegal = true;
4936 }
4937 } else {
4938 // Class file version is pre-JAVA_1_5_VERSION
4939 if (!is_public || is_static || is_final || is_native || !is_abstract) {
4940 is_illegal = true;
4941 }
4942 }
4943 } else { // not interface
4944 if (has_illegal_visibility(flags)) {
4945 is_illegal = true;
4946 } else {
4947 if (is_initializer) {
4948 if (is_static || is_final || is_synchronized || is_native ||
4949 is_abstract || (major_gte_15 && is_bridge)) {
4950 is_illegal = true;
4951 }
4952 } else { // not initializer
4953 if (is_abstract) {
4954 if ((is_final || is_native || is_private || is_static ||
4955 (major_gte_15 && (is_synchronized || is_strict)))) {
4956 is_illegal = true;
4957 }
4958 }
4959 }
4960 }
4961 }
4962
4963 if (is_illegal) {
4964 ResourceMark rm(THREAD);
4965 Exceptions::fthrow(
4966 THREAD_AND_LOCATION,
4967 vmSymbols::java_lang_ClassFormatError(),
4968 "Method %s in class %s has illegal modifiers: 0x%X",
4969 name->as_C_string(), _class_name->as_C_string(), flags);
4970 return;
4971 }
4972 }
4973
4974 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
4975 int length,
4976 TRAPS) const {
4977 assert(_need_verify, "only called when _need_verify is true");
4978 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
4979 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4980 }
4981 }
5105 // be taken as a field signature. Allow "void" if void_ok.
5106 // Return a pointer to just past the signature.
5107 // Return NULL if no legal signature is found.
5108 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5109 bool void_ok,
5110 unsigned int length,
5111 TRAPS) const {
5112 unsigned int array_dim = 0;
5113 while (length > 0) {
5114 switch (signature[0]) {
5115 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5116 case JVM_SIGNATURE_BOOLEAN:
5117 case JVM_SIGNATURE_BYTE:
5118 case JVM_SIGNATURE_CHAR:
5119 case JVM_SIGNATURE_SHORT:
5120 case JVM_SIGNATURE_INT:
5121 case JVM_SIGNATURE_FLOAT:
5122 case JVM_SIGNATURE_LONG:
5123 case JVM_SIGNATURE_DOUBLE:
5124 return signature + 1;
5125 case JVM_SIGNATURE_CLASS: {
5126 if (_major_version < JAVA_1_5_VERSION) {
5127 // Skip over the class name if one is there
5128 const char* const p = skip_over_field_name(signature + 1, true, --length);
5129
5130 // The next character better be a semicolon
5131 if (p && (p - signature) > 1 && p[0] == ';') {
5132 return p + 1;
5133 }
5134 }
5135 else {
5136 // Skip leading 'L' and ignore first appearance of ';'
5137 signature++;
5138 const char* c = (const char*) memchr(signature, ';', length - 1);
5139 // Format check signature
5140 if (c != NULL) {
5141 int newlen = c - (char*) signature;
5142 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5143 if (!legal) {
5144 classfile_parse_error("Class name contains illegal character "
5145 "in descriptor in class file %s",
5146 CHECK_0);
5147 return NULL;
5148 }
5149 return signature + newlen + 1;
5150 }
5151 }
5152 return NULL;
5153 }
5154 case JVM_SIGNATURE_ARRAY:
5155 array_dim++;
5156 if (array_dim > 255) {
5171
5172 // Checks if name is a legal class name.
5173 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5174 if (!_need_verify || _relax_verify) { return; }
5175
5176 assert(name->refcount() > 0, "symbol must be kept alive");
5177 char* bytes = (char*)name->bytes();
5178 unsigned int length = name->utf8_length();
5179 bool legal = false;
5180
5181 if (length > 0) {
5182 const char* p;
5183 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5184 p = skip_over_field_signature(bytes, false, length, CHECK);
5185 legal = (p != NULL) && ((p - bytes) == (int)length);
5186 } else if (_major_version < JAVA_1_5_VERSION) {
5187 if (bytes[0] != '<') {
5188 p = skip_over_field_name(bytes, true, length);
5189 legal = (p != NULL) && ((p - bytes) == (int)length);
5190 }
5191 } else {
5192 // 4900761: relax the constraints based on JSR202 spec
5193 // Class names may be drawn from the entire Unicode character set.
5194 // Identifiers between '/' must be unqualified names.
5195 // The utf8 string has been verified when parsing cpool entries.
5196 legal = verify_unqualified_name(bytes, length, LegalClass);
5197 }
5198 }
5199 if (!legal) {
5200 ResourceMark rm(THREAD);
5201 assert(_class_name != NULL, "invariant");
5202 Exceptions::fthrow(
5203 THREAD_AND_LOCATION,
5204 vmSymbols::java_lang_ClassFormatError(),
5205 "Illegal class name \"%.*s\" in class file %s", length, bytes,
5206 _class_name->as_C_string()
5207 );
5208 return;
5209 }
5210 }
5345 // Now we better just have a return value
5346 nextp = skip_over_field_signature(p, true, length, CHECK_0);
5347 if (nextp && ((int)length == (nextp - p))) {
5348 return args_size;
5349 }
5350 }
5351 }
5352 }
5353 // Report error
5354 throwIllegalSignature("Method", name, signature, CHECK_0);
5355 return 0;
5356 }
5357
5358 int ClassFileParser::static_field_size() const {
5359 assert(_field_info != NULL, "invariant");
5360 return _field_info->static_field_size;
5361 }
5362
5363 int ClassFileParser::total_oop_map_count() const {
5364 assert(_field_info != NULL, "invariant");
5365 return _field_info->total_oop_map_count;
5366 }
5367
5368 jint ClassFileParser::layout_size() const {
5369 assert(_field_info != NULL, "invariant");
5370 return _field_info->instance_size;
5371 }
5372
5373 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5374 const Array<Method*>* methods) {
5375 assert(ik != NULL, "invariant");
5376 assert(methods != NULL, "invariant");
5377
5378 // Set up Method*::intrinsic_id as soon as we know the names of methods.
5379 // (We used to do this lazily, but now we query it in Rewriter,
5380 // which is eagerly done for every method, so we might as well do it now,
5381 // when everything is fresh in memory.)
5382 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik);
5383
5384 if (klass_id != vmSymbols::NO_SID) {
5385 for (int j = 0; j < methods->length(); ++j) {
5475
5476
5477 if (ik->should_store_fingerprint()) {
5478 ik->store_fingerprint(_stream->compute_fingerprint());
5479 }
5480
5481 ik->set_has_passed_fingerprint_check(false);
5482 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5483 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5484 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5485 if (aot_fp != 0 && aot_fp == fp) {
5486 // This class matches with a class saved in an AOT library
5487 ik->set_has_passed_fingerprint_check(true);
5488 } else {
5489 ResourceMark rm;
5490 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5491 ik->external_name(), aot_fp, _stream->compute_fingerprint());
5492 }
5493 }
5494
5495 return ik;
5496 }
5497
5498 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {
5499 assert(ik != NULL, "invariant");
5500
5501 // Set name and CLD before adding to CLD
5502 ik->set_class_loader_data(_loader_data);
5503 ik->set_name(_class_name);
5504
5505 // Add all classes to our internal class loader list here,
5506 // including classes in the bootstrap (NULL) class loader.
5507 const bool publicize = !is_internal();
5508
5509 _loader_data->add_class(ik, publicize);
5510
5511 set_klass_to_deallocate(ik);
5512
5513 assert(_field_info != NULL, "invariant");
5514 assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5515 assert(ik->nonstatic_oop_map_count() == _field_info->total_oop_map_count,
5516 "sanity");
5517
5518 assert(ik->is_instance_klass(), "sanity");
5519 assert(ik->size_helper() == _field_info->instance_size, "sanity");
5520
5521 // Fill in information already parsed
5522 ik->set_should_verify_class(_need_verify);
5523
5524 // Not yet: supers are done below to support the new subtype-checking fields
5525 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5526 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5527 assert(_fac != NULL, "invariant");
5528 ik->set_static_oop_field_count(_fac->count[STATIC_OOP]);
5529
5530 // this transfers ownership of a lot of arrays from
5531 // the parser onto the InstanceKlass*
5532 apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5533
5534 // note that is not safe to use the fields in the parser from this point on
5535 assert(NULL == _cp, "invariant");
5536 assert(NULL == _fields, "invariant");
5537 assert(NULL == _methods, "invariant");
5538 assert(NULL == _inner_classes, "invariant");
5539 assert(NULL == _nest_members, "invariant");
5540 assert(NULL == _local_interfaces, "invariant");
5541 assert(NULL == _combined_annotations, "invariant");
5542
5543 if (_has_final_method) {
5544 ik->set_has_final_method();
5545 }
5546
5547 ik->copy_method_ordering(_method_ordering, CHECK);
5548 // The InstanceKlass::_methods_jmethod_ids cache
5596
5597 // Miranda methods
5598 if ((_num_miranda_methods > 0) ||
5599 // if this class introduced new miranda methods or
5600 (_super_klass != NULL && _super_klass->has_miranda_methods())
5601 // super class exists and this class inherited miranda methods
5602 ) {
5603 ik->set_has_miranda_methods(); // then set a flag
5604 }
5605
5606 // Fill in information needed to compute superclasses.
5607 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5608 ik->set_transitive_interfaces(_transitive_interfaces);
5609 _transitive_interfaces = NULL;
5610
5611 // Initialize itable offset tables
5612 klassItable::setup_itable_offset_table(ik);
5613
5614 // Compute transitive closure of interfaces this class implements
5615 // Do final class setup
5616 fill_oop_maps(ik,
5617 _field_info->nonstatic_oop_map_count,
5618 _field_info->nonstatic_oop_offsets,
5619 _field_info->nonstatic_oop_counts);
5620
5621 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5622 set_precomputed_flags(ik);
5623
5624 // check if this class can access its super class
5625 check_super_class_access(ik, CHECK);
5626
5627 // check if this class can access its superinterfaces
5628 check_super_interface_access(ik, CHECK);
5629
5630 // check if this class overrides any final method
5631 check_final_method_override(ik, CHECK);
5632
5633 // reject static interface methods prior to Java 8
5634 if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5635 check_illegal_static_method(ik, CHECK);
5636 }
5637
5638 // Obtain this_klass' module entry
5639 ModuleEntry* module_entry = ik->module();
5640 assert(module_entry != NULL, "module_entry should always be set");
5641
5642 // Obtain java.lang.Module
5652
5653 assert(_all_mirandas != NULL, "invariant");
5654
5655 // Generate any default methods - default methods are public interface methods
5656 // that have a default implementation. This is new with Java 8.
5657 if (_has_nonstatic_concrete_methods) {
5658 DefaultMethods::generate_default_methods(ik,
5659 _all_mirandas,
5660 CHECK);
5661 }
5662
5663 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5664 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5665 !module_entry->has_default_read_edges()) {
5666 if (!module_entry->set_has_default_read_edges()) {
5667 // We won a potential race
5668 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5669 }
5670 }
5671
5672 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5673
5674 if (!is_internal()) {
5675 if (log_is_enabled(Info, class, load)) {
5676 ResourceMark rm;
5677 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5678 ik->print_class_load_logging(_loader_data, module_name, _stream);
5679 }
5680
5681 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5682 ik->major_version() != JAVA_MIN_SUPPORTED_VERSION &&
5683 log_is_enabled(Info, class, preview)) {
5684 ResourceMark rm;
5685 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5686 ik->external_name(), ik->major_version());
5687 }
5688
5689 if (log_is_enabled(Debug, class, resolve)) {
5690 ResourceMark rm;
5691 // print out the superclass.
5841 _protection_domain(protection_domain),
5842 _access_flags(),
5843 _pub_level(pub_level),
5844 _bad_constant_seen(0),
5845 _synthetic_flag(false),
5846 _sde_length(false),
5847 _sde_buffer(NULL),
5848 _sourcefile_index(0),
5849 _generic_signature_index(0),
5850 _major_version(0),
5851 _minor_version(0),
5852 _this_class_index(0),
5853 _super_class_index(0),
5854 _itfs_len(0),
5855 _java_fields_count(0),
5856 _need_verify(false),
5857 _relax_verify(false),
5858 _has_nonstatic_concrete_methods(false),
5859 _declares_nonstatic_concrete_methods(false),
5860 _has_final_method(false),
5861 _has_finalizer(false),
5862 _has_empty_finalizer(false),
5863 _has_vanilla_constructor(false),
5864 _max_bootstrap_specifier_index(-1) {
5865
5866 _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
5867 _class_name->increment_refcount();
5868
5869 assert(THREAD->is_Java_thread(), "invariant");
5870 assert(_loader_data != NULL, "invariant");
5871 assert(stream != NULL, "invariant");
5872 assert(_stream != NULL, "invariant");
5873 assert(_stream->buffer() == _stream->current(), "invariant");
5874 assert(_class_name != NULL, "invariant");
5875 assert(0 == _access_flags.as_int(), "invariant");
5876
5877 // Figure out whether we can skip format checking (matching classic VM behavior)
5878 if (DumpSharedSpaces) {
5879 // verify == true means it's a 'remote' class (i.e., non-boot class)
5880 // Verification decision is based on BytecodeVerificationRemote flag
6036
6037 _orig_cp_size = cp_size;
6038 if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6039 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6040 }
6041 cp_size += _max_num_patched_klasses;
6042
6043 _cp = ConstantPool::allocate(_loader_data,
6044 cp_size,
6045 CHECK);
6046
6047 ConstantPool* const cp = _cp;
6048
6049 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6050
6051 assert(cp_size == (const u2)cp->length(), "invariant");
6052
6053 // ACCESS FLAGS
6054 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6055
6056 // Access flags
6057 jint flags;
6058 // JVM_ACC_MODULE is defined in JDK-9 and later.
6059 if (_major_version >= JAVA_9_VERSION) {
6060 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
6061 } else {
6062 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
6063 }
6064
6065 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6066 // Set abstract bit for old class files for backward compatibility
6067 flags |= JVM_ACC_ABSTRACT;
6068 }
6069
6070 verify_legal_class_modifiers(flags, CHECK);
6071
6072 short bad_constant = class_bad_constant_seen();
6073 if (bad_constant != 0) {
6074 // Do not throw CFE until after the access_flags are checked because if
6075 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6076 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6077 }
6078
6079 _access_flags.set_flags(flags);
6080
6081 // This class and superclass
6082 _this_class_index = stream->get_u2_fast();
6083 check_property(
6084 valid_cp_range(_this_class_index, cp_size) &&
6179 _super_class_index = stream->get_u2_fast();
6180 _super_klass = parse_super_class(cp,
6181 _super_class_index,
6182 _need_verify,
6183 CHECK);
6184
6185 // Interfaces
6186 _itfs_len = stream->get_u2_fast();
6187 parse_interfaces(stream,
6188 _itfs_len,
6189 cp,
6190 &_has_nonstatic_concrete_methods,
6191 CHECK);
6192
6193 assert(_local_interfaces != NULL, "invariant");
6194
6195 // Fields (offsets are filled in later)
6196 _fac = new FieldAllocationCount();
6197 parse_fields(stream,
6198 _access_flags.is_interface(),
6199 _fac,
6200 cp,
6201 cp_size,
6202 &_java_fields_count,
6203 CHECK);
6204
6205 assert(_fields != NULL, "invariant");
6206
6207 // Methods
6208 AccessFlags promoted_flags;
6209 parse_methods(stream,
6210 _access_flags.is_interface(),
6211 &promoted_flags,
6212 &_has_final_method,
6213 &_declares_nonstatic_concrete_methods,
6214 CHECK);
6215
6216 assert(_methods != NULL, "invariant");
6217
6218 // promote flags from parse_methods() to the klass' flags
6219 _access_flags.add_promoted_flags(promoted_flags.as_int());
6220
6221 if (_declares_nonstatic_concrete_methods) {
6222 _has_nonstatic_concrete_methods = true;
6223 }
6224
6225 // Additional attributes/annotations
6226 _parsed_annotations = new ClassAnnotationCollector();
6227 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6228
6229 assert(_inner_classes != NULL, "invariant");
6230
6272 true,
6273 CHECK);
6274 }
6275
6276 if (_super_klass != NULL) {
6277 if (_super_klass->has_nonstatic_concrete_methods()) {
6278 _has_nonstatic_concrete_methods = true;
6279 }
6280
6281 if (_super_klass->is_interface()) {
6282 ResourceMark rm(THREAD);
6283 Exceptions::fthrow(
6284 THREAD_AND_LOCATION,
6285 vmSymbols::java_lang_IncompatibleClassChangeError(),
6286 "class %s has interface %s as super class",
6287 _class_name->as_klass_external_name(),
6288 _super_klass->external_name()
6289 );
6290 return;
6291 }
6292 // Make sure super class is not final
6293 if (_super_klass->is_final()) {
6294 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6295 }
6296 }
6297
6298 // Compute the transitive list of all unique interfaces implemented by this class
6299 _transitive_interfaces =
6300 compute_transitive_interfaces(_super_klass,
6301 _local_interfaces,
6302 _loader_data,
6303 CHECK);
6304
6305 assert(_transitive_interfaces != NULL, "invariant");
6306
6307 // sort methods
6308 _method_ordering = sort_methods(_methods);
6309
6310 _all_mirandas = new GrowableArray<Method*>(20);
6311
6312 Handle loader(THREAD, _loader_data->class_loader());
6313 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6314 &_num_miranda_methods,
6315 _all_mirandas,
6316 _super_klass,
6317 _methods,
6318 _access_flags,
6319 _major_version,
6320 loader,
6321 _class_name,
6322 _local_interfaces,
6323 CHECK);
6324
6325 // Size of Java itable (in words)
6326 _itable_size = _access_flags.is_interface() ? 0 :
6327 klassItable::compute_itable_size(_transitive_interfaces);
6328
6329 assert(_fac != NULL, "invariant");
6330 assert(_parsed_annotations != NULL, "invariant");
6331
6332 _field_info = new FieldLayoutInfo();
6333 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6334
6335 // Compute reference typ
6336 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6337
6338 }
6339
6340 void ClassFileParser::set_klass(InstanceKlass* klass) {
6341
6342 #ifdef ASSERT
6343 if (klass != NULL) {
6344 assert(NULL == _klass, "leaking?");
6345 }
6346 #endif
6347
6348 _klass = klass;
6349 }
6350
6351 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6352
6353 #ifdef ASSERT
6354 if (klass != NULL) {
6355 assert(NULL == _klass_to_deallocate, "leaking?");
6356 }
6357 #endif
6358
6359 _klass_to_deallocate = klass;
6360 }
6361
6362 // Caller responsible for ResourceMark
6363 // clone stream with rewound position
6364 const ClassFileStream* ClassFileParser::clone_stream() const {
6365 assert(_stream != NULL, "invariant");
6366
6367 return _stream->clone();
6368 }
6369 // ----------------------------------------------------------------------------
6370 // debugging
6371
6372 #ifdef ASSERT
6373
6374 // return true if class_name contains no '.' (internal format is '/')
6375 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6376 if (class_name != NULL) {
6377 ResourceMark rm;
6378 char* name = class_name->as_C_string();
6379 return strchr(name, '.') == NULL;
6380 } else {
6381 return true;
6382 }
6383 }
6384
6385 #endif
|
38 #include "classfile/verifier.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "memory/allocation.hpp"
43 #include "memory/metadataFactory.hpp"
44 #include "memory/oopFactory.hpp"
45 #include "memory/resourceArea.hpp"
46 #include "memory/universe.hpp"
47 #include "oops/annotations.hpp"
48 #include "oops/constantPool.inline.hpp"
49 #include "oops/fieldStreams.hpp"
50 #include "oops/instanceKlass.hpp"
51 #include "oops/instanceMirrorKlass.hpp"
52 #include "oops/klass.inline.hpp"
53 #include "oops/klassVtable.hpp"
54 #include "oops/metadata.hpp"
55 #include "oops/method.hpp"
56 #include "oops/oop.inline.hpp"
57 #include "oops/symbol.hpp"
58 #include "oops/valueKlass.hpp"
59 #include "prims/jvmtiExport.hpp"
60 #include "prims/jvmtiThreadState.hpp"
61 #include "runtime/arguments.hpp"
62 #include "runtime/handles.inline.hpp"
63 #include "runtime/javaCalls.hpp"
64 #include "runtime/os.hpp"
65 #include "runtime/perfData.hpp"
66 #include "runtime/reflection.hpp"
67 #include "runtime/safepointVerifiers.hpp"
68 #include "runtime/signature.hpp"
69 #include "runtime/timer.hpp"
70 #include "services/classLoadingService.hpp"
71 #include "services/threadService.hpp"
72 #include "utilities/align.hpp"
73 #include "utilities/bitMap.inline.hpp"
74 #include "utilities/copy.hpp"
75 #include "utilities/exceptions.hpp"
76 #include "utilities/globalDefinitions.hpp"
77 #include "utilities/growableArray.hpp"
78 #include "utilities/macros.hpp"
106 // - also used as the max version when running in jdk6
107 #define JAVA_6_VERSION 50
108
109 // Used for backward compatibility reasons:
110 // - to disallow argument and require ACC_STATIC for <clinit> methods
111 #define JAVA_7_VERSION 51
112
113 // Extension method support.
114 #define JAVA_8_VERSION 52
115
116 #define JAVA_9_VERSION 53
117
118 #define JAVA_10_VERSION 54
119
120 #define JAVA_11_VERSION 55
121
122 #define JAVA_12_VERSION 56
123
124 #define JAVA_13_VERSION 57
125
126 #define CONSTANT_CLASS_DESCRIPTORS 57
127
128 void ClassFileParser::set_class_bad_constant_seen(short bad_constant) {
129 assert((bad_constant == 19 || bad_constant == 20) && _major_version >= JAVA_9_VERSION,
130 "Unexpected bad constant pool entry");
131 if (_bad_constant_seen == 0) _bad_constant_seen = bad_constant;
132 }
133
134 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
135 ConstantPool* cp,
136 const int length,
137 TRAPS) {
138 assert(stream != NULL, "invariant");
139 assert(cp != NULL, "invariant");
140
141 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
142 // this function (_current can be allocated in a register, with scalar
143 // replacement of aggregates). The _current pointer is copied back to
144 // stream() when this function returns. DON'T call another method within
145 // this method that uses stream().
146 const ClassFileStream cfs1 = *stream;
147 const ClassFileStream* const cfs = &cfs1;
148
149 assert(cfs->allocated_on_stack(), "should be local");
150 debug_only(const u1* const old_current = stream->current();)
151
152 // Used for batching symbol allocations.
153 const char* names[SymbolTable::symbol_alloc_batch_size];
154 int lengths[SymbolTable::symbol_alloc_batch_size];
155 int indices[SymbolTable::symbol_alloc_batch_size];
156 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
157 int names_count = 0;
158
159 // parsing Index 0 is unused
160 for (int index = 1; index < length; index++) {
161 // Each of the following case guarantees one more byte in the stream
162 // for the following tag or the access_flags following constant pool,
163 // so we don't need bounds-check for reading tag.
164 const u1 tag = cfs->get_u1_fast();
165 switch (tag) {
166 case JVM_CONSTANT_Class: {
167 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
168 const u2 name_index = cfs->get_u2_fast();
169 cp->klass_index_at_put(index, name_index);
170 break;
171 }
172 case JVM_CONSTANT_Fieldref: {
173 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
174 const u2 class_index = cfs->get_u2_fast();
175 const u2 name_and_type_index = cfs->get_u2_fast();
176 cp->field_at_put(index, class_index, name_and_type_index);
177 break;
178 }
179 case JVM_CONSTANT_Methodref: {
180 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
181 const u2 class_index = cfs->get_u2_fast();
182 const u2 name_and_type_index = cfs->get_u2_fast();
183 cp->method_at_put(index, class_index, name_and_type_index);
184 break;
185 }
186 case JVM_CONSTANT_InterfaceMethodref: {
478 check_property(valid_symbol_at(name_ref_index),
479 "Invalid constant pool index %u in class file %s",
480 name_ref_index, CHECK);
481 check_property(valid_symbol_at(signature_ref_index),
482 "Invalid constant pool index %u in class file %s",
483 signature_ref_index, CHECK);
484 break;
485 }
486 case JVM_CONSTANT_Utf8:
487 break;
488 case JVM_CONSTANT_UnresolvedClass: // fall-through
489 case JVM_CONSTANT_UnresolvedClassInError: {
490 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
491 break;
492 }
493 case JVM_CONSTANT_ClassIndex: {
494 const int class_index = cp->klass_index_at(index);
495 check_property(valid_symbol_at(class_index),
496 "Invalid constant pool index %u in class file %s",
497 class_index, CHECK);
498
499 Symbol* const name = cp->symbol_at(class_index);
500 const unsigned int name_len = name->utf8_length();
501 if (name->is_Q_signature()) {
502 cp->unresolved_qdescriptor_at_put(index, class_index, num_klasses++);
503 } else {
504 cp->unresolved_klass_at_put(index, class_index, num_klasses++);
505 }
506 break;
507 }
508 case JVM_CONSTANT_StringIndex: {
509 const int string_index = cp->string_index_at(index);
510 check_property(valid_symbol_at(string_index),
511 "Invalid constant pool index %u in class file %s",
512 string_index, CHECK);
513 Symbol* const sym = cp->symbol_at(string_index);
514 cp->unresolved_string_at_put(index, sym);
515 break;
516 }
517 case JVM_CONSTANT_MethodHandle: {
518 const int ref_index = cp->method_handle_index_at(index);
519 check_property(valid_cp_range(ref_index, length),
520 "Invalid constant pool index %u in class file %s",
521 ref_index, CHECK);
522 const constantTag tag = cp->tag_at(ref_index);
523 const int ref_kind = cp->method_handle_ref_kind_at(index);
524
525 switch (ref_kind) {
1454 CHECK);
1455 parsed_annotations->set_field_annotations(a);
1456 a = assemble_annotations(runtime_visible_type_annotations,
1457 runtime_visible_type_annotations_length,
1458 runtime_invisible_type_annotations,
1459 runtime_invisible_type_annotations_length,
1460 CHECK);
1461 parsed_annotations->set_field_type_annotations(a);
1462 return;
1463 }
1464
1465
1466 // Field allocation types. Used for computing field offsets.
1467
1468 enum FieldAllocationType {
1469 STATIC_OOP, // Oops
1470 STATIC_BYTE, // Boolean, Byte, char
1471 STATIC_SHORT, // shorts
1472 STATIC_WORD, // ints
1473 STATIC_DOUBLE, // aligned long or double
1474 STATIC_FLATTENABLE, // flattenable field
1475 NONSTATIC_OOP,
1476 NONSTATIC_BYTE,
1477 NONSTATIC_SHORT,
1478 NONSTATIC_WORD,
1479 NONSTATIC_DOUBLE,
1480 NONSTATIC_FLATTENABLE,
1481 MAX_FIELD_ALLOCATION_TYPE,
1482 BAD_ALLOCATION_TYPE = -1
1483 };
1484
1485 static FieldAllocationType _basic_type_to_atype[2 * (T_CONFLICT + 1)] = {
1486 BAD_ALLOCATION_TYPE, // 0
1487 BAD_ALLOCATION_TYPE, // 1
1488 BAD_ALLOCATION_TYPE, // 2
1489 BAD_ALLOCATION_TYPE, // 3
1490 NONSTATIC_BYTE , // T_BOOLEAN = 4,
1491 NONSTATIC_SHORT, // T_CHAR = 5,
1492 NONSTATIC_WORD, // T_FLOAT = 6,
1493 NONSTATIC_DOUBLE, // T_DOUBLE = 7,
1494 NONSTATIC_BYTE, // T_BYTE = 8,
1495 NONSTATIC_SHORT, // T_SHORT = 9,
1496 NONSTATIC_WORD, // T_INT = 10,
1497 NONSTATIC_DOUBLE, // T_LONG = 11,
1498 NONSTATIC_OOP, // T_OBJECT = 12,
1499 NONSTATIC_OOP, // T_ARRAY = 13,
1500 NONSTATIC_OOP, // T_VALUETYPE = 14,
1501 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1502 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1503 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1504 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1505 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1506 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20,
1507 BAD_ALLOCATION_TYPE, // 0
1508 BAD_ALLOCATION_TYPE, // 1
1509 BAD_ALLOCATION_TYPE, // 2
1510 BAD_ALLOCATION_TYPE, // 3
1511 STATIC_BYTE , // T_BOOLEAN = 4,
1512 STATIC_SHORT, // T_CHAR = 5,
1513 STATIC_WORD, // T_FLOAT = 6,
1514 STATIC_DOUBLE, // T_DOUBLE = 7,
1515 STATIC_BYTE, // T_BYTE = 8,
1516 STATIC_SHORT, // T_SHORT = 9,
1517 STATIC_WORD, // T_INT = 10,
1518 STATIC_DOUBLE, // T_LONG = 11,
1519 STATIC_OOP, // T_OBJECT = 12,
1520 STATIC_OOP, // T_ARRAY = 13,
1521 STATIC_OOP, // T_VALUETYPE = 14,
1522 BAD_ALLOCATION_TYPE, // T_VOID = 15,
1523 BAD_ALLOCATION_TYPE, // T_ADDRESS = 16,
1524 BAD_ALLOCATION_TYPE, // T_NARROWOOP = 17,
1525 BAD_ALLOCATION_TYPE, // T_METADATA = 18,
1526 BAD_ALLOCATION_TYPE, // T_NARROWKLASS = 19,
1527 BAD_ALLOCATION_TYPE, // T_CONFLICT = 20
1528 };
1529
1530 static FieldAllocationType basic_type_to_atype(bool is_static, BasicType type, bool is_flattenable) {
1531 assert(type >= T_BOOLEAN && type < T_VOID, "only allowable values");
1532 FieldAllocationType result = _basic_type_to_atype[type + (is_static ? (T_CONFLICT + 1) : 0)];
1533 assert(result != BAD_ALLOCATION_TYPE, "bad type");
1534 if (is_flattenable) {
1535 result = is_static ? STATIC_FLATTENABLE : NONSTATIC_FLATTENABLE;
1536 }
1537 return result;
1538 }
1539
1540 class ClassFileParser::FieldAllocationCount : public ResourceObj {
1541 public:
1542 u2 count[MAX_FIELD_ALLOCATION_TYPE];
1543
1544 FieldAllocationCount() {
1545 for (int i = 0; i < MAX_FIELD_ALLOCATION_TYPE; i++) {
1546 count[i] = 0;
1547 }
1548 }
1549
1550 FieldAllocationType update(bool is_static, BasicType type, bool is_flattenable) {
1551 FieldAllocationType atype = basic_type_to_atype(is_static, type, is_flattenable);
1552 if (atype != BAD_ALLOCATION_TYPE) {
1553 // Make sure there is no overflow with injected fields.
1554 assert(count[atype] < 0xFFFF, "More than 65535 fields");
1555 count[atype]++;
1556 }
1557 return atype;
1558 }
1559 };
1560
1561 // Side-effects: populates the _fields, _fields_annotations,
1562 // _fields_type_annotations fields
1563 void ClassFileParser::parse_fields(const ClassFileStream* const cfs,
1564 bool is_interface,
1565 bool is_value_type,
1566 FieldAllocationCount* const fac,
1567 ConstantPool* cp,
1568 const int cp_size,
1569 u2* const java_fields_count_ptr,
1570 TRAPS) {
1571
1572 assert(cfs != NULL, "invariant");
1573 assert(fac != NULL, "invariant");
1574 assert(cp != NULL, "invariant");
1575 assert(java_fields_count_ptr != NULL, "invariant");
1576
1577 assert(NULL == _fields, "invariant");
1578 assert(NULL == _fields_annotations, "invariant");
1579 assert(NULL == _fields_type_annotations, "invariant");
1580
1581 cfs->guarantee_more(2, CHECK); // length
1582 const u2 length = cfs->get_u2_fast();
1583 *java_fields_count_ptr = length;
1584
1585 int num_injected = 0;
1586 const InjectedField* const injected = JavaClasses::get_injected(_class_name,
1587 &num_injected);
1588
1589 const int total_fields = length + num_injected + (is_value_type ? 1 : 0);
1590
1591 // The field array starts with tuples of shorts
1592 // [access, name index, sig index, initial value index, byte offset].
1593 // A generic signature slot only exists for field with generic
1594 // signature attribute. And the access flag is set with
1595 // JVM_ACC_FIELD_HAS_GENERIC_SIGNATURE for that field. The generic
1596 // signature slots are at the end of the field array and after all
1597 // other fields data.
1598 //
1599 // f1: [access, name index, sig index, initial value index, low_offset, high_offset]
1600 // f2: [access, name index, sig index, initial value index, low_offset, high_offset]
1601 // ...
1602 // fn: [access, name index, sig index, initial value index, low_offset, high_offset]
1603 // [generic signature index]
1604 // [generic signature index]
1605 // ...
1606 //
1607 // Allocate a temporary resource array for field data. For each field,
1608 // a slot is reserved in the temporary array for the generic signature
1609 // index. After parsing all fields, the data are copied to a permanent
1610 // array and any unused slots will be discarded.
1611 ResourceMark rm(THREAD);
1612 u2* const fa = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
1613 u2,
1614 total_fields * (FieldInfo::field_slots + 1));
1615
1616 // The generic signature slots start after all other fields' data.
1617 int generic_signature_slot = total_fields * FieldInfo::field_slots;
1618 int num_generic_signature = 0;
1619 for (int n = 0; n < length; n++) {
1620 // access_flags, name_index, descriptor_index, attributes_count
1621 cfs->guarantee_more(8, CHECK);
1622
1623 jint recognized_modifiers = JVM_RECOGNIZED_FIELD_MODIFIERS;
1624
1625 const jint flags = cfs->get_u2_fast() & recognized_modifiers;
1626 verify_legal_field_modifiers(flags, is_interface, is_value_type, CHECK);
1627 AccessFlags access_flags;
1628 access_flags.set_flags(flags);
1629
1630 const u2 name_index = cfs->get_u2_fast();
1631 check_property(valid_symbol_at(name_index),
1632 "Invalid constant pool index %u for field name in class file %s",
1633 name_index, CHECK);
1634 const Symbol* const name = cp->symbol_at(name_index);
1635 verify_legal_field_name(name, CHECK);
1636
1637 const u2 signature_index = cfs->get_u2_fast();
1638 check_property(valid_symbol_at(signature_index),
1639 "Invalid constant pool index %u for field signature in class file %s",
1640 signature_index, CHECK);
1641 const Symbol* const sig = cp->symbol_at(signature_index);
1642 verify_legal_field_signature(name, sig, CHECK);
1643 assert(!access_flags.is_flattenable(), "ACC_FLATTENABLE should have been filtered out");
1644 if (sig->is_Q_signature()) {
1645 // assert(_major_version >= CONSTANT_CLASS_DESCRIPTORS, "Q-descriptors are only supported in recent classfiles");
1646 access_flags.set_is_flattenable();
1647 }
1648 if (access_flags.is_flattenable()) {
1649 // Array flattenability cannot be specified. Arrays of value classes are
1650 // are always flattenable. Arrays of other classes are not flattenable.
1651 if (sig->utf8_length() > 1 && sig->char_at(0) == '[') {
1652 classfile_parse_error(
1653 "Field \"%s\" with signature \"%s\" in class file %s is invalid."
1654 " ACC_FLATTENABLE cannot be specified for an array",
1655 name->as_C_string(), sig->as_klass_external_name(), CHECK);
1656 }
1657 _has_flattenable_fields = true;
1658 }
1659
1660 u2 constantvalue_index = 0;
1661 bool is_synthetic = false;
1662 u2 generic_signature_index = 0;
1663 const bool is_static = access_flags.is_static();
1664 FieldAnnotationCollector parsed_annotations(_loader_data);
1665
1666 const u2 attributes_count = cfs->get_u2_fast();
1667 if (attributes_count > 0) {
1668 parse_field_attributes(cfs,
1669 attributes_count,
1670 is_static,
1671 signature_index,
1672 &constantvalue_index,
1673 &is_synthetic,
1674 &generic_signature_index,
1675 &parsed_annotations,
1676 CHECK);
1677
1678 if (parsed_annotations.field_annotations() != NULL) {
1698
1699 if (is_synthetic) {
1700 access_flags.set_is_synthetic();
1701 }
1702 if (generic_signature_index != 0) {
1703 access_flags.set_field_has_generic_signature();
1704 fa[generic_signature_slot] = generic_signature_index;
1705 generic_signature_slot ++;
1706 num_generic_signature ++;
1707 }
1708 }
1709
1710 FieldInfo* const field = FieldInfo::from_field_array(fa, n);
1711 field->initialize(access_flags.as_short(),
1712 name_index,
1713 signature_index,
1714 constantvalue_index);
1715 const BasicType type = cp->basic_type_for_signature_at(signature_index);
1716
1717 // Remember how many oops we encountered and compute allocation type
1718 const FieldAllocationType atype = fac->update(is_static, type, access_flags.is_flattenable());
1719 field->set_allocation_type(atype);
1720
1721 // After field is initialized with type, we can augment it with aux info
1722 if (parsed_annotations.has_any_annotations())
1723 parsed_annotations.apply_to(field);
1724 }
1725
1726 int index = length;
1727 if (num_injected != 0) {
1728 for (int n = 0; n < num_injected; n++) {
1729 // Check for duplicates
1730 if (injected[n].may_be_java) {
1731 const Symbol* const name = injected[n].name();
1732 const Symbol* const signature = injected[n].signature();
1733 bool duplicate = false;
1734 for (int i = 0; i < length; i++) {
1735 const FieldInfo* const f = FieldInfo::from_field_array(fa, i);
1736 if (name == cp->symbol_at(f->name_index()) &&
1737 signature == cp->symbol_at(f->signature_index())) {
1738 // Symbol is desclared in Java so skip this one
1739 duplicate = true;
1740 break;
1741 }
1742 }
1743 if (duplicate) {
1744 // These will be removed from the field array at the end
1745 continue;
1746 }
1747 }
1748
1749 // Injected field
1750 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1751 field->initialize(JVM_ACC_FIELD_INTERNAL,
1752 injected[n].name_index,
1753 injected[n].signature_index,
1754 0);
1755
1756 const BasicType type = FieldType::basic_type(injected[n].signature());
1757
1758 // Remember how many oops we encountered and compute allocation type
1759 const FieldAllocationType atype = fac->update(false, type, false);
1760 field->set_allocation_type(atype);
1761 index++;
1762 }
1763 }
1764
1765 if (is_value_type) {
1766 index = length + num_injected;
1767 FieldInfo* const field = FieldInfo::from_field_array(fa, index);
1768 field->initialize(JVM_ACC_FIELD_INTERNAL | JVM_ACC_STATIC,
1769 vmSymbols::default_value_name_enum,
1770 vmSymbols::java_lang_Object_enum,
1771 0);
1772 const BasicType type = FieldType::basic_type(vmSymbols::object_signature());
1773 const FieldAllocationType atype = fac->update(true, type, false);
1774 field->set_allocation_type(atype);
1775 index++;
1776 }
1777
1778 assert(NULL == _fields, "invariant");
1779
1780 _fields =
1781 MetadataFactory::new_array<u2>(_loader_data,
1782 index * FieldInfo::field_slots + num_generic_signature,
1783 CHECK);
1784 // Sometimes injected fields already exist in the Java source so
1785 // the fields array could be too long. In that case the
1786 // fields array is trimed. Also unused slots that were reserved
1787 // for generic signature indexes are discarded.
1788 {
1789 int i = 0;
1790 for (; i < index * FieldInfo::field_slots; i++) {
1791 _fields->at_put(i, fa[i]);
1792 }
1793 for (int j = total_fields * FieldInfo::field_slots;
1794 j < generic_signature_slot; j++) {
1795 _fields->at_put(i++, fa[j]);
1796 }
1797 assert(_fields->length() == i, "");
2381 runtime_visible_type_annotations_length,
2382 runtime_invisible_type_annotations,
2383 runtime_invisible_type_annotations_length,
2384 CHECK);
2385 cm->set_type_annotations(a);
2386 }
2387 }
2388
2389
2390 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
2391 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
2392 // Method* to save footprint, so we only know the size of the resulting Method* when the
2393 // entire method attribute is parsed.
2394 //
2395 // The promoted_flags parameter is used to pass relevant access_flags
2396 // from the method back up to the containing klass. These flag values
2397 // are added to klass's access_flags.
2398
2399 Method* ClassFileParser::parse_method(const ClassFileStream* const cfs,
2400 bool is_interface,
2401 bool is_value_type,
2402 const ConstantPool* cp,
2403 AccessFlags* const promoted_flags,
2404 TRAPS) {
2405 assert(cfs != NULL, "invariant");
2406 assert(cp != NULL, "invariant");
2407 assert(promoted_flags != NULL, "invariant");
2408
2409 ResourceMark rm(THREAD);
2410 // Parse fixed parts:
2411 // access_flags, name_index, descriptor_index, attributes_count
2412 cfs->guarantee_more(8, CHECK_NULL);
2413
2414 int flags = cfs->get_u2_fast();
2415 const u2 name_index = cfs->get_u2_fast();
2416 const int cp_size = cp->length();
2417 check_property(
2418 valid_symbol_at(name_index),
2419 "Illegal constant pool index %u for method name in class file %s",
2420 name_index, CHECK_NULL);
2421 const Symbol* const name = cp->symbol_at(name_index);
2422 verify_legal_method_name(name, CHECK_NULL);
2423
2424 const u2 signature_index = cfs->get_u2_fast();
2425 guarantee_property(
2426 valid_symbol_at(signature_index),
2427 "Illegal constant pool index %u for method signature in class file %s",
2428 signature_index, CHECK_NULL);
2429 const Symbol* const signature = cp->symbol_at(signature_index);
2430
2431 if (name == vmSymbols::class_initializer_name()) {
2432 // We ignore the other access flags for a valid class initializer.
2433 // (JVM Spec 2nd ed., chapter 4.6)
2434 if (_major_version < 51) { // backward compatibility
2435 flags = JVM_ACC_STATIC;
2436 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
2437 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
2438 } else {
2439 classfile_parse_error("Method <clinit> is not static in class file %s", CHECK_NULL);
2440 }
2441 } else {
2442 verify_legal_method_modifiers(flags, is_interface, is_value_type, name, CHECK_NULL);
2443 }
2444
2445 if (name == vmSymbols::object_initializer_name()) {
2446 if (is_interface) {
2447 classfile_parse_error("Interface cannot have a method named <init>, class file %s", CHECK_NULL);
2448 /* TBD: uncomment when javac stops generating <init>() for value types.
2449 } else if (is_value_type) {
2450 classfile_parse_error("Value Type cannot have a method named <init>, class file %s", CHECK_NULL);
2451 */
2452 }
2453 }
2454
2455 int args_size = -1; // only used when _need_verify is true
2456 if (_need_verify) {
2457 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
2458 verify_legal_method_signature(name, signature, CHECK_NULL);
2459 if (args_size > MAX_ARGS_SIZE) {
2460 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_NULL);
2461 }
2462 }
2463
2464 AccessFlags access_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
2465
2466 // Default values for code and exceptions attribute elements
2467 u2 max_stack = 0;
2468 u2 max_locals = 0;
2469 u4 code_length = 0;
2470 const u1* code_start = 0;
2471 u2 exception_table_length = 0;
2472 const unsafe_u2* exception_table_start = NULL; // (potentially unaligned) pointer to array of u2 elements
3003 _has_finalizer = true;
3004 }
3005 }
3006 if (name == vmSymbols::object_initializer_name() &&
3007 signature == vmSymbols::void_method_signature() &&
3008 m->is_vanilla_constructor()) {
3009 _has_vanilla_constructor = true;
3010 }
3011
3012 NOT_PRODUCT(m->verify());
3013 return m;
3014 }
3015
3016
3017 // The promoted_flags parameter is used to pass relevant access_flags
3018 // from the methods back up to the containing klass. These flag values
3019 // are added to klass's access_flags.
3020 // Side-effects: populates the _methods field in the parser
3021 void ClassFileParser::parse_methods(const ClassFileStream* const cfs,
3022 bool is_interface,
3023 bool is_value_type,
3024 AccessFlags* promoted_flags,
3025 bool* has_final_method,
3026 bool* declares_nonstatic_concrete_methods,
3027 TRAPS) {
3028 assert(cfs != NULL, "invariant");
3029 assert(promoted_flags != NULL, "invariant");
3030 assert(has_final_method != NULL, "invariant");
3031 assert(declares_nonstatic_concrete_methods != NULL, "invariant");
3032
3033 assert(NULL == _methods, "invariant");
3034
3035 cfs->guarantee_more(2, CHECK); // length
3036 const u2 length = cfs->get_u2_fast();
3037 if (length == 0) {
3038 _methods = Universe::the_empty_method_array();
3039 } else {
3040 _methods = MetadataFactory::new_array<Method*>(_loader_data,
3041 length,
3042 NULL,
3043 CHECK);
3044
3045 for (int index = 0; index < length; index++) {
3046 Method* method = parse_method(cfs,
3047 is_interface,
3048 is_value_type,
3049 _cp,
3050 promoted_flags,
3051 CHECK);
3052
3053 if (method->is_final()) {
3054 *has_final_method = true;
3055 }
3056 // declares_nonstatic_concrete_methods: declares concrete instance methods, any access flags
3057 // used for interface initialization, and default method inheritance analysis
3058 if (is_interface && !(*declares_nonstatic_concrete_methods)
3059 && !method->is_abstract() && !method->is_static()) {
3060 *declares_nonstatic_concrete_methods = true;
3061 }
3062 _methods->at_put(index, method);
3063 }
3064
3065 if (_need_verify && length > 1) {
3066 // Check duplicated methods
3067 ResourceMark rm(THREAD);
3068 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
3221 valid_klass_reference_at(inner_class_info_index),
3222 "inner_class_info_index %u has bad constant type in class file %s",
3223 inner_class_info_index, CHECK_0);
3224 // Outer class index
3225 const u2 outer_class_info_index = cfs->get_u2_fast();
3226 check_property(
3227 outer_class_info_index == 0 ||
3228 valid_klass_reference_at(outer_class_info_index),
3229 "outer_class_info_index %u has bad constant type in class file %s",
3230 outer_class_info_index, CHECK_0);
3231 // Inner class name
3232 const u2 inner_name_index = cfs->get_u2_fast();
3233 check_property(
3234 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3235 "inner_name_index %u has bad constant type in class file %s",
3236 inner_name_index, CHECK_0);
3237 if (_need_verify) {
3238 guarantee_property(inner_class_info_index != outer_class_info_index,
3239 "Class is both outer and inner class in class file %s", CHECK_0);
3240 }
3241
3242 jint recognized_modifiers = RECOGNIZED_INNER_CLASS_MODIFIERS;
3243 // JVM_ACC_MODULE is defined in JDK-9 and later.
3244 if (_major_version >= JAVA_9_VERSION) {
3245 recognized_modifiers |= JVM_ACC_MODULE;
3246 }
3247 // JVM_ACC_VALUE is defined for class file version 55 and later
3248 if (supports_value_types()) {
3249 recognized_modifiers |= JVM_ACC_VALUE;
3250 }
3251
3252 // Access flags
3253 jint flags = cfs->get_u2_fast() & recognized_modifiers;
3254
3255 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3256 // Set abstract bit for old class files for backward compatibility
3257 flags |= JVM_ACC_ABSTRACT;
3258 }
3259 verify_legal_class_modifiers(flags, CHECK_0);
3260 AccessFlags inner_access_flags(flags);
3261
3262 inner_classes->at_put(index++, inner_class_info_index);
3263 inner_classes->at_put(index++, outer_class_info_index);
3264 inner_classes->at_put(index++, inner_name_index);
3265 inner_classes->at_put(index++, inner_access_flags.as_short());
3266 }
3267
3268 // 4347400: make sure there's no duplicate entry in the classes array
3269 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3270 for(int i = 0; i < length * 4; i += 4) {
3271 for(int j = i + 4; j < length * 4; j += 4) {
3272 guarantee_property((inner_classes->at(i) != inner_classes->at(j) ||
3273 inner_classes->at(i+1) != inner_classes->at(j+1) ||
3274 inner_classes->at(i+2) != inner_classes->at(j+2) ||
3437 u2 attributes_count = cfs->get_u2_fast();
3438 bool parsed_sourcefile_attribute = false;
3439 bool parsed_innerclasses_attribute = false;
3440 bool parsed_nest_members_attribute = false;
3441 bool parsed_nest_host_attribute = false;
3442 bool parsed_enclosingmethod_attribute = false;
3443 bool parsed_bootstrap_methods_attribute = false;
3444 const u1* runtime_visible_annotations = NULL;
3445 int runtime_visible_annotations_length = 0;
3446 const u1* runtime_invisible_annotations = NULL;
3447 int runtime_invisible_annotations_length = 0;
3448 const u1* runtime_visible_type_annotations = NULL;
3449 int runtime_visible_type_annotations_length = 0;
3450 const u1* runtime_invisible_type_annotations = NULL;
3451 int runtime_invisible_type_annotations_length = 0;
3452 bool runtime_invisible_type_annotations_exists = false;
3453 bool runtime_invisible_annotations_exists = false;
3454 bool parsed_source_debug_ext_annotations_exist = false;
3455 const u1* inner_classes_attribute_start = NULL;
3456 u4 inner_classes_attribute_length = 0;
3457 const u1* value_types_attribute_start = NULL;
3458 u4 value_types_attribute_length = 0;
3459 u2 enclosing_method_class_index = 0;
3460 u2 enclosing_method_method_index = 0;
3461 const u1* nest_members_attribute_start = NULL;
3462 u4 nest_members_attribute_length = 0;
3463
3464 // Iterate over attributes
3465 while (attributes_count--) {
3466 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
3467 const u2 attribute_name_index = cfs->get_u2_fast();
3468 const u4 attribute_length = cfs->get_u4_fast();
3469 check_property(
3470 valid_symbol_at(attribute_name_index),
3471 "Attribute name has bad constant pool index %u in class file %s",
3472 attribute_name_index, CHECK);
3473 const Symbol* const tag = cp->symbol_at(attribute_name_index);
3474 if (tag == vmSymbols::tag_source_file()) {
3475 // Check for SourceFile tag
3476 if (_need_verify) {
3477 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
3478 }
3788 }
3789 }
3790 if (runtime_invisible_annotations != NULL) {
3791 for (int i = 0; i < runtime_invisible_annotations_length; i++) {
3792 int append = runtime_visible_annotations_length+i;
3793 annotations->at_put(append, runtime_invisible_annotations[i]);
3794 }
3795 }
3796 }
3797 return annotations;
3798 }
3799
3800 const InstanceKlass* ClassFileParser::parse_super_class(ConstantPool* const cp,
3801 const int super_class_index,
3802 const bool need_verify,
3803 TRAPS) {
3804 assert(cp != NULL, "invariant");
3805 const InstanceKlass* super_klass = NULL;
3806
3807 if (super_class_index == 0) {
3808 check_property(_class_name == vmSymbols::java_lang_Object()
3809 || (_access_flags.get_flags() & JVM_ACC_VALUE),
3810 "Invalid superclass index %u in class file %s",
3811 super_class_index,
3812 CHECK_NULL);
3813 } else {
3814 check_property(valid_klass_reference_at(super_class_index),
3815 "Invalid superclass index %u in class file %s",
3816 super_class_index,
3817 CHECK_NULL);
3818 // The class name should be legal because it is checked when parsing constant pool.
3819 // However, make sure it is not an array type.
3820 bool is_array = false;
3821 if (cp->tag_at(super_class_index).is_klass()) {
3822 super_klass = InstanceKlass::cast(cp->resolved_klass_at(super_class_index));
3823 if (need_verify)
3824 is_array = super_klass->is_array_klass();
3825 } else if (need_verify) {
3826 is_array = (cp->klass_name_at(super_class_index)->char_at(0) == JVM_SIGNATURE_ARRAY);
3827 }
3828 if (need_verify) {
3829 guarantee_property(!is_array,
3830 "Bad superclass name in class file %s", CHECK_NULL);
3831 }
3832 }
3833 return super_klass;
3834 }
3835
3836 #ifndef PRODUCT
3837 static void print_field_layout(const Symbol* name,
3838 Array<u2>* fields,
3839 const constantPoolHandle& cp,
3840 int instance_size,
3841 int instance_fields_start,
3842 int instance_fields_end,
3843 int static_fields_end) {
3844
3845 assert(name != NULL, "invariant");
3846
3847 tty->print("%s: field layout\n", name->as_klass_external_name());
3848 tty->print(" @%3d %s\n", instance_fields_start, "--- instance fields start ---");
3849 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3850 if (!fs.access_flags().is_static()) {
3851 tty->print(" @%3d \"%s\" %s\n",
3852 fs.offset(),
3853 fs.name()->as_klass_external_name(),
3854 fs.signature()->as_klass_external_name());
3855 }
3856 }
3857 tty->print(" @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3858 tty->print(" @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3859 tty->print(" @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3860 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3861 if (fs.access_flags().is_static()) {
3862 tty->print(" @%3d \"%s\" %s\n",
3863 fs.offset(),
3864 fs.name()->as_klass_external_name(),
3865 fs.signature()->as_klass_external_name());
3866 }
3867 }
3868 tty->print(" @%3d %s\n", static_fields_end, "--- static fields end ---");
3869 tty->print("\n");
3870 }
3871 #endif
3872
3873 // Values needed for oopmap and InstanceKlass creation
3874 class ClassFileParser::FieldLayoutInfo : public ResourceObj {
3875 public:
3876 OopMapBlocksBuilder* oop_map_blocks;
3877 int instance_size;
3878 int nonstatic_field_size;
3879 int static_field_size;
3880 bool has_nonstatic_fields;
3881 };
3882
3883 // Utility to collect and compact oop maps during layout
3884 class ClassFileParser::OopMapBlocksBuilder : public ResourceObj {
3885 public:
3886 OopMapBlock* nonstatic_oop_maps;
3887 unsigned int nonstatic_oop_map_count;
3888 unsigned int max_nonstatic_oop_maps;
3889
3890 public:
3891 OopMapBlocksBuilder(unsigned int max_blocks, TRAPS) {
3892 max_nonstatic_oop_maps = max_blocks;
3893 nonstatic_oop_map_count = 0;
3894 if (max_blocks == 0) {
3895 nonstatic_oop_maps = NULL;
3896 } else {
3897 nonstatic_oop_maps = NEW_RESOURCE_ARRAY_IN_THREAD(
3898 THREAD, OopMapBlock, max_nonstatic_oop_maps);
3899 memset(nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3900 }
3901 }
3902
3903 OopMapBlock* last_oop_map() const {
3904 assert(nonstatic_oop_map_count > 0, "Has no oop maps");
3905 return nonstatic_oop_maps + (nonstatic_oop_map_count - 1);
3906 }
3907
3908 // addition of super oop maps
3909 void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {
3910 assert(nof_blocks && nonstatic_oop_map_count == 0 &&
3911 nof_blocks <= max_nonstatic_oop_maps, "invariant");
3912
3913 memcpy(nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);
3914 nonstatic_oop_map_count += nof_blocks;
3915 }
3916
3917 // collection of oops
3918 void add(int offset, int count) {
3919 if (nonstatic_oop_map_count == 0) {
3920 nonstatic_oop_map_count++;
3921 }
3922 OopMapBlock* nonstatic_oop_map = last_oop_map();
3923 if (nonstatic_oop_map->count() == 0) { // Unused map, set it up
3924 nonstatic_oop_map->set_offset(offset);
3925 nonstatic_oop_map->set_count(count);
3926 } else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add
3927 nonstatic_oop_map->increment_count(count);
3928 } else { // Need a new one...
3929 nonstatic_oop_map_count++;
3930 assert(nonstatic_oop_map_count <= max_nonstatic_oop_maps, "range check");
3931 nonstatic_oop_map = last_oop_map();
3932 nonstatic_oop_map->set_offset(offset);
3933 nonstatic_oop_map->set_count(count);
3934 }
3935 }
3936
3937 // general purpose copy, e.g. into allocated instanceKlass
3938 void copy(OopMapBlock* dst) {
3939 if (nonstatic_oop_map_count != 0) {
3940 memcpy(dst, nonstatic_oop_maps, sizeof(OopMapBlock) * nonstatic_oop_map_count);
3941 }
3942 }
3943
3944 // Sort and compact adjacent blocks
3945 void compact(TRAPS) {
3946 if (nonstatic_oop_map_count <= 1) {
3947 return;
3948 }
3949 /*
3950 * Since field layout sneeks in oops before values, we will be able to condense
3951 * blocks. There is potential to compact between super, own refs and values
3952 * containing refs.
3953 *
3954 * Currently compaction is slightly limited due to values being 8 byte aligned.
3955 * This may well change: FixMe if doesn't, the code below is fairly general purpose
3956 * and maybe it doesn't need to be.
3957 */
3958 qsort(nonstatic_oop_maps, nonstatic_oop_map_count, sizeof(OopMapBlock),
3959 (_sort_Fn)OopMapBlock::compare_offset);
3960 if (nonstatic_oop_map_count < 2) {
3961 return;
3962 }
3963
3964 //Make a temp copy, and iterate through and copy back into the orig
3965 ResourceMark rm(THREAD);
3966 OopMapBlock* oop_maps_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, OopMapBlock,
3967 nonstatic_oop_map_count);
3968 OopMapBlock* oop_maps_copy_end = oop_maps_copy + nonstatic_oop_map_count;
3969 copy(oop_maps_copy);
3970 OopMapBlock* nonstatic_oop_map = nonstatic_oop_maps;
3971 unsigned int new_count = 1;
3972 oop_maps_copy++;
3973 while(oop_maps_copy < oop_maps_copy_end) {
3974 assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");
3975 if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {
3976 nonstatic_oop_map->increment_count(oop_maps_copy->count());
3977 } else {
3978 nonstatic_oop_map++;
3979 new_count++;
3980 nonstatic_oop_map->set_offset(oop_maps_copy->offset());
3981 nonstatic_oop_map->set_count(oop_maps_copy->count());
3982 }
3983 oop_maps_copy++;
3984 }
3985 assert(new_count <= nonstatic_oop_map_count, "end up with more maps after compact() ?");
3986 nonstatic_oop_map_count = new_count;
3987 }
3988
3989 void print_on(outputStream* st) const {
3990 st->print_cr(" OopMapBlocks: %3d /%3d", nonstatic_oop_map_count, max_nonstatic_oop_maps);
3991 if (nonstatic_oop_map_count > 0) {
3992 OopMapBlock* map = nonstatic_oop_maps;
3993 OopMapBlock* last_map = last_oop_map();
3994 assert(map <= last_map, "Last less than first");
3995 while (map <= last_map) {
3996 st->print_cr(" Offset: %3d -%3d Count: %3d", map->offset(),
3997 map->offset() + map->offset_span() - heapOopSize, map->count());
3998 map++;
3999 }
4000 }
4001 }
4002
4003 void print_value_on(outputStream* st) const {
4004 print_on(st);
4005 }
4006
4007 };
4008
4009 void ClassFileParser::throwValueTypeLimitation(THREAD_AND_LOCATION_DECL,
4010 const char* msg,
4011 const Symbol* name,
4012 const Symbol* sig) const {
4013
4014 ResourceMark rm(THREAD);
4015 if (name == NULL || sig == NULL) {
4016 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4017 vmSymbols::java_lang_ClassFormatError(),
4018 "class: %s - %s", _class_name->as_C_string(), msg);
4019 }
4020 else {
4021 Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4022 vmSymbols::java_lang_ClassFormatError(),
4023 "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
4024 _class_name->as_C_string(), msg);
4025 }
4026 }
4027
4028 // Layout fields and fill in FieldLayoutInfo. Could use more refactoring!
4029 void ClassFileParser::layout_fields(ConstantPool* cp,
4030 const FieldAllocationCount* fac,
4031 const ClassAnnotationCollector* parsed_annotations,
4032 FieldLayoutInfo* info,
4033 TRAPS) {
4034
4035 assert(cp != NULL, "invariant");
4036
4037 // Field size and offset computation
4038 int nonstatic_field_size = _super_klass == NULL ? 0 :
4039 _super_klass->nonstatic_field_size();
4040 int next_nonstatic_valuetype_offset = 0;
4041 int first_nonstatic_valuetype_offset = 0;
4042
4043 // Fields that are value types are handled differently depending if they are static or not:
4044 // - static fields are oops
4045 // - non-static fields are embedded
4046
4047 // Count the contended fields by type.
4048 //
4049 // We ignore static fields, because @Contended is not supported for them.
4050 // The layout code below will also ignore the static fields.
4051 int nonstatic_contended_count = 0;
4052 FieldAllocationCount fac_contended;
4053 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4054 FieldAllocationType atype = (FieldAllocationType) fs.allocation_type();
4055 if (fs.is_contended()) {
4056 fac_contended.count[atype]++;
4057 if (!fs.access_flags().is_static()) {
4058 nonstatic_contended_count++;
4059 }
4060 }
4061 }
4062
4063
4064 // Calculate the starting byte offsets
4065 int next_static_oop_offset = InstanceMirrorKlass::offset_of_static_fields();
4066 // Value types in static fields are not embedded, they are handled with oops
4067 int next_static_double_offset = next_static_oop_offset +
4068 ((fac->count[STATIC_OOP] + fac->count[STATIC_FLATTENABLE]) * heapOopSize);
4069 if (fac->count[STATIC_DOUBLE]) {
4070 next_static_double_offset = align_up(next_static_double_offset, BytesPerLong);
4071 }
4072
4073 int next_static_word_offset = next_static_double_offset +
4074 ((fac->count[STATIC_DOUBLE]) * BytesPerLong);
4075 int next_static_short_offset = next_static_word_offset +
4076 ((fac->count[STATIC_WORD]) * BytesPerInt);
4077 int next_static_byte_offset = next_static_short_offset +
4078 ((fac->count[STATIC_SHORT]) * BytesPerShort);
4079
4080 int nonstatic_fields_start = instanceOopDesc::base_offset_in_bytes() +
4081 nonstatic_field_size * heapOopSize;
4082
4083 // First field of value types is aligned on a long boundary in order to ease
4084 // in-lining of value types (with header removal) in packed arrays and
4085 // flatten value types
4086 int initial_value_type_padding = 0;
4087 if (is_value_type()) {
4088 int old = nonstatic_fields_start;
4089 nonstatic_fields_start = align_up(nonstatic_fields_start, BytesPerLong);
4090 initial_value_type_padding = nonstatic_fields_start - old;
4091 }
4092
4093 int next_nonstatic_field_offset = nonstatic_fields_start;
4094
4095 const bool is_contended_class = parsed_annotations->is_contended();
4096
4097 // Class is contended, pad before all the fields
4098 if (is_contended_class) {
4099 next_nonstatic_field_offset += ContendedPaddingWidth;
4100 }
4101
4102 // Temporary value types restrictions
4103 if (is_value_type()) {
4104 if (is_contended_class) {
4105 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support @Contended annotation yet");
4106 return;
4107 }
4108 }
4109
4110 // Compute the non-contended fields count.
4111 // The packing code below relies on these counts to determine if some field
4112 // can be squeezed into the alignment gap. Contended fields are obviously
4113 // exempt from that.
4114 unsigned int nonstatic_double_count = fac->count[NONSTATIC_DOUBLE] - fac_contended.count[NONSTATIC_DOUBLE];
4115 unsigned int nonstatic_word_count = fac->count[NONSTATIC_WORD] - fac_contended.count[NONSTATIC_WORD];
4116 unsigned int nonstatic_short_count = fac->count[NONSTATIC_SHORT] - fac_contended.count[NONSTATIC_SHORT];
4117 unsigned int nonstatic_byte_count = fac->count[NONSTATIC_BYTE] - fac_contended.count[NONSTATIC_BYTE];
4118 unsigned int nonstatic_oop_count = fac->count[NONSTATIC_OOP] - fac_contended.count[NONSTATIC_OOP];
4119
4120 int static_value_type_count = 0;
4121 int nonstatic_value_type_count = 0;
4122 int* nonstatic_value_type_indexes = NULL;
4123 Klass** nonstatic_value_type_klasses = NULL;
4124 unsigned int value_type_oop_map_count = 0;
4125 int not_flattened_value_types = 0;
4126
4127 int max_nonstatic_value_type = fac->count[NONSTATIC_FLATTENABLE] + 1;
4128
4129 nonstatic_value_type_indexes = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, int,
4130 max_nonstatic_value_type);
4131 for (int i = 0; i < max_nonstatic_value_type; i++) {
4132 nonstatic_value_type_indexes[i] = -1;
4133 }
4134 nonstatic_value_type_klasses = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, Klass*,
4135 max_nonstatic_value_type);
4136
4137 for (AllFieldStream fs(_fields, _cp); !fs.done(); fs.next()) {
4138 if (fs.allocation_type() == STATIC_FLATTENABLE) {
4139 // Pre-resolve the flattenable field and check for value type circularity
4140 // issues. Note that super-class circularity checks are not needed here
4141 // because flattenable fields can only be in value types and value types
4142 // only have java.lang.Object as their super class.
4143 // Also, note that super-interface circularity checks are not needed
4144 // because interfaces cannot be value types.
4145 ResourceMark rm;
4146 if (!fs.signature()->is_Q_signature()) {
4147 THROW(vmSymbols::java_lang_ClassFormatError());
4148 }
4149 Klass* klass =
4150 SystemDictionary::resolve_flattenable_field_or_fail(&fs,
4151 Handle(THREAD, _loader_data->class_loader()),
4152 _protection_domain, true, CHECK);
4153 assert(klass != NULL, "Sanity check");
4154 if (!klass->access_flags().is_value_type()) {
4155 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
4156 }
4157 static_value_type_count++;
4158 } else if (fs.allocation_type() == NONSTATIC_FLATTENABLE) {
4159 // Pre-resolve the flattenable field and check for value type circularity issues.
4160 ResourceMark rm;
4161 if (!fs.signature()->is_Q_signature()) {
4162 THROW(vmSymbols::java_lang_ClassFormatError());
4163 }
4164 Klass* klass =
4165 SystemDictionary::resolve_flattenable_field_or_fail(&fs,
4166 Handle(THREAD, _loader_data->class_loader()),
4167 _protection_domain, true, CHECK);
4168 assert(klass != NULL, "Sanity check");
4169 if (!klass->access_flags().is_value_type()) {
4170 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
4171 }
4172 ValueKlass* vk = ValueKlass::cast(klass);
4173 // Conditions to apply flattening or not should be defined in a single place
4174 if ((ValueFieldMaxFlatSize < 0) || (vk->size_helper() * HeapWordSize) <= ValueFieldMaxFlatSize) {
4175 nonstatic_value_type_indexes[nonstatic_value_type_count] = fs.index();
4176 nonstatic_value_type_klasses[nonstatic_value_type_count] = klass;
4177 nonstatic_value_type_count++;
4178
4179 ValueKlass* vklass = ValueKlass::cast(klass);
4180 if (vklass->contains_oops()) {
4181 value_type_oop_map_count += vklass->nonstatic_oop_map_count();
4182 }
4183 fs.set_flattened(true);
4184 } else {
4185 not_flattened_value_types++;
4186 fs.set_flattened(false);
4187 }
4188 }
4189 }
4190
4191 // Adjusting non_static_oop_count to take into account not flattened value types;
4192 nonstatic_oop_count += not_flattened_value_types;
4193
4194 // Total non-static fields count, including every contended field
4195 unsigned int nonstatic_fields_count = fac->count[NONSTATIC_DOUBLE] + fac->count[NONSTATIC_WORD] +
4196 fac->count[NONSTATIC_SHORT] + fac->count[NONSTATIC_BYTE] +
4197 fac->count[NONSTATIC_OOP] + fac->count[NONSTATIC_FLATTENABLE];
4198
4199 const bool super_has_nonstatic_fields =
4200 (_super_klass != NULL && _super_klass->has_nonstatic_fields());
4201 const bool has_nonstatic_fields =
4202 super_has_nonstatic_fields || (nonstatic_fields_count != 0);
4203 const bool has_nonstatic_value_fields = nonstatic_value_type_count > 0;
4204
4205 if (is_value_type() && (!has_nonstatic_fields)) {
4206 // There are a number of fixes required throughout the type system and JIT
4207 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support zero instance size yet");
4208 return;
4209 }
4210
4211 // Prepare list of oops for oop map generation.
4212 //
4213 // "offset" and "count" lists are describing the set of contiguous oop
4214 // regions. offset[i] is the start of the i-th region, which then has
4215 // count[i] oops following. Before we know how many regions are required,
4216 // we pessimistically allocate the maps to fit all the oops into the
4217 // distinct regions.
4218 //
4219 int super_oop_map_count = (_super_klass == NULL) ? 0 :_super_klass->nonstatic_oop_map_count();
4220 int max_oop_map_count =
4221 super_oop_map_count +
4222 fac->count[NONSTATIC_OOP] +
4223 value_type_oop_map_count +
4224 not_flattened_value_types;
4225
4226 OopMapBlocksBuilder* nonstatic_oop_maps = new OopMapBlocksBuilder(max_oop_map_count, THREAD);
4227 if (super_oop_map_count > 0) {
4228 nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(),
4229 _super_klass->nonstatic_oop_map_count());
4230 }
4231
4232 int first_nonstatic_oop_offset = 0; // will be set for first oop field
4233
4234 bool compact_fields = CompactFields;
4235 int allocation_style = FieldsAllocationStyle;
4236 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
4237 assert(false, "0 <= FieldsAllocationStyle <= 2");
4238 allocation_style = 1; // Optimistic
4239 }
4240
4241 // The next classes have predefined hard-coded fields offsets
4242 // (see in JavaClasses::compute_hard_coded_offsets()).
4243 // Use default fields allocation order for them.
4244 if( (allocation_style != 0 || compact_fields ) && _loader_data->class_loader() == NULL &&
4245 (_class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
4246 _class_name == vmSymbols::java_lang_Class() ||
4247 _class_name == vmSymbols::java_lang_ClassLoader() ||
4248 _class_name == vmSymbols::java_lang_ref_Reference() ||
4249 _class_name == vmSymbols::java_lang_ref_SoftReference() ||
4250 _class_name == vmSymbols::java_lang_StackTraceElement() ||
4259 _class_name == vmSymbols::java_lang_Integer() ||
4260 _class_name == vmSymbols::java_lang_Long())) {
4261 allocation_style = 0; // Allocate oops first
4262 compact_fields = false; // Don't compact fields
4263 }
4264
4265 int next_nonstatic_oop_offset = 0;
4266 int next_nonstatic_double_offset = 0;
4267
4268 // Rearrange fields for a given allocation style
4269 if( allocation_style == 0 ) {
4270 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes, padded fields
4271 next_nonstatic_oop_offset = next_nonstatic_field_offset;
4272 next_nonstatic_double_offset = next_nonstatic_oop_offset +
4273 (nonstatic_oop_count * heapOopSize);
4274 } else if( allocation_style == 1 ) {
4275 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops, padded fields
4276 next_nonstatic_double_offset = next_nonstatic_field_offset;
4277 } else if( allocation_style == 2 ) {
4278 // Fields allocation: oops fields in super and sub classes are together.
4279 if( nonstatic_field_size > 0 && super_oop_map_count > 0 ) {
4280 if (next_nonstatic_field_offset == nonstatic_oop_maps->last_oop_map()->end_offset()) {
4281 allocation_style = 0; // allocate oops first
4282 next_nonstatic_oop_offset = next_nonstatic_field_offset;
4283 next_nonstatic_double_offset = next_nonstatic_oop_offset +
4284 (nonstatic_oop_count * heapOopSize);
4285 }
4286 }
4287 if( allocation_style == 2 ) {
4288 allocation_style = 1; // allocate oops last
4289 next_nonstatic_double_offset = next_nonstatic_field_offset;
4290 }
4291 } else {
4292 ShouldNotReachHere();
4293 }
4294
4295 int nonstatic_oop_space_count = 0;
4296 int nonstatic_word_space_count = 0;
4297 int nonstatic_short_space_count = 0;
4298 int nonstatic_byte_space_count = 0;
4299 int nonstatic_oop_space_offset = 0;
4300 int nonstatic_word_space_offset = 0;
4343 }
4344
4345 int next_nonstatic_word_offset = next_nonstatic_double_offset +
4346 (nonstatic_double_count * BytesPerLong);
4347 int next_nonstatic_short_offset = next_nonstatic_word_offset +
4348 (nonstatic_word_count * BytesPerInt);
4349 int next_nonstatic_byte_offset = next_nonstatic_short_offset +
4350 (nonstatic_short_count * BytesPerShort);
4351 int next_nonstatic_padded_offset = next_nonstatic_byte_offset +
4352 nonstatic_byte_count;
4353
4354 // let oops jump before padding with this allocation style
4355 if( allocation_style == 1 ) {
4356 next_nonstatic_oop_offset = next_nonstatic_padded_offset;
4357 if( nonstatic_oop_count > 0 ) {
4358 next_nonstatic_oop_offset = align_up(next_nonstatic_oop_offset, heapOopSize);
4359 }
4360 next_nonstatic_padded_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
4361 }
4362
4363 // Aligning embedded value types
4364 // bug below, the current algorithm to layout embedded value types always put them at the
4365 // end of the layout, which doesn't match the different allocation policies the VM is
4366 // supposed to provide => FixMe
4367 // Note also that the current alignment policy is to make each value type starting on a
4368 // 64 bits boundary. This could be optimized later. For instance, it could be nice to
4369 // align value types according to their most constrained internal type.
4370 next_nonstatic_valuetype_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4371 int next_value_type_index = 0;
4372
4373 // Iterate over fields again and compute correct offsets.
4374 // The field allocation type was temporarily stored in the offset slot.
4375 // oop fields are located before non-oop fields (static and non-static).
4376 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
4377
4378 // skip already laid out fields
4379 if (fs.is_offset_set()) continue;
4380
4381 // contended instance fields are handled below
4382 if (fs.is_contended() && !fs.access_flags().is_static()) continue;
4383
4384 int real_offset = 0;
4385 const FieldAllocationType atype = (const FieldAllocationType) fs.allocation_type();
4386
4387 // pack the rest of the fields
4388 switch (atype) {
4389 // Value types in static fields are handled with oops
4390 case STATIC_FLATTENABLE: // Fallthrough
4391 case STATIC_OOP:
4392 real_offset = next_static_oop_offset;
4393 next_static_oop_offset += heapOopSize;
4394 break;
4395 case STATIC_BYTE:
4396 real_offset = next_static_byte_offset;
4397 next_static_byte_offset += 1;
4398 break;
4399 case STATIC_SHORT:
4400 real_offset = next_static_short_offset;
4401 next_static_short_offset += BytesPerShort;
4402 break;
4403 case STATIC_WORD:
4404 real_offset = next_static_word_offset;
4405 next_static_word_offset += BytesPerInt;
4406 break;
4407 case STATIC_DOUBLE:
4408 real_offset = next_static_double_offset;
4409 next_static_double_offset += BytesPerLong;
4410 break;
4411 case NONSTATIC_FLATTENABLE:
4412 if (fs.is_flattened()) {
4413 Klass* klass = nonstatic_value_type_klasses[next_value_type_index];
4414 assert(klass != NULL, "Klass should have been loaded and resolved earlier");
4415 assert(klass->access_flags().is_value_type(),"Must be a value type");
4416 ValueKlass* vklass = ValueKlass::cast(klass);
4417 real_offset = next_nonstatic_valuetype_offset;
4418 next_nonstatic_valuetype_offset += (vklass->size_helper()) * wordSize - vklass->first_field_offset();
4419 // aligning next value type on a 64 bits boundary
4420 next_nonstatic_valuetype_offset = align_up(next_nonstatic_valuetype_offset, BytesPerLong);
4421 next_value_type_index += 1;
4422
4423 if (vklass->contains_oops()) { // add flatten oop maps
4424 int diff = real_offset - vklass->first_field_offset();
4425 const OopMapBlock* map = vklass->start_of_nonstatic_oop_maps();
4426 const OopMapBlock* const last_map = map + vklass->nonstatic_oop_map_count();
4427 while (map < last_map) {
4428 nonstatic_oop_maps->add(map->offset() + diff, map->count());
4429 map++;
4430 }
4431 }
4432 break;
4433 } else {
4434 // Fall through
4435 }
4436 case NONSTATIC_OOP:
4437 if( nonstatic_oop_space_count > 0 ) {
4438 real_offset = nonstatic_oop_space_offset;
4439 nonstatic_oop_space_offset += heapOopSize;
4440 nonstatic_oop_space_count -= 1;
4441 } else {
4442 real_offset = next_nonstatic_oop_offset;
4443 next_nonstatic_oop_offset += heapOopSize;
4444 }
4445 nonstatic_oop_maps->add(real_offset, 1);
4446 break;
4447 case NONSTATIC_BYTE:
4448 if( nonstatic_byte_space_count > 0 ) {
4449 real_offset = nonstatic_byte_space_offset;
4450 nonstatic_byte_space_offset += 1;
4451 nonstatic_byte_space_count -= 1;
4452 } else {
4453 real_offset = next_nonstatic_byte_offset;
4454 next_nonstatic_byte_offset += 1;
4455 }
4456 break;
4457 case NONSTATIC_SHORT:
4458 if( nonstatic_short_space_count > 0 ) {
4459 real_offset = nonstatic_short_space_offset;
4460 nonstatic_short_space_offset += BytesPerShort;
4461 nonstatic_short_space_count -= 1;
4462 } else {
4463 real_offset = next_nonstatic_short_offset;
4464 next_nonstatic_short_offset += BytesPerShort;
4465 }
4534 break;
4535
4536 case NONSTATIC_SHORT:
4537 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerShort);
4538 real_offset = next_nonstatic_padded_offset;
4539 next_nonstatic_padded_offset += BytesPerShort;
4540 break;
4541
4542 case NONSTATIC_WORD:
4543 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerInt);
4544 real_offset = next_nonstatic_padded_offset;
4545 next_nonstatic_padded_offset += BytesPerInt;
4546 break;
4547
4548 case NONSTATIC_DOUBLE:
4549 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, BytesPerLong);
4550 real_offset = next_nonstatic_padded_offset;
4551 next_nonstatic_padded_offset += BytesPerLong;
4552 break;
4553
4554 // Value types in static fields are handled with oops
4555 case NONSTATIC_FLATTENABLE:
4556 throwValueTypeLimitation(THREAD_AND_LOCATION,
4557 "@Contended annotation not supported for value types yet", fs.name(), fs.signature());
4558 return;
4559
4560 case NONSTATIC_OOP:
4561 next_nonstatic_padded_offset = align_up(next_nonstatic_padded_offset, heapOopSize);
4562 real_offset = next_nonstatic_padded_offset;
4563 next_nonstatic_padded_offset += heapOopSize;
4564 nonstatic_oop_maps->add(real_offset, 1);
4565 break;
4566
4567 default:
4568 ShouldNotReachHere();
4569 }
4570
4571 if (fs.contended_group() == 0) {
4572 // Contended group defines the equivalence class over the fields:
4573 // the fields within the same contended group are not inter-padded.
4574 // The only exception is default group, which does not incur the
4575 // equivalence, and so requires intra-padding.
4576 next_nonstatic_padded_offset += ContendedPaddingWidth;
4577 }
4578
4579 fs.set_offset(real_offset);
4580 } // for
4581
4582 // Start laying out the next group.
4583 // Note that this will effectively pad the last group in the back;
4584 // this is expected to alleviate memory contention effects for
4585 // subclass fields and/or adjacent object.
4586 // If this was the default group, the padding is already in place.
4587 if (current_group != 0) {
4588 next_nonstatic_padded_offset += ContendedPaddingWidth;
4589 }
4590 }
4591
4592 // handle static fields
4593 }
4594
4595 // Entire class is contended, pad in the back.
4596 // This helps to alleviate memory contention effects for subclass fields
4597 // and/or adjacent object.
4598 if (is_contended_class) {
4599 assert(!is_value_type(), "@Contended not supported for value types yet");
4600 next_nonstatic_padded_offset += ContendedPaddingWidth;
4601 }
4602
4603 int notaligned_nonstatic_fields_end;
4604 if (nonstatic_value_type_count != 0) {
4605 notaligned_nonstatic_fields_end = next_nonstatic_valuetype_offset;
4606 } else {
4607 notaligned_nonstatic_fields_end = next_nonstatic_padded_offset;
4608 }
4609
4610 int nonstatic_field_sz_align = heapOopSize;
4611 if (is_value_type()) {
4612 if ((notaligned_nonstatic_fields_end - nonstatic_fields_start) > heapOopSize) {
4613 nonstatic_field_sz_align = BytesPerLong; // value copy of fields only uses jlong copy
4614 }
4615 }
4616 int nonstatic_fields_end = align_up(notaligned_nonstatic_fields_end, nonstatic_field_sz_align);
4617 int instance_end = align_up(notaligned_nonstatic_fields_end, wordSize);
4618 int static_fields_end = align_up(next_static_byte_offset, wordSize);
4619
4620 int static_field_size = (static_fields_end -
4621 InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
4622 nonstatic_field_size = nonstatic_field_size +
4623 (nonstatic_fields_end - nonstatic_fields_start) / heapOopSize;
4624
4625 int instance_size = align_object_size(instance_end / wordSize);
4626
4627 assert(instance_size == align_object_size(align_up(
4628 (instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize)
4629 + initial_value_type_padding, wordSize) / wordSize), "consistent layout helper value");
4630
4631
4632 // Invariant: nonstatic_field end/start should only change if there are
4633 // nonstatic fields in the class, or if the class is contended. We compare
4634 // against the non-aligned value, so that end alignment will not fail the
4635 // assert without actually having the fields.
4636 assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4637 is_contended_class ||
4638 (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4639
4640 // Number of non-static oop map blocks allocated at end of klass.
4641 nonstatic_oop_maps->compact(THREAD);
4642
4643 #ifndef PRODUCT
4644 if ((PrintFieldLayout && !is_value_type()) ||
4645 (PrintValueLayout && (is_value_type() || has_nonstatic_value_fields))) {
4646 print_field_layout(_class_name,
4647 _fields,
4648 cp,
4649 instance_size,
4650 nonstatic_fields_start,
4651 nonstatic_fields_end,
4652 static_fields_end);
4653 nonstatic_oop_maps->print_on(tty);
4654 tty->print("\n");
4655 }
4656
4657 #endif
4658 // Pass back information needed for InstanceKlass creation
4659 info->oop_map_blocks = nonstatic_oop_maps;
4660 info->instance_size = instance_size;
4661 info->static_field_size = static_field_size;
4662 info->nonstatic_field_size = nonstatic_field_size;
4663 info->has_nonstatic_fields = has_nonstatic_fields;
4664 }
4665
4666 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik, TRAPS) {
4667 assert(ik != NULL, "invariant");
4668
4669 const Klass* const super = ik->super();
4670
4671 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4672 // in which case we don't have to register objects as finalizable
4673 if (!_has_empty_finalizer) {
4674 if (_has_finalizer ||
4675 (super != NULL && super->has_finalizer())) {
4676 ik->set_has_finalizer();
4677 }
4678 }
4679
4680 #ifdef ASSERT
4681 bool f = false;
4682 const Method* const m = ik->lookup_method(vmSymbols::finalize_method_name(),
4683 vmSymbols::void_method_signature());
4684 if (m != NULL && !m->is_empty_method()) {
4685 f = true;
4686 }
4687
4688 // Spec doesn't prevent agent from redefinition of empty finalizer.
4689 // Despite the fact that it's generally bad idea and redefined finalizer
4690 // will not work as expected we shouldn't abort vm in this case
4691 if (!ik->has_redefined_this_or_super()) {
4692 assert(ik->has_finalizer() == f, "inconsistent has_finalizer");
4693 }
4694 #endif
4695
4696 // Check if this klass supports the java.lang.Cloneable interface
4697 if (SystemDictionary::Cloneable_klass_loaded()) {
4698 if (ik->is_subtype_of(SystemDictionary::Cloneable_klass())) {
4699 if (ik->is_value()) {
4700 throwValueTypeLimitation(THREAD_AND_LOCATION, "Value Types do not support Cloneable");
4701 return;
4702 }
4703 ik->set_is_cloneable();
4704 }
4705 }
4706
4707 // Check if this klass has a vanilla default constructor
4708 if (super == NULL) {
4709 // java.lang.Object has empty default constructor
4710 ik->set_has_vanilla_constructor();
4711 } else {
4712 if (super->has_vanilla_constructor() &&
4713 _has_vanilla_constructor) {
4714 ik->set_has_vanilla_constructor();
4715 }
4716 #ifdef ASSERT
4717 bool v = false;
4718 if (super->has_vanilla_constructor()) {
4719 const Method* const constructor =
4720 ik->find_method(vmSymbols::object_initializer_name(),
4721 vmSymbols::void_method_signature());
4722 if (constructor != NULL && constructor->is_vanilla_constructor()) {
4723 v = true;
4724 }
4725 }
4726 assert(v == ik->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
4727 #endif
4728 }
4729
4730 // If it cannot be fast-path allocated, set a bit in the layout helper.
4731 // See documentation of InstanceKlass::can_be_fastpath_allocated().
4732 assert(ik->size_helper() > 0, "layout_helper is initialized");
4733 if ((!RegisterFinalizersAtInit && ik->has_finalizer())
4734 || ik->is_abstract() || ik->is_interface()
4735 || (ik->name() == vmSymbols::java_lang_Class() && ik->class_loader() == NULL)
4736 || ik->size_helper() >= FastAllocateSizeLimit) {
4737 // Forbid fast-path allocation.
4738 const jint lh = Klass::instance_layout_helper(ik->size_helper(), true);
4739 ik->set_layout_helper(lh);
4740 }
4741 }
4742
4743 bool ClassFileParser::supports_value_types() const {
4744 // Value types are only supported by class file version 55 and later
4745 return _major_version >= JAVA_11_VERSION;
4746 }
4747
4748 // utility methods for appending an array with check for duplicates
4749
4750 static void append_interfaces(GrowableArray<InstanceKlass*>* result,
4751 const Array<InstanceKlass*>* const ifs) {
4752 // iterate over new interfaces
4753 for (int i = 0; i < ifs->length(); i++) {
4754 InstanceKlass* const e = ifs->at(i);
4755 assert(e->is_klass() && e->is_interface(), "just checking");
4756 // add new interface
4757 result->append_if_missing(e);
4758 }
4759 }
4760
4761 static Array<InstanceKlass*>* compute_transitive_interfaces(const InstanceKlass* super,
4762 Array<InstanceKlass*>* local_ifs,
4763 ClassLoaderData* loader_data,
4764 TRAPS) {
4765 assert(local_ifs != NULL, "invariant");
4766 assert(loader_data != NULL, "invariant");
4767
4990 const Method* const m = methods->at(index);
4991 // if m is static and not the init method, throw a verify error
4992 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4993 ResourceMark rm(THREAD);
4994 Exceptions::fthrow(
4995 THREAD_AND_LOCATION,
4996 vmSymbols::java_lang_VerifyError(),
4997 "Illegal static method %s in interface %s",
4998 m->name()->as_C_string(),
4999 this_klass->external_name()
5000 );
5001 return;
5002 }
5003 }
5004 }
5005
5006 // utility methods for format checking
5007
5008 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
5009 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
5010 const bool is_value_type = (flags & JVM_ACC_VALUE) != 0;
5011 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
5012 assert(supports_value_types() || !is_value_type, "JVM_ACC_VALUE should not be set");
5013 if (is_module) {
5014 ResourceMark rm(THREAD);
5015 Exceptions::fthrow(
5016 THREAD_AND_LOCATION,
5017 vmSymbols::java_lang_NoClassDefFoundError(),
5018 "%s is not a class because access_flag ACC_MODULE is set",
5019 _class_name->as_C_string());
5020 return;
5021 }
5022
5023 if (is_value_type && !EnableValhalla) {
5024 ResourceMark rm(THREAD);
5025 Exceptions::fthrow(
5026 THREAD_AND_LOCATION,
5027 vmSymbols::java_lang_ClassFormatError(),
5028 "Class modifier ACC_VALUE in class %s requires option -XX:+EnableValhalla",
5029 _class_name->as_C_string()
5030 );
5031 }
5032
5033 if (!_need_verify) { return; }
5034
5035 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
5036 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
5037 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
5038 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
5039 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
5040 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
5041 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
5042
5043 if ((is_abstract && is_final) ||
5044 (is_interface && !is_abstract) ||
5045 (is_interface && major_gte_15 && (is_super || is_enum)) ||
5046 (!is_interface && major_gte_15 && is_annotation) ||
5047 (is_value_type && (is_interface || is_abstract || is_enum || !is_final))) {
5048 ResourceMark rm(THREAD);
5049 Exceptions::fthrow(
5050 THREAD_AND_LOCATION,
5051 vmSymbols::java_lang_ClassFormatError(),
5052 "Illegal class modifiers in class %s: 0x%X",
5053 _class_name->as_C_string(), flags
5054 );
5055 return;
5056 }
5057 }
5058
5059 static bool has_illegal_visibility(jint flags) {
5060 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
5061 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5062 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
5063
5064 return ((is_public && is_protected) ||
5065 (is_public && is_private) ||
5066 (is_protected && is_private));
5067 }
5110 ResourceMark rm(THREAD);
5111 Exceptions::fthrow(
5112 THREAD_AND_LOCATION,
5113 vmSymbols::java_lang_UnsupportedClassVersionError(),
5114 "%s (class file version %u.%u) was compiled with an invalid major version",
5115 class_name->as_C_string(), major, minor);
5116 } else if (minor != 0) {
5117 ResourceMark rm(THREAD);
5118 Exceptions::fthrow(
5119 THREAD_AND_LOCATION,
5120 vmSymbols::java_lang_UnsupportedClassVersionError(),
5121 "%s (class file version %u.%u) was compiled with an invalid non-zero minor version",
5122 class_name->as_C_string(), major, minor);
5123 }
5124 }
5125 }
5126 }
5127
5128 void ClassFileParser::verify_legal_field_modifiers(jint flags,
5129 bool is_interface,
5130 bool is_value_type,
5131 TRAPS) const {
5132 if (!_need_verify) { return; }
5133
5134 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
5135 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5136 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
5137 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
5138 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
5139 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
5140 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
5141 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
5142 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
5143
5144 bool is_illegal = false;
5145
5146 if (is_interface) {
5147 if (!is_public || !is_static || !is_final || is_private ||
5148 is_protected || is_volatile || is_transient ||
5149 (major_gte_15 && is_enum)) {
5150 is_illegal = true;
5151 }
5152 } else { // not interface
5153 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
5154 is_illegal = true;
5155 } else {
5156 if (is_value_type && !is_static && !is_final) {
5157 is_illegal = true;
5158 }
5159 }
5160 }
5161
5162 if (is_illegal) {
5163 ResourceMark rm(THREAD);
5164 Exceptions::fthrow(
5165 THREAD_AND_LOCATION,
5166 vmSymbols::java_lang_ClassFormatError(),
5167 "Illegal field modifiers in class %s: 0x%X",
5168 _class_name->as_C_string(), flags);
5169 return;
5170 }
5171 }
5172
5173 void ClassFileParser::verify_legal_method_modifiers(jint flags,
5174 bool is_interface,
5175 bool is_value_type,
5176 const Symbol* name,
5177 TRAPS) const {
5178 if (!_need_verify) { return; }
5179
5180 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
5181 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
5182 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
5183 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
5184 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
5185 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
5186 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
5187 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
5188 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
5189 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
5190 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
5191 const bool major_gte_8 = _major_version >= JAVA_8_VERSION;
5192 const bool is_initializer = (name == vmSymbols::object_initializer_name());
5193
5194 bool is_illegal = false;
5195
5215 if (!is_public || is_private || is_protected || is_static || is_final ||
5216 is_synchronized || is_native || !is_abstract || is_strict) {
5217 is_illegal = true;
5218 }
5219 } else {
5220 // Class file version is pre-JAVA_1_5_VERSION
5221 if (!is_public || is_static || is_final || is_native || !is_abstract) {
5222 is_illegal = true;
5223 }
5224 }
5225 } else { // not interface
5226 if (has_illegal_visibility(flags)) {
5227 is_illegal = true;
5228 } else {
5229 if (is_initializer) {
5230 if (is_static || is_final || is_synchronized || is_native ||
5231 is_abstract || (major_gte_15 && is_bridge)) {
5232 is_illegal = true;
5233 }
5234 } else { // not initializer
5235 if (is_value_type && is_synchronized && !is_static) {
5236 is_illegal = true;
5237 } else {
5238 if (is_abstract) {
5239 if ((is_final || is_native || is_private || is_static ||
5240 (major_gte_15 && (is_synchronized || is_strict)))) {
5241 is_illegal = true;
5242 }
5243 }
5244 }
5245 }
5246 }
5247 }
5248
5249 if (is_illegal) {
5250 ResourceMark rm(THREAD);
5251 Exceptions::fthrow(
5252 THREAD_AND_LOCATION,
5253 vmSymbols::java_lang_ClassFormatError(),
5254 "Method %s in class %s has illegal modifiers: 0x%X",
5255 name->as_C_string(), _class_name->as_C_string(), flags);
5256 return;
5257 }
5258 }
5259
5260 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer,
5261 int length,
5262 TRAPS) const {
5263 assert(_need_verify, "only called when _need_verify is true");
5264 if (!UTF8::is_legal_utf8(buffer, length, _major_version <= 47)) {
5265 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
5266 }
5267 }
5391 // be taken as a field signature. Allow "void" if void_ok.
5392 // Return a pointer to just past the signature.
5393 // Return NULL if no legal signature is found.
5394 const char* ClassFileParser::skip_over_field_signature(const char* signature,
5395 bool void_ok,
5396 unsigned int length,
5397 TRAPS) const {
5398 unsigned int array_dim = 0;
5399 while (length > 0) {
5400 switch (signature[0]) {
5401 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
5402 case JVM_SIGNATURE_BOOLEAN:
5403 case JVM_SIGNATURE_BYTE:
5404 case JVM_SIGNATURE_CHAR:
5405 case JVM_SIGNATURE_SHORT:
5406 case JVM_SIGNATURE_INT:
5407 case JVM_SIGNATURE_FLOAT:
5408 case JVM_SIGNATURE_LONG:
5409 case JVM_SIGNATURE_DOUBLE:
5410 return signature + 1;
5411 case JVM_SIGNATURE_VALUETYPE:
5412 // Can't enable this check until JDK upgrades the bytecode generators
5413 // if (_major_version < CONSTANT_CLASS_DESCRIPTORS ) {
5414 // classfile_parse_error("Class name contains illegal Q-signature "
5415 // "in descriptor in class file %s",
5416 // CHECK_0);
5417 // }
5418 // fall through
5419 case JVM_SIGNATURE_CLASS:
5420 {
5421 if (_major_version < JAVA_1_5_VERSION) {
5422 // Skip over the class name if one is there
5423 const char* const p = skip_over_field_name(signature + 1, true, --length);
5424
5425 // The next character better be a semicolon
5426 if (p && (p - signature) > 1 && p[0] == ';') {
5427 return p + 1;
5428 }
5429 }
5430 else {
5431 // Skip leading 'L' or 'Q' and ignore first appearance of ';'
5432 signature++;
5433 const char* c = (const char*) memchr(signature, ';', length - 1);
5434 // Format check signature
5435 if (c != NULL) {
5436 int newlen = c - (char*) signature;
5437 bool legal = verify_unqualified_name(signature, newlen, LegalClass);
5438 if (!legal) {
5439 classfile_parse_error("Class name contains illegal character "
5440 "in descriptor in class file %s",
5441 CHECK_0);
5442 return NULL;
5443 }
5444 return signature + newlen + 1;
5445 }
5446 }
5447 return NULL;
5448 }
5449 case JVM_SIGNATURE_ARRAY:
5450 array_dim++;
5451 if (array_dim > 255) {
5466
5467 // Checks if name is a legal class name.
5468 void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
5469 if (!_need_verify || _relax_verify) { return; }
5470
5471 assert(name->refcount() > 0, "symbol must be kept alive");
5472 char* bytes = (char*)name->bytes();
5473 unsigned int length = name->utf8_length();
5474 bool legal = false;
5475
5476 if (length > 0) {
5477 const char* p;
5478 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
5479 p = skip_over_field_signature(bytes, false, length, CHECK);
5480 legal = (p != NULL) && ((p - bytes) == (int)length);
5481 } else if (_major_version < JAVA_1_5_VERSION) {
5482 if (bytes[0] != '<') {
5483 p = skip_over_field_name(bytes, true, length);
5484 legal = (p != NULL) && ((p - bytes) == (int)length);
5485 }
5486 } else if (_major_version >= CONSTANT_CLASS_DESCRIPTORS && bytes[length - 1] == ';' ) {
5487 // Support for L...; and Q...; descriptors
5488 legal = verify_unqualified_name(bytes + 1, length - 2, LegalClass);
5489 } else {
5490 // 4900761: relax the constraints based on JSR202 spec
5491 // Class names may be drawn from the entire Unicode character set.
5492 // Identifiers between '/' must be unqualified names.
5493 // The utf8 string has been verified when parsing cpool entries.
5494 legal = verify_unqualified_name(bytes, length, LegalClass);
5495 }
5496 }
5497 if (!legal) {
5498 ResourceMark rm(THREAD);
5499 assert(_class_name != NULL, "invariant");
5500 Exceptions::fthrow(
5501 THREAD_AND_LOCATION,
5502 vmSymbols::java_lang_ClassFormatError(),
5503 "Illegal class name \"%.*s\" in class file %s", length, bytes,
5504 _class_name->as_C_string()
5505 );
5506 return;
5507 }
5508 }
5643 // Now we better just have a return value
5644 nextp = skip_over_field_signature(p, true, length, CHECK_0);
5645 if (nextp && ((int)length == (nextp - p))) {
5646 return args_size;
5647 }
5648 }
5649 }
5650 }
5651 // Report error
5652 throwIllegalSignature("Method", name, signature, CHECK_0);
5653 return 0;
5654 }
5655
5656 int ClassFileParser::static_field_size() const {
5657 assert(_field_info != NULL, "invariant");
5658 return _field_info->static_field_size;
5659 }
5660
5661 int ClassFileParser::total_oop_map_count() const {
5662 assert(_field_info != NULL, "invariant");
5663 return _field_info->oop_map_blocks->nonstatic_oop_map_count;
5664 }
5665
5666 jint ClassFileParser::layout_size() const {
5667 assert(_field_info != NULL, "invariant");
5668 return _field_info->instance_size;
5669 }
5670
5671 static void check_methods_for_intrinsics(const InstanceKlass* ik,
5672 const Array<Method*>* methods) {
5673 assert(ik != NULL, "invariant");
5674 assert(methods != NULL, "invariant");
5675
5676 // Set up Method*::intrinsic_id as soon as we know the names of methods.
5677 // (We used to do this lazily, but now we query it in Rewriter,
5678 // which is eagerly done for every method, so we might as well do it now,
5679 // when everything is fresh in memory.)
5680 const vmSymbols::SID klass_id = Method::klass_id_for_intrinsics(ik);
5681
5682 if (klass_id != vmSymbols::NO_SID) {
5683 for (int j = 0; j < methods->length(); ++j) {
5773
5774
5775 if (ik->should_store_fingerprint()) {
5776 ik->store_fingerprint(_stream->compute_fingerprint());
5777 }
5778
5779 ik->set_has_passed_fingerprint_check(false);
5780 if (UseAOT && ik->supers_have_passed_fingerprint_checks()) {
5781 uint64_t aot_fp = AOTLoader::get_saved_fingerprint(ik);
5782 uint64_t fp = ik->has_stored_fingerprint() ? ik->get_stored_fingerprint() : _stream->compute_fingerprint();
5783 if (aot_fp != 0 && aot_fp == fp) {
5784 // This class matches with a class saved in an AOT library
5785 ik->set_has_passed_fingerprint_check(true);
5786 } else {
5787 ResourceMark rm;
5788 log_info(class, fingerprint)("%s : expected = " PTR64_FORMAT " actual = " PTR64_FORMAT,
5789 ik->external_name(), aot_fp, _stream->compute_fingerprint());
5790 }
5791 }
5792
5793 if (ik->is_value()) {
5794 ValueKlass* vk = ValueKlass::cast(ik);
5795 oop val = ik->allocate_instance(CHECK_NULL);
5796 vk->set_default_value(val);
5797 }
5798
5799 return ik;
5800 }
5801
5802 void ClassFileParser::fill_instance_klass(InstanceKlass* ik, bool changed_by_loadhook, TRAPS) {
5803 assert(ik != NULL, "invariant");
5804
5805 // Set name and CLD before adding to CLD
5806 ik->set_class_loader_data(_loader_data);
5807 ik->set_name(_class_name);
5808
5809 // Add all classes to our internal class loader list here,
5810 // including classes in the bootstrap (NULL) class loader.
5811 const bool publicize = !is_internal();
5812
5813 _loader_data->add_class(ik, publicize);
5814
5815 set_klass_to_deallocate(ik);
5816
5817 assert(_field_info != NULL, "invariant");
5818 assert(ik->static_field_size() == _field_info->static_field_size, "sanity");
5819 assert(ik->nonstatic_oop_map_count() == _field_info->oop_map_blocks->nonstatic_oop_map_count,
5820 "sanity");
5821
5822 assert(ik->is_instance_klass(), "sanity");
5823 assert(ik->size_helper() == _field_info->instance_size, "sanity");
5824
5825 // Fill in information already parsed
5826 ik->set_should_verify_class(_need_verify);
5827
5828 // Not yet: supers are done below to support the new subtype-checking fields
5829 ik->set_nonstatic_field_size(_field_info->nonstatic_field_size);
5830 ik->set_has_nonstatic_fields(_field_info->has_nonstatic_fields);
5831 assert(_fac != NULL, "invariant");
5832 ik->set_static_oop_field_count(_fac->count[STATIC_OOP] + _fac->count[STATIC_FLATTENABLE]);
5833
5834 // this transfers ownership of a lot of arrays from
5835 // the parser onto the InstanceKlass*
5836 apply_parsed_class_metadata(ik, _java_fields_count, CHECK);
5837
5838 // note that is not safe to use the fields in the parser from this point on
5839 assert(NULL == _cp, "invariant");
5840 assert(NULL == _fields, "invariant");
5841 assert(NULL == _methods, "invariant");
5842 assert(NULL == _inner_classes, "invariant");
5843 assert(NULL == _nest_members, "invariant");
5844 assert(NULL == _local_interfaces, "invariant");
5845 assert(NULL == _combined_annotations, "invariant");
5846
5847 if (_has_final_method) {
5848 ik->set_has_final_method();
5849 }
5850
5851 ik->copy_method_ordering(_method_ordering, CHECK);
5852 // The InstanceKlass::_methods_jmethod_ids cache
5900
5901 // Miranda methods
5902 if ((_num_miranda_methods > 0) ||
5903 // if this class introduced new miranda methods or
5904 (_super_klass != NULL && _super_klass->has_miranda_methods())
5905 // super class exists and this class inherited miranda methods
5906 ) {
5907 ik->set_has_miranda_methods(); // then set a flag
5908 }
5909
5910 // Fill in information needed to compute superclasses.
5911 ik->initialize_supers(const_cast<InstanceKlass*>(_super_klass), _transitive_interfaces, CHECK);
5912 ik->set_transitive_interfaces(_transitive_interfaces);
5913 _transitive_interfaces = NULL;
5914
5915 // Initialize itable offset tables
5916 klassItable::setup_itable_offset_table(ik);
5917
5918 // Compute transitive closure of interfaces this class implements
5919 // Do final class setup
5920 OopMapBlocksBuilder* oop_map_blocks = _field_info->oop_map_blocks;
5921 if (oop_map_blocks->nonstatic_oop_map_count > 0) {
5922 oop_map_blocks->copy(ik->start_of_nonstatic_oop_maps());
5923 }
5924
5925 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
5926 set_precomputed_flags(ik, CHECK);
5927
5928 // check if this class can access its super class
5929 check_super_class_access(ik, CHECK);
5930
5931 // check if this class can access its superinterfaces
5932 check_super_interface_access(ik, CHECK);
5933
5934 // check if this class overrides any final method
5935 check_final_method_override(ik, CHECK);
5936
5937 // reject static interface methods prior to Java 8
5938 if (ik->is_interface() && _major_version < JAVA_8_VERSION) {
5939 check_illegal_static_method(ik, CHECK);
5940 }
5941
5942 // Obtain this_klass' module entry
5943 ModuleEntry* module_entry = ik->module();
5944 assert(module_entry != NULL, "module_entry should always be set");
5945
5946 // Obtain java.lang.Module
5956
5957 assert(_all_mirandas != NULL, "invariant");
5958
5959 // Generate any default methods - default methods are public interface methods
5960 // that have a default implementation. This is new with Java 8.
5961 if (_has_nonstatic_concrete_methods) {
5962 DefaultMethods::generate_default_methods(ik,
5963 _all_mirandas,
5964 CHECK);
5965 }
5966
5967 // Add read edges to the unnamed modules of the bootstrap and app class loaders.
5968 if (changed_by_loadhook && !module_handle.is_null() && module_entry->is_named() &&
5969 !module_entry->has_default_read_edges()) {
5970 if (!module_entry->set_has_default_read_edges()) {
5971 // We won a potential race
5972 JvmtiExport::add_default_read_edges(module_handle, THREAD);
5973 }
5974 }
5975
5976 int nfields = ik->java_fields_count();
5977 if (ik->is_value()) nfields++;
5978 for (int i = 0; i < nfields; i++) {
5979 if (ik->field_access_flags(i) & JVM_ACC_FLATTENABLE) {
5980 Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK);
5981 // Value classes must have been pre-loaded
5982 Klass* klass = SystemDictionary::find(klass_name,
5983 Handle(THREAD, ik->class_loader()),
5984 Handle(THREAD, ik->protection_domain()), CHECK);
5985 assert(klass != NULL, "Sanity check");
5986 assert(klass->access_flags().is_value_type(), "Value type expected");
5987 ik->set_value_field_klass(i, klass);
5988 klass_name->decrement_refcount();
5989 } else if (is_value_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0)
5990 && ((ik->field_access_flags(i) & JVM_ACC_STATIC) != 0)) {
5991 ValueKlass::cast(ik)->set_default_value_offset(ik->field_offset(i));
5992 }
5993 }
5994
5995 if (is_value_type()) {
5996 ValueKlass::cast(ik)->initialize_calling_convention(CHECK);
5997 }
5998
5999 ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
6000
6001 if (!is_internal()) {
6002 if (log_is_enabled(Info, class, load)) {
6003 ResourceMark rm;
6004 const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
6005 ik->print_class_load_logging(_loader_data, module_name, _stream);
6006 }
6007
6008 if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
6009 ik->major_version() != JAVA_MIN_SUPPORTED_VERSION &&
6010 log_is_enabled(Info, class, preview)) {
6011 ResourceMark rm;
6012 log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
6013 ik->external_name(), ik->major_version());
6014 }
6015
6016 if (log_is_enabled(Debug, class, resolve)) {
6017 ResourceMark rm;
6018 // print out the superclass.
6168 _protection_domain(protection_domain),
6169 _access_flags(),
6170 _pub_level(pub_level),
6171 _bad_constant_seen(0),
6172 _synthetic_flag(false),
6173 _sde_length(false),
6174 _sde_buffer(NULL),
6175 _sourcefile_index(0),
6176 _generic_signature_index(0),
6177 _major_version(0),
6178 _minor_version(0),
6179 _this_class_index(0),
6180 _super_class_index(0),
6181 _itfs_len(0),
6182 _java_fields_count(0),
6183 _need_verify(false),
6184 _relax_verify(false),
6185 _has_nonstatic_concrete_methods(false),
6186 _declares_nonstatic_concrete_methods(false),
6187 _has_final_method(false),
6188 _has_flattenable_fields(false),
6189 _has_finalizer(false),
6190 _has_empty_finalizer(false),
6191 _has_vanilla_constructor(false),
6192 _max_bootstrap_specifier_index(-1) {
6193
6194 _class_name = name != NULL ? name : vmSymbols::unknown_class_name();
6195 _class_name->increment_refcount();
6196
6197 assert(THREAD->is_Java_thread(), "invariant");
6198 assert(_loader_data != NULL, "invariant");
6199 assert(stream != NULL, "invariant");
6200 assert(_stream != NULL, "invariant");
6201 assert(_stream->buffer() == _stream->current(), "invariant");
6202 assert(_class_name != NULL, "invariant");
6203 assert(0 == _access_flags.as_int(), "invariant");
6204
6205 // Figure out whether we can skip format checking (matching classic VM behavior)
6206 if (DumpSharedSpaces) {
6207 // verify == true means it's a 'remote' class (i.e., non-boot class)
6208 // Verification decision is based on BytecodeVerificationRemote flag
6364
6365 _orig_cp_size = cp_size;
6366 if (int(cp_size) + _max_num_patched_klasses > 0xffff) {
6367 THROW_MSG(vmSymbols::java_lang_InternalError(), "not enough space for patched classes");
6368 }
6369 cp_size += _max_num_patched_klasses;
6370
6371 _cp = ConstantPool::allocate(_loader_data,
6372 cp_size,
6373 CHECK);
6374
6375 ConstantPool* const cp = _cp;
6376
6377 parse_constant_pool(stream, cp, _orig_cp_size, CHECK);
6378
6379 assert(cp_size == (const u2)cp->length(), "invariant");
6380
6381 // ACCESS FLAGS
6382 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
6383
6384 jint recognized_modifiers = JVM_RECOGNIZED_CLASS_MODIFIERS;
6385 // JVM_ACC_MODULE is defined in JDK-9 and later.
6386 if (_major_version >= JAVA_9_VERSION) {
6387 recognized_modifiers |= JVM_ACC_MODULE;
6388 }
6389 // JVM_ACC_VALUE is defined for class file version 55 and later
6390 if (supports_value_types()) {
6391 recognized_modifiers |= JVM_ACC_VALUE;
6392 }
6393
6394 // Access flags
6395 jint flags = stream->get_u2_fast() & recognized_modifiers;
6396
6397 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
6398 // Set abstract bit for old class files for backward compatibility
6399 flags |= JVM_ACC_ABSTRACT;
6400 }
6401
6402 verify_legal_class_modifiers(flags, CHECK);
6403
6404 short bad_constant = class_bad_constant_seen();
6405 if (bad_constant != 0) {
6406 // Do not throw CFE until after the access_flags are checked because if
6407 // ACC_MODULE is set in the access flags, then NCDFE must be thrown, not CFE.
6408 classfile_parse_error("Unknown constant tag %u in class file %s", bad_constant, CHECK);
6409 }
6410
6411 _access_flags.set_flags(flags);
6412
6413 // This class and superclass
6414 _this_class_index = stream->get_u2_fast();
6415 check_property(
6416 valid_cp_range(_this_class_index, cp_size) &&
6511 _super_class_index = stream->get_u2_fast();
6512 _super_klass = parse_super_class(cp,
6513 _super_class_index,
6514 _need_verify,
6515 CHECK);
6516
6517 // Interfaces
6518 _itfs_len = stream->get_u2_fast();
6519 parse_interfaces(stream,
6520 _itfs_len,
6521 cp,
6522 &_has_nonstatic_concrete_methods,
6523 CHECK);
6524
6525 assert(_local_interfaces != NULL, "invariant");
6526
6527 // Fields (offsets are filled in later)
6528 _fac = new FieldAllocationCount();
6529 parse_fields(stream,
6530 _access_flags.is_interface(),
6531 _access_flags.is_value_type(),
6532 _fac,
6533 cp,
6534 cp_size,
6535 &_java_fields_count,
6536 CHECK);
6537
6538 assert(_fields != NULL, "invariant");
6539
6540 // Methods
6541 AccessFlags promoted_flags;
6542 parse_methods(stream,
6543 _access_flags.is_interface(),
6544 _access_flags.is_value_type(),
6545 &promoted_flags,
6546 &_has_final_method,
6547 &_declares_nonstatic_concrete_methods,
6548 CHECK);
6549
6550 assert(_methods != NULL, "invariant");
6551
6552 // promote flags from parse_methods() to the klass' flags
6553 _access_flags.add_promoted_flags(promoted_flags.as_int());
6554
6555 if (_declares_nonstatic_concrete_methods) {
6556 _has_nonstatic_concrete_methods = true;
6557 }
6558
6559 // Additional attributes/annotations
6560 _parsed_annotations = new ClassAnnotationCollector();
6561 parse_classfile_attributes(stream, cp, _parsed_annotations, CHECK);
6562
6563 assert(_inner_classes != NULL, "invariant");
6564
6606 true,
6607 CHECK);
6608 }
6609
6610 if (_super_klass != NULL) {
6611 if (_super_klass->has_nonstatic_concrete_methods()) {
6612 _has_nonstatic_concrete_methods = true;
6613 }
6614
6615 if (_super_klass->is_interface()) {
6616 ResourceMark rm(THREAD);
6617 Exceptions::fthrow(
6618 THREAD_AND_LOCATION,
6619 vmSymbols::java_lang_IncompatibleClassChangeError(),
6620 "class %s has interface %s as super class",
6621 _class_name->as_klass_external_name(),
6622 _super_klass->external_name()
6623 );
6624 return;
6625 }
6626
6627 // For a value class, only java/lang/Object is an acceptable super class
6628 if (_access_flags.get_flags() & JVM_ACC_VALUE) {
6629 guarantee_property(_super_klass->name() == vmSymbols::java_lang_Object(),
6630 "Value type must have java.lang.Object as superclass in class file %s",
6631 CHECK);
6632 }
6633
6634 // Make sure super class is not final
6635 if (_super_klass->is_final()) {
6636 THROW_MSG(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class");
6637 }
6638 }
6639
6640 // Compute the transitive list of all unique interfaces implemented by this class
6641 _transitive_interfaces =
6642 compute_transitive_interfaces(_super_klass,
6643 _local_interfaces,
6644 _loader_data,
6645 CHECK);
6646
6647 assert(_transitive_interfaces != NULL, "invariant");
6648
6649 // sort methods
6650 _method_ordering = sort_methods(_methods);
6651
6652 _all_mirandas = new GrowableArray<Method*>(20);
6653
6654 Handle loader(THREAD, _loader_data->class_loader());
6655 klassVtable::compute_vtable_size_and_num_mirandas(&_vtable_size,
6656 &_num_miranda_methods,
6657 _all_mirandas,
6658 _super_klass,
6659 _methods,
6660 _access_flags,
6661 _major_version,
6662 loader,
6663 _class_name,
6664 _local_interfaces,
6665 CHECK);
6666
6667 // Size of Java itable (in words)
6668 _itable_size = _access_flags.is_interface() ? 0 :
6669 klassItable::compute_itable_size(_transitive_interfaces);
6670
6671 assert(_fac != NULL, "invariant");
6672 assert(_parsed_annotations != NULL, "invariant");
6673
6674
6675 for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6676 if (fs.is_flattenable()) {
6677 // Pre-load value class
6678 Klass* klass = SystemDictionary::resolve_flattenable_field_or_fail(&fs,
6679 Handle(THREAD, _loader_data->class_loader()),
6680 _protection_domain, true, CHECK);
6681 assert(klass != NULL, "Sanity check");
6682 assert(klass->access_flags().is_value_type(), "Value type expected");
6683 _has_flattenable_fields = true;
6684 }
6685 }
6686
6687 _field_info = new FieldLayoutInfo();
6688 layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6689
6690 // Compute reference typ
6691 _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6692
6693 }
6694
6695 void ClassFileParser::set_klass(InstanceKlass* klass) {
6696
6697 #ifdef ASSERT
6698 if (klass != NULL) {
6699 assert(NULL == _klass, "leaking?");
6700 }
6701 #endif
6702
6703 _klass = klass;
6704 }
6705
6706 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6707
6708 #ifdef ASSERT
6709 if (klass != NULL) {
6710 assert(NULL == _klass_to_deallocate, "leaking?");
6711 }
6712 #endif
6713
6714 _klass_to_deallocate = klass;
6715 }
6716
6717 // Caller responsible for ResourceMark
6718 // clone stream with rewound position
6719 const ClassFileStream* ClassFileParser::clone_stream() const {
6720 assert(_stream != NULL, "invariant");
6721
6722 return _stream->clone();
6723 }
6724
6725 // ----------------------------------------------------------------------------
6726 // debugging
6727
6728 #ifdef ASSERT
6729
6730 // return true if class_name contains no '.' (internal format is '/')
6731 bool ClassFileParser::is_internal_format(Symbol* class_name) {
6732 if (class_name != NULL) {
6733 ResourceMark rm;
6734 char* name = class_name->as_C_string();
6735 return strchr(name, '.') == NULL;
6736 } else {
6737 return true;
6738 }
6739 }
6740
6741 #endif
|