1 /*
  2  * Copyright (c) 2015, 2023, 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_CDS_CLASSLISTPARSER_HPP
 26 #define SHARE_CDS_CLASSLISTPARSER_HPP
 27 
 28 #include "interpreter/bytecodes.hpp"
 29 #include "utilities/exceptions.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 #include "utilities/growableArray.hpp"
 32 #include "utilities/resizeableResourceHash.hpp"
 33 
 34 class constantPoolHandle;
 35 class Thread;
 36 
 37 class CDSIndyInfo {
 38   GrowableArray<const char*>* _items;
 39 public:
 40   CDSIndyInfo() : _items(nullptr) {}
 41   void add_item(const char* item) {
 42     if (_items == nullptr) {
 43       _items = new GrowableArray<const char*>(9);
 44     }
 45     assert(_items != nullptr, "sanity");
 46     _items->append(item);
 47   }
 48   void add_ref_kind(int ref_kind) {
 49     switch (ref_kind) {
 50     case JVM_REF_getField         : _items->append("REF_getField"); break;
 51     case JVM_REF_getStatic        : _items->append("REF_getStatic"); break;
 52     case JVM_REF_putField         : _items->append("REF_putField"); break;
 53     case JVM_REF_putStatic        : _items->append("REF_putStatic"); break;
 54     case JVM_REF_invokeVirtual    : _items->append("REF_invokeVirtual"); break;
 55     case JVM_REF_invokeStatic     : _items->append("REF_invokeStatic"); break;
 56     case JVM_REF_invokeSpecial    : _items->append("REF_invokeSpecial"); break;
 57     case JVM_REF_newInvokeSpecial : _items->append("REF_newInvokeSpecial"); break;
 58     case JVM_REF_invokeInterface  : _items->append("REF_invokeInterface"); break;
 59     default                       : ShouldNotReachHere();
 60     }
 61   }
 62   GrowableArray<const char*>* items() {
 63     return _items;
 64   }
 65 };
 66 
 67 class ClassListParser : public StackObj {
 68   static const char* CONSTANT_POOL_TAG;
 69   static const char* DYNAMIC_PROXY_TAG;
 70   static const char* LAMBDA_PROXY_TAG;
 71   static const char* LAMBDA_FORM_TAG;
 72 public:
 73   static const char* CLASS_REFLECTION_DATA_TAG;
 74   static const char* LOADER_NEGATIVE_CACHE_TAG;
 75 
 76   enum ParseMode {
 77     _parse_all,
 78     _parse_lambda_forms_invokers_only,
 79   };
 80 
 81 private:
 82   // Must be C_HEAP allocated -- we don't want nested resource allocations.
 83   typedef ResizeableResourceHashtable<int, InstanceKlass*,
 84                                       AnyObj::C_HEAP, mtClassShared> ID2KlassTable;
 85 
 86   enum {
 87     _unspecified      = -999,
 88 
 89     // Max number of bytes allowed per line in the classlist.
 90     // Theoretically Java class names could be 65535 bytes in length. Also, an input line
 91     // could have a very long path name up to JVM_MAXPATHLEN bytes in length. In reality,
 92     // 4K bytes is more than enough.
 93     _max_allowed_line_len = 65536, // WAS 4096,
 94     _line_buf_extra       = 10, // for detecting input too long
 95     _line_buf_size        = _max_allowed_line_len + _line_buf_extra
 96   };
 97 
 98   // Use a small initial size in debug build to test resizing logic
 99   static const int INITIAL_TABLE_SIZE = DEBUG_ONLY(17) NOT_DEBUG(1987);
100   static const int MAX_TABLE_SIZE = 61333;
101   static volatile Thread* _parsing_thread; // the thread that created _instance
102   static ClassListParser* _instance; // the singleton.
103   const char* _classlist_file;
104   FILE* _file;
105 
106   ID2KlassTable _id2klass_table;
107 
108   // The following field contains information from the *current* line being
109   // parsed.
110   char                _line[_line_buf_size];  // The buffer that holds the current line. Some characters in
111                                               // the buffer may be overwritten by '\0' during parsing.
112   int                 _line_len;              // Original length of the input line.
113   int                 _line_no;               // Line number for current line being parsed
114   const char*         _class_name;
115   GrowableArray<const char*>* _indy_items;    // items related to invoke dynamic for archiving lambda proxy classes
116   int                 _id;
117   int                 _super;
118   GrowableArray<int>* _interfaces;
119   bool                _interfaces_specified;
120   const char*         _source;
121   bool                _lambda_form_line;
122   bool                _constant_pool_line;
123   bool                _class_reflection_data_line;
124   bool                _loader_negative_cache_line;
125   ParseMode           _parse_mode;
126 
127   bool parse_int_option(const char* option_name, int* value);
128   bool parse_uint_option(const char* option_name, int* value);
129   InstanceKlass* load_class_from_source(Symbol* class_name, TRAPS);
130   ID2KlassTable* id2klass_table() {
131     return &_id2klass_table;
132   }
133   InstanceKlass* lookup_class_by_id(int id);
134   void print_specified_interfaces();
135   void print_actual_interfaces(InstanceKlass *ik);
136   bool is_matching_cp_entry(const constantPoolHandle &pool, int cp_index, TRAPS);
137 
138   InstanceKlass* find_builtin_class_helper(JavaThread* current, Symbol* class_name_symbol, oop class_loader_oop);
139   InstanceKlass* find_builtin_class(JavaThread* current, const char* class_name);
140 
141   void resolve_indy(JavaThread* current, Symbol* class_name_symbol);
142   void resolve_indy_impl(Symbol* class_name_symbol, TRAPS);
143   bool parse_one_line();
144   Klass* load_current_class(Symbol* class_name_symbol, TRAPS);
145   void parse_constant_pool_tag();
146   void parse_class_reflection_data_tag();
147   void parse_dynamic_proxy_tag();
148   void parse_loader_negative_cache_tag();
149 
150   ClassListParser(const char* file, ParseMode _parse_mode);
151   ~ClassListParser();
152   void print_diagnostic_info(outputStream* st, const char* msg, va_list ap) ATTRIBUTE_PRINTF(3, 0);
153   void print_diagnostic_info(outputStream* st, const char* msg, ...) ATTRIBUTE_PRINTF(3, 0);
154   void constant_pool_resolution_warning(const char* msg, ...) ATTRIBUTE_PRINTF(2, 0);
155   void error(const char* msg, ...) ATTRIBUTE_PRINTF(2, 0);
156   oop loader_from_type(const char* loader_name);
157 public:
158   static int parse_classlist(const char* classlist_path, ParseMode parse_mode, TRAPS) {
159     ClassListParser parser(classlist_path, parse_mode);
160     return parser.parse(THREAD); // returns the number of classes loaded.
161   }
162 
163   static bool is_parsing_thread();
164   static ClassListParser* instance() {
165     assert(is_parsing_thread(), "call this only in the thread that created ClassListParsing::_instance");
166     assert(_instance != nullptr, "must be");
167     return _instance;
168   }
169   static const char* lambda_proxy_tag() {
170     return LAMBDA_PROXY_TAG;
171   }
172   static const char* lambda_form_tag() {
173     return LAMBDA_FORM_TAG;
174   }
175 
176   int parse(TRAPS);
177   void split_tokens_by_whitespace(int offset);
178   int split_at_tag_from_line();
179   bool parse_at_tags();
180   char* _token;
181   void parse_int(int* value);
182   void parse_uint(int* value);
183   bool try_parse_uint(int* value);
184   bool skip_token(const char* option_name);
185   void skip_whitespaces();
186   void skip_non_whitespaces();
187 
188   bool is_id_specified() {
189     return _id != _unspecified;
190   }
191   bool is_super_specified() {
192     return _super != _unspecified;
193   }
194   bool are_interfaces_specified() {
195     return _interfaces->length() > 0;
196   }
197   int id() {
198     assert(is_id_specified(), "do not query unspecified id");
199     return _id;
200   }
201   int super() {
202     assert(is_super_specified(), "do not query unspecified super");
203     return _super;
204   }
205   void check_already_loaded(const char* which, int id) {
206     if (!id2klass_table()->contains(id)) {
207       error("%s id %d is not yet loaded", which, id);
208     }
209   }
210 
211   const char* current_class_name() {
212     return _class_name;
213   }
214 
215   bool is_loading_from_source();
216 
217   bool lambda_form_line() { return _lambda_form_line; }
218 
219   // Look up the super or interface of the current class being loaded
220   // (in this->load_current_class()).
221   InstanceKlass* lookup_super_for_current_class(Symbol* super_name);
222   InstanceKlass* lookup_interface_for_current_class(Symbol* interface_name);
223 
224   static void populate_cds_indy_info(const constantPoolHandle &pool, int cp_index, CDSIndyInfo* cii, TRAPS);
225 };
226 #endif // SHARE_CDS_CLASSLISTPARSER_HPP