1 /*
  2  * Copyright (c) 1997, 2017, 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 "classfile/altHashing.hpp"
 27 #include "classfile/javaClasses.inline.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "oops/access.inline.hpp"
 30 #include "oops/oop.inline.hpp"
 31 #include "oops/verifyOopClosure.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/thread.inline.hpp"
 34 #include "utilities/copy.hpp"
 35 #if INCLUDE_ALL_GCS
 36 #include "gc/g1/g1Allocator.inline.hpp"
 37 #endif
 38 
 39 bool always_do_update_barrier = false;
 40 
 41 bool oopDesc::equals(oop o1, oop o2) {
 42   return Access<>::equals(o1, o2);
 43 }
 44 
 45 void oopDesc::print_on(outputStream* st) const {
 46   if (this == NULL) {
 47     st->print_cr("NULL");
 48   } else {
 49     klass()->oop_print_on(oop(this), st);
 50   }
 51 }
 52 
 53 void oopDesc::print_address_on(outputStream* st) const {
 54   st->print("{" INTPTR_FORMAT "}", p2i(this));
 55 
 56 }
 57 
 58 void oopDesc::print()         { print_on(tty);         }
 59 
 60 void oopDesc::print_address() { print_address_on(tty); }
 61 
 62 char* oopDesc::print_string() {
 63   stringStream st;
 64   print_on(&st);
 65   return st.as_string();
 66 }
 67 
 68 void oopDesc::print_value() {
 69   print_value_on(tty);
 70 }
 71 
 72 char* oopDesc::print_value_string() {
 73   char buf[100];
 74   stringStream st(buf, sizeof(buf));
 75   print_value_on(&st);
 76   return st.as_string();
 77 }
 78 
 79 void oopDesc::print_value_on(outputStream* st) const {
 80   oop obj = oop(this);
 81   if (this == NULL) {
 82     st->print("NULL");
 83   } else if (java_lang_String::is_instance(obj)) {
 84     java_lang_String::print(obj, st);
 85     print_address_on(st);
 86   } else {
 87     klass()->oop_print_value_on(obj, st);
 88   }
 89 }
 90 
 91 
 92 void oopDesc::verify_on(outputStream* st) {
 93   if (this != NULL) {
 94     klass()->oop_verify_on(this, st);
 95   }
 96 }
 97 
 98 
 99 void oopDesc::verify() {
100   verify_on(tty);
101 }
102 
103 intptr_t oopDesc::slow_identity_hash() {
104   // slow case; we have to acquire the micro lock in order to locate the header
105   Thread* THREAD = Thread::current();
106   ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
107   HandleMark hm(THREAD);
108   Handle object(THREAD, this);
109   return ObjectSynchronizer::identity_hash_value_for(object);
110 }
111 
112 // When String table needs to rehash
113 unsigned int oopDesc::new_hash(juint seed) {
114   EXCEPTION_MARK;
115   ResourceMark rm;
116   int length;
117   jchar* chars = java_lang_String::as_unicode_string(this, length, THREAD);
118   if (chars != NULL) {
119     // Use alternate hashing algorithm on the string
120     return AltHashing::murmur3_32(seed, chars, length);
121   } else {
122     vm_exit_out_of_memory(length, OOM_MALLOC_ERROR, "unable to create Unicode strings for String table rehash");
123     return 0;
124   }
125 }
126 
127 // used only for asserts and guarantees
128 bool oopDesc::is_oop(oop obj, bool ignore_mark_word) {
129   if (!check_obj_alignment(obj)) return false;
130   if (!Universe::heap()->is_in_reserved(obj)) return false;
131   // obj is aligned and accessible in heap
132   if (Universe::heap()->is_in_reserved(obj->klass_or_null())) return false;
133 
134   // Header verification: the mark is typically non-NULL. If we're
135   // at a safepoint, it must not be null.
136   // Outside of a safepoint, the header could be changing (for example,
137   // another thread could be inflating a lock on this object).
138   if (ignore_mark_word) {
139     return true;
140   }
141   if (obj->mark() != NULL) {
142     return true;
143   }
144   return !SafepointSynchronize::is_at_safepoint();
145 }
146 
147 // used only for asserts and guarantees
148 bool oopDesc::is_oop_or_null(oop obj, bool ignore_mark_word) {
149   return obj == NULL ? true : is_oop(obj, ignore_mark_word);
150 }
151 
152 #ifndef PRODUCT
153 // used only for asserts
154 bool oopDesc::is_unlocked_oop() const {
155   if (!Universe::heap()->is_in_reserved(this)) return false;
156   return mark()->is_unlocked();
157 }
158 #endif // PRODUCT
159 
160 VerifyOopClosure VerifyOopClosure::verify_oop;
161 
162 template <class T> void VerifyOopClosure::do_oop_work(T* p) {
163   oop obj = RawAccess<>::oop_load(p);
164   guarantee(oopDesc::is_oop_or_null(obj), "invalid oop: " INTPTR_FORMAT, p2i((oopDesc*) obj));
165 }
166 
167 void VerifyOopClosure::do_oop(oop* p)       { VerifyOopClosure::do_oop_work(p); }
168 void VerifyOopClosure::do_oop(narrowOop* p) { VerifyOopClosure::do_oop_work(p); }
169 
170 // type test operations that doesn't require inclusion of oop.inline.hpp.
171 bool oopDesc::is_instance_noinline()          const { return is_instance();            }
172 bool oopDesc::is_array_noinline()             const { return is_array();               }
173 bool oopDesc::is_objArray_noinline()          const { return is_objArray();            }
174 bool oopDesc::is_typeArray_noinline()         const { return is_typeArray();           }
175 
176 bool oopDesc::has_klass_gap() {
177   // Only has a klass gap when compressed class pointers are used.
178   return UseCompressedClassPointers;
179 }
180 
181 oop oopDesc::obj_field_acquire(int offset) const                      { return HeapAccess<MO_ACQUIRE>::oop_load_at(as_oop(), offset); }
182 
183 void oopDesc::obj_field_put_raw(int offset, oop value)                { RawAccess<>::oop_store_at(as_oop(), offset, value); }
184 void oopDesc::release_obj_field_put(int offset, oop value)            { HeapAccess<MO_RELEASE>::oop_store_at(as_oop(), offset, value); }
185 void oopDesc::obj_field_put_volatile(int offset, oop value)           { HeapAccess<MO_SEQ_CST>::oop_store_at(as_oop(), offset, value); }
186 
187 address oopDesc::address_field(int offset) const                      { return HeapAccess<>::load_at(as_oop(), offset); }
188 address oopDesc::address_field_acquire(int offset) const              { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
189 
190 void oopDesc::address_field_put(int offset, address value)            { HeapAccess<>::store_at(as_oop(), offset, value); }
191 void oopDesc::release_address_field_put(int offset, address value)    { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
192 
193 Metadata* oopDesc::metadata_field(int offset) const                   { return HeapAccess<>::load_at(as_oop(), offset); }
194 void oopDesc::metadata_field_put(int offset, Metadata* value)         { HeapAccess<>::store_at(as_oop(), offset, value); }
195 
196 Metadata* oopDesc::metadata_field_acquire(int offset) const           { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
197 void oopDesc::release_metadata_field_put(int offset, Metadata* value) { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
198 
199 jbyte oopDesc::byte_field_acquire(int offset) const                   { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
200 void oopDesc::release_byte_field_put(int offset, jbyte value)         { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
201 
202 jchar oopDesc::char_field_acquire(int offset) const                   { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
203 void oopDesc::release_char_field_put(int offset, jchar value)         { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
204 
205 jboolean oopDesc::bool_field_acquire(int offset) const                { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
206 void oopDesc::release_bool_field_put(int offset, jboolean value)      { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, jboolean(value & 1)); }
207 
208 jint oopDesc::int_field_acquire(int offset) const                     { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
209 void oopDesc::release_int_field_put(int offset, jint value)           { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
210 
211 jshort oopDesc::short_field_acquire(int offset) const                 { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
212 void oopDesc::release_short_field_put(int offset, jshort value)       { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
213 
214 jlong oopDesc::long_field_acquire(int offset) const                   { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
215 void oopDesc::release_long_field_put(int offset, jlong value)         { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
216 
217 jfloat oopDesc::float_field_acquire(int offset) const                 { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
218 void oopDesc::release_float_field_put(int offset, jfloat value)       { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
219 
220 jdouble oopDesc::double_field_acquire(int offset) const               { return HeapAccess<MO_ACQUIRE>::load_at(as_oop(), offset); }
221 void oopDesc::release_double_field_put(int offset, jdouble value)     { HeapAccess<MO_RELEASE>::store_at(as_oop(), offset, value); }
222 
223 #if INCLUDE_CDS_JAVA_HEAP
224 bool oopDesc::is_archive_object(oop p) {
225   return (p == NULL) ? false : G1ArchiveAllocator::is_archive_object(p);
226 }
227 #endif