1 /*
  2  * Copyright (c) 1997, 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 #include "precompiled.hpp"
 26 #include "memory/allocation.inline.hpp"
 27 #include "oops/constantPool.hpp"
 28 #include "oops/method.hpp"
 29 #include "oops/oop.inline.hpp"
 30 #include "oops/inlineKlass.hpp"
 31 #include "runtime/atomic.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/thread.inline.hpp"
 34 
 35 #ifdef ASSERT
 36 oop* HandleArea::allocate_handle(oop obj) {
 37   assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
 38   assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
 39   assert(oopDesc::is_oop(obj), "not an oop: " INTPTR_FORMAT, p2i(obj));
 40   return real_allocate_handle(obj);
 41 }
 42 #endif
 43 
 44 // Copy constructors and destructors for metadata handles
 45 // These do too much to inline.
 46 #define DEF_METADATA_HANDLE_FN_NOINLINE(name, type) \
 47 name##Handle::name##Handle(const name##Handle &h) {                    \
 48   _value = h._value;                                                   \
 49   if (_value != NULL) {                                                \
 50     assert(_value->is_valid(), "obj is valid");                        \
 51     if (h._thread != NULL) {                                           \
 52       assert(h._thread == Thread::current(), "thread must be current");\
 53       _thread = h._thread;                                             \
 54     } else {                                                           \
 55       _thread = Thread::current();                                     \
 56     }                                                                  \
 57     assert(_thread->is_in_live_stack((address)this), "not on stack?"); \
 58     _thread->metadata_handles()->push((Metadata*)_value);              \
 59   } else {                                                             \
 60     _thread = NULL;                                                    \
 61   }                                                                    \
 62 }                                                                      \
 63 name##Handle& name##Handle::operator=(const name##Handle &s) {         \
 64   remove();                                                            \
 65   _value = s._value;                                                   \
 66   if (_value != NULL) {                                                \
 67     assert(_value->is_valid(), "obj is valid");                        \
 68     if (s._thread != NULL) {                                           \
 69       assert(s._thread == Thread::current(), "thread must be current");\
 70       _thread = s._thread;                                             \
 71     } else {                                                           \
 72       _thread = Thread::current();                                     \
 73     }                                                                  \
 74     assert(_thread->is_in_live_stack((address)this), "not on stack?"); \
 75     _thread->metadata_handles()->push((Metadata*)_value);              \
 76   } else {                                                             \
 77     _thread = NULL;                                                    \
 78   }                                                                    \
 79   return *this;                                                        \
 80 }                                                                      \
 81 inline void name##Handle::remove() {                                   \
 82   if (_value != NULL) {                                                \
 83     int i = _thread->metadata_handles()->find_from_end((Metadata*)_value); \
 84     assert(i!=-1, "not in metadata_handles list");                     \
 85     _thread->metadata_handles()->remove_at(i);                         \
 86   }                                                                    \
 87 }                                                                      \
 88 name##Handle::~name##Handle () { remove(); }                           \
 89 
 90 DEF_METADATA_HANDLE_FN_NOINLINE(method, Method)
 91 DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool)
 92 
 93 
 94 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
 95   oop* bottom = (oop*) chunk->bottom();
 96   oop* top    = (oop*) chunk_top;
 97   uintx handles_visited = top - bottom;
 98   assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
 99   // during GC phase 3, a handle may be a forward pointer that
100   // is not yet valid, so loosen the assertion
101   while (bottom < top) {
102     f->do_oop(bottom++);
103   }
104   return handles_visited;
105 }
106 
107 void HandleArea::oops_do(OopClosure* f) {
108   uintx handles_visited = 0;
109   // First handle the current chunk. It is filled to the high water mark.
110   handles_visited += chunk_oops_do(f, _chunk, _hwm);
111   // Then handle all previous chunks. They are completely filled.
112   Chunk* k = _first;
113   while(k != _chunk) {
114     handles_visited += chunk_oops_do(f, k, k->top());
115     k = k->next();
116   }
117 
118   if (_prev != NULL) _prev->oops_do(f);
119 }
120 
121 void HandleMark::initialize(Thread* thread) {
122   _thread = thread;  // Not the current thread during thread creation.
123   // Save area
124   _area  = thread->handle_area();
125   // Save current top
126   _chunk = _area->_chunk;
127   _hwm   = _area->_hwm;
128   _max   = _area->_max;
129   _size_in_bytes = _area->_size_in_bytes;
130   debug_only(_area->_handle_mark_nesting++);
131   assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
132 
133   // Link this in the thread
134   set_previous_handle_mark(thread->last_handle_mark());
135   thread->set_last_handle_mark(this);
136 }
137 
138 HandleMark::~HandleMark() {
139   assert(_area == _thread->handle_area(), "sanity check");
140   assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
141 
142   pop_and_restore();
143 #ifdef ASSERT
144   // clear out first chunk (to detect allocation bugs)
145   if (ZapVMHandleArea) {
146     memset(_hwm, badHandleValue, _max - _hwm);
147   }
148 #endif
149 
150   // Unlink this from the thread
151   _thread->set_last_handle_mark(previous_handle_mark());
152 }
153 
154 void HandleMark::chop_later_chunks() {
155   // reset arena size before delete chunks. Otherwise, the total
156   // arena size could exceed total chunk size
157   _area->set_size_in_bytes(size_in_bytes());
158   _chunk->next_chop();
159 }
160 
161 void* HandleMark::operator new(size_t size) throw() {
162   return AllocateHeap(size, mtThread);
163 }
164 
165 void* HandleMark::operator new [] (size_t size) throw() {
166   return AllocateHeap(size, mtThread);
167 }
168 
169 void HandleMark::operator delete(void* p) {
170   FreeHeap(p);
171 }
172 
173 void HandleMark::operator delete[](void* p) {
174   FreeHeap(p);
175 }
176 
177 #ifdef ASSERT
178 
179 NoHandleMark::NoHandleMark() {
180   HandleArea* area = Thread::current()->handle_area();
181   area->_no_handle_mark_nesting++;
182   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
183 }
184 
185 
186 NoHandleMark::~NoHandleMark() {
187   HandleArea* area = Thread::current()->handle_area();
188   assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
189   area->_no_handle_mark_nesting--;
190 }
191 
192 
193 ResetNoHandleMark::ResetNoHandleMark() {
194   HandleArea* area = Thread::current()->handle_area();
195   _no_handle_mark_nesting = area->_no_handle_mark_nesting;
196   area->_no_handle_mark_nesting = 0;
197 }
198 
199 
200 ResetNoHandleMark::~ResetNoHandleMark() {
201   HandleArea* area = Thread::current()->handle_area();
202   area->_no_handle_mark_nesting = _no_handle_mark_nesting;
203 }
204 
205 #endif // ASSERT