88
89 // Used for two backward compatibility reasons:
90 // - to check for new additions to the class file format in JDK1.5
91 // - to check for bug fixes in the format checker in JDK1.5
92 #define JAVA_1_5_VERSION 49
93
94 // Used for backward compatibility reasons:
95 // - to check for javac bug fixes that happened after 1.5
96 // - also used as the max version when running in jdk6
97 #define JAVA_6_VERSION 50
98
99 // Used for backward compatibility reasons:
100 // - to disallow argument and require ACC_STATIC for <clinit> methods
101 #define JAVA_7_VERSION 51
102
103 // Extension method support.
104 #define JAVA_8_VERSION 52
105
106 #define JAVA_9_VERSION 53
107
108 void ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
109 ConstantPool* cp,
110 const int length,
111 TRAPS) {
112 assert(stream != NULL, "invariant");
113 assert(cp != NULL, "invariant");
114
115 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
116 // this function (_current can be allocated in a register, with scalar
117 // replacement of aggregates). The _current pointer is copied back to
118 // stream() when this function returns. DON'T call another method within
119 // this method that uses stream().
120 const ClassFileStream cfs1 = *stream;
121 const ClassFileStream* const cfs = &cfs1;
122
123 assert(cfs->allocated_on_stack(), "should be local");
124 debug_only(const u1* const old_current = stream->current();)
125
126 // Used for batching symbol allocations.
127 const char* names[SymbolTable::symbol_alloc_batch_size];
128 int lengths[SymbolTable::symbol_alloc_batch_size];
129 int indices[SymbolTable::symbol_alloc_batch_size];
130 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
131 int names_count = 0;
132
133 // parsing Index 0 is unused
134 for (int index = 1; index < length; index++) {
135 // Each of the following case guarantees one more byte in the stream
136 // for the following tag or the access_flags following constant pool,
137 // so we don't need bounds-check for reading tag.
138 const u1 tag = cfs->get_u1_fast();
139 switch (tag) {
140 case JVM_CONSTANT_Class : {
141 cfs->guarantee_more(3, CHECK); // name_index, tag/access_flags
142 const u2 name_index = cfs->get_u2_fast();
143 cp->klass_index_at_put(index, name_index);
144 break;
145 }
146 case JVM_CONSTANT_Fieldref: {
147 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
148 const u2 class_index = cfs->get_u2_fast();
149 const u2 name_and_type_index = cfs->get_u2_fast();
150 cp->field_at_put(index, class_index, name_and_type_index);
151 break;
152 }
153 case JVM_CONSTANT_Methodref: {
154 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
155 const u2 class_index = cfs->get_u2_fast();
156 const u2 name_and_type_index = cfs->get_u2_fast();
157 cp->method_at_put(index, class_index, name_and_type_index);
158 break;
159 }
160 case JVM_CONSTANT_InterfaceMethodref: {
161 cfs->guarantee_more(5, CHECK); // class_index, name_and_type_index, tag/access_flags
162 const u2 class_index = cfs->get_u2_fast();
163 const u2 name_and_type_index = cfs->get_u2_fast();
164 cp->interface_method_at_put(index, class_index, name_and_type_index);
165 break;
166 }
167 case JVM_CONSTANT_String : {
168 cfs->guarantee_more(3, CHECK); // string_index, tag/access_flags
169 const u2 string_index = cfs->get_u2_fast();
170 cp->string_index_at_put(index, string_index);
171 break;
172 }
173 case JVM_CONSTANT_MethodHandle :
174 case JVM_CONSTANT_MethodType: {
175 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
176 classfile_parse_error(
177 "Class file version does not support constant tag %u in class file %s",
178 tag, CHECK);
179 }
180 if (tag == JVM_CONSTANT_MethodHandle) {
181 cfs->guarantee_more(4, CHECK); // ref_kind, method_index, tag/access_flags
182 const u1 ref_kind = cfs->get_u1_fast();
183 const u2 method_index = cfs->get_u2_fast();
184 cp->method_handle_index_at_put(index, ref_kind, method_index);
185 }
186 else if (tag == JVM_CONSTANT_MethodType) {
187 cfs->guarantee_more(3, CHECK); // signature_index, tag/access_flags
188 const u2 signature_index = cfs->get_u2_fast();
189 cp->method_type_index_at_put(index, signature_index);
190 }
191 else {
192 ShouldNotReachHere();
193 }
194 break;
195 }
196 case JVM_CONSTANT_InvokeDynamic : {
197 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
198 classfile_parse_error(
199 "Class file version does not support constant tag %u in class file %s",
200 tag, CHECK);
201 }
202 cfs->guarantee_more(5, CHECK); // bsm_index, nt, tag/access_flags
203 const u2 bootstrap_specifier_index = cfs->get_u2_fast();
204 const u2 name_and_type_index = cfs->get_u2_fast();
205 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) {
206 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later
207 }
208 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index);
209 break;
210 }
211 case JVM_CONSTANT_Integer: {
212 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
213 const u4 bytes = cfs->get_u4_fast();
214 cp->int_at_put(index, (jint)bytes);
215 break;
216 }
217 case JVM_CONSTANT_Float: {
218 cfs->guarantee_more(5, CHECK); // bytes, tag/access_flags
219 const u4 bytes = cfs->get_u4_fast();
220 cp->float_at_put(index, *(jfloat*)&bytes);
221 break;
222 }
223 case JVM_CONSTANT_Long: {
224 // A mangled type might cause you to overrun allocated memory
225 guarantee_property(index + 1 < length,
226 "Invalid constant pool entry %u in class file %s",
227 index,
228 CHECK);
229 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
230 const u8 bytes = cfs->get_u8_fast();
231 cp->long_at_put(index, bytes);
232 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
233 break;
234 }
235 case JVM_CONSTANT_Double: {
236 // A mangled type might cause you to overrun allocated memory
237 guarantee_property(index+1 < length,
238 "Invalid constant pool entry %u in class file %s",
239 index,
240 CHECK);
241 cfs->guarantee_more(9, CHECK); // bytes, tag/access_flags
242 const u8 bytes = cfs->get_u8_fast();
243 cp->double_at_put(index, *(jdouble*)&bytes);
244 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
245 break;
246 }
247 case JVM_CONSTANT_NameAndType: {
248 cfs->guarantee_more(5, CHECK); // name_index, signature_index, tag/access_flags
249 const u2 name_index = cfs->get_u2_fast();
250 const u2 signature_index = cfs->get_u2_fast();
251 cp->name_and_type_at_put(index, name_index, signature_index);
252 break;
253 }
254 case JVM_CONSTANT_Utf8 : {
255 cfs->guarantee_more(2, CHECK); // utf8_length
256 u2 utf8_length = cfs->get_u2_fast();
257 const u1* utf8_buffer = cfs->get_u1_buffer();
258 assert(utf8_buffer != NULL, "null utf8 buffer");
259 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.
260 cfs->guarantee_more(utf8_length+1, CHECK); // utf8 string, tag/access_flags
261 cfs->skip_u1_fast(utf8_length);
262
263 // Before storing the symbol, make sure it's legal
264 if (_need_verify) {
265 verify_legal_utf8(utf8_buffer, utf8_length, CHECK);
266 }
267
268 if (has_cp_patch_at(index)) {
269 Handle patch = clear_cp_patch_at(index);
270 guarantee_property(java_lang_String::is_instance(patch()),
271 "Illegal utf8 patch at %d in class file %s",
272 index,
273 CHECK);
274 const char* const str = java_lang_String::as_utf8_string(patch());
275 // (could use java_lang_String::as_symbol instead, but might as well batch them)
276 utf8_buffer = (const u1*) str;
277 utf8_length = (int) strlen(str);
278 }
279
280 unsigned int hash;
281 Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer,
282 utf8_length,
283 hash);
284 if (result == NULL) {
285 names[names_count] = (const char*)utf8_buffer;
286 lengths[names_count] = utf8_length;
287 indices[names_count] = index;
288 hashValues[names_count++] = hash;
289 if (names_count == SymbolTable::symbol_alloc_batch_size) {
290 SymbolTable::new_symbols(_loader_data,
291 cp,
292 names_count,
293 names,
294 lengths,
295 indices,
296 hashValues,
297 CHECK);
298 names_count = 0;
299 }
300 } else {
301 cp->symbol_at_put(index, result);
302 }
303 break;
304 }
305 default: {
306 classfile_parse_error("Unknown constant tag %u in class file %s",
307 tag,
308 CHECK);
309 break;
310 }
311 } // end of switch(tag)
312 } // end of for
313
314 // Allocate the remaining symbols
315 if (names_count > 0) {
316 SymbolTable::new_symbols(_loader_data,
317 cp,
318 names_count,
319 names,
320 lengths,
321 indices,
322 hashValues,
323 CHECK);
324 }
325
326 // Copy _current pointer of local copy back to stream.
327 assert(stream->current() == old_current, "non-exclusive use of stream");
328 stream->set_current(cfs1.current());
329
330 }
331
332 static inline bool valid_cp_range(int index, int length) {
333 return (index > 0 && index < length);
334 }
335
336 static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) {
337 assert(cp != NULL, "invariant");
338 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) {
339 return cp->symbol_at(index);
340 }
341 return NULL;
342 }
343
344 #ifdef ASSERT
345 PRAGMA_DIAG_PUSH
346 PRAGMA_FORMAT_NONLITERAL_IGNORED
347 void ClassFileParser::report_assert_property_failure(const char* msg, TRAPS) const {
348 ResourceMark rm(THREAD);
349 fatal(msg, _class_name->as_C_string());
350 }
351
352 void ClassFileParser::report_assert_property_failure(const char* msg,
353 int index,
354 TRAPS) const {
355 ResourceMark rm(THREAD);
356 fatal(msg, index, _class_name->as_C_string());
357 }
358 PRAGMA_DIAG_POP
359 #endif
360
361 void ClassFileParser::parse_constant_pool(const ClassFileStream* const stream,
362 ConstantPool* const cp,
363 const int length,
364 TRAPS) {
365 assert(cp != NULL, "invariant");
366 assert(stream != NULL, "invariant");
367
368 // parsing constant pool entries
369 parse_constant_pool_entries(stream, cp, length, CHECK);
370
371 int index = 1; // declared outside of loops for portability
372
373 // first verification pass - validate cross references
374 // and fixup class and string constants
375 for (index = 1; index < length; index++) { // Index 0 is unused
376 const jbyte tag = cp->tag_at(index).value();
377 switch (tag) {
378 case JVM_CONSTANT_Class: {
379 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
380 break;
381 }
382 case JVM_CONSTANT_Fieldref:
383 // fall through
384 case JVM_CONSTANT_Methodref:
385 // fall through
386 case JVM_CONSTANT_InterfaceMethodref: {
387 if (!_need_verify) break;
388 const int klass_ref_index = cp->klass_ref_index_at(index);
389 const int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
390 check_property(valid_klass_reference_at(klass_ref_index),
391 "Invalid constant pool index %u in class file %s",
392 klass_ref_index, CHECK);
393 check_property(valid_cp_range(name_and_type_ref_index, length) &&
394 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
395 "Invalid constant pool index %u in class file %s",
396 name_and_type_ref_index, CHECK);
397 break;
398 }
399 case JVM_CONSTANT_String: {
400 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
401 break;
402 }
403 case JVM_CONSTANT_Integer:
404 break;
405 case JVM_CONSTANT_Float:
406 break;
407 case JVM_CONSTANT_Long:
408 case JVM_CONSTANT_Double: {
409 index++;
410 check_property(
411 (index < length && cp->tag_at(index).is_invalid()),
412 "Improper constant pool long/double index %u in class file %s",
413 index, CHECK);
414 break;
415 }
416 case JVM_CONSTANT_NameAndType: {
417 if (!_need_verify) break;
418 const int name_ref_index = cp->name_ref_index_at(index);
419 const int signature_ref_index = cp->signature_ref_index_at(index);
420 check_property(valid_symbol_at(name_ref_index),
421 "Invalid constant pool index %u in class file %s",
422 name_ref_index, CHECK);
423 check_property(valid_symbol_at(signature_ref_index),
424 "Invalid constant pool index %u in class file %s",
425 signature_ref_index, CHECK);
426 break;
427 }
428 case JVM_CONSTANT_Utf8:
429 break;
430 case JVM_CONSTANT_UnresolvedClass: // fall-through
431 case JVM_CONSTANT_UnresolvedClassInError: {
432 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
433 break;
434 }
435 case JVM_CONSTANT_ClassIndex: {
436 const int class_index = cp->klass_index_at(index);
437 check_property(valid_symbol_at(class_index),
438 "Invalid constant pool index %u in class file %s",
439 class_index, CHECK);
440 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
441 break;
442 }
443 case JVM_CONSTANT_StringIndex: {
444 const int string_index = cp->string_index_at(index);
445 check_property(valid_symbol_at(string_index),
446 "Invalid constant pool index %u in class file %s",
447 string_index, CHECK);
448 Symbol* const sym = cp->symbol_at(string_index);
449 cp->unresolved_string_at_put(index, sym);
450 break;
451 }
452 case JVM_CONSTANT_MethodHandle: {
453 const int ref_index = cp->method_handle_index_at(index);
454 check_property(valid_cp_range(ref_index, length),
455 "Invalid constant pool index %u in class file %s",
456 ref_index, CHECK);
457 const constantTag tag = cp->tag_at(ref_index);
458 const int ref_kind = cp->method_handle_ref_kind_at(index);
459
460 switch (ref_kind) {
461 case JVM_REF_getField:
462 case JVM_REF_getStatic:
463 case JVM_REF_putField:
464 case JVM_REF_putStatic: {
465 check_property(
466 tag.is_field(),
467 "Invalid constant pool index %u in class file %s (not a field)",
468 ref_index, CHECK);
469 break;
470 }
471 case JVM_REF_invokeVirtual:
472 case JVM_REF_newInvokeSpecial: {
473 check_property(
474 tag.is_method(),
475 "Invalid constant pool index %u in class file %s (not a method)",
476 ref_index, CHECK);
477 break;
478 }
479 case JVM_REF_invokeStatic:
480 case JVM_REF_invokeSpecial: {
481 check_property(
482 tag.is_method() ||
483 ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()),
484 "Invalid constant pool index %u in class file %s (not a method)",
485 ref_index, CHECK);
486 break;
487 }
488 case JVM_REF_invokeInterface: {
489 check_property(
490 tag.is_interface_method(),
491 "Invalid constant pool index %u in class file %s (not an interface method)",
492 ref_index, CHECK);
493 break;
494 }
495 default: {
496 classfile_parse_error(
497 "Bad method handle kind at constant pool index %u in class file %s",
498 index, CHECK);
499 }
500 } // switch(refkind)
501 // Keep the ref_index unchanged. It will be indirected at link-time.
502 break;
503 } // case MethodHandle
504 case JVM_CONSTANT_MethodType: {
505 const int ref_index = cp->method_type_index_at(index);
506 check_property(valid_symbol_at(ref_index),
507 "Invalid constant pool index %u in class file %s",
508 ref_index, CHECK);
509 break;
510 }
511 case JVM_CONSTANT_InvokeDynamic: {
512 const int name_and_type_ref_index =
513 cp->invoke_dynamic_name_and_type_ref_index_at(index);
514
515 check_property(valid_cp_range(name_and_type_ref_index, length) &&
516 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
517 "Invalid constant pool index %u in class file %s",
518 name_and_type_ref_index, CHECK);
519 // bootstrap specifier index must be checked later,
520 // when BootstrapMethods attr is available
521 break;
522 }
523 default: {
524 fatal("bad constant pool tag value %u", cp->tag_at(index).value());
525 ShouldNotReachHere();
526 break;
527 }
528 } // switch(tag)
529 } // end of for
530
531 if (_cp_patches != NULL) {
532 // need to treat this_class specially...
533 int this_class_index;
534 {
535 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
536 const u1* const mark = stream->current();
537 stream->skip_u2_fast(1); // skip flags
538 this_class_index = stream->get_u2_fast();
539 stream->set_current(mark); // revert to mark
540 }
541
542 for (index = 1; index < length; index++) { // Index 0 is unused
543 if (has_cp_patch_at(index)) {
544 guarantee_property(index != this_class_index,
545 "Illegal constant pool patch to self at %d in class file %s",
546 index, CHECK);
547 patch_constant_pool(cp, index, cp_patch_at(index), CHECK);
548 }
549 }
550 }
551
552 if (!_need_verify) {
553 return;
554 }
555
556 // second verification pass - checks the strings are of the right format.
557 // but not yet to the other entries
558 for (index = 1; index < length; index++) {
559 const jbyte tag = cp->tag_at(index).value();
560 switch (tag) {
561 case JVM_CONSTANT_UnresolvedClass: {
562 const Symbol* const class_name = cp->klass_name_at(index);
563 // check the name, even if _cp_patches will overwrite it
564 verify_legal_class_name(class_name, CHECK);
565 break;
566 }
567 case JVM_CONSTANT_NameAndType: {
568 if (_need_verify) {
569 const int sig_index = cp->signature_ref_index_at(index);
570 const int name_index = cp->name_ref_index_at(index);
571 const Symbol* const name = cp->symbol_at(name_index);
572 const Symbol* const sig = cp->symbol_at(sig_index);
573 guarantee_property(sig->utf8_length() != 0,
574 "Illegal zero length constant pool entry at %d in class %s",
575 sig_index, CHECK);
576 guarantee_property(name->utf8_length() != 0,
577 "Illegal zero length constant pool entry at %d in class %s",
578 name_index, CHECK);
579
580 if (sig->byte_at(0) == JVM_SIGNATURE_FUNC) {
581 // Format check method name and signature
582 verify_legal_method_name(name, CHECK);
583 verify_legal_method_signature(name, sig, CHECK);
584 } else {
585 // Format check field name and signature
586 verify_legal_field_name(name, CHECK);
587 verify_legal_field_signature(name, sig, CHECK);
588 }
589 }
590 break;
591 }
592 case JVM_CONSTANT_InvokeDynamic:
593 case JVM_CONSTANT_Fieldref:
594 case JVM_CONSTANT_Methodref:
595 case JVM_CONSTANT_InterfaceMethodref: {
596 const int name_and_type_ref_index =
597 cp->name_and_type_ref_index_at(index);
598 // already verified to be utf8
599 const int name_ref_index =
600 cp->name_ref_index_at(name_and_type_ref_index);
601 // already verified to be utf8
602 const int signature_ref_index =
603 cp->signature_ref_index_at(name_and_type_ref_index);
604 const Symbol* const name = cp->symbol_at(name_ref_index);
605 const Symbol* const signature = cp->symbol_at(signature_ref_index);
606 if (tag == JVM_CONSTANT_Fieldref) {
607 if (_need_verify) {
608 // Field name and signature are verified above, when iterating NameAndType_info.
609 // Need only to be sure signature is non-zero length and the right type.
610 if (signature->utf8_length() == 0 ||
611 signature->byte_at(0) == JVM_SIGNATURE_FUNC) {
612 throwIllegalSignature("Field", name, signature, CHECK);
613 }
614 }
615 } else {
616 if (_need_verify) {
617 // Method name and signature are verified above, when iterating NameAndType_info.
618 // Need only to be sure signature is non-zero length and the right type.
619 if (signature->utf8_length() == 0 ||
620 signature->byte_at(0) != JVM_SIGNATURE_FUNC) {
621 throwIllegalSignature("Method", name, signature, CHECK);
622 }
623 }
624 // 4509014: If a class method name begins with '<', it must be "<init>"
625 const unsigned int name_len = name->utf8_length();
626 if (tag == JVM_CONSTANT_Methodref &&
627 name_len != 0 &&
628 name->byte_at(0) == '<' &&
629 name != vmSymbols::object_initializer_name()) {
630 classfile_parse_error(
631 "Bad method name at constant pool index %u in class file %s",
632 name_ref_index, CHECK);
633 }
634 }
635 break;
636 }
637 case JVM_CONSTANT_MethodHandle: {
638 const int ref_index = cp->method_handle_index_at(index);
639 const int ref_kind = cp->method_handle_ref_kind_at(index);
640 switch (ref_kind) {
641 case JVM_REF_invokeVirtual:
642 case JVM_REF_invokeStatic:
643 case JVM_REF_invokeSpecial:
644 case JVM_REF_newInvokeSpecial: {
645 const int name_and_type_ref_index =
646 cp->name_and_type_ref_index_at(ref_index);
647 const int name_ref_index =
648 cp->name_ref_index_at(name_and_type_ref_index);
649 const Symbol* const name = cp->symbol_at(name_ref_index);
650 if (ref_kind == JVM_REF_newInvokeSpecial) {
651 if (name != vmSymbols::object_initializer_name()) {
652 classfile_parse_error(
653 "Bad constructor name at constant pool index %u in class file %s",
654 name_ref_index, CHECK);
655 }
656 } else {
657 if (name == vmSymbols::object_initializer_name()) {
658 classfile_parse_error(
659 "Bad method name at constant pool index %u in class file %s",
660 name_ref_index, CHECK);
661 }
662 }
663 break;
664 }
665 // Other ref_kinds are already fully checked in previous pass.
666 } // switch(ref_kind)
667 break;
668 }
669 case JVM_CONSTANT_MethodType: {
670 const Symbol* const no_name = vmSymbols::type_name(); // place holder
671 const Symbol* const signature = cp->method_type_signature_at(index);
672 verify_legal_method_signature(no_name, signature, CHECK);
673 break;
674 }
675 case JVM_CONSTANT_Utf8: {
676 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
677 }
678 } // switch(tag)
679 } // end of for
680 }
681
682 void ClassFileParser::patch_constant_pool(ConstantPool* cp,
683 int index,
684 Handle patch,
685 TRAPS) {
686 assert(cp != NULL, "invariant");
687
688 BasicType patch_type = T_VOID;
689
690 switch (cp->tag_at(index).value()) {
691
692 case JVM_CONSTANT_UnresolvedClass: {
693 // Patching a class means pre-resolving it.
694 // The name in the constant pool is ignored.
695 if (java_lang_Class::is_instance(patch())) {
696 guarantee_property(!java_lang_Class::is_primitive(patch()),
697 "Illegal class patch at %d in class file %s",
698 index, CHECK);
699 cp->klass_at_put(index, java_lang_Class::as_Klass(patch()));
3036 inner_class_info_index, CHECK_0);
3037 // Outer class index
3038 const u2 outer_class_info_index = cfs->get_u2_fast();
3039 check_property(
3040 outer_class_info_index == 0 ||
3041 valid_klass_reference_at(outer_class_info_index),
3042 "outer_class_info_index %u has bad constant type in class file %s",
3043 outer_class_info_index, CHECK_0);
3044 // Inner class name
3045 const u2 inner_name_index = cfs->get_u2_fast();
3046 check_property(
3047 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3048 "inner_name_index %u has bad constant type in class file %s",
3049 inner_name_index, CHECK_0);
3050 if (_need_verify) {
3051 guarantee_property(inner_class_info_index != outer_class_info_index,
3052 "Class is both outer and inner class in class file %s", CHECK_0);
3053 }
3054 // Access flags
3055 jint flags;
3056 // JVM_ACC_MODULE is defined in JDK-9 and later.
3057 if (_major_version >= JAVA_9_VERSION) {
3058 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3059 } else {
3060 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3061 }
3062 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3063 // Set abstract bit for old class files for backward compatibility
3064 flags |= JVM_ACC_ABSTRACT;
3065 }
3066 verify_legal_class_modifiers(flags, CHECK_0);
3067 AccessFlags inner_access_flags(flags);
3068
3069 inner_classes->at_put(index++, inner_class_info_index);
3070 inner_classes->at_put(index++, outer_class_info_index);
3071 inner_classes->at_put(index++, inner_name_index);
3072 inner_classes->at_put(index++, inner_access_flags.as_short());
3073 }
3074
3075 // 4347400: make sure there's no duplicate entry in the classes array
3076 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3077 for(int i = 0; i < length * 4; i += 4) {
3078 for(int j = i + 4; j < length * 4; j += 4) {
4513 for (int index = 0; index < num_methods; index++) {
4514 const Method* const m = methods->at(index);
4515 // if m is static and not the init method, throw a verify error
4516 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4517 ResourceMark rm(THREAD);
4518 Exceptions::fthrow(
4519 THREAD_AND_LOCATION,
4520 vmSymbols::java_lang_VerifyError(),
4521 "Illegal static method %s in interface %s",
4522 m->name()->as_C_string(),
4523 this_klass->external_name()
4524 );
4525 return;
4526 }
4527 }
4528 }
4529
4530 // utility methods for format checking
4531
4532 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4533 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4534 assert(_major_version >= JAVA_9_VERSION || !is_module, "JVM_ACC_MODULE should not be set");
4535 if (is_module) {
4536 ResourceMark rm(THREAD);
4537 Exceptions::fthrow(
4538 THREAD_AND_LOCATION,
4539 vmSymbols::java_lang_NoClassDefFoundError(),
4540 "%s is not a class because access_flag ACC_MODULE is set",
4541 _class_name->as_C_string());
4542 return;
4543 }
4544
4545 if (!_need_verify) { return; }
4546
4547 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4548 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4549 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4550 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4551 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4552 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4553 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4554
4555 if ((is_abstract && is_final) ||
4556 (is_interface && !is_abstract) ||
4557 (is_interface && major_gte_15 && (is_super || is_enum)) ||
4558 (!is_interface && major_gte_15 && is_annotation)) {
4559 ResourceMark rm(THREAD);
4560 Exceptions::fthrow(
4561 THREAD_AND_LOCATION,
4562 vmSymbols::java_lang_ClassFormatError(),
4563 "Illegal class modifiers in class %s: 0x%X",
4564 _class_name->as_C_string(), flags
4565 );
4566 return;
4567 }
4568 }
4569
4570 static bool has_illegal_visibility(jint flags) {
4571 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4572 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4573 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4574
5725 _major_version,
5726 _minor_version,
5727 JAVA_MAX_SUPPORTED_VERSION,
5728 JAVA_MAX_SUPPORTED_MINOR_VERSION);
5729 return;
5730 }
5731
5732 stream->guarantee_more(3, CHECK); // length, first cp tag
5733 const u2 cp_size = stream->get_u2_fast();
5734
5735 guarantee_property(
5736 cp_size >= 1, "Illegal constant pool size %u in class file %s",
5737 cp_size, CHECK);
5738
5739 _cp = ConstantPool::allocate(_loader_data,
5740 cp_size,
5741 CHECK);
5742
5743 ConstantPool* const cp = _cp;
5744
5745 parse_constant_pool(stream, cp, cp_size, CHECK);
5746
5747 assert(cp_size == (const u2)cp->length(), "invariant");
5748
5749 // ACCESS FLAGS
5750 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5751
5752 // Access flags
5753 jint flags;
5754 // JVM_ACC_MODULE is defined in JDK-9 and later.
5755 if (_major_version >= JAVA_9_VERSION) {
5756 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5757 } else {
5758 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5759 }
5760
5761 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5762 // Set abstract bit for old class files for backward compatibility
5763 flags |= JVM_ACC_ABSTRACT;
5764 }
5765
5766 verify_legal_class_modifiers(flags, CHECK);
5767
5768 _access_flags.set_flags(flags);
5769
5770
5771 // This class and superclass
5772 _this_class_index = stream->get_u2_fast();
5773 check_property(
5774 valid_cp_range(_this_class_index, cp_size) &&
5775 cp->tag_at(_this_class_index).is_unresolved_klass(),
5776 "Invalid this class index %u in constant pool in class file %s",
5777 _this_class_index, CHECK);
5778
5779 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5780 assert(class_name_in_cp != NULL, "class_name can't be null");
5781
5782 // Update _class_name which could be null previously
5783 // to reflect the name in the constant pool
5784 _class_name = class_name_in_cp;
5785
5786 // Don't need to check whether this class name is legal or not.
5787 // It has been checked when constant pool is parsed.
5788 // However, make sure it is not an array type.
5789 if (_need_verify) {
|
88
89 // Used for two backward compatibility reasons:
90 // - to check for new additions to the class file format in JDK1.5
91 // - to check for bug fixes in the format checker in JDK1.5
92 #define JAVA_1_5_VERSION 49
93
94 // Used for backward compatibility reasons:
95 // - to check for javac bug fixes that happened after 1.5
96 // - also used as the max version when running in jdk6
97 #define JAVA_6_VERSION 50
98
99 // Used for backward compatibility reasons:
100 // - to disallow argument and require ACC_STATIC for <clinit> methods
101 #define JAVA_7_VERSION 51
102
103 // Extension method support.
104 #define JAVA_8_VERSION 52
105
106 #define JAVA_9_VERSION 53
107
108 int ClassFileParser::parse_constant_pool_entries(const ClassFileStream* const stream,
109 ConstantPool* cp,
110 const int length,
111 TRAPS) {
112 assert(stream != NULL, "invariant");
113 assert(cp != NULL, "invariant");
114
115 // Used to keep track of whether a constant pool item 19 or 20 is found. These
116 // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed
117 // in regular class files. For class file version >= 53, a CFE cannot be thrown
118 // immediately when these are seen because a NCDFE must be thrown if the class's
119 // access_flags have ACC_MODULE set. But, the access_flags haven't been looked
120 // at yet. So, the bad constant pool item is cached here and returned as the
121 // function result. Returning zero means no constant pool item 19 or 20 was found.
122 int bad_tag = 0;
123
124 // Use a local copy of ClassFileStream. It helps the C++ compiler to optimize
125 // this function (_current can be allocated in a register, with scalar
126 // replacement of aggregates). The _current pointer is copied back to
127 // stream() when this function returns. DON'T call another method within
128 // this method that uses stream().
129 const ClassFileStream cfs1 = *stream;
130 const ClassFileStream* const cfs = &cfs1;
131
132 assert(cfs->allocated_on_stack(), "should be local");
133 debug_only(const u1* const old_current = stream->current();)
134
135 // Used for batching symbol allocations.
136 const char* names[SymbolTable::symbol_alloc_batch_size];
137 int lengths[SymbolTable::symbol_alloc_batch_size];
138 int indices[SymbolTable::symbol_alloc_batch_size];
139 unsigned int hashValues[SymbolTable::symbol_alloc_batch_size];
140 int names_count = 0;
141
142 // parsing Index 0 is unused
143 for (int index = 1; index < length; index++) {
144 // Each of the following case guarantees one more byte in the stream
145 // for the following tag or the access_flags following constant pool,
146 // so we don't need bounds-check for reading tag.
147 const u1 tag = cfs->get_u1_fast();
148 switch (tag) {
149 case JVM_CONSTANT_Class : {
150 cfs->guarantee_more(3, CHECK_0); // name_index, tag/access_flags
151 const u2 name_index = cfs->get_u2_fast();
152 cp->klass_index_at_put(index, name_index);
153 break;
154 }
155 case JVM_CONSTANT_Fieldref: {
156 cfs->guarantee_more(5, CHECK_0); // class_index, name_and_type_index, tag/access_flags
157 const u2 class_index = cfs->get_u2_fast();
158 const u2 name_and_type_index = cfs->get_u2_fast();
159 cp->field_at_put(index, class_index, name_and_type_index);
160 break;
161 }
162 case JVM_CONSTANT_Methodref: {
163 cfs->guarantee_more(5, CHECK_0); // class_index, name_and_type_index, tag/access_flags
164 const u2 class_index = cfs->get_u2_fast();
165 const u2 name_and_type_index = cfs->get_u2_fast();
166 cp->method_at_put(index, class_index, name_and_type_index);
167 break;
168 }
169 case JVM_CONSTANT_InterfaceMethodref: {
170 cfs->guarantee_more(5, CHECK_0); // class_index, name_and_type_index, tag/access_flags
171 const u2 class_index = cfs->get_u2_fast();
172 const u2 name_and_type_index = cfs->get_u2_fast();
173 cp->interface_method_at_put(index, class_index, name_and_type_index);
174 break;
175 }
176 case JVM_CONSTANT_String : {
177 cfs->guarantee_more(3, CHECK_0); // string_index, tag/access_flags
178 const u2 string_index = cfs->get_u2_fast();
179 cp->string_index_at_put(index, string_index);
180 break;
181 }
182 case JVM_CONSTANT_MethodHandle :
183 case JVM_CONSTANT_MethodType: {
184 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
185 classfile_parse_error(
186 "Class file version does not support constant tag %u in class file %s",
187 tag, CHECK_0);
188 }
189 if (tag == JVM_CONSTANT_MethodHandle) {
190 cfs->guarantee_more(4, CHECK_0); // ref_kind, method_index, tag/access_flags
191 const u1 ref_kind = cfs->get_u1_fast();
192 const u2 method_index = cfs->get_u2_fast();
193 cp->method_handle_index_at_put(index, ref_kind, method_index);
194 }
195 else if (tag == JVM_CONSTANT_MethodType) {
196 cfs->guarantee_more(3, CHECK_0); // signature_index, tag/access_flags
197 const u2 signature_index = cfs->get_u2_fast();
198 cp->method_type_index_at_put(index, signature_index);
199 }
200 else {
201 ShouldNotReachHere();
202 }
203 break;
204 }
205 case JVM_CONSTANT_InvokeDynamic : {
206 if (_major_version < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
207 classfile_parse_error(
208 "Class file version does not support constant tag %u in class file %s",
209 tag, CHECK_0);
210 }
211 cfs->guarantee_more(5, CHECK_0); // bsm_index, nt, tag/access_flags
212 const u2 bootstrap_specifier_index = cfs->get_u2_fast();
213 const u2 name_and_type_index = cfs->get_u2_fast();
214 if (_max_bootstrap_specifier_index < (int) bootstrap_specifier_index) {
215 _max_bootstrap_specifier_index = (int) bootstrap_specifier_index; // collect for later
216 }
217 cp->invoke_dynamic_at_put(index, bootstrap_specifier_index, name_and_type_index);
218 break;
219 }
220 case JVM_CONSTANT_Integer: {
221 cfs->guarantee_more(5, CHECK_0); // bytes, tag/access_flags
222 const u4 bytes = cfs->get_u4_fast();
223 cp->int_at_put(index, (jint)bytes);
224 break;
225 }
226 case JVM_CONSTANT_Float: {
227 cfs->guarantee_more(5, CHECK_0); // bytes, tag/access_flags
228 const u4 bytes = cfs->get_u4_fast();
229 cp->float_at_put(index, *(jfloat*)&bytes);
230 break;
231 }
232 case JVM_CONSTANT_Long: {
233 // A mangled type might cause you to overrun allocated memory
234 guarantee_property(index + 1 < length,
235 "Invalid constant pool entry %u in class file %s",
236 index,
237 CHECK_0);
238 cfs->guarantee_more(9, CHECK_0); // bytes, tag/access_flags
239 const u8 bytes = cfs->get_u8_fast();
240 cp->long_at_put(index, bytes);
241 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
242 break;
243 }
244 case JVM_CONSTANT_Double: {
245 // A mangled type might cause you to overrun allocated memory
246 guarantee_property(index+1 < length,
247 "Invalid constant pool entry %u in class file %s",
248 index,
249 CHECK_0);
250 cfs->guarantee_more(9, CHECK_0); // bytes, tag/access_flags
251 const u8 bytes = cfs->get_u8_fast();
252 cp->double_at_put(index, *(jdouble*)&bytes);
253 index++; // Skip entry following eigth-byte constant, see JVM book p. 98
254 break;
255 }
256 case JVM_CONSTANT_NameAndType: {
257 cfs->guarantee_more(5, CHECK_0); // name_index, signature_index, tag/access_flags
258 const u2 name_index = cfs->get_u2_fast();
259 const u2 signature_index = cfs->get_u2_fast();
260 cp->name_and_type_at_put(index, name_index, signature_index);
261 break;
262 }
263 case JVM_CONSTANT_Utf8 : {
264 cfs->guarantee_more(2, CHECK_0); // utf8_length
265 u2 utf8_length = cfs->get_u2_fast();
266 const u1* utf8_buffer = cfs->get_u1_buffer();
267 assert(utf8_buffer != NULL, "null utf8 buffer");
268 // Got utf8 string, guarantee utf8_length+1 bytes, set stream position forward.
269 cfs->guarantee_more(utf8_length+1, CHECK_0); // utf8 string, tag/access_flags
270 cfs->skip_u1_fast(utf8_length);
271
272 // Before storing the symbol, make sure it's legal
273 if (_need_verify) {
274 verify_legal_utf8(utf8_buffer, utf8_length, CHECK_0);
275 }
276
277 if (has_cp_patch_at(index)) {
278 Handle patch = clear_cp_patch_at(index);
279 guarantee_property(java_lang_String::is_instance(patch()),
280 "Illegal utf8 patch at %d in class file %s",
281 index,
282 CHECK_0);
283 const char* const str = java_lang_String::as_utf8_string(patch());
284 // (could use java_lang_String::as_symbol instead, but might as well batch them)
285 utf8_buffer = (const u1*) str;
286 utf8_length = (int) strlen(str);
287 }
288
289 unsigned int hash;
290 Symbol* const result = SymbolTable::lookup_only((const char*)utf8_buffer,
291 utf8_length,
292 hash);
293 if (result == NULL) {
294 names[names_count] = (const char*)utf8_buffer;
295 lengths[names_count] = utf8_length;
296 indices[names_count] = index;
297 hashValues[names_count++] = hash;
298 if (names_count == SymbolTable::symbol_alloc_batch_size) {
299 SymbolTable::new_symbols(_loader_data,
300 cp,
301 names_count,
302 names,
303 lengths,
304 indices,
305 hashValues,
306 CHECK_0);
307 names_count = 0;
308 }
309 } else {
310 cp->symbol_at_put(index, result);
311 }
312 break;
313 }
314 case 19:
315 case 20: {
316 // Record that an error occured in these two cases but keep parsing so
317 // that ACC_Module can be checked for in the access_flags. Need to
318 // throw NoClassDefFoundError in that case.
319 if (_major_version >= JAVA_9_VERSION) {
320 cfs->guarantee_more(3, CHECK_0);
321 cfs->get_u2_fast();
322 if (bad_tag == 0) bad_tag = tag;
323 break;
324 }
325 }
326 default: {
327 classfile_parse_error("Unknown constant tag %u in class file %s",
328 tag,
329 CHECK_0);
330 break;
331 }
332 } // end of switch(tag)
333 } // end of for
334
335 // Allocate the remaining symbols
336 if (names_count > 0) {
337 SymbolTable::new_symbols(_loader_data,
338 cp,
339 names_count,
340 names,
341 lengths,
342 indices,
343 hashValues,
344 CHECK_0);
345 }
346
347 // Copy _current pointer of local copy back to stream.
348 assert(stream->current() == old_current, "non-exclusive use of stream");
349 stream->set_current(cfs1.current());
350
351 return bad_tag;
352 }
353
354 static inline bool valid_cp_range(int index, int length) {
355 return (index > 0 && index < length);
356 }
357
358 static inline Symbol* check_symbol_at(const ConstantPool* cp, int index) {
359 assert(cp != NULL, "invariant");
360 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8()) {
361 return cp->symbol_at(index);
362 }
363 return NULL;
364 }
365
366 #ifdef ASSERT
367 PRAGMA_DIAG_PUSH
368 PRAGMA_FORMAT_NONLITERAL_IGNORED
369 void ClassFileParser::report_assert_property_failure(const char* msg, TRAPS) const {
370 ResourceMark rm(THREAD);
371 fatal(msg, _class_name->as_C_string());
372 }
373
374 void ClassFileParser::report_assert_property_failure(const char* msg,
375 int index,
376 TRAPS) const {
377 ResourceMark rm(THREAD);
378 fatal(msg, index, _class_name->as_C_string());
379 }
380 PRAGMA_DIAG_POP
381 #endif
382
383 int ClassFileParser::parse_constant_pool(const ClassFileStream* const stream,
384 ConstantPool* const cp,
385 const int length,
386 TRAPS) {
387 assert(cp != NULL, "invariant");
388 assert(stream != NULL, "invariant");
389
390 // parsing constant pool entries
391 int result = parse_constant_pool_entries(stream, cp, length, CHECK_0);
392 if (result != 0) {
393 // Either a CONSTANT_Module or CONSTANT_Package entry was found in the constant
394 // pool. Return it so the caller can decide whether to throw NCDFE if it finds
395 // ACC_MODULE in the class's access_flags or throw CFE for the bad constant
396 // pool entry.
397 assert((result == 19 || result == 20) && _major_version >= JAVA_9_VERSION,
398 "Unexpected result from parse_constant_pool_entries()");
399 return result;
400 }
401
402 int index = 1; // declared outside of loops for portability
403
404 // first verification pass - validate cross references
405 // and fixup class and string constants
406 for (index = 1; index < length; index++) { // Index 0 is unused
407 const jbyte tag = cp->tag_at(index).value();
408 switch (tag) {
409 case JVM_CONSTANT_Class: {
410 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
411 break;
412 }
413 case JVM_CONSTANT_Fieldref:
414 // fall through
415 case JVM_CONSTANT_Methodref:
416 // fall through
417 case JVM_CONSTANT_InterfaceMethodref: {
418 if (!_need_verify) break;
419 const int klass_ref_index = cp->klass_ref_index_at(index);
420 const int name_and_type_ref_index = cp->name_and_type_ref_index_at(index);
421 check_property(valid_klass_reference_at(klass_ref_index),
422 "Invalid constant pool index %u in class file %s",
423 klass_ref_index, CHECK_0);
424 check_property(valid_cp_range(name_and_type_ref_index, length) &&
425 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
426 "Invalid constant pool index %u in class file %s",
427 name_and_type_ref_index, CHECK_0);
428 break;
429 }
430 case JVM_CONSTANT_String: {
431 ShouldNotReachHere(); // Only JVM_CONSTANT_StringIndex should be present
432 break;
433 }
434 case JVM_CONSTANT_Integer:
435 break;
436 case JVM_CONSTANT_Float:
437 break;
438 case JVM_CONSTANT_Long:
439 case JVM_CONSTANT_Double: {
440 index++;
441 check_property(
442 (index < length && cp->tag_at(index).is_invalid()),
443 "Improper constant pool long/double index %u in class file %s",
444 index, CHECK_0);
445 break;
446 }
447 case JVM_CONSTANT_NameAndType: {
448 if (!_need_verify) break;
449 const int name_ref_index = cp->name_ref_index_at(index);
450 const int signature_ref_index = cp->signature_ref_index_at(index);
451 check_property(valid_symbol_at(name_ref_index),
452 "Invalid constant pool index %u in class file %s",
453 name_ref_index, CHECK_0);
454 check_property(valid_symbol_at(signature_ref_index),
455 "Invalid constant pool index %u in class file %s",
456 signature_ref_index, CHECK_0);
457 break;
458 }
459 case JVM_CONSTANT_Utf8:
460 break;
461 case JVM_CONSTANT_UnresolvedClass: // fall-through
462 case JVM_CONSTANT_UnresolvedClassInError: {
463 ShouldNotReachHere(); // Only JVM_CONSTANT_ClassIndex should be present
464 break;
465 }
466 case JVM_CONSTANT_ClassIndex: {
467 const int class_index = cp->klass_index_at(index);
468 check_property(valid_symbol_at(class_index),
469 "Invalid constant pool index %u in class file %s",
470 class_index, CHECK_0);
471 cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
472 break;
473 }
474 case JVM_CONSTANT_StringIndex: {
475 const int string_index = cp->string_index_at(index);
476 check_property(valid_symbol_at(string_index),
477 "Invalid constant pool index %u in class file %s",
478 string_index, CHECK_0);
479 Symbol* const sym = cp->symbol_at(string_index);
480 cp->unresolved_string_at_put(index, sym);
481 break;
482 }
483 case JVM_CONSTANT_MethodHandle: {
484 const int ref_index = cp->method_handle_index_at(index);
485 check_property(valid_cp_range(ref_index, length),
486 "Invalid constant pool index %u in class file %s",
487 ref_index, CHECK_0);
488 const constantTag tag = cp->tag_at(ref_index);
489 const int ref_kind = cp->method_handle_ref_kind_at(index);
490
491 switch (ref_kind) {
492 case JVM_REF_getField:
493 case JVM_REF_getStatic:
494 case JVM_REF_putField:
495 case JVM_REF_putStatic: {
496 check_property(
497 tag.is_field(),
498 "Invalid constant pool index %u in class file %s (not a field)",
499 ref_index, CHECK_0);
500 break;
501 }
502 case JVM_REF_invokeVirtual:
503 case JVM_REF_newInvokeSpecial: {
504 check_property(
505 tag.is_method(),
506 "Invalid constant pool index %u in class file %s (not a method)",
507 ref_index, CHECK_0);
508 break;
509 }
510 case JVM_REF_invokeStatic:
511 case JVM_REF_invokeSpecial: {
512 check_property(
513 tag.is_method() ||
514 ((_major_version >= JAVA_8_VERSION) && tag.is_interface_method()),
515 "Invalid constant pool index %u in class file %s (not a method)",
516 ref_index, CHECK_0);
517 break;
518 }
519 case JVM_REF_invokeInterface: {
520 check_property(
521 tag.is_interface_method(),
522 "Invalid constant pool index %u in class file %s (not an interface method)",
523 ref_index, CHECK_0);
524 break;
525 }
526 default: {
527 classfile_parse_error(
528 "Bad method handle kind at constant pool index %u in class file %s",
529 index, CHECK_0);
530 }
531 } // switch(refkind)
532 // Keep the ref_index unchanged. It will be indirected at link-time.
533 break;
534 } // case MethodHandle
535 case JVM_CONSTANT_MethodType: {
536 const int ref_index = cp->method_type_index_at(index);
537 check_property(valid_symbol_at(ref_index),
538 "Invalid constant pool index %u in class file %s",
539 ref_index, CHECK_0);
540 break;
541 }
542 case JVM_CONSTANT_InvokeDynamic: {
543 const int name_and_type_ref_index =
544 cp->invoke_dynamic_name_and_type_ref_index_at(index);
545
546 check_property(valid_cp_range(name_and_type_ref_index, length) &&
547 cp->tag_at(name_and_type_ref_index).is_name_and_type(),
548 "Invalid constant pool index %u in class file %s",
549 name_and_type_ref_index, CHECK_0);
550 // bootstrap specifier index must be checked later,
551 // when BootstrapMethods attr is available
552 break;
553 }
554 default: {
555 fatal("bad constant pool tag value %u", cp->tag_at(index).value());
556 ShouldNotReachHere();
557 break;
558 }
559 } // switch(tag)
560 } // end of for
561
562 if (_cp_patches != NULL) {
563 // need to treat this_class specially...
564 int this_class_index;
565 {
566 stream->guarantee_more(8, CHECK_0); // flags, this_class, super_class, infs_len
567 const u1* const mark = stream->current();
568 stream->skip_u2_fast(1); // skip flags
569 this_class_index = stream->get_u2_fast();
570 stream->set_current(mark); // revert to mark
571 }
572
573 for (index = 1; index < length; index++) { // Index 0 is unused
574 if (has_cp_patch_at(index)) {
575 guarantee_property(index != this_class_index,
576 "Illegal constant pool patch to self at %d in class file %s",
577 index, CHECK_0);
578 patch_constant_pool(cp, index, cp_patch_at(index), CHECK_0);
579 }
580 }
581 }
582
583 if (!_need_verify) {
584 return 0;
585 }
586
587 // second verification pass - checks the strings are of the right format.
588 // but not yet to the other entries
589 for (index = 1; index < length; index++) {
590 const jbyte tag = cp->tag_at(index).value();
591 switch (tag) {
592 case JVM_CONSTANT_UnresolvedClass: {
593 const Symbol* const class_name = cp->klass_name_at(index);
594 // check the name, even if _cp_patches will overwrite it
595 verify_legal_class_name(class_name, CHECK_0);
596 break;
597 }
598 case JVM_CONSTANT_NameAndType: {
599 if (_need_verify) {
600 const int sig_index = cp->signature_ref_index_at(index);
601 const int name_index = cp->name_ref_index_at(index);
602 const Symbol* const name = cp->symbol_at(name_index);
603 const Symbol* const sig = cp->symbol_at(sig_index);
604 guarantee_property(sig->utf8_length() != 0,
605 "Illegal zero length constant pool entry at %d in class %s",
606 sig_index, CHECK_0);
607 guarantee_property(name->utf8_length() != 0,
608 "Illegal zero length constant pool entry at %d in class %s",
609 name_index, CHECK_0);
610
611 if (sig->byte_at(0) == JVM_SIGNATURE_FUNC) {
612 // Format check method name and signature
613 verify_legal_method_name(name, CHECK_0);
614 verify_legal_method_signature(name, sig, CHECK_0);
615 } else {
616 // Format check field name and signature
617 verify_legal_field_name(name, CHECK_0);
618 verify_legal_field_signature(name, sig, CHECK_0);
619 }
620 }
621 break;
622 }
623 case JVM_CONSTANT_InvokeDynamic:
624 case JVM_CONSTANT_Fieldref:
625 case JVM_CONSTANT_Methodref:
626 case JVM_CONSTANT_InterfaceMethodref: {
627 const int name_and_type_ref_index =
628 cp->name_and_type_ref_index_at(index);
629 // already verified to be utf8
630 const int name_ref_index =
631 cp->name_ref_index_at(name_and_type_ref_index);
632 // already verified to be utf8
633 const int signature_ref_index =
634 cp->signature_ref_index_at(name_and_type_ref_index);
635 const Symbol* const name = cp->symbol_at(name_ref_index);
636 const Symbol* const signature = cp->symbol_at(signature_ref_index);
637 if (tag == JVM_CONSTANT_Fieldref) {
638 if (_need_verify) {
639 // Field name and signature are verified above, when iterating NameAndType_info.
640 // Need only to be sure signature is non-zero length and the right type.
641 if (signature->utf8_length() == 0 ||
642 signature->byte_at(0) == JVM_SIGNATURE_FUNC) {
643 throwIllegalSignature("Field", name, signature, CHECK_0);
644 }
645 }
646 } else {
647 if (_need_verify) {
648 // Method name and signature are verified above, when iterating NameAndType_info.
649 // Need only to be sure signature is non-zero length and the right type.
650 if (signature->utf8_length() == 0 ||
651 signature->byte_at(0) != JVM_SIGNATURE_FUNC) {
652 throwIllegalSignature("Method", name, signature, CHECK_0);
653 }
654 }
655 // 4509014: If a class method name begins with '<', it must be "<init>"
656 const unsigned int name_len = name->utf8_length();
657 if (tag == JVM_CONSTANT_Methodref &&
658 name_len != 0 &&
659 name->byte_at(0) == '<' &&
660 name != vmSymbols::object_initializer_name()) {
661 classfile_parse_error(
662 "Bad method name at constant pool index %u in class file %s",
663 name_ref_index, CHECK_0);
664 }
665 }
666 break;
667 }
668 case JVM_CONSTANT_MethodHandle: {
669 const int ref_index = cp->method_handle_index_at(index);
670 const int ref_kind = cp->method_handle_ref_kind_at(index);
671 switch (ref_kind) {
672 case JVM_REF_invokeVirtual:
673 case JVM_REF_invokeStatic:
674 case JVM_REF_invokeSpecial:
675 case JVM_REF_newInvokeSpecial: {
676 const int name_and_type_ref_index =
677 cp->name_and_type_ref_index_at(ref_index);
678 const int name_ref_index =
679 cp->name_ref_index_at(name_and_type_ref_index);
680 const Symbol* const name = cp->symbol_at(name_ref_index);
681 if (ref_kind == JVM_REF_newInvokeSpecial) {
682 if (name != vmSymbols::object_initializer_name()) {
683 classfile_parse_error(
684 "Bad constructor name at constant pool index %u in class file %s",
685 name_ref_index, CHECK_0);
686 }
687 } else {
688 if (name == vmSymbols::object_initializer_name()) {
689 classfile_parse_error(
690 "Bad method name at constant pool index %u in class file %s",
691 name_ref_index, CHECK_0);
692 }
693 }
694 break;
695 }
696 // Other ref_kinds are already fully checked in previous pass.
697 } // switch(ref_kind)
698 break;
699 }
700 case JVM_CONSTANT_MethodType: {
701 const Symbol* const no_name = vmSymbols::type_name(); // place holder
702 const Symbol* const signature = cp->method_type_signature_at(index);
703 verify_legal_method_signature(no_name, signature, CHECK_0);
704 break;
705 }
706 case JVM_CONSTANT_Utf8: {
707 assert(cp->symbol_at(index)->refcount() != 0, "count corrupted");
708 }
709 } // switch(tag)
710 } // end of for
711 return 0;
712 }
713
714 void ClassFileParser::patch_constant_pool(ConstantPool* cp,
715 int index,
716 Handle patch,
717 TRAPS) {
718 assert(cp != NULL, "invariant");
719
720 BasicType patch_type = T_VOID;
721
722 switch (cp->tag_at(index).value()) {
723
724 case JVM_CONSTANT_UnresolvedClass: {
725 // Patching a class means pre-resolving it.
726 // The name in the constant pool is ignored.
727 if (java_lang_Class::is_instance(patch())) {
728 guarantee_property(!java_lang_Class::is_primitive(patch()),
729 "Illegal class patch at %d in class file %s",
730 index, CHECK);
731 cp->klass_at_put(index, java_lang_Class::as_Klass(patch()));
3068 inner_class_info_index, CHECK_0);
3069 // Outer class index
3070 const u2 outer_class_info_index = cfs->get_u2_fast();
3071 check_property(
3072 outer_class_info_index == 0 ||
3073 valid_klass_reference_at(outer_class_info_index),
3074 "outer_class_info_index %u has bad constant type in class file %s",
3075 outer_class_info_index, CHECK_0);
3076 // Inner class name
3077 const u2 inner_name_index = cfs->get_u2_fast();
3078 check_property(
3079 inner_name_index == 0 || valid_symbol_at(inner_name_index),
3080 "inner_name_index %u has bad constant type in class file %s",
3081 inner_name_index, CHECK_0);
3082 if (_need_verify) {
3083 guarantee_property(inner_class_info_index != outer_class_info_index,
3084 "Class is both outer and inner class in class file %s", CHECK_0);
3085 }
3086 // Access flags
3087 jint flags;
3088 // JVM_ACC_MODULE is not allowed in JDK-9 and later.
3089 if (_major_version >= JAVA_9_VERSION) {
3090 flags = cfs->get_u2_fast() & (RECOGNIZED_INNER_CLASS_MODIFIERS | JVM_ACC_MODULE);
3091 if ((flags & JVM_ACC_MODULE) != 0) {
3092 ResourceMark rm(THREAD);
3093 Exceptions::fthrow(
3094 THREAD_AND_LOCATION,
3095 vmSymbols::java_lang_NoClassDefFoundError(),
3096 "%s is not a class because access_flag ACC_MODULE is set",
3097 _class_name->as_C_string());
3098 return 0;
3099 }
3100 } else {
3101 flags = cfs->get_u2_fast() & RECOGNIZED_INNER_CLASS_MODIFIERS;
3102 }
3103 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3104 // Set abstract bit for old class files for backward compatibility
3105 flags |= JVM_ACC_ABSTRACT;
3106 }
3107 verify_legal_class_modifiers(flags, CHECK_0);
3108 AccessFlags inner_access_flags(flags);
3109
3110 inner_classes->at_put(index++, inner_class_info_index);
3111 inner_classes->at_put(index++, outer_class_info_index);
3112 inner_classes->at_put(index++, inner_name_index);
3113 inner_classes->at_put(index++, inner_access_flags.as_short());
3114 }
3115
3116 // 4347400: make sure there's no duplicate entry in the classes array
3117 if (_need_verify && _major_version >= JAVA_1_5_VERSION) {
3118 for(int i = 0; i < length * 4; i += 4) {
3119 for(int j = i + 4; j < length * 4; j += 4) {
4554 for (int index = 0; index < num_methods; index++) {
4555 const Method* const m = methods->at(index);
4556 // if m is static and not the init method, throw a verify error
4557 if ((m->is_static()) && (m->name() != vmSymbols::class_initializer_name())) {
4558 ResourceMark rm(THREAD);
4559 Exceptions::fthrow(
4560 THREAD_AND_LOCATION,
4561 vmSymbols::java_lang_VerifyError(),
4562 "Illegal static method %s in interface %s",
4563 m->name()->as_C_string(),
4564 this_klass->external_name()
4565 );
4566 return;
4567 }
4568 }
4569 }
4570
4571 // utility methods for format checking
4572
4573 void ClassFileParser::verify_legal_class_modifiers(jint flags, TRAPS) const {
4574 if (!_need_verify) { return; }
4575
4576 const bool is_interface = (flags & JVM_ACC_INTERFACE) != 0;
4577 const bool is_abstract = (flags & JVM_ACC_ABSTRACT) != 0;
4578 const bool is_final = (flags & JVM_ACC_FINAL) != 0;
4579 const bool is_super = (flags & JVM_ACC_SUPER) != 0;
4580 const bool is_enum = (flags & JVM_ACC_ENUM) != 0;
4581 const bool is_annotation = (flags & JVM_ACC_ANNOTATION) != 0;
4582 const bool is_module = (flags & JVM_ACC_MODULE) != 0;
4583 const bool major_gte_15 = _major_version >= JAVA_1_5_VERSION;
4584
4585 assert(!(_major_version >= JAVA_9_VERSION && is_module),
4586 "ACC_MODULE should not be set in class file whose version >= 53");
4587
4588 if ((is_abstract && is_final) ||
4589 (is_interface && !is_abstract) ||
4590 (is_interface && major_gte_15 && (is_super || is_enum)) ||
4591 (!is_interface && major_gte_15 && is_annotation)) {
4592 ResourceMark rm(THREAD);
4593 Exceptions::fthrow(
4594 THREAD_AND_LOCATION,
4595 vmSymbols::java_lang_ClassFormatError(),
4596 "Illegal class modifiers in class %s: 0x%X",
4597 _class_name->as_C_string(), flags
4598 );
4599 return;
4600 }
4601 }
4602
4603 static bool has_illegal_visibility(jint flags) {
4604 const bool is_public = (flags & JVM_ACC_PUBLIC) != 0;
4605 const bool is_protected = (flags & JVM_ACC_PROTECTED) != 0;
4606 const bool is_private = (flags & JVM_ACC_PRIVATE) != 0;
4607
5758 _major_version,
5759 _minor_version,
5760 JAVA_MAX_SUPPORTED_VERSION,
5761 JAVA_MAX_SUPPORTED_MINOR_VERSION);
5762 return;
5763 }
5764
5765 stream->guarantee_more(3, CHECK); // length, first cp tag
5766 const u2 cp_size = stream->get_u2_fast();
5767
5768 guarantee_property(
5769 cp_size >= 1, "Illegal constant pool size %u in class file %s",
5770 cp_size, CHECK);
5771
5772 _cp = ConstantPool::allocate(_loader_data,
5773 cp_size,
5774 CHECK);
5775
5776 ConstantPool* const cp = _cp;
5777
5778 int cp_result = parse_constant_pool(stream, cp, cp_size, CHECK);
5779
5780 assert(cp_size == (const u2)cp->length(), "invariant");
5781
5782 // ACCESS FLAGS
5783 stream->guarantee_more(8, CHECK); // flags, this_class, super_class, infs_len
5784
5785 // Access flags
5786 jint flags;
5787 // JVM_ACC_MODULE is not allowed in JDK-9 and later.
5788 if (_major_version >= JAVA_9_VERSION) {
5789 flags = stream->get_u2_fast() & (JVM_RECOGNIZED_CLASS_MODIFIERS | JVM_ACC_MODULE);
5790 if ((flags & JVM_ACC_MODULE) != 0) {
5791 ResourceMark rm(THREAD);
5792 Exceptions::fthrow(
5793 THREAD_AND_LOCATION,
5794 vmSymbols::java_lang_NoClassDefFoundError(),
5795 "%s is not a class because access_flag ACC_MODULE is set",
5796 _class_name->as_C_string());
5797 return;
5798 } else if (cp_result != 0) {
5799 // Found a bad constant pool item and ACC_MODULE was not set so go ahead
5800 // and throw a CFE.
5801 classfile_parse_error("Unknown constant tag %u in class file %s", cp_result, CHECK);
5802 return;
5803 }
5804 } else {
5805 flags = stream->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
5806 }
5807
5808 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
5809 // Set abstract bit for old class files for backward compatibility
5810 flags |= JVM_ACC_ABSTRACT;
5811 }
5812
5813 _access_flags.set_flags(flags);
5814
5815 verify_legal_class_modifiers((jint)_access_flags.as_int(), CHECK);
5816
5817 // This class and superclass
5818 _this_class_index = stream->get_u2_fast();
5819 check_property(
5820 valid_cp_range(_this_class_index, cp_size) &&
5821 cp->tag_at(_this_class_index).is_unresolved_klass(),
5822 "Invalid this class index %u in constant pool in class file %s",
5823 _this_class_index, CHECK);
5824
5825 Symbol* const class_name_in_cp = cp->klass_name_at(_this_class_index);
5826 assert(class_name_in_cp != NULL, "class_name can't be null");
5827
5828 // Update _class_name which could be null previously
5829 // to reflect the name in the constant pool
5830 _class_name = class_name_in_cp;
5831
5832 // Don't need to check whether this class name is legal or not.
5833 // It has been checked when constant pool is parsed.
5834 // However, make sure it is not an array type.
5835 if (_need_verify) {
|