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