1 /*
  2  * Copyright (c) 1997, 2019, 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.hpp"
 27 #include "memory/allocation.inline.hpp"
 28 #include "memory/arena.hpp"
 29 #include "memory/metaspaceShared.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "runtime/atomic.hpp"
 32 #include "runtime/os.hpp"
 33 #include "runtime/task.hpp"
 34 #include "runtime/threadCritical.hpp"
 35 #include "services/memTracker.hpp"
 36 #include "utilities/ostream.hpp"
 37 
 38 // allocate using malloc; will fail if no memory available
 39 char* AllocateHeap(size_t size,
 40                    MEMFLAGS flags,
 41                    const NativeCallStack& stack,
 42                    AllocFailType alloc_failmode /* = AllocFailStrategy::EXIT_OOM*/) {
 43   char* p = (char*) os::malloc(size, flags, stack);
 44   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
 45     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
 46   }
 47   return p;
 48 }
 49 
 50 char* AllocateHeap(size_t size,
 51                    MEMFLAGS flags,
 52                    AllocFailType alloc_failmode /* = AllocFailStrategy::EXIT_OOM*/) {
 53   return AllocateHeap(size, flags, CALLER_PC);
 54 }
 55 
 56 char* ReallocateHeap(char *old,
 57                      size_t size,
 58                      MEMFLAGS flag,
 59                      AllocFailType alloc_failmode) {
 60   char* p = (char*) os::realloc(old, size, flag, CALLER_PC);
 61   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
 62     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
 63   }
 64   return p;
 65 }
 66 
 67 // handles NULL pointers
 68 void FreeHeap(void* p) {
 69   os::free(p);
 70 }
 71 
 72 void* MetaspaceObj::_shared_metaspace_base = NULL;
 73 void* MetaspaceObj::_shared_metaspace_top  = NULL;
 74 
 75 void* StackObj::operator new(size_t size)     throw() { ShouldNotCallThis(); return 0; }
 76 void  StackObj::operator delete(void* p)              { ShouldNotCallThis(); }
 77 void* StackObj::operator new [](size_t size)  throw() { ShouldNotCallThis(); return 0; }
 78 void  StackObj::operator delete [](void* p)           { ShouldNotCallThis(); }
 79 
 80 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
 81                                  size_t word_size,
 82                                  MetaspaceObj::Type type, TRAPS) throw() {
 83   // Klass has it's own operator new
 84   return Metaspace::allocate(loader_data, word_size, type, THREAD);
 85 }
 86 
 87 bool MetaspaceObj::is_valid(const MetaspaceObj* p) {
 88   // Weed out obvious bogus values first without traversing metaspace
 89   if ((size_t)p < os::min_page_size()) {
 90     return false;
 91   } else if (!is_aligned((address)p, sizeof(MetaWord))) {
 92     return false;
 93   }
 94   return Metaspace::contains((void*)p);
 95 }
 96 
 97 void MetaspaceObj::print_address_on(outputStream* st) const {
 98   st->print(" {" INTPTR_FORMAT "}", p2i(this));
 99 }
100 
101 void* ResourceObj::operator new(size_t size, Arena *arena) throw() {
102   address res = (address)arena->Amalloc(size);
103   DEBUG_ONLY(set_allocation_type(res, ARENA);)
104   return res;
105 }
106 
107 void* ResourceObj::operator new [](size_t size, Arena *arena) throw() {
108   address res = (address)arena->Amalloc(size);
109   DEBUG_ONLY(set_allocation_type(res, ARENA);)
110   return res;
111 }
112 
113 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) throw() {
114   address res = NULL;
115   switch (type) {
116    case C_HEAP:
117     res = (address)AllocateHeap(size, flags, CALLER_PC);
118     DEBUG_ONLY(set_allocation_type(res, C_HEAP);)
119     break;
120    case RESOURCE_AREA:
121     // new(size) sets allocation type RESOURCE_AREA.
122     res = (address)operator new(size);
123     break;
124    default:
125     ShouldNotReachHere();
126   }
127   return res;
128 }
129 
130 void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw() {
131   return (address) operator new(size, type, flags);
132 }
133 
134 void* ResourceObj::operator new(size_t size, const std::nothrow_t&  nothrow_constant,
135     allocation_type type, MEMFLAGS flags) throw() {
136   // should only call this with std::nothrow, use other operator new() otherwise
137   address res = NULL;
138   switch (type) {
139    case C_HEAP:
140     res = (address)AllocateHeap(size, flags, CALLER_PC, AllocFailStrategy::RETURN_NULL);
141     DEBUG_ONLY(if (res!= NULL) set_allocation_type(res, C_HEAP);)
142     break;
143    case RESOURCE_AREA:
144     // new(size) sets allocation type RESOURCE_AREA.
145     res = (address)operator new(size, std::nothrow);
146     break;
147    default:
148     ShouldNotReachHere();
149   }
150   return res;
151 }
152 
153 void* ResourceObj::operator new [](size_t size, const std::nothrow_t&  nothrow_constant,
154     allocation_type type, MEMFLAGS flags) throw() {
155   return (address)operator new(size, nothrow_constant, type, flags);
156 }
157 
158 void ResourceObj::operator delete(void* p) {
159   assert(((ResourceObj *)p)->allocated_on_C_heap(),
160          "delete only allowed for C_HEAP objects");
161   DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)
162   FreeHeap(p);
163 }
164 
165 void ResourceObj::operator delete [](void* p) {
166   operator delete(p);
167 }
168 
169 #ifdef ASSERT
170 void ResourceObj::set_allocation_type(address res, allocation_type type) {
171   // Set allocation type in the resource object
172   uintptr_t allocation = (uintptr_t)res;
173   assert((allocation & allocation_mask) == 0, "address should be aligned to 4 bytes at least: " INTPTR_FORMAT, p2i(res));
174   assert(type <= allocation_mask, "incorrect allocation type");
175   ResourceObj* resobj = (ResourceObj *)res;
176   resobj->_allocation_t[0] = ~(allocation + type);
177   if (type != STACK_OR_EMBEDDED) {
178     // Called from operator new(), set verification value.
179     resobj->_allocation_t[1] = (uintptr_t)&(resobj->_allocation_t[1]) + type;
180   }
181 }
182 
183 ResourceObj::allocation_type ResourceObj::get_allocation_type() const {
184   assert(~(_allocation_t[0] | allocation_mask) == (uintptr_t)this, "lost resource object");
185   return (allocation_type)((~_allocation_t[0]) & allocation_mask);
186 }
187 
188 bool ResourceObj::is_type_set() const {
189   allocation_type type = (allocation_type)(_allocation_t[1] & allocation_mask);
190   return get_allocation_type()  == type &&
191          (_allocation_t[1] - type) == (uintptr_t)(&_allocation_t[1]);
192 }
193 
194 // This whole business of passing information from ResourceObj::operator new
195 // to the ResourceObj constructor via fields in the "object" is technically UB.
196 // But it seems to work within the limitations of HotSpot usage (such as no
197 // multiple inheritance) with the compilers and compiler options we're using.
198 // And it gives some possibly useful checking for misuse of ResourceObj.
199 void ResourceObj::initialize_allocation_info() {
200   if (~(_allocation_t[0] | allocation_mask) != (uintptr_t)this) {
201     // Operator new() is not called for allocations
202     // on stack and for embedded objects.
203     set_allocation_type((address)this, STACK_OR_EMBEDDED);
204   } else if (allocated_on_stack()) { // STACK_OR_EMBEDDED
205     // For some reason we got a value which resembles
206     // an embedded or stack object (operator new() does not
207     // set such type). Keep it since it is valid value
208     // (even if it was garbage).
209     // Ignore garbage in other fields.
210   } else if (is_type_set()) {
211     // Operator new() was called and type was set.
212     assert(!allocated_on_stack(),
213            "not embedded or stack, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
214            p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]);
215   } else {
216     // Operator new() was not called.
217     // Assume that it is embedded or stack object.
218     set_allocation_type((address)this, STACK_OR_EMBEDDED);
219   }
220   _allocation_t[1] = 0; // Zap verification value
221 }
222 
223 ResourceObj::ResourceObj() {
224   initialize_allocation_info();
225 }
226 
227 ResourceObj::ResourceObj(const ResourceObj&) {
228   // Initialize _allocation_t as a new object, ignoring object being copied.
229   initialize_allocation_info();
230 }
231 
232 ResourceObj& ResourceObj::operator=(const ResourceObj& r) {
233   assert(allocated_on_stack(),
234          "copy only into local, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
235          p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]);
236   // Keep current _allocation_t value;
237   return *this;
238 }
239 
240 ResourceObj::~ResourceObj() {
241   // allocated_on_C_heap() also checks that encoded (in _allocation) address == this.
242   if (!allocated_on_C_heap()) { // ResourceObj::delete() will zap _allocation for C_heap.
243     _allocation_t[0] = (uintptr_t)badHeapOopVal; // zap type
244   }
245 }
246 #endif // ASSERT
247 
248 //--------------------------------------------------------------------------------------
249 // Non-product code
250 
251 #ifndef PRODUCT
252 void AllocatedObj::print() const       { print_on(tty); }
253 void AllocatedObj::print_value() const { print_value_on(tty); }
254 
255 void AllocatedObj::print_on(outputStream* st) const {
256   st->print_cr("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));
257 }
258 
259 void AllocatedObj::print_value_on(outputStream* st) const {
260   st->print("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));
261 }
262 
263 ReallocMark::ReallocMark() {
264 #ifdef ASSERT
265   Thread *thread = Thread::current();
266   _nesting = thread->resource_area()->nesting();
267 #endif
268 }
269 
270 void ReallocMark::check() {
271 #ifdef ASSERT
272   if (_nesting != Thread::current()->resource_area()->nesting()) {
273     fatal("allocation bug: array could grow within nested ResourceMark");
274   }
275 #endif
276 }
277 
278 #endif // Non-product