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