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