Thu, 29 Sep 2011 09:53:56 -0700
7092278: "jmap -finalizerinfo" throws "sun.jvm.hotspot.utilities.AssertionFailure: invalid cp index 0 137"
Reviewed-by: kvn
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 unsigned int 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 count[atype]++;
1064 return atype;
1065 }
1066 };
1069 typeArrayHandle ClassFileParser::parse_fields(Symbol* class_name,
1070 constantPoolHandle cp, bool is_interface,
1071 FieldAllocationCount *fac,
1072 objArrayHandle* fields_annotations,
1073 int* java_fields_count_ptr, TRAPS) {
1074 ClassFileStream* cfs = stream();
1075 typeArrayHandle nullHandle;
1076 cfs->guarantee_more(2, CHECK_(nullHandle)); // length
1077 u2 length = cfs->get_u2_fast();
1078 *java_fields_count_ptr = length;
1080 int num_injected = 0;
1081 InjectedField* injected = JavaClasses::get_injected(class_name, &num_injected);
1083 // Tuples of shorts [access, name index, sig index, initial value index, byte offset, generic signature index]
1084 typeArrayOop new_fields = oopFactory::new_permanent_shortArray((length + num_injected) * FieldInfo::field_slots, CHECK_(nullHandle));
1085 typeArrayHandle fields(THREAD, new_fields);
1087 typeArrayHandle field_annotations;
1088 for (int n = 0; n < length; n++) {
1089 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
1091 AccessFlags access_flags;
1092 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_FIELD_MODIFIERS;
1093 verify_legal_field_modifiers(flags, is_interface, CHECK_(nullHandle));
1094 access_flags.set_flags(flags);
1096 u2 name_index = cfs->get_u2_fast();
1097 int cp_size = cp->length();
1098 check_property(
1099 valid_cp_range(name_index, cp_size) && cp->tag_at(name_index).is_utf8(),
1100 "Invalid constant pool index %u for field name in class file %s",
1101 name_index, CHECK_(nullHandle));
1102 Symbol* name = cp->symbol_at(name_index);
1103 verify_legal_field_name(name, CHECK_(nullHandle));
1105 u2 signature_index = cfs->get_u2_fast();
1106 check_property(
1107 valid_cp_range(signature_index, cp_size) &&
1108 cp->tag_at(signature_index).is_utf8(),
1109 "Invalid constant pool index %u for field signature in class file %s",
1110 signature_index, CHECK_(nullHandle));
1111 Symbol* sig = cp->symbol_at(signature_index);
1112 verify_legal_field_signature(name, sig, CHECK_(nullHandle));
1114 u2 constantvalue_index = 0;
1115 bool is_synthetic = false;
1116 u2 generic_signature_index = 0;
1117 bool is_static = access_flags.is_static();
1119 u2 attributes_count = cfs->get_u2_fast();
1120 if (attributes_count > 0) {
1121 parse_field_attributes(cp, attributes_count, is_static, signature_index,
1122 &constantvalue_index, &is_synthetic,
1123 &generic_signature_index, &field_annotations,
1124 CHECK_(nullHandle));
1125 if (field_annotations.not_null()) {
1126 if (fields_annotations->is_null()) {
1127 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1128 *fields_annotations = objArrayHandle(THREAD, md);
1129 }
1130 (*fields_annotations)->obj_at_put(n, field_annotations());
1131 }
1132 if (is_synthetic) {
1133 access_flags.set_is_synthetic();
1134 }
1135 }
1137 FieldInfo* field = FieldInfo::from_field_array(fields(), n);
1138 field->initialize(access_flags.as_short(),
1139 name_index,
1140 signature_index,
1141 constantvalue_index,
1142 generic_signature_index,
1143 0);
1145 BasicType type = cp->basic_type_for_signature_at(signature_index);
1147 // Remember how many oops we encountered and compute allocation type
1148 FieldAllocationType atype = fac->update(is_static, type);
1150 // The correct offset is computed later (all oop fields will be located together)
1151 // We temporarily store the allocation type in the offset field
1152 field->set_offset(atype);
1153 }
1155 if (num_injected != 0) {
1156 int index = length;
1157 for (int n = 0; n < num_injected; n++) {
1158 // Check for duplicates
1159 if (injected[n].may_be_java) {
1160 Symbol* name = injected[n].name();
1161 Symbol* signature = injected[n].signature();
1162 bool duplicate = false;
1163 for (int i = 0; i < length; i++) {
1164 FieldInfo* f = FieldInfo::from_field_array(fields(), i);
1165 if (name == cp->symbol_at(f->name_index()) &&
1166 signature == cp->symbol_at(f->signature_index())) {
1167 // Symbol is desclared in Java so skip this one
1168 duplicate = true;
1169 break;
1170 }
1171 }
1172 if (duplicate) {
1173 // These will be removed from the field array at the end
1174 continue;
1175 }
1176 }
1178 // Injected field
1179 FieldInfo* field = FieldInfo::from_field_array(fields(), index);
1180 field->initialize(JVM_ACC_FIELD_INTERNAL,
1181 injected[n].name_index,
1182 injected[n].signature_index,
1183 0,
1184 0,
1185 0);
1187 BasicType type = FieldType::basic_type(injected[n].signature());
1189 // Remember how many oops we encountered and compute allocation type
1190 FieldAllocationType atype = fac->update(false, type);
1192 // The correct offset is computed later (all oop fields will be located together)
1193 // We temporarily store the allocation type in the offset field
1194 field->set_offset(atype);
1195 index++;
1196 }
1198 if (index < length + num_injected) {
1199 // sometimes injected fields already exist in the Java source so
1200 // the fields array could be too long. In that case trim the
1201 // fields array.
1202 new_fields = oopFactory::new_permanent_shortArray(index * FieldInfo::field_slots, CHECK_(nullHandle));
1203 for (int i = 0; i < index * FieldInfo::field_slots; i++) {
1204 new_fields->short_at_put(i, fields->short_at(i));
1205 }
1206 fields = new_fields;
1207 }
1208 }
1210 if (_need_verify && length > 1) {
1211 // Check duplicated fields
1212 ResourceMark rm(THREAD);
1213 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
1214 THREAD, NameSigHash*, HASH_ROW_SIZE);
1215 initialize_hashtable(names_and_sigs);
1216 bool dup = false;
1217 {
1218 debug_only(No_Safepoint_Verifier nsv;)
1219 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
1220 Symbol* name = fs.name();
1221 Symbol* sig = fs.signature();
1222 // If no duplicates, add name/signature in hashtable names_and_sigs.
1223 if (!put_after_lookup(name, sig, names_and_sigs)) {
1224 dup = true;
1225 break;
1226 }
1227 }
1228 }
1229 if (dup) {
1230 classfile_parse_error("Duplicate field name&signature in class file %s",
1231 CHECK_(nullHandle));
1232 }
1233 }
1235 return fields;
1236 }
1239 static void copy_u2_with_conversion(u2* dest, u2* src, int length) {
1240 while (length-- > 0) {
1241 *dest++ = Bytes::get_Java_u2((u1*) (src++));
1242 }
1243 }
1246 typeArrayHandle ClassFileParser::parse_exception_table(u4 code_length,
1247 u4 exception_table_length,
1248 constantPoolHandle cp,
1249 TRAPS) {
1250 ClassFileStream* cfs = stream();
1251 typeArrayHandle nullHandle;
1253 // 4-tuples of ints [start_pc, end_pc, handler_pc, catch_type index]
1254 typeArrayOop eh = oopFactory::new_permanent_intArray(exception_table_length*4, CHECK_(nullHandle));
1255 typeArrayHandle exception_handlers = typeArrayHandle(THREAD, eh);
1257 int index = 0;
1258 cfs->guarantee_more(8 * exception_table_length, CHECK_(nullHandle)); // start_pc, end_pc, handler_pc, catch_type_index
1259 for (unsigned int i = 0; i < exception_table_length; i++) {
1260 u2 start_pc = cfs->get_u2_fast();
1261 u2 end_pc = cfs->get_u2_fast();
1262 u2 handler_pc = cfs->get_u2_fast();
1263 u2 catch_type_index = cfs->get_u2_fast();
1264 // Will check legal target after parsing code array in verifier.
1265 if (_need_verify) {
1266 guarantee_property((start_pc < end_pc) && (end_pc <= code_length),
1267 "Illegal exception table range in class file %s", CHECK_(nullHandle));
1268 guarantee_property(handler_pc < code_length,
1269 "Illegal exception table handler in class file %s", CHECK_(nullHandle));
1270 if (catch_type_index != 0) {
1271 guarantee_property(valid_cp_range(catch_type_index, cp->length()) &&
1272 is_klass_reference(cp, catch_type_index),
1273 "Catch type in exception table has bad constant type in class file %s", CHECK_(nullHandle));
1274 }
1275 }
1276 exception_handlers->int_at_put(index++, start_pc);
1277 exception_handlers->int_at_put(index++, end_pc);
1278 exception_handlers->int_at_put(index++, handler_pc);
1279 exception_handlers->int_at_put(index++, catch_type_index);
1280 }
1281 return exception_handlers;
1282 }
1284 void ClassFileParser::parse_linenumber_table(
1285 u4 code_attribute_length, u4 code_length,
1286 CompressedLineNumberWriteStream** write_stream, TRAPS) {
1287 ClassFileStream* cfs = stream();
1288 unsigned int num_entries = cfs->get_u2(CHECK);
1290 // Each entry is a u2 start_pc, and a u2 line_number
1291 unsigned int length_in_bytes = num_entries * (sizeof(u2) + sizeof(u2));
1293 // Verify line number attribute and table length
1294 check_property(
1295 code_attribute_length == sizeof(u2) + length_in_bytes,
1296 "LineNumberTable attribute has wrong length in class file %s", CHECK);
1298 cfs->guarantee_more(length_in_bytes, CHECK);
1300 if ((*write_stream) == NULL) {
1301 if (length_in_bytes > fixed_buffer_size) {
1302 (*write_stream) = new CompressedLineNumberWriteStream(length_in_bytes);
1303 } else {
1304 (*write_stream) = new CompressedLineNumberWriteStream(
1305 linenumbertable_buffer, fixed_buffer_size);
1306 }
1307 }
1309 while (num_entries-- > 0) {
1310 u2 bci = cfs->get_u2_fast(); // start_pc
1311 u2 line = cfs->get_u2_fast(); // line_number
1312 guarantee_property(bci < code_length,
1313 "Invalid pc in LineNumberTable in class file %s", CHECK);
1314 (*write_stream)->write_pair(bci, line);
1315 }
1316 }
1319 // Class file LocalVariableTable elements.
1320 class Classfile_LVT_Element VALUE_OBJ_CLASS_SPEC {
1321 public:
1322 u2 start_bci;
1323 u2 length;
1324 u2 name_cp_index;
1325 u2 descriptor_cp_index;
1326 u2 slot;
1327 };
1330 class LVT_Hash: public CHeapObj {
1331 public:
1332 LocalVariableTableElement *_elem; // element
1333 LVT_Hash* _next; // Next entry in hash table
1334 };
1336 unsigned int hash(LocalVariableTableElement *elem) {
1337 unsigned int raw_hash = elem->start_bci;
1339 raw_hash = elem->length + raw_hash * 37;
1340 raw_hash = elem->name_cp_index + raw_hash * 37;
1341 raw_hash = elem->slot + raw_hash * 37;
1343 return raw_hash % HASH_ROW_SIZE;
1344 }
1346 void initialize_hashtable(LVT_Hash** table) {
1347 for (int i = 0; i < HASH_ROW_SIZE; i++) {
1348 table[i] = NULL;
1349 }
1350 }
1352 void clear_hashtable(LVT_Hash** table) {
1353 for (int i = 0; i < HASH_ROW_SIZE; i++) {
1354 LVT_Hash* current = table[i];
1355 LVT_Hash* next;
1356 while (current != NULL) {
1357 next = current->_next;
1358 current->_next = NULL;
1359 delete(current);
1360 current = next;
1361 }
1362 table[i] = NULL;
1363 }
1364 }
1366 LVT_Hash* LVT_lookup(LocalVariableTableElement *elem, int index, LVT_Hash** table) {
1367 LVT_Hash* entry = table[index];
1369 /*
1370 * 3-tuple start_bci/length/slot has to be unique key,
1371 * so the following comparison seems to be redundant:
1372 * && elem->name_cp_index == entry->_elem->name_cp_index
1373 */
1374 while (entry != NULL) {
1375 if (elem->start_bci == entry->_elem->start_bci
1376 && elem->length == entry->_elem->length
1377 && elem->name_cp_index == entry->_elem->name_cp_index
1378 && elem->slot == entry->_elem->slot
1379 ) {
1380 return entry;
1381 }
1382 entry = entry->_next;
1383 }
1384 return NULL;
1385 }
1387 // Return false if the local variable is found in table.
1388 // Return true if no duplicate is found.
1389 // And local variable is added as a new entry in table.
1390 bool LVT_put_after_lookup(LocalVariableTableElement *elem, LVT_Hash** table) {
1391 // First lookup for duplicates
1392 int index = hash(elem);
1393 LVT_Hash* entry = LVT_lookup(elem, index, table);
1395 if (entry != NULL) {
1396 return false;
1397 }
1398 // No duplicate is found, allocate a new entry and fill it.
1399 if ((entry = new LVT_Hash()) == NULL) {
1400 return false;
1401 }
1402 entry->_elem = elem;
1404 // Insert into hash table
1405 entry->_next = table[index];
1406 table[index] = entry;
1408 return true;
1409 }
1411 void copy_lvt_element(Classfile_LVT_Element *src, LocalVariableTableElement *lvt) {
1412 lvt->start_bci = Bytes::get_Java_u2((u1*) &src->start_bci);
1413 lvt->length = Bytes::get_Java_u2((u1*) &src->length);
1414 lvt->name_cp_index = Bytes::get_Java_u2((u1*) &src->name_cp_index);
1415 lvt->descriptor_cp_index = Bytes::get_Java_u2((u1*) &src->descriptor_cp_index);
1416 lvt->signature_cp_index = 0;
1417 lvt->slot = Bytes::get_Java_u2((u1*) &src->slot);
1418 }
1420 // Function is used to parse both attributes:
1421 // LocalVariableTable (LVT) and LocalVariableTypeTable (LVTT)
1422 u2* ClassFileParser::parse_localvariable_table(u4 code_length,
1423 u2 max_locals,
1424 u4 code_attribute_length,
1425 constantPoolHandle cp,
1426 u2* localvariable_table_length,
1427 bool isLVTT,
1428 TRAPS) {
1429 ClassFileStream* cfs = stream();
1430 const char * tbl_name = (isLVTT) ? "LocalVariableTypeTable" : "LocalVariableTable";
1431 *localvariable_table_length = cfs->get_u2(CHECK_NULL);
1432 unsigned int size = (*localvariable_table_length) * sizeof(Classfile_LVT_Element) / sizeof(u2);
1433 // Verify local variable table attribute has right length
1434 if (_need_verify) {
1435 guarantee_property(code_attribute_length == (sizeof(*localvariable_table_length) + size * sizeof(u2)),
1436 "%s has wrong length in class file %s", tbl_name, CHECK_NULL);
1437 }
1438 u2* localvariable_table_start = cfs->get_u2_buffer();
1439 assert(localvariable_table_start != NULL, "null local variable table");
1440 if (!_need_verify) {
1441 cfs->skip_u2_fast(size);
1442 } else {
1443 cfs->guarantee_more(size * 2, CHECK_NULL);
1444 for(int i = 0; i < (*localvariable_table_length); i++) {
1445 u2 start_pc = cfs->get_u2_fast();
1446 u2 length = cfs->get_u2_fast();
1447 u2 name_index = cfs->get_u2_fast();
1448 u2 descriptor_index = cfs->get_u2_fast();
1449 u2 index = cfs->get_u2_fast();
1450 // Assign to a u4 to avoid overflow
1451 u4 end_pc = (u4)start_pc + (u4)length;
1453 if (start_pc >= code_length) {
1454 classfile_parse_error(
1455 "Invalid start_pc %u in %s in class file %s",
1456 start_pc, tbl_name, CHECK_NULL);
1457 }
1458 if (end_pc > code_length) {
1459 classfile_parse_error(
1460 "Invalid length %u in %s in class file %s",
1461 length, tbl_name, CHECK_NULL);
1462 }
1463 int cp_size = cp->length();
1464 guarantee_property(
1465 valid_cp_range(name_index, cp_size) &&
1466 cp->tag_at(name_index).is_utf8(),
1467 "Name index %u in %s has bad constant type in class file %s",
1468 name_index, tbl_name, CHECK_NULL);
1469 guarantee_property(
1470 valid_cp_range(descriptor_index, cp_size) &&
1471 cp->tag_at(descriptor_index).is_utf8(),
1472 "Signature index %u in %s has bad constant type in class file %s",
1473 descriptor_index, tbl_name, CHECK_NULL);
1475 Symbol* name = cp->symbol_at(name_index);
1476 Symbol* sig = cp->symbol_at(descriptor_index);
1477 verify_legal_field_name(name, CHECK_NULL);
1478 u2 extra_slot = 0;
1479 if (!isLVTT) {
1480 verify_legal_field_signature(name, sig, CHECK_NULL);
1482 // 4894874: check special cases for double and long local variables
1483 if (sig == vmSymbols::type_signature(T_DOUBLE) ||
1484 sig == vmSymbols::type_signature(T_LONG)) {
1485 extra_slot = 1;
1486 }
1487 }
1488 guarantee_property((index + extra_slot) < max_locals,
1489 "Invalid index %u in %s in class file %s",
1490 index, tbl_name, CHECK_NULL);
1491 }
1492 }
1493 return localvariable_table_start;
1494 }
1497 void ClassFileParser::parse_type_array(u2 array_length, u4 code_length, u4* u1_index, u4* u2_index,
1498 u1* u1_array, u2* u2_array, constantPoolHandle cp, TRAPS) {
1499 ClassFileStream* cfs = stream();
1500 u2 index = 0; // index in the array with long/double occupying two slots
1501 u4 i1 = *u1_index;
1502 u4 i2 = *u2_index + 1;
1503 for(int i = 0; i < array_length; i++) {
1504 u1 tag = u1_array[i1++] = cfs->get_u1(CHECK);
1505 index++;
1506 if (tag == ITEM_Long || tag == ITEM_Double) {
1507 index++;
1508 } else if (tag == ITEM_Object) {
1509 u2 class_index = u2_array[i2++] = cfs->get_u2(CHECK);
1510 guarantee_property(valid_cp_range(class_index, cp->length()) &&
1511 is_klass_reference(cp, class_index),
1512 "Bad class index %u in StackMap in class file %s",
1513 class_index, CHECK);
1514 } else if (tag == ITEM_Uninitialized) {
1515 u2 offset = u2_array[i2++] = cfs->get_u2(CHECK);
1516 guarantee_property(
1517 offset < code_length,
1518 "Bad uninitialized type offset %u in StackMap in class file %s",
1519 offset, CHECK);
1520 } else {
1521 guarantee_property(
1522 tag <= (u1)ITEM_Uninitialized,
1523 "Unknown variable type %u in StackMap in class file %s",
1524 tag, CHECK);
1525 }
1526 }
1527 u2_array[*u2_index] = index;
1528 *u1_index = i1;
1529 *u2_index = i2;
1530 }
1532 typeArrayOop ClassFileParser::parse_stackmap_table(
1533 u4 code_attribute_length, TRAPS) {
1534 if (code_attribute_length == 0)
1535 return NULL;
1537 ClassFileStream* cfs = stream();
1538 u1* stackmap_table_start = cfs->get_u1_buffer();
1539 assert(stackmap_table_start != NULL, "null stackmap table");
1541 // check code_attribute_length first
1542 stream()->skip_u1(code_attribute_length, CHECK_NULL);
1544 if (!_need_verify && !DumpSharedSpaces) {
1545 return NULL;
1546 }
1548 typeArrayOop stackmap_data =
1549 oopFactory::new_permanent_byteArray(code_attribute_length, CHECK_NULL);
1551 stackmap_data->set_length(code_attribute_length);
1552 memcpy((void*)stackmap_data->byte_at_addr(0),
1553 (void*)stackmap_table_start, code_attribute_length);
1554 return stackmap_data;
1555 }
1557 u2* ClassFileParser::parse_checked_exceptions(u2* checked_exceptions_length,
1558 u4 method_attribute_length,
1559 constantPoolHandle cp, TRAPS) {
1560 ClassFileStream* cfs = stream();
1561 cfs->guarantee_more(2, CHECK_NULL); // checked_exceptions_length
1562 *checked_exceptions_length = cfs->get_u2_fast();
1563 unsigned int size = (*checked_exceptions_length) * sizeof(CheckedExceptionElement) / sizeof(u2);
1564 u2* checked_exceptions_start = cfs->get_u2_buffer();
1565 assert(checked_exceptions_start != NULL, "null checked exceptions");
1566 if (!_need_verify) {
1567 cfs->skip_u2_fast(size);
1568 } else {
1569 // Verify each value in the checked exception table
1570 u2 checked_exception;
1571 u2 len = *checked_exceptions_length;
1572 cfs->guarantee_more(2 * len, CHECK_NULL);
1573 for (int i = 0; i < len; i++) {
1574 checked_exception = cfs->get_u2_fast();
1575 check_property(
1576 valid_cp_range(checked_exception, cp->length()) &&
1577 is_klass_reference(cp, checked_exception),
1578 "Exception name has bad type at constant pool %u in class file %s",
1579 checked_exception, CHECK_NULL);
1580 }
1581 }
1582 // check exceptions attribute length
1583 if (_need_verify) {
1584 guarantee_property(method_attribute_length == (sizeof(*checked_exceptions_length) +
1585 sizeof(u2) * size),
1586 "Exceptions attribute has wrong length in class file %s", CHECK_NULL);
1587 }
1588 return checked_exceptions_start;
1589 }
1591 void ClassFileParser::throwIllegalSignature(
1592 const char* type, Symbol* name, Symbol* sig, TRAPS) {
1593 ResourceMark rm(THREAD);
1594 Exceptions::fthrow(THREAD_AND_LOCATION,
1595 vmSymbols::java_lang_ClassFormatError(),
1596 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1597 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1598 }
1600 #define MAX_ARGS_SIZE 255
1601 #define MAX_CODE_SIZE 65535
1602 #define INITIAL_MAX_LVT_NUMBER 256
1604 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
1605 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the
1606 // methodOop to save footprint, so we only know the size of the resulting methodOop when the
1607 // entire method attribute is parsed.
1608 //
1609 // The promoted_flags parameter is used to pass relevant access_flags
1610 // from the method back up to the containing klass. These flag values
1611 // are added to klass's access_flags.
1613 methodHandle ClassFileParser::parse_method(constantPoolHandle cp, bool is_interface,
1614 AccessFlags *promoted_flags,
1615 typeArrayHandle* method_annotations,
1616 typeArrayHandle* method_parameter_annotations,
1617 typeArrayHandle* method_default_annotations,
1618 TRAPS) {
1619 ClassFileStream* cfs = stream();
1620 methodHandle nullHandle;
1621 ResourceMark rm(THREAD);
1622 // Parse fixed parts
1623 cfs->guarantee_more(8, CHECK_(nullHandle)); // access_flags, name_index, descriptor_index, attributes_count
1625 int flags = cfs->get_u2_fast();
1626 u2 name_index = cfs->get_u2_fast();
1627 int cp_size = cp->length();
1628 check_property(
1629 valid_cp_range(name_index, cp_size) &&
1630 cp->tag_at(name_index).is_utf8(),
1631 "Illegal constant pool index %u for method name in class file %s",
1632 name_index, CHECK_(nullHandle));
1633 Symbol* name = cp->symbol_at(name_index);
1634 verify_legal_method_name(name, CHECK_(nullHandle));
1636 u2 signature_index = cfs->get_u2_fast();
1637 guarantee_property(
1638 valid_cp_range(signature_index, cp_size) &&
1639 cp->tag_at(signature_index).is_utf8(),
1640 "Illegal constant pool index %u for method signature in class file %s",
1641 signature_index, CHECK_(nullHandle));
1642 Symbol* signature = cp->symbol_at(signature_index);
1644 AccessFlags access_flags;
1645 if (name == vmSymbols::class_initializer_name()) {
1646 // We ignore the other access flags for a valid class initializer.
1647 // (JVM Spec 2nd ed., chapter 4.6)
1648 if (_major_version < 51) { // backward compatibility
1649 flags = JVM_ACC_STATIC;
1650 } else if ((flags & JVM_ACC_STATIC) == JVM_ACC_STATIC) {
1651 flags &= JVM_ACC_STATIC | JVM_ACC_STRICT;
1652 }
1653 } else {
1654 verify_legal_method_modifiers(flags, is_interface, name, CHECK_(nullHandle));
1655 }
1657 int args_size = -1; // only used when _need_verify is true
1658 if (_need_verify) {
1659 args_size = ((flags & JVM_ACC_STATIC) ? 0 : 1) +
1660 verify_legal_method_signature(name, signature, CHECK_(nullHandle));
1661 if (args_size > MAX_ARGS_SIZE) {
1662 classfile_parse_error("Too many arguments in method signature in class file %s", CHECK_(nullHandle));
1663 }
1664 }
1666 access_flags.set_flags(flags & JVM_RECOGNIZED_METHOD_MODIFIERS);
1668 // Default values for code and exceptions attribute elements
1669 u2 max_stack = 0;
1670 u2 max_locals = 0;
1671 u4 code_length = 0;
1672 u1* code_start = 0;
1673 u2 exception_table_length = 0;
1674 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1675 u2 checked_exceptions_length = 0;
1676 u2* checked_exceptions_start = NULL;
1677 CompressedLineNumberWriteStream* linenumber_table = NULL;
1678 int linenumber_table_length = 0;
1679 int total_lvt_length = 0;
1680 u2 lvt_cnt = 0;
1681 u2 lvtt_cnt = 0;
1682 bool lvt_allocated = false;
1683 u2 max_lvt_cnt = INITIAL_MAX_LVT_NUMBER;
1684 u2 max_lvtt_cnt = INITIAL_MAX_LVT_NUMBER;
1685 u2* localvariable_table_length;
1686 u2** localvariable_table_start;
1687 u2* localvariable_type_table_length;
1688 u2** localvariable_type_table_start;
1689 bool parsed_code_attribute = false;
1690 bool parsed_checked_exceptions_attribute = false;
1691 bool parsed_stackmap_attribute = false;
1692 // stackmap attribute - JDK1.5
1693 typeArrayHandle stackmap_data;
1694 u2 generic_signature_index = 0;
1695 u1* runtime_visible_annotations = NULL;
1696 int runtime_visible_annotations_length = 0;
1697 u1* runtime_invisible_annotations = NULL;
1698 int runtime_invisible_annotations_length = 0;
1699 u1* runtime_visible_parameter_annotations = NULL;
1700 int runtime_visible_parameter_annotations_length = 0;
1701 u1* runtime_invisible_parameter_annotations = NULL;
1702 int runtime_invisible_parameter_annotations_length = 0;
1703 u1* annotation_default = NULL;
1704 int annotation_default_length = 0;
1706 // Parse code and exceptions attribute
1707 u2 method_attributes_count = cfs->get_u2_fast();
1708 while (method_attributes_count--) {
1709 cfs->guarantee_more(6, CHECK_(nullHandle)); // method_attribute_name_index, method_attribute_length
1710 u2 method_attribute_name_index = cfs->get_u2_fast();
1711 u4 method_attribute_length = cfs->get_u4_fast();
1712 check_property(
1713 valid_cp_range(method_attribute_name_index, cp_size) &&
1714 cp->tag_at(method_attribute_name_index).is_utf8(),
1715 "Invalid method attribute name index %u in class file %s",
1716 method_attribute_name_index, CHECK_(nullHandle));
1718 Symbol* method_attribute_name = cp->symbol_at(method_attribute_name_index);
1719 if (method_attribute_name == vmSymbols::tag_code()) {
1720 // Parse Code attribute
1721 if (_need_verify) {
1722 guarantee_property(!access_flags.is_native() && !access_flags.is_abstract(),
1723 "Code attribute in native or abstract methods in class file %s",
1724 CHECK_(nullHandle));
1725 }
1726 if (parsed_code_attribute) {
1727 classfile_parse_error("Multiple Code attributes in class file %s", CHECK_(nullHandle));
1728 }
1729 parsed_code_attribute = true;
1731 // Stack size, locals size, and code size
1732 if (_major_version == 45 && _minor_version <= 2) {
1733 cfs->guarantee_more(4, CHECK_(nullHandle));
1734 max_stack = cfs->get_u1_fast();
1735 max_locals = cfs->get_u1_fast();
1736 code_length = cfs->get_u2_fast();
1737 } else {
1738 cfs->guarantee_more(8, CHECK_(nullHandle));
1739 max_stack = cfs->get_u2_fast();
1740 max_locals = cfs->get_u2_fast();
1741 code_length = cfs->get_u4_fast();
1742 }
1743 if (_need_verify) {
1744 guarantee_property(args_size <= max_locals,
1745 "Arguments can't fit into locals in class file %s", CHECK_(nullHandle));
1746 guarantee_property(code_length > 0 && code_length <= MAX_CODE_SIZE,
1747 "Invalid method Code length %u in class file %s",
1748 code_length, CHECK_(nullHandle));
1749 }
1750 // Code pointer
1751 code_start = cfs->get_u1_buffer();
1752 assert(code_start != NULL, "null code start");
1753 cfs->guarantee_more(code_length, CHECK_(nullHandle));
1754 cfs->skip_u1_fast(code_length);
1756 // Exception handler table
1757 cfs->guarantee_more(2, CHECK_(nullHandle)); // exception_table_length
1758 exception_table_length = cfs->get_u2_fast();
1759 if (exception_table_length > 0) {
1760 exception_handlers =
1761 parse_exception_table(code_length, exception_table_length, cp, CHECK_(nullHandle));
1762 }
1764 // Parse additional attributes in code attribute
1765 cfs->guarantee_more(2, CHECK_(nullHandle)); // code_attributes_count
1766 u2 code_attributes_count = cfs->get_u2_fast();
1768 unsigned int calculated_attribute_length = 0;
1770 if (_major_version > 45 || (_major_version == 45 && _minor_version > 2)) {
1771 calculated_attribute_length =
1772 sizeof(max_stack) + sizeof(max_locals) + sizeof(code_length);
1773 } else {
1774 // max_stack, locals and length are smaller in pre-version 45.2 classes
1775 calculated_attribute_length = sizeof(u1) + sizeof(u1) + sizeof(u2);
1776 }
1777 calculated_attribute_length +=
1778 code_length +
1779 sizeof(exception_table_length) +
1780 sizeof(code_attributes_count) +
1781 exception_table_length *
1782 ( sizeof(u2) + // start_pc
1783 sizeof(u2) + // end_pc
1784 sizeof(u2) + // handler_pc
1785 sizeof(u2) ); // catch_type_index
1787 while (code_attributes_count--) {
1788 cfs->guarantee_more(6, CHECK_(nullHandle)); // code_attribute_name_index, code_attribute_length
1789 u2 code_attribute_name_index = cfs->get_u2_fast();
1790 u4 code_attribute_length = cfs->get_u4_fast();
1791 calculated_attribute_length += code_attribute_length +
1792 sizeof(code_attribute_name_index) +
1793 sizeof(code_attribute_length);
1794 check_property(valid_cp_range(code_attribute_name_index, cp_size) &&
1795 cp->tag_at(code_attribute_name_index).is_utf8(),
1796 "Invalid code attribute name index %u in class file %s",
1797 code_attribute_name_index,
1798 CHECK_(nullHandle));
1799 if (LoadLineNumberTables &&
1800 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_line_number_table()) {
1801 // Parse and compress line number table
1802 parse_linenumber_table(code_attribute_length, code_length,
1803 &linenumber_table, CHECK_(nullHandle));
1805 } else if (LoadLocalVariableTables &&
1806 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_table()) {
1807 // Parse local variable table
1808 if (!lvt_allocated) {
1809 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1810 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1811 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1812 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1813 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1814 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1815 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1816 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1817 lvt_allocated = true;
1818 }
1819 if (lvt_cnt == max_lvt_cnt) {
1820 max_lvt_cnt <<= 1;
1821 REALLOC_RESOURCE_ARRAY(u2, localvariable_table_length, lvt_cnt, max_lvt_cnt);
1822 REALLOC_RESOURCE_ARRAY(u2*, localvariable_table_start, lvt_cnt, max_lvt_cnt);
1823 }
1824 localvariable_table_start[lvt_cnt] =
1825 parse_localvariable_table(code_length,
1826 max_locals,
1827 code_attribute_length,
1828 cp,
1829 &localvariable_table_length[lvt_cnt],
1830 false, // is not LVTT
1831 CHECK_(nullHandle));
1832 total_lvt_length += localvariable_table_length[lvt_cnt];
1833 lvt_cnt++;
1834 } else if (LoadLocalVariableTypeTables &&
1835 _major_version >= JAVA_1_5_VERSION &&
1836 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_local_variable_type_table()) {
1837 if (!lvt_allocated) {
1838 localvariable_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1839 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1840 localvariable_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1841 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1842 localvariable_type_table_length = NEW_RESOURCE_ARRAY_IN_THREAD(
1843 THREAD, u2, INITIAL_MAX_LVT_NUMBER);
1844 localvariable_type_table_start = NEW_RESOURCE_ARRAY_IN_THREAD(
1845 THREAD, u2*, INITIAL_MAX_LVT_NUMBER);
1846 lvt_allocated = true;
1847 }
1848 // Parse local variable type table
1849 if (lvtt_cnt == max_lvtt_cnt) {
1850 max_lvtt_cnt <<= 1;
1851 REALLOC_RESOURCE_ARRAY(u2, localvariable_type_table_length, lvtt_cnt, max_lvtt_cnt);
1852 REALLOC_RESOURCE_ARRAY(u2*, localvariable_type_table_start, lvtt_cnt, max_lvtt_cnt);
1853 }
1854 localvariable_type_table_start[lvtt_cnt] =
1855 parse_localvariable_table(code_length,
1856 max_locals,
1857 code_attribute_length,
1858 cp,
1859 &localvariable_type_table_length[lvtt_cnt],
1860 true, // is LVTT
1861 CHECK_(nullHandle));
1862 lvtt_cnt++;
1863 } else if (UseSplitVerifier &&
1864 _major_version >= Verifier::STACKMAP_ATTRIBUTE_MAJOR_VERSION &&
1865 cp->symbol_at(code_attribute_name_index) == vmSymbols::tag_stack_map_table()) {
1866 // Stack map is only needed by the new verifier in JDK1.5.
1867 if (parsed_stackmap_attribute) {
1868 classfile_parse_error("Multiple StackMapTable attributes in class file %s", CHECK_(nullHandle));
1869 }
1870 typeArrayOop sm =
1871 parse_stackmap_table(code_attribute_length, CHECK_(nullHandle));
1872 stackmap_data = typeArrayHandle(THREAD, sm);
1873 parsed_stackmap_attribute = true;
1874 } else {
1875 // Skip unknown attributes
1876 cfs->skip_u1(code_attribute_length, CHECK_(nullHandle));
1877 }
1878 }
1879 // check method attribute length
1880 if (_need_verify) {
1881 guarantee_property(method_attribute_length == calculated_attribute_length,
1882 "Code segment has wrong length in class file %s", CHECK_(nullHandle));
1883 }
1884 } else if (method_attribute_name == vmSymbols::tag_exceptions()) {
1885 // Parse Exceptions attribute
1886 if (parsed_checked_exceptions_attribute) {
1887 classfile_parse_error("Multiple Exceptions attributes in class file %s", CHECK_(nullHandle));
1888 }
1889 parsed_checked_exceptions_attribute = true;
1890 checked_exceptions_start =
1891 parse_checked_exceptions(&checked_exceptions_length,
1892 method_attribute_length,
1893 cp, CHECK_(nullHandle));
1894 } else if (method_attribute_name == vmSymbols::tag_synthetic()) {
1895 if (method_attribute_length != 0) {
1896 classfile_parse_error(
1897 "Invalid Synthetic method attribute length %u in class file %s",
1898 method_attribute_length, CHECK_(nullHandle));
1899 }
1900 // Should we check that there hasn't already been a synthetic attribute?
1901 access_flags.set_is_synthetic();
1902 } else if (method_attribute_name == vmSymbols::tag_deprecated()) { // 4276120
1903 if (method_attribute_length != 0) {
1904 classfile_parse_error(
1905 "Invalid Deprecated method attribute length %u in class file %s",
1906 method_attribute_length, CHECK_(nullHandle));
1907 }
1908 } else if (_major_version >= JAVA_1_5_VERSION) {
1909 if (method_attribute_name == vmSymbols::tag_signature()) {
1910 if (method_attribute_length != 2) {
1911 classfile_parse_error(
1912 "Invalid Signature attribute length %u in class file %s",
1913 method_attribute_length, CHECK_(nullHandle));
1914 }
1915 cfs->guarantee_more(2, CHECK_(nullHandle)); // generic_signature_index
1916 generic_signature_index = cfs->get_u2_fast();
1917 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1918 runtime_visible_annotations_length = method_attribute_length;
1919 runtime_visible_annotations = cfs->get_u1_buffer();
1920 assert(runtime_visible_annotations != NULL, "null visible annotations");
1921 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
1922 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1923 runtime_invisible_annotations_length = method_attribute_length;
1924 runtime_invisible_annotations = cfs->get_u1_buffer();
1925 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
1926 cfs->skip_u1(runtime_invisible_annotations_length, CHECK_(nullHandle));
1927 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_parameter_annotations()) {
1928 runtime_visible_parameter_annotations_length = method_attribute_length;
1929 runtime_visible_parameter_annotations = cfs->get_u1_buffer();
1930 assert(runtime_visible_parameter_annotations != NULL, "null visible parameter annotations");
1931 cfs->skip_u1(runtime_visible_parameter_annotations_length, CHECK_(nullHandle));
1932 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_parameter_annotations()) {
1933 runtime_invisible_parameter_annotations_length = method_attribute_length;
1934 runtime_invisible_parameter_annotations = cfs->get_u1_buffer();
1935 assert(runtime_invisible_parameter_annotations != NULL, "null invisible parameter annotations");
1936 cfs->skip_u1(runtime_invisible_parameter_annotations_length, CHECK_(nullHandle));
1937 } else if (method_attribute_name == vmSymbols::tag_annotation_default()) {
1938 annotation_default_length = method_attribute_length;
1939 annotation_default = cfs->get_u1_buffer();
1940 assert(annotation_default != NULL, "null annotation default");
1941 cfs->skip_u1(annotation_default_length, CHECK_(nullHandle));
1942 } else {
1943 // Skip unknown attributes
1944 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1945 }
1946 } else {
1947 // Skip unknown attributes
1948 cfs->skip_u1(method_attribute_length, CHECK_(nullHandle));
1949 }
1950 }
1952 if (linenumber_table != NULL) {
1953 linenumber_table->write_terminator();
1954 linenumber_table_length = linenumber_table->position();
1955 }
1957 // Make sure there's at least one Code attribute in non-native/non-abstract method
1958 if (_need_verify) {
1959 guarantee_property(access_flags.is_native() || access_flags.is_abstract() || parsed_code_attribute,
1960 "Absent Code attribute in method that is not native or abstract in class file %s", CHECK_(nullHandle));
1961 }
1963 // All sizing information for a methodOop is finally available, now create it
1964 methodOop m_oop = oopFactory::new_method(code_length, access_flags, linenumber_table_length,
1965 total_lvt_length, checked_exceptions_length,
1966 oopDesc::IsSafeConc, CHECK_(nullHandle));
1967 methodHandle m (THREAD, m_oop);
1969 ClassLoadingService::add_class_method_size(m_oop->size()*HeapWordSize);
1971 // Fill in information from fixed part (access_flags already set)
1972 m->set_constants(cp());
1973 m->set_name_index(name_index);
1974 m->set_signature_index(signature_index);
1975 m->set_generic_signature_index(generic_signature_index);
1976 #ifdef CC_INTERP
1977 // hmm is there a gc issue here??
1978 ResultTypeFinder rtf(cp->symbol_at(signature_index));
1979 m->set_result_index(rtf.type());
1980 #endif
1982 if (args_size >= 0) {
1983 m->set_size_of_parameters(args_size);
1984 } else {
1985 m->compute_size_of_parameters(THREAD);
1986 }
1987 #ifdef ASSERT
1988 if (args_size >= 0) {
1989 m->compute_size_of_parameters(THREAD);
1990 assert(args_size == m->size_of_parameters(), "");
1991 }
1992 #endif
1994 // Fill in code attribute information
1995 m->set_max_stack(max_stack);
1996 m->set_max_locals(max_locals);
1997 m->constMethod()->set_stackmap_data(stackmap_data());
1999 /**
2000 * The exception_table field is the flag used to indicate
2001 * that the methodOop and it's associated constMethodOop are partially
2002 * initialized and thus are exempt from pre/post GC verification. Once
2003 * the field is set, the oops are considered fully initialized so make
2004 * sure that the oops can pass verification when this field is set.
2005 */
2006 m->set_exception_table(exception_handlers());
2008 // Copy byte codes
2009 m->set_code(code_start);
2011 // Copy line number table
2012 if (linenumber_table != NULL) {
2013 memcpy(m->compressed_linenumber_table(),
2014 linenumber_table->buffer(), linenumber_table_length);
2015 }
2017 // Copy checked exceptions
2018 if (checked_exceptions_length > 0) {
2019 int size = checked_exceptions_length * sizeof(CheckedExceptionElement) / sizeof(u2);
2020 copy_u2_with_conversion((u2*) m->checked_exceptions_start(), checked_exceptions_start, size);
2021 }
2023 /* Copy class file LVT's/LVTT's into the HotSpot internal LVT.
2024 *
2025 * Rules for LVT's and LVTT's are:
2026 * - There can be any number of LVT's and LVTT's.
2027 * - If there are n LVT's, it is the same as if there was just
2028 * one LVT containing all the entries from the n LVT's.
2029 * - There may be no more than one LVT entry per local variable.
2030 * Two LVT entries are 'equal' if these fields are the same:
2031 * start_pc, length, name, slot
2032 * - There may be no more than one LVTT entry per each LVT entry.
2033 * Each LVTT entry has to match some LVT entry.
2034 * - HotSpot internal LVT keeps natural ordering of class file LVT entries.
2035 */
2036 if (total_lvt_length > 0) {
2037 int tbl_no, idx;
2039 promoted_flags->set_has_localvariable_table();
2041 LVT_Hash** lvt_Hash = NEW_RESOURCE_ARRAY(LVT_Hash*, HASH_ROW_SIZE);
2042 initialize_hashtable(lvt_Hash);
2044 // To fill LocalVariableTable in
2045 Classfile_LVT_Element* cf_lvt;
2046 LocalVariableTableElement* lvt = m->localvariable_table_start();
2048 for (tbl_no = 0; tbl_no < lvt_cnt; tbl_no++) {
2049 cf_lvt = (Classfile_LVT_Element *) localvariable_table_start[tbl_no];
2050 for (idx = 0; idx < localvariable_table_length[tbl_no]; idx++, lvt++) {
2051 copy_lvt_element(&cf_lvt[idx], lvt);
2052 // If no duplicates, add LVT elem in hashtable lvt_Hash.
2053 if (LVT_put_after_lookup(lvt, lvt_Hash) == false
2054 && _need_verify
2055 && _major_version >= JAVA_1_5_VERSION ) {
2056 clear_hashtable(lvt_Hash);
2057 classfile_parse_error("Duplicated LocalVariableTable attribute "
2058 "entry for '%s' in class file %s",
2059 cp->symbol_at(lvt->name_cp_index)->as_utf8(),
2060 CHECK_(nullHandle));
2061 }
2062 }
2063 }
2065 // To merge LocalVariableTable and LocalVariableTypeTable
2066 Classfile_LVT_Element* cf_lvtt;
2067 LocalVariableTableElement lvtt_elem;
2069 for (tbl_no = 0; tbl_no < lvtt_cnt; tbl_no++) {
2070 cf_lvtt = (Classfile_LVT_Element *) localvariable_type_table_start[tbl_no];
2071 for (idx = 0; idx < localvariable_type_table_length[tbl_no]; idx++) {
2072 copy_lvt_element(&cf_lvtt[idx], &lvtt_elem);
2073 int index = hash(&lvtt_elem);
2074 LVT_Hash* entry = LVT_lookup(&lvtt_elem, index, lvt_Hash);
2075 if (entry == NULL) {
2076 if (_need_verify) {
2077 clear_hashtable(lvt_Hash);
2078 classfile_parse_error("LVTT entry for '%s' in class file %s "
2079 "does not match any LVT entry",
2080 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
2081 CHECK_(nullHandle));
2082 }
2083 } else if (entry->_elem->signature_cp_index != 0 && _need_verify) {
2084 clear_hashtable(lvt_Hash);
2085 classfile_parse_error("Duplicated LocalVariableTypeTable attribute "
2086 "entry for '%s' in class file %s",
2087 cp->symbol_at(lvtt_elem.name_cp_index)->as_utf8(),
2088 CHECK_(nullHandle));
2089 } else {
2090 // to add generic signatures into LocalVariableTable
2091 entry->_elem->signature_cp_index = lvtt_elem.descriptor_cp_index;
2092 }
2093 }
2094 }
2095 clear_hashtable(lvt_Hash);
2096 }
2098 *method_annotations = assemble_annotations(runtime_visible_annotations,
2099 runtime_visible_annotations_length,
2100 runtime_invisible_annotations,
2101 runtime_invisible_annotations_length,
2102 CHECK_(nullHandle));
2103 *method_parameter_annotations = assemble_annotations(runtime_visible_parameter_annotations,
2104 runtime_visible_parameter_annotations_length,
2105 runtime_invisible_parameter_annotations,
2106 runtime_invisible_parameter_annotations_length,
2107 CHECK_(nullHandle));
2108 *method_default_annotations = assemble_annotations(annotation_default,
2109 annotation_default_length,
2110 NULL,
2111 0,
2112 CHECK_(nullHandle));
2114 if (name == vmSymbols::finalize_method_name() &&
2115 signature == vmSymbols::void_method_signature()) {
2116 if (m->is_empty_method()) {
2117 _has_empty_finalizer = true;
2118 } else {
2119 _has_finalizer = true;
2120 }
2121 }
2122 if (name == vmSymbols::object_initializer_name() &&
2123 signature == vmSymbols::void_method_signature() &&
2124 m->is_vanilla_constructor()) {
2125 _has_vanilla_constructor = true;
2126 }
2128 if (EnableInvokeDynamic && (m->is_method_handle_invoke() ||
2129 m->is_method_handle_adapter())) {
2130 THROW_MSG_(vmSymbols::java_lang_VirtualMachineError(),
2131 "Method handle invokers must be defined internally to the VM", nullHandle);
2132 }
2134 return m;
2135 }
2138 // The promoted_flags parameter is used to pass relevant access_flags
2139 // from the methods back up to the containing klass. These flag values
2140 // are added to klass's access_flags.
2142 objArrayHandle ClassFileParser::parse_methods(constantPoolHandle cp, bool is_interface,
2143 AccessFlags* promoted_flags,
2144 bool* has_final_method,
2145 objArrayOop* methods_annotations_oop,
2146 objArrayOop* methods_parameter_annotations_oop,
2147 objArrayOop* methods_default_annotations_oop,
2148 TRAPS) {
2149 ClassFileStream* cfs = stream();
2150 objArrayHandle nullHandle;
2151 typeArrayHandle method_annotations;
2152 typeArrayHandle method_parameter_annotations;
2153 typeArrayHandle method_default_annotations;
2154 cfs->guarantee_more(2, CHECK_(nullHandle)); // length
2155 u2 length = cfs->get_u2_fast();
2156 if (length == 0) {
2157 return objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2158 } else {
2159 objArrayOop m = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
2160 objArrayHandle methods(THREAD, m);
2161 HandleMark hm(THREAD);
2162 objArrayHandle methods_annotations;
2163 objArrayHandle methods_parameter_annotations;
2164 objArrayHandle methods_default_annotations;
2165 for (int index = 0; index < length; index++) {
2166 methodHandle method = parse_method(cp, is_interface,
2167 promoted_flags,
2168 &method_annotations,
2169 &method_parameter_annotations,
2170 &method_default_annotations,
2171 CHECK_(nullHandle));
2172 if (method->is_final()) {
2173 *has_final_method = true;
2174 }
2175 methods->obj_at_put(index, method());
2176 if (method_annotations.not_null()) {
2177 if (methods_annotations.is_null()) {
2178 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
2179 methods_annotations = objArrayHandle(THREAD, md);
2180 }
2181 methods_annotations->obj_at_put(index, method_annotations());
2182 }
2183 if (method_parameter_annotations.not_null()) {
2184 if (methods_parameter_annotations.is_null()) {
2185 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
2186 methods_parameter_annotations = objArrayHandle(THREAD, md);
2187 }
2188 methods_parameter_annotations->obj_at_put(index, method_parameter_annotations());
2189 }
2190 if (method_default_annotations.not_null()) {
2191 if (methods_default_annotations.is_null()) {
2192 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
2193 methods_default_annotations = objArrayHandle(THREAD, md);
2194 }
2195 methods_default_annotations->obj_at_put(index, method_default_annotations());
2196 }
2197 }
2198 if (_need_verify && length > 1) {
2199 // Check duplicated methods
2200 ResourceMark rm(THREAD);
2201 NameSigHash** names_and_sigs = NEW_RESOURCE_ARRAY_IN_THREAD(
2202 THREAD, NameSigHash*, HASH_ROW_SIZE);
2203 initialize_hashtable(names_and_sigs);
2204 bool dup = false;
2205 {
2206 debug_only(No_Safepoint_Verifier nsv;)
2207 for (int i = 0; i < length; i++) {
2208 methodOop m = (methodOop)methods->obj_at(i);
2209 // If no duplicates, add name/signature in hashtable names_and_sigs.
2210 if (!put_after_lookup(m->name(), m->signature(), names_and_sigs)) {
2211 dup = true;
2212 break;
2213 }
2214 }
2215 }
2216 if (dup) {
2217 classfile_parse_error("Duplicate method name&signature in class file %s",
2218 CHECK_(nullHandle));
2219 }
2220 }
2222 *methods_annotations_oop = methods_annotations();
2223 *methods_parameter_annotations_oop = methods_parameter_annotations();
2224 *methods_default_annotations_oop = methods_default_annotations();
2226 return methods;
2227 }
2228 }
2231 typeArrayHandle ClassFileParser::sort_methods(objArrayHandle methods,
2232 objArrayHandle methods_annotations,
2233 objArrayHandle methods_parameter_annotations,
2234 objArrayHandle methods_default_annotations,
2235 TRAPS) {
2236 typeArrayHandle nullHandle;
2237 int length = methods()->length();
2238 // If JVMTI original method ordering or sharing is enabled we have to
2239 // remember the original class file ordering.
2240 // We temporarily use the vtable_index field in the methodOop to store the
2241 // class file index, so we can read in after calling qsort.
2242 // Put the method ordering in the shared archive.
2243 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) {
2244 for (int index = 0; index < length; index++) {
2245 methodOop m = methodOop(methods->obj_at(index));
2246 assert(!m->valid_vtable_index(), "vtable index should not be set");
2247 m->set_vtable_index(index);
2248 }
2249 }
2250 // Sort method array by ascending method name (for faster lookups & vtable construction)
2251 // Note that the ordering is not alphabetical, see Symbol::fast_compare
2252 methodOopDesc::sort_methods(methods(),
2253 methods_annotations(),
2254 methods_parameter_annotations(),
2255 methods_default_annotations());
2257 // If JVMTI original method ordering or sharing is enabled construct int
2258 // array remembering the original ordering
2259 if (JvmtiExport::can_maintain_original_method_order() || DumpSharedSpaces) {
2260 typeArrayOop new_ordering = oopFactory::new_permanent_intArray(length, CHECK_(nullHandle));
2261 typeArrayHandle method_ordering(THREAD, new_ordering);
2262 for (int index = 0; index < length; index++) {
2263 methodOop m = methodOop(methods->obj_at(index));
2264 int old_index = m->vtable_index();
2265 assert(old_index >= 0 && old_index < length, "invalid method index");
2266 method_ordering->int_at_put(index, old_index);
2267 m->set_vtable_index(methodOopDesc::invalid_vtable_index);
2268 }
2269 return method_ordering;
2270 } else {
2271 return typeArrayHandle(THREAD, Universe::the_empty_int_array());
2272 }
2273 }
2276 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2277 ClassFileStream* cfs = stream();
2278 cfs->guarantee_more(2, CHECK); // sourcefile_index
2279 u2 sourcefile_index = cfs->get_u2_fast();
2280 check_property(
2281 valid_cp_range(sourcefile_index, cp->length()) &&
2282 cp->tag_at(sourcefile_index).is_utf8(),
2283 "Invalid SourceFile attribute at constant pool index %u in class file %s",
2284 sourcefile_index, CHECK);
2285 k->set_source_file_name(cp->symbol_at(sourcefile_index));
2286 }
2290 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
2291 instanceKlassHandle k,
2292 int length, TRAPS) {
2293 ClassFileStream* cfs = stream();
2294 u1* sde_buffer = cfs->get_u1_buffer();
2295 assert(sde_buffer != NULL, "null sde buffer");
2297 // Don't bother storing it if there is no way to retrieve it
2298 if (JvmtiExport::can_get_source_debug_extension()) {
2299 // Optimistically assume that only 1 byte UTF format is used
2300 // (common case)
2301 TempNewSymbol sde_symbol = SymbolTable::new_symbol((const char*)sde_buffer, length, CHECK);
2302 k->set_source_debug_extension(sde_symbol);
2303 // Note that set_source_debug_extension() increments the reference count
2304 // for its copy of the Symbol*, so use a TempNewSymbol here.
2305 }
2306 // Got utf8 string, set stream position forward
2307 cfs->skip_u1(length, CHECK);
2308 }
2311 // Inner classes can be static, private or protected (classic VM does this)
2312 #define RECOGNIZED_INNER_CLASS_MODIFIERS (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED | JVM_ACC_STATIC)
2314 // Return number of classes in the inner classes attribute table
2315 u2 ClassFileParser::parse_classfile_inner_classes_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2316 ClassFileStream* cfs = stream();
2317 cfs->guarantee_more(2, CHECK_0); // length
2318 u2 length = cfs->get_u2_fast();
2320 // 4-tuples of shorts [inner_class_info_index, outer_class_info_index, inner_name_index, inner_class_access_flags]
2321 typeArrayOop ic = oopFactory::new_permanent_shortArray(length*4, CHECK_0);
2322 typeArrayHandle inner_classes(THREAD, ic);
2323 int index = 0;
2324 int cp_size = cp->length();
2325 cfs->guarantee_more(8 * length, CHECK_0); // 4-tuples of u2
2326 for (int n = 0; n < length; n++) {
2327 // Inner class index
2328 u2 inner_class_info_index = cfs->get_u2_fast();
2329 check_property(
2330 inner_class_info_index == 0 ||
2331 (valid_cp_range(inner_class_info_index, cp_size) &&
2332 is_klass_reference(cp, inner_class_info_index)),
2333 "inner_class_info_index %u has bad constant type in class file %s",
2334 inner_class_info_index, CHECK_0);
2335 // Outer class index
2336 u2 outer_class_info_index = cfs->get_u2_fast();
2337 check_property(
2338 outer_class_info_index == 0 ||
2339 (valid_cp_range(outer_class_info_index, cp_size) &&
2340 is_klass_reference(cp, outer_class_info_index)),
2341 "outer_class_info_index %u has bad constant type in class file %s",
2342 outer_class_info_index, CHECK_0);
2343 // Inner class name
2344 u2 inner_name_index = cfs->get_u2_fast();
2345 check_property(
2346 inner_name_index == 0 || (valid_cp_range(inner_name_index, cp_size) &&
2347 cp->tag_at(inner_name_index).is_utf8()),
2348 "inner_name_index %u has bad constant type in class file %s",
2349 inner_name_index, CHECK_0);
2350 if (_need_verify) {
2351 guarantee_property(inner_class_info_index != outer_class_info_index,
2352 "Class is both outer and inner class in class file %s", CHECK_0);
2353 }
2354 // Access flags
2355 AccessFlags inner_access_flags;
2356 jint flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
2357 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2358 // Set abstract bit for old class files for backward compatibility
2359 flags |= JVM_ACC_ABSTRACT;
2360 }
2361 verify_legal_class_modifiers(flags, CHECK_0);
2362 inner_access_flags.set_flags(flags);
2364 inner_classes->short_at_put(index++, inner_class_info_index);
2365 inner_classes->short_at_put(index++, outer_class_info_index);
2366 inner_classes->short_at_put(index++, inner_name_index);
2367 inner_classes->short_at_put(index++, inner_access_flags.as_short());
2368 }
2370 // 4347400: make sure there's no duplicate entry in the classes array
2371 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
2372 for(int i = 0; i < inner_classes->length(); i += 4) {
2373 for(int j = i + 4; j < inner_classes->length(); j += 4) {
2374 guarantee_property((inner_classes->ushort_at(i) != inner_classes->ushort_at(j) ||
2375 inner_classes->ushort_at(i+1) != inner_classes->ushort_at(j+1) ||
2376 inner_classes->ushort_at(i+2) != inner_classes->ushort_at(j+2) ||
2377 inner_classes->ushort_at(i+3) != inner_classes->ushort_at(j+3)),
2378 "Duplicate entry in InnerClasses in class file %s",
2379 CHECK_0);
2380 }
2381 }
2382 }
2384 // Update instanceKlass with inner class info.
2385 k->set_inner_classes(inner_classes());
2386 return length;
2387 }
2389 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2390 k->set_is_synthetic();
2391 }
2393 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2394 ClassFileStream* cfs = stream();
2395 u2 signature_index = cfs->get_u2(CHECK);
2396 check_property(
2397 valid_cp_range(signature_index, cp->length()) &&
2398 cp->tag_at(signature_index).is_utf8(),
2399 "Invalid constant pool index %u in Signature attribute in class file %s",
2400 signature_index, CHECK);
2401 k->set_generic_signature(cp->symbol_at(signature_index));
2402 }
2404 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, instanceKlassHandle k,
2405 u4 attribute_byte_length, TRAPS) {
2406 ClassFileStream* cfs = stream();
2407 u1* current_start = cfs->current();
2409 cfs->guarantee_more(2, CHECK); // length
2410 int attribute_array_length = cfs->get_u2_fast();
2412 guarantee_property(_max_bootstrap_specifier_index < attribute_array_length,
2413 "Short length on BootstrapMethods in class file %s",
2414 CHECK);
2416 // The attribute contains a counted array of counted tuples of shorts,
2417 // represending bootstrap specifiers:
2418 // length*{bootstrap_method_index, argument_count*{argument_index}}
2419 int operand_count = (attribute_byte_length - sizeof(u2)) / sizeof(u2);
2420 // operand_count = number of shorts in attr, except for leading length
2422 // The attribute is copied into a short[] array.
2423 // The array begins with a series of short[2] pairs, one for each tuple.
2424 int index_size = (attribute_array_length * 2);
2426 typeArrayOop operands_oop = oopFactory::new_permanent_intArray(index_size + operand_count, CHECK);
2427 typeArrayHandle operands(THREAD, operands_oop);
2428 operands_oop = NULL; // tidy
2430 int operand_fill_index = index_size;
2431 int cp_size = cp->length();
2433 for (int n = 0; n < attribute_array_length; n++) {
2434 // Store a 32-bit offset into the header of the operand array.
2435 assert(constantPoolOopDesc::operand_offset_at(operands(), n) == 0, "");
2436 constantPoolOopDesc::operand_offset_at_put(operands(), n, operand_fill_index);
2438 // Read a bootstrap specifier.
2439 cfs->guarantee_more(sizeof(u2) * 2, CHECK); // bsm, argc
2440 u2 bootstrap_method_index = cfs->get_u2_fast();
2441 u2 argument_count = cfs->get_u2_fast();
2442 check_property(
2443 valid_cp_range(bootstrap_method_index, cp_size) &&
2444 cp->tag_at(bootstrap_method_index).is_method_handle(),
2445 "bootstrap_method_index %u has bad constant type in class file %s",
2446 bootstrap_method_index,
2447 CHECK);
2448 operands->short_at_put(operand_fill_index++, bootstrap_method_index);
2449 operands->short_at_put(operand_fill_index++, argument_count);
2451 cfs->guarantee_more(sizeof(u2) * argument_count, CHECK); // argv[argc]
2452 for (int j = 0; j < argument_count; j++) {
2453 u2 argument_index = cfs->get_u2_fast();
2454 check_property(
2455 valid_cp_range(argument_index, cp_size) &&
2456 cp->tag_at(argument_index).is_loadable_constant(),
2457 "argument_index %u has bad constant type in class file %s",
2458 argument_index,
2459 CHECK);
2460 operands->short_at_put(operand_fill_index++, argument_index);
2461 }
2462 }
2464 assert(operand_fill_index == operands()->length(), "exact fill");
2465 assert(constantPoolOopDesc::operand_array_length(operands()) == attribute_array_length, "correct decode");
2467 u1* current_end = cfs->current();
2468 guarantee_property(current_end == current_start + attribute_byte_length,
2469 "Bad length on BootstrapMethods in class file %s",
2470 CHECK);
2472 cp->set_operands(operands());
2473 }
2476 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) {
2477 ClassFileStream* cfs = stream();
2478 // Set inner classes attribute to default sentinel
2479 k->set_inner_classes(Universe::the_empty_short_array());
2480 cfs->guarantee_more(2, CHECK); // attributes_count
2481 u2 attributes_count = cfs->get_u2_fast();
2482 bool parsed_sourcefile_attribute = false;
2483 bool parsed_innerclasses_attribute = false;
2484 bool parsed_enclosingmethod_attribute = false;
2485 bool parsed_bootstrap_methods_attribute = false;
2486 u1* runtime_visible_annotations = NULL;
2487 int runtime_visible_annotations_length = 0;
2488 u1* runtime_invisible_annotations = NULL;
2489 int runtime_invisible_annotations_length = 0;
2490 // Iterate over attributes
2491 while (attributes_count--) {
2492 cfs->guarantee_more(6, CHECK); // attribute_name_index, attribute_length
2493 u2 attribute_name_index = cfs->get_u2_fast();
2494 u4 attribute_length = cfs->get_u4_fast();
2495 check_property(
2496 valid_cp_range(attribute_name_index, cp->length()) &&
2497 cp->tag_at(attribute_name_index).is_utf8(),
2498 "Attribute name has bad constant pool index %u in class file %s",
2499 attribute_name_index, CHECK);
2500 Symbol* tag = cp->symbol_at(attribute_name_index);
2501 if (tag == vmSymbols::tag_source_file()) {
2502 // Check for SourceFile tag
2503 if (_need_verify) {
2504 guarantee_property(attribute_length == 2, "Wrong SourceFile attribute length in class file %s", CHECK);
2505 }
2506 if (parsed_sourcefile_attribute) {
2507 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2508 } else {
2509 parsed_sourcefile_attribute = true;
2510 }
2511 parse_classfile_sourcefile_attribute(cp, k, CHECK);
2512 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2513 // Check for SourceDebugExtension tag
2514 parse_classfile_source_debug_extension_attribute(cp, k, (int)attribute_length, CHECK);
2515 } else if (tag == vmSymbols::tag_inner_classes()) {
2516 // Check for InnerClasses tag
2517 if (parsed_innerclasses_attribute) {
2518 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2519 } else {
2520 parsed_innerclasses_attribute = true;
2521 }
2522 u2 num_of_classes = parse_classfile_inner_classes_attribute(cp, k, CHECK);
2523 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
2524 guarantee_property(attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
2525 "Wrong InnerClasses attribute length in class file %s", CHECK);
2526 }
2527 } else if (tag == vmSymbols::tag_synthetic()) {
2528 // Check for Synthetic tag
2529 // Shouldn't we check that the synthetic flags wasn't already set? - not required in spec
2530 if (attribute_length != 0) {
2531 classfile_parse_error(
2532 "Invalid Synthetic classfile attribute length %u in class file %s",
2533 attribute_length, CHECK);
2534 }
2535 parse_classfile_synthetic_attribute(cp, k, CHECK);
2536 } else if (tag == vmSymbols::tag_deprecated()) {
2537 // Check for Deprecatd tag - 4276120
2538 if (attribute_length != 0) {
2539 classfile_parse_error(
2540 "Invalid Deprecated classfile attribute length %u in class file %s",
2541 attribute_length, CHECK);
2542 }
2543 } else if (_major_version >= JAVA_1_5_VERSION) {
2544 if (tag == vmSymbols::tag_signature()) {
2545 if (attribute_length != 2) {
2546 classfile_parse_error(
2547 "Wrong Signature attribute length %u in class file %s",
2548 attribute_length, CHECK);
2549 }
2550 parse_classfile_signature_attribute(cp, k, CHECK);
2551 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2552 runtime_visible_annotations_length = attribute_length;
2553 runtime_visible_annotations = cfs->get_u1_buffer();
2554 assert(runtime_visible_annotations != NULL, "null visible annotations");
2555 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2556 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2557 runtime_invisible_annotations_length = attribute_length;
2558 runtime_invisible_annotations = cfs->get_u1_buffer();
2559 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2560 cfs->skip_u1(runtime_invisible_annotations_length, CHECK);
2561 } else if (tag == vmSymbols::tag_enclosing_method()) {
2562 if (parsed_enclosingmethod_attribute) {
2563 classfile_parse_error("Multiple EnclosingMethod attributes in class file %s", CHECK);
2564 } else {
2565 parsed_enclosingmethod_attribute = true;
2566 }
2567 cfs->guarantee_more(4, CHECK); // class_index, method_index
2568 u2 class_index = cfs->get_u2_fast();
2569 u2 method_index = cfs->get_u2_fast();
2570 if (class_index == 0) {
2571 classfile_parse_error("Invalid class index in EnclosingMethod attribute in class file %s", CHECK);
2572 }
2573 // Validate the constant pool indices and types
2574 if (!cp->is_within_bounds(class_index) ||
2575 !is_klass_reference(cp, class_index)) {
2576 classfile_parse_error("Invalid or out-of-bounds class index in EnclosingMethod attribute in class file %s", CHECK);
2577 }
2578 if (method_index != 0 &&
2579 (!cp->is_within_bounds(method_index) ||
2580 !cp->tag_at(method_index).is_name_and_type())) {
2581 classfile_parse_error("Invalid or out-of-bounds method index in EnclosingMethod attribute in class file %s", CHECK);
2582 }
2583 k->set_enclosing_method_indices(class_index, method_index);
2584 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
2585 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2586 if (parsed_bootstrap_methods_attribute)
2587 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2588 parsed_bootstrap_methods_attribute = true;
2589 parse_classfile_bootstrap_methods_attribute(cp, k, attribute_length, CHECK);
2590 } else {
2591 // Unknown attribute
2592 cfs->skip_u1(attribute_length, CHECK);
2593 }
2594 } else {
2595 // Unknown attribute
2596 cfs->skip_u1(attribute_length, CHECK);
2597 }
2598 }
2599 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2600 runtime_visible_annotations_length,
2601 runtime_invisible_annotations,
2602 runtime_invisible_annotations_length,
2603 CHECK);
2604 k->set_class_annotations(annotations());
2606 if (_max_bootstrap_specifier_index >= 0) {
2607 guarantee_property(parsed_bootstrap_methods_attribute,
2608 "Missing BootstrapMethods attribute in class file %s", CHECK);
2609 }
2610 }
2613 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
2614 int runtime_visible_annotations_length,
2615 u1* runtime_invisible_annotations,
2616 int runtime_invisible_annotations_length, TRAPS) {
2617 typeArrayHandle annotations;
2618 if (runtime_visible_annotations != NULL ||
2619 runtime_invisible_annotations != NULL) {
2620 typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2621 runtime_invisible_annotations_length, CHECK_(annotations));
2622 annotations = typeArrayHandle(THREAD, anno);
2623 if (runtime_visible_annotations != NULL) {
2624 memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2625 }
2626 if (runtime_invisible_annotations != NULL) {
2627 memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2628 }
2629 }
2630 return annotations;
2631 }
2634 instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name,
2635 Handle class_loader,
2636 Handle protection_domain,
2637 KlassHandle host_klass,
2638 GrowableArray<Handle>* cp_patches,
2639 TempNewSymbol& parsed_name,
2640 bool verify,
2641 TRAPS) {
2642 // So that JVMTI can cache class file in the state before retransformable agents
2643 // have modified it
2644 unsigned char *cached_class_file_bytes = NULL;
2645 jint cached_class_file_length;
2647 ClassFileStream* cfs = stream();
2648 // Timing
2649 assert(THREAD->is_Java_thread(), "must be a JavaThread");
2650 JavaThread* jt = (JavaThread*) THREAD;
2652 PerfClassTraceTime ctimer(ClassLoader::perf_class_parse_time(),
2653 ClassLoader::perf_class_parse_selftime(),
2654 NULL,
2655 jt->get_thread_stat()->perf_recursion_counts_addr(),
2656 jt->get_thread_stat()->perf_timers_addr(),
2657 PerfClassTraceTime::PARSE_CLASS);
2659 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false;
2660 _max_bootstrap_specifier_index = -1;
2662 if (JvmtiExport::should_post_class_file_load_hook()) {
2663 unsigned char* ptr = cfs->buffer();
2664 unsigned char* end_ptr = cfs->buffer() + cfs->length();
2666 JvmtiExport::post_class_file_load_hook(name, class_loader, protection_domain,
2667 &ptr, &end_ptr,
2668 &cached_class_file_bytes,
2669 &cached_class_file_length);
2671 if (ptr != cfs->buffer()) {
2672 // JVMTI agent has modified class file data.
2673 // Set new class file stream using JVMTI agent modified
2674 // class file data.
2675 cfs = new ClassFileStream(ptr, end_ptr - ptr, cfs->source());
2676 set_stream(cfs);
2677 }
2678 }
2680 _host_klass = host_klass;
2681 _cp_patches = cp_patches;
2683 instanceKlassHandle nullHandle;
2685 // Figure out whether we can skip format checking (matching classic VM behavior)
2686 _need_verify = Verifier::should_verify_for(class_loader(), verify);
2688 // Set the verify flag in stream
2689 cfs->set_verify(_need_verify);
2691 // Save the class file name for easier error message printing.
2692 _class_name = (name != NULL) ? name : vmSymbols::unknown_class_name();
2694 cfs->guarantee_more(8, CHECK_(nullHandle)); // magic, major, minor
2695 // Magic value
2696 u4 magic = cfs->get_u4_fast();
2697 guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
2698 "Incompatible magic value %u in class file %s",
2699 magic, CHECK_(nullHandle));
2701 // Version numbers
2702 u2 minor_version = cfs->get_u2_fast();
2703 u2 major_version = cfs->get_u2_fast();
2705 // Check version numbers - we check this even with verifier off
2706 if (!is_supported_version(major_version, minor_version)) {
2707 if (name == NULL) {
2708 Exceptions::fthrow(
2709 THREAD_AND_LOCATION,
2710 vmSymbols::java_lang_UnsupportedClassVersionError(),
2711 "Unsupported major.minor version %u.%u",
2712 major_version,
2713 minor_version);
2714 } else {
2715 ResourceMark rm(THREAD);
2716 Exceptions::fthrow(
2717 THREAD_AND_LOCATION,
2718 vmSymbols::java_lang_UnsupportedClassVersionError(),
2719 "%s : Unsupported major.minor version %u.%u",
2720 name->as_C_string(),
2721 major_version,
2722 minor_version);
2723 }
2724 return nullHandle;
2725 }
2727 _major_version = major_version;
2728 _minor_version = minor_version;
2731 // Check if verification needs to be relaxed for this class file
2732 // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
2733 _relax_verify = Verifier::relax_verify_for(class_loader());
2735 // Constant pool
2736 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
2737 ConstantPoolCleaner error_handler(cp); // set constant pool to be cleaned up.
2739 int cp_size = cp->length();
2741 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
2743 // Access flags
2744 AccessFlags access_flags;
2745 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
2747 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
2748 // Set abstract bit for old class files for backward compatibility
2749 flags |= JVM_ACC_ABSTRACT;
2750 }
2751 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
2752 access_flags.set_flags(flags);
2754 // This class and superclass
2755 instanceKlassHandle super_klass;
2756 u2 this_class_index = cfs->get_u2_fast();
2757 check_property(
2758 valid_cp_range(this_class_index, cp_size) &&
2759 cp->tag_at(this_class_index).is_unresolved_klass(),
2760 "Invalid this class index %u in constant pool in class file %s",
2761 this_class_index, CHECK_(nullHandle));
2763 Symbol* class_name = cp->unresolved_klass_at(this_class_index);
2764 assert(class_name != NULL, "class_name can't be null");
2766 // It's important to set parsed_name *before* resolving the super class.
2767 // (it's used for cleanup by the caller if parsing fails)
2768 parsed_name = class_name;
2769 // parsed_name is returned and can be used if there's an error, so add to
2770 // its reference count. Caller will decrement the refcount.
2771 parsed_name->increment_refcount();
2773 // Update _class_name which could be null previously to be class_name
2774 _class_name = class_name;
2776 // Don't need to check whether this class name is legal or not.
2777 // It has been checked when constant pool is parsed.
2778 // However, make sure it is not an array type.
2779 if (_need_verify) {
2780 guarantee_property(class_name->byte_at(0) != JVM_SIGNATURE_ARRAY,
2781 "Bad class name in class file %s",
2782 CHECK_(nullHandle));
2783 }
2785 klassOop preserve_this_klass; // for storing result across HandleMark
2787 // release all handles when parsing is done
2788 { HandleMark hm(THREAD);
2790 // Checks if name in class file matches requested name
2791 if (name != NULL && class_name != name) {
2792 ResourceMark rm(THREAD);
2793 Exceptions::fthrow(
2794 THREAD_AND_LOCATION,
2795 vmSymbols::java_lang_NoClassDefFoundError(),
2796 "%s (wrong name: %s)",
2797 name->as_C_string(),
2798 class_name->as_C_string()
2799 );
2800 return nullHandle;
2801 }
2803 if (TraceClassLoadingPreorder) {
2804 tty->print("[Loading %s", name->as_klass_external_name());
2805 if (cfs->source() != NULL) tty->print(" from %s", cfs->source());
2806 tty->print_cr("]");
2807 }
2809 u2 super_class_index = cfs->get_u2_fast();
2810 if (super_class_index == 0) {
2811 check_property(class_name == vmSymbols::java_lang_Object(),
2812 "Invalid superclass index %u in class file %s",
2813 super_class_index,
2814 CHECK_(nullHandle));
2815 } else {
2816 check_property(valid_cp_range(super_class_index, cp_size) &&
2817 is_klass_reference(cp, super_class_index),
2818 "Invalid superclass index %u in class file %s",
2819 super_class_index,
2820 CHECK_(nullHandle));
2821 // The class name should be legal because it is checked when parsing constant pool.
2822 // However, make sure it is not an array type.
2823 bool is_array = false;
2824 if (cp->tag_at(super_class_index).is_klass()) {
2825 super_klass = instanceKlassHandle(THREAD, cp->resolved_klass_at(super_class_index));
2826 if (_need_verify)
2827 is_array = super_klass->oop_is_array();
2828 } else if (_need_verify) {
2829 is_array = (cp->unresolved_klass_at(super_class_index)->byte_at(0) == JVM_SIGNATURE_ARRAY);
2830 }
2831 if (_need_verify) {
2832 guarantee_property(!is_array,
2833 "Bad superclass name in class file %s", CHECK_(nullHandle));
2834 }
2835 }
2837 // Interfaces
2838 u2 itfs_len = cfs->get_u2_fast();
2839 objArrayHandle local_interfaces;
2840 if (itfs_len == 0) {
2841 local_interfaces = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
2842 } else {
2843 local_interfaces = parse_interfaces(cp, itfs_len, class_loader, protection_domain, _class_name, CHECK_(nullHandle));
2844 }
2846 int java_fields_count = 0;
2847 // Fields (offsets are filled in later)
2848 FieldAllocationCount fac;
2849 objArrayHandle fields_annotations;
2850 typeArrayHandle fields = parse_fields(class_name, cp, access_flags.is_interface(), &fac, &fields_annotations,
2851 &java_fields_count,
2852 CHECK_(nullHandle));
2853 // Methods
2854 bool has_final_method = false;
2855 AccessFlags promoted_flags;
2856 promoted_flags.set_flags(0);
2857 // These need to be oop pointers because they are allocated lazily
2858 // inside parse_methods inside a nested HandleMark
2859 objArrayOop methods_annotations_oop = NULL;
2860 objArrayOop methods_parameter_annotations_oop = NULL;
2861 objArrayOop methods_default_annotations_oop = NULL;
2862 objArrayHandle methods = parse_methods(cp, access_flags.is_interface(),
2863 &promoted_flags,
2864 &has_final_method,
2865 &methods_annotations_oop,
2866 &methods_parameter_annotations_oop,
2867 &methods_default_annotations_oop,
2868 CHECK_(nullHandle));
2870 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2871 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2872 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
2874 // We check super class after class file is parsed and format is checked
2875 if (super_class_index > 0 && super_klass.is_null()) {
2876 Symbol* sk = cp->klass_name_at(super_class_index);
2877 if (access_flags.is_interface()) {
2878 // Before attempting to resolve the superclass, check for class format
2879 // errors not checked yet.
2880 guarantee_property(sk == vmSymbols::java_lang_Object(),
2881 "Interfaces must have java.lang.Object as superclass in class file %s",
2882 CHECK_(nullHandle));
2883 }
2884 klassOop k = SystemDictionary::resolve_super_or_fail(class_name,
2885 sk,
2886 class_loader,
2887 protection_domain,
2888 true,
2889 CHECK_(nullHandle));
2891 KlassHandle kh (THREAD, k);
2892 super_klass = instanceKlassHandle(THREAD, kh());
2893 if (LinkWellKnownClasses) // my super class is well known to me
2894 cp->klass_at_put(super_class_index, super_klass()); // eagerly resolve
2895 }
2896 if (super_klass.not_null()) {
2897 if (super_klass->is_interface()) {
2898 ResourceMark rm(THREAD);
2899 Exceptions::fthrow(
2900 THREAD_AND_LOCATION,
2901 vmSymbols::java_lang_IncompatibleClassChangeError(),
2902 "class %s has interface %s as super class",
2903 class_name->as_klass_external_name(),
2904 super_klass->external_name()
2905 );
2906 return nullHandle;
2907 }
2908 // Make sure super class is not final
2909 if (super_klass->is_final()) {
2910 THROW_MSG_(vmSymbols::java_lang_VerifyError(), "Cannot inherit from final class", nullHandle);
2911 }
2912 }
2914 // Compute the transitive list of all unique interfaces implemented by this class
2915 objArrayHandle transitive_interfaces = compute_transitive_interfaces(super_klass, local_interfaces, CHECK_(nullHandle));
2917 // sort methods
2918 typeArrayHandle method_ordering = sort_methods(methods,
2919 methods_annotations,
2920 methods_parameter_annotations,
2921 methods_default_annotations,
2922 CHECK_(nullHandle));
2924 // promote flags from parse_methods() to the klass' flags
2925 access_flags.add_promoted_flags(promoted_flags.as_int());
2927 // Size of Java vtable (in words)
2928 int vtable_size = 0;
2929 int itable_size = 0;
2930 int num_miranda_methods = 0;
2932 klassVtable::compute_vtable_size_and_num_mirandas(vtable_size,
2933 num_miranda_methods,
2934 super_klass(),
2935 methods(),
2936 access_flags,
2937 class_loader,
2938 class_name,
2939 local_interfaces(),
2940 CHECK_(nullHandle));
2942 // Size of Java itable (in words)
2943 itable_size = access_flags.is_interface() ? 0 : klassItable::compute_itable_size(transitive_interfaces);
2945 // Field size and offset computation
2946 int nonstatic_field_size = super_klass() == NULL ? 0 : super_klass->nonstatic_field_size();
2947 #ifndef PRODUCT
2948 int orig_nonstatic_field_size = 0;
2949 #endif
2950 int static_field_size = 0;
2951 int next_static_oop_offset;
2952 int next_static_double_offset;
2953 int next_static_word_offset;
2954 int next_static_short_offset;
2955 int next_static_byte_offset;
2956 int next_static_type_offset;
2957 int next_nonstatic_oop_offset;
2958 int next_nonstatic_double_offset;
2959 int next_nonstatic_word_offset;
2960 int next_nonstatic_short_offset;
2961 int next_nonstatic_byte_offset;
2962 int next_nonstatic_type_offset;
2963 int first_nonstatic_oop_offset;
2964 int first_nonstatic_field_offset;
2965 int next_nonstatic_field_offset;
2967 // Calculate the starting byte offsets
2968 next_static_oop_offset = instanceMirrorKlass::offset_of_static_fields();
2969 next_static_double_offset = next_static_oop_offset +
2970 (fac.count[STATIC_OOP] * heapOopSize);
2971 if ( fac.count[STATIC_DOUBLE] &&
2972 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
2973 Universe::field_type_should_be_aligned(T_LONG)) ) {
2974 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
2975 }
2977 next_static_word_offset = next_static_double_offset +
2978 (fac.count[STATIC_DOUBLE] * BytesPerLong);
2979 next_static_short_offset = next_static_word_offset +
2980 (fac.count[STATIC_WORD] * BytesPerInt);
2981 next_static_byte_offset = next_static_short_offset +
2982 (fac.count[STATIC_SHORT] * BytesPerShort);
2983 next_static_type_offset = align_size_up((next_static_byte_offset +
2984 fac.count[STATIC_BYTE] ), wordSize );
2985 static_field_size = (next_static_type_offset -
2986 next_static_oop_offset) / wordSize;
2988 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
2989 nonstatic_field_size * heapOopSize;
2990 next_nonstatic_field_offset = first_nonstatic_field_offset;
2992 unsigned int nonstatic_double_count = fac.count[NONSTATIC_DOUBLE];
2993 unsigned int nonstatic_word_count = fac.count[NONSTATIC_WORD];
2994 unsigned int nonstatic_short_count = fac.count[NONSTATIC_SHORT];
2995 unsigned int nonstatic_byte_count = fac.count[NONSTATIC_BYTE];
2996 unsigned int nonstatic_oop_count = fac.count[NONSTATIC_OOP];
2998 bool super_has_nonstatic_fields =
2999 (super_klass() != NULL && super_klass->has_nonstatic_fields());
3000 bool has_nonstatic_fields = super_has_nonstatic_fields ||
3001 ((nonstatic_double_count + nonstatic_word_count +
3002 nonstatic_short_count + nonstatic_byte_count +
3003 nonstatic_oop_count) != 0);
3006 // Prepare list of oops for oop map generation.
3007 int* nonstatic_oop_offsets;
3008 unsigned int* nonstatic_oop_counts;
3009 unsigned int nonstatic_oop_map_count = 0;
3011 nonstatic_oop_offsets = NEW_RESOURCE_ARRAY_IN_THREAD(
3012 THREAD, int, nonstatic_oop_count + 1);
3013 nonstatic_oop_counts = NEW_RESOURCE_ARRAY_IN_THREAD(
3014 THREAD, unsigned int, nonstatic_oop_count + 1);
3016 first_nonstatic_oop_offset = 0; // will be set for first oop field
3018 #ifndef PRODUCT
3019 if( PrintCompactFieldsSavings ) {
3020 next_nonstatic_double_offset = next_nonstatic_field_offset +
3021 (nonstatic_oop_count * heapOopSize);
3022 if ( nonstatic_double_count > 0 ) {
3023 next_nonstatic_double_offset = align_size_up(next_nonstatic_double_offset, BytesPerLong);
3024 }
3025 next_nonstatic_word_offset = next_nonstatic_double_offset +
3026 (nonstatic_double_count * BytesPerLong);
3027 next_nonstatic_short_offset = next_nonstatic_word_offset +
3028 (nonstatic_word_count * BytesPerInt);
3029 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3030 (nonstatic_short_count * BytesPerShort);
3031 next_nonstatic_type_offset = align_size_up((next_nonstatic_byte_offset +
3032 nonstatic_byte_count ), heapOopSize );
3033 orig_nonstatic_field_size = nonstatic_field_size +
3034 ((next_nonstatic_type_offset - first_nonstatic_field_offset)/heapOopSize);
3035 }
3036 #endif
3037 bool compact_fields = CompactFields;
3038 int allocation_style = FieldsAllocationStyle;
3039 if( allocation_style < 0 || allocation_style > 2 ) { // Out of range?
3040 assert(false, "0 <= FieldsAllocationStyle <= 2");
3041 allocation_style = 1; // Optimistic
3042 }
3044 // The next classes have predefined hard-coded fields offsets
3045 // (see in JavaClasses::compute_hard_coded_offsets()).
3046 // Use default fields allocation order for them.
3047 if( (allocation_style != 0 || compact_fields ) && class_loader.is_null() &&
3048 (class_name == vmSymbols::java_lang_AssertionStatusDirectives() ||
3049 class_name == vmSymbols::java_lang_Class() ||
3050 class_name == vmSymbols::java_lang_ClassLoader() ||
3051 class_name == vmSymbols::java_lang_ref_Reference() ||
3052 class_name == vmSymbols::java_lang_ref_SoftReference() ||
3053 class_name == vmSymbols::java_lang_StackTraceElement() ||
3054 class_name == vmSymbols::java_lang_String() ||
3055 class_name == vmSymbols::java_lang_Throwable() ||
3056 class_name == vmSymbols::java_lang_Boolean() ||
3057 class_name == vmSymbols::java_lang_Character() ||
3058 class_name == vmSymbols::java_lang_Float() ||
3059 class_name == vmSymbols::java_lang_Double() ||
3060 class_name == vmSymbols::java_lang_Byte() ||
3061 class_name == vmSymbols::java_lang_Short() ||
3062 class_name == vmSymbols::java_lang_Integer() ||
3063 class_name == vmSymbols::java_lang_Long())) {
3064 allocation_style = 0; // Allocate oops first
3065 compact_fields = false; // Don't compact fields
3066 }
3068 if( allocation_style == 0 ) {
3069 // Fields order: oops, longs/doubles, ints, shorts/chars, bytes
3070 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3071 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3072 (nonstatic_oop_count * heapOopSize);
3073 } else if( allocation_style == 1 ) {
3074 // Fields order: longs/doubles, ints, shorts/chars, bytes, oops
3075 next_nonstatic_double_offset = next_nonstatic_field_offset;
3076 } else if( allocation_style == 2 ) {
3077 // Fields allocation: oops fields in super and sub classes are together.
3078 if( nonstatic_field_size > 0 && super_klass() != NULL &&
3079 super_klass->nonstatic_oop_map_size() > 0 ) {
3080 int map_count = super_klass->nonstatic_oop_map_count();
3081 OopMapBlock* first_map = super_klass->start_of_nonstatic_oop_maps();
3082 OopMapBlock* last_map = first_map + map_count - 1;
3083 int next_offset = last_map->offset() + (last_map->count() * heapOopSize);
3084 if (next_offset == next_nonstatic_field_offset) {
3085 allocation_style = 0; // allocate oops first
3086 next_nonstatic_oop_offset = next_nonstatic_field_offset;
3087 next_nonstatic_double_offset = next_nonstatic_oop_offset +
3088 (nonstatic_oop_count * heapOopSize);
3089 }
3090 }
3091 if( allocation_style == 2 ) {
3092 allocation_style = 1; // allocate oops last
3093 next_nonstatic_double_offset = next_nonstatic_field_offset;
3094 }
3095 } else {
3096 ShouldNotReachHere();
3097 }
3099 int nonstatic_oop_space_count = 0;
3100 int nonstatic_word_space_count = 0;
3101 int nonstatic_short_space_count = 0;
3102 int nonstatic_byte_space_count = 0;
3103 int nonstatic_oop_space_offset;
3104 int nonstatic_word_space_offset;
3105 int nonstatic_short_space_offset;
3106 int nonstatic_byte_space_offset;
3108 if( nonstatic_double_count > 0 ) {
3109 int offset = next_nonstatic_double_offset;
3110 next_nonstatic_double_offset = align_size_up(offset, BytesPerLong);
3111 if( compact_fields && offset != next_nonstatic_double_offset ) {
3112 // Allocate available fields into the gap before double field.
3113 int length = next_nonstatic_double_offset - offset;
3114 assert(length == BytesPerInt, "");
3115 nonstatic_word_space_offset = offset;
3116 if( nonstatic_word_count > 0 ) {
3117 nonstatic_word_count -= 1;
3118 nonstatic_word_space_count = 1; // Only one will fit
3119 length -= BytesPerInt;
3120 offset += BytesPerInt;
3121 }
3122 nonstatic_short_space_offset = offset;
3123 while( length >= BytesPerShort && nonstatic_short_count > 0 ) {
3124 nonstatic_short_count -= 1;
3125 nonstatic_short_space_count += 1;
3126 length -= BytesPerShort;
3127 offset += BytesPerShort;
3128 }
3129 nonstatic_byte_space_offset = offset;
3130 while( length > 0 && nonstatic_byte_count > 0 ) {
3131 nonstatic_byte_count -= 1;
3132 nonstatic_byte_space_count += 1;
3133 length -= 1;
3134 }
3135 // Allocate oop field in the gap if there are no other fields for that.
3136 nonstatic_oop_space_offset = offset;
3137 if( length >= heapOopSize && nonstatic_oop_count > 0 &&
3138 allocation_style != 0 ) { // when oop fields not first
3139 nonstatic_oop_count -= 1;
3140 nonstatic_oop_space_count = 1; // Only one will fit
3141 length -= heapOopSize;
3142 offset += heapOopSize;
3143 }
3144 }
3145 }
3147 next_nonstatic_word_offset = next_nonstatic_double_offset +
3148 (nonstatic_double_count * BytesPerLong);
3149 next_nonstatic_short_offset = next_nonstatic_word_offset +
3150 (nonstatic_word_count * BytesPerInt);
3151 next_nonstatic_byte_offset = next_nonstatic_short_offset +
3152 (nonstatic_short_count * BytesPerShort);
3154 int notaligned_offset;
3155 if( allocation_style == 0 ) {
3156 notaligned_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3157 } else { // allocation_style == 1
3158 next_nonstatic_oop_offset = next_nonstatic_byte_offset + nonstatic_byte_count;
3159 if( nonstatic_oop_count > 0 ) {
3160 next_nonstatic_oop_offset = align_size_up(next_nonstatic_oop_offset, heapOopSize);
3161 }
3162 notaligned_offset = next_nonstatic_oop_offset + (nonstatic_oop_count * heapOopSize);
3163 }
3164 next_nonstatic_type_offset = align_size_up(notaligned_offset, heapOopSize );
3165 nonstatic_field_size = nonstatic_field_size + ((next_nonstatic_type_offset
3166 - first_nonstatic_field_offset)/heapOopSize);
3168 // Iterate over fields again and compute correct offsets.
3169 // The field allocation type was temporarily stored in the offset slot.
3170 // oop fields are located before non-oop fields (static and non-static).
3171 for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3172 int real_offset;
3173 FieldAllocationType atype = (FieldAllocationType) fs.offset();
3174 switch (atype) {
3175 case STATIC_OOP:
3176 real_offset = next_static_oop_offset;
3177 next_static_oop_offset += heapOopSize;
3178 break;
3179 case STATIC_BYTE:
3180 real_offset = next_static_byte_offset;
3181 next_static_byte_offset += 1;
3182 break;
3183 case STATIC_SHORT:
3184 real_offset = next_static_short_offset;
3185 next_static_short_offset += BytesPerShort;
3186 break;
3187 case STATIC_WORD:
3188 real_offset = next_static_word_offset;
3189 next_static_word_offset += BytesPerInt;
3190 break;
3191 case STATIC_DOUBLE:
3192 real_offset = next_static_double_offset;
3193 next_static_double_offset += BytesPerLong;
3194 break;
3195 case NONSTATIC_OOP:
3196 if( nonstatic_oop_space_count > 0 ) {
3197 real_offset = nonstatic_oop_space_offset;
3198 nonstatic_oop_space_offset += heapOopSize;
3199 nonstatic_oop_space_count -= 1;
3200 } else {
3201 real_offset = next_nonstatic_oop_offset;
3202 next_nonstatic_oop_offset += heapOopSize;
3203 }
3204 // Update oop maps
3205 if( nonstatic_oop_map_count > 0 &&
3206 nonstatic_oop_offsets[nonstatic_oop_map_count - 1] ==
3207 real_offset -
3208 int(nonstatic_oop_counts[nonstatic_oop_map_count - 1]) *
3209 heapOopSize ) {
3210 // Extend current oop map
3211 nonstatic_oop_counts[nonstatic_oop_map_count - 1] += 1;
3212 } else {
3213 // Create new oop map
3214 nonstatic_oop_offsets[nonstatic_oop_map_count] = real_offset;
3215 nonstatic_oop_counts [nonstatic_oop_map_count] = 1;
3216 nonstatic_oop_map_count += 1;
3217 if( first_nonstatic_oop_offset == 0 ) { // Undefined
3218 first_nonstatic_oop_offset = real_offset;
3219 }
3220 }
3221 break;
3222 case NONSTATIC_BYTE:
3223 if( nonstatic_byte_space_count > 0 ) {
3224 real_offset = nonstatic_byte_space_offset;
3225 nonstatic_byte_space_offset += 1;
3226 nonstatic_byte_space_count -= 1;
3227 } else {
3228 real_offset = next_nonstatic_byte_offset;
3229 next_nonstatic_byte_offset += 1;
3230 }
3231 break;
3232 case NONSTATIC_SHORT:
3233 if( nonstatic_short_space_count > 0 ) {
3234 real_offset = nonstatic_short_space_offset;
3235 nonstatic_short_space_offset += BytesPerShort;
3236 nonstatic_short_space_count -= 1;
3237 } else {
3238 real_offset = next_nonstatic_short_offset;
3239 next_nonstatic_short_offset += BytesPerShort;
3240 }
3241 break;
3242 case NONSTATIC_WORD:
3243 if( nonstatic_word_space_count > 0 ) {
3244 real_offset = nonstatic_word_space_offset;
3245 nonstatic_word_space_offset += BytesPerInt;
3246 nonstatic_word_space_count -= 1;
3247 } else {
3248 real_offset = next_nonstatic_word_offset;
3249 next_nonstatic_word_offset += BytesPerInt;
3250 }
3251 break;
3252 case NONSTATIC_DOUBLE:
3253 real_offset = next_nonstatic_double_offset;
3254 next_nonstatic_double_offset += BytesPerLong;
3255 break;
3256 default:
3257 ShouldNotReachHere();
3258 }
3259 fs.set_offset(real_offset);
3260 }
3262 // Size of instances
3263 int instance_size;
3265 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3266 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3268 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3270 // Number of non-static oop map blocks allocated at end of klass.
3271 const unsigned int total_oop_map_count =
3272 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3273 first_nonstatic_oop_offset);
3275 // Compute reference type
3276 ReferenceType rt;
3277 if (super_klass() == NULL) {
3278 rt = REF_NONE;
3279 } else {
3280 rt = super_klass->reference_type();
3281 }
3283 // We can now create the basic klassOop for this klass
3284 klassOop ik = oopFactory::new_instanceKlass(name, vtable_size, itable_size,
3285 static_field_size,
3286 total_oop_map_count,
3287 rt, CHECK_(nullHandle));
3288 instanceKlassHandle this_klass (THREAD, ik);
3290 assert(this_klass->static_field_size() == static_field_size, "sanity");
3291 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3292 "sanity");
3294 // Fill in information already parsed
3295 this_klass->set_access_flags(access_flags);
3296 this_klass->set_should_verify_class(verify);
3297 jint lh = Klass::instance_layout_helper(instance_size, false);
3298 this_klass->set_layout_helper(lh);
3299 assert(this_klass->oop_is_instance(), "layout is correct");
3300 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3301 // Not yet: supers are done below to support the new subtype-checking fields
3302 //this_klass->set_super(super_klass());
3303 this_klass->set_class_loader(class_loader());
3304 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3305 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3306 this_klass->set_static_oop_field_count(fac.count[STATIC_OOP]);
3307 cp->set_pool_holder(this_klass());
3308 error_handler.set_in_error(false); // turn off error handler for cp
3309 this_klass->set_constants(cp());
3310 this_klass->set_local_interfaces(local_interfaces());
3311 this_klass->set_fields(fields(), java_fields_count);
3312 this_klass->set_methods(methods());
3313 if (has_final_method) {
3314 this_klass->set_has_final_method();
3315 }
3316 this_klass->set_method_ordering(method_ordering());
3317 // The instanceKlass::_methods_jmethod_ids cache and the
3318 // instanceKlass::_methods_cached_itable_indices cache are
3319 // both managed on the assumption that the initial cache
3320 // size is equal to the number of methods in the class. If
3321 // that changes, then instanceKlass::idnum_can_increment()
3322 // has to be changed accordingly.
3323 this_klass->set_initial_method_idnum(methods->length());
3324 this_klass->set_name(cp->klass_name_at(this_class_index));
3325 if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself
3326 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3327 this_klass->set_protection_domain(protection_domain());
3328 this_klass->set_fields_annotations(fields_annotations());
3329 this_klass->set_methods_annotations(methods_annotations());
3330 this_klass->set_methods_parameter_annotations(methods_parameter_annotations());
3331 this_klass->set_methods_default_annotations(methods_default_annotations());
3333 this_klass->set_minor_version(minor_version);
3334 this_klass->set_major_version(major_version);
3336 // Set up methodOop::intrinsic_id as soon as we know the names of methods.
3337 // (We used to do this lazily, but now we query it in Rewriter,
3338 // which is eagerly done for every method, so we might as well do it now,
3339 // when everything is fresh in memory.)
3340 if (methodOopDesc::klass_id_for_intrinsics(this_klass->as_klassOop()) != vmSymbols::NO_SID) {
3341 for (int j = 0; j < methods->length(); j++) {
3342 ((methodOop)methods->obj_at(j))->init_intrinsic_id();
3343 }
3344 }
3346 if (cached_class_file_bytes != NULL) {
3347 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3348 this_klass->set_cached_class_file(cached_class_file_bytes,
3349 cached_class_file_length);
3350 }
3352 // Miranda methods
3353 if ((num_miranda_methods > 0) ||
3354 // if this class introduced new miranda methods or
3355 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3356 // super class exists and this class inherited miranda methods
3357 ) {
3358 this_klass->set_has_miranda_methods(); // then set a flag
3359 }
3361 // Additional attributes
3362 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle));
3364 // Make sure this is the end of class file stream
3365 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3367 // VerifyOops believes that once this has been set, the object is completely loaded.
3368 // Compute transitive closure of interfaces this class implements
3369 this_klass->set_transitive_interfaces(transitive_interfaces());
3371 // Fill in information needed to compute superclasses.
3372 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
3374 // Initialize itable offset tables
3375 klassItable::setup_itable_offset_table(this_klass);
3377 // Do final class setup
3378 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3380 set_precomputed_flags(this_klass);
3382 // reinitialize modifiers, using the InnerClasses attribute
3383 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3384 this_klass->set_modifier_flags(computed_modifiers);
3386 // check if this class can access its super class
3387 check_super_class_access(this_klass, CHECK_(nullHandle));
3389 // check if this class can access its superinterfaces
3390 check_super_interface_access(this_klass, CHECK_(nullHandle));
3392 // check if this class overrides any final method
3393 check_final_method_override(this_klass, CHECK_(nullHandle));
3395 // check that if this class is an interface then it doesn't have static methods
3396 if (this_klass->is_interface()) {
3397 check_illegal_static_method(this_klass, CHECK_(nullHandle));
3398 }
3400 // Allocate mirror and initialize static fields
3401 java_lang_Class::create_mirror(this_klass, CHECK_(nullHandle));
3403 ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()),
3404 false /* not shared class */);
3406 if (TraceClassLoading) {
3407 // print in a single call to reduce interleaving of output
3408 if (cfs->source() != NULL) {
3409 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3410 cfs->source());
3411 } else if (class_loader.is_null()) {
3412 if (THREAD->is_Java_thread()) {
3413 klassOop caller = ((JavaThread*)THREAD)->security_get_caller_class(1);
3414 tty->print("[Loaded %s by instance of %s]\n",
3415 this_klass->external_name(),
3416 instanceKlass::cast(caller)->external_name());
3417 } else {
3418 tty->print("[Loaded %s]\n", this_klass->external_name());
3419 }
3420 } else {
3421 ResourceMark rm;
3422 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3423 instanceKlass::cast(class_loader->klass())->external_name());
3424 }
3425 }
3427 if (TraceClassResolution) {
3428 // print out the superclass.
3429 const char * from = Klass::cast(this_klass())->external_name();
3430 if (this_klass->java_super() != NULL) {
3431 tty->print("RESOLVE %s %s (super)\n", from, instanceKlass::cast(this_klass->java_super())->external_name());
3432 }
3433 // print out each of the interface classes referred to by this class.
3434 objArrayHandle local_interfaces(THREAD, this_klass->local_interfaces());
3435 if (!local_interfaces.is_null()) {
3436 int length = local_interfaces->length();
3437 for (int i = 0; i < length; i++) {
3438 klassOop k = klassOop(local_interfaces->obj_at(i));
3439 instanceKlass* to_class = instanceKlass::cast(k);
3440 const char * to = to_class->external_name();
3441 tty->print("RESOLVE %s %s (interface)\n", from, to);
3442 }
3443 }
3444 }
3446 #ifndef PRODUCT
3447 if( PrintCompactFieldsSavings ) {
3448 if( nonstatic_field_size < orig_nonstatic_field_size ) {
3449 tty->print("[Saved %d of %d bytes in %s]\n",
3450 (orig_nonstatic_field_size - nonstatic_field_size)*heapOopSize,
3451 orig_nonstatic_field_size*heapOopSize,
3452 this_klass->external_name());
3453 } else if( nonstatic_field_size > orig_nonstatic_field_size ) {
3454 tty->print("[Wasted %d over %d bytes in %s]\n",
3455 (nonstatic_field_size - orig_nonstatic_field_size)*heapOopSize,
3456 orig_nonstatic_field_size*heapOopSize,
3457 this_klass->external_name());
3458 }
3459 }
3460 #endif
3462 // preserve result across HandleMark
3463 preserve_this_klass = this_klass();
3464 }
3466 // Create new handle outside HandleMark
3467 instanceKlassHandle this_klass (THREAD, preserve_this_klass);
3468 debug_only(this_klass->as_klassOop()->verify();)
3470 return this_klass;
3471 }
3474 unsigned int
3475 ClassFileParser::compute_oop_map_count(instanceKlassHandle super,
3476 unsigned int nonstatic_oop_map_count,
3477 int first_nonstatic_oop_offset) {
3478 unsigned int map_count =
3479 super.is_null() ? 0 : super->nonstatic_oop_map_count();
3480 if (nonstatic_oop_map_count > 0) {
3481 // We have oops to add to map
3482 if (map_count == 0) {
3483 map_count = nonstatic_oop_map_count;
3484 } else {
3485 // Check whether we should add a new map block or whether the last one can
3486 // be extended
3487 OopMapBlock* const first_map = super->start_of_nonstatic_oop_maps();
3488 OopMapBlock* const last_map = first_map + map_count - 1;
3490 int next_offset = last_map->offset() + last_map->count() * heapOopSize;
3491 if (next_offset == first_nonstatic_oop_offset) {
3492 // There is no gap bettwen superklass's last oop field and first
3493 // local oop field, merge maps.
3494 nonstatic_oop_map_count -= 1;
3495 } else {
3496 // Superklass didn't end with a oop field, add extra maps
3497 assert(next_offset < first_nonstatic_oop_offset, "just checking");
3498 }
3499 map_count += nonstatic_oop_map_count;
3500 }
3501 }
3502 return map_count;
3503 }
3506 void ClassFileParser::fill_oop_maps(instanceKlassHandle k,
3507 unsigned int nonstatic_oop_map_count,
3508 int* nonstatic_oop_offsets,
3509 unsigned int* nonstatic_oop_counts) {
3510 OopMapBlock* this_oop_map = k->start_of_nonstatic_oop_maps();
3511 const instanceKlass* const super = k->superklass();
3512 const unsigned int super_count = super ? super->nonstatic_oop_map_count() : 0;
3513 if (super_count > 0) {
3514 // Copy maps from superklass
3515 OopMapBlock* super_oop_map = super->start_of_nonstatic_oop_maps();
3516 for (unsigned int i = 0; i < super_count; ++i) {
3517 *this_oop_map++ = *super_oop_map++;
3518 }
3519 }
3521 if (nonstatic_oop_map_count > 0) {
3522 if (super_count + nonstatic_oop_map_count > k->nonstatic_oop_map_count()) {
3523 // The counts differ because there is no gap between superklass's last oop
3524 // field and the first local oop field. Extend the last oop map copied
3525 // from the superklass instead of creating new one.
3526 nonstatic_oop_map_count--;
3527 nonstatic_oop_offsets++;
3528 this_oop_map--;
3529 this_oop_map->set_count(this_oop_map->count() + *nonstatic_oop_counts++);
3530 this_oop_map++;
3531 }
3533 // Add new map blocks, fill them
3534 while (nonstatic_oop_map_count-- > 0) {
3535 this_oop_map->set_offset(*nonstatic_oop_offsets++);
3536 this_oop_map->set_count(*nonstatic_oop_counts++);
3537 this_oop_map++;
3538 }
3539 assert(k->start_of_nonstatic_oop_maps() + k->nonstatic_oop_map_count() ==
3540 this_oop_map, "sanity");
3541 }
3542 }
3545 void ClassFileParser::set_precomputed_flags(instanceKlassHandle k) {
3546 klassOop super = k->super();
3548 // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
3549 // in which case we don't have to register objects as finalizable
3550 if (!_has_empty_finalizer) {
3551 if (_has_finalizer ||
3552 (super != NULL && super->klass_part()->has_finalizer())) {
3553 k->set_has_finalizer();
3554 }
3555 }
3557 #ifdef ASSERT
3558 bool f = false;
3559 methodOop m = k->lookup_method(vmSymbols::finalize_method_name(),
3560 vmSymbols::void_method_signature());
3561 if (m != NULL && !m->is_empty_method()) {
3562 f = true;
3563 }
3564 assert(f == k->has_finalizer(), "inconsistent has_finalizer");
3565 #endif
3567 // Check if this klass supports the java.lang.Cloneable interface
3568 if (SystemDictionary::Cloneable_klass_loaded()) {
3569 if (k->is_subtype_of(SystemDictionary::Cloneable_klass())) {
3570 k->set_is_cloneable();
3571 }
3572 }
3574 // Check if this klass has a vanilla default constructor
3575 if (super == NULL) {
3576 // java.lang.Object has empty default constructor
3577 k->set_has_vanilla_constructor();
3578 } else {
3579 if (Klass::cast(super)->has_vanilla_constructor() &&
3580 _has_vanilla_constructor) {
3581 k->set_has_vanilla_constructor();
3582 }
3583 #ifdef ASSERT
3584 bool v = false;
3585 if (Klass::cast(super)->has_vanilla_constructor()) {
3586 methodOop constructor = k->find_method(vmSymbols::object_initializer_name(
3587 ), vmSymbols::void_method_signature());
3588 if (constructor != NULL && constructor->is_vanilla_constructor()) {
3589 v = true;
3590 }
3591 }
3592 assert(v == k->has_vanilla_constructor(), "inconsistent has_vanilla_constructor");
3593 #endif
3594 }
3596 // If it cannot be fast-path allocated, set a bit in the layout helper.
3597 // See documentation of instanceKlass::can_be_fastpath_allocated().
3598 assert(k->size_helper() > 0, "layout_helper is initialized");
3599 if ((!RegisterFinalizersAtInit && k->has_finalizer())
3600 || k->is_abstract() || k->is_interface()
3601 || (k->name() == vmSymbols::java_lang_Class()
3602 && k->class_loader() == NULL)
3603 || k->size_helper() >= FastAllocateSizeLimit) {
3604 // Forbid fast-path allocation.
3605 jint lh = Klass::instance_layout_helper(k->size_helper(), true);
3606 k->set_layout_helper(lh);
3607 }
3608 }
3611 // utility method for appending and array with check for duplicates
3613 void append_interfaces(objArrayHandle result, int& index, objArrayOop ifs) {
3614 // iterate over new interfaces
3615 for (int i = 0; i < ifs->length(); i++) {
3616 oop e = ifs->obj_at(i);
3617 assert(e->is_klass() && instanceKlass::cast(klassOop(e))->is_interface(), "just checking");
3618 // check for duplicates
3619 bool duplicate = false;
3620 for (int j = 0; j < index; j++) {
3621 if (result->obj_at(j) == e) {
3622 duplicate = true;
3623 break;
3624 }
3625 }
3626 // add new interface
3627 if (!duplicate) {
3628 result->obj_at_put(index++, e);
3629 }
3630 }
3631 }
3633 objArrayHandle ClassFileParser::compute_transitive_interfaces(instanceKlassHandle super, objArrayHandle local_ifs, TRAPS) {
3634 // Compute maximum size for transitive interfaces
3635 int max_transitive_size = 0;
3636 int super_size = 0;
3637 // Add superclass transitive interfaces size
3638 if (super.not_null()) {
3639 super_size = super->transitive_interfaces()->length();
3640 max_transitive_size += super_size;
3641 }
3642 // Add local interfaces' super interfaces
3643 int local_size = local_ifs->length();
3644 for (int i = 0; i < local_size; i++) {
3645 klassOop l = klassOop(local_ifs->obj_at(i));
3646 max_transitive_size += instanceKlass::cast(l)->transitive_interfaces()->length();
3647 }
3648 // Finally add local interfaces
3649 max_transitive_size += local_size;
3650 // Construct array
3651 objArrayHandle result;
3652 if (max_transitive_size == 0) {
3653 // no interfaces, use canonicalized array
3654 result = objArrayHandle(THREAD, Universe::the_empty_system_obj_array());
3655 } else if (max_transitive_size == super_size) {
3656 // no new local interfaces added, share superklass' transitive interface array
3657 result = objArrayHandle(THREAD, super->transitive_interfaces());
3658 } else if (max_transitive_size == local_size) {
3659 // only local interfaces added, share local interface array
3660 result = local_ifs;
3661 } else {
3662 objArrayHandle nullHandle;
3663 objArrayOop new_objarray = oopFactory::new_system_objArray(max_transitive_size, CHECK_(nullHandle));
3664 result = objArrayHandle(THREAD, new_objarray);
3665 int index = 0;
3666 // Copy down from superclass
3667 if (super.not_null()) {
3668 append_interfaces(result, index, super->transitive_interfaces());
3669 }
3670 // Copy down from local interfaces' superinterfaces
3671 for (int i = 0; i < local_ifs->length(); i++) {
3672 klassOop l = klassOop(local_ifs->obj_at(i));
3673 append_interfaces(result, index, instanceKlass::cast(l)->transitive_interfaces());
3674 }
3675 // Finally add local interfaces
3676 append_interfaces(result, index, local_ifs());
3678 // Check if duplicates were removed
3679 if (index != max_transitive_size) {
3680 assert(index < max_transitive_size, "just checking");
3681 objArrayOop new_result = oopFactory::new_system_objArray(index, CHECK_(nullHandle));
3682 for (int i = 0; i < index; i++) {
3683 oop e = result->obj_at(i);
3684 assert(e != NULL, "just checking");
3685 new_result->obj_at_put(i, e);
3686 }
3687 result = objArrayHandle(THREAD, new_result);
3688 }
3689 }
3690 return result;
3691 }
3694 void ClassFileParser::check_super_class_access(instanceKlassHandle this_klass, TRAPS) {
3695 klassOop super = this_klass->super();
3696 if ((super != NULL) &&
3697 (!Reflection::verify_class_access(this_klass->as_klassOop(), super, false))) {
3698 ResourceMark rm(THREAD);
3699 Exceptions::fthrow(
3700 THREAD_AND_LOCATION,
3701 vmSymbols::java_lang_IllegalAccessError(),
3702 "class %s cannot access its superclass %s",
3703 this_klass->external_name(),
3704 instanceKlass::cast(super)->external_name()
3705 );
3706 return;
3707 }
3708 }
3711 void ClassFileParser::check_super_interface_access(instanceKlassHandle this_klass, TRAPS) {
3712 objArrayHandle local_interfaces (THREAD, this_klass->local_interfaces());
3713 int lng = local_interfaces->length();
3714 for (int i = lng - 1; i >= 0; i--) {
3715 klassOop k = klassOop(local_interfaces->obj_at(i));
3716 assert (k != NULL && Klass::cast(k)->is_interface(), "invalid interface");
3717 if (!Reflection::verify_class_access(this_klass->as_klassOop(), k, false)) {
3718 ResourceMark rm(THREAD);
3719 Exceptions::fthrow(
3720 THREAD_AND_LOCATION,
3721 vmSymbols::java_lang_IllegalAccessError(),
3722 "class %s cannot access its superinterface %s",
3723 this_klass->external_name(),
3724 instanceKlass::cast(k)->external_name()
3725 );
3726 return;
3727 }
3728 }
3729 }
3732 void ClassFileParser::check_final_method_override(instanceKlassHandle this_klass, TRAPS) {
3733 objArrayHandle methods (THREAD, this_klass->methods());
3734 int num_methods = methods->length();
3736 // go thru each method and check if it overrides a final method
3737 for (int index = 0; index < num_methods; index++) {
3738 methodOop m = (methodOop)methods->obj_at(index);
3740 // skip private, static and <init> methods
3741 if ((!m->is_private()) &&
3742 (!m->is_static()) &&
3743 (m->name() != vmSymbols::object_initializer_name())) {
3745 Symbol* name = m->name();
3746 Symbol* signature = m->signature();
3747 klassOop k = this_klass->super();
3748 methodOop super_m = NULL;
3749 while (k != NULL) {
3750 // skip supers that don't have final methods.
3751 if (k->klass_part()->has_final_method()) {
3752 // lookup a matching method in the super class hierarchy
3753 super_m = instanceKlass::cast(k)->lookup_method(name, signature);
3754 if (super_m == NULL) {
3755 break; // didn't find any match; get out
3756 }
3758 if (super_m->is_final() &&
3759 // matching method in super is final
3760 (Reflection::verify_field_access(this_klass->as_klassOop(),
3761 super_m->method_holder(),
3762 super_m->method_holder(),
3763 super_m->access_flags(), false))
3764 // this class can access super final method and therefore override
3765 ) {
3766 ResourceMark rm(THREAD);
3767 Exceptions::fthrow(
3768 THREAD_AND_LOCATION,
3769 vmSymbols::java_lang_VerifyError(),
3770 "class %s overrides final method %s.%s",
3771 this_klass->external_name(),
3772 name->as_C_string(),
3773 signature->as_C_string()
3774 );
3775 return;
3776 }
3778 // continue to look from super_m's holder's super.
3779 k = instanceKlass::cast(super_m->method_holder())->super();
3780 continue;
3781 }
3783 k = k->klass_part()->super();
3784 }
3785 }
3786 }
3787 }
3790 // assumes that this_klass is an interface
3791 void ClassFileParser::check_illegal_static_method(instanceKlassHandle this_klass, TRAPS) {
3792 assert(this_klass->is_interface(), "not an interface");
3793 objArrayHandle methods (THREAD, this_klass->methods());
3794 int num_methods = methods->length();
3796 for (int index = 0; index < num_methods; index++) {
3797 methodOop m = (methodOop)methods->obj_at(index);
3798 // if m is static and not the init method, throw a verify error
3799 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
3800 ResourceMark rm(THREAD);
3801 Exceptions::fthrow(
3802 THREAD_AND_LOCATION,
3803 vmSymbols::java_lang_VerifyError(),
3804 "Illegal static method %s in interface %s",
3805 m->name()->as_C_string(),
3806 this_klass->external_name()
3807 );
3808 return;
3809 }
3810 }
3811 }
3813 // utility methods for format checking
3815 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) {
3816 if (!_need_verify) { return; }
3818 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
3819 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
3820 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3821 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
3822 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3823 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
3824 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3826 if ((is_abstract && is_final) ||
3827 (is_interface && !is_abstract) ||
3828 (is_interface && major_gte_15 && (is_super || is_enum)) ||
3829 (!is_interface && major_gte_15 && is_annotation)) {
3830 ResourceMark rm(THREAD);
3831 Exceptions::fthrow(
3832 THREAD_AND_LOCATION,
3833 vmSymbols::java_lang_ClassFormatError(),
3834 "Illegal class modifiers in class %s: 0x%X",
3835 _class_name->as_C_string(), flags
3836 );
3837 return;
3838 }
3839 }
3841 bool ClassFileParser::has_illegal_visibility(jint flags) {
3842 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3843 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3844 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3846 return ((is_public && is_protected) ||
3847 (is_public && is_private) ||
3848 (is_protected && is_private));
3849 }
3851 bool ClassFileParser::is_supported_version(u2 major, u2 minor) {
3852 u2 max_version =
3853 JDK_Version::is_gte_jdk17x_version() ? JAVA_MAX_SUPPORTED_VERSION :
3854 (JDK_Version::is_gte_jdk16x_version() ? JAVA_6_VERSION : JAVA_1_5_VERSION);
3855 return (major >= JAVA_MIN_SUPPORTED_VERSION) &&
3856 (major <= max_version) &&
3857 ((major != max_version) ||
3858 (minor <= JAVA_MAX_SUPPORTED_MINOR_VERSION));
3859 }
3861 void ClassFileParser::verify_legal_field_modifiers(
3862 jint flags, bool is_interface, TRAPS) {
3863 if (!_need_verify) { return; }
3865 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3866 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
3867 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3868 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3869 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3870 const bool is_volatile = (flags & JVM_ACC_VOLATILE) != 0;
3871 const bool is_transient = (flags & JVM_ACC_TRANSIENT) != 0;
3872 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
3873 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3875 bool is_illegal = false;
3877 if (is_interface) {
3878 if (!is_public || !is_static || !is_final || is_private ||
3879 is_protected || is_volatile || is_transient ||
3880 (major_gte_15 && is_enum)) {
3881 is_illegal = true;
3882 }
3883 } else { // not interface
3884 if (has_illegal_visibility(flags) || (is_final && is_volatile)) {
3885 is_illegal = true;
3886 }
3887 }
3889 if (is_illegal) {
3890 ResourceMark rm(THREAD);
3891 Exceptions::fthrow(
3892 THREAD_AND_LOCATION,
3893 vmSymbols::java_lang_ClassFormatError(),
3894 "Illegal field modifiers in class %s: 0x%X",
3895 _class_name->as_C_string(), flags);
3896 return;
3897 }
3898 }
3900 void ClassFileParser::verify_legal_method_modifiers(
3901 jint flags, bool is_interface, Symbol* name, TRAPS) {
3902 if (!_need_verify) { return; }
3904 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
3905 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
3906 const bool is_static = (flags & JVM_ACC_STATIC) != 0;
3907 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
3908 const bool is_native = (flags & JVM_ACC_NATIVE) != 0;
3909 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
3910 const bool is_bridge = (flags & JVM_ACC_BRIDGE) != 0;
3911 const bool is_strict = (flags & JVM_ACC_STRICT) != 0;
3912 const bool is_synchronized = (flags & JVM_ACC_SYNCHRONIZED) != 0;
3913 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
3914 const bool is_initializer = (name == vmSymbols::object_initializer_name());
3916 bool is_illegal = false;
3918 if (is_interface) {
3919 if (!is_abstract || !is_public || is_static || is_final ||
3920 is_native || (major_gte_15 && (is_synchronized || is_strict))) {
3921 is_illegal = true;
3922 }
3923 } else { // not interface
3924 if (is_initializer) {
3925 if (is_static || is_final || is_synchronized || is_native ||
3926 is_abstract || (major_gte_15 && is_bridge)) {
3927 is_illegal = true;
3928 }
3929 } else { // not initializer
3930 if (is_abstract) {
3931 if ((is_final || is_native || is_private || is_static ||
3932 (major_gte_15 && (is_synchronized || is_strict)))) {
3933 is_illegal = true;
3934 }
3935 }
3936 if (has_illegal_visibility(flags)) {
3937 is_illegal = true;
3938 }
3939 }
3940 }
3942 if (is_illegal) {
3943 ResourceMark rm(THREAD);
3944 Exceptions::fthrow(
3945 THREAD_AND_LOCATION,
3946 vmSymbols::java_lang_ClassFormatError(),
3947 "Method %s in class %s has illegal modifiers: 0x%X",
3948 name->as_C_string(), _class_name->as_C_string(), flags);
3949 return;
3950 }
3951 }
3953 void ClassFileParser::verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) {
3954 assert(_need_verify, "only called when _need_verify is true");
3955 int i = 0;
3956 int count = length >> 2;
3957 for (int k=0; k<count; k++) {
3958 unsigned char b0 = buffer[i];
3959 unsigned char b1 = buffer[i+1];
3960 unsigned char b2 = buffer[i+2];
3961 unsigned char b3 = buffer[i+3];
3962 // For an unsigned char v,
3963 // (v | v - 1) is < 128 (highest bit 0) for 0 < v < 128;
3964 // (v | v - 1) is >= 128 (highest bit 1) for v == 0 or v >= 128.
3965 unsigned char res = b0 | b0 - 1 |
3966 b1 | b1 - 1 |
3967 b2 | b2 - 1 |
3968 b3 | b3 - 1;
3969 if (res >= 128) break;
3970 i += 4;
3971 }
3972 for(; i < length; i++) {
3973 unsigned short c;
3974 // no embedded zeros
3975 guarantee_property((buffer[i] != 0), "Illegal UTF8 string in constant pool in class file %s", CHECK);
3976 if(buffer[i] < 128) {
3977 continue;
3978 }
3979 if ((i + 5) < length) { // see if it's legal supplementary character
3980 if (UTF8::is_supplementary_character(&buffer[i])) {
3981 c = UTF8::get_supplementary_character(&buffer[i]);
3982 i += 5;
3983 continue;
3984 }
3985 }
3986 switch (buffer[i] >> 4) {
3987 default: break;
3988 case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
3989 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
3990 case 0xC: case 0xD: // 110xxxxx 10xxxxxx
3991 c = (buffer[i] & 0x1F) << 6;
3992 i++;
3993 if ((i < length) && ((buffer[i] & 0xC0) == 0x80)) {
3994 c += buffer[i] & 0x3F;
3995 if (_major_version <= 47 || c == 0 || c >= 0x80) {
3996 // for classes with major > 47, c must a null or a character in its shortest form
3997 break;
3998 }
3999 }
4000 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4001 case 0xE: // 1110xxxx 10xxxxxx 10xxxxxx
4002 c = (buffer[i] & 0xF) << 12;
4003 i += 2;
4004 if ((i < length) && ((buffer[i-1] & 0xC0) == 0x80) && ((buffer[i] & 0xC0) == 0x80)) {
4005 c += ((buffer[i-1] & 0x3F) << 6) + (buffer[i] & 0x3F);
4006 if (_major_version <= 47 || c >= 0x800) {
4007 // for classes with major > 47, c must be in its shortest form
4008 break;
4009 }
4010 }
4011 classfile_parse_error("Illegal UTF8 string in constant pool in class file %s", CHECK);
4012 } // end of switch
4013 } // end of for
4014 }
4016 // Checks if name is a legal class name.
4017 void ClassFileParser::verify_legal_class_name(Symbol* name, TRAPS) {
4018 if (!_need_verify || _relax_verify) { return; }
4020 char buf[fixed_buffer_size];
4021 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4022 unsigned int length = name->utf8_length();
4023 bool legal = false;
4025 if (length > 0) {
4026 char* p;
4027 if (bytes[0] == JVM_SIGNATURE_ARRAY) {
4028 p = skip_over_field_signature(bytes, false, length, CHECK);
4029 legal = (p != NULL) && ((p - bytes) == (int)length);
4030 } else if (_major_version < JAVA_1_5_VERSION) {
4031 if (bytes[0] != '<') {
4032 p = skip_over_field_name(bytes, true, length);
4033 legal = (p != NULL) && ((p - bytes) == (int)length);
4034 }
4035 } else {
4036 // 4900761: relax the constraints based on JSR202 spec
4037 // Class names may be drawn from the entire Unicode character set.
4038 // Identifiers between '/' must be unqualified names.
4039 // The utf8 string has been verified when parsing cpool entries.
4040 legal = verify_unqualified_name(bytes, length, LegalClass);
4041 }
4042 }
4043 if (!legal) {
4044 ResourceMark rm(THREAD);
4045 Exceptions::fthrow(
4046 THREAD_AND_LOCATION,
4047 vmSymbols::java_lang_ClassFormatError(),
4048 "Illegal class name \"%s\" in class file %s", bytes,
4049 _class_name->as_C_string()
4050 );
4051 return;
4052 }
4053 }
4055 // Checks if name is a legal field name.
4056 void ClassFileParser::verify_legal_field_name(Symbol* name, TRAPS) {
4057 if (!_need_verify || _relax_verify) { return; }
4059 char buf[fixed_buffer_size];
4060 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4061 unsigned int length = name->utf8_length();
4062 bool legal = false;
4064 if (length > 0) {
4065 if (_major_version < JAVA_1_5_VERSION) {
4066 if (bytes[0] != '<') {
4067 char* p = skip_over_field_name(bytes, false, length);
4068 legal = (p != NULL) && ((p - bytes) == (int)length);
4069 }
4070 } else {
4071 // 4881221: relax the constraints based on JSR202 spec
4072 legal = verify_unqualified_name(bytes, length, LegalField);
4073 }
4074 }
4076 if (!legal) {
4077 ResourceMark rm(THREAD);
4078 Exceptions::fthrow(
4079 THREAD_AND_LOCATION,
4080 vmSymbols::java_lang_ClassFormatError(),
4081 "Illegal field name \"%s\" in class %s", bytes,
4082 _class_name->as_C_string()
4083 );
4084 return;
4085 }
4086 }
4088 // Checks if name is a legal method name.
4089 void ClassFileParser::verify_legal_method_name(Symbol* name, TRAPS) {
4090 if (!_need_verify || _relax_verify) { return; }
4092 assert(name != NULL, "method name is null");
4093 char buf[fixed_buffer_size];
4094 char* bytes = name->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4095 unsigned int length = name->utf8_length();
4096 bool legal = false;
4098 if (length > 0) {
4099 if (bytes[0] == '<') {
4100 if (name == vmSymbols::object_initializer_name() || name == vmSymbols::class_initializer_name()) {
4101 legal = true;
4102 }
4103 } else if (_major_version < JAVA_1_5_VERSION) {
4104 char* p;
4105 p = skip_over_field_name(bytes, false, length);
4106 legal = (p != NULL) && ((p - bytes) == (int)length);
4107 } else {
4108 // 4881221: relax the constraints based on JSR202 spec
4109 legal = verify_unqualified_name(bytes, length, LegalMethod);
4110 }
4111 }
4113 if (!legal) {
4114 ResourceMark rm(THREAD);
4115 Exceptions::fthrow(
4116 THREAD_AND_LOCATION,
4117 vmSymbols::java_lang_ClassFormatError(),
4118 "Illegal method name \"%s\" in class %s", bytes,
4119 _class_name->as_C_string()
4120 );
4121 return;
4122 }
4123 }
4126 // Checks if signature is a legal field signature.
4127 void ClassFileParser::verify_legal_field_signature(Symbol* name, Symbol* signature, TRAPS) {
4128 if (!_need_verify) { return; }
4130 char buf[fixed_buffer_size];
4131 char* bytes = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4132 unsigned int length = signature->utf8_length();
4133 char* p = skip_over_field_signature(bytes, false, length, CHECK);
4135 if (p == NULL || (p - bytes) != (int)length) {
4136 throwIllegalSignature("Field", name, signature, CHECK);
4137 }
4138 }
4140 // Checks if signature is a legal method signature.
4141 // Returns number of parameters
4142 int ClassFileParser::verify_legal_method_signature(Symbol* name, Symbol* signature, TRAPS) {
4143 if (!_need_verify) {
4144 // make sure caller's args_size will be less than 0 even for non-static
4145 // method so it will be recomputed in compute_size_of_parameters().
4146 return -2;
4147 }
4149 unsigned int args_size = 0;
4150 char buf[fixed_buffer_size];
4151 char* p = signature->as_utf8_flexible_buffer(THREAD, buf, fixed_buffer_size);
4152 unsigned int length = signature->utf8_length();
4153 char* nextp;
4155 // The first character must be a '('
4156 if ((length > 0) && (*p++ == JVM_SIGNATURE_FUNC)) {
4157 length--;
4158 // Skip over legal field signatures
4159 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4160 while ((length > 0) && (nextp != NULL)) {
4161 args_size++;
4162 if (p[0] == 'J' || p[0] == 'D') {
4163 args_size++;
4164 }
4165 length -= nextp - p;
4166 p = nextp;
4167 nextp = skip_over_field_signature(p, false, length, CHECK_0);
4168 }
4169 // The first non-signature thing better be a ')'
4170 if ((length > 0) && (*p++ == JVM_SIGNATURE_ENDFUNC)) {
4171 length--;
4172 if (name->utf8_length() > 0 && name->byte_at(0) == '<') {
4173 // All internal methods must return void
4174 if ((length == 1) && (p[0] == JVM_SIGNATURE_VOID)) {
4175 return args_size;
4176 }
4177 } else {
4178 // Now we better just have a return value
4179 nextp = skip_over_field_signature(p, true, length, CHECK_0);
4180 if (nextp && ((int)length == (nextp - p))) {
4181 return args_size;
4182 }
4183 }
4184 }
4185 }
4186 // Report error
4187 throwIllegalSignature("Method", name, signature, CHECK_0);
4188 return 0;
4189 }
4192 // Unqualified names may not contain the characters '.', ';', '[', or '/'.
4193 // Method names also may not contain the characters '<' or '>', unless <init>
4194 // or <clinit>. Note that method names may not be <init> or <clinit> in this
4195 // method. Because these names have been checked as special cases before
4196 // calling this method in verify_legal_method_name.
4197 bool ClassFileParser::verify_unqualified_name(
4198 char* name, unsigned int length, int type) {
4199 jchar ch;
4201 for (char* p = name; p != name + length; ) {
4202 ch = *p;
4203 if (ch < 128) {
4204 p++;
4205 if (ch == '.' || ch == ';' || ch == '[' ) {
4206 return false; // do not permit '.', ';', or '['
4207 }
4208 if (type != LegalClass && ch == '/') {
4209 return false; // do not permit '/' unless it's class name
4210 }
4211 if (type == LegalMethod && (ch == '<' || ch == '>')) {
4212 return false; // do not permit '<' or '>' in method names
4213 }
4214 } else {
4215 char* tmp_p = UTF8::next(p, &ch);
4216 p = tmp_p;
4217 }
4218 }
4219 return true;
4220 }
4223 // Take pointer to a string. Skip over the longest part of the string that could
4224 // be taken as a fieldname. Allow '/' if slash_ok is true.
4225 // Return a pointer to just past the fieldname.
4226 // Return NULL if no fieldname at all was found, or in the case of slash_ok
4227 // being true, we saw consecutive slashes (meaning we were looking for a
4228 // qualified path but found something that was badly-formed).
4229 char* ClassFileParser::skip_over_field_name(char* name, bool slash_ok, unsigned int length) {
4230 char* p;
4231 jchar ch;
4232 jboolean last_is_slash = false;
4233 jboolean not_first_ch = false;
4235 for (p = name; p != name + length; not_first_ch = true) {
4236 char* old_p = p;
4237 ch = *p;
4238 if (ch < 128) {
4239 p++;
4240 // quick check for ascii
4241 if ((ch >= 'a' && ch <= 'z') ||
4242 (ch >= 'A' && ch <= 'Z') ||
4243 (ch == '_' || ch == '$') ||
4244 (not_first_ch && ch >= '0' && ch <= '9')) {
4245 last_is_slash = false;
4246 continue;
4247 }
4248 if (slash_ok && ch == '/') {
4249 if (last_is_slash) {
4250 return NULL; // Don't permit consecutive slashes
4251 }
4252 last_is_slash = true;
4253 continue;
4254 }
4255 } else {
4256 jint unicode_ch;
4257 char* tmp_p = UTF8::next_character(p, &unicode_ch);
4258 p = tmp_p;
4259 last_is_slash = false;
4260 // Check if ch is Java identifier start or is Java identifier part
4261 // 4672820: call java.lang.Character methods directly without generating separate tables.
4262 EXCEPTION_MARK;
4263 instanceKlassHandle klass (THREAD, SystemDictionary::Character_klass());
4265 // return value
4266 JavaValue result(T_BOOLEAN);
4267 // Set up the arguments to isJavaIdentifierStart and isJavaIdentifierPart
4268 JavaCallArguments args;
4269 args.push_int(unicode_ch);
4271 // public static boolean isJavaIdentifierStart(char ch);
4272 JavaCalls::call_static(&result,
4273 klass,
4274 vmSymbols::isJavaIdentifierStart_name(),
4275 vmSymbols::int_bool_signature(),
4276 &args,
4277 THREAD);
4279 if (HAS_PENDING_EXCEPTION) {
4280 CLEAR_PENDING_EXCEPTION;
4281 return 0;
4282 }
4283 if (result.get_jboolean()) {
4284 continue;
4285 }
4287 if (not_first_ch) {
4288 // public static boolean isJavaIdentifierPart(char ch);
4289 JavaCalls::call_static(&result,
4290 klass,
4291 vmSymbols::isJavaIdentifierPart_name(),
4292 vmSymbols::int_bool_signature(),
4293 &args,
4294 THREAD);
4296 if (HAS_PENDING_EXCEPTION) {
4297 CLEAR_PENDING_EXCEPTION;
4298 return 0;
4299 }
4301 if (result.get_jboolean()) {
4302 continue;
4303 }
4304 }
4305 }
4306 return (not_first_ch) ? old_p : NULL;
4307 }
4308 return (not_first_ch) ? p : NULL;
4309 }
4312 // Take pointer to a string. Skip over the longest part of the string that could
4313 // be taken as a field signature. Allow "void" if void_ok.
4314 // Return a pointer to just past the signature.
4315 // Return NULL if no legal signature is found.
4316 char* ClassFileParser::skip_over_field_signature(char* signature,
4317 bool void_ok,
4318 unsigned int length,
4319 TRAPS) {
4320 unsigned int array_dim = 0;
4321 while (length > 0) {
4322 switch (signature[0]) {
4323 case JVM_SIGNATURE_VOID: if (!void_ok) { return NULL; }
4324 case JVM_SIGNATURE_BOOLEAN:
4325 case JVM_SIGNATURE_BYTE:
4326 case JVM_SIGNATURE_CHAR:
4327 case JVM_SIGNATURE_SHORT:
4328 case JVM_SIGNATURE_INT:
4329 case JVM_SIGNATURE_FLOAT:
4330 case JVM_SIGNATURE_LONG:
4331 case JVM_SIGNATURE_DOUBLE:
4332 return signature + 1;
4333 case JVM_SIGNATURE_CLASS: {
4334 if (_major_version < JAVA_1_5_VERSION) {
4335 // Skip over the class name if one is there
4336 char* p = skip_over_field_name(signature + 1, true, --length);
4338 // The next character better be a semicolon
4339 if (p && (p - signature) > 1 && p[0] == ';') {
4340 return p + 1;
4341 }
4342 } else {
4343 // 4900761: For class version > 48, any unicode is allowed in class name.
4344 length--;
4345 signature++;
4346 while (length > 0 && signature[0] != ';') {
4347 if (signature[0] == '.') {
4348 classfile_parse_error("Class name contains illegal character '.' in descriptor in class file %s", CHECK_0);
4349 }
4350 length--;
4351 signature++;
4352 }
4353 if (signature[0] == ';') { return signature + 1; }
4354 }
4356 return NULL;
4357 }
4358 case JVM_SIGNATURE_ARRAY:
4359 array_dim++;
4360 if (array_dim > 255) {
4361 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
4362 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
4363 }
4364 // The rest of what's there better be a legal signature
4365 signature++;
4366 length--;
4367 void_ok = false;
4368 break;
4370 default:
4371 return NULL;
4372 }
4373 }
4374 return NULL;
4375 }