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