1 /*
  2  * Copyright (c) 1999, 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_CI_CIINSTANCEKLASS_HPP
 26 #define SHARE_CI_CIINSTANCEKLASS_HPP
 27 
 28 #include "ci/ciConstantPoolCache.hpp"
 29 #include "ci/ciFlags.hpp"
 30 #include "ci/ciKlass.hpp"
 31 #include "ci/ciSymbol.hpp"
 32 #include "oops/instanceKlass.hpp"
 33 
 34 // ciInstanceKlass
 35 //
 36 // This class represents a Klass* in the HotSpot virtual machine
 37 // whose Klass part is an InstanceKlass.  It may or may not
 38 // be loaded.
 39 class ciInstanceKlass : public ciKlass {
 40   CI_PACKAGE_ACCESS
 41   friend class ciBytecodeStream;
 42   friend class ciEnv;
 43   friend class ciExceptionHandler;
 44   friend class ciMethod;
 45   friend class ciField;
 46   friend class ciReplay;
 47 
 48 private:
 49   enum SubklassValue { subklass_unknown, subklass_false, subklass_true };
 50 
 51   jobject                _loader;
 52   jobject                _protection_domain;
 53 
 54   InstanceKlass::ClassState _init_state;           // state of class
 55   bool                   _is_shared;
 56   bool                   _has_finalizer;
 57   SubklassValue          _has_subklass;
 58   bool                   _has_nonstatic_fields;
 59   bool                   _has_nonstatic_concrete_methods;
 60   bool                   _is_hidden;
 61   bool                   _is_record;
 62   bool                   _has_trusted_loader;
 63 
 64   ciFlags                _flags;
 65 
 66   // Lazy fields get filled in only upon request.
 67   ciInstanceKlass*       _super;
 68   ciInstance*            _java_mirror;
 69 
 70   ciConstantPoolCache*   _field_cache;  // cached map index->field
 71  public:
 72   GrowableArray<ciField*>* _nonstatic_fields;
 73  private:
 74   int                    _has_injected_fields; // any non static injected fields? lazily initialized.
 75 
 76   // The possible values of the _implementor fall into following three cases:
 77   //   null: no implementor.
 78   //   A ciInstanceKlass that's not itself: one implementor.
 79   //   Itself: more than one implementor.
 80   ciInstanceKlass*       _implementor;
 81   GrowableArray<ciInstanceKlass*>* _transitive_interfaces;
 82 
 83   void compute_injected_fields();
 84   bool compute_injected_fields_helper();
 85   void compute_transitive_interfaces();
 86 
 87 protected:
 88   ciInstanceKlass(Klass* k);
 89   ciInstanceKlass(ciSymbol* name, jobject loader, jobject protection_domain, BasicType bt = T_OBJECT); // for unloaded klasses
 90 
 91   InstanceKlass* get_instanceKlass() const {
 92     return InstanceKlass::cast(get_Klass());
 93   }
 94 
 95   oop loader();
 96   jobject loader_handle();
 97 
 98   oop protection_domain();
 99   jobject protection_domain_handle();
100 
101   const char* type_string() { return "ciInstanceKlass"; }
102 
103   bool is_in_package_impl(const char* packagename, int len);
104 
105   void print_impl(outputStream* st);
106 
107   ciConstantPoolCache* field_cache();
108 
109   bool is_shared() { return _is_shared; }
110 
111   void compute_shared_init_state();
112   bool compute_shared_has_subklass();
113   virtual int compute_nonstatic_fields();
114   GrowableArray<ciField*>* compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields, bool flatten = true);
115   bool compute_has_trusted_loader();
116 
117   // Update the init_state for shared klasses
118   void update_if_shared(InstanceKlass::ClassState expected) {
119     if (_is_shared && _init_state != expected) {
120       if (is_loaded()) compute_shared_init_state();
121     }
122   }
123 
124 public:
125   // Has this klass been initialized?
126   bool                   is_initialized() {
127     update_if_shared(InstanceKlass::fully_initialized);
128     return _init_state == InstanceKlass::fully_initialized;
129   }
130   bool                   is_not_initialized() {
131     update_if_shared(InstanceKlass::fully_initialized);
132     return _init_state < InstanceKlass::being_initialized;
133   }
134   // Is this klass being initialized?
135   bool                   is_being_initialized() {
136     update_if_shared(InstanceKlass::being_initialized);
137     return _init_state == InstanceKlass::being_initialized;
138   }
139   // Has this klass been linked?
140   bool                   is_linked() {
141     update_if_shared(InstanceKlass::linked);
142     return _init_state >= InstanceKlass::linked;
143   }
144   // Is this klass in error state?
145   bool                   is_in_error_state() {
146     update_if_shared(InstanceKlass::initialization_error);
147     return _init_state == InstanceKlass::initialization_error;
148   }
149 
150   // General klass information.
151   ciFlags                flags()          {
152     assert(is_loaded(), "must be loaded");
153     return _flags;
154   }
155   bool                   has_finalizer()  {
156     assert(is_loaded(), "must be loaded");
157     return _has_finalizer; }
158   bool                   has_subklass()   {
159     assert(is_loaded(), "must be loaded");
160     // Ignore cached subklass_false case.
161     // It could be invalidated by concurrent class loading and
162     // can result in type paradoxes during compilation when
163     // a subclass is observed, but has_subklass() returns false.
164     if (_has_subklass == subklass_true) {
165       return true;
166     }
167     if (flags().is_final()) {
168       return false;
169     }
170     return compute_shared_has_subklass();
171   }
172 
173   jint                   layout_helper_size_in_bytes()  {
174     return Klass::layout_helper_size_in_bytes(layout_helper());
175   }
176   jint                   size_helper()  {
177     return (Klass::layout_helper_size_in_bytes(layout_helper())
178             >> LogHeapWordSize);
179   }
180   jint                   has_nonstatic_fields()  {
181     assert(is_loaded(), "must be loaded");
182     return _has_nonstatic_fields; }
183   ciInstanceKlass*       super();
184   jint                   nof_implementors() {
185     ciInstanceKlass* impl;
186     assert(is_loaded(), "must be loaded");
187     impl = implementor();
188     if (impl == nullptr) {
189       return 0;
190     } else if (impl != this) {
191       return 1;
192     } else {
193       return 2;
194     }
195   }
196   bool has_nonstatic_concrete_methods()  {
197     assert(is_loaded(), "must be loaded");
198     return _has_nonstatic_concrete_methods;
199   }
200 
201   bool is_hidden() const {
202     return _is_hidden;
203   }
204 
205   bool is_record() const {
206     return _is_record;
207   }
208 
209   ciInstanceKlass* get_canonical_holder(int offset);
210   ciField* get_field_by_offset(int field_offset, bool is_static);
211   ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
212   // get field descriptor at field_offset ignoring flattening
213   ciField* get_non_flat_field_by_offset(int field_offset);
214 
215   // total number of nonstatic fields (including inherited):
216   int nof_nonstatic_fields() {
217     if (_nonstatic_fields == nullptr) {
218       return compute_nonstatic_fields();
219     } else {
220       return _nonstatic_fields->length();
221     }
222   }
223 
224   bool has_injected_fields() {
225     if (_has_injected_fields == -1) {
226       compute_injected_fields();
227     }
228     return _has_injected_fields > 0 ? true : false;
229   }
230 
231   bool has_object_fields() const;
232 
233   // nth nonstatic field (presented by ascending address)
234   ciField* nonstatic_field_at(int i) {
235     assert(_nonstatic_fields != nullptr, "");
236     return _nonstatic_fields->at(i);
237   }
238 
239   ciInstanceKlass* unique_concrete_subklass();
240   bool has_finalizable_subclass();
241 
242   bool contains_field_offset(int offset);
243 
244   // Get the instance of java.lang.Class corresponding to
245   // this klass.  This instance is used for locking of
246   // synchronized static methods of this klass.
247   ciInstance*            java_mirror();
248 
249   // Java access flags
250   bool is_public      () { return flags().is_public(); }
251   bool is_final       () { return flags().is_final(); }
252   bool is_interface   () { return flags().is_interface(); }
253   bool is_abstract    () { return flags().is_abstract(); }
254   bool is_abstract_value_klass() { return is_abstract() && !flags().is_identity(); }
255 
256   ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
257   // Note:  To find a method from name and type strings, use ciSymbol::make,
258   // but consider adding to vmSymbols.hpp instead.
259 
260   bool is_leaf_type();
261   ciInstanceKlass* implementor();
262 
263   ciInstanceKlass* unique_implementor() {
264     assert(is_loaded(), "must be loaded");
265     ciInstanceKlass* impl = implementor();
266     return (impl != this ? impl : nullptr);
267   }
268 
269   virtual bool can_be_inline_klass(bool is_exact = false);
270 
271   // Is the defining class loader of this class the default loader?
272   bool uses_default_loader() const;
273 
274   bool is_java_lang_Object() const;
275 
276   BasicType box_klass_type() const;
277   bool is_box_klass() const;
278   bool is_boxed_value_offset(int offset) const;
279 
280   // Is this klass in the given package?
281   bool is_in_package(const char* packagename) {
282     return is_in_package(packagename, (int) strlen(packagename));
283   }
284   bool is_in_package(const char* packagename, int len);
285 
286   // What kind of ciObject is this?
287   bool is_instance_klass() const { return true; }
288 
289   virtual ciKlass* exact_klass() {
290     if (is_loaded() && is_final() && !is_interface()) {
291       return this;
292     }
293     return nullptr;
294   }
295 
296   bool can_be_instantiated() {
297     assert(is_loaded(), "must be loaded");
298     return !is_interface() && !is_abstract();
299   }
300 
301   bool has_trusted_loader() const {
302     return _has_trusted_loader;
303   }
304   GrowableArray<ciInstanceKlass*>* transitive_interfaces() const;
305 
306   // Replay support
307 
308   // Dump the current state of this klass for compilation replay.
309   virtual void dump_replay_data(outputStream* out);
310 
311   static void dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik);
312 
313 
314   // Return stable class name suitable for replay file.
315   const char *replay_name() const;
316 
317 #ifdef ASSERT
318   bool debug_final_field_at(int offset);
319   bool debug_stable_field_at(int offset);
320 #endif
321 };
322 
323 #endif // SHARE_CI_CIINSTANCEKLASS_HPP