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