Fri, 04 Jun 2010 17:44:51 -0400
Merge
1 /*
2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_classFileParser.cpp.incl"
28 // We generally try to create the oops directly when parsing, rather than
29 // allocating temporary data structures and copying the bytes twice. A
30 // temporary area is only needed when parsing utf8 entries in the constant
31 // pool and when parsing line number tables.
33 // We add assert in debug mode when class format is not checked.
35 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
36 #define JAVA_MIN_SUPPORTED_VERSION 45
37 #define JAVA_MAX_SUPPORTED_VERSION 51
38 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
40 // Used for two backward compatibility reasons:
41 // - to check for new additions to the class file format in JDK1.5
42 // - to check for bug fixes in the format checker in JDK1.5
43 #define JAVA_1_5_VERSION 49
45 // Used for backward compatibility reasons:
46 // - to check for javac bug fixes that happened after 1.5
47 // - also used as the max version when running in jdk6
48 #define JAVA_6_VERSION 50
50 // Used for backward compatibility reasons:
51 // - to check NameAndType_info signatures more aggressively
52 #define JAVA_7_VERSION 51
55 void ClassFileParser::parse_constant_pool_entries(constantPoolHandle cp, int length, TRAPS) {
56 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
57 // this function (_current can be allocated in a register, with scalar
58 // replacement of aggregates). The _current pointer is copied back to
59 // stream() when this function returns. DON'T call another method within
60 // this method that uses stream().
61 ClassFileStream* cfs0 = stream();
62 ClassFileStream cfs1 = *cfs0;
63 ClassFileStream* cfs = &cfs1;
64 #ifdef ASSERT
65 u1* old_current = cfs0->current();
66 #endif
68 // Used for batching symbol allocations.
69 const char* names[SymbolTable::symbol_alloc_batch_size];
70 int lengths[SymbolTable::symbol_alloc_batch_size];
71 int indices[SymbolTable::symbol_alloc_batch_size];
72 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
73 int names_count = 0;
75 // parsing Index 0 is unused
76 for (int index = 1; index < length; index++) {
77 // Each of the following case guarantees one more byte in the stream
78 // for the following tag or the access_flags following constant pool,
79 // so we don't need bounds-check for reading tag.
80 u1 tag = cfs->get_u1_fast();
81 switch (tag) {
82 case JVM_CONSTANT_Class :
83 {
84 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
85 u2 name_index = cfs->get_u2_fast();
86 cp->klass_index_at_put(index, name_index);
87 }
88 break;
89 case JVM_CONSTANT_Fieldref :
90 {
91 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
92 u2 class_index = cfs->get_u2_fast();
93 u2 name_and_type_index = cfs->get_u2_fast();
94 cp->field_at_put(index, class_index, name_and_type_index);
95 }
96 break;
97 case JVM_CONSTANT_Methodref :
98 {
99 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
100 u2 class_index = cfs->get_u2_fast();
101 u2 name_and_type_index = cfs->get_u2_fast();
102 cp->method_at_put(index, class_index, name_and_type_index);
103 }
104 break;
105 case JVM_CONSTANT_InterfaceMethodref :
106 {
107 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
108 u2 class_index = cfs->get_u2_fast();
109 u2 name_and_type_index = cfs->get_u2_fast();
110 cp->interface_method_at_put(index, class_index, name_and_type_index);
111 }
112 break;
113 case JVM_CONSTANT_String :
114 {
115 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags
116 u2 string_index = cfs->get_u2_fast();
117 cp->string_index_at_put(index, string_index);
118 }
119 break;
120 case JVM_CONSTANT_Integer :
121 {
122 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
123 u4 bytes = cfs->get_u4_fast();
124 cp->int_at_put(index, (jint) bytes);
125 }
126 break;
127 case JVM_CONSTANT_Float :
128 {
129 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
130 u4 bytes = cfs->get_u4_fast();
131 cp->float_at_put(index, *(jfloat*)&bytes);
132 }
133 break;
134 case JVM_CONSTANT_Long :
135 // A mangled type might cause you to overrun allocated memory
136 guarantee_property(index+1 < length,
137 "Invalid constant pool entry %u in class file %s",
138 index, CHECK);
139 {
140 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
141 u8 bytes = cfs->get_u8_fast();
142 cp->long_at_put(index, bytes);
143 }
144 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
145 break;
146 case JVM_CONSTANT_Double :
147 // A mangled type might cause you to overrun allocated memory
148 guarantee_property(index+1 < length,
149 "Invalid constant pool entry %u in class file %s",
150 index, CHECK);
151 {
152 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
153 u8 bytes = cfs->get_u8_fast();
154 cp->double_at_put(index, *(jdouble*)&bytes);
155 }
156 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
157 break;
158 case JVM_CONSTANT_NameAndType :
159 {
160 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags
161 u2 name_index = cfs->get_u2_fast();
162 u2 signature_index = cfs->get_u2_fast();
163 cp->name_and_type_at_put(index, name_index, signature_index);
164 }
165 break;
166 case JVM_CONSTANT_Utf8 :
167 {
168 cfs->guarantee_more(2, CHECK); // utf8_length
169 u2 utf8_length = cfs->get_u2_fast();
170 u1* utf8_buffer = cfs->get_u1_buffer();
171 assert(utf8_buffer != NULL, "null utf8 buffer");
172 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.
173 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags
174 cfs->skip_u1_fast(utf8_length);
176 // Before storing the symbol, make sure it's legal
177 if (_need_verify) {
178 verify_legal_utf8((unsigned char*)utf8_buffer, utf8_length, CHECK);
179 }
181 if (AnonymousClasses && has_cp_patch_at(index)) {
182 Handle patch = clear_cp_patch_at(index);
183 guarantee_property(java_lang_String::is_instance(patch()),
184 "Illegal utf8 patch at %d in class file %s",
185 index, CHECK);
186 char* str = java_lang_String::as_utf8_string(patch());
187 // (could use java_lang_String::as_symbol instead, but might as well batch them)
188 utf8_buffer = (u1*) str;
189 utf8_length = (int) strlen(str);
190 }
192 unsigned int hash;
193 symbolOop result = SymbolTable::lookup_only((char*)utf8_buffer, utf8_length, hash);
194 if (result == NULL) {
195 names[names_count] = (char*)utf8_buffer;
196 lengths[names_count] = utf8_length;
197 indices[names_count] = index;
198 hashValues[names_count++] = hash;
199 if (names_count == SymbolTable::symbol_alloc_batch_size) {
200 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK);
201 names_count = 0;
202 }
203 } else {
204 cp->symbol_at_put(index, result);
205 }
206 }
207 break;
208 default:
209 classfile_parse_error(
210 "Unknown constant tag %u in class file %s", tag, CHECK);
211 break;
212 }
213 }
215 // Allocate the remaining symbols
216 if (names_count > 0) {
217 oopFactory::new_symbols(cp, names_count, names, lengths, indices, hashValues, CHECK);
218 }
220 // Copy _current pointer of local copy back to stream().
221 #ifdef ASSERT
222 assert(cfs0->current() == old_current, "non-exclusive use of stream()");
223 #endif
224 cfs0->set_current(cfs1.current());
225 }
227 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
229 constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) {
230 ClassFileStream* cfs = stream();
231 constantPoolHandle nullHandle;
233 cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag
234 u2 length = cfs->get_u2_fast();
235 guarantee_property(
236 length >= 1, "Illegal constant pool size %u in class file %s",
237 length, CHECK_(nullHandle));
238 constantPoolOop constant_pool =
239 oopFactory::new_constantPool(length,
240 methodOopDesc::IsSafeConc,
241 CHECK_(nullHandle));
242 constantPoolHandle cp (THREAD, constant_pool);
244 cp->set_partially_loaded(); // Enables heap verify to work on partial constantPoolOops
246 // parsing constant pool entries
247 parse_constant_pool_entries(cp, length, CHECK_(nullHandle));
249 int index = 1; // declared outside of loops for portability
251 // first verification pass - validate cross references and fixup class and string constants
252 for (index = 1; index < length; index++) { // Index 0 is unused
253 switch (cp->tag_at(index).value()) {
254 case JVM_CONSTANT_Class :
255 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
256 break;
257 case JVM_CONSTANT_Fieldref :
258 // fall through
259 case JVM_CONSTANT_Methodref :
260 // fall through
261 case JVM_CONSTANT_InterfaceMethodref : {
262 if (!_need_verify) break;
263 int klass_ref_index = cp->klass_ref_index_at(index);
264 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
265 check_property(valid_cp_range(klass_ref_index, length) &&
266 is_klass_reference(cp, klass_ref_index),
267 "Invalid constant pool index %u in class file %s",
268 klass_ref_index,
269 CHECK_(nullHandle));
270 check_property(valid_cp_range(name_and_type_ref_index, length) &&
271 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
272 "Invalid constant pool index %u in class file %s",
273 name_and_type_ref_index,
274 CHECK_(nullHandle));
275 break;
276 }
277 case JVM_CONSTANT_String :
278 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
279 break;
280 case JVM_CONSTANT_Integer :
281 break;
282 case JVM_CONSTANT_Float :
283 break;
284 case JVM_CONSTANT_Long :
285 case JVM_CONSTANT_Double :
286 index++;
287 check_property(
288 (index < length && cp->tag_at(index).is_invalid()),
289 "Improper constant pool long/double index %u in class file %s",
290 index, CHECK_(nullHandle));
291 break;
292 case JVM_CONSTANT_NameAndType : {
293 if (!_need_verify) break;
294 int name_ref_index = cp->name_ref_index_at(index);
295 int signature_ref_index = cp->signature_ref_index_at(index);
296 check_property(
297 valid_cp_range(name_ref_index, length) &&
298 cp->tag_at(name_ref_index).is_utf8(),
299 "Invalid constant pool index %u in class file %s",
300 name_ref_index, CHECK_(nullHandle));
301 check_property(
302 valid_cp_range(signature_ref_index, length) &&
303 cp->tag_at(signature_ref_index).is_utf8(),
304 "Invalid constant pool index %u in class file %s",
305 signature_ref_index, CHECK_(nullHandle));
306 break;
307 }
308 case JVM_CONSTANT_Utf8 :
309 break;
310 case JVM_CONSTANT_UnresolvedClass : // fall-through
311 case JVM_CONSTANT_UnresolvedClassInError:
312 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
313 break;
314 case JVM_CONSTANT_ClassIndex :
315 {
316 int class_index = cp->klass_index_at(index);
317 check_property(
318 valid_cp_range(class_index, length) &&
319 cp->tag_at(class_index).is_utf8(),
320 "Invalid constant pool index %u in class file %s",
321 class_index, CHECK_(nullHandle));
322 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
323 }
324 break;
325 case JVM_CONSTANT_UnresolvedString :
326 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
327 break;
328 case JVM_CONSTANT_StringIndex :
329 {
330 int string_index = cp->string_index_at(index);
331 check_property(
332 valid_cp_range(string_index, length) &&
333 cp->tag_at(string_index).is_utf8(),
334 "Invalid constant pool index %u in class file %s",
335 string_index, CHECK_(nullHandle));
336 symbolOop sym = cp->symbol_at(string_index);
337 cp->unresolved_string_at_put(index, sym);
338 }
339 break;
340 default:
341 fatal(err_msg("bad constant pool tag value %u",
342 cp->tag_at(index).value()));
343 ShouldNotReachHere();
344 break;
345 } // end of switch
346 } // end of for
348 if (_cp_patches != NULL) {
349 // need to treat this_class specially...
350 assert(AnonymousClasses, "");
351 int this_class_index;
352 {
353 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
354 u1* mark = cfs->current();
355 u2 flags = cfs->get_u2_fast();
356 this_class_index = cfs->get_u2_fast();
357 cfs->set_current(mark); // revert to mark
358 }
360 for (index = 1; index < length; index++) { // Index 0 is unused
361 if (has_cp_patch_at(index)) {
362 guarantee_property(index != this_class_index,
363 "Illegal constant pool patch to self at %d in class file %s",
364 index, CHECK_(nullHandle));
365 patch_constant_pool(cp, index, cp_patch_at(index), CHECK_(nullHandle));
366 }
367 }
368 // Ensure that all the patches have been used.
369 for (index = 0; index < _cp_patches->length(); index++) {
370 guarantee_property(!has_cp_patch_at(index),
371 "Unused constant pool patch at %d in class file %s",
372 index, CHECK_(nullHandle));
373 }
374 }
376 if (!_need_verify) {
377 return cp;
378 }
380 // second verification pass - checks the strings are of the right format.
381 // but not yet to the other entries
382 for (index = 1; index < length; index++) {
383 jbyte tag = cp->tag_at(index).value();
384 switch (tag) {
385 case JVM_CONSTANT_UnresolvedClass: {
386 symbolHandle class_name(THREAD, cp->unresolved_klass_at(index));
387 // check the name, even if _cp_patches will overwrite it
388 verify_legal_class_name(class_name, CHECK_(nullHandle));
389 break;
390 }
391 case JVM_CONSTANT_NameAndType: {
392 if (_need_verify && _major_version >= JAVA_7_VERSION) {
393 int sig_index = cp->signature_ref_index_at(index);
394 int name_index = cp->name_ref_index_at(index);
395 symbolHandle name(THREAD, cp->symbol_at(name_index));
396 symbolHandle sig(THREAD, cp->symbol_at(sig_index));
397 if (sig->byte_at(0) == JVM_SIGNATURE_FUNC) {
398 verify_legal_method_signature(name, sig, CHECK_(nullHandle));
399 } else {
400 verify_legal_field_signature(name, sig, CHECK_(nullHandle));
401 }
402 }
403 break;
404 }
405 case JVM_CONSTANT_Fieldref:
406 case JVM_CONSTANT_Methodref:
407 case JVM_CONSTANT_InterfaceMethodref: {
408 int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
409 // already verified to be utf8
410 int name_ref_index = cp->name_ref_index_at(name_and_type_ref_index);
411 // already verified to be utf8
412 int signature_ref_index = cp->signature_ref_index_at(name_and_type_ref_index);
413 symbolHandle name(THREAD, cp->symbol_at(name_ref_index));
414 symbolHandle signature(THREAD, cp->symbol_at(signature_ref_index));
415 if (tag == JVM_CONSTANT_Fieldref) {
416 verify_legal_field_name(name, CHECK_(nullHandle));
417 if (_need_verify && _major_version >= JAVA_7_VERSION) {
418 // Signature is verified above, when iterating NameAndType_info.
419 // Need only to be sure it's the right type.
420 if (signature->byte_at(0) == JVM_SIGNATURE_FUNC) {
421 throwIllegalSignature(
422 "Field", name, signature, CHECK_(nullHandle));
423 }
424 } else {
425 verify_legal_field_signature(name, signature, CHECK_(nullHandle));
426 }
427 } else {
428 verify_legal_method_name(name, CHECK_(nullHandle));
429 if (_need_verify && _major_version >= JAVA_7_VERSION) {
430 // Signature is verified above, when iterating NameAndType_info.
431 // Need only to be sure it's the right type.
432 if (signature->byte_at(0) != JVM_SIGNATURE_FUNC) {
433 throwIllegalSignature(
434 "Method", name, signature, CHECK_(nullHandle));
435 }
436 } else {
437 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
438 }
439 if (tag == JVM_CONSTANT_Methodref) {
440 // 4509014: If a class method name begins with '<', it must be "<init>".
441 assert(!name.is_null(), "method name in constant pool is null");
442 unsigned int name_len = name->utf8_length();
443 assert(name_len > 0, "bad method name"); // already verified as legal name
444 if (name->byte_at(0) == '<') {
445 if (name() != vmSymbols::object_initializer_name()) {
446 classfile_parse_error(
447 "Bad method name at constant pool index %u in class file %s",
448 name_ref_index, CHECK_(nullHandle));
449 }
450 }
451 }
452 }
453 break;
454 }
455 } // end of switch
456 } // end of for
458 return cp;
459 }
462 void ClassFileParser::patch_constant_pool(constantPoolHandle cp, int index, Handle patch, TRAPS) {
463 assert(AnonymousClasses, "");
464 BasicType patch_type = T_VOID;
465 switch (cp->tag_at(index).value()) {
467 case JVM_CONSTANT_UnresolvedClass :
468 // Patching a class means pre-resolving it.
469 // The name in the constant pool is ignored.
470 if (patch->klass() == SystemDictionary::Class_klass()) { // %%% java_lang_Class::is_instance
471 guarantee_property(!java_lang_Class::is_primitive(patch()),
472 "Illegal class patch at %d in class file %s",
473 index, CHECK);
474 cp->klass_at_put(index, java_lang_Class::as_klassOop(patch()));
475 } else {
476 guarantee_property(java_lang_String::is_instance(patch()),
477 "Illegal class patch at %d in class file %s",
478 index, CHECK);
479 symbolHandle name = java_lang_String::as_symbol(patch(), CHECK);
480 cp->unresolved_klass_at_put(index, name());
481 }
482 break;
484 case JVM_CONSTANT_UnresolvedString :
485 // Patching a string means pre-resolving it.
486 // The spelling in the constant pool is ignored.
487 // The constant reference may be any object whatever.
488 // If it is not a real interned string, the constant is referred
489 // to as a "pseudo-string", and must be presented to the CP
490 // explicitly, because it may require scavenging.
491 cp->pseudo_string_at_put(index, patch());
492 break;
494 case JVM_CONSTANT_Integer : patch_type = T_INT; goto patch_prim;
495 case JVM_CONSTANT_Float : patch_type = T_FLOAT; goto patch_prim;
496 case JVM_CONSTANT_Long : patch_type = T_LONG; goto patch_prim;
497 case JVM_CONSTANT_Double : patch_type = T_DOUBLE; goto patch_prim;
498 patch_prim:
499 {
500 jvalue value;
501 BasicType value_type = java_lang_boxing_object::get_value(patch(), &value);
502 guarantee_property(value_type == patch_type,
503 "Illegal primitive patch at %d in class file %s",
504 index, CHECK);
505 switch (value_type) {
506 case T_INT: cp->int_at_put(index, value.i); break;
507 case T_FLOAT: cp->float_at_put(index, value.f); break;
508 case T_LONG: cp->long_at_put(index, value.j); break;
509 case T_DOUBLE: cp->double_at_put(index, value.d); break;
510 default: assert(false, "");
511 }
512 }
513 break;
515 default:
516 // %%% TODO: put method handles into CONSTANT_InterfaceMethodref, etc.
517 guarantee_property(!has_cp_patch_at(index),
518 "Illegal unexpected patch at %d in class file %s",
519 index, CHECK);
520 return;
521 }
523 // On fall-through, mark the patch as used.
524 clear_cp_patch_at(index);
525 }
529 class NameSigHash: public ResourceObj {
530 public:
531 symbolOop _name; // name
532 symbolOop _sig; // signature
533 NameSigHash* _next; // Next entry in hash table
534 };
537 #define HASH_ROW_SIZE 256
539 unsigned int hash(symbolOop name, symbolOop sig) {
540 unsigned int raw_hash = 0;
541 raw_hash += ((unsigned int)(uintptr_t)name) >> (LogHeapWordSize + 2);
542 raw_hash += ((unsigned int)(uintptr_t)sig) >> LogHeapWordSize;
544 return (raw_hash + (unsigned int)(uintptr_t)name) % HASH_ROW_SIZE;
545 }
548 void initialize_hashtable(NameSigHash** table) {
549 memset((void*)table, 0, sizeof(NameSigHash*) * HASH_ROW_SIZE);
550 }
552 // Return false if the name/sig combination is found in table.
553 // Return true if no duplicate is found. And name/sig is added as a new entry in table.
554 // The old format checker uses heap sort to find duplicates.
555 // NOTE: caller should guarantee that GC doesn't happen during the life cycle
556 // of table since we don't expect symbolOop's to move.
557 bool put_after_lookup(symbolOop name, symbolOop sig, NameSigHash** table) {
558 assert(name != NULL, "name in constant pool is NULL");
560 // First lookup for duplicates
561 int index = hash(name, sig);
562 NameSigHash* entry = table[index];
563 while (entry != NULL) {
564 if (entry->_name == name && entry->_sig == sig) {
565 return false;
566 }
567 entry = entry->_next;
568 }
570 // No duplicate is found, allocate a new entry and fill it.
571 entry = new NameSigHash();
572 entry->_name = name;
573 entry->_sig = sig;
575 // Insert into hash table
576 entry->_next = table[index];
577 table[index] = entry;
579 return true;
580 }
583 objArrayHandle ClassFileParser::parse_interfaces(constantPoolHandle cp,
584 int length,
585 Handle class_loader,
586 Handle protection_domain,
587 symbolHandle class_name,
588 TRAPS) {
589 ClassFileStream* cfs = stream();
590 assert(length > 0, "only called for length>0");
591 objArrayHandle nullHandle;
592 objArrayOop interface_oop = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
593 objArrayHandle interfaces (THREAD, interface_oop);
595 int index;
596 for (index = 0; index < length; index++) {
597 u2 interface_index = cfs->get_u2(CHECK_(nullHandle));
598 KlassHandle interf;
599 check_property(
600 valid_cp_range(interface_index, cp->length()) &&
601 is_klass_reference(cp, interface_index),
602 "Interface name has bad constant pool index %u in class file %s",
603 interface_index, CHECK_(nullHandle));
604 if (cp->tag_at(interface_index).is_klass()) {
605 interf = KlassHandle(THREAD, cp->resolved_klass_at(interface_index));
606 } else {
607 symbolHandle unresolved_klass (THREAD, cp->klass_name_at(interface_index));
609 // Don't need to check legal name because it's checked when parsing constant pool.
610 // But need to make sure it's not an array type.
611 guarantee_property(unresolved_klass->byte_at(0) != JVM_SIGNATURE_ARRAY,
612 "Bad interface name in class file %s", CHECK_(nullHandle));
614 // Call resolve_super so classcircularity is checked
615 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
616 unresolved_klass, class_loader, protection_domain,
617 false, CHECK_(nullHandle));
618 interf = KlassHandle(THREAD, k);
620 if (LinkWellKnownClasses) // my super type is well known to me
621 cp->klass_at_put(interface_index, interf()); // eagerly resolve
622 }
624 if (!Klass::cast(interf())->is_interface()) {
625 THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(), "Implementing class", nullHandle);
626 }
627 interfaces->obj_at_put(index, interf());
628 }
630 if (!_need_verify || length <= 1) {
631 return interfaces;
632 }
634 // Check if there's any duplicates in interfaces
635 ResourceMark rm(THREAD);
636 NameSigHash** interface_names = NEW_RESOURCE_ARRAY_IN_THREAD(
637 THREAD, NameSigHash*, HASH_ROW_SIZE);
638 initialize_hashtable(interface_names);
639 bool dup = false;
640 {
641 debug_only(No_Safepoint_Verifier nsv;)
642 for (index = 0; index < length; index++) {
643 klassOop k = (klassOop)interfaces->obj_at(index);
644 symbolOop name = instanceKlass::cast(k)->name();
645 // If no duplicates, add (name, NULL) in hashtable interface_names.
646 if (!put_after_lookup(name, NULL, interface_names)) {
647 dup = true;
648 break;
649 }
650 }
651 }
652 if (dup) {
653 classfile_parse_error("Duplicate interface name in class file %s",
654 CHECK_(nullHandle));
655 }
657 return interfaces;
658 }
661 void ClassFileParser::verify_constantvalue(int constantvalue_index, int signature_index, constantPoolHandle cp, TRAPS) {
662 // Make sure the constant pool entry is of a type appropriate to this field
663 guarantee_property(
664 (constantvalue_index > 0 &&
665 constantvalue_index < cp->length()),
666 "Bad initial value index %u in ConstantValue attribute in class file %s",
667 constantvalue_index, CHECK);
668 constantTag value_type = cp->tag_at(constantvalue_index);
669 switch ( cp->basic_type_for_signature_at(signature_index) ) {
670 case T_LONG:
671 guarantee_property(value_type.is_long(), "Inconsistent constant value type in class file %s", CHECK);
672 break;
673 case T_FLOAT:
674 guarantee_property(value_type.is_float(), "Inconsistent constant value type in class file %s", CHECK);
675 break;
676 case T_DOUBLE:
677 guarantee_property(value_type.is_double(), "Inconsistent constant value type in class file %s", CHECK);
678 break;
679 case T_BYTE: case T_CHAR: case T_SHORT: case T_BOOLEAN: case T_INT:
680 guarantee_property(value_type.is_int(), "Inconsistent constant value type in class file %s", CHECK);
681 break;
682 case T_OBJECT:
683 guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
684 && (value_type.is_string() || value_type.is_unresolved_string())),
685 "Bad string initial value in class file %s", CHECK);
686 break;
687 default:
688 classfile_parse_error(
689 "Unable to set initial value %u in class file %s",
690 constantvalue_index, CHECK);
691 }
692 }
695 // Parse attributes for a field.
696 void ClassFileParser::parse_field_attributes(constantPoolHandle cp,
697 u2 attributes_count,
698 bool is_static, u2 signature_index,
699 u2* constantvalue_index_addr,
700 bool* is_synthetic_addr,
701 u2* generic_signature_index_addr,
702 typeArrayHandle* field_annotations,
703 TRAPS) {
704 ClassFileStream* cfs = stream();
705 assert(attributes_count > 0, "length should be greater than 0");
706 u2 constantvalue_index = 0;
707 u2 generic_signature_index = 0;
708 bool is_synthetic = false;
709 u1* runtime_visible_annotations = NULL;
710 int runtime_visible_annotations_length = 0;
711 u1* runtime_invisible_annotations = NULL;
712 int runtime_invisible_annotations_length = 0;
713 while (attributes_count--) {
714 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
715 u2 attribute_name_index = cfs->get_u2_fast();
716 u4 attribute_length = cfs->get_u4_fast();
717 check_property(valid_cp_range(attribute_name_index, cp->length()) &&
718 cp->tag_at(attribute_name_index).is_utf8(),
719 "Invalid field attribute index %u in class file %s",
720 attribute_name_index,
721 CHECK);
722 symbolOop attribute_name = cp->symbol_at(attribute_name_index);
723 if (is_static && attribute_name == vmSymbols::tag_constant_value()) {
724 // ignore if non-static
725 if (constantvalue_index != 0) {
726 classfile_parse_error("Duplicate ConstantValue attribute in class file %s", CHECK);
727 }
728 check_property(
729 attribute_length == 2,
730 "Invalid ConstantValue field attribute length %u in class file %s",
731 attribute_length, CHECK);
732 constantvalue_index = cfs->get_u2(CHECK);
733 if (_need_verify) {
734 verify_constantvalue(constantvalue_index, signature_index, cp, CHECK);
735 }
736 } else if (attribute_name == vmSymbols::tag_synthetic()) {
737 if (attribute_length != 0) {
738 classfile_parse_error(
739 "Invalid Synthetic field attribute length %u in class file %s",
740 attribute_length, CHECK);
741 }
742 is_synthetic = true;
743 } else if (attribute_name == vmSymbols::tag_deprecated()) { // 4276120
744 if (attribute_length != 0) {
745 classfile_parse_error(
746 "Invalid Deprecated field attribute length %u in class file %s",
747 attribute_length, CHECK);
748 }
749 } else if (_major_version >= JAVA_1_5_VERSION) {
750 if (attribute_name == vmSymbols::tag_signature()) {
751 if (attribute_length != 2) {
752 classfile_parse_error(
753 "Wrong size %u for field's Signature attribute in class file %s",
754 attribute_length, CHECK);
755 }
756 generic_signature_index = cfs->get_u2(CHECK);
757 } else if (attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
758 runtime_visible_annotations_length = attribute_length;
759 runtime_visible_annotations = cfs->get_u1_buffer();
760 assert(runtime_visible_annotations != NULL, "null visible annotations");
761 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
762 } else if (PreserveAllAnnotations && attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
763 runtime_invisible_annotations_length = attribute_length;
764 runtime_invisible_annotations = cfs->get_u1_buffer();
765 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
766 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
767 } else {
768 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
769 }
770 } else {
771 cfs->skip_u1(attribute_length, CHECK); // Skip unknown attributes
772 }
773 }
775 *constantvalue_index_addr = constantvalue_index;
776 *is_synthetic_addr = is_synthetic;
777 *generic_signature_index_addr = generic_signature_index;
778 *field_annotations = assemble_annotations(runtime_visible_annotations,
779 runtime_visible_annotations_length,
780 runtime_invisible_annotations,
781 runtime_invisible_annotations_length,
782 CHECK);
783 return;
784 }
787 // Field allocation types. Used for computing field offsets.
789 enum FieldAllocationType {
790 STATIC_OOP, // Oops
791 STATIC_BYTE, // Boolean, Byte, char
792 STATIC_SHORT, // shorts
793 STATIC_WORD, // ints
794 STATIC_DOUBLE, // long or double
795 STATIC_ALIGNED_DOUBLE,// aligned long or double
796 NONSTATIC_OOP,
797 NONSTATIC_BYTE,
798 NONSTATIC_SHORT,
799 NONSTATIC_WORD,
800 NONSTATIC_DOUBLE,
801 NONSTATIC_ALIGNED_DOUBLE
802 };
805 struct FieldAllocationCount {
806 unsigned int static_oop_count;
807 unsigned int static_byte_count;
808 unsigned int static_short_count;
809 unsigned int static_word_count;
810 unsigned int static_double_count;
811 unsigned int nonstatic_oop_count;
812 unsigned int nonstatic_byte_count;
813 unsigned int nonstatic_short_count;
814 unsigned int nonstatic_word_count;
815 unsigned int nonstatic_double_count;
816 };
818 typeArrayHandle ClassFileParser::parse_fields(constantPoolHandle cp, bool is_interface,
819 struct FieldAllocationCount *fac,
820 objArrayHandle* fields_annotations, TRAPS) {
821 ClassFileStream* cfs = stream();
822 typeArrayHandle nullHandle;
823 cfs->guarantee_more(2, CHECK_(nullHandle)); // length
824 u2 length = cfs->get_u2_fast();
825 // Tuples of shorts [access, name index, sig index, initial value index, byte offset, generic signature index]
826 typeArrayOop new_fields = oopFactory::new_permanent_shortArray(length*instanceKlass::next_offset, CHECK_(nullHandle));
827 typeArrayHandle fields(THREAD, new_fields);
829 int index = 0;
830 typeArrayHandle field_annotations;
831 for (int n = 0; n < length; n++) {
832 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
834 AccessFlags access_flags;
835 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
836 verify_legal_field_modifiers(flags, is_interface, CHECK_(nullHandle));
837 access_flags.set_flags(flags);
839 u2 name_index = cfs->get_u2_fast();
840 int cp_size = cp->length();
841 check_property(
842 valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(),
843 "Invalid constant pool index %u for field name in class file %s",
844 name_index, CHECK_(nullHandle));
845 symbolHandle name(THREAD, cp->symbol_at(name_index));
846 verify_legal_field_name(name, CHECK_(nullHandle));
848 u2 signature_index = cfs->get_u2_fast();
849 check_property(
850 valid_cp_range(signature_index, cp_size) &&
851 cp->tag_at(signature_index).is_utf8(),
852 "Invalid constant pool index %u for field signature in class file %s",
853 signature_index, CHECK_(nullHandle));
854 symbolHandle sig(THREAD, cp->symbol_at(signature_index));
855 verify_legal_field_signature(name, sig, CHECK_(nullHandle));
857 u2 constantvalue_index = 0;
858 bool is_synthetic = false;
859 u2 generic_signature_index = 0;
860 bool is_static = access_flags.is_static();
862 u2 attributes_count = cfs->get_u2_fast();
863 if (attributes_count > 0) {
864 parse_field_attributes(cp, attributes_count, is_static, signature_index,
865 &constantvalue_index, &is_synthetic,
866 &generic_signature_index, &field_annotations,
867 CHECK_(nullHandle));
868 if (field_annotations.not_null()) {
869 if (fields_annotations->is_null()) {
870 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
871 *fields_annotations = objArrayHandle(THREAD, md);
872 }
873 (*fields_annotations)->obj_at_put(n, field_annotations());
874 }
875 if (is_synthetic) {
876 access_flags.set_is_synthetic();
877 }
878 }
880 fields->short_at_put(index++, access_flags.as_short());
881 fields->short_at_put(index++, name_index);
882 fields->short_at_put(index++, signature_index);
883 fields->short_at_put(index++, constantvalue_index);
885 // Remember how many oops we encountered and compute allocation type
886 BasicType type = cp->basic_type_for_signature_at(signature_index);
887 FieldAllocationType atype;
888 if ( is_static ) {
889 switch ( type ) {
890 case T_BOOLEAN:
891 case T_BYTE:
892 fac->static_byte_count++;
893 atype = STATIC_BYTE;
894 break;
895 case T_LONG:
896 case T_DOUBLE:
897 if (Universe::field_type_should_be_aligned(type)) {
898 atype = STATIC_ALIGNED_DOUBLE;
899 } else {
900 atype = STATIC_DOUBLE;
901 }
902 fac->static_double_count++;
903 break;
904 case T_CHAR:
905 case T_SHORT:
906 fac->static_short_count++;
907 atype = STATIC_SHORT;
908 break;
909 case T_FLOAT:
910 case T_INT:
911 fac->static_word_count++;
912 atype = STATIC_WORD;
913 break;
914 case T_ARRAY:
915 case T_OBJECT:
916 fac->static_oop_count++;
917 atype = STATIC_OOP;
918 break;
919 case T_ADDRESS:
920 case T_VOID:
921 default:
922 assert(0, "bad field type");
923 }
924 } else {
925 switch ( type ) {
926 case T_BOOLEAN:
927 case T_BYTE:
928 fac->nonstatic_byte_count++;
929 atype = NONSTATIC_BYTE;
930 break;
931 case T_LONG:
932 case T_DOUBLE:
933 if (Universe::field_type_should_be_aligned(type)) {
934 atype = NONSTATIC_ALIGNED_DOUBLE;
935 } else {
936 atype = NONSTATIC_DOUBLE;
937 }
938 fac->nonstatic_double_count++;
939 break;
940 case T_CHAR:
941 case T_SHORT:
942 fac->nonstatic_short_count++;
943 atype = NONSTATIC_SHORT;
944 break;
945 case T_FLOAT:
946 case T_INT:
947 fac->nonstatic_word_count++;
948 atype = NONSTATIC_WORD;
949 break;
950 case T_ARRAY:
951 case T_OBJECT:
952 fac->nonstatic_oop_count++;
953 atype = NONSTATIC_OOP;
954 break;
955 case T_ADDRESS:
956 case T_VOID:
957 default:
958 assert(0, "bad field type");
959 }
960 }
962 // The correct offset is computed later (all oop fields will be located together)
963 // We temporarily store the allocation type in the offset field
964 fields->short_at_put(index++, atype);
965 fields->short_at_put(index++, 0); // Clear out high word of byte offset
966 fields->short_at_put(index++, generic_signature_index);
967 }
969 if (_need_verify && length > 1) {
970 // Check duplicated fields
971 ResourceMark rm(THREAD);
972 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
973 THREAD, NameSigHash*, HASH_ROW_SIZE);
974 initialize_hashtable(names_and_sigs);
975 bool dup = false;
976 {
977 debug_only(No_Safepoint_Verifier nsv;)
978 for (int i = 0; i < length*instanceKlass::next_offset; i += instanceKlass::next_offset) {
979 int name_index = fields->ushort_at(i + instanceKlass::name_index_offset);
980 symbolOop name = cp->symbol_at(name_index);
981 int sig_index = fields->ushort_at(i + instanceKlass::signature_index_offset);
982 symbolOop sig = cp->symbol_at(sig_index);
983 // If no duplicates, add name/signature in hashtable names_and_sigs.
984 if (!put_after_lookup(name, sig, names_and_sigs)) {
985 dup = true;
986 break;
987 }
988 }
989 }
990 if (dup) {
991 classfile_parse_error("Duplicate field name&signature in class file %s",
992 CHECK_(nullHandle));
993 }
994 }
996 return fields;
997 }
1000 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1001 while (length-- > 0) {
1002 *dest++ = Bytes::get_Java_u2((u1*) (src++));
1003 }
1004 }
1007 typeArrayHandle ClassFileParser::parse_exception_table(u4 code_length,
1008 u4 exception_table_length,
1009 constantPoolHandle cp,
1010 TRAPS) {
1011 ClassFileStream* cfs = stream();
1012 typeArrayHandle nullHandle;
1014 // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index]
1015 typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle));
1016 typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh);
1018 int index = 0;
1019 cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index
1020 for (unsigned int i = 0; i < exception_table_length; i++) {
1021 u2 start_pc = cfs->get_u2_fast();
1022 u2 end_pc = cfs->get_u2_fast();
1023 u2 handler_pc = cfs->get_u2_fast();
1024 u2 catch_type_index = cfs->get_u2_fast();
1025 // Will check legal target after parsing code array in verifier.
1026 if (_need_verify) {
1027 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
1028 "Illegal exception table range in class file %s", CHECK_(nullHandle));
1029 guarantee_property(handler_pc < code_length,
1030 "Illegal exception table handler in class file %s", CHECK_(nullHandle));
1031 if (catch_type_index != 0) {
1032 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
1033 is_klass_reference(cp, catch_type_index),
1034 "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle));
1035 }
1036 }
1037 exception_handlers->int_at_put(index++, start_pc);
1038 exception_handlers->int_at_put(index++, end_pc);
1039 exception_handlers->int_at_put(index++, handler_pc);
1040 exception_handlers->int_at_put(index++, catch_type_index);
1041 }
1042 return exception_handlers;
1043 }
1045 void ClassFileParser::parse_linenumber_table(
1046 u4 code_attribute_length, u4 code_length,
1047 CompressedLineNumberWriteStream** write_stream, TRAPS) {
1048 ClassFileStream* cfs = stream();
1049 unsigned int num_entries = cfs->get_u2(CHECK);
1051 // Each entry is a u2 start_pc, and a u2 line_number
1052 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
1054 // Verify line number attribute and table length
1055 check_property(
1056 code_attribute_length == sizeof(u2) + length_in_bytes,
1057 "LineNumberTable attribute has wrong length in class file %s", CHECK);
1059 cfs->guarantee_more(length_in_bytes, CHECK);
1061 if ((*write_stream) == NULL) {
1062 if (length_in_bytes > fixed_buffer_size) {
1063 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes);
1064 } else {
1065 (*write_stream) = new CompressedLineNumberWriteStream(
1066 linenumbertable_buffer, fixed_buffer_size);
1067 }
1068 }
1070 while (num_entries-- > 0) {
1071 u2 bci = cfs->get_u2_fast(); // start_pc
1072 u2 line = cfs->get_u2_fast(); // line_number
1073 guarantee_property(bci < code_length,
1074 "Invalid pc in LineNumberTable in class file %s", CHECK);
1075 (*write_stream)->write_pair(bci, line);
1076 }
1077 }
1080 // Class file LocalVariableTable elements.
1081 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC {
1082 public:
1083 u2 start_bci;
1084 u2 length;
1085 u2 name_cp_index;
1086 u2 descriptor_cp_index;
1087 u2 slot;
1088 };
1091 class LVT_Hash: public CHeapObj {
1092 public:
1093 LocalVariableTableElement *_elem; // element
1094 LVT_Hash* _next; // Next entry in hash table
1095 };
1097 unsigned int hash(LocalVariableTableElement *elem) {
1098 unsigned int raw_hash = elem->start_bci;
1100 raw_hash = elem->length + raw_hash * 37;
1101 raw_hash = elem->name_cp_index + raw_hash * 37;
1102 raw_hash = elem->slot + raw_hash * 37;
1104 return raw_hash % HASH_ROW_SIZE;
1105 }
1107 void initialize_hashtable(LVT_Hash** table) {
1108 for (int i = 0; i < HASH_ROW_SIZE; i++) {
1109 table[i] = NULL;
1110 }
1111 }
1113 void clear_hashtable(LVT_Hash** table) {
1114 for (int i = 0; i < HASH_ROW_SIZE; i++) {
1115 LVT_Hash* current = table[i];
1116 LVT_Hash* next;
1117 while (current != NULL) {
1118 next = current->_next;
1119 current->_next = NULL;
1120 delete(current);
1121 current = next;
1122 }
1123 table[i] = NULL;
1124 }
1125 }
1127 LVT_Hash* LVT_lookup(LocalVariableTableElement *elem, int index, LVT_Hash** table) {
1128 LVT_Hash* entry = table[index];
1130 /*
1131 * 3-tuple start_bci/length/slot has to be unique key,
1132 * so the following comparison seems to be redundant:
1133 * && elem->name_cp_index == entry->_elem->name_cp_index
1134 */
1135 while (entry != NULL) {
1136 if (elem->start_bci == entry->_elem->start_bci
1137 && elem->length == entry->_elem->length
1138 && elem->name_cp_index == entry->_elem->name_cp_index
1139 && elem->slot == entry->_elem->slot
1140 ) {
1141 return entry;
1142 }
1143 entry = entry->_next;
1144 }
1145 return NULL;
1146 }
1148 // Return false if the local variable is found in table.
1149 // Return true if no duplicate is found.
1150 // And local variable is added as a new entry in table.
1151 bool LVT_put_after_lookup(LocalVariableTableElement *elem, LVT_Hash** table) {
1152 // First lookup for duplicates
1153 int index = hash(elem);
1154 LVT_Hash* entry = LVT_lookup(elem, index, table);
1156 if (entry != NULL) {
1157 return false;
1158 }
1159 // No duplicate is found, allocate a new entry and fill it.
1160 if ((entry = new LVT_Hash()) == NULL) {
1161 return false;
1162 }
1163 entry->_elem = elem;
1165 // Insert into hash table
1166 entry->_next = table[index];
1167 table[index] = entry;
1169 return true;
1170 }
1172 void copy_lvt_element(Classfile_LVT_Element *src, LocalVariableTableElement *lvt) {
1173 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci);
1174 lvt->length = Bytes::get_Java_u2((u1*) &src->length);
1175 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index);
1176 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index);
1177 lvt->signature_cp_index = 0;
1178 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot);
1179 }
1181 // Function is used to parse both attributes:
1182 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)
1183 u2* ClassFileParser::parse_localvariable_table(u4 code_length,
1184 u2 max_locals,
1185 u4 code_attribute_length,
1186 constantPoolHandle cp,
1187 u2* localvariable_table_length,
1188 bool isLVTT,
1189 TRAPS) {
1190 ClassFileStream* cfs = stream();
1191 const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";
1192 *localvariable_table_length = cfs->get_u2(CHECK_NULL);
1193 unsigned int size = (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2);
1194 // Verify local variable table attribute has right length
1195 if (_need_verify) {
1196 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)),
1197 "%s has wrong length in class file %s", tbl_name, CHECK_NULL);
1198 }
1199 u2* localvariable_table_start = cfs->get_u2_buffer();
1200 assert(localvariable_table_start != NULL, "null local variable table");
1201 if (!_need_verify) {
1202 cfs->skip_u2_fast(size);
1203 } else {
1204 cfs->guarantee_more(size * 2, CHECK_NULL);
1205 for(int i = 0; i < (*localvariable_table_length); i++) {
1206 u2 start_pc = cfs->get_u2_fast();
1207 u2 length = cfs->get_u2_fast();
1208 u2 name_index = cfs->get_u2_fast();
1209 u2 descriptor_index = cfs->get_u2_fast();
1210 u2 index = cfs->get_u2_fast();
1211 // Assign to a u4 to avoid overflow
1212 u4 end_pc = (u4)start_pc + (u4)length;
1214 if (start_pc >= code_length) {
1215 classfile_parse_error(
1216 "Invalid start_pc %u in %s in class file %s",
1217 start_pc, tbl_name, CHECK_NULL);
1218 }
1219 if (end_pc > code_length) {
1220 classfile_parse_error(
1221 "Invalid length %u in %s in class file %s",
1222 length, tbl_name, CHECK_NULL);
1223 }
1224 int cp_size = cp->length();
1225 guarantee_property(
1226 valid_cp_range(name_index, cp_size) &&
1227 cp->tag_at(name_index).is_utf8(),
1228 "Name index %u in %s has bad constant type in class file %s",
1229 name_index, tbl_name, CHECK_NULL);
1230 guarantee_property(
1231 valid_cp_range(descriptor_index, cp_size) &&
1232 cp->tag_at(descriptor_index).is_utf8(),
1233 "Signature index %u in %s has bad constant type in class file %s",
1234 descriptor_index, tbl_name, CHECK_NULL);
1236 symbolHandle name(THREAD, cp->symbol_at(name_index));
1237 symbolHandle sig(THREAD, cp->symbol_at(descriptor_index));
1238 verify_legal_field_name(name, CHECK_NULL);
1239 u2 extra_slot = 0;
1240 if (!isLVTT) {
1241 verify_legal_field_signature(name, sig, CHECK_NULL);
1243 // 4894874: check special cases for double and long local variables
1244 if (sig() == vmSymbols::type_signature(T_DOUBLE) ||
1245 sig() == vmSymbols::type_signature(T_LONG)) {
1246 extra_slot = 1;
1247 }
1248 }
1249 guarantee_property((index + extra_slot) < max_locals,
1250 "Invalid index %u in %s in class file %s",
1251 index, tbl_name, CHECK_NULL);
1252 }
1253 }
1254 return localvariable_table_start;
1255 }
1258 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1259 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS) {
1260 ClassFileStream* cfs = stream();
1261 u2 index = 0; // index in the array with long/double occupying two slots
1262 u4 i1 = *u1_index;
1263 u4 i2 = *u2_index + 1;
1264 for(int i = 0; i < array_length; i++) {
1265 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1266 index++;
1267 if (tag == ITEM_Long || tag == ITEM_Double) {
1268 index++;
1269 } else if (tag == ITEM_Object) {
1270 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK);
1271 guarantee_property(valid_cp_range(class_index, cp->length()) &&
1272 is_klass_reference(cp, class_index),
1273 "Bad class index %u in StackMap in class file %s",
1274 class_index, CHECK);
1275 } else if (tag == ITEM_Uninitialized) {
1276 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK);
1277 guarantee_property(
1278 offset < code_length,
1279 "Bad uninitialized type offset %u in StackMap in class file %s",
1280 offset, CHECK);
1281 } else {
1282 guarantee_property(
1283 tag <= (u1)ITEM_Uninitialized,
1284 "Unknown variable type %u in StackMap in class file %s",
1285 tag, CHECK);
1286 }
1287 }
1288 u2_array[*u2_index] = index;
1289 *u1_index = i1;
1290 *u2_index = i2;
1291 }
1293 typeArrayOop ClassFileParser::parse_stackmap_table(
1294 u4 code_attribute_length, TRAPS) {
1295 if (code_attribute_length == 0)
1296 return NULL;
1298 ClassFileStream* cfs = stream();
1299 u1* stackmap_table_start = cfs->get_u1_buffer();
1300 assert(stackmap_table_start != NULL, "null stackmap table");
1302 // check code_attribute_length first
1303 stream()->skip_u1(code_attribute_length, CHECK_NULL);
1305 if (!_need_verify && !DumpSharedSpaces) {
1306 return NULL;
1307 }
1309 typeArrayOop stackmap_data =
1310 oopFactory::new_permanent_byteArray(code_attribute_length, CHECK_NULL);
1312 stackmap_data->set_length(code_attribute_length);
1313 memcpy((void*)stackmap_data->byte_at_addr(0),
1314 (void*)stackmap_table_start, code_attribute_length);
1315 return stackmap_data;
1316 }
1318 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length,
1319 u4 method_attribute_length,
1320 constantPoolHandle cp, TRAPS) {
1321 ClassFileStream* cfs = stream();
1322 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length
1323 *checked_exceptions_length = cfs->get_u2_fast();
1324 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);
1325 u2* checked_exceptions_start = cfs->get_u2_buffer();
1326 assert(checked_exceptions_start != NULL, "null checked exceptions");
1327 if (!_need_verify) {
1328 cfs->skip_u2_fast(size);
1329 } else {
1330 // Verify each value in the checked exception table
1331 u2 checked_exception;
1332 u2 len = *checked_exceptions_length;
1333 cfs->guarantee_more(2 * len, CHECK_NULL);
1334 for (int i = 0; i < len; i++) {
1335 checked_exception = cfs->get_u2_fast();
1336 check_property(
1337 valid_cp_range(checked_exception, cp->length()) &&
1338 is_klass_reference(cp, checked_exception),
1339 "Exception name has bad type at constant pool %u in class file %s",
1340 checked_exception, CHECK_NULL);
1341 }
1342 }
1343 // check exceptions attribute length
1344 if (_need_verify) {
1345 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1346 sizeof(u2) * size),
1347 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1348 }
1349 return checked_exceptions_start;
1350 }
1352 void ClassFileParser::throwIllegalSignature(
1353 const char* type, symbolHandle name, symbolHandle sig, TRAPS) {
1354 ResourceMark rm(THREAD);
1355 Exceptions::fthrow(THREAD_AND_LOCATION,
1356 vmSymbols::java_lang_ClassFormatError(),
1357 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1358 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1359 }
1361 #define MAX_ARGS_SIZE 255
1362 #define MAX_CODE_SIZE 65535
1363 #define INITIAL_MAX_LVT_NUMBER 256
1365 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
1366 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the
1367 // methodOop to save footprint, so we only know the size of the resulting methodOop when the
1368 // entire method attribute is parsed.
1369 //
1370 // The promoted_flags parameter is used to pass relevant access_flags
1371 // from the method back up to the containing klass. These flag values
1372 // are added to klass's access_flags.
1374 methodHandle ClassFileParser::parse_method(constantPoolHandle cp, bool is_interface,
1375 AccessFlags *promoted_flags,
1376 typeArrayHandle* method_annotations,
1377 typeArrayHandle* method_parameter_annotations,
1378 typeArrayHandle* method_default_annotations,
1379 TRAPS) {
1380 ClassFileStream* cfs = stream();
1381 methodHandle nullHandle;
1382 ResourceMark rm(THREAD);
1383 // Parse fixed parts
1384 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
1386 int flags = cfs->get_u2_fast();
1387 u2 name_index = cfs->get_u2_fast();
1388 int cp_size = cp->length();
1389 check_property(
1390 valid_cp_range(name_index, cp_size) &&
1391 cp->tag_at(name_index).is_utf8(),
1392 "Illegal constant pool index %u for method name in class file %s",
1393 name_index, CHECK_(nullHandle));
1394 symbolHandle name(THREAD, cp->symbol_at(name_index));
1395 verify_legal_method_name(name, CHECK_(nullHandle));
1397 u2 signature_index = cfs->get_u2_fast();
1398 guarantee_property(
1399 valid_cp_range(signature_index, cp_size) &&
1400 cp->tag_at(signature_index).is_utf8(),
1401 "Illegal constant pool index %u for method signature in class file %s",
1402 signature_index, CHECK_(nullHandle));
1403 symbolHandle signature(THREAD, cp->symbol_at(signature_index));
1405 AccessFlags access_flags;
1406 if (name == vmSymbols::class_initializer_name()) {
1407 // We ignore the access flags for a class initializer. (JVM Spec. p. 116)
1408 flags = JVM_ACC_STATIC;
1409 } else {
1410 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1411 }
1413 int args_size = -1; // only used when _need_verify is true
1414 if (_need_verify) {
1415 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1416 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1417 if (args_size > MAX_ARGS_SIZE) {
1418 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1419 }
1420 }
1422 access_flags.set_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
1424 // Default values for code and exceptions attribute elements
1425 u2 max_stack = 0;
1426 u2 max_locals = 0;
1427 u4 code_length = 0;
1428 u1* code_start = 0;
1429 u2 exception_table_length = 0;
1430 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1431 u2 checked_exceptions_length = 0;
1432 u2* checked_exceptions_start = NULL;
1433 CompressedLineNumberWriteStream* linenumber_table = NULL;
1434 int linenumber_table_length = 0;
1435 int total_lvt_length = 0;
1436 u2 lvt_cnt = 0;
1437 u2 lvtt_cnt = 0;
1438 bool lvt_allocated = false;
1439 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER;
1440 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER;
1441 u2* localvariable_table_length;
1442 u2** localvariable_table_start;
1443 u2* localvariable_type_table_length;
1444 u2** localvariable_type_table_start;
1445 bool parsed_code_attribute = false;
1446 bool parsed_checked_exceptions_attribute = false;
1447 bool parsed_stackmap_attribute = false;
1448 // stackmap attribute - JDK1.5
1449 typeArrayHandle stackmap_data;
1450 u2 generic_signature_index = 0;
1451 u1* runtime_visible_annotations = NULL;
1452 int runtime_visible_annotations_length = 0;
1453 u1* runtime_invisible_annotations = NULL;
1454 int runtime_invisible_annotations_length = 0;
1455 u1* runtime_visible_parameter_annotations = NULL;
1456 int runtime_visible_parameter_annotations_length = 0;
1457 u1* runtime_invisible_parameter_annotations = NULL;
1458 int runtime_invisible_parameter_annotations_length = 0;
1459 u1* annotation_default = NULL;
1460 int annotation_default_length = 0;
1462 // Parse code and exceptions attribute
1463 u2 method_attributes_count = cfs->get_u2_fast();
1464 while (method_attributes_count--) {
1465 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length
1466 u2 method_attribute_name_index = cfs->get_u2_fast();
1467 u4 method_attribute_length = cfs->get_u4_fast();
1468 check_property(
1469 valid_cp_range(method_attribute_name_index, cp_size) &&
1470 cp->tag_at(method_attribute_name_index).is_utf8(),
1471 "Invalid method attribute name index %u in class file %s",
1472 method_attribute_name_index, CHECK_(nullHandle));
1474 symbolOop method_attribute_name = cp->symbol_at(method_attribute_name_index);
1475 if (method_attribute_name == vmSymbols::tag_code()) {
1476 // Parse Code attribute
1477 if (_need_verify) {
1478 guarantee_property(!access_flags.is_native() && !access_flags.is_abstract(),
1479 "Code attribute in native or abstract methods in class file %s",
1480 CHECK_(nullHandle));
1481 }
1482 if (parsed_code_attribute) {
1483 classfile_parse_error("Multiple Code attributes in class file %s", CHECK_(nullHandle));
1484 }
1485 parsed_code_attribute = true;
1487 // Stack size, locals size, and code size
1488 if (_major_version == 45 && _minor_version <= 2) {
1489 cfs->guarantee_more(4, CHECK_(nullHandle));
1490 max_stack = cfs->get_u1_fast();
1491 max_locals = cfs->get_u1_fast();
1492 code_length = cfs->get_u2_fast();
1493 } else {
1494 cfs->guarantee_more(8, CHECK_(nullHandle));
1495 max_stack = cfs->get_u2_fast();
1496 max_locals = cfs->get_u2_fast();
1497 code_length = cfs->get_u4_fast();
1498 }
1499 if (_need_verify) {
1500 guarantee_property(args_size <= max_locals,
1501 "Arguments can't fit into locals in class file %s", CHECK_(nullHandle));
1502 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE,
1503 "Invalid method Code length %u in class file %s",
1504 code_length, CHECK_(nullHandle));
1505 }
1506 // Code pointer
1507 code_start = cfs->get_u1_buffer();
1508 assert(code_start != NULL, "null code start");
1509 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1510 cfs->skip_u1_fast(code_length);
1512 // Exception handler table
1513 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1514 exception_table_length = cfs->get_u2_fast();
1515 if (exception_table_length > 0) {
1516 exception_handlers =
1517 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1518 }
1520 // Parse additional attributes in code attribute
1521 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1522 u2 code_attributes_count = cfs->get_u2_fast();
1524 unsigned int calculated_attribute_length = 0;
1526 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) {
1527 calculated_attribute_length =
1528 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
1529 } else {
1530 // max_stack, locals and length are smaller in pre-version 45.2 classes
1531 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2);
1532 }
1533 calculated_attribute_length +=
1534 code_length +
1535 sizeof(exception_table_length) +
1536 sizeof(code_attributes_count) +
1537 exception_table_length *
1538 ( sizeof(u2) + // start_pc
1539 sizeof(u2) + // end_pc
1540 sizeof(u2) + // handler_pc
1541 sizeof(u2) ); // catch_type_index
1543 while (code_attributes_count--) {
1544 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
1545 u2 code_attribute_name_index = cfs->get_u2_fast();
1546 u4 code_attribute_length = cfs->get_u4_fast();
1547 calculated_attribute_length += code_attribute_length +
1548 sizeof(code_attribute_name_index) +
1549 sizeof(code_attribute_length);
1550 check_property(valid_cp_range(code_attribute_name_index, cp_size) &&
1551 cp->tag_at(code_attribute_name_index).is_utf8(),
1552 "Invalid code attribute name index %u in class file %s",
1553 code_attribute_name_index,
1554 CHECK_(nullHandle));
1555 if (LoadLineNumberTables &&
1556 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
1557 // Parse and compress line number table
1558 parse_linenumber_table(code_attribute_length, code_length,
1559 &linenumber_table, CHECK_(nullHandle));
1561 } else if (LoadLocalVariableTables &&
1562 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) {
1563 // Parse local variable table
1564 if (!lvt_allocated) {
1565 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1566 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1567 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1568 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1569 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1570 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1571 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1572 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1573 lvt_allocated = true;
1574 }
1575 if (lvt_cnt == max_lvt_cnt) {
1576 max_lvt_cnt <<= 1;
1577 REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt);
1578 REALLOC_RESOURCE_ARRAY(u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt);
1579 }
1580 localvariable_table_start[lvt_cnt] =
1581 parse_localvariable_table(code_length,
1582 max_locals,
1583 code_attribute_length,
1584 cp,
1585 &localvariable_table_length[lvt_cnt],
1586 false, // is not LVTT
1587 CHECK_(nullHandle));
1588 total_lvt_length += localvariable_table_length[lvt_cnt];
1589 lvt_cnt++;
1590 } else if (LoadLocalVariableTypeTables &&
1591 _major_version >= JAVA_1_5_VERSION &&
1592 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) {
1593 if (!lvt_allocated) {
1594 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1595 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1596 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1597 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1598 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1599 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1600 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1601 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1602 lvt_allocated = true;
1603 }
1604 // Parse local variable type table
1605 if (lvtt_cnt == max_lvtt_cnt) {
1606 max_lvtt_cnt <<= 1;
1607 REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt);
1608 REALLOC_RESOURCE_ARRAY(u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt);
1609 }
1610 localvariable_type_table_start[lvtt_cnt] =
1611 parse_localvariable_table(code_length,
1612 max_locals,
1613 code_attribute_length,
1614 cp,
1615 &localvariable_type_table_length[lvtt_cnt],
1616 true, // is LVTT
1617 CHECK_(nullHandle));
1618 lvtt_cnt++;
1619 } else if (UseSplitVerifier &&
1620 _major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&
1621 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) {
1622 // Stack map is only needed by the new verifier in JDK1.5.
1623 if (parsed_stackmap_attribute) {
1624 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle));
1625 }
1626 typeArrayOop sm =
1627 parse_stackmap_table(code_attribute_length, CHECK_(nullHandle));
1628 stackmap_data = typeArrayHandle(THREAD, sm);
1629 parsed_stackmap_attribute = true;
1630 } else {
1631 // Skip unknown attributes
1632 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle));
1633 }
1634 }
1635 // check method attribute length
1636 if (_need_verify) {
1637 guarantee_property(method_attribute_length == calculated_attribute_length,
1638 "Code segment has wrong length in class file %s", CHECK_(nullHandle));
1639 }
1640 } else if (method_attribute_name == vmSymbols::tag_exceptions()) {
1641 // Parse Exceptions attribute
1642 if (parsed_checked_exceptions_attribute) {
1643 classfile_parse_error("Multiple Exceptions attributes in class file %s", CHECK_(nullHandle));
1644 }
1645 parsed_checked_exceptions_attribute = true;
1646 checked_exceptions_start =
1647 parse_checked_exceptions(&checked_exceptions_length,
1648 method_attribute_length,
1649 cp, CHECK_(nullHandle));
1650 } else if (method_attribute_name == vmSymbols::tag_synthetic()) {
1651 if (method_attribute_length != 0) {
1652 classfile_parse_error(
1653 "Invalid Synthetic method attribute length %u in class file %s",
1654 method_attribute_length, CHECK_(nullHandle));
1655 }
1656 // Should we check that there hasn't already been a synthetic attribute?
1657 access_flags.set_is_synthetic();
1658 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120
1659 if (method_attribute_length != 0) {
1660 classfile_parse_error(
1661 "Invalid Deprecated method attribute length %u in class file %s",
1662 method_attribute_length, CHECK_(nullHandle));
1663 }
1664 } else if (_major_version >= JAVA_1_5_VERSION) {
1665 if (method_attribute_name == vmSymbols::tag_signature()) {
1666 if (method_attribute_length != 2) {
1667 classfile_parse_error(
1668 "Invalid Signature attribute length %u in class file %s",
1669 method_attribute_length, CHECK_(nullHandle));
1670 }
1671 cfs->guarantee_more(2, CHECK_(nullHandle)); // generic_signature_index
1672 generic_signature_index = cfs->get_u2_fast();
1673 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1674 runtime_visible_annotations_length = method_attribute_length;
1675 runtime_visible_annotations = cfs->get_u1_buffer();
1676 assert(runtime_visible_annotations != NULL, "null visible annotations");
1677 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
1678 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1679 runtime_invisible_annotations_length = method_attribute_length;
1680 runtime_invisible_annotations = cfs->get_u1_buffer();
1681 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1682 cfs->skip_u1(runtime_invisible_annotations_length, CHECK_(nullHandle));
1683 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
1684 runtime_visible_parameter_annotations_length = method_attribute_length;
1685 runtime_visible_parameter_annotations = cfs->get_u1_buffer();
1686 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations");
1687 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_(nullHandle));
1688 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) {
1689 runtime_invisible_parameter_annotations_length = method_attribute_length;
1690 runtime_invisible_parameter_annotations = cfs->get_u1_buffer();
1691 assert(runtime_invisible_parameter_annotations != NULL, "null invisible parameter annotations");
1692 cfs->skip_u1(runtime_invisible_parameter_annotations_length, CHECK_(nullHandle));
1693 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) {
1694 annotation_default_length = method_attribute_length;
1695 annotation_default = cfs->get_u1_buffer();
1696 assert(annotation_default != NULL, "null annotation default");
1697 cfs->skip_u1(annotation_default_length, CHECK_(nullHandle));
1698 } else {
1699 // Skip unknown attributes
1700 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1701 }
1702 } else {
1703 // Skip unknown attributes
1704 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1705 }
1706 }
1708 if (linenumber_table != NULL) {
1709 linenumber_table->write_terminator();
1710 linenumber_table_length = linenumber_table->position();
1711 }
1713 // Make sure there's at least one Code attribute in non-native/non-abstract method
1714 if (_need_verify) {
1715 guarantee_property(access_flags.is_native() || access_flags.is_abstract() || parsed_code_attribute,
1716 "Absent Code attribute in method that is not native or abstract in class file %s", CHECK_(nullHandle));
1717 }
1719 // All sizing information for a methodOop is finally available, now create it
1720 methodOop m_oop = oopFactory::new_method(
1721 code_length, access_flags, linenumber_table_length,
1722 total_lvt_length, checked_exceptions_length,
1723 methodOopDesc::IsSafeConc, CHECK_(nullHandle));
1724 methodHandle m (THREAD, m_oop);
1726 ClassLoadingService::add_class_method_size(m_oop->size()*HeapWordSize);
1728 // Fill in information from fixed part (access_flags already set)
1729 m->set_constants(cp());
1730 m->set_name_index(name_index);
1731 m->set_signature_index(signature_index);
1732 m->set_generic_signature_index(generic_signature_index);
1733 #ifdef CC_INTERP
1734 // hmm is there a gc issue here??
1735 ResultTypeFinder rtf(cp->symbol_at(signature_index));
1736 m->set_result_index(rtf.type());
1737 #endif
1739 if (args_size >= 0) {
1740 m->set_size_of_parameters(args_size);
1741 } else {
1742 m->compute_size_of_parameters(THREAD);
1743 }
1744 #ifdef ASSERT
1745 if (args_size >= 0) {
1746 m->compute_size_of_parameters(THREAD);
1747 assert(args_size == m->size_of_parameters(), "");
1748 }
1749 #endif
1751 // Fill in code attribute information
1752 m->set_max_stack(max_stack);
1753 m->set_max_locals(max_locals);
1754 m->constMethod()->set_stackmap_data(stackmap_data());
1756 /**
1757 * The exception_table field is the flag used to indicate
1758 * that the methodOop and it's associated constMethodOop are partially
1759 * initialized and thus are exempt from pre/post GC verification. Once
1760 * the field is set, the oops are considered fully initialized so make
1761 * sure that the oops can pass verification when this field is set.
1762 */
1763 m->set_exception_table(exception_handlers());
1765 // Copy byte codes
1766 m->set_code(code_start);
1768 // Copy line number table
1769 if (linenumber_table != NULL) {
1770 memcpy(m->compressed_linenumber_table(),
1771 linenumber_table->buffer(), linenumber_table_length);
1772 }
1774 // Copy checked exceptions
1775 if (checked_exceptions_length > 0) {
1776 int size = checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2);
1777 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), checked_exceptions_start, size);
1778 }
1780 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT.
1781 *
1782 * Rules for LVT's and LVTT's are:
1783 * - There can be any number of LVT's and LVTT's.
1784 * - If there are n LVT's, it is the same as if there was just
1785 * one LVT containing all the entries from the n LVT's.
1786 * - There may be no more than one LVT entry per local variable.
1787 * Two LVT entries are 'equal' if these fields are the same:
1788 * start_pc, length, name, slot
1789 * - There may be no more than one LVTT entry per each LVT entry.
1790 * Each LVTT entry has to match some LVT entry.
1791 * - HotSpot internal LVT keeps natural ordering of class file LVT entries.
1792 */
1793 if (total_lvt_length > 0) {
1794 int tbl_no, idx;
1796 promoted_flags->set_has_localvariable_table();
1798 LVT_Hash** lvt_Hash = NEW_RESOURCE_ARRAY(LVT_Hash*, HASH_ROW_SIZE);
1799 initialize_hashtable(lvt_Hash);
1801 // To fill LocalVariableTable in
1802 Classfile_LVT_Element* cf_lvt;
1803 LocalVariableTableElement* lvt = m->localvariable_table_start();
1805 for (tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) {
1806 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no];
1807 for (idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) {
1808 copy_lvt_element(&cf_lvt[idx], lvt);
1809 // If no duplicates, add LVT elem in hashtable lvt_Hash.
1810 if (LVT_put_after_lookup(lvt, lvt_Hash) == false
1811 && _need_verify
1812 && _major_version >= JAVA_1_5_VERSION ) {
1813 clear_hashtable(lvt_Hash);
1814 classfile_parse_error("Duplicated LocalVariableTable attribute "
1815 "entry for '%s' in class file %s",
1816 cp->symbol_at(lvt->name_cp_index)->as_utf8(),
1817 CHECK_(nullHandle));
1818 }
1819 }
1820 }
1822 // To merge LocalVariableTable and LocalVariableTypeTable
1823 Classfile_LVT_Element* cf_lvtt;
1824 LocalVariableTableElement lvtt_elem;
1826 for (tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) {
1827 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no];
1828 for (idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) {
1829 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem);
1830 int index = hash(&lvtt_elem);
1831 LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash);
1832 if (entry == NULL) {
1833 if (_need_verify) {
1834 clear_hashtable(lvt_Hash);
1835 classfile_parse_error("LVTT entry for '%s' in class file %s "
1836 "does not match any LVT entry",
1837 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1838 CHECK_(nullHandle));
1839 }
1840 } else if (entry->_elem->signature_cp_index != 0 && _need_verify) {
1841 clear_hashtable(lvt_Hash);
1842 classfile_parse_error("Duplicated LocalVariableTypeTable attribute "
1843 "entry for '%s' in class file %s",
1844 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
1845 CHECK_(nullHandle));
1846 } else {
1847 // to add generic signatures into LocalVariableTable
1848 entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index;
1849 }
1850 }
1851 }
1852 clear_hashtable(lvt_Hash);
1853 }
1855 *method_annotations = assemble_annotations(runtime_visible_annotations,
1856 runtime_visible_annotations_length,
1857 runtime_invisible_annotations,
1858 runtime_invisible_annotations_length,
1859 CHECK_(nullHandle));
1860 *method_parameter_annotations = assemble_annotations(runtime_visible_parameter_annotations,
1861 runtime_visible_parameter_annotations_length,
1862 runtime_invisible_parameter_annotations,
1863 runtime_invisible_parameter_annotations_length,
1864 CHECK_(nullHandle));
1865 *method_default_annotations = assemble_annotations(annotation_default,
1866 annotation_default_length,
1867 NULL,
1868 0,
1869 CHECK_(nullHandle));
1871 if (name() == vmSymbols::finalize_method_name() &&
1872 signature() == vmSymbols::void_method_signature()) {
1873 if (m->is_empty_method()) {
1874 _has_empty_finalizer = true;
1875 } else {
1876 _has_finalizer = true;
1877 }
1878 }
1879 if (name() == vmSymbols::object_initializer_name() &&
1880 signature() == vmSymbols::void_method_signature() &&
1881 m->is_vanilla_constructor()) {
1882 _has_vanilla_constructor = true;
1883 }
1885 if (EnableMethodHandles && (m->is_method_handle_invoke() ||
1886 m->is_method_handle_adapter())) {
1887 THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(),
1888 "Method handle invokers must be defined internally to the VM", nullHandle);
1889 }
1891 return m;
1892 }
1895 // The promoted_flags parameter is used to pass relevant access_flags
1896 // from the methods back up to the containing klass. These flag values
1897 // are added to klass's access_flags.
1899 objArrayHandle ClassFileParser::parse_methods(constantPoolHandle cp, bool is_interface,
1900 AccessFlags* promoted_flags,
1901 bool* has_final_method,
1902 objArrayOop* methods_annotations_oop,
1903 objArrayOop* methods_parameter_annotations_oop,
1904 objArrayOop* methods_default_annotations_oop,
1905 TRAPS) {
1906 ClassFileStream* cfs = stream();
1907 objArrayHandle nullHandle;
1908 typeArrayHandle method_annotations;
1909 typeArrayHandle method_parameter_annotations;
1910 typeArrayHandle method_default_annotations;
1911 cfs->guarantee_more(2, CHECK_(nullHandle)); // length
1912 u2 length = cfs->get_u2_fast();
1913 if (length == 0) {
1914 return objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
1915 } else {
1916 objArrayOop m = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1917 objArrayHandle methods(THREAD, m);
1918 HandleMark hm(THREAD);
1919 objArrayHandle methods_annotations;
1920 objArrayHandle methods_parameter_annotations;
1921 objArrayHandle methods_default_annotations;
1922 for (int index = 0; index < length; index++) {
1923 methodHandle method = parse_method(cp, is_interface,
1924 promoted_flags,
1925 &method_annotations,
1926 &method_parameter_annotations,
1927 &method_default_annotations,
1928 CHECK_(nullHandle));
1929 if (method->is_final()) {
1930 *has_final_method = true;
1931 }
1932 methods->obj_at_put(index, method());
1933 if (method_annotations.not_null()) {
1934 if (methods_annotations.is_null()) {
1935 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1936 methods_annotations = objArrayHandle(THREAD, md);
1937 }
1938 methods_annotations->obj_at_put(index, method_annotations());
1939 }
1940 if (method_parameter_annotations.not_null()) {
1941 if (methods_parameter_annotations.is_null()) {
1942 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1943 methods_parameter_annotations = objArrayHandle(THREAD, md);
1944 }
1945 methods_parameter_annotations->obj_at_put(index, method_parameter_annotations());
1946 }
1947 if (method_default_annotations.not_null()) {
1948 if (methods_default_annotations.is_null()) {
1949 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1950 methods_default_annotations = objArrayHandle(THREAD, md);
1951 }
1952 methods_default_annotations->obj_at_put(index, method_default_annotations());
1953 }
1954 }
1955 if (_need_verify && length > 1) {
1956 // Check duplicated methods
1957 ResourceMark rm(THREAD);
1958 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1959 THREAD, NameSigHash*, HASH_ROW_SIZE);
1960 initialize_hashtable(names_and_sigs);
1961 bool dup = false;
1962 {
1963 debug_only(No_Safepoint_Verifier nsv;)
1964 for (int i = 0; i < length; i++) {
1965 methodOop m = (methodOop)methods->obj_at(i);
1966 // If no duplicates, add name/signature in hashtable names_and_sigs.
1967 if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) {
1968 dup = true;
1969 break;
1970 }
1971 }
1972 }
1973 if (dup) {
1974 classfile_parse_error("Duplicate method name&signature in class file %s",
1975 CHECK_(nullHandle));
1976 }
1977 }
1979 *methods_annotations_oop = methods_annotations();
1980 *methods_parameter_annotations_oop = methods_parameter_annotations();
1981 *methods_default_annotations_oop = methods_default_annotations();
1983 return methods;
1984 }
1985 }
1988 typeArrayHandle ClassFileParser::sort_methods(objArrayHandle methods,
1989 objArrayHandle methods_annotations,
1990 objArrayHandle methods_parameter_annotations,
1991 objArrayHandle methods_default_annotations,
1992 TRAPS) {
1993 typeArrayHandle nullHandle;
1994 int length = methods()->length();
1995 // If JVMTI original method ordering is enabled we have to
1996 // remember the original class file ordering.
1997 // We temporarily use the vtable_index field in the methodOop to store the
1998 // class file index, so we can read in after calling qsort.
1999 if (JvmtiExport::can_maintain_original_method_order()) {
2000 for (int index = 0; index < length; index++) {
2001 methodOop m = methodOop(methods->obj_at(index));
2002 assert(!m->valid_vtable_index(), "vtable index should not be set");
2003 m->set_vtable_index(index);
2004 }
2005 }
2006 // Sort method array by ascending method name (for faster lookups & vtable construction)
2007 // Note that the ordering is not alphabetical, see symbolOopDesc::fast_compare
2008 methodOopDesc::sort_methods(methods(),
2009 methods_annotations(),
2010 methods_parameter_annotations(),
2011 methods_default_annotations());
2013 // If JVMTI original method ordering is enabled construct int array remembering the original ordering
2014 if (JvmtiExport::can_maintain_original_method_order()) {
2015 typeArrayOop new_ordering = oopFactory::new_permanent_intArray(length, CHECK_(nullHandle));
2016 typeArrayHandle method_ordering(THREAD, new_ordering);
2017 for (int index = 0; index < length; index++) {
2018 methodOop m = methodOop(methods->obj_at(index));
2019 int old_index = m->vtable_index();
2020 assert(old_index >= 0 && old_index < length, "invalid method index");
2021 method_ordering->int_at_put(index, old_index);
2022 m->set_vtable_index(methodOopDesc::invalid_vtable_index);
2023 }
2024 return method_ordering;
2025 } else {
2026 return typeArrayHandle(THREAD, Universe::the_empty_int_array());
2027 }
2028 }
2031 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2032 ClassFileStream* cfs = stream();
2033 cfs->guarantee_more(2, CHECK); // sourcefile_index
2034 u2 sourcefile_index = cfs->get_u2_fast();
2035 check_property(
2036 valid_cp_range(sourcefile_index, cp->length()) &&
2037 cp->tag_at(sourcefile_index).is_utf8(),
2038 "Invalid SourceFile attribute at constant pool index %u in class file %s",
2039 sourcefile_index, CHECK);
2040 k->set_source_file_name(cp->symbol_at(sourcefile_index));
2041 }
2045 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
2046 instanceKlassHandle k,
2047 int length, TRAPS) {
2048 ClassFileStream* cfs = stream();
2049 u1* sde_buffer = cfs->get_u1_buffer();
2050 assert(sde_buffer != NULL, "null sde buffer");
2052 // Don't bother storing it if there is no way to retrieve it
2053 if (JvmtiExport::can_get_source_debug_extension()) {
2054 // Optimistically assume that only 1 byte UTF format is used
2055 // (common case)
2056 symbolOop sde_symbol = oopFactory::new_symbol((char*)sde_buffer,
2057 length, CHECK);
2058 k->set_source_debug_extension(sde_symbol);
2059 }
2060 // Got utf8 string, set stream position forward
2061 cfs->skip_u1(length, CHECK);
2062 }
2065 // Inner classes can be static, private or protected (classic VM does this)
2066 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC)
2068 // Return number of classes in the inner classes attribute table
2069 u2 ClassFileParser::parse_classfile_inner_classes_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2070 ClassFileStream* cfs = stream();
2071 cfs->guarantee_more(2, CHECK_0); // length
2072 u2 length = cfs->get_u2_fast();
2074 // 4-tuples of shorts [inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags]
2075 typeArrayOop ic = oopFactory::new_permanent_shortArray(length*4, CHECK_0);
2076 typeArrayHandle inner_classes(THREAD, ic);
2077 int index = 0;
2078 int cp_size = cp->length();
2079 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
2080 for (int n = 0; n < length; n++) {
2081 // Inner class index
2082 u2 inner_class_info_index = cfs->get_u2_fast();
2083 check_property(
2084 inner_class_info_index == 0 ||
2085 (valid_cp_range(inner_class_info_index, cp_size) &&
2086 is_klass_reference(cp, inner_class_info_index)),
2087 "inner_class_info_index %u has bad constant type in class file %s",
2088 inner_class_info_index, CHECK_0);
2089 // Outer class index
2090 u2 outer_class_info_index = cfs->get_u2_fast();
2091 check_property(
2092 outer_class_info_index == 0 ||
2093 (valid_cp_range(outer_class_info_index, cp_size) &&
2094 is_klass_reference(cp, outer_class_info_index)),
2095 "outer_class_info_index %u has bad constant type in class file %s",
2096 outer_class_info_index, CHECK_0);
2097 // Inner class name
2098 u2 inner_name_index = cfs->get_u2_fast();
2099 check_property(
2100 inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) &&
2101 cp->tag_at(inner_name_index).is_utf8()),
2102 "inner_name_index %u has bad constant type in class file %s",
2103 inner_name_index, CHECK_0);
2104 if (_need_verify) {
2105 guarantee_property(inner_class_info_index != outer_class_info_index,
2106 "Class is both outer and inner class in class file %s", CHECK_0);
2107 }
2108 // Access flags
2109 AccessFlags inner_access_flags;
2110 jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
2111 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2112 // Set abstract bit for old class files for backward compatibility
2113 flags |= JVM_ACC_ABSTRACT;
2114 }
2115 verify_legal_class_modifiers(flags, CHECK_0);
2116 inner_access_flags.set_flags(flags);
2118 inner_classes->short_at_put(index++, inner_class_info_index);
2119 inner_classes->short_at_put(index++, outer_class_info_index);
2120 inner_classes->short_at_put(index++, inner_name_index);
2121 inner_classes->short_at_put(index++, inner_access_flags.as_short());
2122 }
2124 // 4347400: make sure there's no duplicate entry in the classes array
2125 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
2126 for(int i = 0; i < inner_classes->length(); i += 4) {
2127 for(int j = i + 4; j < inner_classes->length(); j += 4) {
2128 guarantee_property((inner_classes->ushort_at(i) != inner_classes->ushort_at(j) ||
2129 inner_classes->ushort_at(i+1) != inner_classes->ushort_at(j+1) ||
2130 inner_classes->ushort_at(i+2) != inner_classes->ushort_at(j+2) ||
2131 inner_classes->ushort_at(i+3) != inner_classes->ushort_at(j+3)),
2132 "Duplicate entry in InnerClasses in class file %s",
2133 CHECK_0);
2134 }
2135 }
2136 }
2138 // Update instanceKlass with inner class info.
2139 k->set_inner_classes(inner_classes());
2140 return length;
2141 }
2143 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2144 k->set_is_synthetic();
2145 }
2147 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2148 ClassFileStream* cfs = stream();
2149 u2 signature_index = cfs->get_u2(CHECK);
2150 check_property(
2151 valid_cp_range(signature_index, cp->length()) &&
2152 cp->tag_at(signature_index).is_utf8(),
2153 "Invalid constant pool index %u in Signature attribute in class file %s",
2154 signature_index, CHECK);
2155 k->set_generic_signature(cp->symbol_at(signature_index));
2156 }
2158 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2159 ClassFileStream* cfs = stream();
2160 // Set inner classes attribute to default sentinel
2161 k->set_inner_classes(Universe::the_empty_short_array());
2162 cfs->guarantee_more(2, CHECK); // attributes_count
2163 u2 attributes_count = cfs->get_u2_fast();
2164 bool parsed_sourcefile_attribute = false;
2165 bool parsed_innerclasses_attribute = false;
2166 bool parsed_enclosingmethod_attribute = false;
2167 u1* runtime_visible_annotations = NULL;
2168 int runtime_visible_annotations_length = 0;
2169 u1* runtime_invisible_annotations = NULL;
2170 int runtime_invisible_annotations_length = 0;
2171 // Iterate over attributes
2172 while (attributes_count--) {
2173 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2174 u2 attribute_name_index = cfs->get_u2_fast();
2175 u4 attribute_length = cfs->get_u4_fast();
2176 check_property(
2177 valid_cp_range(attribute_name_index, cp->length()) &&
2178 cp->tag_at(attribute_name_index).is_utf8(),
2179 "Attribute name has bad constant pool index %u in class file %s",
2180 attribute_name_index, CHECK);
2181 symbolOop tag = cp->symbol_at(attribute_name_index);
2182 if (tag == vmSymbols::tag_source_file()) {
2183 // Check for SourceFile tag
2184 if (_need_verify) {
2185 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2186 }
2187 if (parsed_sourcefile_attribute) {
2188 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2189 } else {
2190 parsed_sourcefile_attribute = true;
2191 }
2192 parse_classfile_sourcefile_attribute(cp, k, CHECK);
2193 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2194 // Check for SourceDebugExtension tag
2195 parse_classfile_source_debug_extension_attribute(cp, k, (int)attribute_length, CHECK);
2196 } else if (tag == vmSymbols::tag_inner_classes()) {
2197 // Check for InnerClasses tag
2198 if (parsed_innerclasses_attribute) {
2199 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2200 } else {
2201 parsed_innerclasses_attribute = true;
2202 }
2203 u2 num_of_classes = parse_classfile_inner_classes_attribute(cp, k, CHECK);
2204 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
2205 guarantee_property(attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
2206 "Wrong InnerClasses attribute length in class file %s", CHECK);
2207 }
2208 } else if (tag == vmSymbols::tag_synthetic()) {
2209 // Check for Synthetic tag
2210 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec
2211 if (attribute_length != 0) {
2212 classfile_parse_error(
2213 "Invalid Synthetic classfile attribute length %u in class file %s",
2214 attribute_length, CHECK);
2215 }
2216 parse_classfile_synthetic_attribute(cp, k, CHECK);
2217 } else if (tag == vmSymbols::tag_deprecated()) {
2218 // Check for Deprecatd tag - 4276120
2219 if (attribute_length != 0) {
2220 classfile_parse_error(
2221 "Invalid Deprecated classfile attribute length %u in class file %s",
2222 attribute_length, CHECK);
2223 }
2224 } else if (_major_version >= JAVA_1_5_VERSION) {
2225 if (tag == vmSymbols::tag_signature()) {
2226 if (attribute_length != 2) {
2227 classfile_parse_error(
2228 "Wrong Signature attribute length %u in class file %s",
2229 attribute_length, CHECK);
2230 }
2231 parse_classfile_signature_attribute(cp, k, CHECK);
2232 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2233 runtime_visible_annotations_length = attribute_length;
2234 runtime_visible_annotations = cfs->get_u1_buffer();
2235 assert(runtime_visible_annotations != NULL, "null visible annotations");
2236 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2237 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2238 runtime_invisible_annotations_length = attribute_length;
2239 runtime_invisible_annotations = cfs->get_u1_buffer();
2240 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2241 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
2242 } else if (tag == vmSymbols::tag_enclosing_method()) {
2243 if (parsed_enclosingmethod_attribute) {
2244 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2245 } else {
2246 parsed_enclosingmethod_attribute = true;
2247 }
2248 cfs->guarantee_more(4, CHECK); // class_index, method_index
2249 u2 class_index = cfs->get_u2_fast();
2250 u2 method_index = cfs->get_u2_fast();
2251 if (class_index == 0) {
2252 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2253 }
2254 // Validate the constant pool indices and types
2255 if (!cp->is_within_bounds(class_index) ||
2256 !is_klass_reference(cp, class_index)) {
2257 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2258 }
2259 if (method_index != 0 &&
2260 (!cp->is_within_bounds(method_index) ||
2261 !cp->tag_at(method_index).is_name_and_type())) {
2262 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2263 }
2264 k->set_enclosing_method_indices(class_index, method_index);
2265 } else {
2266 // Unknown attribute
2267 cfs->skip_u1(attribute_length, CHECK);
2268 }
2269 } else {
2270 // Unknown attribute
2271 cfs->skip_u1(attribute_length, CHECK);
2272 }
2273 }
2274 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2275 runtime_visible_annotations_length,
2276 runtime_invisible_annotations,
2277 runtime_invisible_annotations_length,
2278 CHECK);
2279 k->set_class_annotations(annotations());
2280 }
2283 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
2284 int runtime_visible_annotations_length,
2285 u1* runtime_invisible_annotations,
2286 int runtime_invisible_annotations_length, TRAPS) {
2287 typeArrayHandle annotations;
2288 if (runtime_visible_annotations != NULL ||
2289 runtime_invisible_annotations != NULL) {
2290 typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2291 runtime_invisible_annotations_length, CHECK_(annotations));
2292 annotations = typeArrayHandle(THREAD, anno);
2293 if (runtime_visible_annotations != NULL) {
2294 memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2295 }
2296 if (runtime_invisible_annotations != NULL) {
2297 memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2298 }
2299 }
2300 return annotations;
2301 }
2304 static void initialize_static_field(fieldDescriptor* fd, TRAPS) {
2305 KlassHandle h_k (THREAD, fd->field_holder());
2306 assert(h_k.not_null() && fd->is_static(), "just checking");
2307 if (fd->has_initial_value()) {
2308 BasicType t = fd->field_type();
2309 switch (t) {
2310 case T_BYTE:
2311 h_k()->byte_field_put(fd->offset(), fd->int_initial_value());
2312 break;
2313 case T_BOOLEAN:
2314 h_k()->bool_field_put(fd->offset(), fd->int_initial_value());
2315 break;
2316 case T_CHAR:
2317 h_k()->char_field_put(fd->offset(), fd->int_initial_value());
2318 break;
2319 case T_SHORT:
2320 h_k()->short_field_put(fd->offset(), fd->int_initial_value());
2321 break;
2322 case T_INT:
2323 h_k()->int_field_put(fd->offset(), fd->int_initial_value());
2324 break;
2325 case T_FLOAT:
2326 h_k()->float_field_put(fd->offset(), fd->float_initial_value());
2327 break;
2328 case T_DOUBLE:
2329 h_k()->double_field_put(fd->offset(), fd->double_initial_value());
2330 break;
2331 case T_LONG:
2332 h_k()->long_field_put(fd->offset(), fd->long_initial_value());
2333 break;
2334 case T_OBJECT:
2335 {
2336 #ifdef ASSERT
2337 symbolOop sym = oopFactory::new_symbol("Ljava/lang/String;", CHECK);
2338 assert(fd->signature() == sym, "just checking");
2339 #endif
2340 oop string = fd->string_initial_value(CHECK);
2341 h_k()->obj_field_put(fd->offset(), string);
2342 }
2343 break;
2344 default:
2345 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
2346 "Illegal ConstantValue attribute in class file");
2347 }
2348 }
2349 }
2352 void ClassFileParser::java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
2353 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS) {
2354 // This code is for compatibility with earlier jdk's that do not
2355 // have the "discovered" field in java.lang.ref.Reference. For 1.5
2356 // the check for the "discovered" field should issue a warning if
2357 // the field is not found. For 1.6 this code should be issue a
2358 // fatal error if the "discovered" field is not found.
2359 //
2360 // Increment fac.nonstatic_oop_count so that the start of the
2361 // next type of non-static oops leaves room for the fake oop.
2362 // Do not increment next_nonstatic_oop_offset so that the
2363 // fake oop is place after the java.lang.ref.Reference oop
2364 // fields.
2365 //
2366 // Check the fields in java.lang.ref.Reference for the "discovered"
2367 // field. If it is not present, artifically create a field for it.
2368 // This allows this VM to run on early JDK where the field is not
2369 // present.
2371 //
2372 // Increment fac.nonstatic_oop_count so that the start of the
2373 // next type of non-static oops leaves room for the fake oop.
2374 // Do not increment next_nonstatic_oop_offset so that the
2375 // fake oop is place after the java.lang.ref.Reference oop
2376 // fields.
2377 //
2378 // Check the fields in java.lang.ref.Reference for the "discovered"
2379 // field. If it is not present, artifically create a field for it.
2380 // This allows this VM to run on early JDK where the field is not
2381 // present.
2382 int reference_sig_index = 0;
2383 int reference_name_index = 0;
2384 int reference_index = 0;
2385 int extra = java_lang_ref_Reference::number_of_fake_oop_fields;
2386 const int n = (*fields_ptr)()->length();
2387 for (int i = 0; i < n; i += instanceKlass::next_offset ) {
2388 int name_index =
2389 (*fields_ptr)()->ushort_at(i + instanceKlass::name_index_offset);
2390 int sig_index =
2391 (*fields_ptr)()->ushort_at(i + instanceKlass::signature_index_offset);
2392 symbolOop f_name = cp->symbol_at(name_index);
2393 symbolOop f_sig = cp->symbol_at(sig_index);
2394 if (f_sig == vmSymbols::reference_signature() && reference_index == 0) {
2395 // Save the index for reference signature for later use.
2396 // The fake discovered field does not entries in the
2397 // constant pool so the index for its signature cannot
2398 // be extracted from the constant pool. It will need
2399 // later, however. It's signature is vmSymbols::reference_signature()
2400 // so same an index for that signature.
2401 reference_sig_index = sig_index;
2402 reference_name_index = name_index;
2403 reference_index = i;
2404 }
2405 if (f_name == vmSymbols::reference_discovered_name() &&
2406 f_sig == vmSymbols::reference_signature()) {
2407 // The values below are fake but will force extra
2408 // non-static oop fields and a corresponding non-static
2409 // oop map block to be allocated.
2410 extra = 0;
2411 break;
2412 }
2413 }
2414 if (extra != 0) {
2415 fac_ptr->nonstatic_oop_count += extra;
2416 // Add the additional entry to "fields" so that the klass
2417 // contains the "discoverd" field and the field will be initialized
2418 // in instances of the object.
2419 int fields_with_fix_length = (*fields_ptr)()->length() +
2420 instanceKlass::next_offset;
2421 typeArrayOop ff = oopFactory::new_permanent_shortArray(
2422 fields_with_fix_length, CHECK);
2423 typeArrayHandle fields_with_fix(THREAD, ff);
2425 // Take everything from the original but the length.
2426 for (int idx = 0; idx < (*fields_ptr)->length(); idx++) {
2427 fields_with_fix->ushort_at_put(idx, (*fields_ptr)->ushort_at(idx));
2428 }
2430 // Add the fake field at the end.
2431 int i = (*fields_ptr)->length();
2432 // There is no name index for the fake "discovered" field nor
2433 // signature but a signature is needed so that the field will
2434 // be properly initialized. Use one found for
2435 // one of the other reference fields. Be sure the index for the
2436 // name is 0. In fieldDescriptor::initialize() the index of the
2437 // name is checked. That check is by passed for the last nonstatic
2438 // oop field in a java.lang.ref.Reference which is assumed to be
2439 // this artificial "discovered" field. An assertion checks that
2440 // the name index is 0.
2441 assert(reference_index != 0, "Missing signature for reference");
2443 int j;
2444 for (j = 0; j < instanceKlass::next_offset; j++) {
2445 fields_with_fix->ushort_at_put(i + j,
2446 (*fields_ptr)->ushort_at(reference_index +j));
2447 }
2448 // Clear the public access flag and set the private access flag.
2449 short flags;
2450 flags =
2451 fields_with_fix->ushort_at(i + instanceKlass::access_flags_offset);
2452 assert(!(flags & JVM_RECOGNIZED_FIELD_MODIFIERS), "Unexpected access flags set");
2453 flags = flags & (~JVM_ACC_PUBLIC);
2454 flags = flags | JVM_ACC_PRIVATE;
2455 AccessFlags access_flags;
2456 access_flags.set_flags(flags);
2457 assert(!access_flags.is_public(), "Failed to clear public flag");
2458 assert(access_flags.is_private(), "Failed to set private flag");
2459 fields_with_fix->ushort_at_put(i + instanceKlass::access_flags_offset,
2460 flags);
2462 assert(fields_with_fix->ushort_at(i + instanceKlass::name_index_offset)
2463 == reference_name_index, "The fake reference name is incorrect");
2464 assert(fields_with_fix->ushort_at(i + instanceKlass::signature_index_offset)
2465 == reference_sig_index, "The fake reference signature is incorrect");
2466 // The type of the field is stored in the low_offset entry during
2467 // parsing.
2468 assert(fields_with_fix->ushort_at(i + instanceKlass::low_offset) ==
2469 NONSTATIC_OOP, "The fake reference type is incorrect");
2471 // "fields" is allocated in the permanent generation. Disgard
2472 // it and let it be collected.
2473 (*fields_ptr) = fields_with_fix;
2474 }
2475 return;
2476 }
2479 void ClassFileParser::java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
2480 FieldAllocationCount *fac_ptr, TRAPS) {
2481 // Add fake fields for java.lang.Class instances
2482 //
2483 // This is not particularly nice. We should consider adding a
2484 // private transient object field at the Java level to
2485 // java.lang.Class. Alternatively we could add a subclass of
2486 // instanceKlass which provides an accessor and size computer for
2487 // this field, but that appears to be more code than this hack.
2488 //
2489 // NOTE that we wedge these in at the beginning rather than the
2490 // end of the object because the Class layout changed between JDK
2491 // 1.3 and JDK 1.4 with the new reflection implementation; some
2492 // nonstatic oop fields were added at the Java level. The offsets
2493 // of these fake fields can't change between these two JDK
2494 // versions because when the offsets are computed at bootstrap
2495 // time we don't know yet which version of the JDK we're running in.
2497 // The values below are fake but will force two non-static oop fields and
2498 // a corresponding non-static oop map block to be allocated.
2499 const int extra = java_lang_Class::number_of_fake_oop_fields;
2500 fac_ptr->nonstatic_oop_count += extra;
2501 }
2504 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2505 // Cause the extra fake fields in java.lang.Class to show up before
2506 // the Java fields for layout compatibility between 1.3 and 1.4
2507 // Incrementing next_nonstatic_oop_offset here advances the
2508 // location where the real java fields are placed.
2509 const int extra = java_lang_Class::number_of_fake_oop_fields;
2510 (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2511 }
2514 // Force MethodHandle.vmentry to be an unmanaged pointer.
2515 // There is no way for a classfile to express this, so we must help it.
2516 void ClassFileParser::java_dyn_MethodHandle_fix_pre(constantPoolHandle cp,
2517 typeArrayHandle* fields_ptr,
2518 FieldAllocationCount *fac_ptr,
2519 TRAPS) {
2520 // Add fake fields for java.dyn.MethodHandle instances
2521 //
2522 // This is not particularly nice, but since there is no way to express
2523 // a native wordSize field in Java, we must do it at this level.
2525 if (!EnableMethodHandles) return;
2527 int word_sig_index = 0;
2528 const int cp_size = cp->length();
2529 for (int index = 1; index < cp_size; index++) {
2530 if (cp->tag_at(index).is_utf8() &&
2531 cp->symbol_at(index) == vmSymbols::machine_word_signature()) {
2532 word_sig_index = index;
2533 break;
2534 }
2535 }
2537 if (word_sig_index == 0)
2538 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2539 "missing I or J signature (for vmentry) in java.dyn.MethodHandle");
2541 bool found_vmentry = false;
2543 const int n = (*fields_ptr)()->length();
2544 for (int i = 0; i < n; i += instanceKlass::next_offset) {
2545 int name_index = (*fields_ptr)->ushort_at(i + instanceKlass::name_index_offset);
2546 int sig_index = (*fields_ptr)->ushort_at(i + instanceKlass::signature_index_offset);
2547 int acc_flags = (*fields_ptr)->ushort_at(i + instanceKlass::access_flags_offset);
2548 symbolOop f_name = cp->symbol_at(name_index);
2549 symbolOop f_sig = cp->symbol_at(sig_index);
2550 if (f_sig == vmSymbols::byte_signature() &&
2551 f_name == vmSymbols::vmentry_name() &&
2552 (acc_flags & JVM_ACC_STATIC) == 0) {
2553 // Adjust the field type from byte to an unmanaged pointer.
2554 assert(fac_ptr->nonstatic_byte_count > 0, "");
2555 fac_ptr->nonstatic_byte_count -= 1;
2556 (*fields_ptr)->ushort_at_put(i + instanceKlass::signature_index_offset,
2557 word_sig_index);
2558 fac_ptr->nonstatic_word_count += 1;
2560 FieldAllocationType atype = (FieldAllocationType) (*fields_ptr)->ushort_at(i + instanceKlass::low_offset);
2561 assert(atype == NONSTATIC_BYTE, "");
2562 FieldAllocationType new_atype = NONSTATIC_WORD;
2563 (*fields_ptr)->ushort_at_put(i + instanceKlass::low_offset, new_atype);
2565 found_vmentry = true;
2566 break;
2567 }
2568 }
2570 if (!found_vmentry)
2571 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2572 "missing vmentry byte field in java.dyn.MethodHandle");
2574 }
2577 instanceKlassHandle ClassFileParser::parseClassFile(symbolHandle name,
2578 Handle class_loader,
2579 Handle protection_domain,
2580 KlassHandle host_klass,
2581 GrowableArray<Handle>* cp_patches,
2582 symbolHandle& parsed_name,
2583 bool verify,
2584 TRAPS) {
2585 // So that JVMTI can cache class file in the state before retransformable agents
2586 // have modified it
2587 unsigned char *cached_class_file_bytes = NULL;
2588 jint cached_class_file_length;
2590 ClassFileStream* cfs = stream();
2591 // Timing
2592 assert(THREAD->is_Java_thread(), "must be a JavaThread");
2593 JavaThread* jt = (JavaThread*) THREAD;
2595 PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(),
2596 ClassLoader::perf_class_parse_selftime(),
2597 NULL,
2598 jt->get_thread_stat()->perf_recursion_counts_addr(),
2599 jt->get_thread_stat()->perf_timers_addr(),
2600 PerfClassTraceTime::PARSE_CLASS);
2602 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2604 if (JvmtiExport::should_post_class_file_load_hook()) {
2605 unsigned char* ptr = cfs->buffer();
2606 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2608 JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain,
2609 &ptr, &end_ptr,
2610 &cached_class_file_bytes,
2611 &cached_class_file_length);
2613 if (ptr != cfs->buffer()) {
2614 // JVMTI agent has modified class file data.
2615 // Set new class file stream using JVMTI agent modified
2616 // class file data.
2617 cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source());
2618 set_stream(cfs);
2619 }
2620 }
2622 _host_klass = host_klass;
2623 _cp_patches = cp_patches;
2625 instanceKlassHandle nullHandle;
2627 // Figure out whether we can skip format checking (matching classic VM behavior)
2628 _need_verify = Verifier::should_verify_for(class_loader(), verify);
2630 // Set the verify flag in stream
2631 cfs->set_verify(_need_verify);
2633 // Save the class file name for easier error message printing.
2634 _class_name = name.not_null()? name : vmSymbolHandles::unknown_class_name();
2636 cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor
2637 // Magic value
2638 u4 magic = cfs->get_u4_fast();
2639 guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
2640 "Incompatible magic value %u in class file %s",
2641 magic, CHECK_(nullHandle));
2643 // Version numbers
2644 u2 minor_version = cfs->get_u2_fast();
2645 u2 major_version = cfs->get_u2_fast();
2647 // Check version numbers - we check this even with verifier off
2648 if (!is_supported_version(major_version, minor_version)) {
2649 if (name.is_null()) {
2650 Exceptions::fthrow(
2651 THREAD_AND_LOCATION,
2652 vmSymbolHandles::java_lang_UnsupportedClassVersionError(),
2653 "Unsupported major.minor version %u.%u",
2654 major_version,
2655 minor_version);
2656 } else {
2657 ResourceMark rm(THREAD);
2658 Exceptions::fthrow(
2659 THREAD_AND_LOCATION,
2660 vmSymbolHandles::java_lang_UnsupportedClassVersionError(),
2661 "%s : Unsupported major.minor version %u.%u",
2662 name->as_C_string(),
2663 major_version,
2664 minor_version);
2665 }
2666 return nullHandle;
2667 }
2669 _major_version = major_version;
2670 _minor_version = minor_version;
2673 // Check if verification needs to be relaxed for this class file
2674 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
2675 _relax_verify = Verifier::relax_verify_for(class_loader());
2677 // Constant pool
2678 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
2679 int cp_size = cp->length();
2681 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
2683 // Access flags
2684 AccessFlags access_flags;
2685 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
2687 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2688 // Set abstract bit for old class files for backward compatibility
2689 flags |= JVM_ACC_ABSTRACT;
2690 }
2691 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
2692 access_flags.set_flags(flags);
2694 // This class and superclass
2695 instanceKlassHandle super_klass;
2696 u2 this_class_index = cfs->get_u2_fast();
2697 check_property(
2698 valid_cp_range(this_class_index, cp_size) &&
2699 cp->tag_at(this_class_index).is_unresolved_klass(),
2700 "Invalid this class index %u in constant pool in class file %s",
2701 this_class_index, CHECK_(nullHandle));
2703 symbolHandle class_name (THREAD, cp->unresolved_klass_at(this_class_index));
2704 assert(class_name.not_null(), "class_name can't be null");
2706 // It's important to set parsed_name *before* resolving the super class.
2707 // (it's used for cleanup by the caller if parsing fails)
2708 parsed_name = class_name;
2710 // Update _class_name which could be null previously to be class_name
2711 _class_name = class_name;
2713 // Don't need to check whether this class name is legal or not.
2714 // It has been checked when constant pool is parsed.
2715 // However, make sure it is not an array type.
2716 if (_need_verify) {
2717 guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY,
2718 "Bad class name in class file %s",
2719 CHECK_(nullHandle));
2720 }
2722 klassOop preserve_this_klass; // for storing result across HandleMark
2724 // release all handles when parsing is done
2725 { HandleMark hm(THREAD);
2727 // Checks if name in class file matches requested name
2728 if (name.not_null() && class_name() != name()) {
2729 ResourceMark rm(THREAD);
2730 Exceptions::fthrow(
2731 THREAD_AND_LOCATION,
2732 vmSymbolHandles::java_lang_NoClassDefFoundError(),
2733 "%s (wrong name: %s)",
2734 name->as_C_string(),
2735 class_name->as_C_string()
2736 );
2737 return nullHandle;
2738 }
2740 if (TraceClassLoadingPreorder) {
2741 tty->print("[Loading %s", name()->as_klass_external_name());
2742 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
2743 tty->print_cr("]");
2744 }
2746 u2 super_class_index = cfs->get_u2_fast();
2747 if (super_class_index == 0) {
2748 check_property(class_name() == vmSymbols::java_lang_Object(),
2749 "Invalid superclass index %u in class file %s",
2750 super_class_index,
2751 CHECK_(nullHandle));
2752 } else {
2753 check_property(valid_cp_range(super_class_index, cp_size) &&
2754 is_klass_reference(cp, super_class_index),
2755 "Invalid superclass index %u in class file %s",
2756 super_class_index,
2757 CHECK_(nullHandle));
2758 // The class name should be legal because it is checked when parsing constant pool.
2759 // However, make sure it is not an array type.
2760 bool is_array = false;
2761 if (cp->tag_at(super_class_index).is_klass()) {
2762 super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index));
2763 if (_need_verify)
2764 is_array = super_klass->oop_is_array();
2765 } else if (_need_verify) {
2766 is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
2767 }
2768 if (_need_verify) {
2769 guarantee_property(!is_array,
2770 "Bad superclass name in class file %s", CHECK_(nullHandle));
2771 }
2772 }
2774 // Interfaces
2775 u2 itfs_len = cfs->get_u2_fast();
2776 objArrayHandle local_interfaces;
2777 if (itfs_len == 0) {
2778 local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2779 } else {
2780 local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle));
2781 }
2783 // Fields (offsets are filled in later)
2784 struct FieldAllocationCount fac = {0,0,0,0,0,0,0,0,0,0};
2785 objArrayHandle fields_annotations;
2786 typeArrayHandle fields = parse_fields(cp, access_flags.is_interface(), &fac, &fields_annotations, CHECK_(nullHandle));
2787 // Methods
2788 bool has_final_method = false;
2789 AccessFlags promoted_flags;
2790 promoted_flags.set_flags(0);
2791 // These need to be oop pointers because they are allocated lazily
2792 // inside parse_methods inside a nested HandleMark
2793 objArrayOop methods_annotations_oop = NULL;
2794 objArrayOop methods_parameter_annotations_oop = NULL;
2795 objArrayOop methods_default_annotations_oop = NULL;
2796 objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
2797 &promoted_flags,
2798 &has_final_method,
2799 &methods_annotations_oop,
2800 &methods_parameter_annotations_oop,
2801 &methods_default_annotations_oop,
2802 CHECK_(nullHandle));
2804 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2805 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2806 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
2808 // We check super class after class file is parsed and format is checked
2809 if (super_class_index > 0 && super_klass.is_null()) {
2810 symbolHandle sk (THREAD, cp->klass_name_at(super_class_index));
2811 if (access_flags.is_interface()) {
2812 // Before attempting to resolve the superclass, check for class format
2813 // errors not checked yet.
2814 guarantee_property(sk() == vmSymbols::java_lang_Object(),
2815 "Interfaces must have java.lang.Object as superclass in class file %s",
2816 CHECK_(nullHandle));
2817 }
2818 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
2819 sk,
2820 class_loader,
2821 protection_domain,
2822 true,
2823 CHECK_(nullHandle));
2825 KlassHandle kh (THREAD, k);
2826 super_klass = instanceKlassHandle(THREAD, kh());
2827 if (LinkWellKnownClasses) // my super class is well known to me
2828 cp->klass_at_put(super_class_index, super_klass()); // eagerly resolve
2829 }
2830 if (super_klass.not_null()) {
2831 if (super_klass->is_interface()) {
2832 ResourceMark rm(THREAD);
2833 Exceptions::fthrow(
2834 THREAD_AND_LOCATION,
2835 vmSymbolHandles::java_lang_IncompatibleClassChangeError(),
2836 "class %s has interface %s as super class",
2837 class_name->as_klass_external_name(),
2838 super_klass->external_name()
2839 );
2840 return nullHandle;
2841 }
2842 // Make sure super class is not final
2843 if (super_klass->is_final()) {
2844 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
2845 }
2846 }
2848 // Compute the transitive list of all unique interfaces implemented by this class
2849 objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
2851 // sort methods
2852 typeArrayHandle method_ordering = sort_methods(methods,
2853 methods_annotations,
2854 methods_parameter_annotations,
2855 methods_default_annotations,
2856 CHECK_(nullHandle));
2858 // promote flags from parse_methods() to the klass' flags
2859 access_flags.add_promoted_flags(promoted_flags.as_int());
2861 // Size of Java vtable (in words)
2862 int vtable_size = 0;
2863 int itable_size = 0;
2864 int num_miranda_methods = 0;
2866 klassVtable::compute_vtable_size_and_num_mirandas(vtable_size,
2867 num_miranda_methods,
2868 super_klass(),
2869 methods(),
2870 access_flags,
2871 class_loader,
2872 class_name,
2873 local_interfaces(),
2874 CHECK_(nullHandle));
2876 // Size of Java itable (in words)
2877 itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(transitive_interfaces);
2879 // Field size and offset computation
2880 int nonstatic_field_size = super_klass() == NULL ? 0 : super_klass->nonstatic_field_size();
2881 #ifndef PRODUCT
2882 int orig_nonstatic_field_size = 0;
2883 #endif
2884 int static_field_size = 0;
2885 int next_static_oop_offset;
2886 int next_static_double_offset;
2887 int next_static_word_offset;
2888 int next_static_short_offset;
2889 int next_static_byte_offset;
2890 int next_static_type_offset;
2891 int next_nonstatic_oop_offset;
2892 int next_nonstatic_double_offset;
2893 int next_nonstatic_word_offset;
2894 int next_nonstatic_short_offset;
2895 int next_nonstatic_byte_offset;
2896 int next_nonstatic_type_offset;
2897 int first_nonstatic_oop_offset;
2898 int first_nonstatic_field_offset;
2899 int next_nonstatic_field_offset;
2901 // Calculate the starting byte offsets
2902 next_static_oop_offset = (instanceKlass::header_size() +
2903 align_object_offset(vtable_size) +
2904 align_object_offset(itable_size)) * wordSize;
2905 next_static_double_offset = next_static_oop_offset +
2906 (fac.static_oop_count * heapOopSize);
2907 if ( fac.static_double_count &&
2908 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
2909 Universe::field_type_should_be_aligned(T_LONG)) ) {
2910 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
2911 }
2913 next_static_word_offset = next_static_double_offset +
2914 (fac.static_double_count * BytesPerLong);
2915 next_static_short_offset = next_static_word_offset +
2916 (fac.static_word_count * BytesPerInt);
2917 next_static_byte_offset = next_static_short_offset +
2918 (fac.static_short_count * BytesPerShort);
2919 next_static_type_offset = align_size_up((next_static_byte_offset +
2920 fac.static_byte_count ), wordSize );
2921 static_field_size = (next_static_type_offset -
2922 next_static_oop_offset) / wordSize;
2923 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
2924 nonstatic_field_size * heapOopSize;
2925 next_nonstatic_field_offset = first_nonstatic_field_offset;
2927 // Add fake fields for java.lang.Class instances (also see below)
2928 if (class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2929 java_lang_Class_fix_pre(&methods, &fac, CHECK_(nullHandle));
2930 }
2932 // adjust the vmentry field declaration in java.dyn.MethodHandle
2933 if (EnableMethodHandles && class_name() == vmSymbols::sun_dyn_MethodHandleImpl() && class_loader.is_null()) {
2934 java_dyn_MethodHandle_fix_pre(cp, &fields, &fac, CHECK_(nullHandle));
2935 }
2937 // Add a fake "discovered" field if it is not present
2938 // for compatibility with earlier jdk's.
2939 if (class_name() == vmSymbols::java_lang_ref_Reference()
2940 && class_loader.is_null()) {
2941 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
2942 }
2943 // end of "discovered" field compactibility fix
2945 unsigned int nonstatic_double_count = fac.nonstatic_double_count;
2946 unsigned int nonstatic_word_count = fac.nonstatic_word_count;
2947 unsigned int nonstatic_short_count = fac.nonstatic_short_count;
2948 unsigned int nonstatic_byte_count = fac.nonstatic_byte_count;
2949 unsigned int nonstatic_oop_count = fac.nonstatic_oop_count;
2951 bool super_has_nonstatic_fields =
2952 (super_klass() != NULL && super_klass->has_nonstatic_fields());
2953 bool has_nonstatic_fields = super_has_nonstatic_fields ||
2954 ((nonstatic_double_count + nonstatic_word_count +
2955 nonstatic_short_count + nonstatic_byte_count +
2956 nonstatic_oop_count) != 0);
2959 // Prepare list of oops for oop map generation.
2960 int* nonstatic_oop_offsets;
2961 unsigned int* nonstatic_oop_counts;
2962 unsigned int nonstatic_oop_map_count = 0;
2964 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
2965 THREAD, int, nonstatic_oop_count + 1);
2966 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
2967 THREAD, unsigned int, nonstatic_oop_count + 1);
2969 // Add fake fields for java.lang.Class instances (also see above).
2970 // FieldsAllocationStyle and CompactFields values will be reset to default.
2971 if(class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
2972 java_lang_Class_fix_post(&next_nonstatic_field_offset);
2973 nonstatic_oop_offsets[0] = first_nonstatic_field_offset;
2974 const uint fake_oop_count = (next_nonstatic_field_offset -
2975 first_nonstatic_field_offset) / heapOopSize;
2976 nonstatic_oop_counts[0] = fake_oop_count;
2977 nonstatic_oop_map_count = 1;
2978 nonstatic_oop_count -= fake_oop_count;
2979 first_nonstatic_oop_offset = first_nonstatic_field_offset;
2980 } else {
2981 first_nonstatic_oop_offset = 0; // will be set for first oop field
2982 }
2984 #ifndef PRODUCT
2985 if( PrintCompactFieldsSavings ) {
2986 next_nonstatic_double_offset = next_nonstatic_field_offset +
2987 (nonstatic_oop_count * heapOopSize);
2988 if ( nonstatic_double_count > 0 ) {
2989 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
2990 }
2991 next_nonstatic_word_offset = next_nonstatic_double_offset +
2992 (nonstatic_double_count * BytesPerLong);
2993 next_nonstatic_short_offset = next_nonstatic_word_offset +
2994 (nonstatic_word_count * BytesPerInt);
2995 next_nonstatic_byte_offset = next_nonstatic_short_offset +
2996 (nonstatic_short_count * BytesPerShort);
2997 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
2998 nonstatic_byte_count ), heapOopSize );
2999 orig_nonstatic_field_size = nonstatic_field_size +
3000 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3001 }
3002 #endif
3003 bool compact_fields = CompactFields;
3004 int allocation_style = FieldsAllocationStyle;
3005 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3006 assert(false, "0 <= FieldsAllocationStyle <= 2");
3007 allocation_style = 1; // Optimistic
3008 }
3010 // The next classes have predefined hard-coded fields offsets
3011 // (see in JavaClasses::compute_hard_coded_offsets()).
3012 // Use default fields allocation order for them.
3013 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3014 (class_name() == vmSymbols::java_lang_AssertionStatusDirectives() ||
3015 class_name() == vmSymbols::java_lang_Class() ||
3016 class_name() == vmSymbols::java_lang_ClassLoader() ||
3017 class_name() == vmSymbols::java_lang_ref_Reference() ||
3018 class_name() == vmSymbols::java_lang_ref_SoftReference() ||
3019 class_name() == vmSymbols::java_lang_StackTraceElement() ||
3020 class_name() == vmSymbols::java_lang_String() ||
3021 class_name() == vmSymbols::java_lang_Throwable() ||
3022 class_name() == vmSymbols::java_lang_Boolean() ||
3023 class_name() == vmSymbols::java_lang_Character() ||
3024 class_name() == vmSymbols::java_lang_Float() ||
3025 class_name() == vmSymbols::java_lang_Double() ||
3026 class_name() == vmSymbols::java_lang_Byte() ||
3027 class_name() == vmSymbols::java_lang_Short() ||
3028 class_name() == vmSymbols::java_lang_Integer() ||
3029 class_name() == vmSymbols::java_lang_Long())) {
3030 allocation_style = 0; // Allocate oops first
3031 compact_fields = false; // Don't compact fields
3032 }
3034 if( allocation_style == 0 ) {
3035 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes
3036 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3037 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3038 (nonstatic_oop_count * heapOopSize);
3039 } else if( allocation_style == 1 ) {
3040 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops
3041 next_nonstatic_double_offset = next_nonstatic_field_offset;
3042 } else if( allocation_style == 2 ) {
3043 // Fields allocation: oops fields in super and sub classes are together.
3044 if( nonstatic_field_size > 0 && super_klass() != NULL &&
3045 super_klass->nonstatic_oop_map_size() > 0 ) {
3046 int map_size = super_klass->nonstatic_oop_map_size();
3047 OopMapBlock* first_map = super_klass->start_of_nonstatic_oop_maps();
3048 OopMapBlock* last_map = first_map + map_size - 1;
3049 int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3050 if (next_offset == next_nonstatic_field_offset) {
3051 allocation_style = 0; // allocate oops first
3052 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3053 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3054 (nonstatic_oop_count * heapOopSize);
3055 }
3056 }
3057 if( allocation_style == 2 ) {
3058 allocation_style = 1; // allocate oops last
3059 next_nonstatic_double_offset = next_nonstatic_field_offset;
3060 }
3061 } else {
3062 ShouldNotReachHere();
3063 }
3065 int nonstatic_oop_space_count = 0;
3066 int nonstatic_word_space_count = 0;
3067 int nonstatic_short_space_count = 0;
3068 int nonstatic_byte_space_count = 0;
3069 int nonstatic_oop_space_offset;
3070 int nonstatic_word_space_offset;
3071 int nonstatic_short_space_offset;
3072 int nonstatic_byte_space_offset;
3074 if( nonstatic_double_count > 0 ) {
3075 int offset = next_nonstatic_double_offset;
3076 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
3077 if( compact_fields && offset != next_nonstatic_double_offset ) {
3078 // Allocate available fields into the gap before double field.
3079 int length = next_nonstatic_double_offset - offset;
3080 assert(length == BytesPerInt, "");
3081 nonstatic_word_space_offset = offset;
3082 if( nonstatic_word_count > 0 ) {
3083 nonstatic_word_count -= 1;
3084 nonstatic_word_space_count = 1; // Only one will fit
3085 length -= BytesPerInt;
3086 offset += BytesPerInt;
3087 }
3088 nonstatic_short_space_offset = offset;
3089 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
3090 nonstatic_short_count -= 1;
3091 nonstatic_short_space_count += 1;
3092 length -= BytesPerShort;
3093 offset += BytesPerShort;
3094 }
3095 nonstatic_byte_space_offset = offset;
3096 while( length > 0 && nonstatic_byte_count > 0 ) {
3097 nonstatic_byte_count -= 1;
3098 nonstatic_byte_space_count += 1;
3099 length -= 1;
3100 }
3101 // Allocate oop field in the gap if there are no other fields for that.
3102 nonstatic_oop_space_offset = offset;
3103 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
3104 allocation_style != 0 ) { // when oop fields not first
3105 nonstatic_oop_count -= 1;
3106 nonstatic_oop_space_count = 1; // Only one will fit
3107 length -= heapOopSize;
3108 offset += heapOopSize;
3109 }
3110 }
3111 }
3113 next_nonstatic_word_offset = next_nonstatic_double_offset +
3114 (nonstatic_double_count * BytesPerLong);
3115 next_nonstatic_short_offset = next_nonstatic_word_offset +
3116 (nonstatic_word_count * BytesPerInt);
3117 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3118 (nonstatic_short_count * BytesPerShort);
3120 int notaligned_offset;
3121 if( allocation_style == 0 ) {
3122 notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3123 } else { // allocation_style == 1
3124 next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3125 if( nonstatic_oop_count > 0 ) {
3126 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3127 }
3128 notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3129 }
3130 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3131 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3132 - first_nonstatic_field_offset)/heapOopSize);
3134 // Iterate over fields again and compute correct offsets.
3135 // The field allocation type was temporarily stored in the offset slot.
3136 // oop fields are located before non-oop fields (static and non-static).
3137 int len = fields->length();
3138 for (int i = 0; i < len; i += instanceKlass::next_offset) {
3139 int real_offset;
3140 FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i + instanceKlass::low_offset);
3141 switch (atype) {
3142 case STATIC_OOP:
3143 real_offset = next_static_oop_offset;
3144 next_static_oop_offset += heapOopSize;
3145 break;
3146 case STATIC_BYTE:
3147 real_offset = next_static_byte_offset;
3148 next_static_byte_offset += 1;
3149 break;
3150 case STATIC_SHORT:
3151 real_offset = next_static_short_offset;
3152 next_static_short_offset += BytesPerShort;
3153 break;
3154 case STATIC_WORD:
3155 real_offset = next_static_word_offset;
3156 next_static_word_offset += BytesPerInt;
3157 break;
3158 case STATIC_ALIGNED_DOUBLE:
3159 case STATIC_DOUBLE:
3160 real_offset = next_static_double_offset;
3161 next_static_double_offset += BytesPerLong;
3162 break;
3163 case NONSTATIC_OOP:
3164 if( nonstatic_oop_space_count > 0 ) {
3165 real_offset = nonstatic_oop_space_offset;
3166 nonstatic_oop_space_offset += heapOopSize;
3167 nonstatic_oop_space_count -= 1;
3168 } else {
3169 real_offset = next_nonstatic_oop_offset;
3170 next_nonstatic_oop_offset += heapOopSize;
3171 }
3172 // Update oop maps
3173 if( nonstatic_oop_map_count > 0 &&
3174 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3175 real_offset -
3176 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3177 heapOopSize ) {
3178 // Extend current oop map
3179 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
3180 } else {
3181 // Create new oop map
3182 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3183 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3184 nonstatic_oop_map_count += 1;
3185 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3186 first_nonstatic_oop_offset = real_offset;
3187 }
3188 }
3189 break;
3190 case NONSTATIC_BYTE:
3191 if( nonstatic_byte_space_count > 0 ) {
3192 real_offset = nonstatic_byte_space_offset;
3193 nonstatic_byte_space_offset += 1;
3194 nonstatic_byte_space_count -= 1;
3195 } else {
3196 real_offset = next_nonstatic_byte_offset;
3197 next_nonstatic_byte_offset += 1;
3198 }
3199 break;
3200 case NONSTATIC_SHORT:
3201 if( nonstatic_short_space_count > 0 ) {
3202 real_offset = nonstatic_short_space_offset;
3203 nonstatic_short_space_offset += BytesPerShort;
3204 nonstatic_short_space_count -= 1;
3205 } else {
3206 real_offset = next_nonstatic_short_offset;
3207 next_nonstatic_short_offset += BytesPerShort;
3208 }
3209 break;
3210 case NONSTATIC_WORD:
3211 if( nonstatic_word_space_count > 0 ) {
3212 real_offset = nonstatic_word_space_offset;
3213 nonstatic_word_space_offset += BytesPerInt;
3214 nonstatic_word_space_count -= 1;
3215 } else {
3216 real_offset = next_nonstatic_word_offset;
3217 next_nonstatic_word_offset += BytesPerInt;
3218 }
3219 break;
3220 case NONSTATIC_ALIGNED_DOUBLE:
3221 case NONSTATIC_DOUBLE:
3222 real_offset = next_nonstatic_double_offset;
3223 next_nonstatic_double_offset += BytesPerLong;
3224 break;
3225 default:
3226 ShouldNotReachHere();
3227 }
3228 fields->short_at_put(i + instanceKlass::low_offset, extract_low_short_from_int(real_offset));
3229 fields->short_at_put(i + instanceKlass::high_offset, extract_high_short_from_int(real_offset));
3230 }
3232 // Size of instances
3233 int instance_size;
3235 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3236 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3238 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3240 // Number of non-static oop map blocks allocated at end of klass.
3241 const unsigned int total_oop_map_count =
3242 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3243 first_nonstatic_oop_offset);
3245 // Compute reference type
3246 ReferenceType rt;
3247 if (super_klass() == NULL) {
3248 rt = REF_NONE;
3249 } else {
3250 rt = super_klass->reference_type();
3251 }
3253 // We can now create the basic klassOop for this klass
3254 klassOop ik = oopFactory::new_instanceKlass(vtable_size, itable_size,
3255 static_field_size,
3256 total_oop_map_count,
3257 rt, CHECK_(nullHandle));
3258 instanceKlassHandle this_klass (THREAD, ik);
3260 assert(this_klass->static_field_size() == static_field_size, "sanity");
3261 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3262 "sanity");
3264 // Fill in information already parsed
3265 this_klass->set_access_flags(access_flags);
3266 this_klass->set_should_verify_class(verify);
3267 jint lh = Klass::instance_layout_helper(instance_size, false);
3268 this_klass->set_layout_helper(lh);
3269 assert(this_klass->oop_is_instance(), "layout is correct");
3270 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3271 // Not yet: supers are done below to support the new subtype-checking fields
3272 //this_klass->set_super(super_klass());
3273 this_klass->set_class_loader(class_loader());
3274 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3275 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3276 this_klass->set_static_oop_field_size(fac.static_oop_count);
3277 cp->set_pool_holder(this_klass());
3278 this_klass->set_constants(cp());
3279 this_klass->set_local_interfaces(local_interfaces());
3280 this_klass->set_fields(fields());
3281 this_klass->set_methods(methods());
3282 if (has_final_method) {
3283 this_klass->set_has_final_method();
3284 }
3285 this_klass->set_method_ordering(method_ordering());
3286 // The instanceKlass::_methods_jmethod_ids cache and the
3287 // instanceKlass::_methods_cached_itable_indices cache are
3288 // both managed on the assumption that the initial cache
3289 // size is equal to the number of methods in the class. If
3290 // that changes, then instanceKlass::idnum_can_increment()
3291 // has to be changed accordingly.
3292 this_klass->set_initial_method_idnum(methods->length());
3293 this_klass->set_name(cp->klass_name_at(this_class_index));
3294 if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself
3295 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3296 this_klass->set_protection_domain(protection_domain());
3297 this_klass->set_fields_annotations(fields_annotations());
3298 this_klass->set_methods_annotations(methods_annotations());
3299 this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3300 this_klass->set_methods_default_annotations(methods_default_annotations());
3302 this_klass->set_minor_version(minor_version);
3303 this_klass->set_major_version(major_version);
3305 // Set up methodOop::intrinsic_id as soon as we know the names of methods.
3306 // (We used to do this lazily, but now we query it in Rewriter,
3307 // which is eagerly done for every method, so we might as well do it now,
3308 // when everything is fresh in memory.)
3309 if (methodOopDesc::klass_id_for_intrinsics(this_klass->as_klassOop()) != vmSymbols::NO_SID) {
3310 for (int j = 0; j < methods->length(); j++) {
3311 ((methodOop)methods->obj_at(j))->init_intrinsic_id();
3312 }
3313 }
3315 if (cached_class_file_bytes != NULL) {
3316 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3317 this_klass->set_cached_class_file(cached_class_file_bytes,
3318 cached_class_file_length);
3319 }
3321 // Miranda methods
3322 if ((num_miranda_methods > 0) ||
3323 // if this class introduced new miranda methods or
3324 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3325 // super class exists and this class inherited miranda methods
3326 ) {
3327 this_klass->set_has_miranda_methods(); // then set a flag
3328 }
3330 // Additional attributes
3331 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle));
3333 // Make sure this is the end of class file stream
3334 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3336 // Initialize static fields
3337 this_klass->do_local_static_fields(&initialize_static_field, CHECK_(nullHandle));
3339 // VerifyOops believes that once this has been set, the object is completely loaded.
3340 // Compute transitive closure of interfaces this class implements
3341 this_klass->set_transitive_interfaces(transitive_interfaces());
3343 // Fill in information needed to compute superclasses.
3344 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
3346 // Initialize itable offset tables
3347 klassItable::setup_itable_offset_table(this_klass);
3349 // Do final class setup
3350 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3352 set_precomputed_flags(this_klass);
3354 // reinitialize modifiers, using the InnerClasses attribute
3355 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3356 this_klass->set_modifier_flags(computed_modifiers);
3358 // check if this class can access its super class
3359 check_super_class_access(this_klass, CHECK_(nullHandle));
3361 // check if this class can access its superinterfaces
3362 check_super_interface_access(this_klass, CHECK_(nullHandle));
3364 // check if this class overrides any final method
3365 check_final_method_override(this_klass, CHECK_(nullHandle));
3367 // check that if this class is an interface then it doesn't have static methods
3368 if (this_klass->is_interface()) {
3369 check_illegal_static_method(this_klass, CHECK_(nullHandle));
3370 }
3372 ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()),
3373 false /* not shared class */);
3375 if (TraceClassLoading) {
3376 // print in a single call to reduce interleaving of output
3377 if (cfs->source() != NULL) {
3378 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3379 cfs->source());
3380 } else if (class_loader.is_null()) {
3381 if (THREAD->is_Java_thread()) {
3382 klassOop caller = ((JavaThread*)THREAD)->security_get_caller_class(1);
3383 tty->print("[Loaded %s by instance of %s]\n",
3384 this_klass->external_name(),
3385 instanceKlass::cast(caller)->external_name());
3386 } else {
3387 tty->print("[Loaded %s]\n", this_klass->external_name());
3388 }
3389 } else {
3390 ResourceMark rm;
3391 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3392 instanceKlass::cast(class_loader->klass())->external_name());
3393 }
3394 }
3396 if (TraceClassResolution) {
3397 // print out the superclass.
3398 const char * from = Klass::cast(this_klass())->external_name();
3399 if (this_klass->java_super() != NULL) {
3400 tty->print("RESOLVE %s %s (super)\n", from, instanceKlass::cast(this_klass->java_super())->external_name());
3401 }
3402 // print out each of the interface classes referred to by this class.
3403 objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces());
3404 if (!local_interfaces.is_null()) {
3405 int length = local_interfaces->length();
3406 for (int i = 0; i < length; i++) {
3407 klassOop k = klassOop(local_interfaces->obj_at(i));
3408 instanceKlass* to_class = instanceKlass::cast(k);
3409 const char * to = to_class->external_name();
3410 tty->print("RESOLVE %s %s (interface)\n", from, to);
3411 }
3412 }
3413 }
3415 #ifndef PRODUCT
3416 if( PrintCompactFieldsSavings ) {
3417 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3418 tty->print("[Saved %d of %d bytes in %s]\n",
3419 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
3420 orig_nonstatic_field_size*heapOopSize,
3421 this_klass->external_name());
3422 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3423 tty->print("[Wasted %d over %d bytes in %s]\n",
3424 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
3425 orig_nonstatic_field_size*heapOopSize,
3426 this_klass->external_name());
3427 }
3428 }
3429 #endif
3431 // preserve result across HandleMark
3432 preserve_this_klass = this_klass();
3433 }
3435 // Create new handle outside HandleMark
3436 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
3437 debug_only(this_klass->as_klassOop()->verify();)
3439 return this_klass;
3440 }
3443 unsigned int
3444 ClassFileParser::compute_oop_map_count(instanceKlassHandle super,
3445 unsigned int nonstatic_oop_map_count,
3446 int first_nonstatic_oop_offset) {
3447 unsigned int map_count =
3448 super.is_null() ? 0 : super->nonstatic_oop_map_count();
3449 if (nonstatic_oop_map_count > 0) {
3450 // We have oops to add to map
3451 if (map_count == 0) {
3452 map_count = nonstatic_oop_map_count;
3453 } else {
3454 // Check whether we should add a new map block or whether the last one can
3455 // be extended
3456 OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps();
3457 OopMapBlock* const last_map = first_map + map_count - 1;
3459 int next_offset = last_map->offset() + last_map->count() * heapOopSize;
3460 if (next_offset == first_nonstatic_oop_offset) {
3461 // There is no gap bettwen superklass's last oop field and first
3462 // local oop field, merge maps.
3463 nonstatic_oop_map_count -= 1;
3464 } else {
3465 // Superklass didn't end with a oop field, add extra maps
3466 assert(next_offset < first_nonstatic_oop_offset, "just checking");
3467 }
3468 map_count += nonstatic_oop_map_count;
3469 }
3470 }
3471 return map_count;
3472 }
3475 void ClassFileParser::fill_oop_maps(instanceKlassHandle k,
3476 unsigned int nonstatic_oop_map_count,
3477 int* nonstatic_oop_offsets,
3478 unsigned int* nonstatic_oop_counts) {
3479 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
3480 const instanceKlass* const super = k->superklass();
3481 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0;
3482 if (super_count > 0) {
3483 // Copy maps from superklass
3484 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps();
3485 for (unsigned int i = 0; i < super_count; ++i) {
3486 *this_oop_map++ = *super_oop_map++;
3487 }
3488 }
3490 if (nonstatic_oop_map_count > 0) {
3491 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) {
3492 // The counts differ because there is no gap between superklass's last oop
3493 // field and the first local oop field. Extend the last oop map copied
3494 // from the superklass instead of creating new one.
3495 nonstatic_oop_map_count--;
3496 nonstatic_oop_offsets++;
3497 this_oop_map--;
3498 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++);
3499 this_oop_map++;
3500 }
3502 // Add new map blocks, fill them
3503 while (nonstatic_oop_map_count-- > 0) {
3504 this_oop_map->set_offset(*nonstatic_oop_offsets++);
3505 this_oop_map->set_count(*nonstatic_oop_counts++);
3506 this_oop_map++;
3507 }
3508 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() ==
3509 this_oop_map, "sanity");
3510 }
3511 }
3514 void ClassFileParser::set_precomputed_flags(instanceKlassHandle k) {
3515 klassOop super = k->super();
3517 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
3518 // in which case we don't have to register objects as finalizable
3519 if (!_has_empty_finalizer) {
3520 if (_has_finalizer ||
3521 (super != NULL && super->klass_part()->has_finalizer())) {
3522 k->set_has_finalizer();
3523 }
3524 }
3526 #ifdef ASSERT
3527 bool f = false;
3528 methodOop m = k->lookup_method(vmSymbols::finalize_method_name(),
3529 vmSymbols::void_method_signature());
3530 if (m != NULL && !m->is_empty_method()) {
3531 f = true;
3532 }
3533 assert(f == k->has_finalizer(), "inconsistent has_finalizer");
3534 #endif
3536 // Check if this klass supports the java.lang.Cloneable interface
3537 if (SystemDictionary::Cloneable_klass_loaded()) {
3538 if (k->is_subtype_of(SystemDictionary::Cloneable_klass())) {
3539 k->set_is_cloneable();
3540 }
3541 }
3543 // Check if this klass has a vanilla default constructor
3544 if (super == NULL) {
3545 // java.lang.Object has empty default constructor
3546 k->set_has_vanilla_constructor();
3547 } else {
3548 if (Klass::cast(super)->has_vanilla_constructor() &&
3549 _has_vanilla_constructor) {
3550 k->set_has_vanilla_constructor();
3551 }
3552 #ifdef ASSERT
3553 bool v = false;
3554 if (Klass::cast(super)->has_vanilla_constructor()) {
3555 methodOop constructor = k->find_method(vmSymbols::object_initializer_name(
3556 ), vmSymbols::void_method_signature());
3557 if (constructor != NULL && constructor->is_vanilla_constructor()) {
3558 v = true;
3559 }
3560 }
3561 assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
3562 #endif
3563 }
3565 // If it cannot be fast-path allocated, set a bit in the layout helper.
3566 // See documentation of instanceKlass::can_be_fastpath_allocated().
3567 assert(k->size_helper() > 0, "layout_helper is initialized");
3568 if ((!RegisterFinalizersAtInit && k->has_finalizer())
3569 || k->is_abstract() || k->is_interface()
3570 || (k->name() == vmSymbols::java_lang_Class()
3571 && k->class_loader() == NULL)
3572 || k->size_helper() >= FastAllocateSizeLimit) {
3573 // Forbid fast-path allocation.
3574 jint lh = Klass::instance_layout_helper(k->size_helper(), true);
3575 k->set_layout_helper(lh);
3576 }
3577 }
3580 // utility method for appending and array with check for duplicates
3582 void append_interfaces(objArrayHandle result, int& index, objArrayOop ifs) {
3583 // iterate over new interfaces
3584 for (int i = 0; i < ifs->length(); i++) {
3585 oop e = ifs->obj_at(i);
3586 assert(e->is_klass() && instanceKlass::cast(klassOop(e))->is_interface(), "just checking");
3587 // check for duplicates
3588 bool duplicate = false;
3589 for (int j = 0; j < index; j++) {
3590 if (result->obj_at(j) == e) {
3591 duplicate = true;
3592 break;
3593 }
3594 }
3595 // add new interface
3596 if (!duplicate) {
3597 result->obj_at_put(index++, e);
3598 }
3599 }
3600 }
3602 objArrayHandle ClassFileParser::compute_transitive_interfaces(instanceKlassHandle super, objArrayHandle local_ifs, TRAPS) {
3603 // Compute maximum size for transitive interfaces
3604 int max_transitive_size = 0;
3605 int super_size = 0;
3606 // Add superclass transitive interfaces size
3607 if (super.not_null()) {
3608 super_size = super->transitive_interfaces()->length();
3609 max_transitive_size += super_size;
3610 }
3611 // Add local interfaces' super interfaces
3612 int local_size = local_ifs->length();
3613 for (int i = 0; i < local_size; i++) {
3614 klassOop l = klassOop(local_ifs->obj_at(i));
3615 max_transitive_size += instanceKlass::cast(l)->transitive_interfaces()->length();
3616 }
3617 // Finally add local interfaces
3618 max_transitive_size += local_size;
3619 // Construct array
3620 objArrayHandle result;
3621 if (max_transitive_size == 0) {
3622 // no interfaces, use canonicalized array
3623 result = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
3624 } else if (max_transitive_size == super_size) {
3625 // no new local interfaces added, share superklass' transitive interface array
3626 result = objArrayHandle(THREAD, super->transitive_interfaces());
3627 } else if (max_transitive_size == local_size) {
3628 // only local interfaces added, share local interface array
3629 result = local_ifs;
3630 } else {
3631 objArrayHandle nullHandle;
3632 objArrayOop new_objarray = oopFactory::new_system_objArray(max_transitive_size, CHECK_(nullHandle));
3633 result = objArrayHandle(THREAD, new_objarray);
3634 int index = 0;
3635 // Copy down from superclass
3636 if (super.not_null()) {
3637 append_interfaces(result, index, super->transitive_interfaces());
3638 }
3639 // Copy down from local interfaces' superinterfaces
3640 for (int i = 0; i < local_ifs->length(); i++) {
3641 klassOop l = klassOop(local_ifs->obj_at(i));
3642 append_interfaces(result, index, instanceKlass::cast(l)->transitive_interfaces());
3643 }
3644 // Finally add local interfaces
3645 append_interfaces(result, index, local_ifs());
3647 // Check if duplicates were removed
3648 if (index != max_transitive_size) {
3649 assert(index < max_transitive_size, "just checking");
3650 objArrayOop new_result = oopFactory::new_system_objArray(index, CHECK_(nullHandle));
3651 for (int i = 0; i < index; i++) {
3652 oop e = result->obj_at(i);
3653 assert(e != NULL, "just checking");
3654 new_result->obj_at_put(i, e);
3655 }
3656 result = objArrayHandle(THREAD, new_result);
3657 }
3658 }
3659 return result;
3660 }
3663 void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) {
3664 klassOop super = this_klass->super();
3665 if ((super != NULL) &&
3666 (!Reflection::verify_class_access(this_klass->as_klassOop(), super, false))) {
3667 ResourceMark rm(THREAD);
3668 Exceptions::fthrow(
3669 THREAD_AND_LOCATION,
3670 vmSymbolHandles::java_lang_IllegalAccessError(),
3671 "class %s cannot access its superclass %s",
3672 this_klass->external_name(),
3673 instanceKlass::cast(super)->external_name()
3674 );
3675 return;
3676 }
3677 }
3680 void ClassFileParser::check_super_interface_access(instanceKlassHandle this_klass, TRAPS) {
3681 objArrayHandle local_interfaces (THREAD, this_klass->local_interfaces());
3682 int lng = local_interfaces->length();
3683 for (int i = lng - 1; i >= 0; i--) {
3684 klassOop k = klassOop(local_interfaces->obj_at(i));
3685 assert (k != NULL && Klass::cast(k)->is_interface(), "invalid interface");
3686 if (!Reflection::verify_class_access(this_klass->as_klassOop(), k, false)) {
3687 ResourceMark rm(THREAD);
3688 Exceptions::fthrow(
3689 THREAD_AND_LOCATION,
3690 vmSymbolHandles::java_lang_IllegalAccessError(),
3691 "class %s cannot access its superinterface %s",
3692 this_klass->external_name(),
3693 instanceKlass::cast(k)->external_name()
3694 );
3695 return;
3696 }
3697 }
3698 }
3701 void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) {
3702 objArrayHandle methods (THREAD, this_klass->methods());
3703 int num_methods = methods->length();
3705 // go thru each method and check if it overrides a final method
3706 for (int index = 0; index < num_methods; index++) {
3707 methodOop m = (methodOop)methods->obj_at(index);
3709 // skip private, static and <init> methods
3710 if ((!m->is_private()) &&
3711 (!m->is_static()) &&
3712 (m->name() != vmSymbols::object_initializer_name())) {
3714 symbolOop name = m->name();
3715 symbolOop signature = m->signature();
3716 klassOop k = this_klass->super();
3717 methodOop super_m = NULL;
3718 while (k != NULL) {
3719 // skip supers that don't have final methods.
3720 if (k->klass_part()->has_final_method()) {
3721 // lookup a matching method in the super class hierarchy
3722 super_m = instanceKlass::cast(k)->lookup_method(name, signature);
3723 if (super_m == NULL) {
3724 break; // didn't find any match; get out
3725 }
3727 if (super_m->is_final() &&
3728 // matching method in super is final
3729 (Reflection::verify_field_access(this_klass->as_klassOop(),
3730 super_m->method_holder(),
3731 super_m->method_holder(),
3732 super_m->access_flags(), false))
3733 // this class can access super final method and therefore override
3734 ) {
3735 ResourceMark rm(THREAD);
3736 Exceptions::fthrow(
3737 THREAD_AND_LOCATION,
3738 vmSymbolHandles::java_lang_VerifyError(),
3739 "class %s overrides final method %s.%s",
3740 this_klass->external_name(),
3741 name->as_C_string(),
3742 signature->as_C_string()
3743 );
3744 return;
3745 }
3747 // continue to look from super_m's holder's super.
3748 k = instanceKlass::cast(super_m->method_holder())->super();
3749 continue;
3750 }
3752 k = k->klass_part()->super();
3753 }
3754 }
3755 }
3756 }
3759 // assumes that this_klass is an interface
3760 void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) {
3761 assert(this_klass->is_interface(), "not an interface");
3762 objArrayHandle methods (THREAD, this_klass->methods());
3763 int num_methods = methods->length();
3765 for (int index = 0; index < num_methods; index++) {
3766 methodOop m = (methodOop)methods->obj_at(index);
3767 // if m is static and not the init method, throw a verify error
3768 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
3769 ResourceMark rm(THREAD);
3770 Exceptions::fthrow(
3771 THREAD_AND_LOCATION,
3772 vmSymbolHandles::java_lang_VerifyError(),
3773 "Illegal static method %s in interface %s",
3774 m->name()->as_C_string(),
3775 this_klass->external_name()
3776 );
3777 return;
3778 }
3779 }
3780 }
3782 // utility methods for format checking
3784 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) {
3785 if (!_need_verify) { return; }
3787 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3788 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
3789 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3790 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
3791 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3792 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
3793 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3795 if ((is_abstract && is_final) ||
3796 (is_interface && !is_abstract) ||
3797 (is_interface && major_gte_15 && (is_super || is_enum)) ||
3798 (!is_interface && major_gte_15 && is_annotation)) {
3799 ResourceMark rm(THREAD);
3800 Exceptions::fthrow(
3801 THREAD_AND_LOCATION,
3802 vmSymbolHandles::java_lang_ClassFormatError(),
3803 "Illegal class modifiers in class %s: 0x%X",
3804 _class_name->as_C_string(), flags
3805 );
3806 return;
3807 }
3808 }
3810 bool ClassFileParser::has_illegal_visibility(jint flags) {
3811 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3812 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3813 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3815 return ((is_public && is_protected) ||
3816 (is_public && is_private) ||
3817 (is_protected && is_private));
3818 }
3820 bool ClassFileParser::is_supported_version(u2 major, u2 minor) {
3821 u2 max_version =
3822 JDK_Version::is_gte_jdk17x_version() ? JAVA_MAX_SUPPORTED_VERSION :
3823 (JDK_Version::is_gte_jdk16x_version() ? JAVA_6_VERSION : JAVA_1_5_VERSION);
3824 return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
3825 (major <= max_version) &&
3826 ((major != max_version) ||
3827 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION));
3828 }
3830 void ClassFileParser::verify_legal_field_modifiers(
3831 jint flags, bool is_interface, TRAPS) {
3832 if (!_need_verify) { return; }
3834 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3835 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3836 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3837 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3838 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3839 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
3840 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
3841 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3842 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3844 bool is_illegal = false;
3846 if (is_interface) {
3847 if (!is_public || !is_static || !is_final || is_private ||
3848 is_protected || is_volatile || is_transient ||
3849 (major_gte_15 && is_enum)) {
3850 is_illegal = true;
3851 }
3852 } else { // not interface
3853 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
3854 is_illegal = true;
3855 }
3856 }
3858 if (is_illegal) {
3859 ResourceMark rm(THREAD);
3860 Exceptions::fthrow(
3861 THREAD_AND_LOCATION,
3862 vmSymbolHandles::java_lang_ClassFormatError(),
3863 "Illegal field modifiers in class %s: 0x%X",
3864 _class_name->as_C_string(), flags);
3865 return;
3866 }
3867 }
3869 void ClassFileParser::verify_legal_method_modifiers(
3870 jint flags, bool is_interface, symbolHandle name, TRAPS) {
3871 if (!_need_verify) { return; }
3873 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3874 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3875 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3876 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3877 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
3878 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
3879 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
3880 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
3881 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
3882 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3883 const bool is_initializer = (name == vmSymbols::object_initializer_name());
3885 bool is_illegal = false;
3887 if (is_interface) {
3888 if (!is_abstract || !is_public || is_static || is_final ||
3889 is_native || (major_gte_15 && (is_synchronized || is_strict))) {
3890 is_illegal = true;
3891 }
3892 } else { // not interface
3893 if (is_initializer) {
3894 if (is_static || is_final || is_synchronized || is_native ||
3895 is_abstract || (major_gte_15 && is_bridge)) {
3896 is_illegal = true;
3897 }
3898 } else { // not initializer
3899 if (is_abstract) {
3900 if ((is_final || is_native || is_private || is_static ||
3901 (major_gte_15 && (is_synchronized || is_strict)))) {
3902 is_illegal = true;
3903 }
3904 }
3905 if (has_illegal_visibility(flags)) {
3906 is_illegal = true;
3907 }
3908 }
3909 }
3911 if (is_illegal) {
3912 ResourceMark rm(THREAD);
3913 Exceptions::fthrow(
3914 THREAD_AND_LOCATION,
3915 vmSymbolHandles::java_lang_ClassFormatError(),
3916 "Method %s in class %s has illegal modifiers: 0x%X",
3917 name->as_C_string(), _class_name->as_C_string(), flags);
3918 return;
3919 }
3920 }
3922 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) {
3923 assert(_need_verify, "only called when _need_verify is true");
3924 int i = 0;
3925 int count = length >> 2;
3926 for (int k=0; k<count; k++) {
3927 unsigned char b0 = buffer[i];
3928 unsigned char b1 = buffer[i+1];
3929 unsigned char b2 = buffer[i+2];
3930 unsigned char b3 = buffer[i+3];
3931 // For an unsigned char v,
3932 // (v | v - 1) is < 128 (highest bit 0) for 0 < v < 128;
3933 // (v | v - 1) is >= 128 (highest bit 1) for v == 0 or v >= 128.
3934 unsigned char res = b0 | b0 - 1 |
3935 b1 | b1 - 1 |
3936 b2 | b2 - 1 |
3937 b3 | b3 - 1;
3938 if (res >= 128) break;
3939 i += 4;
3940 }
3941 for(; i < length; i++) {
3942 unsigned short c;
3943 // no embedded zeros
3944 guarantee_property((buffer[i] != 0), "Illegal UTF8 string in constant pool in class file %s", CHECK);
3945 if(buffer[i] < 128) {
3946 continue;
3947 }
3948 if ((i + 5) < length) { // see if it's legal supplementary character
3949 if (UTF8::is_supplementary_character(&buffer[i])) {
3950 c = UTF8::get_supplementary_character(&buffer[i]);
3951 i += 5;
3952 continue;
3953 }
3954 }
3955 switch (buffer[i] >> 4) {
3956 default: break;
3957 case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
3958 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
3959 case 0xC: case 0xD: // 110xxxxx 10xxxxxx
3960 c = (buffer[i] & 0x1F) << 6;
3961 i++;
3962 if ((i < length) && ((buffer[i] & 0xC0) == 0x80)) {
3963 c += buffer[i] & 0x3F;
3964 if (_major_version <= 47 || c == 0 || c >= 0x80) {
3965 // for classes with major > 47, c must a null or a character in its shortest form
3966 break;
3967 }
3968 }
3969 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
3970 case 0xE: // 1110xxxx 10xxxxxx 10xxxxxx
3971 c = (buffer[i] & 0xF) << 12;
3972 i += 2;
3973 if ((i < length) && ((buffer[i-1] & 0xC0) == 0x80) && ((buffer[i] & 0xC0) == 0x80)) {
3974 c += ((buffer[i-1] & 0x3F) << 6) + (buffer[i] & 0x3F);
3975 if (_major_version <= 47 || c >= 0x800) {
3976 // for classes with major > 47, c must be in its shortest form
3977 break;
3978 }
3979 }
3980 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
3981 } // end of switch
3982 } // end of for
3983 }
3985 // Checks if name is a legal class name.
3986 void ClassFileParser::verify_legal_class_name(symbolHandle name, TRAPS) {
3987 if (!_need_verify || _relax_verify) { return; }
3989 char buf[fixed_buffer_size];
3990 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
3991 unsigned int length = name->utf8_length();
3992 bool legal = false;
3994 if (length > 0) {
3995 char* p;
3996 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
3997 p = skip_over_field_signature(bytes, false, length, CHECK);
3998 legal = (p != NULL) && ((p - bytes) == (int)length);
3999 } else if (_major_version < JAVA_1_5_VERSION) {
4000 if (bytes[0] != '<') {
4001 p = skip_over_field_name(bytes, true, length);
4002 legal = (p != NULL) && ((p - bytes) == (int)length);
4003 }
4004 } else {
4005 // 4900761: relax the constraints based on JSR202 spec
4006 // Class names may be drawn from the entire Unicode character set.
4007 // Identifiers between '/' must be unqualified names.
4008 // The utf8 string has been verified when parsing cpool entries.
4009 legal = verify_unqualified_name(bytes, length, LegalClass);
4010 }
4011 }
4012 if (!legal) {
4013 ResourceMark rm(THREAD);
4014 Exceptions::fthrow(
4015 THREAD_AND_LOCATION,
4016 vmSymbolHandles::java_lang_ClassFormatError(),
4017 "Illegal class name \"%s\" in class file %s", bytes,
4018 _class_name->as_C_string()
4019 );
4020 return;
4021 }
4022 }
4024 // Checks if name is a legal field name.
4025 void ClassFileParser::verify_legal_field_name(symbolHandle name, TRAPS) {
4026 if (!_need_verify || _relax_verify) { return; }
4028 char buf[fixed_buffer_size];
4029 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4030 unsigned int length = name->utf8_length();
4031 bool legal = false;
4033 if (length > 0) {
4034 if (_major_version < JAVA_1_5_VERSION) {
4035 if (bytes[0] != '<') {
4036 char* p = skip_over_field_name(bytes, false, length);
4037 legal = (p != NULL) && ((p - bytes) == (int)length);
4038 }
4039 } else {
4040 // 4881221: relax the constraints based on JSR202 spec
4041 legal = verify_unqualified_name(bytes, length, LegalField);
4042 }
4043 }
4045 if (!legal) {
4046 ResourceMark rm(THREAD);
4047 Exceptions::fthrow(
4048 THREAD_AND_LOCATION,
4049 vmSymbolHandles::java_lang_ClassFormatError(),
4050 "Illegal field name \"%s\" in class %s", bytes,
4051 _class_name->as_C_string()
4052 );
4053 return;
4054 }
4055 }
4057 // Checks if name is a legal method name.
4058 void ClassFileParser::verify_legal_method_name(symbolHandle name, TRAPS) {
4059 if (!_need_verify || _relax_verify) { return; }
4061 assert(!name.is_null(), "method name is null");
4062 char buf[fixed_buffer_size];
4063 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4064 unsigned int length = name->utf8_length();
4065 bool legal = false;
4067 if (length > 0) {
4068 if (bytes[0] == '<') {
4069 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4070 legal = true;
4071 }
4072 } else if (_major_version < JAVA_1_5_VERSION) {
4073 char* p;
4074 p = skip_over_field_name(bytes, false, length);
4075 legal = (p != NULL) && ((p - bytes) == (int)length);
4076 } else {
4077 // 4881221: relax the constraints based on JSR202 spec
4078 legal = verify_unqualified_name(bytes, length, LegalMethod);
4079 }
4080 }
4082 if (!legal) {
4083 ResourceMark rm(THREAD);
4084 Exceptions::fthrow(
4085 THREAD_AND_LOCATION,
4086 vmSymbolHandles::java_lang_ClassFormatError(),
4087 "Illegal method name \"%s\" in class %s", bytes,
4088 _class_name->as_C_string()
4089 );
4090 return;
4091 }
4092 }
4095 // Checks if signature is a legal field signature.
4096 void ClassFileParser::verify_legal_field_signature(symbolHandle name, symbolHandle signature, TRAPS) {
4097 if (!_need_verify) { return; }
4099 char buf[fixed_buffer_size];
4100 char* bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4101 unsigned int length = signature->utf8_length();
4102 char* p = skip_over_field_signature(bytes, false, length, CHECK);
4104 if (p == NULL || (p - bytes) != (int)length) {
4105 throwIllegalSignature("Field", name, signature, CHECK);
4106 }
4107 }
4109 // Checks if signature is a legal method signature.
4110 // Returns number of parameters
4111 int ClassFileParser::verify_legal_method_signature(symbolHandle name, symbolHandle signature, TRAPS) {
4112 if (!_need_verify) {
4113 // make sure caller's args_size will be less than 0 even for non-static
4114 // method so it will be recomputed in compute_size_of_parameters().
4115 return -2;
4116 }
4118 unsigned int args_size = 0;
4119 char buf[fixed_buffer_size];
4120 char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4121 unsigned int length = signature->utf8_length();
4122 char* nextp;
4124 // The first character must be a '('
4125 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {
4126 length--;
4127 // Skip over legal field signatures
4128 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4129 while ((length > 0) && (nextp != NULL)) {
4130 args_size++;
4131 if (p[0] == 'J' || p[0] == 'D') {
4132 args_size++;
4133 }
4134 length -= nextp - p;
4135 p = nextp;
4136 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4137 }
4138 // The first non-signature thing better be a ')'
4139 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4140 length--;
4141 if (name->utf8_length() > 0 && name->byte_at(0) == '<') {
4142 // All internal methods must return void
4143 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
4144 return args_size;
4145 }
4146 } else {
4147 // Now we better just have a return value
4148 nextp = skip_over_field_signature(p, true, length, CHECK_0);
4149 if (nextp && ((int)length == (nextp - p))) {
4150 return args_size;
4151 }
4152 }
4153 }
4154 }
4155 // Report error
4156 throwIllegalSignature("Method", name, signature, CHECK_0);
4157 return 0;
4158 }
4161 // Unqualified names may not contain the characters '.', ';', or '/'.
4162 // Method names also may not contain the characters '<' or '>', unless <init> or <clinit>.
4163 // Note that method names may not be <init> or <clinit> in this method.
4164 // Because these names have been checked as special cases before calling this method
4165 // in verify_legal_method_name.
4166 bool ClassFileParser::verify_unqualified_name(char* name, unsigned int length, int type) {
4167 jchar ch;
4169 for (char* p = name; p != name + length; ) {
4170 ch = *p;
4171 if (ch < 128) {
4172 p++;
4173 if (ch == '.' || ch == ';') {
4174 return false; // do not permit '.' or ';'
4175 }
4176 if (type != LegalClass && ch == '/') {
4177 return false; // do not permit '/' unless it's class name
4178 }
4179 if (type == LegalMethod && (ch == '<' || ch == '>')) {
4180 return false; // do not permit '<' or '>' in method names
4181 }
4182 } else {
4183 char* tmp_p = UTF8::next(p, &ch);
4184 p = tmp_p;
4185 }
4186 }
4187 return true;
4188 }
4191 // Take pointer to a string. Skip over the longest part of the string that could
4192 // be taken as a fieldname. Allow '/' if slash_ok is true.
4193 // Return a pointer to just past the fieldname.
4194 // Return NULL if no fieldname at all was found, or in the case of slash_ok
4195 // being true, we saw consecutive slashes (meaning we were looking for a
4196 // qualified path but found something that was badly-formed).
4197 char* ClassFileParser::skip_over_field_name(char* name, bool slash_ok, unsigned int length) {
4198 char* p;
4199 jchar ch;
4200 jboolean last_is_slash = false;
4201 jboolean not_first_ch = false;
4203 for (p = name; p != name + length; not_first_ch = true) {
4204 char* old_p = p;
4205 ch = *p;
4206 if (ch < 128) {
4207 p++;
4208 // quick check for ascii
4209 if ((ch >= 'a' && ch <= 'z') ||
4210 (ch >= 'A' && ch <= 'Z') ||
4211 (ch == '_' || ch == '$') ||
4212 (not_first_ch && ch >= '0' && ch <= '9')) {
4213 last_is_slash = false;
4214 continue;
4215 }
4216 if (slash_ok && ch == '/') {
4217 if (last_is_slash) {
4218 return NULL; // Don't permit consecutive slashes
4219 }
4220 last_is_slash = true;
4221 continue;
4222 }
4223 } else {
4224 jint unicode_ch;
4225 char* tmp_p = UTF8::next_character(p, &unicode_ch);
4226 p = tmp_p;
4227 last_is_slash = false;
4228 // Check if ch is Java identifier start or is Java identifier part
4229 // 4672820: call java.lang.Character methods directly without generating separate tables.
4230 EXCEPTION_MARK;
4231 instanceKlassHandle klass (THREAD, SystemDictionary::Character_klass());
4233 // return value
4234 JavaValue result(T_BOOLEAN);
4235 // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart
4236 JavaCallArguments args;
4237 args.push_int(unicode_ch);
4239 // public static boolean isJavaIdentifierStart(char ch);
4240 JavaCalls::call_static(&result,
4241 klass,
4242 vmSymbolHandles::isJavaIdentifierStart_name(),
4243 vmSymbolHandles::int_bool_signature(),
4244 &args,
4245 THREAD);
4247 if (HAS_PENDING_EXCEPTION) {
4248 CLEAR_PENDING_EXCEPTION;
4249 return 0;
4250 }
4251 if (result.get_jboolean()) {
4252 continue;
4253 }
4255 if (not_first_ch) {
4256 // public static boolean isJavaIdentifierPart(char ch);
4257 JavaCalls::call_static(&result,
4258 klass,
4259 vmSymbolHandles::isJavaIdentifierPart_name(),
4260 vmSymbolHandles::int_bool_signature(),
4261 &args,
4262 THREAD);
4264 if (HAS_PENDING_EXCEPTION) {
4265 CLEAR_PENDING_EXCEPTION;
4266 return 0;
4267 }
4269 if (result.get_jboolean()) {
4270 continue;
4271 }
4272 }
4273 }
4274 return (not_first_ch) ? old_p : NULL;
4275 }
4276 return (not_first_ch) ? p : NULL;
4277 }
4280 // Take pointer to a string. Skip over the longest part of the string that could
4281 // be taken as a field signature. Allow "void" if void_ok.
4282 // Return a pointer to just past the signature.
4283 // Return NULL if no legal signature is found.
4284 char* ClassFileParser::skip_over_field_signature(char* signature,
4285 bool void_ok,
4286 unsigned int length,
4287 TRAPS) {
4288 unsigned int array_dim = 0;
4289 while (length > 0) {
4290 switch (signature[0]) {
4291 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
4292 case JVM_SIGNATURE_BOOLEAN:
4293 case JVM_SIGNATURE_BYTE:
4294 case JVM_SIGNATURE_CHAR:
4295 case JVM_SIGNATURE_SHORT:
4296 case JVM_SIGNATURE_INT:
4297 case JVM_SIGNATURE_FLOAT:
4298 case JVM_SIGNATURE_LONG:
4299 case JVM_SIGNATURE_DOUBLE:
4300 return signature + 1;
4301 case JVM_SIGNATURE_CLASS: {
4302 if (_major_version < JAVA_1_5_VERSION) {
4303 // Skip over the class name if one is there
4304 char* p = skip_over_field_name(signature + 1, true, --length);
4306 // The next character better be a semicolon
4307 if (p && (p - signature) > 1 && p[0] == ';') {
4308 return p + 1;
4309 }
4310 } else {
4311 // 4900761: For class version > 48, any unicode is allowed in class name.
4312 length--;
4313 signature++;
4314 while (length > 0 && signature[0] != ';') {
4315 if (signature[0] == '.') {
4316 classfile_parse_error("Class name contains illegal character '.' in descriptor in class file %s", CHECK_0);
4317 }
4318 length--;
4319 signature++;
4320 }
4321 if (signature[0] == ';') { return signature + 1; }
4322 }
4324 return NULL;
4325 }
4326 case JVM_SIGNATURE_ARRAY:
4327 array_dim++;
4328 if (array_dim > 255) {
4329 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
4330 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
4331 }
4332 // The rest of what's there better be a legal signature
4333 signature++;
4334 length--;
4335 void_ok = false;
4336 break;
4338 default:
4339 return NULL;
4340 }
4341 }
4342 return NULL;
4343 }