1 /*
   2  * Copyright (c) 1997, 2017, 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  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  27 
  28 #include "memory/referenceType.hpp"
  29 #include "runtime/handles.inline.hpp"
  30 #include "oops/constantPool.hpp"
  31 #include "oops/typeArrayOop.hpp"
  32 #include "utilities/accessFlags.hpp"
  33 
  34 class Annotations;
  35 template <typename T>
  36 class Array;
  37 class ClassFileStream;
  38 class ClassLoaderData;
  39 class CompressedLineNumberWriteStream;
  40 class ConstMethod;
  41 class FieldInfo;
  42 template <typename T>
  43 class GrowableArray;
  44 class InstanceKlass;
  45 class Symbol;
  46 class TempNewSymbol;
  47 
  48 // Parser for for .class files
  49 //
  50 // The bytes describing the class file structure is read from a Stream object
  51 
  52 class ClassFileParser VALUE_OBJ_CLASS_SPEC {
  53 
  54  class ClassAnnotationCollector;
  55  class FieldAllocationCount;
  56  class FieldAnnotationCollector;
  57  class FieldLayoutInfo;
  58  class OopMapBlocksBuilder;
  59 
  60  public:
  61   // The ClassFileParser has an associated "publicity" level
  62   // It is used to control which subsystems (if any)
  63   // will observe the parsing (logging, events, tracing).
  64   // Default level is "BROADCAST", which is equivalent to
  65   // a "public" parsing attempt.
  66   //
  67   // "INTERNAL" level should be entirely private to the
  68   // caller - this allows for internal reuse of ClassFileParser
  69   //
  70   enum Publicity {
  71     INTERNAL,
  72     BROADCAST,
  73     NOF_PUBLICITY_LEVELS
  74   };
  75 
  76   enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
  77 
  78  private:
  79   const ClassFileStream* _stream; // Actual input stream
  80   const Symbol* _requested_name;
  81   Symbol* _class_name;
  82   mutable ClassLoaderData* _loader_data;
  83   const InstanceKlass* _host_klass;
  84   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  85   int _num_patched_klasses;
  86   int _max_num_patched_klasses;
  87   int _orig_cp_size;
  88   int _first_patched_klass_resolved_index;
  89 
  90   // Metadata created before the instance klass is created.  Must be deallocated
  91   // if not transferred to the InstanceKlass upon successful class loading
  92   // in which case these pointers have been set to NULL.
  93   const InstanceKlass* _super_klass;
  94   ConstantPool* _cp;
  95   Array<u2>* _fields;
  96   Array<Method*>* _methods;
  97   Array<u2>* _inner_classes;
  98   Array<Klass*>* _local_interfaces;
  99   Array<Klass*>* _transitive_interfaces;
 100   Annotations* _combined_annotations;
 101   AnnotationArray* _annotations;
 102   AnnotationArray* _type_annotations;
 103   Array<AnnotationArray*>* _fields_annotations;
 104   Array<AnnotationArray*>* _fields_type_annotations;
 105   InstanceKlass* _klass;  // InstanceKlass* once created.
 106   InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
 107 
 108   ClassAnnotationCollector* _parsed_annotations;
 109   FieldAllocationCount* _fac;
 110   FieldLayoutInfo* _field_info;
 111   const intArray* _method_ordering;
 112   GrowableArray<Method*>* _all_mirandas;
 113 
 114   enum { fixed_buffer_size = 128 };
 115   u_char _linenumbertable_buffer[fixed_buffer_size];
 116 
 117   // Size of Java vtable (in words)
 118   int _vtable_size;
 119   int _itable_size;
 120 
 121   int _num_miranda_methods;
 122 
 123   ReferenceType _rt;
 124   Handle _protection_domain;
 125   AccessFlags _access_flags;
 126 
 127   // for tracing and notifications
 128   Publicity _pub_level;
 129 
 130   // Used to keep track of whether a constant pool item 19 or 20 is found.  These
 131   // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed
 132   // in regular class files.  For class file version >= 53, a CFE cannot be thrown
 133   // immediately when these are seen because a NCDFE must be thrown if the class's
 134   // access_flags have ACC_MODULE set.  But, the access_flags haven't been looked
 135   // at yet.  So, the bad constant pool item is cached here.  A value of zero
 136   // means that no constant pool item 19 or 20 was found.
 137   short _bad_constant_seen;
 138 
 139   // class attributes parsed before the instance klass is created:
 140   bool _synthetic_flag;
 141   int _sde_length;
 142   const char* _sde_buffer;
 143   u2 _sourcefile_index;
 144   u2 _generic_signature_index;
 145 
 146   u2 _major_version;
 147   u2 _minor_version;
 148   u2 _this_class_index;
 149   u2 _super_class_index;
 150   u2 _itfs_len;
 151   u2 _java_fields_count;
 152 
 153   bool _need_verify;
 154   bool _relax_verify;
 155 
 156   bool _has_nonstatic_concrete_methods;
 157   bool _declares_nonstatic_concrete_methods;
 158   bool _has_final_method;
 159   bool _has_value_fields;
 160 
 161   // precomputed flags
 162   bool _has_finalizer;
 163   bool _has_empty_finalizer;
 164   bool _has_vanilla_constructor;
 165   int _max_bootstrap_specifier_index;  // detects BSS values
 166 
 167   void parse_stream(const ClassFileStream* const stream, TRAPS);
 168 
 169   void post_process_parsed_stream(const ClassFileStream* const stream,
 170                                   ConstantPool* cp,
 171                                   TRAPS);
 172 
 173   void prepend_host_package_name(const InstanceKlass* host_klass, TRAPS);
 174   void fix_anonymous_class_name(TRAPS);
 175 
 176   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS);
 177   void set_klass(InstanceKlass* instance);
 178 
 179   void set_class_bad_constant_seen(short bad_constant);
 180   short class_bad_constant_seen() { return  _bad_constant_seen; }
 181   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 182   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 183   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 184   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 185 
 186   void create_combined_annotations(TRAPS);
 187   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 188   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 189   void clear_class_metadata();
 190 
 191   // Constant pool parsing
 192   void parse_constant_pool_entries(const ClassFileStream* const stream,
 193                                    ConstantPool* cp,
 194                                    const int length,
 195                                    TRAPS);
 196 
 197   void parse_constant_pool(const ClassFileStream* const cfs,
 198                            ConstantPool* const cp,
 199                            const int length,
 200                            TRAPS);
 201 
 202   // Interface parsing
 203   void parse_interfaces(const ClassFileStream* const stream,
 204                         const int itfs_len,
 205                         ConstantPool* const cp,
 206                         bool* has_nonstatic_concrete_methods,
 207                         TRAPS);
 208 
 209   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 210                                          const int super_class_index,
 211                                          const bool need_verify,
 212                                          TRAPS);
 213 
 214   // Field parsing
 215   void parse_field_attributes(const ClassFileStream* const cfs,
 216                               u2 attributes_count,
 217                               bool is_static,
 218                               u2 signature_index,
 219                               u2* const constantvalue_index_addr,
 220                               bool* const is_synthetic_addr,
 221                               u2* const generic_signature_index_addr,
 222                               FieldAnnotationCollector* parsed_annotations,
 223                               TRAPS);
 224 
 225   void parse_fields(const ClassFileStream* const cfs,
 226                     bool is_interface,
 227                     FieldAllocationCount* const fac,
 228                     ConstantPool* cp,
 229                     const int cp_size,
 230                     u2* const java_fields_count_ptr,
 231                     TRAPS);
 232 
 233   // Method parsing
 234   Method* parse_method(const ClassFileStream* const cfs,
 235                        bool is_interface,
 236                        const ConstantPool* cp,
 237                        AccessFlags* const promoted_flags,
 238                        TRAPS);
 239 
 240   void parse_methods(const ClassFileStream* const cfs,
 241                      bool is_interface,
 242                      AccessFlags* const promoted_flags,
 243                      bool* const has_final_method,
 244                      bool* const declares_nonstatic_concrete_methods,
 245                      TRAPS);
 246 
 247   const u2* parse_exception_table(const ClassFileStream* const stream,
 248                                   u4 code_length,
 249                                   u4 exception_table_length,
 250                                   TRAPS);
 251 
 252   void parse_linenumber_table(u4 code_attribute_length,
 253                               u4 code_length,
 254                               CompressedLineNumberWriteStream**const write_stream,
 255                               TRAPS);
 256 
 257   const u2* parse_localvariable_table(const ClassFileStream* const cfs,
 258                                       u4 code_length,
 259                                       u2 max_locals,
 260                                       u4 code_attribute_length,
 261                                       u2* const localvariable_table_length,
 262                                       bool isLVTT,
 263                                       TRAPS);
 264 
 265   const u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 266                                      u2* const checked_exceptions_length,
 267                                      u4 method_attribute_length,
 268                                      TRAPS);
 269 
 270   void parse_type_array(u2 array_length,
 271                         u4 code_length,
 272                         u4* const u1_index,
 273                         u4* const u2_index,
 274                         u1* const u1_array,
 275                         u2* const u2_array,
 276                         TRAPS);
 277 
 278   // Classfile attribute parsing
 279   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 280   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 281   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 282                                                         int length,
 283                                                         TRAPS);
 284 
 285   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 286                                                const u1* const inner_classes_attribute_start,
 287                                                bool parsed_enclosingmethod_attribute,
 288                                                u2 enclosing_method_class_index,
 289                                                u2 enclosing_method_method_index,
 290                                                TRAPS);
 291 
 292   void parse_classfile_attributes(const ClassFileStream* const cfs,
 293                                   ConstantPool* cp,
 294                                   ClassAnnotationCollector* parsed_annotations,
 295                                   TRAPS);
 296 
 297   void parse_classfile_synthetic_attribute(TRAPS);
 298   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 299   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 300                                                    ConstantPool* cp,
 301                                                    u4 attribute_length,
 302                                                    TRAPS);
 303 
 304   // Annotations handling
 305   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 306                                         int runtime_visible_annotations_length,
 307                                         const u1* const runtime_invisible_annotations,
 308                                         int runtime_invisible_annotations_length,
 309                                         TRAPS);
 310 
 311   void set_precomputed_flags(InstanceKlass* k);
 312 
 313   // Format checker methods
 314   void classfile_parse_error(const char* msg, TRAPS) const;
 315   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 316   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 317   void classfile_parse_error(const char* msg,
 318                              int index,
 319                              const char *name,
 320                              TRAPS) const;
 321   void classfile_parse_error(const char* msg,
 322                              const char* name,
 323                              const char* signature,
 324                              TRAPS) const;
 325 
 326   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 327     if (!b) { classfile_parse_error(msg, CHECK); }
 328   }
 329 
 330   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 331   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 332 
 333   inline void assert_property(bool b, const char* msg, TRAPS) const {
 334 #ifdef ASSERT
 335     if (!b) {
 336       report_assert_property_failure(msg, THREAD);
 337     }
 338 #endif
 339   }
 340 
 341   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 342 #ifdef ASSERT
 343     if (!b) {
 344       report_assert_property_failure(msg, index, THREAD);
 345     }
 346 #endif
 347   }
 348 
 349   inline void check_property(bool property,
 350                              const char* msg,
 351                              int index,
 352                              TRAPS) const {
 353     if (_need_verify) {
 354       guarantee_property(property, msg, index, CHECK);
 355     } else {
 356       assert_property(property, msg, index, CHECK);
 357     }
 358   }
 359 
 360   inline void check_property(bool property, const char* msg, TRAPS) const {
 361     if (_need_verify) {
 362       guarantee_property(property, msg, CHECK);
 363     } else {
 364       assert_property(property, msg, CHECK);
 365     }
 366   }
 367 
 368   inline void guarantee_property(bool b,
 369                                  const char* msg,
 370                                  int index,
 371                                  TRAPS) const {
 372     if (!b) { classfile_parse_error(msg, index, CHECK); }
 373   }
 374 
 375   inline void guarantee_property(bool b,
 376                                  const char* msg,
 377                                  const char *name,
 378                                  TRAPS) const {
 379     if (!b) { classfile_parse_error(msg, name, CHECK); }
 380   }
 381 
 382   inline void guarantee_property(bool b,
 383                                  const char* msg,
 384                                  int index,
 385                                  const char *name,
 386                                  TRAPS) const {
 387     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 388   }
 389 
 390   void throwIllegalSignature(const char* type,
 391                              const Symbol* name,
 392                              const Symbol* sig,
 393                              TRAPS) const;
 394 
 395   void throwValueTypeLimitation(THREAD_AND_LOCATION_DECL,
 396                                 const char* msg,
 397                                 const Symbol* name = NULL,
 398                                 const Symbol* sig  = NULL) const;
 399 
 400   void verify_constantvalue(const ConstantPool* const cp,
 401                             int constantvalue_index,
 402                             int signature_index,
 403                             TRAPS) const;
 404 
 405   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 406   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 407   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 408   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 409 
 410   void verify_legal_field_signature(const Symbol* fieldname,
 411                                     const Symbol* signature,
 412                                     TRAPS) const;
 413   int  verify_legal_method_signature(const Symbol* methodname,
 414                                      const Symbol* signature,
 415                                      TRAPS) const;
 416 
 417   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 418   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 419   void verify_legal_method_modifiers(jint flags,
 420                                      bool is_interface,
 421                                      const Symbol* name,
 422                                      TRAPS) const;
 423 
 424   const char* skip_over_field_signature(const char* signature,
 425                                         bool void_ok,
 426                                         unsigned int length,
 427                                         TRAPS) const;
 428 
 429   bool has_cp_patch_at(int index) const {
 430     assert(index >= 0, "oob");
 431     return (_cp_patches != NULL
 432             && index < _cp_patches->length()
 433             && _cp_patches->adr_at(index)->not_null());
 434   }
 435 
 436   Handle cp_patch_at(int index) const {
 437     assert(has_cp_patch_at(index), "oob");
 438     return _cp_patches->at(index);
 439   }
 440 
 441   Handle clear_cp_patch_at(int index) {
 442     Handle patch = cp_patch_at(index);
 443     _cp_patches->at_put(index, Handle());
 444     assert(!has_cp_patch_at(index), "");
 445     return patch;
 446   }
 447 
 448   void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name);
 449   void patch_constant_pool(ConstantPool* cp,
 450                            int index,
 451                            Handle patch,
 452                            TRAPS);
 453 
 454   // Wrapper for constantTag.is_klass_[or_]reference.
 455   // In older versions of the VM, Klass*s cannot sneak into early phases of
 456   // constant pool construction, but in later versions they can.
 457   // %%% Let's phase out the old is_klass_reference.
 458   bool valid_klass_reference_at(int index) const {
 459     return _cp->is_within_bounds(index) &&
 460              _cp->tag_at(index).is_klass_or_reference();
 461   }
 462 
 463   // Checks that the cpool index is in range and is a utf8
 464   bool valid_symbol_at(int cpool_index) const {
 465     return _cp->is_within_bounds(cpool_index) &&
 466              _cp->tag_at(cpool_index).is_utf8();
 467   }
 468 
 469   void copy_localvariable_table(const ConstMethod* cm,
 470                                 int lvt_cnt,
 471                                 u2* const localvariable_table_length,
 472                                 const u2**const localvariable_table_start,
 473                                 int lvtt_cnt,
 474                                 u2* const localvariable_type_table_length,
 475                                 const u2** const localvariable_type_table_start,
 476                                 TRAPS);
 477 
 478   void copy_method_annotations(ConstMethod* cm,
 479                                const u1* runtime_visible_annotations,
 480                                int runtime_visible_annotations_length,
 481                                const u1* runtime_invisible_annotations,
 482                                int runtime_invisible_annotations_length,
 483                                const u1* runtime_visible_parameter_annotations,
 484                                int runtime_visible_parameter_annotations_length,
 485                                const u1* runtime_invisible_parameter_annotations,
 486                                int runtime_invisible_parameter_annotations_length,
 487                                const u1* runtime_visible_type_annotations,
 488                                int runtime_visible_type_annotations_length,
 489                                const u1* runtime_invisible_type_annotations,
 490                                int runtime_invisible_type_annotations_length,
 491                                const u1* annotation_default,
 492                                int annotation_default_length,
 493                                TRAPS);
 494 
 495   // lays out fields in class and returns the total oopmap count
 496   void layout_fields(ConstantPool* cp,
 497                      FieldAllocationCount* fac,
 498                      const ClassAnnotationCollector* parsed_annotations,
 499                      FieldLayoutInfo* info,
 500                      TRAPS);
 501 
 502  public:
 503   ClassFileParser(ClassFileStream* stream,
 504                   Symbol* name,
 505                   ClassLoaderData* loader_data,
 506                   Handle protection_domain,
 507                   const InstanceKlass* host_klass,
 508                   GrowableArray<Handle>* cp_patches,
 509                   Publicity pub_level,
 510                   TRAPS);
 511 
 512   ~ClassFileParser();
 513 
 514   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS);
 515 
 516   const ClassFileStream* clone_stream() const;
 517 
 518   void set_klass_to_deallocate(InstanceKlass* klass);
 519 
 520   int static_field_size() const;
 521   int total_oop_map_count() const;
 522   jint layout_size() const;
 523 
 524   int vtable_size() const { return _vtable_size; }
 525   int itable_size() const { return _itable_size; }
 526 
 527   u2 this_class_index() const { return _this_class_index; }
 528   u2 super_class_index() const { return _super_class_index; }
 529 
 530   bool is_anonymous() const { return _host_klass != NULL; }
 531   bool is_interface() const { return _access_flags.is_interface(); }
 532   bool is_value_type() const { return _access_flags.is_value_type(); }
 533   bool is_derive_value_type() const;
 534   bool has_value_fields() const { return _has_value_fields; }
 535 
 536   u2 java_fields_count() const { return _java_fields_count; }
 537 
 538   const InstanceKlass* host_klass() const { return _host_klass; }
 539   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 540   ClassLoaderData* loader_data() const { return _loader_data; }
 541   const Symbol* class_name() const { return _class_name; }
 542   const Klass* super_klass() const { return _super_klass; }
 543 
 544   ReferenceType reference_type() const { return _rt; }
 545   AccessFlags access_flags() const { return _access_flags; }
 546 
 547   bool is_internal() const { return INTERNAL == _pub_level; }
 548 
 549   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 550 
 551 #ifdef ASSERT
 552   static bool is_internal_format(Symbol* class_name);
 553 #endif
 554 
 555 };
 556 
 557 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP