1 /*
  2  * Copyright (c) 2011, 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_OOPS_FIELDINFO_HPP
 26 #define SHARE_OOPS_FIELDINFO_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/typeArrayOop.hpp"
 30 #include "utilities/unsigned5.hpp"
 31 #include "utilities/vmEnums.hpp"
 32 
 33 static constexpr u4 flag_mask(int pos) {
 34   return (u4)1 << pos;
 35 }
 36 
 37 
 38 // Helper class for access to the underlying Array<u1> used to
 39 // store the compressed stream of FieldInfo
 40 template<typename ARR, typename OFF>
 41 struct ArrayHelper {
 42   uint8_t operator()(ARR a, OFF i) const { return a->at(i); };
 43   void operator()(ARR a, OFF i, uint8_t b) const { a->at_put(i,b); };
 44   // So, an expression ArrayWriterHelper() acts like these lambdas:
 45   // auto get = [&](ARR a, OFF i){ return a[i]; };
 46   // auto set = [&](ARR a, OFF i, uint8_t x){ a[i] = x; };
 47 };
 48 
 49 // This class represents the field information contained in the fields
 50 // array of an InstanceKlass.  Currently it's laid on top an array of
 51 // Java shorts but in the future it could simply be used as a real
 52 // array type.  FieldInfo generally shouldn't be used directly.
 53 // Fields should be queried either through InstanceKlass or through
 54 // the various FieldStreams.
 55 class FieldInfo {
 56   friend class fieldDescriptor;
 57   friend class JavaFieldStream;
 58   friend class ClassFileParser;
 59   friend class FieldInfoStream;
 60   friend class FieldStreamBase;
 61   friend class FieldInfoReader;
 62   friend class VMStructs;
 63 
 64  public:
 65 
 66   class FieldFlags {
 67     friend class VMStructs;
 68     friend class JVMCIVMStructs;
 69     friend class FieldDesc;
 70 
 71     // The ordering of this enum is totally internal.  More frequent
 72     // flags should come earlier than less frequent ones, because
 73     // earlier ones compress better.
 74     enum FieldFlagBitPosition {
 75       _ff_null_free_inline_type,  // field's type is an inline type and the field is null free
 76       _ff_flat,         // field is a flat field
 77       _ff_initialized,  // has ConstantValue initializer attribute
 78       _ff_injected,     // internal field injected by the JVM
 79       _ff_generic,      // has a generic signature
 80       _ff_stable,       // trust as stable b/c declared as @Stable
 81       _ff_contended,    // is contended, may have contention-group
 82       _ff_null_marker,  // field has a null marker, optional section include the null marker offset
 83       _ff_internal_null_marker // null marker is internal (inside the layout of a flat field)
 84     };
 85 
 86     // Some but not all of the flag bits signal the presence of an
 87     // additional 32-bit item in the field record.
 88     static const u4 _optional_item_bit_mask =
 89       flag_mask((int)_ff_initialized) |
 90       flag_mask((int)_ff_generic)     |
 91       flag_mask((int)_ff_contended)   |
 92       flag_mask((int)_ff_null_marker);
 93 
 94     // boilerplate:
 95     u4 _flags;
 96 
 97     bool test_flag(FieldFlagBitPosition pos) const {
 98       return (_flags & flag_mask(pos)) != 0;
 99     }
100     void update_flag(FieldFlagBitPosition pos, bool z) {
101       if (z)    _flags |=  flag_mask(pos);
102       else      _flags &= ~flag_mask(pos);
103     }
104 
105    public:
106     FieldFlags(u4 flags) {
107       _flags = flags;
108     }
109     u4 as_uint() const { return _flags; }
110     bool has_any_optionals() const {
111       return (_flags & _optional_item_bit_mask) != 0;
112     }
113 
114     bool is_initialized() const     { return test_flag(_ff_initialized); }
115     bool is_null_free_inline_type() const { return test_flag(_ff_null_free_inline_type); }
116     bool is_flat() const            { return test_flag(_ff_flat); }
117     bool is_injected() const        { return test_flag(_ff_injected); }
118     bool is_generic() const         { return test_flag(_ff_generic); }
119     bool is_stable() const          { return test_flag(_ff_stable); }
120     bool is_contended() const       { return test_flag(_ff_contended); }
121     bool has_null_marker() const    { return test_flag(_ff_null_marker); }
122     bool is_null_marker_internal() const {return test_flag(_ff_internal_null_marker); }
123 
124     void update_initialized(bool z) { update_flag(_ff_initialized, z); }
125     void update_null_free_inline_type(bool z) { update_flag(_ff_null_free_inline_type, z); }
126     void update_flat(bool z)        { update_flag(_ff_flat, z); }
127     void update_injected(bool z)    { update_flag(_ff_injected, z); }
128     void update_generic(bool z)     { update_flag(_ff_generic, z); }
129     void update_stable(bool z)      { update_flag(_ff_stable, z); }
130     void update_contended(bool z)   { update_flag(_ff_contended, z); }
131     void update_null_marker(bool z) { update_flag(_ff_null_marker, z); }
132     void update_internal_null_marker(bool z) { update_flag(_ff_internal_null_marker, z); }
133   };
134 
135  private:
136   // The following items are the unpacked bitwise information content
137   // of a field record.  Per-field metadata extracted from the class
138   // file are stored logically as a group of these items.  The
139   // classfile parser produces these records in a temporary array, and
140   // then compresses them into a FieldInfoStream.
141   //
142   u4 _index;                    // which field it is
143   u2 _name_index;               // index in CP of name
144   u2 _signature_index;          // index in CP of descriptor
145   u4 _offset;                   // offset in object layout
146   AccessFlags _access_flags;    // access flags (JVM spec)
147   FieldFlags _field_flags;      // VM defined flags (not JVM spec)
148   u4 _null_marker_offset;       // null marker offset for this field in the object layout
149   u2 _initializer_index;        // index from ConstantValue attr (or 0)
150   u2 _generic_signature_index;  // index from GenericSignature attr (or 0)
151   u2 _contention_group;         // index from @Contended group item (or 0)
152 
153  public:
154 
155   FieldInfo() : _name_index(0),
156                 _signature_index(0),
157                 _offset(0),
158                 _access_flags(AccessFlags(0)),
159                 _field_flags(FieldFlags(0)),
160                 _null_marker_offset(0),
161                 _initializer_index(0),
162                 _generic_signature_index(0),
163                 _contention_group(0) { }
164 
165   FieldInfo(AccessFlags access_flags, u2 name_index, u2 signature_index, u2 initval_index, FieldInfo::FieldFlags fflags) :
166             _name_index(name_index),
167             _signature_index(signature_index),
168             _offset(0),
169             _access_flags(access_flags),
170             _field_flags(fflags),
171             _null_marker_offset(0),
172             _initializer_index(initval_index),
173             _generic_signature_index(0),
174             _contention_group(0) {
175               if (initval_index != 0) {
176                 _field_flags.update_initialized(true);
177               }
178             }
179 
180   u4 index() const                           { return _index; }
181   void set_index(u4 index)                   { _index = index; }
182   u2 name_index() const                      { return _name_index; }
183   void set_name_index(u2 index)              { _name_index = index; }
184   u2 signature_index() const                 { return _signature_index; }
185   void set_signature_index(u2 index)         { _signature_index = index; }
186   u4 offset() const                          { return _offset; }
187   void set_offset(u4 offset)                 { _offset = offset; }
188   AccessFlags access_flags() const           { return _access_flags; }
189   FieldFlags field_flags() const             { return _field_flags; }
190   FieldFlags* field_flags_addr()             { return &_field_flags; }
191   u4 null_marker_offset() const              { return _null_marker_offset; }
192   void set_null_marker_offset(u4 offset)     { _null_marker_offset = offset; }
193   u2 initializer_index() const               { return _initializer_index; }
194   void set_initializer_index(u2 index)       { _initializer_index = index; }
195   u2 generic_signature_index() const         { return _generic_signature_index; }
196   void set_generic_signature_index(u2 index) { _generic_signature_index = index; }
197   u2 contention_group() const                { return _contention_group; }
198 
199   bool is_contended() const {
200     return _field_flags.is_contended();
201   }
202 
203   u2 contended_group() const {
204     assert(is_contended(), "");
205     return _contention_group;
206   }
207 
208   void set_contended_group(u2 group) {
209     _field_flags.update_contended(true);
210     _contention_group = group;
211   }
212 
213   bool is_offset_set() const {
214     return _offset != 0;
215   }
216 
217   inline Symbol* name(ConstantPool* cp) const;
218 
219   inline Symbol* signature(ConstantPool* cp) const;
220 
221   inline Symbol* lookup_symbol(int symbol_index) const;
222 
223   void print(outputStream* os, ConstantPool* cp);
224   void static print_from_growable_array(outputStream* os, GrowableArray<FieldInfo>* array, ConstantPool* cp);
225 };
226 
227 class FieldInfoStream;
228 
229 // Gadget for sizing and/or writing a stream of field records.
230 template<typename CON>
231 class Mapper {
232   CON* _consumer;  // can be UNSIGNED5::Writer or UNSIGNED5::Sizer
233   int _next_index;
234 public:
235   Mapper(CON* consumer) : _consumer(consumer) { _next_index = 0; }
236   int next_index() const { return _next_index; }
237   void set_next_index(int next_index) { _next_index = next_index; }
238   CON* consumer() const { return _consumer; }
239   void map_field_info(const FieldInfo& fi);
240 };
241 
242 
243 // Gadget for decoding and reading the stream of field records.
244 class FieldInfoReader {
245   friend class FieldInfoStream;
246   friend class ClassFileParser;
247   friend class FieldStreamBase;
248   friend class FieldInfo;
249 
250   UNSIGNED5::Reader<const u1*, int> _r;
251   int _next_index;
252 
253   public:
254   FieldInfoReader(const Array<u1>* fi);
255 
256   private:
257   uint32_t next_uint() { return _r.next_uint(); }
258   void skip(int n) { int s = _r.try_skip(n); assert(s == n,""); }
259 
260 public:
261   int has_next() { return _r.has_next(); }
262   int position() { return _r.position(); }
263   int next_index() { return _next_index; }
264   void read_field_info(FieldInfo& fi);
265   // skip a whole field record, both required and optional bits
266   FieldInfoReader&  skip_field_info();
267 
268   // Skip to the nth field.  If the reader is freshly initialized to
269   // the zero index, this will call skip_field_info() n times.
270   FieldInfoReader& skip_to_field_info(int n);
271 
272   // for random access, if you know where to go up front:
273   FieldInfoReader& set_position_and_next_index(int position, int next_index);
274 };
275 
276 // The format of the stream, after decompression, is a series of
277 // integers organized like this:
278 //
279 //   FieldInfoStream := j=num_java_fields k=num_injected_fields Field[j+k] End
280 //   Field := name sig offset access flags Optionals(flags)
281 //   Optionals(i) := initval?[i&is_init]     // ConstantValue attr
282 //                   gsig?[i&is_generic]     // signature attr
283 //                   group?[i&is_contended]  // Contended anno (group)
284 //   End = 0
285 //
286 class FieldInfoStream : AllStatic {
287   friend class fieldDescriptor;
288   friend class JavaFieldStream;
289   friend class FieldStreamBase;
290   friend class ClassFileParser;
291 
292  public:
293   static int num_java_fields(const Array<u1>* fis);
294   static int num_injected_java_fields(const Array<u1>* fis);
295   static int num_total_fields(const Array<u1>* fis);
296 
297   static Array<u1>* create_FieldInfoStream(GrowableArray<FieldInfo>* fields, int java_fields, int injected_fields,
298                                                           ClassLoaderData* loader_data, TRAPS);
299   static GrowableArray<FieldInfo>* create_FieldInfoArray(const Array<u1>* fis, int* java_fields_count, int* injected_fields_count);
300   static void print_from_fieldinfo_stream(Array<u1>* fis, outputStream* os, ConstantPool* cp);
301 };
302 
303 class FieldStatus {
304   enum FieldStatusBitPosition {
305     _fs_access_watched,       // field access is watched by JVMTI
306     _fs_modification_watched, // field modification is watched by JVMTI
307     _initialized_final_update // (static) final field updated outside (class) initializer
308   };
309 
310   // boilerplate:
311   u1 _flags;
312   static constexpr u1 flag_mask(FieldStatusBitPosition pos) { return (u1)1 << (int)pos; }
313   bool test_flag(FieldStatusBitPosition pos) { return (_flags & flag_mask(pos)) != 0; }
314   // this performs an atomic update on a live status byte!
315   void update_flag(FieldStatusBitPosition pos, bool z);
316   // out-of-line functions do a CAS-loop
317   static void atomic_set_bits(u1& flags, u1 mask);
318   static void atomic_clear_bits(u1& flags, u1 mask);
319 
320   public:
321   FieldStatus() { _flags = 0; }
322   FieldStatus(u1 flags) { _flags = flags; }
323   u1 as_uint() { return _flags; }
324 
325   bool is_access_watched()        { return test_flag(_fs_access_watched); }
326   bool is_modification_watched()  { return test_flag(_fs_modification_watched); }
327   bool is_initialized_final_update() { return test_flag(_initialized_final_update); }
328 
329   void update_access_watched(bool z);
330   void update_modification_watched(bool z);
331   void update_initialized_final_update(bool z);
332 };
333 
334 #endif // SHARE_OOPS_FIELDINFO_HPP
--- EOF ---