1 /*
  2  * Copyright (c) 2018, 2024, 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/javaClasses.hpp"
 27 #include "classfile/vmClasses.hpp"
 28 #include "gc/shared/allocTracer.hpp"
 29 #include "gc/shared/collectedHeap.hpp"
 30 #include "gc/shared/memAllocator.hpp"
 31 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
 32 #include "gc/shared/tlab_globals.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/arrayOop.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "prims/jvmtiExport.hpp"
 37 #include "runtime/continuationJavaClasses.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/sharedRuntime.hpp"
 40 #include "runtime/javaThread.hpp"
 41 #include "services/lowMemoryDetector.hpp"
 42 #include "utilities/align.hpp"
 43 #include "utilities/copy.hpp"
 44 #include "utilities/globalDefinitions.hpp"
 45 
 46 class MemAllocator::Allocation: StackObj {
 47   friend class MemAllocator;
 48 
 49   const MemAllocator& _allocator;
 50   JavaThread*         _thread;
 51   oop*                _obj_ptr;
 52   bool                _overhead_limit_exceeded;
 53   bool                _allocated_outside_tlab;
 54   size_t              _allocated_tlab_size;
 55   bool                _tlab_end_reset_for_sample;
 56 
 57   bool check_out_of_memory();
 58   void verify_before();
 59   void verify_after();
 60   void notify_allocation();
 61   void notify_allocation_jvmti_sampler();
 62   void notify_allocation_low_memory_detector();
 63   void notify_allocation_jfr_sampler();
 64   void notify_allocation_dtrace_sampler();
 65 #ifdef ASSERT
 66   void check_for_valid_allocation_state() const;
 67 #endif
 68 
 69   class PreserveObj;
 70 
 71 public:
 72   Allocation(const MemAllocator& allocator, oop* obj_ptr)
 73     : _allocator(allocator),
 74       _thread(JavaThread::cast(allocator._thread)), // Do not use Allocation in non-JavaThreads.
 75       _obj_ptr(obj_ptr),
 76       _overhead_limit_exceeded(false),
 77       _allocated_outside_tlab(false),
 78       _allocated_tlab_size(0),
 79       _tlab_end_reset_for_sample(false)
 80   {
 81     assert(Thread::current() == allocator._thread, "do not pass MemAllocator across threads");
 82     verify_before();
 83   }
 84 
 85   ~Allocation() {
 86     if (!check_out_of_memory()) {
 87       notify_allocation();
 88     }
 89   }
 90 
 91   oop obj() const { return *_obj_ptr; }
 92 };
 93 
 94 class MemAllocator::Allocation::PreserveObj: StackObj {
 95   HandleMark _handle_mark;
 96   Handle     _handle;
 97   oop* const _obj_ptr;
 98 
 99 public:
100   PreserveObj(JavaThread* thread, oop* obj_ptr)
101     : _handle_mark(thread),
102       _handle(thread, *obj_ptr),
103       _obj_ptr(obj_ptr)
104   {
105     *obj_ptr = nullptr;
106   }
107 
108   ~PreserveObj() {
109     *_obj_ptr = _handle();
110   }
111 
112   oop operator()() const {
113     return _handle();
114   }
115 };
116 
117 bool MemAllocator::Allocation::check_out_of_memory() {
118   JavaThread* THREAD = _thread; // For exception macros.
119   assert(!HAS_PENDING_EXCEPTION, "Unexpected exception, will result in uninitialized storage");
120 
121   if (obj() != nullptr) {
122     return false;
123   }
124 
125   const char* message = _overhead_limit_exceeded ? "GC overhead limit exceeded" : "Java heap space";
126   if (!_thread->is_in_internal_oome_mark()) {
127     // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
128     report_java_out_of_memory(message);
129     if (JvmtiExport::should_post_resource_exhausted()) {
130       JvmtiExport::post_resource_exhausted(
131         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
132         message);
133     }
134 
135     oop exception = _overhead_limit_exceeded ?
136         Universe::out_of_memory_error_gc_overhead_limit() :
137         Universe::out_of_memory_error_java_heap();
138     THROW_OOP_(exception, true);
139   } else {
140     THROW_OOP_(Universe::out_of_memory_error_java_heap_without_backtrace(), true);
141   }
142 }
143 
144 void MemAllocator::Allocation::verify_before() {
145   // Clear unhandled oops for memory allocation.  Memory allocation might
146   // not take out a lock if from tlab, so clear here.
147   JavaThread* THREAD = _thread; // For exception macros.
148   assert(!HAS_PENDING_EXCEPTION, "Should not allocate with exception pending");
149   debug_only(check_for_valid_allocation_state());
150   assert(!Universe::heap()->is_stw_gc_active(), "Allocation during GC pause not allowed");
151 }
152 
153 #ifdef ASSERT
154 void MemAllocator::Allocation::check_for_valid_allocation_state() const {
155   // How to choose between a pending exception and a potential
156   // OutOfMemoryError?  Don't allow pending exceptions.
157   // This is a VM policy failure, so how do we exhaustively test it?
158   assert(!_thread->has_pending_exception(),
159          "shouldn't be allocating with pending exception");
160   // Allocation of an oop can always invoke a safepoint.
161   _thread->check_for_valid_safepoint_state();
162 }
163 #endif
164 
165 void MemAllocator::Allocation::notify_allocation_jvmti_sampler() {
166   // support for JVMTI VMObjectAlloc event (no-op if not enabled)
167   JvmtiExport::vm_object_alloc_event_collector(obj());
168 
169   if (!JvmtiExport::should_post_sampled_object_alloc()) {
170     // Sampling disabled
171     return;
172   }
173 
174   if (!_allocated_outside_tlab && _allocated_tlab_size == 0 && !_tlab_end_reset_for_sample) {
175     // Sample if it's a non-TLAB allocation, or a TLAB allocation that either refills the TLAB
176     // or expands it due to taking a sampler induced slow path.
177     return;
178   }
179 
180   // If we want to be sampling, protect the allocated object with a Handle
181   // before doing the callback. The callback is done in the destructor of
182   // the JvmtiSampledObjectAllocEventCollector.
183   size_t bytes_since_last = 0;
184 
185   {
186     PreserveObj obj_h(_thread, _obj_ptr);
187     JvmtiSampledObjectAllocEventCollector collector;
188     size_t size_in_bytes = _allocator._word_size * HeapWordSize;
189     ThreadLocalAllocBuffer& tlab = _thread->tlab();
190 
191     if (!_allocated_outside_tlab) {
192       bytes_since_last = tlab.bytes_since_last_sample_point();
193     }
194 
195     _thread->heap_sampler().check_for_sampling(obj_h(), size_in_bytes, bytes_since_last);
196   }
197 
198   if (_tlab_end_reset_for_sample || _allocated_tlab_size != 0) {
199     // Tell tlab to forget bytes_since_last if we passed it to the heap sampler.
200     _thread->tlab().set_sample_end(bytes_since_last != 0);
201   }
202 }
203 
204 void MemAllocator::Allocation::notify_allocation_low_memory_detector() {
205   // support low memory notifications (no-op if not enabled)
206   LowMemoryDetector::detect_low_memory_for_collected_pools();
207 }
208 
209 void MemAllocator::Allocation::notify_allocation_jfr_sampler() {
210   HeapWord* mem = cast_from_oop<HeapWord*>(obj());
211   size_t size_in_bytes = _allocator._word_size * HeapWordSize;
212 
213   if (_allocated_outside_tlab) {
214     AllocTracer::send_allocation_outside_tlab(obj()->klass(), mem, size_in_bytes, _thread);
215   } else if (_allocated_tlab_size != 0) {
216     // TLAB was refilled
217     AllocTracer::send_allocation_in_new_tlab(obj()->klass(), mem, _allocated_tlab_size * HeapWordSize,
218                                              size_in_bytes, _thread);
219   }
220 }
221 
222 void MemAllocator::Allocation::notify_allocation_dtrace_sampler() {
223   if (DTraceAllocProbes) {
224     // support for Dtrace object alloc event (no-op most of the time)
225     Klass* klass = obj()->klass();
226     size_t word_size = _allocator._word_size;
227     if (klass != nullptr && klass->name() != nullptr) {
228       SharedRuntime::dtrace_object_alloc(_thread, obj(), word_size);
229     }
230   }
231 }
232 
233 void MemAllocator::Allocation::notify_allocation() {
234   notify_allocation_low_memory_detector();
235   notify_allocation_jfr_sampler();
236   notify_allocation_dtrace_sampler();
237   notify_allocation_jvmti_sampler();
238 }
239 
240 HeapWord* MemAllocator::mem_allocate_outside_tlab(Allocation& allocation) const {
241   allocation._allocated_outside_tlab = true;
242   HeapWord* mem = Universe::heap()->mem_allocate(_word_size, &allocation._overhead_limit_exceeded);
243   if (mem == nullptr) {
244     return mem;
245   }
246 
247   size_t size_in_bytes = _word_size * HeapWordSize;
248   _thread->incr_allocated_bytes(size_in_bytes);
249 
250   return mem;
251 }
252 
253 HeapWord* MemAllocator::mem_allocate_inside_tlab_fast() const {
254   return _thread->tlab().allocate(_word_size);
255 }
256 
257 HeapWord* MemAllocator::mem_allocate_inside_tlab_slow(Allocation& allocation) const {
258   HeapWord* mem = nullptr;
259   ThreadLocalAllocBuffer& tlab = _thread->tlab();
260 
261   if (JvmtiExport::should_post_sampled_object_alloc()) {
262     tlab.set_back_allocation_end();
263     mem = tlab.allocate(_word_size);
264 
265     // We set back the allocation sample point to try to allocate this, reset it
266     // when done.
267     allocation._tlab_end_reset_for_sample = true;
268 
269     if (mem != nullptr) {
270       return mem;
271     }
272   }
273 
274   // Retain tlab and allocate object in shared space if
275   // the amount free in the tlab is too large to discard.
276   if (tlab.free() > tlab.refill_waste_limit()) {
277     tlab.record_slow_allocation(_word_size);
278     return nullptr;
279   }
280 
281   // Discard tlab and allocate a new one.
282   // To minimize fragmentation, the last TLAB may be smaller than the rest.
283   size_t new_tlab_size = tlab.compute_size(_word_size);
284 
285   tlab.retire_before_allocation();
286 
287   if (new_tlab_size == 0) {
288     return nullptr;
289   }
290 
291   // Allocate a new TLAB requesting new_tlab_size. Any size
292   // between minimal and new_tlab_size is accepted.
293   size_t min_tlab_size = ThreadLocalAllocBuffer::compute_min_size(_word_size);
294   mem = Universe::heap()->allocate_new_tlab(min_tlab_size, new_tlab_size, &allocation._allocated_tlab_size);
295   if (mem == nullptr) {
296     assert(allocation._allocated_tlab_size == 0,
297            "Allocation failed, but actual size was updated. min: " SIZE_FORMAT
298            ", desired: " SIZE_FORMAT ", actual: " SIZE_FORMAT,
299            min_tlab_size, new_tlab_size, allocation._allocated_tlab_size);
300     return nullptr;
301   }
302   assert(allocation._allocated_tlab_size != 0, "Allocation succeeded but actual size not updated. mem at: "
303          PTR_FORMAT " min: " SIZE_FORMAT ", desired: " SIZE_FORMAT,
304          p2i(mem), min_tlab_size, new_tlab_size);
305 
306   // ...and clear or zap just allocated TLAB, if needed.
307   if (ZeroTLAB) {
308     Copy::zero_to_words(mem, allocation._allocated_tlab_size);
309   } else if (ZapTLAB) {
310     // Skip mangling the space corresponding to the object header to
311     // ensure that the returned space is not considered parsable by
312     // any concurrent GC thread.
313     size_t hdr_size = oopDesc::header_size();
314     Copy::fill_to_words(mem + hdr_size, allocation._allocated_tlab_size - hdr_size, badHeapWordVal);
315   }
316 
317   tlab.fill(mem, mem + _word_size, allocation._allocated_tlab_size);
318   return mem;
319 }
320 
321 HeapWord* MemAllocator::mem_allocate(Allocation& allocation) const {
322   if (UseTLAB) {
323     // Try allocating from an existing TLAB.
324     HeapWord* mem = mem_allocate_inside_tlab_fast();
325     if (mem != nullptr) {
326       return mem;
327     }
328   }
329 
330   // Allocation of an oop can always invoke a safepoint.
331   debug_only(allocation._thread->check_for_valid_safepoint_state());
332 
333   if (UseTLAB) {
334     // Try refilling the TLAB and allocating the object in it.
335     HeapWord* mem = mem_allocate_inside_tlab_slow(allocation);
336     if (mem != nullptr) {
337       return mem;
338     }
339   }
340 
341   return mem_allocate_outside_tlab(allocation);
342 }
343 
344 oop MemAllocator::allocate() const {
345   oop obj = nullptr;
346   {
347     Allocation allocation(*this, &obj);
348     HeapWord* mem = mem_allocate(allocation);
349     if (mem != nullptr) {
350       obj = initialize(mem);
351     } else {
352       // The unhandled oop detector will poison local variable obj,
353       // so reset it to null if mem is null.
354       obj = nullptr;
355     }
356   }
357   return obj;
358 }
359 
360 void MemAllocator::mem_clear(HeapWord* mem) const {
361   assert(mem != nullptr, "cannot initialize null object");
362   const size_t hs = oopDesc::header_size();
363   assert(_word_size >= hs, "unexpected object size");
364   oopDesc::set_klass_gap(mem, 0);
365   Copy::fill_to_aligned_words(mem + hs, _word_size - hs);
366 }
367 
368 oop MemAllocator::finish(HeapWord* mem) const {
369   assert(mem != nullptr, "null object pointer");
370   // May be bootstrapping
371   oopDesc::set_mark(mem, markWord::prototype());
372   // Need a release store to ensure array/class length, mark word, and
373   // object zeroing are visible before setting the klass non-null, for
374   // concurrent collectors.
375   oopDesc::release_set_klass(mem, _klass);
376   return cast_to_oop(mem);
377 }
378 
379 oop ObjAllocator::initialize(HeapWord* mem) const {
380   mem_clear(mem);
381   return finish(mem);
382 }
383 
384 oop ObjArrayAllocator::initialize(HeapWord* mem) const {
385   // Set array length before setting the _klass field because a
386   // non-null klass field indicates that the object is parsable by
387   // concurrent GC.
388   assert(_length >= 0, "length should be non-negative");
389   if (_do_zero) {
390     mem_clear(mem);
391     mem_zap_end_padding(mem);
392   }
393   arrayOopDesc::set_length(mem, _length);
394   return finish(mem);
395 }
396 
397 #ifndef PRODUCT
398 void ObjArrayAllocator::mem_zap_end_padding(HeapWord* mem) const {
399   const size_t length_in_bytes = static_cast<size_t>(_length) << ArrayKlass::cast(_klass)->log2_element_size();
400   const BasicType element_type = ArrayKlass::cast(_klass)->element_type();
401   const size_t base_offset_in_bytes = arrayOopDesc::base_offset_in_bytes(element_type);
402   const size_t size_in_bytes = _word_size * BytesPerWord;
403 
404   const address obj_end = reinterpret_cast<address>(mem) + size_in_bytes;
405   const address base = reinterpret_cast<address>(mem) + base_offset_in_bytes;
406   const address elements_end = base + length_in_bytes;
407   assert(elements_end <= obj_end, "payload must fit in object");
408   if (elements_end < obj_end) {
409     const size_t padding_in_bytes = obj_end - elements_end;
410     Copy::fill_to_bytes(elements_end, padding_in_bytes, heapPaddingByteVal);
411   }
412 }
413 #endif
414 
415 oop ClassAllocator::initialize(HeapWord* mem) const {
416   // Set oop_size field before setting the _klass field because a
417   // non-null _klass field indicates that the object is parsable by
418   // concurrent GC.
419   assert(_word_size > 0, "oop_size must be positive.");
420   mem_clear(mem);
421   java_lang_Class::set_oop_size(mem, _word_size);
422   return finish(mem);
423 }