1 /*
  2  * Copyright (c) 2015, 2021, 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 "oops/access.inline.hpp"
 31 #include "oops/instanceKlass.inline.hpp"
 32 #include "oops/method.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "oops/oopsHierarchy.hpp"
 35 #include "oops/typeArrayOop.inline.hpp"
 36 
 37 void java_lang_String::set_coder(oop string, jbyte coder) {
 38   string->byte_field_put(_coder_offset, coder);
 39 }
 40 
 41 void java_lang_String::set_value_raw(oop string, typeArrayOop buffer) {
 42   string->obj_field_put_raw(_value_offset, buffer);
 43 }
 44 
 45 void java_lang_String::set_value(oop string, typeArrayOop buffer) {
 46   string->obj_field_put(_value_offset, buffer);
 47 }
 48 
 49 bool java_lang_String::hash_is_set(oop java_string) {
 50   return java_string->int_field(_hash_offset) != 0 || java_string->bool_field(_hashIsZero_offset) != 0;
 51 }
 52 
 53 // Accessors
 54 bool java_lang_String::value_equals(typeArrayOop str_value1, typeArrayOop str_value2) {
 55   return ((str_value1 == str_value2) ||
 56           (str_value1->length() == str_value2->length() &&
 57            (!memcmp(str_value1->base(T_BYTE),
 58                     str_value2->base(T_BYTE),
 59                     str_value2->length() * sizeof(jbyte)))));
 60 }
 61 
 62 typeArrayOop java_lang_String::value(oop java_string) {
 63   assert(is_instance(java_string), "must be java_string");
 64   return (typeArrayOop) java_string->obj_field(_value_offset);
 65 }
 66 
 67 typeArrayOop java_lang_String::value_no_keepalive(oop java_string) {
 68   assert(is_instance(java_string), "must be java_string");
 69   return (typeArrayOop) java_string->obj_field_access<AS_NO_KEEPALIVE>(_value_offset);
 70 }
 71 
 72 bool java_lang_String::is_latin1(oop java_string) {
 73   assert(is_instance(java_string), "must be java_string");
 74   jbyte coder = java_string->byte_field(_coder_offset);
 75   assert(CompactStrings || coder == CODER_UTF16, "Must be UTF16 without CompactStrings");
 76   return coder == CODER_LATIN1;
 77 }
 78 
 79 uint8_t* java_lang_String::flags_addr(oop java_string) {
 80   assert(_initialized, "Must be initialized");
 81   assert(is_instance(java_string), "Must be java string");
 82   return java_string->field_addr<uint8_t>(_flags_offset);
 83 }
 84 
 85 bool java_lang_String::is_flag_set(oop java_string, uint8_t flag_mask) {
 86   return (Atomic::load(flags_addr(java_string)) & flag_mask) != 0;
 87 }
 88 
 89 bool java_lang_String::deduplication_forbidden(oop java_string) {
 90   return is_flag_set(java_string, _deduplication_forbidden_mask);
 91 }
 92 
 93 bool java_lang_String::deduplication_requested(oop java_string) {
 94   return is_flag_set(java_string, _deduplication_requested_mask);
 95 }
 96 
 97 void java_lang_String::set_deduplication_forbidden(oop java_string) {
 98   test_and_set_flag(java_string, _deduplication_forbidden_mask);
 99 }
100 
101 bool java_lang_String::test_and_set_deduplication_requested(oop java_string) {
102   return test_and_set_flag(java_string, _deduplication_requested_mask);
103 }
104 
105 int java_lang_String::length(oop java_string, typeArrayOop value) {
106   assert(_initialized, "Must be initialized");
107   assert(is_instance(java_string), "must be java_string");
108   assert(value_equals(value, java_lang_String::value(java_string)),
109          "value must be equal to java_lang_String::value(java_string)");
110   if (value == NULL) {
111     return 0;
112   }
113   int arr_length = value->length();
114   if (!is_latin1(java_string)) {
115     assert((arr_length & 1) == 0, "should be even for UTF16 string");
116     arr_length >>= 1; // convert number of bytes to number of elements
117   }
118   return arr_length;
119 }
120 
121 int java_lang_String::length(oop java_string) {
122   assert(_initialized, "Must be initialized");
123   assert(is_instance(java_string), "must be java_string");
124   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
125   return length(java_string, value);
126 }
127 
128 bool java_lang_String::is_instance(oop obj) {
129   return obj != NULL && obj->klass() == vmClasses::String_klass();
130 }
131 
132 // Accessors
133 
134 oop java_lang_ref_Reference::weak_referent_no_keepalive(oop ref) {
135   return ref->obj_field_access<ON_WEAK_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
136 }
137 
138 oop java_lang_ref_Reference::phantom_referent_no_keepalive(oop ref) {
139   return ref->obj_field_access<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
140 }
141 
142 oop java_lang_ref_Reference::unknown_referent_no_keepalive(oop ref) {
143   return ref->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(_referent_offset);
144 }
145 
146 oop java_lang_ref_Reference::unknown_referent(oop ref) {
147   return ref->obj_field_access<ON_UNKNOWN_OOP_REF>(_referent_offset);
148 }
149 
150 void java_lang_ref_Reference::clear_referent(oop ref) {
151   ref->obj_field_put_raw(_referent_offset, nullptr);
152 }
153 
154 HeapWord* java_lang_ref_Reference::referent_addr_raw(oop ref) {
155   return ref->field_addr<HeapWord>(_referent_offset);
156 }
157 
158 oop java_lang_ref_Reference::next(oop ref) {
159   return ref->obj_field(_next_offset);
160 }
161 
162 void java_lang_ref_Reference::set_next(oop ref, oop value) {
163   ref->obj_field_put(_next_offset, value);
164 }
165 
166 void java_lang_ref_Reference::set_next_raw(oop ref, oop value) {
167   ref->obj_field_put_raw(_next_offset, value);
168 }
169 
170 HeapWord* java_lang_ref_Reference::next_addr_raw(oop ref) {
171   return ref->field_addr<HeapWord>(_next_offset);
172 }
173 
174 oop java_lang_ref_Reference::discovered(oop ref) {
175   return ref->obj_field(_discovered_offset);
176 }
177 
178 void java_lang_ref_Reference::set_discovered(oop ref, oop value) {
179   ref->obj_field_put(_discovered_offset, value);
180 }
181 
182 void java_lang_ref_Reference::set_discovered_raw(oop ref, oop value) {
183   ref->obj_field_put_raw(_discovered_offset, value);
184 }
185 
186 HeapWord* java_lang_ref_Reference::discovered_addr_raw(oop ref) {
187   return ref->field_addr<HeapWord>(_discovered_offset);
188 }
189 
190 bool java_lang_ref_Reference::is_final(oop ref) {
191   return InstanceKlass::cast(ref->klass())->reference_type() == REF_FINAL;
192 }
193 
194 bool java_lang_ref_Reference::is_phantom(oop ref) {
195   return InstanceKlass::cast(ref->klass())->reference_type() == REF_PHANTOM;
196 }
197 
198 inline oop java_lang_Thread::continuation(oop java_thread) {
199   return java_thread->obj_field(_continuation_offset);
200 }
201 
202 inline oop jdk_internal_vm_ContinuationScope::name(oop ref) {
203   return ref->obj_field(_name_offset);
204 }
205 
206 inline oop jdk_internal_vm_Continuation::scope(oop ref) {
207   return ref->obj_field(_scope_offset);
208 }
209 inline oop jdk_internal_vm_Continuation::target(oop ref) {
210   return ref->obj_field(_target_offset);
211 }
212 inline oop jdk_internal_vm_Continuation::parent(oop ref) {
213   return ref->obj_field(_parent_offset);
214 }
215 inline oop jdk_internal_vm_Continuation::yieldInfo(oop ref) {
216   return ref->obj_field(_yieldInfo_offset);
217 }
218 inline void jdk_internal_vm_Continuation::set_yieldInfo(oop ref, oop value) {
219   ref->obj_field_put(_yieldInfo_offset, value);
220 }
221 inline stackChunkOop jdk_internal_vm_Continuation::tail(oop ref) {
222   return (stackChunkOop)ref->obj_field(_tail_offset);
223 }
224 inline void jdk_internal_vm_Continuation::set_tail(oop ref, stackChunkOop value) {
225   ref->obj_field_put(_tail_offset, value);
226 }
227 inline jshort jdk_internal_vm_Continuation::critical_section(oop ref) {
228   return ref->short_field(_cs_offset);
229 }
230 inline void jdk_internal_vm_Continuation::set_critical_section(oop ref, jshort value) {
231   ref->short_field_put(_cs_offset, value);
232 }
233 
234 inline bool jdk_internal_vm_Continuation::done(oop ref) {
235   return ref->bool_field(_done_offset);
236 }
237 inline bool jdk_internal_vm_Continuation::is_mounted(oop ref) {
238   return ref->bool_field(_mounted_offset) != 0;
239 }
240 inline bool jdk_internal_vm_Continuation::is_preempted(oop ref) {
241   return ref->bool_field(_preempted_offset);
242 }
243 inline void jdk_internal_vm_Continuation::set_preempted(oop ref, bool value) {
244   ref->bool_field_put(_preempted_offset, (jboolean)value);
245 }
246 
247 inline oop java_lang_VirtualThread::vthread_scope() {
248   oop base = vmClasses::VirtualThread_klass()->static_field_base_raw();
249   return base->obj_field(static_vthread_scope_offset);
250 }
251 
252 inline jlong java_lang_VirtualThread::set_jfrTraceId(oop ref, jlong id) {
253   ref->long_field_put(java_lang_Thread::_tid_offset, id);
254   return id;
255 }
256 
257 inline oop jdk_internal_vm_StackChunk::parent(oop ref) {
258   return ref->obj_field(_parent_offset);
259 }
260 inline void jdk_internal_vm_StackChunk::set_parent(oop ref, oop value) {
261   ref->obj_field_put(_parent_offset, value);
262 }
263 
264 template<typename P>
265 inline bool jdk_internal_vm_StackChunk::is_parent_null(oop ref) {
266   return (oop)RawAccess<>::oop_load(ref->field_addr<P>(_parent_offset)) == NULL;
267 }
268 
269 template<typename P>
270 inline void jdk_internal_vm_StackChunk::set_parent_raw(oop ref, oop value) {
271   RawAccess<IS_DEST_UNINITIALIZED>::oop_store(ref->field_addr<P>(_parent_offset), value);
272 }
273 inline oop jdk_internal_vm_StackChunk::cont(oop ref) {
274   return ref->obj_field(_cont_offset);
275 }
276 inline void jdk_internal_vm_StackChunk::set_cont(oop ref, oop value) {
277   ref->obj_field_put(_cont_offset, value);
278 }
279 
280 template<typename P>
281 inline oop jdk_internal_vm_StackChunk::cont_raw(oop ref) {
282   return (oop)RawAccess<>::oop_load(ref->field_addr<P>(_cont_offset));
283 }
284 
285 template<typename P>
286 inline void jdk_internal_vm_StackChunk::set_cont_raw(oop ref, oop value) {
287   RawAccess<IS_DEST_UNINITIALIZED>::oop_store(ref->field_addr<P>(_cont_offset), value);
288 }
289 
290 inline jint jdk_internal_vm_StackChunk::size(oop ref) {
291   return ref->int_field(_size_offset);
292 }
293 inline void jdk_internal_vm_StackChunk::set_size(HeapWord* ref, jint value) {
294   *(cast_to_oop(ref))->field_addr<jint>(_size_offset) = value; // ref->int_field_put(_size_offset, value);
295 }
296 inline jint jdk_internal_vm_StackChunk::sp(oop ref) {
297   return ref->int_field(_sp_offset);
298 }
299 inline void jdk_internal_vm_StackChunk::set_sp(oop ref, jint value) {
300   ref->int_field_put(_sp_offset, value);
301 }
302 inline jlong jdk_internal_vm_StackChunk::pc(oop ref) {
303   return ref->long_field(_pc_offset);
304 }
305 inline void jdk_internal_vm_StackChunk::set_pc(oop ref, jlong value) {
306   ref->long_field_put(_pc_offset, value);
307 }
308 inline jint jdk_internal_vm_StackChunk::argsize(oop ref) {
309   return ref->int_field(_argsize_offset);
310 }
311 inline void jdk_internal_vm_StackChunk::set_argsize(oop ref, jint value) {
312   ref->int_field_put(_argsize_offset, value);
313 }
314 inline jbyte jdk_internal_vm_StackChunk::flags(oop ref) {
315   return ref->byte_field(_flags_offset);
316 }
317 inline void jdk_internal_vm_StackChunk::set_flags(oop ref, jbyte value) {
318   ref->byte_field_put(_flags_offset, value);
319 }
320 inline jint jdk_internal_vm_StackChunk::gc_sp(oop ref) {
321   return ref->int_field(_gcSP_offset);
322 }
323 inline void jdk_internal_vm_StackChunk::set_gc_sp(oop ref, jint value) {
324   ref->int_field_put(_gcSP_offset, value);
325 }
326 inline jlong jdk_internal_vm_StackChunk::mark_cycle(oop ref) {
327   return ref->long_field(_markCycle_offset);
328 }
329 inline void jdk_internal_vm_StackChunk::set_mark_cycle(oop ref, jlong value) {
330   ref->long_field_put(_markCycle_offset, value);
331 }
332 inline jint jdk_internal_vm_StackChunk::maxSize(oop ref) {
333   return ref->int_field(_maxSize_offset);
334 }
335 inline void jdk_internal_vm_StackChunk::set_maxSize(oop ref, jint value) {
336   ref->int_field_put(_maxSize_offset, value);
337 }
338 
339 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
340   site->obj_field_put_volatile(_target_offset, target);
341 }
342 
343 inline oop  java_lang_invoke_CallSite::target(oop site) {
344   return site->obj_field(_target_offset);
345 }
346 
347 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
348   site->obj_field_put(_target_offset, target);
349 }
350 
351 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
352   return obj != NULL && is_subclass(obj->klass());
353 }
354 
355 inline jboolean java_lang_invoke_ConstantCallSite::is_frozen(oop site) {
356   return site->bool_field(_is_frozen_offset);
357 }
358 
359 inline bool java_lang_invoke_ConstantCallSite::is_instance(oop obj) {
360   return obj != NULL && is_subclass(obj->klass());
361 }
362 
363 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
364   return obj != NULL && is_subclass(obj->klass());
365 }
366 
367 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
368   return obj != NULL && obj->klass() == vmClasses::MemberName_klass();
369 }
370 
371 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
372   return obj != NULL && obj->klass() == vmClasses::ResolvedMethodName_klass();
373 }
374 
375 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
376   return obj != NULL && obj->klass() == vmClasses::MethodType_klass();
377 }
378 
379 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
380   return obj != NULL && is_subclass(obj->klass());
381 }
382 
383 inline bool java_lang_Class::is_instance(oop obj) {
384   return obj != NULL && obj->klass() == vmClasses::Class_klass();
385 }
386 
387 inline Klass* java_lang_Class::as_Klass(oop java_class) {
388   //%note memory_2
389   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
390   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
391   assert(k == NULL || k->is_klass(), "type check");
392   return k;
393 }
394 
395 inline bool java_lang_Class::is_primitive(oop java_class) {
396   // should assert:
397   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
398   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
399 
400 #ifdef ASSERT
401   if (is_primitive) {
402     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
403     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
404         "Should be either the T_VOID primitive or a java primitive");
405   }
406 #endif
407 
408   return is_primitive;
409 }
410 
411 inline size_t java_lang_Class::oop_size(oop java_class) {
412   assert(_oop_size_offset != 0, "must be set");
413   int size = java_class->int_field(_oop_size_offset);
414   assert(size > 0, "Oop size must be greater than zero, not %d", size);
415   return size;
416 }
417 
418 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
419   return obj != NULL && is_subclass(obj->klass());
420 }
421 
422 inline bool java_lang_Module::is_instance(oop obj) {
423   return obj != NULL && obj->klass() == vmClasses::Module_klass();
424 }
425 
426 inline int Backtrace::merge_bci_and_version(int bci, int version) {
427   // only store u2 for version, checking for overflow.
428   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
429   assert((jushort)bci == bci, "bci should be short");
430   return build_int_from_shorts(version, bci);
431 }
432 
433 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
434   // only store u2 for mid and cpref, checking for overflow.
435   assert((jushort)mid == mid, "mid should be short");
436   assert((jushort)cpref == cpref, "cpref should be short");
437   return build_int_from_shorts(cpref, mid);
438 }
439 
440 inline int Backtrace::bci_at(unsigned int merged) {
441   return extract_high_short_from_int(merged);
442 }
443 
444 inline int Backtrace::version_at(unsigned int merged) {
445   return extract_low_short_from_int(merged);
446 }
447 
448 inline int Backtrace::mid_at(unsigned int merged) {
449   return extract_high_short_from_int(merged);
450 }
451 
452 inline int Backtrace::cpref_at(unsigned int merged) {
453   return extract_low_short_from_int(merged);
454 }
455 
456 inline int Backtrace::get_line_number(Method* method, int bci) {
457   int line_number = 0;
458   if (method->is_native()) {
459     // Negative value different from -1 below, enabling Java code in
460     // class java.lang.StackTraceElement to distinguish "native" from
461     // "no LineNumberTable".  JDK tests for -2.
462     line_number = -2;
463   } else {
464     // Returns -1 if no LineNumberTable, and otherwise actual line number
465     line_number = method->line_number_from_bci(bci);
466   }
467   return line_number;
468 }
469 
470 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
471   // RedefineClasses() currently permits redefine operations to
472   // happen in parallel using a "last one wins" philosophy. That
473   // spec laxness allows the constant pool entry associated with
474   // the source_file_name_index for any older constant pool version
475   // to be unstable so we shouldn't try to use it.
476   if (holder->constants()->version() != version) {
477     return NULL;
478   } else {
479     return holder->source_file_name();
480   }
481 }
482 
483 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP