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