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 inline bool jdk_internal_vm_Continuation::is_reset(oop ref) {
234   return ref->bool_field(_reset_offset);
235 }
236 
237 inline bool jdk_internal_vm_Continuation::done(oop ref) {
238   return ref->bool_field(_done_offset);
239 }
240 inline bool jdk_internal_vm_Continuation::is_mounted(oop ref) {
241   return ref->bool_field(_mounted_offset) != 0;
242 }
243 inline bool jdk_internal_vm_Continuation::is_preempted(oop ref) {
244   return ref->bool_field(_preempted_offset);
245 }
246 inline void jdk_internal_vm_Continuation::set_preempted(oop ref, bool value) {
247   ref->bool_field_put(_preempted_offset, (jboolean)value);
248 }
249 
250 inline oop java_lang_VirtualThread::vthread_scope() {
251   oop base = vmClasses::VirtualThread_klass()->static_field_base_raw();
252   return base->obj_field(static_vthread_scope_offset);
253 }
254 
255 inline jlong java_lang_VirtualThread::set_jfrTraceId(oop ref, jlong id) {
256   ref->long_field_put(java_lang_Thread::_tid_offset, id);
257   return id;
258 }
259 
260 inline oop jdk_internal_vm_StackChunk::parent(oop ref) {
261   return ref->obj_field(_parent_offset);
262 }
263 inline void jdk_internal_vm_StackChunk::set_parent(oop ref, oop value) {
264   ref->obj_field_put(_parent_offset, value);
265 }
266 
267 template<typename P>
268 inline bool jdk_internal_vm_StackChunk::is_parent_null(oop ref) {
269   return (oop)RawAccess<>::oop_load(ref->field_addr<P>(_parent_offset)) == NULL;
270 }
271 
272 template<typename P>
273 inline void jdk_internal_vm_StackChunk::set_parent_raw(oop ref, oop value) {
274   RawAccess<IS_DEST_UNINITIALIZED>::oop_store(ref->field_addr<P>(_parent_offset), value);
275 }
276 inline oop jdk_internal_vm_StackChunk::cont(oop ref) {
277   return ref->obj_field(_cont_offset);
278 }
279 inline void jdk_internal_vm_StackChunk::set_cont(oop ref, oop value) {
280   ref->obj_field_put(_cont_offset, value);
281 }
282 
283 template<typename P>
284 inline oop jdk_internal_vm_StackChunk::cont_raw(oop ref) {
285   return (oop)RawAccess<>::oop_load(ref->field_addr<P>(_cont_offset));
286 }
287 
288 template<typename P>
289 inline void jdk_internal_vm_StackChunk::set_cont_raw(oop ref, oop value) {
290   RawAccess<IS_DEST_UNINITIALIZED>::oop_store(ref->field_addr<P>(_cont_offset), value);
291 }
292 
293 inline jint jdk_internal_vm_StackChunk::size(oop ref) {
294   return ref->int_field(_size_offset);
295 }
296 inline void jdk_internal_vm_StackChunk::set_size(HeapWord* ref, jint value) {
297   *(cast_to_oop(ref))->field_addr<jint>(_size_offset) = value; // ref->int_field_put(_size_offset, value);
298 }
299 inline jint jdk_internal_vm_StackChunk::sp(oop ref) {
300   return ref->int_field(_sp_offset);
301 }
302 inline void jdk_internal_vm_StackChunk::set_sp(oop ref, jint value) {
303   ref->int_field_put(_sp_offset, value);
304 }
305 inline jlong jdk_internal_vm_StackChunk::pc(oop ref) {
306   return ref->long_field(_pc_offset);
307 }
308 inline void jdk_internal_vm_StackChunk::set_pc(oop ref, jlong value) {
309   ref->long_field_put(_pc_offset, value);
310 }
311 inline jint jdk_internal_vm_StackChunk::argsize(oop ref) {
312   return ref->int_field(_argsize_offset);
313 }
314 inline void jdk_internal_vm_StackChunk::set_argsize(oop ref, jint value) {
315   ref->int_field_put(_argsize_offset, value);
316 }
317 inline jbyte jdk_internal_vm_StackChunk::flags(oop ref) {
318   return ref->byte_field(_flags_offset);
319 }
320 inline void jdk_internal_vm_StackChunk::set_flags(oop ref, jbyte value) {
321   ref->byte_field_put(_flags_offset, value);
322 }
323 inline jint jdk_internal_vm_StackChunk::gc_sp(oop ref) {
324   return ref->int_field(_gcSP_offset);
325 }
326 inline void jdk_internal_vm_StackChunk::set_gc_sp(oop ref, jint value) {
327   ref->int_field_put(_gcSP_offset, value);
328 }
329 inline jlong jdk_internal_vm_StackChunk::mark_cycle(oop ref) {
330   return ref->long_field(_markCycle_offset);
331 }
332 inline void jdk_internal_vm_StackChunk::set_mark_cycle(oop ref, jlong value) {
333   ref->long_field_put(_markCycle_offset, value);
334 }
335 inline jint jdk_internal_vm_StackChunk::maxSize(oop ref) {
336   return ref->int_field(_maxSize_offset);
337 }
338 inline void jdk_internal_vm_StackChunk::set_maxSize(oop ref, jint value) {
339   ref->int_field_put(_maxSize_offset, value);
340 }
341 inline jint jdk_internal_vm_StackChunk::numFrames(oop ref) {
342   return ref->int_field(_numFrames_offset);
343 }
344 inline void jdk_internal_vm_StackChunk::set_numFrames(oop ref, jint value) {
345   ref->int_field_put(_numFrames_offset, value);
346 }
347 inline jint jdk_internal_vm_StackChunk::numOops(oop ref) {
348   return ref->int_field(_numOops_offset);
349 }
350 inline void jdk_internal_vm_StackChunk::set_numOops(oop ref, jint value) {
351   ref->int_field_put(_numOops_offset, value);
352 }
353 
354 inline void java_lang_invoke_CallSite::set_target_volatile(oop site, oop target) {
355   site->obj_field_put_volatile(_target_offset, target);
356 }
357 
358 inline oop  java_lang_invoke_CallSite::target(oop site) {
359   return site->obj_field(_target_offset);
360 }
361 
362 inline void java_lang_invoke_CallSite::set_target(oop site, oop target) {
363   site->obj_field_put(_target_offset, target);
364 }
365 
366 inline bool java_lang_invoke_CallSite::is_instance(oop obj) {
367   return obj != NULL && is_subclass(obj->klass());
368 }
369 
370 inline jboolean java_lang_invoke_ConstantCallSite::is_frozen(oop site) {
371   return site->bool_field(_is_frozen_offset);
372 }
373 
374 inline bool java_lang_invoke_ConstantCallSite::is_instance(oop obj) {
375   return obj != NULL && is_subclass(obj->klass());
376 }
377 
378 inline bool java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(oop obj) {
379   return obj != NULL && is_subclass(obj->klass());
380 }
381 
382 inline bool java_lang_invoke_MemberName::is_instance(oop obj) {
383   return obj != NULL && obj->klass() == vmClasses::MemberName_klass();
384 }
385 
386 inline bool java_lang_invoke_ResolvedMethodName::is_instance(oop obj) {
387   return obj != NULL && obj->klass() == vmClasses::ResolvedMethodName_klass();
388 }
389 
390 inline bool java_lang_invoke_MethodType::is_instance(oop obj) {
391   return obj != NULL && obj->klass() == vmClasses::MethodType_klass();
392 }
393 
394 inline bool java_lang_invoke_MethodHandle::is_instance(oop obj) {
395   return obj != NULL && is_subclass(obj->klass());
396 }
397 
398 inline bool java_lang_Class::is_instance(oop obj) {
399   return obj != NULL && obj->klass() == vmClasses::Class_klass();
400 }
401 
402 inline Klass* java_lang_Class::as_Klass(oop java_class) {
403   //%note memory_2
404   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
405   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
406   assert(k == NULL || k->is_klass(), "type check");
407   return k;
408 }
409 
410 inline bool java_lang_Class::is_primitive(oop java_class) {
411   // should assert:
412   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
413   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
414 
415 #ifdef ASSERT
416   if (is_primitive) {
417     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
418     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
419         "Should be either the T_VOID primitive or a java primitive");
420   }
421 #endif
422 
423   return is_primitive;
424 }
425 
426 inline int java_lang_Class::oop_size(oop java_class) {
427   assert(_oop_size_offset != 0, "must be set");
428   int size = java_class->int_field(_oop_size_offset);
429   assert(size > 0, "Oop size must be greater than zero, not %d", size);
430   return size;
431 }
432 
433 inline bool java_lang_invoke_DirectMethodHandle::is_instance(oop obj) {
434   return obj != NULL && is_subclass(obj->klass());
435 }
436 
437 inline bool java_lang_Module::is_instance(oop obj) {
438   return obj != NULL && obj->klass() == vmClasses::Module_klass();
439 }
440 
441 inline int Backtrace::merge_bci_and_version(int bci, int version) {
442   // only store u2 for version, checking for overflow.
443   if (version > USHRT_MAX || version < 0) version = USHRT_MAX;
444   assert((jushort)bci == bci, "bci should be short");
445   return build_int_from_shorts(version, bci);
446 }
447 
448 inline int Backtrace::merge_mid_and_cpref(int mid, int cpref) {
449   // only store u2 for mid and cpref, checking for overflow.
450   assert((jushort)mid == mid, "mid should be short");
451   assert((jushort)cpref == cpref, "cpref should be short");
452   return build_int_from_shorts(cpref, mid);
453 }
454 
455 inline int Backtrace::bci_at(unsigned int merged) {
456   return extract_high_short_from_int(merged);
457 }
458 
459 inline int Backtrace::version_at(unsigned int merged) {
460   return extract_low_short_from_int(merged);
461 }
462 
463 inline int Backtrace::mid_at(unsigned int merged) {
464   return extract_high_short_from_int(merged);
465 }
466 
467 inline int Backtrace::cpref_at(unsigned int merged) {
468   return extract_low_short_from_int(merged);
469 }
470 
471 inline int Backtrace::get_line_number(Method* method, int bci) {
472   int line_number = 0;
473   if (method->is_native()) {
474     // Negative value different from -1 below, enabling Java code in
475     // class java.lang.StackTraceElement to distinguish "native" from
476     // "no LineNumberTable".  JDK tests for -2.
477     line_number = -2;
478   } else {
479     // Returns -1 if no LineNumberTable, and otherwise actual line number
480     line_number = method->line_number_from_bci(bci);
481   }
482   return line_number;
483 }
484 
485 inline Symbol* Backtrace::get_source_file_name(InstanceKlass* holder, int version) {
486   // RedefineClasses() currently permits redefine operations to
487   // happen in parallel using a "last one wins" philosophy. That
488   // spec laxness allows the constant pool entry associated with
489   // the source_file_name_index for any older constant pool version
490   // to be unstable so we shouldn't try to use it.
491   if (holder->constants()->version() != version) {
492     return NULL;
493   } else {
494     return holder->source_file_name();
495   }
496 }
497 
498 #endif // SHARE_CLASSFILE_JAVACLASSES_INLINE_HPP