1 /*
  2  * Copyright (c) 1997, 2023, 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 #include "precompiled.hpp"
 26 #include "cds/cdsConfig.hpp"
 27 #include "classfile/altHashing.hpp"
 28 #include "classfile/javaClasses.inline.hpp"
 29 #include "gc/shared/collectedHeap.inline.hpp"
 30 #include "gc/shared/gc_globals.hpp"
 31 #include "memory/resourceArea.hpp"
 32 #include "memory/universe.hpp"
 33 #include "oops/access.inline.hpp"
 34 #include "oops/compressedKlass.inline.hpp"
 35 #include "oops/compressedOops.inline.hpp"
 36 #include "oops/oop.inline.hpp"
 37 #include "oops/verifyOopClosure.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/javaThread.hpp"
 40 #include "runtime/synchronizer.hpp"
 41 #include "utilities/macros.hpp"
 42 
 43 void oopDesc::print_on(outputStream* st) const {
 44   if (*((juint*)this) == badHeapWordVal) {
 45     st->print_cr("BAD WORD");
 46   } else if (*((juint*)this) == badMetaWordVal) {
 47     st->print_cr("BAD META WORD");
 48   } else {
 49     klass()->oop_print_on(cast_to_oop(this), st);
 50   }
 51 }
 52 
 53 void oopDesc::print_address_on(outputStream* st) const {
 54   st->print("{" PTR_FORMAT "}", p2i(this));
 55 
 56 }
 57 
 58 void oopDesc::print_name_on(outputStream* st) const {
 59   if (*((juint*)this) == badHeapWordVal) {
 60     st->print_cr("BAD WORD");
 61   } else if (*((juint*)this) == badMetaWordVal) {
 62     st->print_cr("BAD META WORD");
 63   } else {
 64     st->print_cr("%s", klass()->external_name());
 65   }
 66 }
 67 
 68 void oopDesc::print()         { print_on(tty);         }
 69 
 70 void oopDesc::print_address() { print_address_on(tty); }
 71 
 72 char* oopDesc::print_string() {
 73   stringStream st;
 74   print_on(&st);
 75   return st.as_string();
 76 }
 77 
 78 void oopDesc::print_value() {
 79   print_value_on(tty);
 80 }
 81 
 82 char* oopDesc::print_value_string() {
 83   char buf[100];
 84   stringStream st(buf, sizeof(buf));
 85   print_value_on(&st);
 86   return st.as_string();
 87 }
 88 
 89 void oopDesc::print_value_on(outputStream* st) const {
 90   oop obj = const_cast<oopDesc*>(this);
 91   if (java_lang_String::is_instance(obj)) {
 92     java_lang_String::print(obj, st);
 93     print_address_on(st);
 94   } else {
 95     klass()->oop_print_value_on(obj, st);
 96   }
 97 }
 98 
 99 
100 void oopDesc::verify_on(outputStream* st, oopDesc* oop_desc) {
101   if (oop_desc != nullptr) {
102     oop_desc->klass()->oop_verify_on(oop_desc, st);
103   }
104 }
105 
106 
107 void oopDesc::verify(oopDesc* oop_desc) {
108   verify_on(tty, oop_desc);
109 }
110 
111 intptr_t oopDesc::slow_identity_hash() {
112   // slow case; we have to acquire the micro lock in order to locate the header
113   Thread* current = Thread::current();
114   return ObjectSynchronizer::FastHashCode(current, this);
115 }
116 
117 // used only for asserts and guarantees
118 bool oopDesc::is_oop(oop obj, bool ignore_mark_word) {
119   if (!Universe::heap()->is_oop(obj)) {
120     return false;
121   }
122 
123   // Header verification: the mark is typically non-zero. If we're
124   // at a safepoint, it must not be zero, except when using the new lightweight locking.
125   // Outside of a safepoint, the header could be changing (for example,
126   // another thread could be inflating a lock on this object).
127   if (ignore_mark_word) {
128     return true;
129   }
130   if (obj->mark().value() != 0) {
131     return true;
132   }
133   return LockingMode == LM_LIGHTWEIGHT || !SafepointSynchronize::is_at_safepoint();
134 }
135 
136 // used only for asserts and guarantees
137 bool oopDesc::is_oop_or_null(oop obj, bool ignore_mark_word) {
138   return obj == nullptr ? true : is_oop(obj, ignore_mark_word);
139 }
140 
141 VerifyOopClosure VerifyOopClosure::verify_oop;
142 
143 template <class T> void VerifyOopClosure::do_oop_work(T* p) {
144   oop obj = RawAccess<>::oop_load(p);
145   guarantee(oopDesc::is_oop_or_null(obj), "invalid oop: " PTR_FORMAT, p2i(obj));
146 }
147 
148 void VerifyOopClosure::do_oop(oop* p)       { VerifyOopClosure::do_oop_work(p); }
149 void VerifyOopClosure::do_oop(narrowOop* p) { VerifyOopClosure::do_oop_work(p); }
150 
151 // type test operations that doesn't require inclusion of oop.inline.hpp.
152 bool oopDesc::is_instance_noinline()    const { return is_instance();    }
153 bool oopDesc::is_instanceRef_noinline() const { return is_instanceRef(); }
154 bool oopDesc::is_stackChunk_noinline()  const { return is_stackChunk();  }
155 bool oopDesc::is_array_noinline()       const { return is_array();       }
156 bool oopDesc::is_objArray_noinline()    const { return is_objArray();    }
157 bool oopDesc::is_typeArray_noinline()   const { return is_typeArray();   }
158 
159 bool oopDesc::has_klass_gap() {
160   // Only has a klass gap when compressed class pointers are used.
161   // Except when using compact headers.
162   return UseCompressedClassPointers && !UseCompactObjectHeaders;
163 }
164 
165 #if INCLUDE_CDS_JAVA_HEAP
166 void oopDesc::set_narrow_klass(narrowKlass nk) {
167   assert(CDSConfig::is_dumping_heap(), "Used by CDS only. Do not abuse!");
168   assert(UseCompressedClassPointers, "must be");
169   _metadata._compressed_klass = nk;
170 }
171 #endif
172 
173 void* oopDesc::load_klass_raw(oop obj) {
174   if (UseCompactObjectHeaders) {
175     return obj->klass();
176   } else if (UseCompressedClassPointers) {
177     narrowKlass narrow_klass = obj->_metadata._compressed_klass;
178     if (narrow_klass == 0) return nullptr;
179     return (void*)CompressedKlassPointers::decode_raw(narrow_klass);
180   } else {
181     return obj->_metadata._klass;
182   }
183 }
184 
185 void* oopDesc::load_oop_raw(oop obj, int offset) {
186   uintptr_t addr = (uintptr_t)(void*)obj + (uint)offset;
187   if (UseCompressedOops) {
188     narrowOop narrow_oop = *(narrowOop*)addr;
189     if (CompressedOops::is_null(narrow_oop)) return nullptr;
190     return (void*)CompressedOops::decode_raw(narrow_oop);
191   } else {
192     return *(void**)addr;
193   }
194 }
195 
196 oop oopDesc::obj_field_acquire(int offset) const                      { return HeapAccess<MO_ACQUIRE>::oop_load_at(as_oop(), offset); }
197 
198 void oopDesc::obj_field_put_raw(int offset, oop value)                { assert(!(UseZGC && ZGenerational), "Generational ZGC must use store barriers");
199                                                                         RawAccess<>::oop_store_at(as_oop(), offset, value); }
200 void oopDesc::release_obj_field_put(int offset, oop value)            { HeapAccess<MO_RELEASE>::oop_store_at(as_oop(), offset, value); }
201 void oopDesc::obj_field_put_volatile(int offset, oop value)           { HeapAccess<MO_SEQ_CST>::oop_store_at(as_oop(), offset, value); }
202 
203 address oopDesc::address_field(int offset) const                      { return *field_addr<address>(offset); }
204 address oopDesc::address_field_acquire(int offset) const              { return Atomic::load_acquire(field_addr<address>(offset)); }
205 
206 void oopDesc::address_field_put(int offset, address value)            { *field_addr<address>(offset) = value; }
207 void oopDesc::release_address_field_put(int offset, address value)    { Atomic::release_store(field_addr<address>(offset), value); }
208 
209 Metadata* oopDesc::metadata_field(int offset) const                   { return *field_addr<Metadata*>(offset); }
210 void oopDesc::metadata_field_put(int offset, Metadata* value)         { *field_addr<Metadata*>(offset) = value; }
211 
212 Metadata* oopDesc::metadata_field_acquire(int offset) const           { return Atomic::load_acquire(field_addr<Metadata*>(offset)); }
213 void oopDesc::release_metadata_field_put(int offset, Metadata* value) { Atomic::release_store(field_addr<Metadata*>(offset), value); }
214 
215 jbyte oopDesc::byte_field_acquire(int offset) const                   { return Atomic::load_acquire(field_addr<jbyte>(offset)); }
216 void oopDesc::release_byte_field_put(int offset, jbyte value)         { Atomic::release_store(field_addr<jbyte>(offset), value); }
217 
218 jchar oopDesc::char_field_acquire(int offset) const                   { return Atomic::load_acquire(field_addr<jchar>(offset)); }
219 void oopDesc::release_char_field_put(int offset, jchar value)         { Atomic::release_store(field_addr<jchar>(offset), value); }
220 
221 jboolean oopDesc::bool_field_acquire(int offset) const                { return Atomic::load_acquire(field_addr<jboolean>(offset)); }
222 void oopDesc::release_bool_field_put(int offset, jboolean value)      { Atomic::release_store(field_addr<jboolean>(offset), jboolean(value & 1)); }
223 
224 jint oopDesc::int_field_acquire(int offset) const                     { return Atomic::load_acquire(field_addr<jint>(offset)); }
225 void oopDesc::release_int_field_put(int offset, jint value)           { Atomic::release_store(field_addr<jint>(offset), value); }
226 
227 jshort oopDesc::short_field_acquire(int offset) const                 { return Atomic::load_acquire(field_addr<jshort>(offset)); }
228 void oopDesc::release_short_field_put(int offset, jshort value)       { Atomic::release_store(field_addr<jshort>(offset), value); }
229 
230 jlong oopDesc::long_field_acquire(int offset) const                   { return Atomic::load_acquire(field_addr<jlong>(offset)); }
231 void oopDesc::release_long_field_put(int offset, jlong value)         { Atomic::release_store(field_addr<jlong>(offset), value); }
232 
233 jfloat oopDesc::float_field_acquire(int offset) const                 { return Atomic::load_acquire(field_addr<jfloat>(offset)); }
234 void oopDesc::release_float_field_put(int offset, jfloat value)       { Atomic::release_store(field_addr<jfloat>(offset), value); }
235 
236 jdouble oopDesc::double_field_acquire(int offset) const               { return Atomic::load_acquire(field_addr<jdouble>(offset)); }
237 void oopDesc::release_double_field_put(int offset, jdouble value)     { Atomic::release_store(field_addr<jdouble>(offset), value); }
238 
239 #ifdef ASSERT
240 bool oopDesc::size_might_change() {
241   // UseParallelGC and UseG1GC can change the length field
242   // of an "old copy" of an object array in the young gen so it indicates
243   // the grey portion of an already copied array. This will cause the first
244   // disjunct below to fail if the two comparands are computed across such
245   // a concurrent change.
246   return Universe::heap()->is_gc_active() && is_objArray() && is_forwarded() && (UseParallelGC || UseG1GC);
247 }
248 #endif