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   Array<InlineKlass*>* _inline_type_field_klasses;
149   const intArray* _method_ordering;
150   GrowableArray<Method*>* _all_mirandas;
151 
152   enum { fixed_buffer_size = 128 };
153   u_char _linenumbertable_buffer[fixed_buffer_size];
154 
155   // Size of Java vtable (in words)
156   int _vtable_size;
157   int _itable_size;
158 
159   int _num_miranda_methods;
160 
161   int _alignment;
162   int _first_field_offset;
163   int _exact_size_in_bytes;
164 
165   ReferenceType _rt;
166   Handle _protection_domain;
167   AccessFlags _access_flags;
168 
169   // for tracing and notifications
170   Publicity _pub_level;
171 
172   // Used to keep track of whether a constant pool item 19 or 20 is found.  These
173   // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed
174   // in regular class files.  For class file version >= 53, a CFE cannot be thrown
175   // immediately when these are seen because a NCDFE must be thrown if the class's
176   // access_flags have ACC_MODULE set.  But, the access_flags haven't been looked
177   // at yet.  So, the bad constant pool item is cached here.  A value of zero
178   // means that no constant pool item 19 or 20 was found.
179   short _bad_constant_seen;
180 
181   // class attributes parsed before the instance klass is created:
182   bool _synthetic_flag;
183   int _sde_length;
184   const char* _sde_buffer;
185   u2 _sourcefile_index;
186   u2 _generic_signature_index;
187 
188   u2 _major_version;
189   u2 _minor_version;
190   u2 _this_class_index;
191   u2 _super_class_index;
192   u2 _itfs_len;
193   u2 _java_fields_count;
194 
195   bool _need_verify;
196   bool _relax_verify;
197 
198   bool _has_nonstatic_concrete_methods;
199   bool _declares_nonstatic_concrete_methods;
200   bool _has_final_method;
201   bool _has_contended_fields;
202 
203   bool _has_inline_type_fields;
204   bool _has_nonstatic_fields;
205   bool _is_empty_inline_type;
206   bool _is_naturally_atomic;
207   bool _is_declared_atomic;
208   bool _invalid_inline_super;   // if true, invalid super type for an inline type.
209   bool _invalid_identity_super; // if true, invalid super type for an identity type.
210   bool _implements_identityObject;
211   bool _has_injected_identityObject;
212   bool _implements_primitiveObject;
213   bool _has_injected_primitiveObject;
214 
215   // precomputed flags
216   bool _has_finalizer;
217   bool _has_empty_finalizer;
218   bool _has_vanilla_constructor;
219   int _max_bootstrap_specifier_index;  // detects BSS values
220 
221   void parse_stream(const ClassFileStream* const stream, TRAPS);
222 
223   void mangle_hidden_class_name(InstanceKlass* const ik);
224 
225   void post_process_parsed_stream(const ClassFileStream* const stream,
226                                   ConstantPool* cp,
227                                   TRAPS);
228 
229   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH,
230                            const ClassInstanceInfo& cl_inst_info, TRAPS);
231 
232   void set_klass(InstanceKlass* instance);
233 
234   void set_class_bad_constant_seen(short bad_constant);
235   short class_bad_constant_seen() { return  _bad_constant_seen; }
236   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
237   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
238   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
239   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
240 
241   void create_combined_annotations(TRAPS);
242   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
243   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count);
244   void clear_class_metadata();
245 
246   // Constant pool parsing
247   void parse_constant_pool_entries(const ClassFileStream* const stream,
248                                    ConstantPool* cp,
249                                    const int length,
250                                    TRAPS);
251 
252   void parse_constant_pool(const ClassFileStream* const cfs,
253                            ConstantPool* const cp,
254                            const int length,
255                            TRAPS);
256 
257   // Interface parsing
258   void parse_interfaces(const ClassFileStream* const stream,
259                         const int itfs_len,
260                         ConstantPool* const cp,
261                         bool* has_nonstatic_concrete_methods,
262                         bool* is_declared_atomic,
263                         TRAPS);
264 
265   const InstanceKlass* parse_super_class(ConstantPool* const cp,
266                                          const int super_class_index,
267                                          const bool need_verify,
268                                          TRAPS);
269 
270   // Field parsing
271   void parse_field_attributes(const ClassFileStream* const cfs,
272                               u2 attributes_count,
273                               bool is_static,
274                               u2 signature_index,
275                               u2* const constantvalue_index_addr,
276                               bool* const is_synthetic_addr,
277                               u2* const generic_signature_index_addr,
278                               FieldAnnotationCollector* parsed_annotations,
279                               TRAPS);
280 
281   void parse_fields(const ClassFileStream* const cfs,
282                     bool is_interface,
283                     bool is_inline_type,
284                     FieldAllocationCount* const fac,
285                     ConstantPool* cp,
286                     const int cp_size,
287                     u2* const java_fields_count_ptr,
288                     TRAPS);
289 
290   // Method parsing
291   Method* parse_method(const ClassFileStream* const cfs,
292                        bool is_interface,
293                        bool is_inline_type,
294                        const ConstantPool* cp,
295                        AccessFlags* const promoted_flags,
296                        TRAPS);
297 
298   void parse_methods(const ClassFileStream* const cfs,
299                      bool is_interface,
300                      bool is_inline_type,
301                      AccessFlags* const promoted_flags,
302                      bool* const has_final_method,
303                      bool* const declares_nonstatic_concrete_methods,
304                      TRAPS);
305 
306   const unsafe_u2* parse_exception_table(const ClassFileStream* const stream,
307                                          u4 code_length,
308                                          u4 exception_table_length,
309                                          TRAPS);
310 
311   void parse_linenumber_table(u4 code_attribute_length,
312                               u4 code_length,
313                               CompressedLineNumberWriteStream**const write_stream,
314                               TRAPS);
315 
316   const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs,
317                                              u4 code_length,
318                                              u2 max_locals,
319                                              u4 code_attribute_length,
320                                              u2* const localvariable_table_length,
321                                              bool isLVTT,
322                                              TRAPS);
323 
324   const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs,
325                                             u2* const checked_exceptions_length,
326                                             u4 method_attribute_length,
327                                             TRAPS);
328 
329   // Classfile attribute parsing
330   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
331   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
332   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
333                                                         int length,
334                                                         TRAPS);
335 
336   // Check for circularity in InnerClasses attribute.
337   bool check_inner_classes_circularity(const ConstantPool* cp, int length, TRAPS);
338 
339   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
340                                                const ConstantPool* cp,
341                                                const u1* const inner_classes_attribute_start,
342                                                bool parsed_enclosingmethod_attribute,
343                                                u2 enclosing_method_class_index,
344                                                u2 enclosing_method_method_index,
345                                                TRAPS);
346 
347   u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
348                                             const u1* const nest_members_attribute_start,
349                                             TRAPS);
350 
351   u2 parse_classfile_permitted_subclasses_attribute(const ClassFileStream* const cfs,
352                                                     const u1* const permitted_subclasses_attribute_start,
353                                                     TRAPS);
354 
355   u2 parse_classfile_record_attribute(const ClassFileStream* const cfs,
356                                       const ConstantPool* cp,
357                                       const u1* const record_attribute_start,
358                                       TRAPS);
359 
360   void parse_classfile_attributes(const ClassFileStream* const cfs,
361                                   ConstantPool* cp,
362                                   ClassAnnotationCollector* parsed_annotations,
363                                   TRAPS);
364 
365   void parse_classfile_synthetic_attribute();
366   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
367   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
368                                                    ConstantPool* cp,
369                                                    u4 attribute_length,
370                                                    TRAPS);
371 
372   // Annotations handling
373   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
374                                         int runtime_visible_annotations_length,
375                                         const u1* const runtime_invisible_annotations,
376                                         int runtime_invisible_annotations_length,
377                                         TRAPS);
378 
379   void set_precomputed_flags(InstanceKlass* k);
380 
381   // Format checker methods
382   void classfile_parse_error(const char* msg, TRAPS) const;
383   void classfile_parse_error(const char* msg, int index, TRAPS) const;
384   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
385   void classfile_parse_error(const char* msg,
386                              int index,
387                              const char *name,
388                              TRAPS) const;
389   void classfile_parse_error(const char* msg,
390                              const char* name,
391                              const char* signature,
392                              TRAPS) const;
393 
394   void classfile_icce_error(const char* msg,
395                             const Klass* k,
396                             TRAPS) const;
397 
398   void classfile_ucve_error(const char* msg,
399                             const Symbol* class_name,
400                             u2 major,
401                             u2 minor,
402                             TRAPS) const;
403 
404   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
405     if (!b) { classfile_parse_error(msg, THREAD); return; }
406   }
407 
408   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
409   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
410 
411   inline void assert_property(bool b, const char* msg, TRAPS) const {
412 #ifdef ASSERT
413     if (!b) {
414       report_assert_property_failure(msg, THREAD);
415     }
416 #endif
417   }
418 
419   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
420 #ifdef ASSERT
421     if (!b) {
422       report_assert_property_failure(msg, index, THREAD);
423     }
424 #endif
425   }
426 
427   inline void check_property(bool property,
428                              const char* msg,
429                              int index,
430                              TRAPS) const {
431     if (_need_verify) {
432       guarantee_property(property, msg, index, CHECK);
433     } else {
434       assert_property(property, msg, index, CHECK);
435     }
436   }
437 
438   inline void check_property(bool property, const char* msg, TRAPS) const {
439     if (_need_verify) {
440       guarantee_property(property, msg, CHECK);
441     } else {
442       assert_property(property, msg, CHECK);
443     }
444   }
445 
446   inline void guarantee_property(bool b,
447                                  const char* msg,
448                                  int index,
449                                  TRAPS) const {
450     if (!b) { classfile_parse_error(msg, index, THREAD); return; }
451   }
452 
453   inline void guarantee_property(bool b,
454                                  const char* msg,
455                                  const char *name,
456                                  TRAPS) const {
457     if (!b) { classfile_parse_error(msg, name, THREAD); return; }
458   }
459 
460   inline void guarantee_property(bool b,
461                                  const char* msg,
462                                  int index,
463                                  const char *name,
464                                  TRAPS) const {
465     if (!b) { classfile_parse_error(msg, index, name, THREAD); return; }
466   }
467 
468   void throwIllegalSignature(const char* type,
469                              const Symbol* name,
470                              const Symbol* sig,
471                              TRAPS) const;
472 
473   void throwInlineTypeLimitation(THREAD_AND_LOCATION_DECL,
474                                  const char* msg,
475                                  const Symbol* name = NULL,
476                                  const Symbol* sig  = NULL) const;
477 
478   void verify_constantvalue(const ConstantPool* const cp,
479                             int constantvalue_index,
480                             int signature_index,
481                             TRAPS) const;
482 
483   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
484   void verify_legal_class_name(const Symbol* name, TRAPS) const;
485   void verify_legal_field_name(const Symbol* name, TRAPS) const;
486   void verify_legal_method_name(const Symbol* name, TRAPS) const;
487 
488   void verify_legal_field_signature(const Symbol* fieldname,
489                                     const Symbol* signature,
490                                     TRAPS) const;
491   int  verify_legal_method_signature(const Symbol* methodname,
492                                      const Symbol* signature,
493                                      TRAPS) const;
494   void verify_legal_name_with_signature(const Symbol* name,
495                                         const Symbol* signature,
496                                         TRAPS) const;
497 
498   void verify_class_version(u2 major, u2 minor, Symbol* class_name, TRAPS);
499 
500   void verify_legal_class_modifiers(jint flags, TRAPS) const;
501   void verify_legal_field_modifiers(jint flags,
502                                     bool is_interface,
503                                     bool is_inline_type,
504                                     TRAPS) const;
505   void verify_legal_method_modifiers(jint flags,
506                                      bool is_interface,
507                                      bool is_inline_type,
508                                      const Symbol* name,
509                                      TRAPS) const;
510 
511   void check_super_class_access(const InstanceKlass* this_klass,
512                                 TRAPS);
513 
514   void check_super_interface_access(const InstanceKlass* this_klass,
515                                     TRAPS);
516 
517   const char* skip_over_field_signature(const char* signature,
518                                         bool void_ok,
519                                         unsigned int length,
520                                         TRAPS) const;
521 
522   // Wrapper for constantTag.is_klass_[or_]reference.
523   // In older versions of the VM, Klass*s cannot sneak into early phases of
524   // constant pool construction, but in later versions they can.
525   // %%% Let's phase out the old is_klass_reference.
526   bool valid_klass_reference_at(int index) const {
527     return _cp->is_within_bounds(index) &&
528              _cp->tag_at(index).is_klass_or_reference();
529   }
530 
531   // Checks that the cpool index is in range and is a utf8
532   bool valid_symbol_at(int cpool_index) const {
533     return _cp->is_within_bounds(cpool_index) &&
534              _cp->tag_at(cpool_index).is_utf8();
535   }
536 
537   void copy_localvariable_table(const ConstMethod* cm,
538                                 int lvt_cnt,
539                                 u2* const localvariable_table_length,
540                                 const unsafe_u2** const localvariable_table_start,
541                                 int lvtt_cnt,
542                                 u2* const localvariable_type_table_length,
543                                 const unsafe_u2** const localvariable_type_table_start,
544                                 TRAPS);
545 
546   void copy_method_annotations(ConstMethod* cm,
547                                const u1* runtime_visible_annotations,
548                                int runtime_visible_annotations_length,
549                                const u1* runtime_invisible_annotations,
550                                int runtime_invisible_annotations_length,
551                                const u1* runtime_visible_parameter_annotations,
552                                int runtime_visible_parameter_annotations_length,
553                                const u1* runtime_invisible_parameter_annotations,
554                                int runtime_invisible_parameter_annotations_length,
555                                const u1* runtime_visible_type_annotations,
556                                int runtime_visible_type_annotations_length,
557                                const u1* runtime_invisible_type_annotations,
558                                int runtime_invisible_type_annotations_length,
559                                const u1* annotation_default,
560                                int annotation_default_length,
561                                TRAPS);
562 
563   void update_class_name(Symbol* new_name);
564 
565   // Check if the class file supports inline types
566   bool supports_inline_types() const;
567 
568  public:
569   ClassFileParser(ClassFileStream* stream,
570                   Symbol* name,
571                   ClassLoaderData* loader_data,
572                   const ClassLoadInfo* cl_info,
573                   Publicity pub_level,
574                   TRAPS);
575 
576   ~ClassFileParser();
577 
578   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, const ClassInstanceInfo& cl_inst_info, TRAPS);
579 
580   const ClassFileStream* clone_stream() const;
581 
582   void set_klass_to_deallocate(InstanceKlass* klass);
583 
584   int static_field_size() const;
585   int total_oop_map_count() const;
586   jint layout_size() const;
587 
588   int vtable_size() const { return _vtable_size; }
589   int itable_size() const { return _itable_size; }
590 
591   u2 this_class_index() const { return _this_class_index; }
592 
593   bool is_hidden() const { return _is_hidden; }
594   bool is_interface() const { return _access_flags.is_interface(); }
595   bool is_inline_type() const { return _access_flags.is_inline_type(); }
596   bool is_value_capable_class() const;
597   bool has_inline_fields() const { return _has_inline_type_fields; }
598   bool invalid_inline_super() const { return _invalid_inline_super; }
599   void set_invalid_inline_super() { _invalid_inline_super = true; }
600   bool invalid_identity_super() const { return _invalid_identity_super; }
601   void set_invalid_identity_super() { _invalid_identity_super = true; }
602   bool is_invalid_super_for_inline_type();
603 
604   u2 java_fields_count() const { return _java_fields_count; }
605 
606   ClassLoaderData* loader_data() const { return _loader_data; }
607   const Symbol* class_name() const { return _class_name; }
608   const InstanceKlass* super_klass() const { return _super_klass; }
609 
610   ReferenceType reference_type() const { return _rt; }
611   AccessFlags access_flags() const { return _access_flags; }
612 
613   bool is_internal() const { return INTERNAL == _pub_level; }
614 
615   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
616 
617 #ifdef ASSERT
618   static bool is_internal_format(Symbol* class_name);
619 #endif
620 
621 };
622 
623 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP
--- EOF ---