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