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