src/share/vm/classfile/classFileParser.cpp

Print this page




 295   if (names_count > 0) {
 296     SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK);
 297   }
 298 
 299   // Copy _current pointer of local copy back to stream().
 300 #ifdef ASSERT
 301   assert(cfs0->current() == old_current, "non-exclusive use of stream()");
 302 #endif
 303   cfs0->set_current(cfs1.current());
 304 }
 305 
 306 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
 307 
 308 inline Symbol* check_symbol_at(constantPoolHandle cp, int index) {
 309   if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8())
 310     return cp->symbol_at(index);
 311   else
 312     return NULL;
 313 }
 314 
 315 constantPoolHandle ClassFileParser::parse_constant_pool(TRAPS) {
 316   ClassFileStream* cfs = stream();
 317   constantPoolHandle nullHandle;
 318 
 319   cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag
 320   u2 length = cfs->get_u2_fast();
 321   guarantee_property(
 322     length >= 1, "Illegal constant pool size %u in class file %s",
 323     length, CHECK_(nullHandle));
 324   ConstantPool* constant_pool = ConstantPool::allocate(_loader_data, length,
 325                                                         CHECK_(nullHandle));
 326   _cp = constant_pool; // save in case of errors
 327   constantPoolHandle cp (THREAD, constant_pool);
 328 
 329   // parsing constant pool entries
 330   parse_constant_pool_entries(length, CHECK_(nullHandle));
 331 
 332   int index = 1;  // declared outside of loops for portability
 333 
 334   // first verification pass - validate cross references and fixup class and string constants
 335   for (index = 1; index < length; index++) {          // Index 0 is unused
 336     jbyte tag = cp->tag_at(index).value();
 337     switch (tag) {
 338       case JVM_CONSTANT_Class :
 339         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 340         break;
 341       case JVM_CONSTANT_Fieldref :
 342         // fall through
 343       case JVM_CONSTANT_Methodref :
 344         // fall through


 389       case JVM_CONSTANT_UnresolvedClass :         // fall-through
 390       case JVM_CONSTANT_UnresolvedClassInError:
 391         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 392         break;
 393       case JVM_CONSTANT_ClassIndex :
 394         {
 395           int class_index = cp->klass_index_at(index);
 396           check_property(valid_symbol_at(class_index),
 397                  "Invalid constant pool index %u in class file %s",
 398                  class_index, CHECK_(nullHandle));
 399           cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
 400         }
 401         break;
 402       case JVM_CONSTANT_StringIndex :
 403         {
 404           int string_index = cp->string_index_at(index);
 405           check_property(valid_symbol_at(string_index),
 406                  "Invalid constant pool index %u in class file %s",
 407                  string_index, CHECK_(nullHandle));
 408           Symbol* sym = cp->symbol_at(string_index);










 409           cp->unresolved_string_at_put(index, sym);
 410         }
 411         break;
 412       case JVM_CONSTANT_MethodHandle :
 413         {
 414           int ref_index = cp->method_handle_index_at(index);
 415           check_property(
 416             valid_cp_range(ref_index, length),
 417               "Invalid constant pool index %u in class file %s",
 418               ref_index, CHECK_(nullHandle));
 419           constantTag tag = cp->tag_at(ref_index);
 420           int ref_kind  = cp->method_handle_ref_kind_at(index);
 421           switch (ref_kind) {
 422           case JVM_REF_getField:
 423           case JVM_REF_getStatic:
 424           case JVM_REF_putField:
 425           case JVM_REF_putStatic:
 426             check_property(
 427               tag.is_field(),
 428               "Invalid constant pool index %u in class file %s (not a field)",


3866         "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
3867         "this version of the Java Runtime only recognizes class file versions up to %u.%u",
3868         name->as_C_string(),
3869         major_version,
3870         minor_version,
3871         JAVA_MAX_SUPPORTED_VERSION,
3872         JAVA_MAX_SUPPORTED_MINOR_VERSION);
3873     }
3874     return nullHandle;
3875   }
3876 
3877   _major_version = major_version;
3878   _minor_version = minor_version;
3879 
3880 
3881   // Check if verification needs to be relaxed for this class file
3882   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
3883   _relax_verify = Verifier::relax_verify_for(class_loader());
3884 
3885   // Constant pool
3886   constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
3887 
3888   int cp_size = cp->length();
3889 
3890   cfs->guarantee_more(8, CHECK_(nullHandle));  // flags, this_class, super_class, infs_len
3891 
3892   // Access flags
3893   AccessFlags access_flags;
3894   jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
3895 
3896   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3897     // Set abstract bit for old class files for backward compatibility
3898     flags |= JVM_ACC_ABSTRACT;
3899   }
3900   verify_legal_class_modifiers(flags, CHECK_(nullHandle));
3901   access_flags.set_flags(flags);
3902 
3903   // This class and superclass
3904   u2 this_class_index = cfs->get_u2_fast();
3905   check_property(
3906     valid_cp_range(this_class_index, cp_size) &&




 295   if (names_count > 0) {
 296     SymbolTable::new_symbols(_loader_data, _cp, names_count, names, lengths, indices, hashValues, CHECK);
 297   }
 298 
 299   // Copy _current pointer of local copy back to stream().
 300 #ifdef ASSERT
 301   assert(cfs0->current() == old_current, "non-exclusive use of stream()");
 302 #endif
 303   cfs0->set_current(cfs1.current());
 304 }
 305 
 306 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
 307 
 308 inline Symbol* check_symbol_at(constantPoolHandle cp, int index) {
 309   if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8())
 310     return cp->symbol_at(index);
 311   else
 312     return NULL;
 313 }
 314 
 315 constantPoolHandle ClassFileParser::parse_constant_pool(bool patched, TRAPS) {
 316   ClassFileStream* cfs = stream();
 317   constantPoolHandle nullHandle;
 318 
 319   cfs->guarantee_more(3, CHECK_(nullHandle)); // length, first cp tag
 320   u2 length = cfs->get_u2_fast();
 321   guarantee_property(
 322     length >= 1, "Illegal constant pool size %u in class file %s",
 323     length, CHECK_(nullHandle));
 324   ConstantPool* constant_pool = ConstantPool::allocate(_loader_data, length, patched,
 325                                                         CHECK_(nullHandle));
 326   _cp = constant_pool; // save in case of errors
 327   constantPoolHandle cp (THREAD, constant_pool);
 328 
 329   // parsing constant pool entries
 330   parse_constant_pool_entries(length, CHECK_(nullHandle));
 331 
 332   int index = 1;  // declared outside of loops for portability
 333 
 334   // first verification pass - validate cross references and fixup class and string constants
 335   for (index = 1; index < length; index++) {          // Index 0 is unused
 336     jbyte tag = cp->tag_at(index).value();
 337     switch (tag) {
 338       case JVM_CONSTANT_Class :
 339         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 340         break;
 341       case JVM_CONSTANT_Fieldref :
 342         // fall through
 343       case JVM_CONSTANT_Methodref :
 344         // fall through


 389       case JVM_CONSTANT_UnresolvedClass :         // fall-through
 390       case JVM_CONSTANT_UnresolvedClassInError:
 391         ShouldNotReachHere();     // Only JVM_CONSTANT_ClassIndex should be present
 392         break;
 393       case JVM_CONSTANT_ClassIndex :
 394         {
 395           int class_index = cp->klass_index_at(index);
 396           check_property(valid_symbol_at(class_index),
 397                  "Invalid constant pool index %u in class file %s",
 398                  class_index, CHECK_(nullHandle));
 399           cp->unresolved_klass_at_put(index, cp->symbol_at(class_index));
 400         }
 401         break;
 402       case JVM_CONSTANT_StringIndex :
 403         {
 404           int string_index = cp->string_index_at(index);
 405           check_property(valid_symbol_at(string_index),
 406                  "Invalid constant pool index %u in class file %s",
 407                  string_index, CHECK_(nullHandle));
 408           Symbol* sym = cp->symbol_at(string_index);
 409           if (sym != NULL) {
 410             const char* CPH = "CONSTANT_PLACEHOLDER_";
 411             int len = strlen(CPH);
 412             char* utf8 = sym->as_utf8();
 413             // Check if it is pseudo-string
 414             if(strlen(utf8) > len && strncmp(utf8, CPH, len) == 0) {
 415               // Save the Utf8 index for JvmtiConstantPoolReconstituter
 416               cp->map_pseudo_string_indices((u2)index, (u2)string_index);
 417             }
 418           }
 419           cp->unresolved_string_at_put(index, sym);
 420         }
 421         break;
 422       case JVM_CONSTANT_MethodHandle :
 423         {
 424           int ref_index = cp->method_handle_index_at(index);
 425           check_property(
 426             valid_cp_range(ref_index, length),
 427               "Invalid constant pool index %u in class file %s",
 428               ref_index, CHECK_(nullHandle));
 429           constantTag tag = cp->tag_at(ref_index);
 430           int ref_kind  = cp->method_handle_ref_kind_at(index);
 431           switch (ref_kind) {
 432           case JVM_REF_getField:
 433           case JVM_REF_getStatic:
 434           case JVM_REF_putField:
 435           case JVM_REF_putStatic:
 436             check_property(
 437               tag.is_field(),
 438               "Invalid constant pool index %u in class file %s (not a field)",


3876         "%s has been compiled by a more recent version of the Java Runtime (class file version %u.%u), "
3877         "this version of the Java Runtime only recognizes class file versions up to %u.%u",
3878         name->as_C_string(),
3879         major_version,
3880         minor_version,
3881         JAVA_MAX_SUPPORTED_VERSION,
3882         JAVA_MAX_SUPPORTED_MINOR_VERSION);
3883     }
3884     return nullHandle;
3885   }
3886 
3887   _major_version = major_version;
3888   _minor_version = minor_version;
3889 
3890 
3891   // Check if verification needs to be relaxed for this class file
3892   // Do not restrict it to jdk1.0 or jdk1.1 to maintain backward compatibility (4982376)
3893   _relax_verify = Verifier::relax_verify_for(class_loader());
3894 
3895   // Constant pool
3896   constantPoolHandle cp = parse_constant_pool(cp_patches != NULL, CHECK_(nullHandle));
3897 
3898   int cp_size = cp->length();
3899 
3900   cfs->guarantee_more(8, CHECK_(nullHandle));  // flags, this_class, super_class, infs_len
3901 
3902   // Access flags
3903   AccessFlags access_flags;
3904   jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
3905 
3906   if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3907     // Set abstract bit for old class files for backward compatibility
3908     flags |= JVM_ACC_ABSTRACT;
3909   }
3910   verify_legal_class_modifiers(flags, CHECK_(nullHandle));
3911   access_flags.set_flags(flags);
3912 
3913   // This class and superclass
3914   u2 this_class_index = cfs->get_u2_fast();
3915   check_property(
3916     valid_cp_range(this_class_index, cp_size) &&