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