1 /*
  2  * Copyright (c) 2015, 2022, 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_JAVACLASSES_INLINE_HPP
 26 #define SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP
 27 
 28 #include "classfile/javaClasses.hpp"
 29 
 30 #include "memory/referenceType.hpp"
 31 #include "oops/access.inline.hpp"
 32 #include "oops/instanceKlass.inline.hpp"
 33 #include "oops/method.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "oops/oopsHierarchy.hpp"
 36 #include "oops/typeArrayOop.inline.hpp"
 37 
 38 void java_lang_String::set_coder(oop string, jbyte coder) {
 39   string->byte_field_put(_coder_offset, coder);
 40 }
 41 
 42 void java_lang_String::set_value_raw(oop string, typeArrayOop buffer) {
 43   string->obj_field_put_raw(_value_offset, buffer);
 44 }
 45 
 46 void java_lang_String::set_value(oop string, typeArrayOop buffer) {
 47   string->obj_field_put(_value_offset, buffer);
 48 }
 49 
 50 bool java_lang_String::hash_is_set(oop java_string) {
 51   return java_string->int_field(_hash_offset) != 0 || java_string->bool_field(_hashIsZero_offset) != 0;
 52 }
 53 
 54 // Accessors
 55 bool java_lang_String::value_equals(typeArrayOop str_value1, typeArrayOop str_value2) {
 56   return ((str_value1 == str_value2) ||
 57           (str_value1->length() == str_value2->length() &&
 58            (!memcmp(str_value1->base(T_BYTE),
 59                     str_value2->base(T_BYTE),
 60                     str_value2->length() * sizeof(jbyte)))));
 61 }
 62 
 63 typeArrayOop java_lang_String::value(oop java_string) {
 64   assert(is_instance(java_string), "must be java_string");
 65   return (typeArrayOop) java_string->obj_field(_value_offset);
 66 }
 67 
 68 typeArrayOop java_lang_String::value_no_keepalive(oop java_string) {
 69   assert(is_instance(java_string), "must be java_string");
 70   return (typeArrayOop) java_string->obj_field_access<AS_NO_KEEPALIVE>(_value_offset);
 71 }
 72 
 73 bool java_lang_String::is_latin1(oop java_string) {
 74   assert(is_instance(java_string), "must be java_string");
 75   jbyte coder = java_string->byte_field(_coder_offset);
 76   assert(CompactStrings || coder == CODER_UTF16, "Must be UTF16 without CompactStrings");
 77   return coder == CODER_LATIN1;
 78 }
 79 
 80 uint8_t* java_lang_String::flags_addr(oop java_string) {
 81   assert(_initialized, "Must be initialized");
 82   assert(is_instance(java_string), "Must be java string");
 83   return java_string->field_addr<uint8_t>(_flags_offset);
 84 }
 85 
 86 bool java_lang_String::is_flag_set(oop java_string, uint8_t flag_mask) {
 87   return (Atomic::load(flags_addr(java_string)) & flag_mask) != 0;
 88 }
 89 
 90 bool java_lang_String::deduplication_forbidden(oop java_string) {
 91   return is_flag_set(java_string, _deduplication_forbidden_mask);
 92 }
 93 
 94 bool java_lang_String::deduplication_requested(oop java_string) {
 95   return is_flag_set(java_string, _deduplication_requested_mask);
 96 }
 97 
 98 void java_lang_String::set_deduplication_forbidden(oop java_string) {
 99   test_and_set_flag(java_string, _deduplication_forbidden_mask);
100 }
101 
102 bool java_lang_String::test_and_set_deduplication_requested(oop java_string) {
103   return test_and_set_flag(java_string, _deduplication_requested_mask);
104 }
105 
106 int java_lang_String::length(oop java_string, typeArrayOop value) {
107   assert(_initialized, "Must be initialized");
108   assert(is_instance(java_string), "must be java_string");
109   assert(value_equals(value, java_lang_String::value(java_string)),
110          "value must be equal to java_lang_String::value(java_string)");
111   if (value == NULL) {
112     return 0;
113   }
114   int arr_length = value->length();
115   if (!is_latin1(java_string)) {
116     assert((arr_length & 1) == 0, "should be even for UTF16 string");
117     arr_length >>= 1; // convert number of bytes to number of elements
118   }
119   return arr_length;
120 }
121 
122 int java_lang_String::length(oop java_string) {
123   assert(_initialized, "Must be initialized");
124   assert(is_instance(java_string), "must be java_string");
125   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
126   return length(java_string, value);
127 }
128 
129 bool java_lang_String::is_instance(oop obj) {
130   return obj != NULL && obj->klass() == vmClasses::String_klass();
131 }
132 
133 // Accessors
134 
135 oop java_lang_ref_Reference::weak_referent_no_keepalive(oop ref) {
136   assert(java_lang_ref_Reference::is_weak(ref) || java_lang_ref_Reference::is_soft(ref), "must be Weak or Soft Reference");
137   return ref->obj_field_access<ON_WEAK_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
138 }
139 
140 oop java_lang_ref_Reference::weak_referent(oop ref) {
141   assert(java_lang_ref_Reference::is_weak(ref) || java_lang_ref_Reference::is_soft(ref), "must be Weak or Soft Reference");
142   return ref->obj_field_access<ON_WEAK_OOP_REF>(_referent_offset);
143 }
144 
145 oop java_lang_ref_Reference::phantom_referent_no_keepalive(oop ref) {
146   assert(java_lang_ref_Reference::is_phantom(ref), "must be Phantom Reference");
147   return ref->obj_field_access<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
148 }
149 
150 oop java_lang_ref_Reference::unknown_referent_no_keepalive(oop ref) {
151   return ref->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
152 }
153 
154 void java_lang_ref_Reference::clear_referent(oop ref) {
155   HeapAccess<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>::oop_store_at(ref, _referent_offset, nullptr);
156 }
157 
158 void java_lang_ref_Reference::clear_referent_raw(oop ref) {
159   ref->obj_field_put_raw(_referent_offset, nullptr);
160 }
161 
162 HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
163   return ref->field_addr<HeapWord>(_referent_offset);
164 }
165 
166 oop java_lang_ref_Reference::next(oop ref) {
167   return ref->obj_field(_next_offset);
168 }
169 
170 void java_lang_ref_Reference::set_next(oop ref, oop value) {
171   ref->obj_field_put(_next_offset, value);
172 }
173 
174 void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
175   ref->obj_field_put_raw(_next_offset, value);
176 }
177 
178 HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
179   return ref->field_addr<HeapWord>(_next_offset);
180 }
181 
182 oop java_lang_ref_Reference::discovered(oop ref) {
183   return ref->obj_field(_discovered_offset);
184 }
185 
186 void java_lang_ref_Reference::set_discovered(oop ref, oop value) {
187   ref->obj_field_put(_discovered_offset, value);
188 }
189 
190 void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
191   ref->obj_field_put_raw(_discovered_offset, value);
192 }
193 
194 HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
195   return ref->field_addr<HeapWord>(_discovered_offset);
196 }
197 
198 bool java_lang_ref_Reference::is_final(oop ref) {
199   return InstanceKlass::cast(ref->klass())->reference_type() == REF_FINAL;
200 }
201 
202 bool java_lang_ref_Reference::is_phantom(oop ref) {
203   return InstanceKlass::cast(ref->klass())->reference_type() == REF_PHANTOM;
204 }
205 
206 bool java_lang_ref_Reference::is_weak(oop ref) {
207   return InstanceKlass::cast(ref->klass())->reference_type() == REF_WEAK;
208 }
209 
210 bool java_lang_ref_Reference::is_soft(oop ref) {
211   return InstanceKlass::cast(ref->klass())->reference_type() == REF_SOFT;
212 }
213 
214 inline oop java_lang_Thread::continuation(oop java_thread) {
215   return java_thread->obj_field(_continuation_offset);
216 }
217 
218 inline int64_t java_lang_Thread::thread_id(oop java_thread) {
219   return java_thread->long_field(_tid_offset);
220 }
221 
222 inline oop java_lang_VirtualThread::vthread_scope() {
223   oop base = vmClasses::VirtualThread_klass()->static_field_base_raw();
224   return base->obj_field(static_vthread_scope_offset);
225 }
226 
227 #if INCLUDE_JFR
228 inline u2 java_lang_Thread::jfr_epoch(oop ref) {
229   return ref->short_field(_jfr_epoch_offset);
230 }
231 
232 inline void java_lang_Thread::set_jfr_epoch(oop ref, u2 epoch) {
233   ref->short_field_put(_jfr_epoch_offset, epoch);
234 }
235 #endif // INCLUDE_JFR
236 
237 
238 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
239   site->obj_field_put_volatile(_target_offset, target);
240 }
241 
242 inline oop  java_lang_invoke_CallSite::target(oop site) {
243   return site->obj_field(_target_offset);
244 }
245 
246 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
247   site->obj_field_put(_target_offset, target);
248 }
249 
250 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
251   return obj != NULL && is_subclass(obj->klass());
252 }
253 
254 inline jboolean java_lang_invoke_ConstantCallSite::is_frozen(oop site) {
255   return site->bool_field(_is_frozen_offset);
256 }
257 
258 inline bool java_lang_invoke_ConstantCallSite::is_instance(oop obj) {
259   return obj != NULL && is_subclass(obj->klass());
260 }
261 
262 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
263   return obj != NULL && is_subclass(obj->klass());
264 }
265 
266 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
267   return obj != NULL && obj->klass() == vmClasses::MemberName_klass();
268 }
269 
270 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
271   return obj != NULL && obj->klass() == vmClasses::ResolvedMethodName_klass();
272 }
273 
274 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
275   return obj != NULL && obj->klass() == vmClasses::MethodType_klass();
276 }
277 
278 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
279   return obj != NULL && is_subclass(obj->klass());
280 }
281 
282 inline bool java_lang_Class::is_instance(oop obj) {
283   return obj != NULL && obj->klass() == vmClasses::Class_klass();
284 }
285 
286 inline Klass* java_lang_Class::as_Klass(oop java_class) {
287   //%note memory_2
288   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
289   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
290   assert(k == NULL || k->is_klass(), "type check");
291   return k;
292 }
293 
294 inline bool java_lang_Class::is_primitive(oop java_class) {
295   // should assert:
296   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
297   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
298 
299 #ifdef ASSERT
300   if (is_primitive) {
301     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
302     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
303         "Should be either the T_VOID primitive or a java primitive");
304   }
305 #endif
306 
307   return is_primitive;
308 }
309 
310 inline bool java_lang_Class::is_primary_mirror(oop java_class) {
311   Klass* k = as_Klass(java_class);
312   if (k->is_inline_klass()) {
313     return java_class == primary_mirror(java_class);
314   } else {
315     return true;
316   }
317 }
318 
319 inline bool java_lang_Class::is_secondary_mirror(oop java_class) {
320   Klass* k = as_Klass(java_class);
321   if (k->is_inline_klass()) {
322     return java_class == secondary_mirror(java_class);
323   } else {
324     return false;
325   }
326 }
327 
328 inline size_t java_lang_Class::oop_size(oop java_class) {
329   assert(_oop_size_offset != 0, "must be set");
330   int size = java_class->int_field(_oop_size_offset);
331   assert(size > 0, "Oop size must be greater than zero, not %d", size);
332   return size;
333 }
334 
335 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
336   return obj != NULL && is_subclass(obj->klass());
337 }
338 
339 inline bool java_lang_Module::is_instance(oop obj) {
340   return obj != NULL && obj->klass() == vmClasses::Module_klass();
341 }
342 
343 inline int Backtrace::merge_bci_and_version(int bci, int version) {
344   // only store u2 for version, checking for overflow.
345   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
346   assert((jushort)bci == bci, "bci should be short");
347   return build_int_from_shorts(version, bci);
348 }
349 
350 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
351   // only store u2 for mid and cpref, checking for overflow.
352   assert((jushort)mid == mid, "mid should be short");
353   assert((jushort)cpref == cpref, "cpref should be short");
354   return build_int_from_shorts(cpref, mid);
355 }
356 
357 inline int Backtrace::bci_at(unsigned int merged) {
358   return extract_high_short_from_int(merged);
359 }
360 
361 inline int Backtrace::version_at(unsigned int merged) {
362   return extract_low_short_from_int(merged);
363 }
364 
365 inline int Backtrace::mid_at(unsigned int merged) {
366   return extract_high_short_from_int(merged);
367 }
368 
369 inline int Backtrace::cpref_at(unsigned int merged) {
370   return extract_low_short_from_int(merged);
371 }
372 
373 inline int Backtrace::get_line_number(Method* method, int bci) {
374   int line_number = 0;
375   if (method->is_native()) {
376     // Negative value different from -1 below, enabling Java code in
377     // class java.lang.StackTraceElement to distinguish "native" from
378     // "no LineNumberTable".  JDK tests for -2.
379     line_number = -2;
380   } else {
381     // Returns -1 if no LineNumberTable, and otherwise actual line number
382     line_number = method->line_number_from_bci(bci);
383   }
384   return line_number;
385 }
386 
387 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
388   // RedefineClasses() currently permits redefine operations to
389   // happen in parallel using a "last one wins" philosophy. That
390   // spec laxness allows the constant pool entry associated with
391   // the source_file_name_index for any older constant pool version
392   // to be unstable so we shouldn't try to use it.
393   if (holder->constants()->version() != version) {
394     return NULL;
395   } else {
396     return holder->source_file_name();
397   }
398 }
399 
400 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP