Tue, 21 Dec 2010 04:37:30 -0800
7008165: Garbage in ClassFormatError message
Summary: When bootstrap_method_ref in BootstrapMethods attribute points to a wrong CP entry (non-MethodHandle), JVM throws ClassFormatError with a message, where method index and class file name is garbage.
Reviewed-by: iveresov
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 bootstrap_method_index,
2390 CHECK);
2391 operands->short_at_put(operand_fill_index++, bootstrap_method_index);
2392 operands->short_at_put(operand_fill_index++, argument_count);
2394 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]
2395 for (int j = 0; j < argument_count; j++) {
2396 u2 argument_index = cfs->get_u2_fast();
2397 check_property(
2398 valid_cp_range(argument_index, cp_size) &&
2399 cp->tag_at(argument_index).is_loadable_constant(),
2400 "argument_index %u has bad constant type in class file %s",
2401 argument_index,
2402 CHECK);
2403 operands->short_at_put(operand_fill_index++, argument_index);
2404 }
2405 }
2407 assert(operand_fill_index == operands()->length(), "exact fill");
2408 assert(constantPoolOopDesc::operand_array_length(operands()) == attribute_array_length, "correct decode");
2410 u1* current_end = cfs->current();
2411 guarantee_property(current_end == current_start + attribute_byte_length,
2412 "Bad length on BootstrapMethods in class file %s",
2413 CHECK);
2415 cp->set_operands(operands());
2416 }
2419 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2420 ClassFileStream* cfs = stream();
2421 // Set inner classes attribute to default sentinel
2422 k->set_inner_classes(Universe::the_empty_short_array());
2423 cfs->guarantee_more(2, CHECK); // attributes_count
2424 u2 attributes_count = cfs->get_u2_fast();
2425 bool parsed_sourcefile_attribute = false;
2426 bool parsed_innerclasses_attribute = false;
2427 bool parsed_enclosingmethod_attribute = false;
2428 bool parsed_bootstrap_methods_attribute = false;
2429 u1* runtime_visible_annotations = NULL;
2430 int runtime_visible_annotations_length = 0;
2431 u1* runtime_invisible_annotations = NULL;
2432 int runtime_invisible_annotations_length = 0;
2433 // Iterate over attributes
2434 while (attributes_count--) {
2435 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2436 u2 attribute_name_index = cfs->get_u2_fast();
2437 u4 attribute_length = cfs->get_u4_fast();
2438 check_property(
2439 valid_cp_range(attribute_name_index, cp->length()) &&
2440 cp->tag_at(attribute_name_index).is_utf8(),
2441 "Attribute name has bad constant pool index %u in class file %s",
2442 attribute_name_index, CHECK);
2443 symbolOop tag = cp->symbol_at(attribute_name_index);
2444 if (tag == vmSymbols::tag_source_file()) {
2445 // Check for SourceFile tag
2446 if (_need_verify) {
2447 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2448 }
2449 if (parsed_sourcefile_attribute) {
2450 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2451 } else {
2452 parsed_sourcefile_attribute = true;
2453 }
2454 parse_classfile_sourcefile_attribute(cp, k, CHECK);
2455 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2456 // Check for SourceDebugExtension tag
2457 parse_classfile_source_debug_extension_attribute(cp, k, (int)attribute_length, CHECK);
2458 } else if (tag == vmSymbols::tag_inner_classes()) {
2459 // Check for InnerClasses tag
2460 if (parsed_innerclasses_attribute) {
2461 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2462 } else {
2463 parsed_innerclasses_attribute = true;
2464 }
2465 u2 num_of_classes = parse_classfile_inner_classes_attribute(cp, k, CHECK);
2466 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
2467 guarantee_property(attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
2468 "Wrong InnerClasses attribute length in class file %s", CHECK);
2469 }
2470 } else if (tag == vmSymbols::tag_synthetic()) {
2471 // Check for Synthetic tag
2472 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec
2473 if (attribute_length != 0) {
2474 classfile_parse_error(
2475 "Invalid Synthetic classfile attribute length %u in class file %s",
2476 attribute_length, CHECK);
2477 }
2478 parse_classfile_synthetic_attribute(cp, k, CHECK);
2479 } else if (tag == vmSymbols::tag_deprecated()) {
2480 // Check for Deprecatd tag - 4276120
2481 if (attribute_length != 0) {
2482 classfile_parse_error(
2483 "Invalid Deprecated classfile attribute length %u in class file %s",
2484 attribute_length, CHECK);
2485 }
2486 } else if (_major_version >= JAVA_1_5_VERSION) {
2487 if (tag == vmSymbols::tag_signature()) {
2488 if (attribute_length != 2) {
2489 classfile_parse_error(
2490 "Wrong Signature attribute length %u in class file %s",
2491 attribute_length, CHECK);
2492 }
2493 parse_classfile_signature_attribute(cp, k, CHECK);
2494 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2495 runtime_visible_annotations_length = attribute_length;
2496 runtime_visible_annotations = cfs->get_u1_buffer();
2497 assert(runtime_visible_annotations != NULL, "null visible annotations");
2498 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2499 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2500 runtime_invisible_annotations_length = attribute_length;
2501 runtime_invisible_annotations = cfs->get_u1_buffer();
2502 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2503 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
2504 } else if (tag == vmSymbols::tag_enclosing_method()) {
2505 if (parsed_enclosingmethod_attribute) {
2506 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2507 } else {
2508 parsed_enclosingmethod_attribute = true;
2509 }
2510 cfs->guarantee_more(4, CHECK); // class_index, method_index
2511 u2 class_index = cfs->get_u2_fast();
2512 u2 method_index = cfs->get_u2_fast();
2513 if (class_index == 0) {
2514 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2515 }
2516 // Validate the constant pool indices and types
2517 if (!cp->is_within_bounds(class_index) ||
2518 !is_klass_reference(cp, class_index)) {
2519 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2520 }
2521 if (method_index != 0 &&
2522 (!cp->is_within_bounds(method_index) ||
2523 !cp->tag_at(method_index).is_name_and_type())) {
2524 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2525 }
2526 k->set_enclosing_method_indices(class_index, method_index);
2527 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
2528 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2529 if (parsed_bootstrap_methods_attribute)
2530 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2531 parsed_bootstrap_methods_attribute = true;
2532 parse_classfile_bootstrap_methods_attribute(cp, k, attribute_length, CHECK);
2533 } else {
2534 // Unknown attribute
2535 cfs->skip_u1(attribute_length, CHECK);
2536 }
2537 } else {
2538 // Unknown attribute
2539 cfs->skip_u1(attribute_length, CHECK);
2540 }
2541 }
2542 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2543 runtime_visible_annotations_length,
2544 runtime_invisible_annotations,
2545 runtime_invisible_annotations_length,
2546 CHECK);
2547 k->set_class_annotations(annotations());
2549 if (_max_bootstrap_specifier_index >= 0) {
2550 guarantee_property(parsed_bootstrap_methods_attribute,
2551 "Missing BootstrapMethods attribute in class file %s", CHECK);
2552 }
2553 }
2556 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
2557 int runtime_visible_annotations_length,
2558 u1* runtime_invisible_annotations,
2559 int runtime_invisible_annotations_length, TRAPS) {
2560 typeArrayHandle annotations;
2561 if (runtime_visible_annotations != NULL ||
2562 runtime_invisible_annotations != NULL) {
2563 typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2564 runtime_invisible_annotations_length, CHECK_(annotations));
2565 annotations = typeArrayHandle(THREAD, anno);
2566 if (runtime_visible_annotations != NULL) {
2567 memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2568 }
2569 if (runtime_invisible_annotations != NULL) {
2570 memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2571 }
2572 }
2573 return annotations;
2574 }
2577 static void initialize_static_field(fieldDescriptor* fd, TRAPS) {
2578 KlassHandle h_k (THREAD, fd->field_holder());
2579 assert(h_k.not_null() && fd->is_static(), "just checking");
2580 if (fd->has_initial_value()) {
2581 BasicType t = fd->field_type();
2582 switch (t) {
2583 case T_BYTE:
2584 h_k()->byte_field_put(fd->offset(), fd->int_initial_value());
2585 break;
2586 case T_BOOLEAN:
2587 h_k()->bool_field_put(fd->offset(), fd->int_initial_value());
2588 break;
2589 case T_CHAR:
2590 h_k()->char_field_put(fd->offset(), fd->int_initial_value());
2591 break;
2592 case T_SHORT:
2593 h_k()->short_field_put(fd->offset(), fd->int_initial_value());
2594 break;
2595 case T_INT:
2596 h_k()->int_field_put(fd->offset(), fd->int_initial_value());
2597 break;
2598 case T_FLOAT:
2599 h_k()->float_field_put(fd->offset(), fd->float_initial_value());
2600 break;
2601 case T_DOUBLE:
2602 h_k()->double_field_put(fd->offset(), fd->double_initial_value());
2603 break;
2604 case T_LONG:
2605 h_k()->long_field_put(fd->offset(), fd->long_initial_value());
2606 break;
2607 case T_OBJECT:
2608 {
2609 #ifdef ASSERT
2610 symbolOop sym = oopFactory::new_symbol("Ljava/lang/String;", CHECK);
2611 assert(fd->signature() == sym, "just checking");
2612 #endif
2613 oop string = fd->string_initial_value(CHECK);
2614 h_k()->obj_field_put(fd->offset(), string);
2615 }
2616 break;
2617 default:
2618 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
2619 "Illegal ConstantValue attribute in class file");
2620 }
2621 }
2622 }
2625 void ClassFileParser::java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
2626 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS) {
2627 // This code is for compatibility with earlier jdk's that do not
2628 // have the "discovered" field in java.lang.ref.Reference. For 1.5
2629 // the check for the "discovered" field should issue a warning if
2630 // the field is not found. For 1.6 this code should be issue a
2631 // fatal error if the "discovered" field is not found.
2632 //
2633 // Increment fac.nonstatic_oop_count so that the start of the
2634 // next type of non-static oops leaves room for the fake oop.
2635 // Do not increment next_nonstatic_oop_offset so that the
2636 // fake oop is place after the java.lang.ref.Reference oop
2637 // fields.
2638 //
2639 // Check the fields in java.lang.ref.Reference for the "discovered"
2640 // field. If it is not present, artifically create a field for it.
2641 // This allows this VM to run on early JDK where the field is not
2642 // present.
2643 int reference_sig_index = 0;
2644 int reference_name_index = 0;
2645 int reference_index = 0;
2646 int extra = java_lang_ref_Reference::number_of_fake_oop_fields;
2647 const int n = (*fields_ptr)()->length();
2648 for (int i = 0; i < n; i += instanceKlass::next_offset ) {
2649 int name_index =
2650 (*fields_ptr)()->ushort_at(i + instanceKlass::name_index_offset);
2651 int sig_index =
2652 (*fields_ptr)()->ushort_at(i + instanceKlass::signature_index_offset);
2653 symbolOop f_name = cp->symbol_at(name_index);
2654 symbolOop f_sig = cp->symbol_at(sig_index);
2655 if (f_sig == vmSymbols::reference_signature() && reference_index == 0) {
2656 // Save the index for reference signature for later use.
2657 // The fake discovered field does not entries in the
2658 // constant pool so the index for its signature cannot
2659 // be extracted from the constant pool. It will need
2660 // later, however. It's signature is vmSymbols::reference_signature()
2661 // so same an index for that signature.
2662 reference_sig_index = sig_index;
2663 reference_name_index = name_index;
2664 reference_index = i;
2665 }
2666 if (f_name == vmSymbols::reference_discovered_name() &&
2667 f_sig == vmSymbols::reference_signature()) {
2668 // The values below are fake but will force extra
2669 // non-static oop fields and a corresponding non-static
2670 // oop map block to be allocated.
2671 extra = 0;
2672 break;
2673 }
2674 }
2675 if (extra != 0) {
2676 fac_ptr->nonstatic_oop_count += extra;
2677 // Add the additional entry to "fields" so that the klass
2678 // contains the "discoverd" field and the field will be initialized
2679 // in instances of the object.
2680 int fields_with_fix_length = (*fields_ptr)()->length() +
2681 instanceKlass::next_offset;
2682 typeArrayOop ff = oopFactory::new_permanent_shortArray(
2683 fields_with_fix_length, CHECK);
2684 typeArrayHandle fields_with_fix(THREAD, ff);
2686 // Take everything from the original but the length.
2687 for (int idx = 0; idx < (*fields_ptr)->length(); idx++) {
2688 fields_with_fix->ushort_at_put(idx, (*fields_ptr)->ushort_at(idx));
2689 }
2691 // Add the fake field at the end.
2692 int i = (*fields_ptr)->length();
2693 // There is no name index for the fake "discovered" field nor
2694 // signature but a signature is needed so that the field will
2695 // be properly initialized. Use one found for
2696 // one of the other reference fields. Be sure the index for the
2697 // name is 0. In fieldDescriptor::initialize() the index of the
2698 // name is checked. That check is by passed for the last nonstatic
2699 // oop field in a java.lang.ref.Reference which is assumed to be
2700 // this artificial "discovered" field. An assertion checks that
2701 // the name index is 0.
2702 assert(reference_index != 0, "Missing signature for reference");
2704 int j;
2705 for (j = 0; j < instanceKlass::next_offset; j++) {
2706 fields_with_fix->ushort_at_put(i + j,
2707 (*fields_ptr)->ushort_at(reference_index +j));
2708 }
2709 // Clear the public access flag and set the private access flag.
2710 short flags;
2711 flags =
2712 fields_with_fix->ushort_at(i + instanceKlass::access_flags_offset);
2713 assert(!(flags & JVM_RECOGNIZED_FIELD_MODIFIERS), "Unexpected access flags set");
2714 flags = flags & (~JVM_ACC_PUBLIC);
2715 flags = flags | JVM_ACC_PRIVATE;
2716 AccessFlags access_flags;
2717 access_flags.set_flags(flags);
2718 assert(!access_flags.is_public(), "Failed to clear public flag");
2719 assert(access_flags.is_private(), "Failed to set private flag");
2720 fields_with_fix->ushort_at_put(i + instanceKlass::access_flags_offset,
2721 flags);
2723 assert(fields_with_fix->ushort_at(i + instanceKlass::name_index_offset)
2724 == reference_name_index, "The fake reference name is incorrect");
2725 assert(fields_with_fix->ushort_at(i + instanceKlass::signature_index_offset)
2726 == reference_sig_index, "The fake reference signature is incorrect");
2727 // The type of the field is stored in the low_offset entry during
2728 // parsing.
2729 assert(fields_with_fix->ushort_at(i + instanceKlass::low_offset) ==
2730 NONSTATIC_OOP, "The fake reference type is incorrect");
2732 // "fields" is allocated in the permanent generation. Disgard
2733 // it and let it be collected.
2734 (*fields_ptr) = fields_with_fix;
2735 }
2736 return;
2737 }
2740 void ClassFileParser::java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
2741 FieldAllocationCount *fac_ptr, TRAPS) {
2742 // Add fake fields for java.lang.Class instances
2743 //
2744 // This is not particularly nice. We should consider adding a
2745 // private transient object field at the Java level to
2746 // java.lang.Class. Alternatively we could add a subclass of
2747 // instanceKlass which provides an accessor and size computer for
2748 // this field, but that appears to be more code than this hack.
2749 //
2750 // NOTE that we wedge these in at the beginning rather than the
2751 // end of the object because the Class layout changed between JDK
2752 // 1.3 and JDK 1.4 with the new reflection implementation; some
2753 // nonstatic oop fields were added at the Java level. The offsets
2754 // of these fake fields can't change between these two JDK
2755 // versions because when the offsets are computed at bootstrap
2756 // time we don't know yet which version of the JDK we're running in.
2758 // The values below are fake but will force two non-static oop fields and
2759 // a corresponding non-static oop map block to be allocated.
2760 const int extra = java_lang_Class::number_of_fake_oop_fields;
2761 fac_ptr->nonstatic_oop_count += extra;
2762 }
2765 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2766 // Cause the extra fake fields in java.lang.Class to show up before
2767 // the Java fields for layout compatibility between 1.3 and 1.4
2768 // Incrementing next_nonstatic_oop_offset here advances the
2769 // location where the real java fields are placed.
2770 const int extra = java_lang_Class::number_of_fake_oop_fields;
2771 (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2772 }
2775 // Force MethodHandle.vmentry to be an unmanaged pointer.
2776 // There is no way for a classfile to express this, so we must help it.
2777 void ClassFileParser::java_dyn_MethodHandle_fix_pre(constantPoolHandle cp,
2778 typeArrayHandle fields,
2779 FieldAllocationCount *fac_ptr,
2780 TRAPS) {
2781 // Add fake fields for java.dyn.MethodHandle instances
2782 //
2783 // This is not particularly nice, but since there is no way to express
2784 // a native wordSize field in Java, we must do it at this level.
2786 if (!EnableMethodHandles) return;
2788 int word_sig_index = 0;
2789 const int cp_size = cp->length();
2790 for (int index = 1; index < cp_size; index++) {
2791 if (cp->tag_at(index).is_utf8() &&
2792 cp->symbol_at(index) == vmSymbols::machine_word_signature()) {
2793 word_sig_index = index;
2794 break;
2795 }
2796 }
2798 if (word_sig_index == 0)
2799 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2800 "missing I or J signature (for vmentry) in java.dyn.MethodHandle");
2802 // Find vmentry field and change the signature.
2803 bool found_vmentry = false;
2804 for (int i = 0; i < fields->length(); i += instanceKlass::next_offset) {
2805 int name_index = fields->ushort_at(i + instanceKlass::name_index_offset);
2806 int sig_index = fields->ushort_at(i + instanceKlass::signature_index_offset);
2807 int acc_flags = fields->ushort_at(i + instanceKlass::access_flags_offset);
2808 symbolOop f_name = cp->symbol_at(name_index);
2809 symbolOop f_sig = cp->symbol_at(sig_index);
2811 if (f_name == vmSymbols::vmentry_name() && (acc_flags & JVM_ACC_STATIC) == 0) {
2812 if (f_sig == vmSymbols::machine_word_signature()) {
2813 // If the signature of vmentry is already changed, we're done.
2814 found_vmentry = true;
2815 break;
2816 }
2817 else if (f_sig == vmSymbols::byte_signature()) {
2818 // Adjust the field type from byte to an unmanaged pointer.
2819 assert(fac_ptr->nonstatic_byte_count > 0, "");
2820 fac_ptr->nonstatic_byte_count -= 1;
2822 fields->ushort_at_put(i + instanceKlass::signature_index_offset, word_sig_index);
2823 assert(wordSize == longSize || wordSize == jintSize, "ILP32 or LP64");
2824 if (wordSize == longSize) fac_ptr->nonstatic_double_count += 1;
2825 else fac_ptr->nonstatic_word_count += 1;
2827 FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i + instanceKlass::low_offset);
2828 assert(atype == NONSTATIC_BYTE, "");
2829 FieldAllocationType new_atype = (wordSize == longSize) ? NONSTATIC_DOUBLE : NONSTATIC_WORD;
2830 fields->ushort_at_put(i + instanceKlass::low_offset, new_atype);
2832 found_vmentry = true;
2833 break;
2834 }
2835 }
2836 }
2838 if (!found_vmentry)
2839 THROW_MSG(vmSymbols::java_lang_VirtualMachineError(),
2840 "missing vmentry byte field in java.dyn.MethodHandle");
2841 }
2844 instanceKlassHandle ClassFileParser::parseClassFile(symbolHandle name,
2845 Handle class_loader,
2846 Handle protection_domain,
2847 KlassHandle host_klass,
2848 GrowableArray<Handle>* cp_patches,
2849 symbolHandle& parsed_name,
2850 bool verify,
2851 TRAPS) {
2852 // So that JVMTI can cache class file in the state before retransformable agents
2853 // have modified it
2854 unsigned char *cached_class_file_bytes = NULL;
2855 jint cached_class_file_length;
2857 ClassFileStream* cfs = stream();
2858 // Timing
2859 assert(THREAD->is_Java_thread(), "must be a JavaThread");
2860 JavaThread* jt = (JavaThread*) THREAD;
2862 PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(),
2863 ClassLoader::perf_class_parse_selftime(),
2864 NULL,
2865 jt->get_thread_stat()->perf_recursion_counts_addr(),
2866 jt->get_thread_stat()->perf_timers_addr(),
2867 PerfClassTraceTime::PARSE_CLASS);
2869 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2870 _max_bootstrap_specifier_index = -1;
2872 if (JvmtiExport::should_post_class_file_load_hook()) {
2873 unsigned char* ptr = cfs->buffer();
2874 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2876 JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain,
2877 &ptr, &end_ptr,
2878 &cached_class_file_bytes,
2879 &cached_class_file_length);
2881 if (ptr != cfs->buffer()) {
2882 // JVMTI agent has modified class file data.
2883 // Set new class file stream using JVMTI agent modified
2884 // class file data.
2885 cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source());
2886 set_stream(cfs);
2887 }
2888 }
2890 _host_klass = host_klass;
2891 _cp_patches = cp_patches;
2893 instanceKlassHandle nullHandle;
2895 // Figure out whether we can skip format checking (matching classic VM behavior)
2896 _need_verify = Verifier::should_verify_for(class_loader(), verify);
2898 // Set the verify flag in stream
2899 cfs->set_verify(_need_verify);
2901 // Save the class file name for easier error message printing.
2902 _class_name = name.not_null()? name : vmSymbolHandles::unknown_class_name();
2904 cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor
2905 // Magic value
2906 u4 magic = cfs->get_u4_fast();
2907 guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
2908 "Incompatible magic value %u in class file %s",
2909 magic, CHECK_(nullHandle));
2911 // Version numbers
2912 u2 minor_version = cfs->get_u2_fast();
2913 u2 major_version = cfs->get_u2_fast();
2915 // Check version numbers - we check this even with verifier off
2916 if (!is_supported_version(major_version, minor_version)) {
2917 if (name.is_null()) {
2918 Exceptions::fthrow(
2919 THREAD_AND_LOCATION,
2920 vmSymbolHandles::java_lang_UnsupportedClassVersionError(),
2921 "Unsupported major.minor version %u.%u",
2922 major_version,
2923 minor_version);
2924 } else {
2925 ResourceMark rm(THREAD);
2926 Exceptions::fthrow(
2927 THREAD_AND_LOCATION,
2928 vmSymbolHandles::java_lang_UnsupportedClassVersionError(),
2929 "%s : Unsupported major.minor version %u.%u",
2930 name->as_C_string(),
2931 major_version,
2932 minor_version);
2933 }
2934 return nullHandle;
2935 }
2937 _major_version = major_version;
2938 _minor_version = minor_version;
2941 // Check if verification needs to be relaxed for this class file
2942 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
2943 _relax_verify = Verifier::relax_verify_for(class_loader());
2945 // Constant pool
2946 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
2947 int cp_size = cp->length();
2949 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
2951 // Access flags
2952 AccessFlags access_flags;
2953 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
2955 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2956 // Set abstract bit for old class files for backward compatibility
2957 flags |= JVM_ACC_ABSTRACT;
2958 }
2959 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
2960 access_flags.set_flags(flags);
2962 // This class and superclass
2963 instanceKlassHandle super_klass;
2964 u2 this_class_index = cfs->get_u2_fast();
2965 check_property(
2966 valid_cp_range(this_class_index, cp_size) &&
2967 cp->tag_at(this_class_index).is_unresolved_klass(),
2968 "Invalid this class index %u in constant pool in class file %s",
2969 this_class_index, CHECK_(nullHandle));
2971 symbolHandle class_name (THREAD, cp->unresolved_klass_at(this_class_index));
2972 assert(class_name.not_null(), "class_name can't be null");
2974 // It's important to set parsed_name *before* resolving the super class.
2975 // (it's used for cleanup by the caller if parsing fails)
2976 parsed_name = class_name;
2978 // Update _class_name which could be null previously to be class_name
2979 _class_name = class_name;
2981 // Don't need to check whether this class name is legal or not.
2982 // It has been checked when constant pool is parsed.
2983 // However, make sure it is not an array type.
2984 if (_need_verify) {
2985 guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY,
2986 "Bad class name in class file %s",
2987 CHECK_(nullHandle));
2988 }
2990 klassOop preserve_this_klass; // for storing result across HandleMark
2992 // release all handles when parsing is done
2993 { HandleMark hm(THREAD);
2995 // Checks if name in class file matches requested name
2996 if (name.not_null() && class_name() != name()) {
2997 ResourceMark rm(THREAD);
2998 Exceptions::fthrow(
2999 THREAD_AND_LOCATION,
3000 vmSymbolHandles::java_lang_NoClassDefFoundError(),
3001 "%s (wrong name: %s)",
3002 name->as_C_string(),
3003 class_name->as_C_string()
3004 );
3005 return nullHandle;
3006 }
3008 if (TraceClassLoadingPreorder) {
3009 tty->print("[Loading %s", name()->as_klass_external_name());
3010 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
3011 tty->print_cr("]");
3012 }
3014 u2 super_class_index = cfs->get_u2_fast();
3015 if (super_class_index == 0) {
3016 check_property(class_name() == vmSymbols::java_lang_Object(),
3017 "Invalid superclass index %u in class file %s",
3018 super_class_index,
3019 CHECK_(nullHandle));
3020 } else {
3021 check_property(valid_cp_range(super_class_index, cp_size) &&
3022 is_klass_reference(cp, super_class_index),
3023 "Invalid superclass index %u in class file %s",
3024 super_class_index,
3025 CHECK_(nullHandle));
3026 // The class name should be legal because it is checked when parsing constant pool.
3027 // However, make sure it is not an array type.
3028 bool is_array = false;
3029 if (cp->tag_at(super_class_index).is_klass()) {
3030 super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index));
3031 if (_need_verify)
3032 is_array = super_klass->oop_is_array();
3033 } else if (_need_verify) {
3034 is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
3035 }
3036 if (_need_verify) {
3037 guarantee_property(!is_array,
3038 "Bad superclass name in class file %s", CHECK_(nullHandle));
3039 }
3040 }
3042 // Interfaces
3043 u2 itfs_len = cfs->get_u2_fast();
3044 objArrayHandle local_interfaces;
3045 if (itfs_len == 0) {
3046 local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
3047 } else {
3048 local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle));
3049 }
3051 // Fields (offsets are filled in later)
3052 struct FieldAllocationCount fac = {0,0,0,0,0,0,0,0,0,0};
3053 objArrayHandle fields_annotations;
3054 typeArrayHandle fields = parse_fields(cp, access_flags.is_interface(), &fac, &fields_annotations, CHECK_(nullHandle));
3055 // Methods
3056 bool has_final_method = false;
3057 AccessFlags promoted_flags;
3058 promoted_flags.set_flags(0);
3059 // These need to be oop pointers because they are allocated lazily
3060 // inside parse_methods inside a nested HandleMark
3061 objArrayOop methods_annotations_oop = NULL;
3062 objArrayOop methods_parameter_annotations_oop = NULL;
3063 objArrayOop methods_default_annotations_oop = NULL;
3064 objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
3065 &promoted_flags,
3066 &has_final_method,
3067 &methods_annotations_oop,
3068 &methods_parameter_annotations_oop,
3069 &methods_default_annotations_oop,
3070 CHECK_(nullHandle));
3072 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
3073 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
3074 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
3076 // We check super class after class file is parsed and format is checked
3077 if (super_class_index > 0 && super_klass.is_null()) {
3078 symbolHandle sk (THREAD, cp->klass_name_at(super_class_index));
3079 if (access_flags.is_interface()) {
3080 // Before attempting to resolve the superclass, check for class format
3081 // errors not checked yet.
3082 guarantee_property(sk() == vmSymbols::java_lang_Object(),
3083 "Interfaces must have java.lang.Object as superclass in class file %s",
3084 CHECK_(nullHandle));
3085 }
3086 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
3087 sk,
3088 class_loader,
3089 protection_domain,
3090 true,
3091 CHECK_(nullHandle));
3093 KlassHandle kh (THREAD, k);
3094 super_klass = instanceKlassHandle(THREAD, kh());
3095 if (LinkWellKnownClasses) // my super class is well known to me
3096 cp->klass_at_put(super_class_index, super_klass()); // eagerly resolve
3097 }
3098 if (super_klass.not_null()) {
3099 if (super_klass->is_interface()) {
3100 ResourceMark rm(THREAD);
3101 Exceptions::fthrow(
3102 THREAD_AND_LOCATION,
3103 vmSymbolHandles::java_lang_IncompatibleClassChangeError(),
3104 "class %s has interface %s as super class",
3105 class_name->as_klass_external_name(),
3106 super_klass->external_name()
3107 );
3108 return nullHandle;
3109 }
3110 // Make sure super class is not final
3111 if (super_klass->is_final()) {
3112 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
3113 }
3114 }
3116 // Compute the transitive list of all unique interfaces implemented by this class
3117 objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
3119 // sort methods
3120 typeArrayHandle method_ordering = sort_methods(methods,
3121 methods_annotations,
3122 methods_parameter_annotations,
3123 methods_default_annotations,
3124 CHECK_(nullHandle));
3126 // promote flags from parse_methods() to the klass' flags
3127 access_flags.add_promoted_flags(promoted_flags.as_int());
3129 // Size of Java vtable (in words)
3130 int vtable_size = 0;
3131 int itable_size = 0;
3132 int num_miranda_methods = 0;
3134 klassVtable::compute_vtable_size_and_num_mirandas(vtable_size,
3135 num_miranda_methods,
3136 super_klass(),
3137 methods(),
3138 access_flags,
3139 class_loader,
3140 class_name,
3141 local_interfaces(),
3142 CHECK_(nullHandle));
3144 // Size of Java itable (in words)
3145 itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(transitive_interfaces);
3147 // Field size and offset computation
3148 int nonstatic_field_size = super_klass() == NULL ? 0 : super_klass->nonstatic_field_size();
3149 #ifndef PRODUCT
3150 int orig_nonstatic_field_size = 0;
3151 #endif
3152 int static_field_size = 0;
3153 int next_static_oop_offset;
3154 int next_static_double_offset;
3155 int next_static_word_offset;
3156 int next_static_short_offset;
3157 int next_static_byte_offset;
3158 int next_static_type_offset;
3159 int next_nonstatic_oop_offset;
3160 int next_nonstatic_double_offset;
3161 int next_nonstatic_word_offset;
3162 int next_nonstatic_short_offset;
3163 int next_nonstatic_byte_offset;
3164 int next_nonstatic_type_offset;
3165 int first_nonstatic_oop_offset;
3166 int first_nonstatic_field_offset;
3167 int next_nonstatic_field_offset;
3169 // Calculate the starting byte offsets
3170 next_static_oop_offset = (instanceKlass::header_size() +
3171 align_object_offset(vtable_size) +
3172 align_object_offset(itable_size)) * wordSize;
3173 next_static_double_offset = next_static_oop_offset +
3174 (fac.static_oop_count * heapOopSize);
3175 if ( fac.static_double_count &&
3176 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3177 Universe::field_type_should_be_aligned(T_LONG)) ) {
3178 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3179 }
3181 next_static_word_offset = next_static_double_offset +
3182 (fac.static_double_count * BytesPerLong);
3183 next_static_short_offset = next_static_word_offset +
3184 (fac.static_word_count * BytesPerInt);
3185 next_static_byte_offset = next_static_short_offset +
3186 (fac.static_short_count * BytesPerShort);
3187 next_static_type_offset = align_size_up((next_static_byte_offset +
3188 fac.static_byte_count ), wordSize );
3189 static_field_size = (next_static_type_offset -
3190 next_static_oop_offset) / wordSize;
3191 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3192 nonstatic_field_size * heapOopSize;
3193 next_nonstatic_field_offset = first_nonstatic_field_offset;
3195 // Add fake fields for java.lang.Class instances (also see below)
3196 if (class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3197 java_lang_Class_fix_pre(&methods, &fac, CHECK_(nullHandle));
3198 }
3200 // adjust the vmentry field declaration in java.dyn.MethodHandle
3201 if (EnableMethodHandles && class_name() == vmSymbols::sun_dyn_MethodHandleImpl() && class_loader.is_null()) {
3202 java_dyn_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3203 }
3205 // Add a fake "discovered" field if it is not present
3206 // for compatibility with earlier jdk's.
3207 if (class_name() == vmSymbols::java_lang_ref_Reference()
3208 && class_loader.is_null()) {
3209 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
3210 }
3211 // end of "discovered" field compactibility fix
3213 unsigned int nonstatic_double_count = fac.nonstatic_double_count;
3214 unsigned int nonstatic_word_count = fac.nonstatic_word_count;
3215 unsigned int nonstatic_short_count = fac.nonstatic_short_count;
3216 unsigned int nonstatic_byte_count = fac.nonstatic_byte_count;
3217 unsigned int nonstatic_oop_count = fac.nonstatic_oop_count;
3219 bool super_has_nonstatic_fields =
3220 (super_klass() != NULL && super_klass->has_nonstatic_fields());
3221 bool has_nonstatic_fields = super_has_nonstatic_fields ||
3222 ((nonstatic_double_count + nonstatic_word_count +
3223 nonstatic_short_count + nonstatic_byte_count +
3224 nonstatic_oop_count) != 0);
3227 // Prepare list of oops for oop map generation.
3228 int* nonstatic_oop_offsets;
3229 unsigned int* nonstatic_oop_counts;
3230 unsigned int nonstatic_oop_map_count = 0;
3232 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3233 THREAD, int, nonstatic_oop_count + 1);
3234 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3235 THREAD, unsigned int, nonstatic_oop_count + 1);
3237 // Add fake fields for java.lang.Class instances (also see above).
3238 // FieldsAllocationStyle and CompactFields values will be reset to default.
3239 if(class_name() == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3240 java_lang_Class_fix_post(&next_nonstatic_field_offset);
3241 nonstatic_oop_offsets[0] = first_nonstatic_field_offset;
3242 const uint fake_oop_count = (next_nonstatic_field_offset -
3243 first_nonstatic_field_offset) / heapOopSize;
3244 nonstatic_oop_counts[0] = fake_oop_count;
3245 nonstatic_oop_map_count = 1;
3246 nonstatic_oop_count -= fake_oop_count;
3247 first_nonstatic_oop_offset = first_nonstatic_field_offset;
3248 } else {
3249 first_nonstatic_oop_offset = 0; // will be set for first oop field
3250 }
3252 #ifndef PRODUCT
3253 if( PrintCompactFieldsSavings ) {
3254 next_nonstatic_double_offset = next_nonstatic_field_offset +
3255 (nonstatic_oop_count * heapOopSize);
3256 if ( nonstatic_double_count > 0 ) {
3257 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3258 }
3259 next_nonstatic_word_offset = next_nonstatic_double_offset +
3260 (nonstatic_double_count * BytesPerLong);
3261 next_nonstatic_short_offset = next_nonstatic_word_offset +
3262 (nonstatic_word_count * BytesPerInt);
3263 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3264 (nonstatic_short_count * BytesPerShort);
3265 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
3266 nonstatic_byte_count ), heapOopSize );
3267 orig_nonstatic_field_size = nonstatic_field_size +
3268 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3269 }
3270 #endif
3271 bool compact_fields = CompactFields;
3272 int allocation_style = FieldsAllocationStyle;
3273 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3274 assert(false, "0 <= FieldsAllocationStyle <= 2");
3275 allocation_style = 1; // Optimistic
3276 }
3278 // The next classes have predefined hard-coded fields offsets
3279 // (see in JavaClasses::compute_hard_coded_offsets()).
3280 // Use default fields allocation order for them.
3281 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3282 (class_name() == vmSymbols::java_lang_AssertionStatusDirectives() ||
3283 class_name() == vmSymbols::java_lang_Class() ||
3284 class_name() == vmSymbols::java_lang_ClassLoader() ||
3285 class_name() == vmSymbols::java_lang_ref_Reference() ||
3286 class_name() == vmSymbols::java_lang_ref_SoftReference() ||
3287 class_name() == vmSymbols::java_lang_StackTraceElement() ||
3288 class_name() == vmSymbols::java_lang_String() ||
3289 class_name() == vmSymbols::java_lang_Throwable() ||
3290 class_name() == vmSymbols::java_lang_Boolean() ||
3291 class_name() == vmSymbols::java_lang_Character() ||
3292 class_name() == vmSymbols::java_lang_Float() ||
3293 class_name() == vmSymbols::java_lang_Double() ||
3294 class_name() == vmSymbols::java_lang_Byte() ||
3295 class_name() == vmSymbols::java_lang_Short() ||
3296 class_name() == vmSymbols::java_lang_Integer() ||
3297 class_name() == vmSymbols::java_lang_Long())) {
3298 allocation_style = 0; // Allocate oops first
3299 compact_fields = false; // Don't compact fields
3300 }
3302 if( allocation_style == 0 ) {
3303 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes
3304 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3305 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3306 (nonstatic_oop_count * heapOopSize);
3307 } else if( allocation_style == 1 ) {
3308 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops
3309 next_nonstatic_double_offset = next_nonstatic_field_offset;
3310 } else if( allocation_style == 2 ) {
3311 // Fields allocation: oops fields in super and sub classes are together.
3312 if( nonstatic_field_size > 0 && super_klass() != NULL &&
3313 super_klass->nonstatic_oop_map_size() > 0 ) {
3314 int map_size = super_klass->nonstatic_oop_map_size();
3315 OopMapBlock* first_map = super_klass->start_of_nonstatic_oop_maps();
3316 OopMapBlock* last_map = first_map + map_size - 1;
3317 int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3318 if (next_offset == next_nonstatic_field_offset) {
3319 allocation_style = 0; // allocate oops first
3320 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3321 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3322 (nonstatic_oop_count * heapOopSize);
3323 }
3324 }
3325 if( allocation_style == 2 ) {
3326 allocation_style = 1; // allocate oops last
3327 next_nonstatic_double_offset = next_nonstatic_field_offset;
3328 }
3329 } else {
3330 ShouldNotReachHere();
3331 }
3333 int nonstatic_oop_space_count = 0;
3334 int nonstatic_word_space_count = 0;
3335 int nonstatic_short_space_count = 0;
3336 int nonstatic_byte_space_count = 0;
3337 int nonstatic_oop_space_offset;
3338 int nonstatic_word_space_offset;
3339 int nonstatic_short_space_offset;
3340 int nonstatic_byte_space_offset;
3342 if( nonstatic_double_count > 0 ) {
3343 int offset = next_nonstatic_double_offset;
3344 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
3345 if( compact_fields && offset != next_nonstatic_double_offset ) {
3346 // Allocate available fields into the gap before double field.
3347 int length = next_nonstatic_double_offset - offset;
3348 assert(length == BytesPerInt, "");
3349 nonstatic_word_space_offset = offset;
3350 if( nonstatic_word_count > 0 ) {
3351 nonstatic_word_count -= 1;
3352 nonstatic_word_space_count = 1; // Only one will fit
3353 length -= BytesPerInt;
3354 offset += BytesPerInt;
3355 }
3356 nonstatic_short_space_offset = offset;
3357 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
3358 nonstatic_short_count -= 1;
3359 nonstatic_short_space_count += 1;
3360 length -= BytesPerShort;
3361 offset += BytesPerShort;
3362 }
3363 nonstatic_byte_space_offset = offset;
3364 while( length > 0 && nonstatic_byte_count > 0 ) {
3365 nonstatic_byte_count -= 1;
3366 nonstatic_byte_space_count += 1;
3367 length -= 1;
3368 }
3369 // Allocate oop field in the gap if there are no other fields for that.
3370 nonstatic_oop_space_offset = offset;
3371 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
3372 allocation_style != 0 ) { // when oop fields not first
3373 nonstatic_oop_count -= 1;
3374 nonstatic_oop_space_count = 1; // Only one will fit
3375 length -= heapOopSize;
3376 offset += heapOopSize;
3377 }
3378 }
3379 }
3381 next_nonstatic_word_offset = next_nonstatic_double_offset +
3382 (nonstatic_double_count * BytesPerLong);
3383 next_nonstatic_short_offset = next_nonstatic_word_offset +
3384 (nonstatic_word_count * BytesPerInt);
3385 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3386 (nonstatic_short_count * BytesPerShort);
3388 int notaligned_offset;
3389 if( allocation_style == 0 ) {
3390 notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3391 } else { // allocation_style == 1
3392 next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3393 if( nonstatic_oop_count > 0 ) {
3394 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3395 }
3396 notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3397 }
3398 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3399 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3400 - first_nonstatic_field_offset)/heapOopSize);
3402 // Iterate over fields again and compute correct offsets.
3403 // The field allocation type was temporarily stored in the offset slot.
3404 // oop fields are located before non-oop fields (static and non-static).
3405 int len = fields->length();
3406 for (int i = 0; i < len; i += instanceKlass::next_offset) {
3407 int real_offset;
3408 FieldAllocationType atype = (FieldAllocationType) fields->ushort_at(i + instanceKlass::low_offset);
3409 switch (atype) {
3410 case STATIC_OOP:
3411 real_offset = next_static_oop_offset;
3412 next_static_oop_offset += heapOopSize;
3413 break;
3414 case STATIC_BYTE:
3415 real_offset = next_static_byte_offset;
3416 next_static_byte_offset += 1;
3417 break;
3418 case STATIC_SHORT:
3419 real_offset = next_static_short_offset;
3420 next_static_short_offset += BytesPerShort;
3421 break;
3422 case STATIC_WORD:
3423 real_offset = next_static_word_offset;
3424 next_static_word_offset += BytesPerInt;
3425 break;
3426 case STATIC_ALIGNED_DOUBLE:
3427 case STATIC_DOUBLE:
3428 real_offset = next_static_double_offset;
3429 next_static_double_offset += BytesPerLong;
3430 break;
3431 case NONSTATIC_OOP:
3432 if( nonstatic_oop_space_count > 0 ) {
3433 real_offset = nonstatic_oop_space_offset;
3434 nonstatic_oop_space_offset += heapOopSize;
3435 nonstatic_oop_space_count -= 1;
3436 } else {
3437 real_offset = next_nonstatic_oop_offset;
3438 next_nonstatic_oop_offset += heapOopSize;
3439 }
3440 // Update oop maps
3441 if( nonstatic_oop_map_count > 0 &&
3442 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3443 real_offset -
3444 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3445 heapOopSize ) {
3446 // Extend current oop map
3447 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
3448 } else {
3449 // Create new oop map
3450 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3451 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3452 nonstatic_oop_map_count += 1;
3453 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3454 first_nonstatic_oop_offset = real_offset;
3455 }
3456 }
3457 break;
3458 case NONSTATIC_BYTE:
3459 if( nonstatic_byte_space_count > 0 ) {
3460 real_offset = nonstatic_byte_space_offset;
3461 nonstatic_byte_space_offset += 1;
3462 nonstatic_byte_space_count -= 1;
3463 } else {
3464 real_offset = next_nonstatic_byte_offset;
3465 next_nonstatic_byte_offset += 1;
3466 }
3467 break;
3468 case NONSTATIC_SHORT:
3469 if( nonstatic_short_space_count > 0 ) {
3470 real_offset = nonstatic_short_space_offset;
3471 nonstatic_short_space_offset += BytesPerShort;
3472 nonstatic_short_space_count -= 1;
3473 } else {
3474 real_offset = next_nonstatic_short_offset;
3475 next_nonstatic_short_offset += BytesPerShort;
3476 }
3477 break;
3478 case NONSTATIC_WORD:
3479 if( nonstatic_word_space_count > 0 ) {
3480 real_offset = nonstatic_word_space_offset;
3481 nonstatic_word_space_offset += BytesPerInt;
3482 nonstatic_word_space_count -= 1;
3483 } else {
3484 real_offset = next_nonstatic_word_offset;
3485 next_nonstatic_word_offset += BytesPerInt;
3486 }
3487 break;
3488 case NONSTATIC_ALIGNED_DOUBLE:
3489 case NONSTATIC_DOUBLE:
3490 real_offset = next_nonstatic_double_offset;
3491 next_nonstatic_double_offset += BytesPerLong;
3492 break;
3493 default:
3494 ShouldNotReachHere();
3495 }
3496 fields->short_at_put(i + instanceKlass::low_offset, extract_low_short_from_int(real_offset));
3497 fields->short_at_put(i + instanceKlass::high_offset, extract_high_short_from_int(real_offset));
3498 }
3500 // Size of instances
3501 int instance_size;
3503 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3504 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3506 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3508 // Number of non-static oop map blocks allocated at end of klass.
3509 const unsigned int total_oop_map_count =
3510 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3511 first_nonstatic_oop_offset);
3513 // Compute reference type
3514 ReferenceType rt;
3515 if (super_klass() == NULL) {
3516 rt = REF_NONE;
3517 } else {
3518 rt = super_klass->reference_type();
3519 }
3521 // We can now create the basic klassOop for this klass
3522 klassOop ik = oopFactory::new_instanceKlass(vtable_size, itable_size,
3523 static_field_size,
3524 total_oop_map_count,
3525 rt, CHECK_(nullHandle));
3526 instanceKlassHandle this_klass (THREAD, ik);
3528 assert(this_klass->static_field_size() == static_field_size, "sanity");
3529 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3530 "sanity");
3532 // Fill in information already parsed
3533 this_klass->set_access_flags(access_flags);
3534 this_klass->set_should_verify_class(verify);
3535 jint lh = Klass::instance_layout_helper(instance_size, false);
3536 this_klass->set_layout_helper(lh);
3537 assert(this_klass->oop_is_instance(), "layout is correct");
3538 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3539 // Not yet: supers are done below to support the new subtype-checking fields
3540 //this_klass->set_super(super_klass());
3541 this_klass->set_class_loader(class_loader());
3542 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3543 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3544 this_klass->set_static_oop_field_size(fac.static_oop_count);
3545 cp->set_pool_holder(this_klass());
3546 this_klass->set_constants(cp());
3547 this_klass->set_local_interfaces(local_interfaces());
3548 this_klass->set_fields(fields());
3549 this_klass->set_methods(methods());
3550 if (has_final_method) {
3551 this_klass->set_has_final_method();
3552 }
3553 this_klass->set_method_ordering(method_ordering());
3554 // The instanceKlass::_methods_jmethod_ids cache and the
3555 // instanceKlass::_methods_cached_itable_indices cache are
3556 // both managed on the assumption that the initial cache
3557 // size is equal to the number of methods in the class. If
3558 // that changes, then instanceKlass::idnum_can_increment()
3559 // has to be changed accordingly.
3560 this_klass->set_initial_method_idnum(methods->length());
3561 this_klass->set_name(cp->klass_name_at(this_class_index));
3562 if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself
3563 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3564 this_klass->set_protection_domain(protection_domain());
3565 this_klass->set_fields_annotations(fields_annotations());
3566 this_klass->set_methods_annotations(methods_annotations());
3567 this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3568 this_klass->set_methods_default_annotations(methods_default_annotations());
3570 this_klass->set_minor_version(minor_version);
3571 this_klass->set_major_version(major_version);
3573 // Set up methodOop::intrinsic_id as soon as we know the names of methods.
3574 // (We used to do this lazily, but now we query it in Rewriter,
3575 // which is eagerly done for every method, so we might as well do it now,
3576 // when everything is fresh in memory.)
3577 if (methodOopDesc::klass_id_for_intrinsics(this_klass->as_klassOop()) != vmSymbols::NO_SID) {
3578 for (int j = 0; j < methods->length(); j++) {
3579 ((methodOop)methods->obj_at(j))->init_intrinsic_id();
3580 }
3581 }
3583 if (cached_class_file_bytes != NULL) {
3584 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3585 this_klass->set_cached_class_file(cached_class_file_bytes,
3586 cached_class_file_length);
3587 }
3589 // Miranda methods
3590 if ((num_miranda_methods > 0) ||
3591 // if this class introduced new miranda methods or
3592 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3593 // super class exists and this class inherited miranda methods
3594 ) {
3595 this_klass->set_has_miranda_methods(); // then set a flag
3596 }
3598 // Additional attributes
3599 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle));
3601 // Make sure this is the end of class file stream
3602 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3604 // Initialize static fields
3605 this_klass->do_local_static_fields(&initialize_static_field, CHECK_(nullHandle));
3607 // VerifyOops believes that once this has been set, the object is completely loaded.
3608 // Compute transitive closure of interfaces this class implements
3609 this_klass->set_transitive_interfaces(transitive_interfaces());
3611 // Fill in information needed to compute superclasses.
3612 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
3614 // Initialize itable offset tables
3615 klassItable::setup_itable_offset_table(this_klass);
3617 // Do final class setup
3618 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3620 set_precomputed_flags(this_klass);
3622 // reinitialize modifiers, using the InnerClasses attribute
3623 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3624 this_klass->set_modifier_flags(computed_modifiers);
3626 // check if this class can access its super class
3627 check_super_class_access(this_klass, CHECK_(nullHandle));
3629 // check if this class can access its superinterfaces
3630 check_super_interface_access(this_klass, CHECK_(nullHandle));
3632 // check if this class overrides any final method
3633 check_final_method_override(this_klass, CHECK_(nullHandle));
3635 // check that if this class is an interface then it doesn't have static methods
3636 if (this_klass->is_interface()) {
3637 check_illegal_static_method(this_klass, CHECK_(nullHandle));
3638 }
3640 ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()),
3641 false /* not shared class */);
3643 if (TraceClassLoading) {
3644 // print in a single call to reduce interleaving of output
3645 if (cfs->source() != NULL) {
3646 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3647 cfs->source());
3648 } else if (class_loader.is_null()) {
3649 if (THREAD->is_Java_thread()) {
3650 klassOop caller = ((JavaThread*)THREAD)->security_get_caller_class(1);
3651 tty->print("[Loaded %s by instance of %s]\n",
3652 this_klass->external_name(),
3653 instanceKlass::cast(caller)->external_name());
3654 } else {
3655 tty->print("[Loaded %s]\n", this_klass->external_name());
3656 }
3657 } else {
3658 ResourceMark rm;
3659 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3660 instanceKlass::cast(class_loader->klass())->external_name());
3661 }
3662 }
3664 if (TraceClassResolution) {
3665 // print out the superclass.
3666 const char * from = Klass::cast(this_klass())->external_name();
3667 if (this_klass->java_super() != NULL) {
3668 tty->print("RESOLVE %s %s (super)\n", from, instanceKlass::cast(this_klass->java_super())->external_name());
3669 }
3670 // print out each of the interface classes referred to by this class.
3671 objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces());
3672 if (!local_interfaces.is_null()) {
3673 int length = local_interfaces->length();
3674 for (int i = 0; i < length; i++) {
3675 klassOop k = klassOop(local_interfaces->obj_at(i));
3676 instanceKlass* to_class = instanceKlass::cast(k);
3677 const char * to = to_class->external_name();
3678 tty->print("RESOLVE %s %s (interface)\n", from, to);
3679 }
3680 }
3681 }
3683 #ifndef PRODUCT
3684 if( PrintCompactFieldsSavings ) {
3685 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3686 tty->print("[Saved %d of %d bytes in %s]\n",
3687 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
3688 orig_nonstatic_field_size*heapOopSize,
3689 this_klass->external_name());
3690 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3691 tty->print("[Wasted %d over %d bytes in %s]\n",
3692 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
3693 orig_nonstatic_field_size*heapOopSize,
3694 this_klass->external_name());
3695 }
3696 }
3697 #endif
3699 // preserve result across HandleMark
3700 preserve_this_klass = this_klass();
3701 }
3703 // Create new handle outside HandleMark
3704 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
3705 debug_only(this_klass->as_klassOop()->verify();)
3707 return this_klass;
3708 }
3711 unsigned int
3712 ClassFileParser::compute_oop_map_count(instanceKlassHandle super,
3713 unsigned int nonstatic_oop_map_count,
3714 int first_nonstatic_oop_offset) {
3715 unsigned int map_count =
3716 super.is_null() ? 0 : super->nonstatic_oop_map_count();
3717 if (nonstatic_oop_map_count > 0) {
3718 // We have oops to add to map
3719 if (map_count == 0) {
3720 map_count = nonstatic_oop_map_count;
3721 } else {
3722 // Check whether we should add a new map block or whether the last one can
3723 // be extended
3724 OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps();
3725 OopMapBlock* const last_map = first_map + map_count - 1;
3727 int next_offset = last_map->offset() + last_map->count() * heapOopSize;
3728 if (next_offset == first_nonstatic_oop_offset) {
3729 // There is no gap bettwen superklass's last oop field and first
3730 // local oop field, merge maps.
3731 nonstatic_oop_map_count -= 1;
3732 } else {
3733 // Superklass didn't end with a oop field, add extra maps
3734 assert(next_offset < first_nonstatic_oop_offset, "just checking");
3735 }
3736 map_count += nonstatic_oop_map_count;
3737 }
3738 }
3739 return map_count;
3740 }
3743 void ClassFileParser::fill_oop_maps(instanceKlassHandle k,
3744 unsigned int nonstatic_oop_map_count,
3745 int* nonstatic_oop_offsets,
3746 unsigned int* nonstatic_oop_counts) {
3747 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
3748 const instanceKlass* const super = k->superklass();
3749 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0;
3750 if (super_count > 0) {
3751 // Copy maps from superklass
3752 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps();
3753 for (unsigned int i = 0; i < super_count; ++i) {
3754 *this_oop_map++ = *super_oop_map++;
3755 }
3756 }
3758 if (nonstatic_oop_map_count > 0) {
3759 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) {
3760 // The counts differ because there is no gap between superklass's last oop
3761 // field and the first local oop field. Extend the last oop map copied
3762 // from the superklass instead of creating new one.
3763 nonstatic_oop_map_count--;
3764 nonstatic_oop_offsets++;
3765 this_oop_map--;
3766 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++);
3767 this_oop_map++;
3768 }
3770 // Add new map blocks, fill them
3771 while (nonstatic_oop_map_count-- > 0) {
3772 this_oop_map->set_offset(*nonstatic_oop_offsets++);
3773 this_oop_map->set_count(*nonstatic_oop_counts++);
3774 this_oop_map++;
3775 }
3776 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() ==
3777 this_oop_map, "sanity");
3778 }
3779 }
3782 void ClassFileParser::set_precomputed_flags(instanceKlassHandle k) {
3783 klassOop super = k->super();
3785 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
3786 // in which case we don't have to register objects as finalizable
3787 if (!_has_empty_finalizer) {
3788 if (_has_finalizer ||
3789 (super != NULL && super->klass_part()->has_finalizer())) {
3790 k->set_has_finalizer();
3791 }
3792 }
3794 #ifdef ASSERT
3795 bool f = false;
3796 methodOop m = k->lookup_method(vmSymbols::finalize_method_name(),
3797 vmSymbols::void_method_signature());
3798 if (m != NULL && !m->is_empty_method()) {
3799 f = true;
3800 }
3801 assert(f == k->has_finalizer(), "inconsistent has_finalizer");
3802 #endif
3804 // Check if this klass supports the java.lang.Cloneable interface
3805 if (SystemDictionary::Cloneable_klass_loaded()) {
3806 if (k->is_subtype_of(SystemDictionary::Cloneable_klass())) {
3807 k->set_is_cloneable();
3808 }
3809 }
3811 // Check if this klass has a vanilla default constructor
3812 if (super == NULL) {
3813 // java.lang.Object has empty default constructor
3814 k->set_has_vanilla_constructor();
3815 } else {
3816 if (Klass::cast(super)->has_vanilla_constructor() &&
3817 _has_vanilla_constructor) {
3818 k->set_has_vanilla_constructor();
3819 }
3820 #ifdef ASSERT
3821 bool v = false;
3822 if (Klass::cast(super)->has_vanilla_constructor()) {
3823 methodOop constructor = k->find_method(vmSymbols::object_initializer_name(
3824 ), vmSymbols::void_method_signature());
3825 if (constructor != NULL && constructor->is_vanilla_constructor()) {
3826 v = true;
3827 }
3828 }
3829 assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
3830 #endif
3831 }
3833 // If it cannot be fast-path allocated, set a bit in the layout helper.
3834 // See documentation of instanceKlass::can_be_fastpath_allocated().
3835 assert(k->size_helper() > 0, "layout_helper is initialized");
3836 if ((!RegisterFinalizersAtInit && k->has_finalizer())
3837 || k->is_abstract() || k->is_interface()
3838 || (k->name() == vmSymbols::java_lang_Class()
3839 && k->class_loader() == NULL)
3840 || k->size_helper() >= FastAllocateSizeLimit) {
3841 // Forbid fast-path allocation.
3842 jint lh = Klass::instance_layout_helper(k->size_helper(), true);
3843 k->set_layout_helper(lh);
3844 }
3845 }
3848 // utility method for appending and array with check for duplicates
3850 void append_interfaces(objArrayHandle result, int& index, objArrayOop ifs) {
3851 // iterate over new interfaces
3852 for (int i = 0; i < ifs->length(); i++) {
3853 oop e = ifs->obj_at(i);
3854 assert(e->is_klass() && instanceKlass::cast(klassOop(e))->is_interface(), "just checking");
3855 // check for duplicates
3856 bool duplicate = false;
3857 for (int j = 0; j < index; j++) {
3858 if (result->obj_at(j) == e) {
3859 duplicate = true;
3860 break;
3861 }
3862 }
3863 // add new interface
3864 if (!duplicate) {
3865 result->obj_at_put(index++, e);
3866 }
3867 }
3868 }
3870 objArrayHandle ClassFileParser::compute_transitive_interfaces(instanceKlassHandle super, objArrayHandle local_ifs, TRAPS) {
3871 // Compute maximum size for transitive interfaces
3872 int max_transitive_size = 0;
3873 int super_size = 0;
3874 // Add superclass transitive interfaces size
3875 if (super.not_null()) {
3876 super_size = super->transitive_interfaces()->length();
3877 max_transitive_size += super_size;
3878 }
3879 // Add local interfaces' super interfaces
3880 int local_size = local_ifs->length();
3881 for (int i = 0; i < local_size; i++) {
3882 klassOop l = klassOop(local_ifs->obj_at(i));
3883 max_transitive_size += instanceKlass::cast(l)->transitive_interfaces()->length();
3884 }
3885 // Finally add local interfaces
3886 max_transitive_size += local_size;
3887 // Construct array
3888 objArrayHandle result;
3889 if (max_transitive_size == 0) {
3890 // no interfaces, use canonicalized array
3891 result = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
3892 } else if (max_transitive_size == super_size) {
3893 // no new local interfaces added, share superklass' transitive interface array
3894 result = objArrayHandle(THREAD, super->transitive_interfaces());
3895 } else if (max_transitive_size == local_size) {
3896 // only local interfaces added, share local interface array
3897 result = local_ifs;
3898 } else {
3899 objArrayHandle nullHandle;
3900 objArrayOop new_objarray = oopFactory::new_system_objArray(max_transitive_size, CHECK_(nullHandle));
3901 result = objArrayHandle(THREAD, new_objarray);
3902 int index = 0;
3903 // Copy down from superclass
3904 if (super.not_null()) {
3905 append_interfaces(result, index, super->transitive_interfaces());
3906 }
3907 // Copy down from local interfaces' superinterfaces
3908 for (int i = 0; i < local_ifs->length(); i++) {
3909 klassOop l = klassOop(local_ifs->obj_at(i));
3910 append_interfaces(result, index, instanceKlass::cast(l)->transitive_interfaces());
3911 }
3912 // Finally add local interfaces
3913 append_interfaces(result, index, local_ifs());
3915 // Check if duplicates were removed
3916 if (index != max_transitive_size) {
3917 assert(index < max_transitive_size, "just checking");
3918 objArrayOop new_result = oopFactory::new_system_objArray(index, CHECK_(nullHandle));
3919 for (int i = 0; i < index; i++) {
3920 oop e = result->obj_at(i);
3921 assert(e != NULL, "just checking");
3922 new_result->obj_at_put(i, e);
3923 }
3924 result = objArrayHandle(THREAD, new_result);
3925 }
3926 }
3927 return result;
3928 }
3931 void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) {
3932 klassOop super = this_klass->super();
3933 if ((super != NULL) &&
3934 (!Reflection::verify_class_access(this_klass->as_klassOop(), super, false))) {
3935 ResourceMark rm(THREAD);
3936 Exceptions::fthrow(
3937 THREAD_AND_LOCATION,
3938 vmSymbolHandles::java_lang_IllegalAccessError(),
3939 "class %s cannot access its superclass %s",
3940 this_klass->external_name(),
3941 instanceKlass::cast(super)->external_name()
3942 );
3943 return;
3944 }
3945 }
3948 void ClassFileParser::check_super_interface_access(instanceKlassHandle this_klass, TRAPS) {
3949 objArrayHandle local_interfaces (THREAD, this_klass->local_interfaces());
3950 int lng = local_interfaces->length();
3951 for (int i = lng - 1; i >= 0; i--) {
3952 klassOop k = klassOop(local_interfaces->obj_at(i));
3953 assert (k != NULL && Klass::cast(k)->is_interface(), "invalid interface");
3954 if (!Reflection::verify_class_access(this_klass->as_klassOop(), k, false)) {
3955 ResourceMark rm(THREAD);
3956 Exceptions::fthrow(
3957 THREAD_AND_LOCATION,
3958 vmSymbolHandles::java_lang_IllegalAccessError(),
3959 "class %s cannot access its superinterface %s",
3960 this_klass->external_name(),
3961 instanceKlass::cast(k)->external_name()
3962 );
3963 return;
3964 }
3965 }
3966 }
3969 void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) {
3970 objArrayHandle methods (THREAD, this_klass->methods());
3971 int num_methods = methods->length();
3973 // go thru each method and check if it overrides a final method
3974 for (int index = 0; index < num_methods; index++) {
3975 methodOop m = (methodOop)methods->obj_at(index);
3977 // skip private, static and <init> methods
3978 if ((!m->is_private()) &&
3979 (!m->is_static()) &&
3980 (m->name() != vmSymbols::object_initializer_name())) {
3982 symbolOop name = m->name();
3983 symbolOop signature = m->signature();
3984 klassOop k = this_klass->super();
3985 methodOop super_m = NULL;
3986 while (k != NULL) {
3987 // skip supers that don't have final methods.
3988 if (k->klass_part()->has_final_method()) {
3989 // lookup a matching method in the super class hierarchy
3990 super_m = instanceKlass::cast(k)->lookup_method(name, signature);
3991 if (super_m == NULL) {
3992 break; // didn't find any match; get out
3993 }
3995 if (super_m->is_final() &&
3996 // matching method in super is final
3997 (Reflection::verify_field_access(this_klass->as_klassOop(),
3998 super_m->method_holder(),
3999 super_m->method_holder(),
4000 super_m->access_flags(), false))
4001 // this class can access super final method and therefore override
4002 ) {
4003 ResourceMark rm(THREAD);
4004 Exceptions::fthrow(
4005 THREAD_AND_LOCATION,
4006 vmSymbolHandles::java_lang_VerifyError(),
4007 "class %s overrides final method %s.%s",
4008 this_klass->external_name(),
4009 name->as_C_string(),
4010 signature->as_C_string()
4011 );
4012 return;
4013 }
4015 // continue to look from super_m's holder's super.
4016 k = instanceKlass::cast(super_m->method_holder())->super();
4017 continue;
4018 }
4020 k = k->klass_part()->super();
4021 }
4022 }
4023 }
4024 }
4027 // assumes that this_klass is an interface
4028 void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) {
4029 assert(this_klass->is_interface(), "not an interface");
4030 objArrayHandle methods (THREAD, this_klass->methods());
4031 int num_methods = methods->length();
4033 for (int index = 0; index < num_methods; index++) {
4034 methodOop m = (methodOop)methods->obj_at(index);
4035 // if m is static and not the init method, throw a verify error
4036 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4037 ResourceMark rm(THREAD);
4038 Exceptions::fthrow(
4039 THREAD_AND_LOCATION,
4040 vmSymbolHandles::java_lang_VerifyError(),
4041 "Illegal static method %s in interface %s",
4042 m->name()->as_C_string(),
4043 this_klass->external_name()
4044 );
4045 return;
4046 }
4047 }
4048 }
4050 // utility methods for format checking
4052 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) {
4053 if (!_need_verify) { return; }
4055 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4056 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4057 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4058 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4059 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4060 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4061 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4063 if ((is_abstract && is_final) ||
4064 (is_interface && !is_abstract) ||
4065 (is_interface && major_gte_15 && (is_super || is_enum)) ||
4066 (!is_interface && major_gte_15 && is_annotation)) {
4067 ResourceMark rm(THREAD);
4068 Exceptions::fthrow(
4069 THREAD_AND_LOCATION,
4070 vmSymbolHandles::java_lang_ClassFormatError(),
4071 "Illegal class modifiers in class %s: 0x%X",
4072 _class_name->as_C_string(), flags
4073 );
4074 return;
4075 }
4076 }
4078 bool ClassFileParser::has_illegal_visibility(jint flags) {
4079 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4080 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4081 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4083 return ((is_public && is_protected) ||
4084 (is_public && is_private) ||
4085 (is_protected && is_private));
4086 }
4088 bool ClassFileParser::is_supported_version(u2 major, u2 minor) {
4089 u2 max_version =
4090 JDK_Version::is_gte_jdk17x_version() ? JAVA_MAX_SUPPORTED_VERSION :
4091 (JDK_Version::is_gte_jdk16x_version() ? JAVA_6_VERSION : JAVA_1_5_VERSION);
4092 return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
4093 (major <= max_version) &&
4094 ((major != max_version) ||
4095 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION));
4096 }
4098 void ClassFileParser::verify_legal_field_modifiers(
4099 jint flags, bool is_interface, TRAPS) {
4100 if (!_need_verify) { return; }
4102 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4103 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4104 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4105 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4106 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4107 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
4108 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
4109 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4110 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4112 bool is_illegal = false;
4114 if (is_interface) {
4115 if (!is_public || !is_static || !is_final || is_private ||
4116 is_protected || is_volatile || is_transient ||
4117 (major_gte_15 && is_enum)) {
4118 is_illegal = true;
4119 }
4120 } else { // not interface
4121 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
4122 is_illegal = true;
4123 }
4124 }
4126 if (is_illegal) {
4127 ResourceMark rm(THREAD);
4128 Exceptions::fthrow(
4129 THREAD_AND_LOCATION,
4130 vmSymbolHandles::java_lang_ClassFormatError(),
4131 "Illegal field modifiers in class %s: 0x%X",
4132 _class_name->as_C_string(), flags);
4133 return;
4134 }
4135 }
4137 void ClassFileParser::verify_legal_method_modifiers(
4138 jint flags, bool is_interface, symbolHandle name, TRAPS) {
4139 if (!_need_verify) { return; }
4141 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4142 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4143 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
4144 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4145 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
4146 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4147 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
4148 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
4149 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
4150 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4151 const bool is_initializer = (name == vmSymbols::object_initializer_name());
4153 bool is_illegal = false;
4155 if (is_interface) {
4156 if (!is_abstract || !is_public || is_static || is_final ||
4157 is_native || (major_gte_15 && (is_synchronized || is_strict))) {
4158 is_illegal = true;
4159 }
4160 } else { // not interface
4161 if (is_initializer) {
4162 if (is_static || is_final || is_synchronized || is_native ||
4163 is_abstract || (major_gte_15 && is_bridge)) {
4164 is_illegal = true;
4165 }
4166 } else { // not initializer
4167 if (is_abstract) {
4168 if ((is_final || is_native || is_private || is_static ||
4169 (major_gte_15 && (is_synchronized || is_strict)))) {
4170 is_illegal = true;
4171 }
4172 }
4173 if (has_illegal_visibility(flags)) {
4174 is_illegal = true;
4175 }
4176 }
4177 }
4179 if (is_illegal) {
4180 ResourceMark rm(THREAD);
4181 Exceptions::fthrow(
4182 THREAD_AND_LOCATION,
4183 vmSymbolHandles::java_lang_ClassFormatError(),
4184 "Method %s in class %s has illegal modifiers: 0x%X",
4185 name->as_C_string(), _class_name->as_C_string(), flags);
4186 return;
4187 }
4188 }
4190 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) {
4191 assert(_need_verify, "only called when _need_verify is true");
4192 int i = 0;
4193 int count = length >> 2;
4194 for (int k=0; k<count; k++) {
4195 unsigned char b0 = buffer[i];
4196 unsigned char b1 = buffer[i+1];
4197 unsigned char b2 = buffer[i+2];
4198 unsigned char b3 = buffer[i+3];
4199 // For an unsigned char v,
4200 // (v | v - 1) is < 128 (highest bit 0) for 0 < v < 128;
4201 // (v | v - 1) is >= 128 (highest bit 1) for v == 0 or v >= 128.
4202 unsigned char res = b0 | b0 - 1 |
4203 b1 | b1 - 1 |
4204 b2 | b2 - 1 |
4205 b3 | b3 - 1;
4206 if (res >= 128) break;
4207 i += 4;
4208 }
4209 for(; i < length; i++) {
4210 unsigned short c;
4211 // no embedded zeros
4212 guarantee_property((buffer[i] != 0), "Illegal UTF8 string in constant pool in class file %s", CHECK);
4213 if(buffer[i] < 128) {
4214 continue;
4215 }
4216 if ((i + 5) < length) { // see if it's legal supplementary character
4217 if (UTF8::is_supplementary_character(&buffer[i])) {
4218 c = UTF8::get_supplementary_character(&buffer[i]);
4219 i += 5;
4220 continue;
4221 }
4222 }
4223 switch (buffer[i] >> 4) {
4224 default: break;
4225 case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
4226 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4227 case 0xC: case 0xD: // 110xxxxx 10xxxxxx
4228 c = (buffer[i] & 0x1F) << 6;
4229 i++;
4230 if ((i < length) && ((buffer[i] & 0xC0) == 0x80)) {
4231 c += buffer[i] & 0x3F;
4232 if (_major_version <= 47 || c == 0 || c >= 0x80) {
4233 // for classes with major > 47, c must a null or a character in its shortest form
4234 break;
4235 }
4236 }
4237 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4238 case 0xE: // 1110xxxx 10xxxxxx 10xxxxxx
4239 c = (buffer[i] & 0xF) << 12;
4240 i += 2;
4241 if ((i < length) && ((buffer[i-1] & 0xC0) == 0x80) && ((buffer[i] & 0xC0) == 0x80)) {
4242 c += ((buffer[i-1] & 0x3F) << 6) + (buffer[i] & 0x3F);
4243 if (_major_version <= 47 || c >= 0x800) {
4244 // for classes with major > 47, c must be in its shortest form
4245 break;
4246 }
4247 }
4248 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4249 } // end of switch
4250 } // end of for
4251 }
4253 // Checks if name is a legal class name.
4254 void ClassFileParser::verify_legal_class_name(symbolHandle name, TRAPS) {
4255 if (!_need_verify || _relax_verify) { return; }
4257 char buf[fixed_buffer_size];
4258 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4259 unsigned int length = name->utf8_length();
4260 bool legal = false;
4262 if (length > 0) {
4263 char* p;
4264 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4265 p = skip_over_field_signature(bytes, false, length, CHECK);
4266 legal = (p != NULL) && ((p - bytes) == (int)length);
4267 } else if (_major_version < JAVA_1_5_VERSION) {
4268 if (bytes[0] != '<') {
4269 p = skip_over_field_name(bytes, true, length);
4270 legal = (p != NULL) && ((p - bytes) == (int)length);
4271 }
4272 } else {
4273 // 4900761: relax the constraints based on JSR202 spec
4274 // Class names may be drawn from the entire Unicode character set.
4275 // Identifiers between '/' must be unqualified names.
4276 // The utf8 string has been verified when parsing cpool entries.
4277 legal = verify_unqualified_name(bytes, length, LegalClass);
4278 }
4279 }
4280 if (!legal) {
4281 ResourceMark rm(THREAD);
4282 Exceptions::fthrow(
4283 THREAD_AND_LOCATION,
4284 vmSymbolHandles::java_lang_ClassFormatError(),
4285 "Illegal class name \"%s\" in class file %s", bytes,
4286 _class_name->as_C_string()
4287 );
4288 return;
4289 }
4290 }
4292 // Checks if name is a legal field name.
4293 void ClassFileParser::verify_legal_field_name(symbolHandle name, TRAPS) {
4294 if (!_need_verify || _relax_verify) { return; }
4296 char buf[fixed_buffer_size];
4297 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4298 unsigned int length = name->utf8_length();
4299 bool legal = false;
4301 if (length > 0) {
4302 if (_major_version < JAVA_1_5_VERSION) {
4303 if (bytes[0] != '<') {
4304 char* p = skip_over_field_name(bytes, false, length);
4305 legal = (p != NULL) && ((p - bytes) == (int)length);
4306 }
4307 } else {
4308 // 4881221: relax the constraints based on JSR202 spec
4309 legal = verify_unqualified_name(bytes, length, LegalField);
4310 }
4311 }
4313 if (!legal) {
4314 ResourceMark rm(THREAD);
4315 Exceptions::fthrow(
4316 THREAD_AND_LOCATION,
4317 vmSymbolHandles::java_lang_ClassFormatError(),
4318 "Illegal field name \"%s\" in class %s", bytes,
4319 _class_name->as_C_string()
4320 );
4321 return;
4322 }
4323 }
4325 // Checks if name is a legal method name.
4326 void ClassFileParser::verify_legal_method_name(symbolHandle name, TRAPS) {
4327 if (!_need_verify || _relax_verify) { return; }
4329 assert(!name.is_null(), "method name is null");
4330 char buf[fixed_buffer_size];
4331 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4332 unsigned int length = name->utf8_length();
4333 bool legal = false;
4335 if (length > 0) {
4336 if (bytes[0] == '<') {
4337 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4338 legal = true;
4339 }
4340 } else if (_major_version < JAVA_1_5_VERSION) {
4341 char* p;
4342 p = skip_over_field_name(bytes, false, length);
4343 legal = (p != NULL) && ((p - bytes) == (int)length);
4344 } else {
4345 // 4881221: relax the constraints based on JSR202 spec
4346 legal = verify_unqualified_name(bytes, length, LegalMethod);
4347 }
4348 }
4350 if (!legal) {
4351 ResourceMark rm(THREAD);
4352 Exceptions::fthrow(
4353 THREAD_AND_LOCATION,
4354 vmSymbolHandles::java_lang_ClassFormatError(),
4355 "Illegal method name \"%s\" in class %s", bytes,
4356 _class_name->as_C_string()
4357 );
4358 return;
4359 }
4360 }
4363 // Checks if signature is a legal field signature.
4364 void ClassFileParser::verify_legal_field_signature(symbolHandle name, symbolHandle signature, TRAPS) {
4365 if (!_need_verify) { return; }
4367 char buf[fixed_buffer_size];
4368 char* bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4369 unsigned int length = signature->utf8_length();
4370 char* p = skip_over_field_signature(bytes, false, length, CHECK);
4372 if (p == NULL || (p - bytes) != (int)length) {
4373 throwIllegalSignature("Field", name, signature, CHECK);
4374 }
4375 }
4377 // Checks if signature is a legal method signature.
4378 // Returns number of parameters
4379 int ClassFileParser::verify_legal_method_signature(symbolHandle name, symbolHandle signature, TRAPS) {
4380 if (!_need_verify) {
4381 // make sure caller's args_size will be less than 0 even for non-static
4382 // method so it will be recomputed in compute_size_of_parameters().
4383 return -2;
4384 }
4386 unsigned int args_size = 0;
4387 char buf[fixed_buffer_size];
4388 char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4389 unsigned int length = signature->utf8_length();
4390 char* nextp;
4392 // The first character must be a '('
4393 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {
4394 length--;
4395 // Skip over legal field signatures
4396 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4397 while ((length > 0) && (nextp != NULL)) {
4398 args_size++;
4399 if (p[0] == 'J' || p[0] == 'D') {
4400 args_size++;
4401 }
4402 length -= nextp - p;
4403 p = nextp;
4404 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4405 }
4406 // The first non-signature thing better be a ')'
4407 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4408 length--;
4409 if (name->utf8_length() > 0 && name->byte_at(0) == '<') {
4410 // All internal methods must return void
4411 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
4412 return args_size;
4413 }
4414 } else {
4415 // Now we better just have a return value
4416 nextp = skip_over_field_signature(p, true, length, CHECK_0);
4417 if (nextp && ((int)length == (nextp - p))) {
4418 return args_size;
4419 }
4420 }
4421 }
4422 }
4423 // Report error
4424 throwIllegalSignature("Method", name, signature, CHECK_0);
4425 return 0;
4426 }
4429 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4430 // Method names also may not contain the characters '<' or '>', unless <init>
4431 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4432 // method. Because these names have been checked as special cases before
4433 // calling this method in verify_legal_method_name.
4434 bool ClassFileParser::verify_unqualified_name(
4435 char* name, unsigned int length, int type) {
4436 jchar ch;
4438 for (char* p = name; p != name + length; ) {
4439 ch = *p;
4440 if (ch < 128) {
4441 p++;
4442 if (ch == '.' || ch == ';' || ch == '[' ) {
4443 return false; // do not permit '.', ';', or '['
4444 }
4445 if (type != LegalClass && ch == '/') {
4446 return false; // do not permit '/' unless it's class name
4447 }
4448 if (type == LegalMethod && (ch == '<' || ch == '>')) {
4449 return false; // do not permit '<' or '>' in method names
4450 }
4451 } else {
4452 char* tmp_p = UTF8::next(p, &ch);
4453 p = tmp_p;
4454 }
4455 }
4456 return true;
4457 }
4460 // Take pointer to a string. Skip over the longest part of the string that could
4461 // be taken as a fieldname. Allow '/' if slash_ok is true.
4462 // Return a pointer to just past the fieldname.
4463 // Return NULL if no fieldname at all was found, or in the case of slash_ok
4464 // being true, we saw consecutive slashes (meaning we were looking for a
4465 // qualified path but found something that was badly-formed).
4466 char* ClassFileParser::skip_over_field_name(char* name, bool slash_ok, unsigned int length) {
4467 char* p;
4468 jchar ch;
4469 jboolean last_is_slash = false;
4470 jboolean not_first_ch = false;
4472 for (p = name; p != name + length; not_first_ch = true) {
4473 char* old_p = p;
4474 ch = *p;
4475 if (ch < 128) {
4476 p++;
4477 // quick check for ascii
4478 if ((ch >= 'a' && ch <= 'z') ||
4479 (ch >= 'A' && ch <= 'Z') ||
4480 (ch == '_' || ch == '$') ||
4481 (not_first_ch && ch >= '0' && ch <= '9')) {
4482 last_is_slash = false;
4483 continue;
4484 }
4485 if (slash_ok && ch == '/') {
4486 if (last_is_slash) {
4487 return NULL; // Don't permit consecutive slashes
4488 }
4489 last_is_slash = true;
4490 continue;
4491 }
4492 } else {
4493 jint unicode_ch;
4494 char* tmp_p = UTF8::next_character(p, &unicode_ch);
4495 p = tmp_p;
4496 last_is_slash = false;
4497 // Check if ch is Java identifier start or is Java identifier part
4498 // 4672820: call java.lang.Character methods directly without generating separate tables.
4499 EXCEPTION_MARK;
4500 instanceKlassHandle klass (THREAD, SystemDictionary::Character_klass());
4502 // return value
4503 JavaValue result(T_BOOLEAN);
4504 // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart
4505 JavaCallArguments args;
4506 args.push_int(unicode_ch);
4508 // public static boolean isJavaIdentifierStart(char ch);
4509 JavaCalls::call_static(&result,
4510 klass,
4511 vmSymbolHandles::isJavaIdentifierStart_name(),
4512 vmSymbolHandles::int_bool_signature(),
4513 &args,
4514 THREAD);
4516 if (HAS_PENDING_EXCEPTION) {
4517 CLEAR_PENDING_EXCEPTION;
4518 return 0;
4519 }
4520 if (result.get_jboolean()) {
4521 continue;
4522 }
4524 if (not_first_ch) {
4525 // public static boolean isJavaIdentifierPart(char ch);
4526 JavaCalls::call_static(&result,
4527 klass,
4528 vmSymbolHandles::isJavaIdentifierPart_name(),
4529 vmSymbolHandles::int_bool_signature(),
4530 &args,
4531 THREAD);
4533 if (HAS_PENDING_EXCEPTION) {
4534 CLEAR_PENDING_EXCEPTION;
4535 return 0;
4536 }
4538 if (result.get_jboolean()) {
4539 continue;
4540 }
4541 }
4542 }
4543 return (not_first_ch) ? old_p : NULL;
4544 }
4545 return (not_first_ch) ? p : NULL;
4546 }
4549 // Take pointer to a string. Skip over the longest part of the string that could
4550 // be taken as a field signature. Allow "void" if void_ok.
4551 // Return a pointer to just past the signature.
4552 // Return NULL if no legal signature is found.
4553 char* ClassFileParser::skip_over_field_signature(char* signature,
4554 bool void_ok,
4555 unsigned int length,
4556 TRAPS) {
4557 unsigned int array_dim = 0;
4558 while (length > 0) {
4559 switch (signature[0]) {
4560 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
4561 case JVM_SIGNATURE_BOOLEAN:
4562 case JVM_SIGNATURE_BYTE:
4563 case JVM_SIGNATURE_CHAR:
4564 case JVM_SIGNATURE_SHORT:
4565 case JVM_SIGNATURE_INT:
4566 case JVM_SIGNATURE_FLOAT:
4567 case JVM_SIGNATURE_LONG:
4568 case JVM_SIGNATURE_DOUBLE:
4569 return signature + 1;
4570 case JVM_SIGNATURE_CLASS: {
4571 if (_major_version < JAVA_1_5_VERSION) {
4572 // Skip over the class name if one is there
4573 char* p = skip_over_field_name(signature + 1, true, --length);
4575 // The next character better be a semicolon
4576 if (p && (p - signature) > 1 && p[0] == ';') {
4577 return p + 1;
4578 }
4579 } else {
4580 // 4900761: For class version > 48, any unicode is allowed in class name.
4581 length--;
4582 signature++;
4583 while (length > 0 && signature[0] != ';') {
4584 if (signature[0] == '.') {
4585 classfile_parse_error("Class name contains illegal character '.' in descriptor in class file %s", CHECK_0);
4586 }
4587 length--;
4588 signature++;
4589 }
4590 if (signature[0] == ';') { return signature + 1; }
4591 }
4593 return NULL;
4594 }
4595 case JVM_SIGNATURE_ARRAY:
4596 array_dim++;
4597 if (array_dim > 255) {
4598 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
4599 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
4600 }
4601 // The rest of what's there better be a legal signature
4602 signature++;
4603 length--;
4604 void_ok = false;
4605 break;
4607 default:
4608 return NULL;
4609 }
4610 }
4611 return NULL;
4612 }