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   ref->obj_field_put_raw(_referent_offset, nullptr);
156 }
157 
158 HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
159   return ref->field_addr<HeapWord>(_referent_offset);
160 }
161 
162 oop java_lang_ref_Reference::next(oop ref) {
163   return ref->obj_field(_next_offset);
164 }
165 
166 void java_lang_ref_Reference::set_next(oop ref, oop value) {
167   ref->obj_field_put(_next_offset, value);
168 }
169 
170 void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
171   ref->obj_field_put_raw(_next_offset, value);
172 }
173 
174 HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
175   return ref->field_addr<HeapWord>(_next_offset);
176 }
177 
178 oop java_lang_ref_Reference::discovered(oop ref) {
179   return ref->obj_field(_discovered_offset);
180 }
181 
182 void java_lang_ref_Reference::set_discovered(oop ref, oop value) {
183   ref->obj_field_put(_discovered_offset, value);
184 }
185 
186 void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
187   ref->obj_field_put_raw(_discovered_offset, value);
188 }
189 
190 HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
191   return ref->field_addr<HeapWord>(_discovered_offset);
192 }
193 
194 bool java_lang_ref_Reference::is_final(oop ref) {
195   return InstanceKlass::cast(ref->klass())->reference_type() == REF_FINAL;
196 }
197 
198 bool java_lang_ref_Reference::is_phantom(oop ref) {
199   return InstanceKlass::cast(ref->klass())->reference_type() == REF_PHANTOM;
200 }
201 
202 bool java_lang_ref_Reference::is_weak(oop ref) {
203   return InstanceKlass::cast(ref->klass())->reference_type() == REF_WEAK;
204 }
205 
206 bool java_lang_ref_Reference::is_soft(oop ref) {
207   return InstanceKlass::cast(ref->klass())->reference_type() == REF_SOFT;
208 }
209 
210 inline oop java_lang_Thread::continuation(oop java_thread) {
211   return java_thread->obj_field(_continuation_offset);
212 }
213 
214 inline int64_t java_lang_Thread::thread_id(oop java_thread) {
215   return java_thread->long_field(_tid_offset);
216 }
217 
218 inline oop java_lang_VirtualThread::vthread_scope() {
219   oop base = vmClasses::VirtualThread_klass()->static_field_base_raw();
220   return base->obj_field(static_vthread_scope_offset);
221 }
222 
223 #if INCLUDE_JFR
224 inline u2 java_lang_Thread::jfr_epoch(oop ref) {
225   return ref->short_field(_jfr_epoch_offset);
226 }
227 
228 inline void java_lang_Thread::set_jfr_epoch(oop ref, u2 epoch) {
229   ref->short_field_put(_jfr_epoch_offset, epoch);
230 }
231 #endif // INCLUDE_JFR
232 
233 
234 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
235   site->obj_field_put_volatile(_target_offset, target);
236 }
237 
238 inline oop  java_lang_invoke_CallSite::target(oop site) {
239   return site->obj_field(_target_offset);
240 }
241 
242 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
243   site->obj_field_put(_target_offset, target);
244 }
245 
246 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
247   return obj != NULL && is_subclass(obj->klass());
248 }
249 
250 inline jboolean java_lang_invoke_ConstantCallSite::is_frozen(oop site) {
251   return site->bool_field(_is_frozen_offset);
252 }
253 
254 inline bool java_lang_invoke_ConstantCallSite::is_instance(oop obj) {
255   return obj != NULL && is_subclass(obj->klass());
256 }
257 
258 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
259   return obj != NULL && is_subclass(obj->klass());
260 }
261 
262 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
263   return obj != NULL && obj->klass() == vmClasses::MemberName_klass();
264 }
265 
266 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
267   return obj != NULL && obj->klass() == vmClasses::ResolvedMethodName_klass();
268 }
269 
270 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
271   return obj != NULL && obj->klass() == vmClasses::MethodType_klass();
272 }
273 
274 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
275   return obj != NULL && is_subclass(obj->klass());
276 }
277 
278 inline bool java_lang_Class::is_instance(oop obj) {
279   return obj != NULL && obj->klass() == vmClasses::Class_klass();
280 }
281 
282 inline Klass* java_lang_Class::as_Klass(oop java_class) {
283   //%note memory_2
284   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
285   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
286   assert(k == NULL || k->is_klass(), "type check");
287   return k;
288 }
289 
290 inline bool java_lang_Class::is_primitive(oop java_class) {
291   // should assert:
292   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
293   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
294 
295 #ifdef ASSERT
296   if (is_primitive) {
297     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
298     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
299         "Should be either the T_VOID primitive or a java primitive");
300   }
301 #endif
302 
303   return is_primitive;
304 }
305 
306 inline bool java_lang_Class::is_primary_mirror(oop java_class) {
307   Klass* k = as_Klass(java_class);
308   if (k->is_inline_klass()) {
309     return java_class == primary_mirror(java_class);
310   } else {
311     return true;
312   }
313 }
314 
315 inline bool java_lang_Class::is_secondary_mirror(oop java_class) {
316   Klass* k = as_Klass(java_class);
317   if (k->is_inline_klass()) {
318     return java_class == secondary_mirror(java_class);
319   } else {
320     return false;
321   }
322 }
323 
324 inline size_t java_lang_Class::oop_size(oop java_class) {
325   assert(_oop_size_offset != 0, "must be set");
326   int size = java_class->int_field(_oop_size_offset);
327   assert(size > 0, "Oop size must be greater than zero, not %d", size);
328   return size;
329 }
330 
331 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
332   return obj != NULL && is_subclass(obj->klass());
333 }
334 
335 inline bool java_lang_Module::is_instance(oop obj) {
336   return obj != NULL && obj->klass() == vmClasses::Module_klass();
337 }
338 
339 inline int Backtrace::merge_bci_and_version(int bci, int version) {
340   // only store u2 for version, checking for overflow.
341   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
342   assert((jushort)bci == bci, "bci should be short");
343   return build_int_from_shorts(version, bci);
344 }
345 
346 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
347   // only store u2 for mid and cpref, checking for overflow.
348   assert((jushort)mid == mid, "mid should be short");
349   assert((jushort)cpref == cpref, "cpref should be short");
350   return build_int_from_shorts(cpref, mid);
351 }
352 
353 inline int Backtrace::bci_at(unsigned int merged) {
354   return extract_high_short_from_int(merged);
355 }
356 
357 inline int Backtrace::version_at(unsigned int merged) {
358   return extract_low_short_from_int(merged);
359 }
360 
361 inline int Backtrace::mid_at(unsigned int merged) {
362   return extract_high_short_from_int(merged);
363 }
364 
365 inline int Backtrace::cpref_at(unsigned int merged) {
366   return extract_low_short_from_int(merged);
367 }
368 
369 inline int Backtrace::get_line_number(Method* method, int bci) {
370   int line_number = 0;
371   if (method->is_native()) {
372     // Negative value different from -1 below, enabling Java code in
373     // class java.lang.StackTraceElement to distinguish "native" from
374     // "no LineNumberTable".  JDK tests for -2.
375     line_number = -2;
376   } else {
377     // Returns -1 if no LineNumberTable, and otherwise actual line number
378     line_number = method->line_number_from_bci(bci);
379   }
380   return line_number;
381 }
382 
383 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
384   // RedefineClasses() currently permits redefine operations to
385   // happen in parallel using a "last one wins" philosophy. That
386   // spec laxness allows the constant pool entry associated with
387   // the source_file_name_index for any older constant pool version
388   // to be unstable so we shouldn't try to use it.
389   if (holder->constants()->version() != version) {
390     return NULL;
391   } else {
392     return holder->source_file_name();
393   }
394 }
395 
396 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP