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