1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)classFileParser.cpp 1.280 07/07/09 11:19:49 JVM"
3 #endif
4 /*
5 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 #include "incls/_precompiled.incl"
29 #include "incls/_classFileParser.cpp.incl"
30
31 // We generally try to create the oops directly when parsing, rather than allocating
32 // temporary data structures and copying the bytes twice. A temporary area is only
33 // needed when parsing utf8 entries in the constant pool and when parsing line number
34 // tables.
35
36 // We add assert in debug mode when class format is not checked.
37
38 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
39 #define JAVA_MIN_SUPPORTED_VERSION 45
40 #define JAVA_MAX_SUPPORTED_VERSION 50
41 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
42
43 // Used for two backward compatibility reasons:
44 // - to check for new additions to the class file format in JDK1.5
45 // - to check for bug fixes in the format checker in JDK1.5
46 #define JAVA_1_5_VERSION 49
47
48 // Used for backward compatibility reasons:
49 // - to check for javac bug fixes that happened after 1.5
50 #define JAVA_6_VERSION 50
51
52
53 void ClassFileParser::parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS) {
54 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
55 // this function (_current can be allocated in a register, with scalar
56 // replacement of aggregates). The _current pointer is copied back to
57 // stream() when this function returns. DON'T call another method within
58 // this method that uses stream().
59 ClassFileStream* cfs0 = stream();
60 ClassFileStream cfs1 = *cfs0;
61 ClassFileStream* cfs = &cfs1;
62 #ifdef ASSERT
63 u1* old_current = cfs0->current();
64 #endif
65
66 // Used for batching symbol allocations.
67 const char* names[SymbolTable::symbol_alloc_batch_size];
68 int lengths[SymbolTable::symbol_alloc_batch_size];
69 int indices[SymbolTable::symbol_alloc_batch_size];
153 }
154 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
155 break;
156 case JVM_CONSTANT_NameAndType :
157 {
158 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags
159 u2 name_index = cfs->get_u2_fast();
160 u2 signature_index = cfs->get_u2_fast();
161 cp->name_and_type_at_put(index, name_index, signature_index);
162 }
163 break;
164 case JVM_CONSTANT_Utf8 :
165 {
166 cfs->guarantee_more(2, CHECK); // utf8_length
167 u2 utf8_length = cfs->get_u2_fast();
168 u1* utf8_buffer = cfs->get_u1_buffer();
169 assert(utf8_buffer != NULL, "null utf8 buffer");
170 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.
171 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags
172 cfs->skip_u1_fast(utf8_length);
173 // Before storing the symbol, make sure it's legal
174 if (_need_verify) {
175 verify_legal_utf8((unsigned char*)utf8_buffer, utf8_length, CHECK);
176 }
177
178 unsigned int hash;
179 symbolOop result = SymbolTable::lookup_only((char*)utf8_buffer, utf8_length, hash);
180 if (result == NULL) {
181 names[names_count] = (char*)utf8_buffer;
182 lengths[names_count] = utf8_length;
183 indices[names_count] = index;
184 hashValues[names_count++] = hash;
185 if (names_count == SymbolTable::symbol_alloc_batch_size) {
186 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK);
187 names_count = 0;
188 }
189 } else {
190 cp->symbol_at_put(index, result);
191 }
192 }
193 break;
194 default:
195 classfile_parse_error(
196 "Unknown constant tag %u in class file %s", tag, CHECK);
197 break;
230 // parsing constant pool entries
231 parse_constant_pool_entries(cp, length, CHECK_(nullHandle));
232
233 int index = 1; // declared outside of loops for portability
234
235 // first verification pass - validate cross references and fixup class and string constants
236 for (index = 1; index < length; index++) { // Index 0 is unused
237 switch (cp->tag_at(index).value()) {
238 case JVM_CONSTANT_Class :
239 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
240 break;
241 case JVM_CONSTANT_Fieldref :
242 // fall through
243 case JVM_CONSTANT_Methodref :
244 // fall through
245 case JVM_CONSTANT_InterfaceMethodref : {
246 if (!_need_verify) break;
247 int klass_ref_index = cp->klass_ref_index_at(index);
248 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
249 check_property(valid_cp_range(klass_ref_index, length) &&
250 cp->tag_at(klass_ref_index).is_klass_reference(),
251 "Invalid constant pool index %u in class file %s",
252 klass_ref_index,
253 CHECK_(nullHandle));
254 check_property(valid_cp_range(name_and_type_ref_index, length) &&
255 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
256 "Invalid constant pool index %u in class file %s",
257 name_and_type_ref_index,
258 CHECK_(nullHandle));
259 break;
260 }
261 case JVM_CONSTANT_String :
262 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
263 break;
264 case JVM_CONSTANT_Integer :
265 break;
266 case JVM_CONSTANT_Float :
267 break;
268 case JVM_CONSTANT_Long :
269 case JVM_CONSTANT_Double :
270 index++;
311 break;
312 case JVM_CONSTANT_StringIndex :
313 {
314 int string_index = cp->string_index_at(index);
315 check_property(
316 valid_cp_range(string_index, length) &&
317 cp->tag_at(string_index).is_utf8(),
318 "Invalid constant pool index %u in class file %s",
319 string_index, CHECK_(nullHandle));
320 symbolOop sym = cp->symbol_at(string_index);
321 cp->unresolved_string_at_put(index, sym);
322 }
323 break;
324 default:
325 fatal1("bad constant pool tag value %u", cp->tag_at(index).value());
326 ShouldNotReachHere();
327 break;
328 } // end of switch
329 } // end of for
330
331 if (!_need_verify) {
332 return cp;
333 }
334
335 // second verification pass - checks the strings are of the right format.
336 for (index = 1; index < length; index++) {
337 jbyte tag = cp->tag_at(index).value();
338 switch (tag) {
339 case JVM_CONSTANT_UnresolvedClass: {
340 symbolHandle class_name(THREAD, cp->unresolved_klass_at(index));
341 verify_legal_class_name(class_name, CHECK_(nullHandle));
342 break;
343 }
344 case JVM_CONSTANT_Fieldref:
345 case JVM_CONSTANT_Methodref:
346 case JVM_CONSTANT_InterfaceMethodref: {
347 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
348 // already verified to be utf8
349 int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index);
350 // already verified to be utf8
351 int signature_ref_index = cp->signature_ref_index_at(name_and_type_ref_index);
352 symbolHandle name(THREAD, cp->symbol_at(name_ref_index));
353 symbolHandle signature(THREAD, cp->symbol_at(signature_ref_index));
354 if (tag == JVM_CONSTANT_Fieldref) {
355 verify_legal_field_name(name, CHECK_(nullHandle));
356 verify_legal_field_signature(name, signature, CHECK_(nullHandle));
357 } else {
358 verify_legal_method_name(name, CHECK_(nullHandle));
359 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
360 if (tag == JVM_CONSTANT_Methodref) {
363 unsigned int name_len = name->utf8_length();
364 assert(name_len > 0, "bad method name"); // already verified as legal name
365 if (name->byte_at(0) == '<') {
366 if (name() != vmSymbols::object_initializer_name()) {
367 classfile_parse_error(
368 "Bad method name at constant pool index %u in class file %s",
369 name_ref_index, CHECK_(nullHandle));
370 }
371 }
372 }
373 }
374 break;
375 }
376 } // end of switch
377 } // end of for
378
379 return cp;
380 }
381
382
383 class NameSigHash: public ResourceObj {
384 public:
385 symbolOop _name; // name
386 symbolOop _sig; // signature
387 NameSigHash* _next; // Next entry in hash table
388 };
389
390
391 #define HASH_ROW_SIZE 256
392
393 unsigned int hash(symbolOop name, symbolOop sig) {
394 unsigned int raw_hash = 0;
395 raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2);
396 raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize;
397
398 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;
399 }
400
401
402 void initialize_hashtable(NameSigHash** table) {
433 return true;
434 }
435
436
437 objArrayHandle ClassFileParser::parse_interfaces(constantPoolHandle cp,
438 int length,
439 Handle class_loader,
440 Handle protection_domain,
441 PerfTraceTime* vmtimer,
442 symbolHandle class_name,
443 TRAPS) {
444 ClassFileStream* cfs = stream();
445 assert(length > 0, "only called for length>0");
446 objArrayHandle nullHandle;
447 objArrayOop interface_oop = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
448 objArrayHandle interfaces (THREAD, interface_oop);
449
450 int index;
451 for (index = 0; index < length; index++) {
452 u2 interface_index = cfs->get_u2(CHECK_(nullHandle));
453 check_property(
454 valid_cp_range(interface_index, cp->length()) &&
455 cp->tag_at(interface_index).is_unresolved_klass(),
456 "Interface name has bad constant pool index %u in class file %s",
457 interface_index, CHECK_(nullHandle));
458 symbolHandle unresolved_klass (THREAD, cp->klass_name_at(interface_index));
459
460 // Don't need to check legal name because it's checked when parsing constant pool.
461 // But need to make sure it's not an array type.
462 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY,
463 "Bad interface name in class file %s", CHECK_(nullHandle));
464
465 vmtimer->suspend(); // do not count recursive loading twice
466 // Call resolve_super so classcircularity is checked
467 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
468 unresolved_klass, class_loader, protection_domain,
469 false, CHECK_(nullHandle));
470 KlassHandle interf (THREAD, k);
471 vmtimer->resume();
472
473 if (!Klass::cast(interf())->is_interface()) {
474 THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", nullHandle);
475 }
476 interfaces->obj_at_put(index, interf());
477 }
478
479 if (!_need_verify || length <= 1) {
480 return interfaces;
481 }
482
483 // Check if there's any duplicates in interfaces
484 ResourceMark rm(THREAD);
485 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(
486 THREAD, NameSigHash*, HASH_ROW_SIZE);
487 initialize_hashtable(interface_names);
488 bool dup = false;
489 {
490 debug_only(No_Safepoint_Verifier nsv;)
491 for (index = 0; index < length; index++) {
492 klassOop k = (klassOop)interfaces->obj_at(index);
862
863 // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index]
864 typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle));
865 typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh);
866
867 int index = 0;
868 cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index
869 for (unsigned int i = 0; i < exception_table_length; i++) {
870 u2 start_pc = cfs->get_u2_fast();
871 u2 end_pc = cfs->get_u2_fast();
872 u2 handler_pc = cfs->get_u2_fast();
873 u2 catch_type_index = cfs->get_u2_fast();
874 // Will check legal target after parsing code array in verifier.
875 if (_need_verify) {
876 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
877 "Illegal exception table range in class file %s", CHECK_(nullHandle));
878 guarantee_property(handler_pc < code_length,
879 "Illegal exception table handler in class file %s", CHECK_(nullHandle));
880 if (catch_type_index != 0) {
881 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
882 (cp->tag_at(catch_type_index).is_klass() ||
883 cp->tag_at(catch_type_index).is_unresolved_klass()),
884 "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle));
885 }
886 }
887 exception_handlers->int_at_put(index++, start_pc);
888 exception_handlers->int_at_put(index++, end_pc);
889 exception_handlers->int_at_put(index++, handler_pc);
890 exception_handlers->int_at_put(index++, catch_type_index);
891 }
892 return exception_handlers;
893 }
894
895 void ClassFileParser::parse_linenumber_table(
896 u4 code_attribute_length, u4 code_length,
897 CompressedLineNumberWriteStream** write_stream, TRAPS) {
898 ClassFileStream* cfs = stream();
899 unsigned int num_entries = cfs->get_u2(CHECK);
900
901 // Each entry is a u2 start_pc, and a u2 line_number
902 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
903
1102 }
1103 }
1104 return localvariable_table_start;
1105 }
1106
1107
1108 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1109 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS) {
1110 ClassFileStream* cfs = stream();
1111 u2 index = 0; // index in the array with long/double occupying two slots
1112 u4 i1 = *u1_index;
1113 u4 i2 = *u2_index + 1;
1114 for(int i = 0; i < array_length; i++) {
1115 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1116 index++;
1117 if (tag == ITEM_Long || tag == ITEM_Double) {
1118 index++;
1119 } else if (tag == ITEM_Object) {
1120 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK);
1121 guarantee_property(valid_cp_range(class_index, cp->length()) &&
1122 cp->tag_at(class_index).is_unresolved_klass(),
1123 "Bad class index %u in StackMap in class file %s",
1124 class_index, CHECK);
1125 } else if (tag == ITEM_Uninitialized) {
1126 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK);
1127 guarantee_property(
1128 offset < code_length,
1129 "Bad uninitialized type offset %u in StackMap in class file %s",
1130 offset, CHECK);
1131 } else {
1132 guarantee_property(
1133 tag <= (u1)ITEM_Uninitialized,
1134 "Unknown variable type %u in StackMap in class file %s",
1135 tag, CHECK);
1136 }
1137 }
1138 u2_array[*u2_index] = index;
1139 *u1_index = i1;
1140 *u2_index = i2;
1141 }
1142
1168 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length,
1169 u4 method_attribute_length,
1170 constantPoolHandle cp, TRAPS) {
1171 ClassFileStream* cfs = stream();
1172 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length
1173 *checked_exceptions_length = cfs->get_u2_fast();
1174 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);
1175 u2* checked_exceptions_start = cfs->get_u2_buffer();
1176 assert(checked_exceptions_start != NULL, "null checked exceptions");
1177 if (!_need_verify) {
1178 cfs->skip_u2_fast(size);
1179 } else {
1180 // Verify each value in the checked exception table
1181 u2 checked_exception;
1182 u2 len = *checked_exceptions_length;
1183 cfs->guarantee_more(2 * len, CHECK_NULL);
1184 for (int i = 0; i < len; i++) {
1185 checked_exception = cfs->get_u2_fast();
1186 check_property(
1187 valid_cp_range(checked_exception, cp->length()) &&
1188 cp->tag_at(checked_exception).is_klass_reference(),
1189 "Exception name has bad type at constant pool %u in class file %s",
1190 checked_exception, CHECK_NULL);
1191 }
1192 }
1193 // check exceptions attribute length
1194 if (_need_verify) {
1195 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1196 sizeof(u2) * size),
1197 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1198 }
1199 return checked_exceptions_start;
1200 }
1201
1202
1203 #define MAX_ARGS_SIZE 255
1204 #define MAX_CODE_SIZE 65535
1205 #define INITIAL_MAX_LVT_NUMBER 256
1206
1207 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
1208 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the
1345 "Invalid method Code length %u in class file %s",
1346 code_length, CHECK_(nullHandle));
1347 }
1348 // Code pointer
1349 code_start = cfs->get_u1_buffer();
1350 assert(code_start != NULL, "null code start");
1351 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1352 cfs->skip_u1_fast(code_length);
1353
1354 // Exception handler table
1355 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1356 exception_table_length = cfs->get_u2_fast();
1357 if (exception_table_length > 0) {
1358 exception_handlers =
1359 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1360 }
1361
1362 // Parse additional attributes in code attribute
1363 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1364 u2 code_attributes_count = cfs->get_u2_fast();
1365 unsigned int calculated_attribute_length = sizeof(max_stack) +
1366 sizeof(max_locals) +
1367 sizeof(code_length) +
1368 code_length +
1369 sizeof(exception_table_length) +
1370 sizeof(code_attributes_count) +
1371 exception_table_length*(sizeof(u2) /* start_pc */+
1372 sizeof(u2) /* end_pc */ +
1373 sizeof(u2) /* handler_pc */ +
1374 sizeof(u2) /* catch_type_index */);
1375
1376 while (code_attributes_count--) {
1377 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
1378 u2 code_attribute_name_index = cfs->get_u2_fast();
1379 u4 code_attribute_length = cfs->get_u4_fast();
1380 calculated_attribute_length += code_attribute_length +
1381 sizeof(code_attribute_name_index) +
1382 sizeof(code_attribute_length);
1383 check_property(valid_cp_range(code_attribute_name_index, cp_size) &&
1384 cp->tag_at(code_attribute_name_index).is_utf8(),
1385 "Invalid code attribute name index %u in class file %s",
1386 code_attribute_name_index,
1387 CHECK_(nullHandle));
1388 if (LoadLineNumberTables &&
1389 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
1390 // Parse and compress line number table
1391 parse_linenumber_table(code_attribute_length, code_length,
1392 &linenumber_table, CHECK_(nullHandle));
1393
1394 } else if (LoadLocalVariableTables &&
1894 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC)
1895
1896 // Return number of classes in the inner classes attribute table
1897 u2 ClassFileParser::parse_classfile_inner_classes_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
1898 ClassFileStream* cfs = stream();
1899 cfs->guarantee_more(2, CHECK_0); // length
1900 u2 length = cfs->get_u2_fast();
1901
1902 // 4-tuples of shorts [inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags]
1903 typeArrayOop ic = oopFactory::new_permanent_shortArray(length*4, CHECK_0);
1904 typeArrayHandle inner_classes(THREAD, ic);
1905 int index = 0;
1906 int cp_size = cp->length();
1907 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
1908 for (int n = 0; n < length; n++) {
1909 // Inner class index
1910 u2 inner_class_info_index = cfs->get_u2_fast();
1911 check_property(
1912 inner_class_info_index == 0 ||
1913 (valid_cp_range(inner_class_info_index, cp_size) &&
1914 cp->tag_at(inner_class_info_index).is_klass_reference()),
1915 "inner_class_info_index %u has bad constant type in class file %s",
1916 inner_class_info_index, CHECK_0);
1917 // Outer class index
1918 u2 outer_class_info_index = cfs->get_u2_fast();
1919 check_property(
1920 outer_class_info_index == 0 ||
1921 (valid_cp_range(outer_class_info_index, cp_size) &&
1922 cp->tag_at(outer_class_info_index).is_klass_reference()),
1923 "outer_class_info_index %u has bad constant type in class file %s",
1924 outer_class_info_index, CHECK_0);
1925 // Inner class name
1926 u2 inner_name_index = cfs->get_u2_fast();
1927 check_property(
1928 inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) &&
1929 cp->tag_at(inner_name_index).is_utf8()),
1930 "inner_name_index %u has bad constant type in class file %s",
1931 inner_name_index, CHECK_0);
1932 if (_need_verify) {
1933 guarantee_property(inner_class_info_index != outer_class_info_index,
1934 "Class is both outer and inner class in class file %s", CHECK_0);
1935 }
1936 // Access flags
1937 AccessFlags inner_access_flags;
1938 jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
1939 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
1940 // Set abstract bit for old class files for backward compatibility
1941 flags |= JVM_ACC_ABSTRACT;
1942 }
2064 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2065 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2066 runtime_invisible_annotations_length = attribute_length;
2067 runtime_invisible_annotations = cfs->get_u1_buffer();
2068 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2069 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
2070 } else if (tag == vmSymbols::tag_enclosing_method()) {
2071 if (parsed_enclosingmethod_attribute) {
2072 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2073 } else {
2074 parsed_enclosingmethod_attribute = true;
2075 }
2076 cfs->guarantee_more(4, CHECK); // class_index, method_index
2077 u2 class_index = cfs->get_u2_fast();
2078 u2 method_index = cfs->get_u2_fast();
2079 if (class_index == 0) {
2080 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2081 }
2082 // Validate the constant pool indices and types
2083 if (!cp->is_within_bounds(class_index) ||
2084 !cp->tag_at(class_index).is_klass_reference()) {
2085 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2086 }
2087 if (method_index != 0 &&
2088 (!cp->is_within_bounds(method_index) ||
2089 !cp->tag_at(method_index).is_name_and_type())) {
2090 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2091 }
2092 k->set_enclosing_method_indices(class_index, method_index);
2093 } else {
2094 // Unknown attribute
2095 cfs->skip_u1(attribute_length, CHECK);
2096 }
2097 } else {
2098 // Unknown attribute
2099 cfs->skip_u1(attribute_length, CHECK);
2100 }
2101 }
2102 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2103 runtime_visible_annotations_length,
2104 runtime_invisible_annotations,
2318 // end of the object because the Class layout changed between JDK
2319 // 1.3 and JDK 1.4 with the new reflection implementation; some
2320 // nonstatic oop fields were added at the Java level. The offsets
2321 // of these fake fields can't change between these two JDK
2322 // versions because when the offsets are computed at bootstrap
2323 // time we don't know yet which version of the JDK we're running in.
2324
2325 // The values below are fake but will force two non-static oop fields and
2326 // a corresponding non-static oop map block to be allocated.
2327 const int extra = java_lang_Class::number_of_fake_oop_fields;
2328 fac_ptr->nonstatic_oop_count += extra;
2329 }
2330
2331
2332 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2333 // Cause the extra fake fields in java.lang.Class to show up before
2334 // the Java fields for layout compatibility between 1.3 and 1.4
2335 // Incrementing next_nonstatic_oop_offset here advances the
2336 // location where the real java fields are placed.
2337 const int extra = java_lang_Class::number_of_fake_oop_fields;
2338 (*next_nonstatic_oop_offset_ptr) += (extra * wordSize);
2339 }
2340
2341
2342 instanceKlassHandle ClassFileParser::parseClassFile(symbolHandle name,
2343 Handle class_loader,
2344 Handle protection_domain,
2345 symbolHandle& parsed_name,
2346 TRAPS) {
2347 // So that JVMTI can cache class file in the state before retransformable agents
2348 // have modified it
2349 unsigned char *cached_class_file_bytes = NULL;
2350 jint cached_class_file_length;
2351
2352 ClassFileStream* cfs = stream();
2353 // Timing
2354 PerfTraceTime vmtimer(ClassLoader::perf_accumulated_time());
2355
2356 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2357
2358 if (JvmtiExport::should_post_class_file_load_hook()) {
2359 unsigned char* ptr = cfs->buffer();
2360 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2361
2362 JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain,
2363 &ptr, &end_ptr,
2364 &cached_class_file_bytes,
2365 &cached_class_file_length);
2366
2367 if (ptr != cfs->buffer()) {
2368 // JVMTI agent has modified class file data.
2369 // Set new class file stream using JVMTI agent modified
2370 // class file data.
2371 cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source());
2372 set_stream(cfs);
2373 }
2374 }
2375
2376
2377 instanceKlassHandle nullHandle;
2378
2379 // Figure out whether we can skip format checking (matching classic VM behavior)
2380 _need_verify = Verifier::should_verify_for(class_loader());
2381
2382 // Set the verify flag in stream
2383 cfs->set_verify(_need_verify);
2384
2385 // Save the class file name for easier error message printing.
2386 _class_name = name.not_null()? name : vmSymbolHandles::unknown_class_name();
2387
2388 cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor
2389 // Magic value
2390 u4 magic = cfs->get_u4_fast();
2391 guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
2392 "Incompatible magic value %u in class file %s",
2393 magic, CHECK_(nullHandle));
2394
2395 // Version numbers
2486 name->as_C_string(),
2487 class_name->as_C_string()
2488 );
2489 return nullHandle;
2490 }
2491
2492 if (TraceClassLoadingPreorder) {
2493 tty->print("[Loading %s", name()->as_klass_external_name());
2494 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
2495 tty->print_cr("]");
2496 }
2497
2498 u2 super_class_index = cfs->get_u2_fast();
2499 if (super_class_index == 0) {
2500 check_property(class_name() == vmSymbols::java_lang_Object(),
2501 "Invalid superclass index %u in class file %s",
2502 super_class_index,
2503 CHECK_(nullHandle));
2504 } else {
2505 check_property(valid_cp_range(super_class_index, cp_size) &&
2506 cp->tag_at(super_class_index).is_unresolved_klass(),
2507 "Invalid superclass index %u in class file %s",
2508 super_class_index,
2509 CHECK_(nullHandle));
2510 // The class name should be legal because it is checked when parsing constant pool.
2511 // However, make sure it is not an array type.
2512 if (_need_verify) {
2513 guarantee_property(cp->unresolved_klass_at(super_class_index)->byte_at(0) != JVM_SIGNATURE_ARRAY,
2514 "Bad superclass name in class file %s", CHECK_(nullHandle));
2515 }
2516 }
2517
2518 // Interfaces
2519 u2 itfs_len = cfs->get_u2_fast();
2520 objArrayHandle local_interfaces;
2521 if (itfs_len == 0) {
2522 local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2523 } else {
2524 local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, &vmtimer, _class_name, CHECK_(nullHandle));
2525 }
2526
2527 // Fields (offsets are filled in later)
2528 struct FieldAllocationCount fac = {0,0,0,0,0,0,0,0,0,0};
2529 objArrayHandle fields_annotations;
2530 typeArrayHandle fields = parse_fields(cp, access_flags.is_interface(), &fac, &fields_annotations, CHECK_(nullHandle));
2531 // Methods
2532 bool has_final_method = false;
2533 AccessFlags promoted_flags;
2534 promoted_flags.set_flags(0);
2535 // These need to be oop pointers because they are allocated lazily
2536 // inside parse_methods inside a nested HandleMark
2537 objArrayOop methods_annotations_oop = NULL;
2538 objArrayOop methods_parameter_annotations_oop = NULL;
2539 objArrayOop methods_default_annotations_oop = NULL;
2540 objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
2541 &promoted_flags,
2542 &has_final_method,
2543 &methods_annotations_oop,
2544 &methods_parameter_annotations_oop,
2545 &methods_default_annotations_oop,
2546 CHECK_(nullHandle));
2547
2548 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2549 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2550 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
2551
2552 // We check super class after class file is parsed and format is checked
2553 if (super_class_index > 0) {
2554 symbolHandle sk (THREAD, cp->klass_name_at(super_class_index));
2555 if (access_flags.is_interface()) {
2556 // Before attempting to resolve the superclass, check for class format
2557 // errors not checked yet.
2558 guarantee_property(sk() == vmSymbols::java_lang_Object(),
2559 "Interfaces must have java.lang.Object as superclass in class file %s",
2560 CHECK_(nullHandle));
2561 }
2562 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
2563 sk,
2564 class_loader,
2565 protection_domain,
2566 true,
2567 CHECK_(nullHandle));
2568 KlassHandle kh (THREAD, k);
2569 super_klass = instanceKlassHandle(THREAD, kh());
2570 if (super_klass->is_interface()) {
2571 ResourceMark rm(THREAD);
2572 Exceptions::fthrow(
2573 THREAD_AND_LOCATION,
2574 vmSymbolHandles::java_lang_IncompatibleClassChangeError(),
2575 "class %s has interface %s as super class",
2576 class_name->as_klass_external_name(),
2577 super_klass->external_name()
2578 );
2579 return nullHandle;
2580 }
2581 // Make sure super class is not final
2582 if (super_klass->is_final()) {
2583 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
2584 }
2585 }
2586
2587 // Compute the transitive list of all unique interfaces implemented by this class
2588 objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
2589
2624 int next_static_double_offset;
2625 int next_static_word_offset;
2626 int next_static_short_offset;
2627 int next_static_byte_offset;
2628 int next_static_type_offset;
2629 int next_nonstatic_oop_offset;
2630 int next_nonstatic_double_offset;
2631 int next_nonstatic_word_offset;
2632 int next_nonstatic_short_offset;
2633 int next_nonstatic_byte_offset;
2634 int next_nonstatic_type_offset;
2635 int first_nonstatic_oop_offset;
2636 int first_nonstatic_field_offset;
2637 int next_nonstatic_field_offset;
2638
2639 // Calculate the starting byte offsets
2640 next_static_oop_offset = (instanceKlass::header_size() +
2641 align_object_offset(vtable_size) +
2642 align_object_offset(itable_size)) * wordSize;
2643 next_static_double_offset = next_static_oop_offset +
2644 (fac.static_oop_count * oopSize);
2645 if ( fac.static_double_count &&
2646 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
2647 Universe::field_type_should_be_aligned(T_LONG)) ) {
2648 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
2649 }
2650
2651 next_static_word_offset = next_static_double_offset +
2652 (fac.static_double_count * BytesPerLong);
2653 next_static_short_offset = next_static_word_offset +
2654 (fac.static_word_count * BytesPerInt);
2655 next_static_byte_offset = next_static_short_offset +
2656 (fac.static_short_count * BytesPerShort);
2657 next_static_type_offset = align_size_up((next_static_byte_offset +
2658 fac.static_byte_count ), wordSize );
2659 static_field_size = (next_static_type_offset -
2660 next_static_oop_offset) / wordSize;
2661 first_nonstatic_field_offset = (instanceOopDesc::header_size() +
2662 nonstatic_field_size) * wordSize;
2663 next_nonstatic_field_offset = first_nonstatic_field_offset;
2664
2665 // Add fake fields for java.lang.Class instances (also see below)
2666 if (class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2667 java_lang_Class_fix_pre(&methods, &fac, CHECK_(nullHandle));
2668 }
2669
2670 // Add a fake "discovered" field if it is not present
2671 // for compatibility with earlier jdk's.
2672 if (class_name() == vmSymbols::java_lang_ref_Reference()
2673 && class_loader.is_null()) {
2674 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
2675 }
2676 // end of "discovered" field compactibility fix
2677
2678 int nonstatic_double_count = fac.nonstatic_double_count;
2679 int nonstatic_word_count = fac.nonstatic_word_count;
2680 int nonstatic_short_count = fac.nonstatic_short_count;
2681 int nonstatic_byte_count = fac.nonstatic_byte_count;
2682 int nonstatic_oop_count = fac.nonstatic_oop_count;
2683
2684 // Prepare list of oops for oop maps generation.
2685 u2* nonstatic_oop_offsets;
2686 u2* nonstatic_oop_length;
2687 int nonstatic_oop_map_count = 0;
2688
2689 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
2690 THREAD, u2, nonstatic_oop_count+1);
2691 nonstatic_oop_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2692 THREAD, u2, nonstatic_oop_count+1);
2693
2694 // Add fake fields for java.lang.Class instances (also see above).
2695 // FieldsAllocationStyle and CompactFields values will be reset to default.
2696 if(class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2697 java_lang_Class_fix_post(&next_nonstatic_field_offset);
2698 nonstatic_oop_offsets[0] = (u2)first_nonstatic_field_offset;
2699 int fake_oop_count = (( next_nonstatic_field_offset -
2700 first_nonstatic_field_offset ) / oopSize);
2701 nonstatic_oop_length [0] = (u2)fake_oop_count;
2702 nonstatic_oop_map_count = 1;
2703 nonstatic_oop_count -= fake_oop_count;
2704 first_nonstatic_oop_offset = first_nonstatic_field_offset;
2705 } else {
2706 first_nonstatic_oop_offset = 0; // will be set for first oop field
2707 }
2708
2709 #ifndef PRODUCT
2710 if( PrintCompactFieldsSavings ) {
2711 next_nonstatic_double_offset = next_nonstatic_field_offset +
2712 (nonstatic_oop_count * oopSize);
2713 if ( nonstatic_double_count > 0 ) {
2714 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
2715 }
2716 next_nonstatic_word_offset = next_nonstatic_double_offset +
2717 (nonstatic_double_count * BytesPerLong);
2718 next_nonstatic_short_offset = next_nonstatic_word_offset +
2719 (nonstatic_word_count * BytesPerInt);
2720 next_nonstatic_byte_offset = next_nonstatic_short_offset +
2721 (nonstatic_short_count * BytesPerShort);
2722 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
2723 nonstatic_byte_count ), wordSize );
2724 orig_nonstatic_field_size = nonstatic_field_size +
2725 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/wordSize);
2726 }
2727 #endif
2728 bool compact_fields = CompactFields;
2729 int allocation_style = FieldsAllocationStyle;
2730 if( allocation_style < 0 || allocation_style > 1 ) { // Out of range?
2731 assert(false, "0 <= FieldsAllocationStyle <= 1");
2732 allocation_style = 1; // Optimistic
2733 }
2734
2735 // The next classes have predefined hard-coded fields offsets
2736 // (see in JavaClasses::compute_hard_coded_offsets()).
2737 // Use default fields allocation order for them.
2738 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
2739 (class_name() == vmSymbols::java_lang_AssertionStatusDirectives() ||
2740 class_name() == vmSymbols::java_lang_Class() ||
2741 class_name() == vmSymbols::java_lang_ClassLoader() ||
2742 class_name() == vmSymbols::java_lang_ref_Reference() ||
2743 class_name() == vmSymbols::java_lang_ref_SoftReference() ||
2744 class_name() == vmSymbols::java_lang_StackTraceElement() ||
2745 class_name() == vmSymbols::java_lang_String() ||
2746 class_name() == vmSymbols::java_lang_Throwable()) ) {
2747 allocation_style = 0; // Allocate oops first
2748 compact_fields = false; // Don't compact fields
2749 }
2750
2751 if( allocation_style == 0 ) {
2752 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes
2753 next_nonstatic_oop_offset = next_nonstatic_field_offset;
2754 next_nonstatic_double_offset = next_nonstatic_oop_offset +
2755 (nonstatic_oop_count * oopSize);
2756 } else if( allocation_style == 1 ) {
2757 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops
2758 next_nonstatic_double_offset = next_nonstatic_field_offset;
2759 } else {
2760 ShouldNotReachHere();
2761 }
2762
2763 int nonstatic_oop_space_count = 0;
2764 int nonstatic_word_space_count = 0;
2765 int nonstatic_short_space_count = 0;
2766 int nonstatic_byte_space_count = 0;
2767 int nonstatic_oop_space_offset;
2768 int nonstatic_word_space_offset;
2769 int nonstatic_short_space_offset;
2770 int nonstatic_byte_space_offset;
2771
2772 if( nonstatic_double_count > 0 ) {
2773 int offset = next_nonstatic_double_offset;
2774 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
2775 if( compact_fields && offset != next_nonstatic_double_offset ) {
2781 nonstatic_word_count -= 1;
2782 nonstatic_word_space_count = 1; // Only one will fit
2783 length -= BytesPerInt;
2784 offset += BytesPerInt;
2785 }
2786 nonstatic_short_space_offset = offset;
2787 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
2788 nonstatic_short_count -= 1;
2789 nonstatic_short_space_count += 1;
2790 length -= BytesPerShort;
2791 offset += BytesPerShort;
2792 }
2793 nonstatic_byte_space_offset = offset;
2794 while( length > 0 && nonstatic_byte_count > 0 ) {
2795 nonstatic_byte_count -= 1;
2796 nonstatic_byte_space_count += 1;
2797 length -= 1;
2798 }
2799 // Allocate oop field in the gap if there are no other fields for that.
2800 nonstatic_oop_space_offset = offset;
2801 if( length >= oopSize && nonstatic_oop_count > 0 &&
2802 allocation_style != 0 ) { // when oop fields not first
2803 nonstatic_oop_count -= 1;
2804 nonstatic_oop_space_count = 1; // Only one will fit
2805 length -= oopSize;
2806 offset += oopSize;
2807 }
2808 }
2809 }
2810
2811 next_nonstatic_word_offset = next_nonstatic_double_offset +
2812 (nonstatic_double_count * BytesPerLong);
2813 next_nonstatic_short_offset = next_nonstatic_word_offset +
2814 (nonstatic_word_count * BytesPerInt);
2815 next_nonstatic_byte_offset = next_nonstatic_short_offset +
2816 (nonstatic_short_count * BytesPerShort);
2817
2818 int notaligned_offset;
2819 if( allocation_style == 0 ) {
2820 notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
2821 } else { // allocation_style == 1
2822 next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
2823 if( nonstatic_oop_count > 0 ) {
2824 notaligned_offset = next_nonstatic_oop_offset;
2825 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, oopSize);
2826 }
2827 notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * oopSize);
2828 }
2829 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
2830 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
2831 - first_nonstatic_field_offset)/wordSize);
2832
2833 // Iterate over fields again and compute correct offsets.
2834 // The field allocation type was temporarily stored in the offset slot.
2835 // oop fields are located before non-oop fields (static and non-static).
2836 int len = fields->length();
2837 for (int i = 0; i < len; i += instanceKlass::next_offset) {
2838 int real_offset;
2839 FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i+4);
2840 switch (atype) {
2841 case STATIC_OOP:
2842 real_offset = next_static_oop_offset;
2843 next_static_oop_offset += oopSize;
2844 break;
2845 case STATIC_BYTE:
2846 real_offset = next_static_byte_offset;
2847 next_static_byte_offset += 1;
2848 break;
2849 case STATIC_SHORT:
2850 real_offset = next_static_short_offset;
2851 next_static_short_offset += BytesPerShort;
2852 break;
2853 case STATIC_WORD:
2854 real_offset = next_static_word_offset;
2855 next_static_word_offset += BytesPerInt;
2856 break;
2857 case STATIC_ALIGNED_DOUBLE:
2858 case STATIC_DOUBLE:
2859 real_offset = next_static_double_offset;
2860 next_static_double_offset += BytesPerLong;
2861 break;
2862 case NONSTATIC_OOP:
2863 if( nonstatic_oop_space_count > 0 ) {
2864 real_offset = nonstatic_oop_space_offset;
2865 nonstatic_oop_space_offset += oopSize;
2866 nonstatic_oop_space_count -= 1;
2867 } else {
2868 real_offset = next_nonstatic_oop_offset;
2869 next_nonstatic_oop_offset += oopSize;
2870 }
2871 // Update oop maps
2872 if( nonstatic_oop_map_count > 0 &&
2873 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
2874 (u2)(real_offset - nonstatic_oop_length[nonstatic_oop_map_count - 1] * oopSize) ) {
2875 // Extend current oop map
2876 nonstatic_oop_length[nonstatic_oop_map_count - 1] += 1;
2877 } else {
2878 // Create new oop map
2879 nonstatic_oop_offsets[nonstatic_oop_map_count] = (u2)real_offset;
2880 nonstatic_oop_length [nonstatic_oop_map_count] = 1;
2881 nonstatic_oop_map_count += 1;
2882 if( first_nonstatic_oop_offset == 0 ) { // Undefined
2883 first_nonstatic_oop_offset = real_offset;
2884 }
2885 }
2886 break;
2887 case NONSTATIC_BYTE:
2888 if( nonstatic_byte_space_count > 0 ) {
2889 real_offset = nonstatic_byte_space_offset;
2890 nonstatic_byte_space_offset += 1;
2891 nonstatic_byte_space_count -= 1;
2892 } else {
2893 real_offset = next_nonstatic_byte_offset;
2894 next_nonstatic_byte_offset += 1;
2912 } else {
2913 real_offset = next_nonstatic_word_offset;
2914 next_nonstatic_word_offset += BytesPerInt;
2915 }
2916 break;
2917 case NONSTATIC_ALIGNED_DOUBLE:
2918 case NONSTATIC_DOUBLE:
2919 real_offset = next_nonstatic_double_offset;
2920 next_nonstatic_double_offset += BytesPerLong;
2921 break;
2922 default:
2923 ShouldNotReachHere();
2924 }
2925 fields->short_at_put(i+4, extract_low_short_from_int(real_offset) );
2926 fields->short_at_put(i+5, extract_high_short_from_int(real_offset) );
2927 }
2928
2929 // Size of instances
2930 int instance_size;
2931
2932 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
2933
2934 assert(instance_size == align_object_size(instanceOopDesc::header_size() + nonstatic_field_size), "consistent layout helper value");
2935
2936 // Size of non-static oop map blocks (in words) allocated at end of klass
2937 int nonstatic_oop_map_size = compute_oop_map_size(super_klass, nonstatic_oop_map_count, first_nonstatic_oop_offset);
2938
2939 // Compute reference type
2940 ReferenceType rt;
2941 if (super_klass() == NULL) {
2942 rt = REF_NONE;
2943 } else {
2944 rt = super_klass->reference_type();
2945 }
2946
2947 // We can now create the basic klassOop for this klass
2948 klassOop ik = oopFactory::new_instanceKlass(
2949 vtable_size, itable_size,
2950 static_field_size, nonstatic_oop_map_size,
2951 rt, CHECK_(nullHandle));
2952 instanceKlassHandle this_klass (THREAD, ik);
2953
2954 assert(this_klass->static_field_size() == static_field_size &&
2955 this_klass->nonstatic_oop_map_size() == nonstatic_oop_map_size, "sanity check");
2956
2957 // Fill in information already parsed
2958 this_klass->set_access_flags(access_flags);
2959 jint lh = Klass::instance_layout_helper(instance_size, false);
2960 this_klass->set_layout_helper(lh);
2961 assert(this_klass->oop_is_instance(), "layout is correct");
2962 assert(this_klass->size_helper() == instance_size, "correct size_helper");
2963 // Not yet: supers are done below to support the new subtype-checking fields
2964 //this_klass->set_super(super_klass());
2965 this_klass->set_class_loader(class_loader());
2966 this_klass->set_nonstatic_field_size(nonstatic_field_size);
2967 this_klass->set_static_oop_field_size(fac.static_oop_count);
2968 cp->set_pool_holder(this_klass());
2969 this_klass->set_constants(cp());
2970 this_klass->set_local_interfaces(local_interfaces());
2971 this_klass->set_fields(fields());
2972 this_klass->set_methods(methods());
2973 if (has_final_method) {
2974 this_klass->set_has_final_method();
2975 }
2976 this_klass->set_method_ordering(method_ordering());
2977 this_klass->set_initial_method_idnum(methods->length());
2978 this_klass->set_name(cp->klass_name_at(this_class_index));
2979 this_klass->set_protection_domain(protection_domain());
2980 this_klass->set_fields_annotations(fields_annotations());
2981 this_klass->set_methods_annotations(methods_annotations());
2982 this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
2983 this_klass->set_methods_default_annotations(methods_default_annotations());
2984
2985 this_klass->set_minor_version(minor_version);
2986 this_klass->set_major_version(major_version);
2987
2988 if (cached_class_file_bytes != NULL) {
2989 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
2990 this_klass->set_cached_class_file(cached_class_file_bytes,
2991 cached_class_file_length);
2992 }
2993
2994 // Miranda methods
2995 if ((num_miranda_methods > 0) ||
2996 // if this class introduced new miranda methods or
2997 (super_klass.not_null() && (super_klass->has_miranda_methods()))
2998 // super class exists and this class inherited miranda methods
3071 const char * from = Klass::cast(this_klass())->external_name();
3072 if (this_klass->java_super() != NULL) {
3073 tty->print("RESOLVE %s %s\n", from, instanceKlass::cast(this_klass->java_super())->external_name());
3074 }
3075 // print out each of the interface classes referred to by this class.
3076 objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces());
3077 if (!local_interfaces.is_null()) {
3078 int length = local_interfaces->length();
3079 for (int i = 0; i < length; i++) {
3080 klassOop k = klassOop(local_interfaces->obj_at(i));
3081 instanceKlass* to_class = instanceKlass::cast(k);
3082 const char * to = to_class->external_name();
3083 tty->print("RESOLVE %s %s\n", from, to);
3084 }
3085 }
3086 }
3087
3088 #ifndef PRODUCT
3089 if( PrintCompactFieldsSavings ) {
3090 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3091 tty->print("[Saved %d of %3d words in %s]\n",
3092 orig_nonstatic_field_size - nonstatic_field_size,
3093 orig_nonstatic_field_size, this_klass->external_name());
3094 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3095 tty->print("[Wasted %d over %3d words in %s]\n",
3096 nonstatic_field_size - orig_nonstatic_field_size,
3097 orig_nonstatic_field_size, this_klass->external_name());
3098 }
3099 }
3100 #endif
3101
3102 // preserve result across HandleMark
3103 preserve_this_klass = this_klass();
3104 }
3105
3106 // Create new handle outside HandleMark
3107 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
3108 debug_only(this_klass->as_klassOop()->verify();)
3109
3110 return this_klass;
3111 }
3112
3113
3114 int ClassFileParser::compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_map_count, int first_nonstatic_oop_offset) {
3115 int map_size = super.is_null() ? 0 : super->nonstatic_oop_map_size();
3116 if (nonstatic_oop_map_count > 0) {
3117 // We have oops to add to map
3118 if (map_size == 0) {
3119 map_size = nonstatic_oop_map_count;
3120 } else {
3121 // Check whether we should add a new map block or whether the last one can be extended
3122 OopMapBlock* first_map = super->start_of_nonstatic_oop_maps();
3123 OopMapBlock* last_map = first_map + map_size - 1;
3124
3125 int next_offset = last_map->offset() + (last_map->length() * oopSize);
3126 if (next_offset == first_nonstatic_oop_offset) {
3127 // There is no gap bettwen superklass's last oop field and first
3128 // local oop field, merge maps.
3129 nonstatic_oop_map_count -= 1;
3130 } else {
3131 // Superklass didn't end with a oop field, add extra maps
3132 assert(next_offset<first_nonstatic_oop_offset, "just checking");
3133 }
3134 map_size += nonstatic_oop_map_count;
3135 }
3136 }
3137 return map_size;
3138 }
3139
3140
3141 void ClassFileParser::fill_oop_maps(instanceKlassHandle k,
3142 int nonstatic_oop_map_count,
3143 u2* nonstatic_oop_offsets, u2* nonstatic_oop_length) {
3144 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
3145 OopMapBlock* last_oop_map = this_oop_map + k->nonstatic_oop_map_size();
3465 THREAD_AND_LOCATION,
3466 vmSymbolHandles::java_lang_ClassFormatError(),
3467 "Illegal class modifiers in class %s: 0x%X",
3468 _class_name->as_C_string(), flags
3469 );
3470 return;
3471 }
3472 }
3473
3474 bool ClassFileParser::has_illegal_visibility(jint flags) {
3475 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3476 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3477 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3478
3479 return ((is_public && is_protected) ||
3480 (is_public && is_private) ||
3481 (is_protected && is_private));
3482 }
3483
3484 bool ClassFileParser::is_supported_version(u2 major, u2 minor) {
3485 return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
3486 (major <= JAVA_MAX_SUPPORTED_VERSION) &&
3487 ((major != JAVA_MAX_SUPPORTED_VERSION) ||
3488 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION));
3489 }
3490
3491 void ClassFileParser::verify_legal_field_modifiers(
3492 jint flags, bool is_interface, TRAPS) {
3493 if (!_need_verify) { return; }
3494
3495 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3496 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3497 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3498 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3499 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3500 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
3501 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
3502 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3503 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3504
3505 bool is_illegal = false;
3506
3507 if (is_interface) {
|
1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)classFileParser.cpp 1.280 07/07/09 11:19:49 JVM"
3 #endif
4 /*
5 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 #include "incls/_precompiled.incl"
29 #include "incls/_classFileParser.cpp.incl"
30
31 // We generally try to create the oops directly when parsing, rather than allocating
32 // temporary data structures and copying the bytes twice. A temporary area is only
33 // needed when parsing utf8 entries in the constant pool and when parsing line number
34 // tables.
35
36 // We add assert in debug mode when class format is not checked.
37
38 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
39 #define JAVA_MIN_SUPPORTED_VERSION 45
40 #define JAVA_MAX_SUPPORTED_VERSION 51
41 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
42
43 // Used for two backward compatibility reasons:
44 // - to check for new additions to the class file format in JDK1.5
45 // - to check for bug fixes in the format checker in JDK1.5
46 #define JAVA_1_5_VERSION 49
47
48 // Used for backward compatibility reasons:
49 // - to check for javac bug fixes that happened after 1.5
50 // - also used as the max version when running in jdk6
51 #define JAVA_6_VERSION 50
52
53
54 void ClassFileParser::parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS) {
55 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
56 // this function (_current can be allocated in a register, with scalar
57 // replacement of aggregates). The _current pointer is copied back to
58 // stream() when this function returns. DON'T call another method within
59 // this method that uses stream().
60 ClassFileStream* cfs0 = stream();
61 ClassFileStream cfs1 = *cfs0;
62 ClassFileStream* cfs = &cfs1;
63 #ifdef ASSERT
64 u1* old_current = cfs0->current();
65 #endif
66
67 // Used for batching symbol allocations.
68 const char* names[SymbolTable::symbol_alloc_batch_size];
69 int lengths[SymbolTable::symbol_alloc_batch_size];
70 int indices[SymbolTable::symbol_alloc_batch_size];
154 }
155 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
156 break;
157 case JVM_CONSTANT_NameAndType :
158 {
159 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags
160 u2 name_index = cfs->get_u2_fast();
161 u2 signature_index = cfs->get_u2_fast();
162 cp->name_and_type_at_put(index, name_index, signature_index);
163 }
164 break;
165 case JVM_CONSTANT_Utf8 :
166 {
167 cfs->guarantee_more(2, CHECK); // utf8_length
168 u2 utf8_length = cfs->get_u2_fast();
169 u1* utf8_buffer = cfs->get_u1_buffer();
170 assert(utf8_buffer != NULL, "null utf8 buffer");
171 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.
172 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags
173 cfs->skip_u1_fast(utf8_length);
174
175 // Before storing the symbol, make sure it's legal
176 if (_need_verify) {
177 verify_legal_utf8((unsigned char*)utf8_buffer, utf8_length, CHECK);
178 }
179
180 if (AnonymousClasses && has_cp_patch_at(index)) {
181 Handle patch = clear_cp_patch_at(index);
182 guarantee_property(java_lang_String::is_instance(patch()),
183 "Illegal utf8 patch at %d in class file %s",
184 index, CHECK);
185 char* str = java_lang_String::as_utf8_string(patch());
186 // (could use java_lang_String::as_symbol instead, but might as well batch them)
187 utf8_buffer = (u1*) str;
188 utf8_length = (int) strlen(str);
189 }
190
191 unsigned int hash;
192 symbolOop result = SymbolTable::lookup_only((char*)utf8_buffer, utf8_length, hash);
193 if (result == NULL) {
194 names[names_count] = (char*)utf8_buffer;
195 lengths[names_count] = utf8_length;
196 indices[names_count] = index;
197 hashValues[names_count++] = hash;
198 if (names_count == SymbolTable::symbol_alloc_batch_size) {
199 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK);
200 names_count = 0;
201 }
202 } else {
203 cp->symbol_at_put(index, result);
204 }
205 }
206 break;
207 default:
208 classfile_parse_error(
209 "Unknown constant tag %u in class file %s", tag, CHECK);
210 break;
243 // parsing constant pool entries
244 parse_constant_pool_entries(cp, length, CHECK_(nullHandle));
245
246 int index = 1; // declared outside of loops for portability
247
248 // first verification pass - validate cross references and fixup class and string constants
249 for (index = 1; index < length; index++) { // Index 0 is unused
250 switch (cp->tag_at(index).value()) {
251 case JVM_CONSTANT_Class :
252 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
253 break;
254 case JVM_CONSTANT_Fieldref :
255 // fall through
256 case JVM_CONSTANT_Methodref :
257 // fall through
258 case JVM_CONSTANT_InterfaceMethodref : {
259 if (!_need_verify) break;
260 int klass_ref_index = cp->klass_ref_index_at(index);
261 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
262 check_property(valid_cp_range(klass_ref_index, length) &&
263 is_klass_reference(cp, klass_ref_index),
264 "Invalid constant pool index %u in class file %s",
265 klass_ref_index,
266 CHECK_(nullHandle));
267 check_property(valid_cp_range(name_and_type_ref_index, length) &&
268 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
269 "Invalid constant pool index %u in class file %s",
270 name_and_type_ref_index,
271 CHECK_(nullHandle));
272 break;
273 }
274 case JVM_CONSTANT_String :
275 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
276 break;
277 case JVM_CONSTANT_Integer :
278 break;
279 case JVM_CONSTANT_Float :
280 break;
281 case JVM_CONSTANT_Long :
282 case JVM_CONSTANT_Double :
283 index++;
324 break;
325 case JVM_CONSTANT_StringIndex :
326 {
327 int string_index = cp->string_index_at(index);
328 check_property(
329 valid_cp_range(string_index, length) &&
330 cp->tag_at(string_index).is_utf8(),
331 "Invalid constant pool index %u in class file %s",
332 string_index, CHECK_(nullHandle));
333 symbolOop sym = cp->symbol_at(string_index);
334 cp->unresolved_string_at_put(index, sym);
335 }
336 break;
337 default:
338 fatal1("bad constant pool tag value %u", cp->tag_at(index).value());
339 ShouldNotReachHere();
340 break;
341 } // end of switch
342 } // end of for
343
344 if (_cp_patches != NULL) {
345 // need to treat this_class specially...
346 assert(AnonymousClasses, "");
347 int this_class_index;
348 {
349 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
350 u1* mark = cfs->current();
351 u2 flags = cfs->get_u2_fast();
352 this_class_index = cfs->get_u2_fast();
353 cfs->set_current(mark); // revert to mark
354 }
355
356 for (index = 1; index < length; index++) { // Index 0 is unused
357 if (has_cp_patch_at(index)) {
358 guarantee_property(index != this_class_index,
359 "Illegal constant pool patch to self at %d in class file %s",
360 index, CHECK_(nullHandle));
361 patch_constant_pool(cp, index, cp_patch_at(index), CHECK_(nullHandle));
362 }
363 }
364 // Ensure that all the patches have been used.
365 for (index = 0; index < _cp_patches->length(); index++) {
366 guarantee_property(!has_cp_patch_at(index),
367 "Unused constant pool patch at %d in class file %s",
368 index, CHECK_(nullHandle));
369 }
370 }
371
372 if (!_need_verify) {
373 return cp;
374 }
375
376 // second verification pass - checks the strings are of the right format.
377 // but not yet to the other entries
378 for (index = 1; index < length; index++) {
379 jbyte tag = cp->tag_at(index).value();
380 switch (tag) {
381 case JVM_CONSTANT_UnresolvedClass: {
382 symbolHandle class_name(THREAD, cp->unresolved_klass_at(index));
383 // check the name, even if _cp_patches will overwrite it
384 verify_legal_class_name(class_name, CHECK_(nullHandle));
385 break;
386 }
387 case JVM_CONSTANT_Fieldref:
388 case JVM_CONSTANT_Methodref:
389 case JVM_CONSTANT_InterfaceMethodref: {
390 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
391 // already verified to be utf8
392 int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index);
393 // already verified to be utf8
394 int signature_ref_index = cp->signature_ref_index_at(name_and_type_ref_index);
395 symbolHandle name(THREAD, cp->symbol_at(name_ref_index));
396 symbolHandle signature(THREAD, cp->symbol_at(signature_ref_index));
397 if (tag == JVM_CONSTANT_Fieldref) {
398 verify_legal_field_name(name, CHECK_(nullHandle));
399 verify_legal_field_signature(name, signature, CHECK_(nullHandle));
400 } else {
401 verify_legal_method_name(name, CHECK_(nullHandle));
402 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
403 if (tag == JVM_CONSTANT_Methodref) {
406 unsigned int name_len = name->utf8_length();
407 assert(name_len > 0, "bad method name"); // already verified as legal name
408 if (name->byte_at(0) == '<') {
409 if (name() != vmSymbols::object_initializer_name()) {
410 classfile_parse_error(
411 "Bad method name at constant pool index %u in class file %s",
412 name_ref_index, CHECK_(nullHandle));
413 }
414 }
415 }
416 }
417 break;
418 }
419 } // end of switch
420 } // end of for
421
422 return cp;
423 }
424
425
426 void ClassFileParser::patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS) {
427 assert(AnonymousClasses, "");
428 BasicType patch_type = T_VOID;
429 switch (cp->tag_at(index).value()) {
430
431 case JVM_CONSTANT_UnresolvedClass :
432 // Patching a class means pre-resolving it.
433 // The name in the constant pool is ignored.
434 if (patch->klass() == SystemDictionary::class_klass()) { // %%% java_lang_Class::is_instance
435 guarantee_property(!java_lang_Class::is_primitive(patch()),
436 "Illegal class patch at %d in class file %s",
437 index, CHECK);
438 cp->klass_at_put(index, java_lang_Class::as_klassOop(patch()));
439 } else {
440 guarantee_property(java_lang_String::is_instance(patch()),
441 "Illegal class patch at %d in class file %s",
442 index, CHECK);
443 symbolHandle name = java_lang_String::as_symbol(patch(), CHECK);
444 cp->unresolved_klass_at_put(index, name());
445 }
446 break;
447
448 case JVM_CONSTANT_UnresolvedString :
449 // Patching a string means pre-resolving it.
450 // The spelling in the constant pool is ignored.
451 // The constant reference may be any object whatever.
452 // If it is not a real interned string, the constant is referred
453 // to as a "pseudo-string", and must be presented to the CP
454 // explicitly, because it may require scavenging.
455 cp->pseudo_string_at_put(index, patch());
456 break;
457
458 case JVM_CONSTANT_Integer : patch_type = T_INT; goto patch_prim;
459 case JVM_CONSTANT_Float : patch_type = T_FLOAT; goto patch_prim;
460 case JVM_CONSTANT_Long : patch_type = T_LONG; goto patch_prim;
461 case JVM_CONSTANT_Double : patch_type = T_DOUBLE; goto patch_prim;
462 patch_prim:
463 {
464 jvalue value;
465 BasicType value_type = java_lang_boxing_object::get_value(patch(), &value);
466 guarantee_property(value_type == patch_type,
467 "Illegal primitive patch at %d in class file %s",
468 index, CHECK);
469 switch (value_type) {
470 case T_INT: cp->int_at_put(index, value.i); break;
471 case T_FLOAT: cp->float_at_put(index, value.f); break;
472 case T_LONG: cp->long_at_put(index, value.j); break;
473 case T_DOUBLE: cp->double_at_put(index, value.d); break;
474 default: assert(false, "");
475 }
476 }
477 break;
478
479 default:
480 // %%% TODO: put method handles into CONSTANT_InterfaceMethodref, etc.
481 guarantee_property(!has_cp_patch_at(index),
482 "Illegal unexpected patch at %d in class file %s",
483 index, CHECK);
484 return;
485 }
486
487 // On fall-through, mark the patch as used.
488 clear_cp_patch_at(index);
489 }
490
491
492
493 class NameSigHash: public ResourceObj {
494 public:
495 symbolOop _name; // name
496 symbolOop _sig; // signature
497 NameSigHash* _next; // Next entry in hash table
498 };
499
500
501 #define HASH_ROW_SIZE 256
502
503 unsigned int hash(symbolOop name, symbolOop sig) {
504 unsigned int raw_hash = 0;
505 raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2);
506 raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize;
507
508 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;
509 }
510
511
512 void initialize_hashtable(NameSigHash** table) {
543 return true;
544 }
545
546
547 objArrayHandle ClassFileParser::parse_interfaces(constantPoolHandle cp,
548 int length,
549 Handle class_loader,
550 Handle protection_domain,
551 PerfTraceTime* vmtimer,
552 symbolHandle class_name,
553 TRAPS) {
554 ClassFileStream* cfs = stream();
555 assert(length > 0, "only called for length>0");
556 objArrayHandle nullHandle;
557 objArrayOop interface_oop = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
558 objArrayHandle interfaces (THREAD, interface_oop);
559
560 int index;
561 for (index = 0; index < length; index++) {
562 u2 interface_index = cfs->get_u2(CHECK_(nullHandle));
563 KlassHandle interf;
564 check_property(
565 valid_cp_range(interface_index, cp->length()) &&
566 is_klass_reference(cp, interface_index),
567 "Interface name has bad constant pool index %u in class file %s",
568 interface_index, CHECK_(nullHandle));
569 if (cp->tag_at(interface_index).is_klass()) {
570 interf = KlassHandle(THREAD, cp->resolved_klass_at(interface_index));
571 } else {
572 symbolHandle unresolved_klass (THREAD, cp->klass_name_at(interface_index));
573
574 // Don't need to check legal name because it's checked when parsing constant pool.
575 // But need to make sure it's not an array type.
576 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY,
577 "Bad interface name in class file %s", CHECK_(nullHandle));
578
579 vmtimer->suspend(); // do not count recursive loading twice
580 // Call resolve_super so classcircularity is checked
581 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
582 unresolved_klass, class_loader, protection_domain,
583 false, CHECK_(nullHandle));
584 interf = KlassHandle(THREAD, k);
585 vmtimer->resume();
586
587 if (LinkWellKnownClasses) // my super type is well known to me
588 cp->klass_at_put(interface_index, interf()); // eagerly resolve
589 }
590
591 if (!Klass::cast(interf())->is_interface()) {
592 THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", nullHandle);
593 }
594 interfaces->obj_at_put(index, interf());
595 }
596
597 if (!_need_verify || length <= 1) {
598 return interfaces;
599 }
600
601 // Check if there's any duplicates in interfaces
602 ResourceMark rm(THREAD);
603 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(
604 THREAD, NameSigHash*, HASH_ROW_SIZE);
605 initialize_hashtable(interface_names);
606 bool dup = false;
607 {
608 debug_only(No_Safepoint_Verifier nsv;)
609 for (index = 0; index < length; index++) {
610 klassOop k = (klassOop)interfaces->obj_at(index);
980
981 // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index]
982 typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle));
983 typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh);
984
985 int index = 0;
986 cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index
987 for (unsigned int i = 0; i < exception_table_length; i++) {
988 u2 start_pc = cfs->get_u2_fast();
989 u2 end_pc = cfs->get_u2_fast();
990 u2 handler_pc = cfs->get_u2_fast();
991 u2 catch_type_index = cfs->get_u2_fast();
992 // Will check legal target after parsing code array in verifier.
993 if (_need_verify) {
994 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
995 "Illegal exception table range in class file %s", CHECK_(nullHandle));
996 guarantee_property(handler_pc < code_length,
997 "Illegal exception table handler in class file %s", CHECK_(nullHandle));
998 if (catch_type_index != 0) {
999 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
1000 is_klass_reference(cp, catch_type_index),
1001 "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle));
1002 }
1003 }
1004 exception_handlers->int_at_put(index++, start_pc);
1005 exception_handlers->int_at_put(index++, end_pc);
1006 exception_handlers->int_at_put(index++, handler_pc);
1007 exception_handlers->int_at_put(index++, catch_type_index);
1008 }
1009 return exception_handlers;
1010 }
1011
1012 void ClassFileParser::parse_linenumber_table(
1013 u4 code_attribute_length, u4 code_length,
1014 CompressedLineNumberWriteStream** write_stream, TRAPS) {
1015 ClassFileStream* cfs = stream();
1016 unsigned int num_entries = cfs->get_u2(CHECK);
1017
1018 // Each entry is a u2 start_pc, and a u2 line_number
1019 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
1020
1219 }
1220 }
1221 return localvariable_table_start;
1222 }
1223
1224
1225 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1226 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS) {
1227 ClassFileStream* cfs = stream();
1228 u2 index = 0; // index in the array with long/double occupying two slots
1229 u4 i1 = *u1_index;
1230 u4 i2 = *u2_index + 1;
1231 for(int i = 0; i < array_length; i++) {
1232 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1233 index++;
1234 if (tag == ITEM_Long || tag == ITEM_Double) {
1235 index++;
1236 } else if (tag == ITEM_Object) {
1237 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK);
1238 guarantee_property(valid_cp_range(class_index, cp->length()) &&
1239 is_klass_reference(cp, class_index),
1240 "Bad class index %u in StackMap in class file %s",
1241 class_index, CHECK);
1242 } else if (tag == ITEM_Uninitialized) {
1243 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK);
1244 guarantee_property(
1245 offset < code_length,
1246 "Bad uninitialized type offset %u in StackMap in class file %s",
1247 offset, CHECK);
1248 } else {
1249 guarantee_property(
1250 tag <= (u1)ITEM_Uninitialized,
1251 "Unknown variable type %u in StackMap in class file %s",
1252 tag, CHECK);
1253 }
1254 }
1255 u2_array[*u2_index] = index;
1256 *u1_index = i1;
1257 *u2_index = i2;
1258 }
1259
1285 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length,
1286 u4 method_attribute_length,
1287 constantPoolHandle cp, TRAPS) {
1288 ClassFileStream* cfs = stream();
1289 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length
1290 *checked_exceptions_length = cfs->get_u2_fast();
1291 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);
1292 u2* checked_exceptions_start = cfs->get_u2_buffer();
1293 assert(checked_exceptions_start != NULL, "null checked exceptions");
1294 if (!_need_verify) {
1295 cfs->skip_u2_fast(size);
1296 } else {
1297 // Verify each value in the checked exception table
1298 u2 checked_exception;
1299 u2 len = *checked_exceptions_length;
1300 cfs->guarantee_more(2 * len, CHECK_NULL);
1301 for (int i = 0; i < len; i++) {
1302 checked_exception = cfs->get_u2_fast();
1303 check_property(
1304 valid_cp_range(checked_exception, cp->length()) &&
1305 is_klass_reference(cp, checked_exception),
1306 "Exception name has bad type at constant pool %u in class file %s",
1307 checked_exception, CHECK_NULL);
1308 }
1309 }
1310 // check exceptions attribute length
1311 if (_need_verify) {
1312 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1313 sizeof(u2) * size),
1314 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1315 }
1316 return checked_exceptions_start;
1317 }
1318
1319
1320 #define MAX_ARGS_SIZE 255
1321 #define MAX_CODE_SIZE 65535
1322 #define INITIAL_MAX_LVT_NUMBER 256
1323
1324 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
1325 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the
1462 "Invalid method Code length %u in class file %s",
1463 code_length, CHECK_(nullHandle));
1464 }
1465 // Code pointer
1466 code_start = cfs->get_u1_buffer();
1467 assert(code_start != NULL, "null code start");
1468 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1469 cfs->skip_u1_fast(code_length);
1470
1471 // Exception handler table
1472 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1473 exception_table_length = cfs->get_u2_fast();
1474 if (exception_table_length > 0) {
1475 exception_handlers =
1476 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1477 }
1478
1479 // Parse additional attributes in code attribute
1480 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1481 u2 code_attributes_count = cfs->get_u2_fast();
1482
1483 unsigned int calculated_attribute_length = 0;
1484
1485 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) {
1486 calculated_attribute_length =
1487 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
1488 } else {
1489 // max_stack, locals and length are smaller in pre-version 45.2 classes
1490 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2);
1491 }
1492 calculated_attribute_length +=
1493 code_length +
1494 sizeof(exception_table_length) +
1495 sizeof(code_attributes_count) +
1496 exception_table_length *
1497 ( sizeof(u2) + // start_pc
1498 sizeof(u2) + // end_pc
1499 sizeof(u2) + // handler_pc
1500 sizeof(u2) ); // catch_type_index
1501
1502 while (code_attributes_count--) {
1503 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
1504 u2 code_attribute_name_index = cfs->get_u2_fast();
1505 u4 code_attribute_length = cfs->get_u4_fast();
1506 calculated_attribute_length += code_attribute_length +
1507 sizeof(code_attribute_name_index) +
1508 sizeof(code_attribute_length);
1509 check_property(valid_cp_range(code_attribute_name_index, cp_size) &&
1510 cp->tag_at(code_attribute_name_index).is_utf8(),
1511 "Invalid code attribute name index %u in class file %s",
1512 code_attribute_name_index,
1513 CHECK_(nullHandle));
1514 if (LoadLineNumberTables &&
1515 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
1516 // Parse and compress line number table
1517 parse_linenumber_table(code_attribute_length, code_length,
1518 &linenumber_table, CHECK_(nullHandle));
1519
1520 } else if (LoadLocalVariableTables &&
2020 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC)
2021
2022 // Return number of classes in the inner classes attribute table
2023 u2 ClassFileParser::parse_classfile_inner_classes_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2024 ClassFileStream* cfs = stream();
2025 cfs->guarantee_more(2, CHECK_0); // length
2026 u2 length = cfs->get_u2_fast();
2027
2028 // 4-tuples of shorts [inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags]
2029 typeArrayOop ic = oopFactory::new_permanent_shortArray(length*4, CHECK_0);
2030 typeArrayHandle inner_classes(THREAD, ic);
2031 int index = 0;
2032 int cp_size = cp->length();
2033 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
2034 for (int n = 0; n < length; n++) {
2035 // Inner class index
2036 u2 inner_class_info_index = cfs->get_u2_fast();
2037 check_property(
2038 inner_class_info_index == 0 ||
2039 (valid_cp_range(inner_class_info_index, cp_size) &&
2040 is_klass_reference(cp, inner_class_info_index)),
2041 "inner_class_info_index %u has bad constant type in class file %s",
2042 inner_class_info_index, CHECK_0);
2043 // Outer class index
2044 u2 outer_class_info_index = cfs->get_u2_fast();
2045 check_property(
2046 outer_class_info_index == 0 ||
2047 (valid_cp_range(outer_class_info_index, cp_size) &&
2048 is_klass_reference(cp, outer_class_info_index)),
2049 "outer_class_info_index %u has bad constant type in class file %s",
2050 outer_class_info_index, CHECK_0);
2051 // Inner class name
2052 u2 inner_name_index = cfs->get_u2_fast();
2053 check_property(
2054 inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) &&
2055 cp->tag_at(inner_name_index).is_utf8()),
2056 "inner_name_index %u has bad constant type in class file %s",
2057 inner_name_index, CHECK_0);
2058 if (_need_verify) {
2059 guarantee_property(inner_class_info_index != outer_class_info_index,
2060 "Class is both outer and inner class in class file %s", CHECK_0);
2061 }
2062 // Access flags
2063 AccessFlags inner_access_flags;
2064 jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
2065 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2066 // Set abstract bit for old class files for backward compatibility
2067 flags |= JVM_ACC_ABSTRACT;
2068 }
2190 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2191 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2192 runtime_invisible_annotations_length = attribute_length;
2193 runtime_invisible_annotations = cfs->get_u1_buffer();
2194 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2195 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
2196 } else if (tag == vmSymbols::tag_enclosing_method()) {
2197 if (parsed_enclosingmethod_attribute) {
2198 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2199 } else {
2200 parsed_enclosingmethod_attribute = true;
2201 }
2202 cfs->guarantee_more(4, CHECK); // class_index, method_index
2203 u2 class_index = cfs->get_u2_fast();
2204 u2 method_index = cfs->get_u2_fast();
2205 if (class_index == 0) {
2206 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2207 }
2208 // Validate the constant pool indices and types
2209 if (!cp->is_within_bounds(class_index) ||
2210 !is_klass_reference(cp, class_index)) {
2211 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2212 }
2213 if (method_index != 0 &&
2214 (!cp->is_within_bounds(method_index) ||
2215 !cp->tag_at(method_index).is_name_and_type())) {
2216 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2217 }
2218 k->set_enclosing_method_indices(class_index, method_index);
2219 } else {
2220 // Unknown attribute
2221 cfs->skip_u1(attribute_length, CHECK);
2222 }
2223 } else {
2224 // Unknown attribute
2225 cfs->skip_u1(attribute_length, CHECK);
2226 }
2227 }
2228 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2229 runtime_visible_annotations_length,
2230 runtime_invisible_annotations,
2444 // end of the object because the Class layout changed between JDK
2445 // 1.3 and JDK 1.4 with the new reflection implementation; some
2446 // nonstatic oop fields were added at the Java level. The offsets
2447 // of these fake fields can't change between these two JDK
2448 // versions because when the offsets are computed at bootstrap
2449 // time we don't know yet which version of the JDK we're running in.
2450
2451 // The values below are fake but will force two non-static oop fields and
2452 // a corresponding non-static oop map block to be allocated.
2453 const int extra = java_lang_Class::number_of_fake_oop_fields;
2454 fac_ptr->nonstatic_oop_count += extra;
2455 }
2456
2457
2458 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2459 // Cause the extra fake fields in java.lang.Class to show up before
2460 // the Java fields for layout compatibility between 1.3 and 1.4
2461 // Incrementing next_nonstatic_oop_offset here advances the
2462 // location where the real java fields are placed.
2463 const int extra = java_lang_Class::number_of_fake_oop_fields;
2464 (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2465 }
2466
2467
2468 instanceKlassHandle ClassFileParser::parseClassFile(symbolHandle name,
2469 Handle class_loader,
2470 Handle protection_domain,
2471 GrowableArray<Handle>* cp_patches,
2472 symbolHandle& parsed_name,
2473 TRAPS) {
2474 // So that JVMTI can cache class file in the state before retransformable agents
2475 // have modified it
2476 unsigned char *cached_class_file_bytes = NULL;
2477 jint cached_class_file_length;
2478
2479 ClassFileStream* cfs = stream();
2480 // Timing
2481 PerfTraceTime vmtimer(ClassLoader::perf_accumulated_time());
2482
2483 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2484
2485 if (JvmtiExport::should_post_class_file_load_hook()) {
2486 unsigned char* ptr = cfs->buffer();
2487 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2488
2489 JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain,
2490 &ptr, &end_ptr,
2491 &cached_class_file_bytes,
2492 &cached_class_file_length);
2493
2494 if (ptr != cfs->buffer()) {
2495 // JVMTI agent has modified class file data.
2496 // Set new class file stream using JVMTI agent modified
2497 // class file data.
2498 cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source());
2499 set_stream(cfs);
2500 }
2501 }
2502
2503 _cp_patches = cp_patches;
2504
2505 instanceKlassHandle nullHandle;
2506
2507 // Figure out whether we can skip format checking (matching classic VM behavior)
2508 _need_verify = Verifier::should_verify_for(class_loader());
2509
2510 // Set the verify flag in stream
2511 cfs->set_verify(_need_verify);
2512
2513 // Save the class file name for easier error message printing.
2514 _class_name = name.not_null()? name : vmSymbolHandles::unknown_class_name();
2515
2516 cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor
2517 // Magic value
2518 u4 magic = cfs->get_u4_fast();
2519 guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
2520 "Incompatible magic value %u in class file %s",
2521 magic, CHECK_(nullHandle));
2522
2523 // Version numbers
2614 name->as_C_string(),
2615 class_name->as_C_string()
2616 );
2617 return nullHandle;
2618 }
2619
2620 if (TraceClassLoadingPreorder) {
2621 tty->print("[Loading %s", name()->as_klass_external_name());
2622 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
2623 tty->print_cr("]");
2624 }
2625
2626 u2 super_class_index = cfs->get_u2_fast();
2627 if (super_class_index == 0) {
2628 check_property(class_name() == vmSymbols::java_lang_Object(),
2629 "Invalid superclass index %u in class file %s",
2630 super_class_index,
2631 CHECK_(nullHandle));
2632 } else {
2633 check_property(valid_cp_range(super_class_index, cp_size) &&
2634 is_klass_reference(cp, super_class_index),
2635 "Invalid superclass index %u in class file %s",
2636 super_class_index,
2637 CHECK_(nullHandle));
2638 // The class name should be legal because it is checked when parsing constant pool.
2639 // However, make sure it is not an array type.
2640 bool is_array = false;
2641 if (cp->tag_at(super_class_index).is_klass()) {
2642 super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index));
2643 if (_need_verify)
2644 is_array = super_klass->oop_is_array();
2645 } else if (_need_verify) {
2646 is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
2647 }
2648 if (_need_verify) {
2649 guarantee_property(!is_array,
2650 "Bad superclass name in class file %s", CHECK_(nullHandle));
2651 }
2652 }
2653
2654 // Interfaces
2655 u2 itfs_len = cfs->get_u2_fast();
2656 objArrayHandle local_interfaces;
2657 if (itfs_len == 0) {
2658 local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2659 } else {
2660 local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, &vmtimer, _class_name, CHECK_(nullHandle));
2661 }
2662
2663 // Fields (offsets are filled in later)
2664 struct FieldAllocationCount fac = {0,0,0,0,0,0,0,0,0,0};
2665 objArrayHandle fields_annotations;
2666 typeArrayHandle fields = parse_fields(cp, access_flags.is_interface(), &fac, &fields_annotations, CHECK_(nullHandle));
2667 // Methods
2668 bool has_final_method = false;
2669 AccessFlags promoted_flags;
2670 promoted_flags.set_flags(0);
2671 // These need to be oop pointers because they are allocated lazily
2672 // inside parse_methods inside a nested HandleMark
2673 objArrayOop methods_annotations_oop = NULL;
2674 objArrayOop methods_parameter_annotations_oop = NULL;
2675 objArrayOop methods_default_annotations_oop = NULL;
2676 objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
2677 &promoted_flags,
2678 &has_final_method,
2679 &methods_annotations_oop,
2680 &methods_parameter_annotations_oop,
2681 &methods_default_annotations_oop,
2682 CHECK_(nullHandle));
2683
2684 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2685 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2686 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
2687
2688 // We check super class after class file is parsed and format is checked
2689 if (super_class_index > 0 && super_klass.is_null()) {
2690 symbolHandle sk (THREAD, cp->klass_name_at(super_class_index));
2691 if (access_flags.is_interface()) {
2692 // Before attempting to resolve the superclass, check for class format
2693 // errors not checked yet.
2694 guarantee_property(sk() == vmSymbols::java_lang_Object(),
2695 "Interfaces must have java.lang.Object as superclass in class file %s",
2696 CHECK_(nullHandle));
2697 }
2698 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
2699 sk,
2700 class_loader,
2701 protection_domain,
2702 true,
2703 CHECK_(nullHandle));
2704 KlassHandle kh (THREAD, k);
2705 super_klass = instanceKlassHandle(THREAD, kh());
2706 if (LinkWellKnownClasses) // my super class is well known to me
2707 cp->klass_at_put(super_class_index, super_klass()); // eagerly resolve
2708 }
2709 if (super_klass.not_null()) {
2710 if (super_klass->is_interface()) {
2711 ResourceMark rm(THREAD);
2712 Exceptions::fthrow(
2713 THREAD_AND_LOCATION,
2714 vmSymbolHandles::java_lang_IncompatibleClassChangeError(),
2715 "class %s has interface %s as super class",
2716 class_name->as_klass_external_name(),
2717 super_klass->external_name()
2718 );
2719 return nullHandle;
2720 }
2721 // Make sure super class is not final
2722 if (super_klass->is_final()) {
2723 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
2724 }
2725 }
2726
2727 // Compute the transitive list of all unique interfaces implemented by this class
2728 objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
2729
2764 int next_static_double_offset;
2765 int next_static_word_offset;
2766 int next_static_short_offset;
2767 int next_static_byte_offset;
2768 int next_static_type_offset;
2769 int next_nonstatic_oop_offset;
2770 int next_nonstatic_double_offset;
2771 int next_nonstatic_word_offset;
2772 int next_nonstatic_short_offset;
2773 int next_nonstatic_byte_offset;
2774 int next_nonstatic_type_offset;
2775 int first_nonstatic_oop_offset;
2776 int first_nonstatic_field_offset;
2777 int next_nonstatic_field_offset;
2778
2779 // Calculate the starting byte offsets
2780 next_static_oop_offset = (instanceKlass::header_size() +
2781 align_object_offset(vtable_size) +
2782 align_object_offset(itable_size)) * wordSize;
2783 next_static_double_offset = next_static_oop_offset +
2784 (fac.static_oop_count * heapOopSize);
2785 if ( fac.static_double_count &&
2786 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
2787 Universe::field_type_should_be_aligned(T_LONG)) ) {
2788 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
2789 }
2790
2791 next_static_word_offset = next_static_double_offset +
2792 (fac.static_double_count * BytesPerLong);
2793 next_static_short_offset = next_static_word_offset +
2794 (fac.static_word_count * BytesPerInt);
2795 next_static_byte_offset = next_static_short_offset +
2796 (fac.static_short_count * BytesPerShort);
2797 next_static_type_offset = align_size_up((next_static_byte_offset +
2798 fac.static_byte_count ), wordSize );
2799 static_field_size = (next_static_type_offset -
2800 next_static_oop_offset) / wordSize;
2801 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
2802 nonstatic_field_size * heapOopSize;
2803 next_nonstatic_field_offset = first_nonstatic_field_offset;
2804
2805 // Add fake fields for java.lang.Class instances (also see below)
2806 if (class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2807 java_lang_Class_fix_pre(&methods, &fac, CHECK_(nullHandle));
2808 }
2809
2810 // Add a fake "discovered" field if it is not present
2811 // for compatibility with earlier jdk's.
2812 if (class_name() == vmSymbols::java_lang_ref_Reference()
2813 && class_loader.is_null()) {
2814 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
2815 }
2816 // end of "discovered" field compactibility fix
2817
2818 int nonstatic_double_count = fac.nonstatic_double_count;
2819 int nonstatic_word_count = fac.nonstatic_word_count;
2820 int nonstatic_short_count = fac.nonstatic_short_count;
2821 int nonstatic_byte_count = fac.nonstatic_byte_count;
2822 int nonstatic_oop_count = fac.nonstatic_oop_count;
2823
2824 bool super_has_nonstatic_fields =
2825 (super_klass() != NULL && super_klass->has_nonstatic_fields());
2826 bool has_nonstatic_fields = super_has_nonstatic_fields ||
2827 ((nonstatic_double_count + nonstatic_word_count +
2828 nonstatic_short_count + nonstatic_byte_count +
2829 nonstatic_oop_count) != 0);
2830
2831
2832 // Prepare list of oops for oop maps generation.
2833 u2* nonstatic_oop_offsets;
2834 u2* nonstatic_oop_length;
2835 int nonstatic_oop_map_count = 0;
2836
2837 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
2838 THREAD, u2, nonstatic_oop_count+1);
2839 nonstatic_oop_length = NEW_RESOURCE_ARRAY_IN_THREAD(
2840 THREAD, u2, nonstatic_oop_count+1);
2841
2842 // Add fake fields for java.lang.Class instances (also see above).
2843 // FieldsAllocationStyle and CompactFields values will be reset to default.
2844 if(class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2845 java_lang_Class_fix_post(&next_nonstatic_field_offset);
2846 nonstatic_oop_offsets[0] = (u2)first_nonstatic_field_offset;
2847 int fake_oop_count = (( next_nonstatic_field_offset -
2848 first_nonstatic_field_offset ) / heapOopSize);
2849 nonstatic_oop_length [0] = (u2)fake_oop_count;
2850 nonstatic_oop_map_count = 1;
2851 nonstatic_oop_count -= fake_oop_count;
2852 first_nonstatic_oop_offset = first_nonstatic_field_offset;
2853 } else {
2854 first_nonstatic_oop_offset = 0; // will be set for first oop field
2855 }
2856
2857 #ifndef PRODUCT
2858 if( PrintCompactFieldsSavings ) {
2859 next_nonstatic_double_offset = next_nonstatic_field_offset +
2860 (nonstatic_oop_count * heapOopSize);
2861 if ( nonstatic_double_count > 0 ) {
2862 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
2863 }
2864 next_nonstatic_word_offset = next_nonstatic_double_offset +
2865 (nonstatic_double_count * BytesPerLong);
2866 next_nonstatic_short_offset = next_nonstatic_word_offset +
2867 (nonstatic_word_count * BytesPerInt);
2868 next_nonstatic_byte_offset = next_nonstatic_short_offset +
2869 (nonstatic_short_count * BytesPerShort);
2870 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
2871 nonstatic_byte_count ), heapOopSize );
2872 orig_nonstatic_field_size = nonstatic_field_size +
2873 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
2874 }
2875 #endif
2876 bool compact_fields = CompactFields;
2877 int allocation_style = FieldsAllocationStyle;
2878 if( allocation_style < 0 || allocation_style > 1 ) { // Out of range?
2879 assert(false, "0 <= FieldsAllocationStyle <= 1");
2880 allocation_style = 1; // Optimistic
2881 }
2882
2883 // The next classes have predefined hard-coded fields offsets
2884 // (see in JavaClasses::compute_hard_coded_offsets()).
2885 // Use default fields allocation order for them.
2886 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
2887 (class_name() == vmSymbols::java_lang_AssertionStatusDirectives() ||
2888 class_name() == vmSymbols::java_lang_Class() ||
2889 class_name() == vmSymbols::java_lang_ClassLoader() ||
2890 class_name() == vmSymbols::java_lang_ref_Reference() ||
2891 class_name() == vmSymbols::java_lang_ref_SoftReference() ||
2892 class_name() == vmSymbols::java_lang_StackTraceElement() ||
2893 class_name() == vmSymbols::java_lang_String() ||
2894 class_name() == vmSymbols::java_lang_Throwable() ||
2895 class_name() == vmSymbols::java_lang_Boolean() ||
2896 class_name() == vmSymbols::java_lang_Character() ||
2897 class_name() == vmSymbols::java_lang_Float() ||
2898 class_name() == vmSymbols::java_lang_Double() ||
2899 class_name() == vmSymbols::java_lang_Byte() ||
2900 class_name() == vmSymbols::java_lang_Short() ||
2901 class_name() == vmSymbols::java_lang_Integer() ||
2902 class_name() == vmSymbols::java_lang_Long())) {
2903 allocation_style = 0; // Allocate oops first
2904 compact_fields = false; // Don't compact fields
2905 }
2906
2907 if( allocation_style == 0 ) {
2908 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes
2909 next_nonstatic_oop_offset = next_nonstatic_field_offset;
2910 next_nonstatic_double_offset = next_nonstatic_oop_offset +
2911 (nonstatic_oop_count * heapOopSize);
2912 } else if( allocation_style == 1 ) {
2913 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops
2914 next_nonstatic_double_offset = next_nonstatic_field_offset;
2915 } else {
2916 ShouldNotReachHere();
2917 }
2918
2919 int nonstatic_oop_space_count = 0;
2920 int nonstatic_word_space_count = 0;
2921 int nonstatic_short_space_count = 0;
2922 int nonstatic_byte_space_count = 0;
2923 int nonstatic_oop_space_offset;
2924 int nonstatic_word_space_offset;
2925 int nonstatic_short_space_offset;
2926 int nonstatic_byte_space_offset;
2927
2928 if( nonstatic_double_count > 0 ) {
2929 int offset = next_nonstatic_double_offset;
2930 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
2931 if( compact_fields && offset != next_nonstatic_double_offset ) {
2937 nonstatic_word_count -= 1;
2938 nonstatic_word_space_count = 1; // Only one will fit
2939 length -= BytesPerInt;
2940 offset += BytesPerInt;
2941 }
2942 nonstatic_short_space_offset = offset;
2943 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
2944 nonstatic_short_count -= 1;
2945 nonstatic_short_space_count += 1;
2946 length -= BytesPerShort;
2947 offset += BytesPerShort;
2948 }
2949 nonstatic_byte_space_offset = offset;
2950 while( length > 0 && nonstatic_byte_count > 0 ) {
2951 nonstatic_byte_count -= 1;
2952 nonstatic_byte_space_count += 1;
2953 length -= 1;
2954 }
2955 // Allocate oop field in the gap if there are no other fields for that.
2956 nonstatic_oop_space_offset = offset;
2957 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
2958 allocation_style != 0 ) { // when oop fields not first
2959 nonstatic_oop_count -= 1;
2960 nonstatic_oop_space_count = 1; // Only one will fit
2961 length -= heapOopSize;
2962 offset += heapOopSize;
2963 }
2964 }
2965 }
2966
2967 next_nonstatic_word_offset = next_nonstatic_double_offset +
2968 (nonstatic_double_count * BytesPerLong);
2969 next_nonstatic_short_offset = next_nonstatic_word_offset +
2970 (nonstatic_word_count * BytesPerInt);
2971 next_nonstatic_byte_offset = next_nonstatic_short_offset +
2972 (nonstatic_short_count * BytesPerShort);
2973
2974 int notaligned_offset;
2975 if( allocation_style == 0 ) {
2976 notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
2977 } else { // allocation_style == 1
2978 next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
2979 if( nonstatic_oop_count > 0 ) {
2980 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
2981 }
2982 notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
2983 }
2984 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
2985 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
2986 - first_nonstatic_field_offset)/heapOopSize);
2987
2988 // Iterate over fields again and compute correct offsets.
2989 // The field allocation type was temporarily stored in the offset slot.
2990 // oop fields are located before non-oop fields (static and non-static).
2991 int len = fields->length();
2992 for (int i = 0; i < len; i += instanceKlass::next_offset) {
2993 int real_offset;
2994 FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i+4);
2995 switch (atype) {
2996 case STATIC_OOP:
2997 real_offset = next_static_oop_offset;
2998 next_static_oop_offset += heapOopSize;
2999 break;
3000 case STATIC_BYTE:
3001 real_offset = next_static_byte_offset;
3002 next_static_byte_offset += 1;
3003 break;
3004 case STATIC_SHORT:
3005 real_offset = next_static_short_offset;
3006 next_static_short_offset += BytesPerShort;
3007 break;
3008 case STATIC_WORD:
3009 real_offset = next_static_word_offset;
3010 next_static_word_offset += BytesPerInt;
3011 break;
3012 case STATIC_ALIGNED_DOUBLE:
3013 case STATIC_DOUBLE:
3014 real_offset = next_static_double_offset;
3015 next_static_double_offset += BytesPerLong;
3016 break;
3017 case NONSTATIC_OOP:
3018 if( nonstatic_oop_space_count > 0 ) {
3019 real_offset = nonstatic_oop_space_offset;
3020 nonstatic_oop_space_offset += heapOopSize;
3021 nonstatic_oop_space_count -= 1;
3022 } else {
3023 real_offset = next_nonstatic_oop_offset;
3024 next_nonstatic_oop_offset += heapOopSize;
3025 }
3026 // Update oop maps
3027 if( nonstatic_oop_map_count > 0 &&
3028 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3029 (u2)(real_offset - nonstatic_oop_length[nonstatic_oop_map_count - 1] * heapOopSize) ) {
3030 // Extend current oop map
3031 nonstatic_oop_length[nonstatic_oop_map_count - 1] += 1;
3032 } else {
3033 // Create new oop map
3034 nonstatic_oop_offsets[nonstatic_oop_map_count] = (u2)real_offset;
3035 nonstatic_oop_length [nonstatic_oop_map_count] = 1;
3036 nonstatic_oop_map_count += 1;
3037 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3038 first_nonstatic_oop_offset = real_offset;
3039 }
3040 }
3041 break;
3042 case NONSTATIC_BYTE:
3043 if( nonstatic_byte_space_count > 0 ) {
3044 real_offset = nonstatic_byte_space_offset;
3045 nonstatic_byte_space_offset += 1;
3046 nonstatic_byte_space_count -= 1;
3047 } else {
3048 real_offset = next_nonstatic_byte_offset;
3049 next_nonstatic_byte_offset += 1;
3067 } else {
3068 real_offset = next_nonstatic_word_offset;
3069 next_nonstatic_word_offset += BytesPerInt;
3070 }
3071 break;
3072 case NONSTATIC_ALIGNED_DOUBLE:
3073 case NONSTATIC_DOUBLE:
3074 real_offset = next_nonstatic_double_offset;
3075 next_nonstatic_double_offset += BytesPerLong;
3076 break;
3077 default:
3078 ShouldNotReachHere();
3079 }
3080 fields->short_at_put(i+4, extract_low_short_from_int(real_offset) );
3081 fields->short_at_put(i+5, extract_high_short_from_int(real_offset) );
3082 }
3083
3084 // Size of instances
3085 int instance_size;
3086
3087 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3088 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3089
3090 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3091
3092 // Size of non-static oop map blocks (in words) allocated at end of klass
3093 int nonstatic_oop_map_size = compute_oop_map_size(super_klass, nonstatic_oop_map_count, first_nonstatic_oop_offset);
3094
3095 // Compute reference type
3096 ReferenceType rt;
3097 if (super_klass() == NULL) {
3098 rt = REF_NONE;
3099 } else {
3100 rt = super_klass->reference_type();
3101 }
3102
3103 // We can now create the basic klassOop for this klass
3104 klassOop ik = oopFactory::new_instanceKlass(
3105 vtable_size, itable_size,
3106 static_field_size, nonstatic_oop_map_size,
3107 rt, CHECK_(nullHandle));
3108 instanceKlassHandle this_klass (THREAD, ik);
3109
3110 assert(this_klass->static_field_size() == static_field_size &&
3111 this_klass->nonstatic_oop_map_size() == nonstatic_oop_map_size, "sanity check");
3112
3113 // Fill in information already parsed
3114 this_klass->set_access_flags(access_flags);
3115 jint lh = Klass::instance_layout_helper(instance_size, false);
3116 this_klass->set_layout_helper(lh);
3117 assert(this_klass->oop_is_instance(), "layout is correct");
3118 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3119 // Not yet: supers are done below to support the new subtype-checking fields
3120 //this_klass->set_super(super_klass());
3121 this_klass->set_class_loader(class_loader());
3122 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3123 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3124 this_klass->set_static_oop_field_size(fac.static_oop_count);
3125 cp->set_pool_holder(this_klass());
3126 this_klass->set_constants(cp());
3127 this_klass->set_local_interfaces(local_interfaces());
3128 this_klass->set_fields(fields());
3129 this_klass->set_methods(methods());
3130 if (has_final_method) {
3131 this_klass->set_has_final_method();
3132 }
3133 this_klass->set_method_ordering(method_ordering());
3134 this_klass->set_initial_method_idnum(methods->length());
3135 this_klass->set_name(cp->klass_name_at(this_class_index));
3136 if (LinkWellKnownClasses) // I am well known to myself
3137 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3138 this_klass->set_protection_domain(protection_domain());
3139 this_klass->set_fields_annotations(fields_annotations());
3140 this_klass->set_methods_annotations(methods_annotations());
3141 this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3142 this_klass->set_methods_default_annotations(methods_default_annotations());
3143
3144 this_klass->set_minor_version(minor_version);
3145 this_klass->set_major_version(major_version);
3146
3147 if (cached_class_file_bytes != NULL) {
3148 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3149 this_klass->set_cached_class_file(cached_class_file_bytes,
3150 cached_class_file_length);
3151 }
3152
3153 // Miranda methods
3154 if ((num_miranda_methods > 0) ||
3155 // if this class introduced new miranda methods or
3156 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3157 // super class exists and this class inherited miranda methods
3230 const char * from = Klass::cast(this_klass())->external_name();
3231 if (this_klass->java_super() != NULL) {
3232 tty->print("RESOLVE %s %s\n", from, instanceKlass::cast(this_klass->java_super())->external_name());
3233 }
3234 // print out each of the interface classes referred to by this class.
3235 objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces());
3236 if (!local_interfaces.is_null()) {
3237 int length = local_interfaces->length();
3238 for (int i = 0; i < length; i++) {
3239 klassOop k = klassOop(local_interfaces->obj_at(i));
3240 instanceKlass* to_class = instanceKlass::cast(k);
3241 const char * to = to_class->external_name();
3242 tty->print("RESOLVE %s %s\n", from, to);
3243 }
3244 }
3245 }
3246
3247 #ifndef PRODUCT
3248 if( PrintCompactFieldsSavings ) {
3249 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3250 tty->print("[Saved %d of %d bytes in %s]\n",
3251 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
3252 orig_nonstatic_field_size*heapOopSize,
3253 this_klass->external_name());
3254 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3255 tty->print("[Wasted %d over %d bytes in %s]\n",
3256 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
3257 orig_nonstatic_field_size*heapOopSize,
3258 this_klass->external_name());
3259 }
3260 }
3261 #endif
3262
3263 // preserve result across HandleMark
3264 preserve_this_klass = this_klass();
3265 }
3266
3267 // Create new handle outside HandleMark
3268 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
3269 debug_only(this_klass->as_klassOop()->verify();)
3270
3271 return this_klass;
3272 }
3273
3274
3275 int ClassFileParser::compute_oop_map_size(instanceKlassHandle super, int nonstatic_oop_map_count, int first_nonstatic_oop_offset) {
3276 int map_size = super.is_null() ? 0 : super->nonstatic_oop_map_size();
3277 if (nonstatic_oop_map_count > 0) {
3278 // We have oops to add to map
3279 if (map_size == 0) {
3280 map_size = nonstatic_oop_map_count;
3281 } else {
3282 // Check whether we should add a new map block or whether the last one can be extended
3283 OopMapBlock* first_map = super->start_of_nonstatic_oop_maps();
3284 OopMapBlock* last_map = first_map + map_size - 1;
3285
3286 int next_offset = last_map->offset() + (last_map->length() * heapOopSize);
3287 if (next_offset == first_nonstatic_oop_offset) {
3288 // There is no gap bettwen superklass's last oop field and first
3289 // local oop field, merge maps.
3290 nonstatic_oop_map_count -= 1;
3291 } else {
3292 // Superklass didn't end with a oop field, add extra maps
3293 assert(next_offset<first_nonstatic_oop_offset, "just checking");
3294 }
3295 map_size += nonstatic_oop_map_count;
3296 }
3297 }
3298 return map_size;
3299 }
3300
3301
3302 void ClassFileParser::fill_oop_maps(instanceKlassHandle k,
3303 int nonstatic_oop_map_count,
3304 u2* nonstatic_oop_offsets, u2* nonstatic_oop_length) {
3305 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
3306 OopMapBlock* last_oop_map = this_oop_map + k->nonstatic_oop_map_size();
3626 THREAD_AND_LOCATION,
3627 vmSymbolHandles::java_lang_ClassFormatError(),
3628 "Illegal class modifiers in class %s: 0x%X",
3629 _class_name->as_C_string(), flags
3630 );
3631 return;
3632 }
3633 }
3634
3635 bool ClassFileParser::has_illegal_visibility(jint flags) {
3636 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3637 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3638 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3639
3640 return ((is_public && is_protected) ||
3641 (is_public && is_private) ||
3642 (is_protected && is_private));
3643 }
3644
3645 bool ClassFileParser::is_supported_version(u2 major, u2 minor) {
3646 u2 max_version = JDK_Version::is_gte_jdk17x_version() ?
3647 JAVA_MAX_SUPPORTED_VERSION : JAVA_6_VERSION;
3648 return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
3649 (major <= max_version) &&
3650 ((major != max_version) ||
3651 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION));
3652 }
3653
3654 void ClassFileParser::verify_legal_field_modifiers(
3655 jint flags, bool is_interface, TRAPS) {
3656 if (!_need_verify) { return; }
3657
3658 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3659 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3660 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3661 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3662 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3663 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
3664 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
3665 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3666 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3667
3668 bool is_illegal = false;
3669
3670 if (is_interface) {
|