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