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