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