--- old/src/hotspot/share/classfile/stringTable.cpp 2019-07-17 10:20:05.053484228 -0400 +++ new/src/hotspot/share/classfile/stringTable.cpp 2019-07-17 10:20:04.833484236 -0400 @@ -342,7 +342,7 @@ if (found_string != NULL) { return found_string; } - return do_intern(string_or_null_h, name, len, hash, CHECK_NULL); + return do_intern(string_or_null_h, name, len, hash, THREAD); } oop StringTable::do_intern(Handle string_or_null_h, const jchar* name, --- old/src/hotspot/share/gc/shared/memAllocator.cpp 2019-07-17 10:20:05.377484217 -0400 +++ new/src/hotspot/share/gc/shared/memAllocator.cpp 2019-07-17 10:20:05.165484224 -0400 @@ -44,7 +44,7 @@ const MemAllocator& _allocator; Thread* _thread; - oop* _obj_ptr; + Handle _handle; bool _overhead_limit_exceeded; bool _allocated_outside_tlab; size_t _allocated_tlab_size; @@ -64,13 +64,10 @@ void check_for_valid_allocation_state() const; #endif - class PreserveObj; - public: - Allocation(const MemAllocator& allocator, oop* obj_ptr) + Allocation(const MemAllocator& allocator) : _allocator(allocator), _thread(Thread::current()), - _obj_ptr(obj_ptr), _overhead_limit_exceeded(false), _allocated_outside_tlab(false), _allocated_tlab_size(0), @@ -86,30 +83,8 @@ } } - oop obj() const { return *_obj_ptr; } -}; - -class MemAllocator::Allocation::PreserveObj: StackObj { - HandleMark _handle_mark; - Handle _handle; - oop* const _obj_ptr; - -public: - PreserveObj(Thread* thread, oop* obj_ptr) - : _handle_mark(thread), - _handle(thread, *obj_ptr), - _obj_ptr(obj_ptr) - { - *obj_ptr = NULL; - } - - ~PreserveObj() { - *_obj_ptr = _handle(); - } - - oop operator()() const { - return _handle(); - } + void set_obj(oop obj) { _handle = Handle(_thread, obj); } + oop obj() const { return _handle(); } }; bool MemAllocator::Allocation::check_out_of_memory() { @@ -198,13 +173,12 @@ return; } - // If we want to be sampling, protect the allocated object with a Handle + // If we want to be sampling, the allocated object is protected with a Handle // before doing the callback. The callback is done in the destructor of // the JvmtiSampledObjectAllocEventCollector. size_t bytes_since_last = 0; { - PreserveObj obj_h(_thread, _obj_ptr); JvmtiSampledObjectAllocEventCollector collector; size_t size_in_bytes = _allocator._word_size * HeapWordSize; ThreadLocalAllocBuffer& tlab = _thread->tlab(); @@ -213,7 +187,7 @@ bytes_since_last = tlab.bytes_since_last_sample_point(); } - _thread->heap_sampler().check_for_sampling(obj_h(), size_in_bytes, bytes_since_last); + _thread->heap_sampler().check_for_sampling(obj(), size_in_bytes, bytes_since_last); } if (_tlab_end_reset_for_sample || _allocated_tlab_size != 0) { @@ -364,15 +338,19 @@ } oop MemAllocator::allocate() const { - oop obj = NULL; + HandleMark hm(_thread); + Handle obj_h; { - Allocation allocation(*this, &obj); + Allocation allocation(*this); HeapWord* mem = mem_allocate(allocation); if (mem != NULL) { - obj = initialize(mem); + oop obj = initialize(mem); + // Save obj in a handle for destructor safepoints + allocation.set_obj(obj); + obj_h = Handle(_thread, obj); } } - return obj; + return obj_h(); } void MemAllocator::mem_clear(HeapWord* mem) const { --- old/src/hotspot/share/runtime/javaCalls.cpp 2019-07-17 10:20:05.705484206 -0400 +++ new/src/hotspot/share/runtime/javaCalls.cpp 2019-07-17 10:20:05.489484213 -0400 @@ -346,9 +346,6 @@ assert(!SafepointSynchronize::is_at_safepoint(), "call to Java code during VM operation"); assert(!thread->handle_area()->no_handle_mark_active(), "cannot call out to Java here"); - - CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops();) - #if INCLUDE_JVMCI // Gets the nmethod (if any) that should be called instead of normal target nmethod* alternative_target = args->alternative_target(); @@ -395,10 +392,6 @@ BasicType result_type = runtime_type_from(result); bool oop_result_flag = (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY); - // NOTE: if we move the computation of the result_val_address inside - // the call to call_stub, the optimizer produces wrong code. - intptr_t* result_val_address = (intptr_t*)(result->get_value_addr()); - // Find receiver Handle receiver = (!method->is_static()) ? args->receiver() : Handle(); @@ -436,6 +429,11 @@ { JavaCallWrapper link(method, receiver, result, CHECK); { HandleMark hm(thread); // HandleMark used by HandleMarkCleaner + // NOTE: if we move the computation of the result_val_address inside + // the call to call_stub, the optimizer produces wrong code. + intptr_t* result_val_address = (intptr_t*)(result->get_value_addr()); + intptr_t* parameter_address = args->parameters(); + StubRoutines::call_stub()( (address)&link, // (intptr_t*)&(result->_value), // see NOTE above (compiler problem) @@ -443,7 +441,7 @@ result_type, method(), entry_point, - args->parameters(), + parameter_address, args->size_of_parameters(), CHECK ); --- old/src/hotspot/share/runtime/unhandledOops.cpp 2019-07-17 10:20:06.033484194 -0400 +++ new/src/hotspot/share/runtime/unhandledOops.cpp 2019-07-17 10:20:05.813484202 -0400 @@ -55,15 +55,15 @@ // For debugging unhandled oop detector _in the debugger_ // You don't want to turn it on in compiled code here. -static bool unhandled_oop_print=0; +static Thread* unhandled_oop_print = NULL; void UnhandledOops::register_unhandled_oop(oop* op, address pc) { if (!_thread->is_in_stack((address)op)) return; - _level ++; - if (unhandled_oop_print) { - for (int i=0; i<_level; i++) tty->print(" "); + _level++; + if (unhandled_oop_print == _thread) { + for (int i=0; i < _level; i++) tty->print(" "); tty->print_cr("r " INTPTR_FORMAT, p2i(op)); } UnhandledOopEntry entry(op, pc); @@ -98,11 +98,11 @@ void UnhandledOops::unregister_unhandled_oop(oop* op) { if (!_thread->is_in_stack((address)op)) return; - _level --; - if (unhandled_oop_print) { - for (int i=0; i<_level; i++) tty->print(" "); + if (unhandled_oop_print == _thread) { + for (int i=0; i < _level; i++) tty->print(" "); tty->print_cr("u " INTPTR_FORMAT, p2i(op)); } + _level--; int i = _oop_list->find_from_end(op, match_oop_entry); assert(i!=-1, "oop not in unhandled_oop_list"); --- old/src/hotspot/share/services/gcNotifier.cpp 2019-07-17 10:20:06.353484183 -0400 +++ new/src/hotspot/share/services/gcNotifier.cpp 2019-07-17 10:20:06.137484191 -0400 @@ -159,7 +159,7 @@ gcInfoklass, vmSymbols::com_sun_management_GcInfo_constructor_signature(), &constructor_args, - CHECK_NH); + THREAD); } void GCNotifier::sendNotification(TRAPS) { --- /dev/null 2019-07-15 12:22:48.964007259 -0400 +++ new/test/hotspot/jtreg/runtime/CheckUnhandledOops/TestOutOfMemory.java 2019-07-17 10:20:06.461484179 -0400 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8227766 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+CheckUnhandledOops -Xmx100m TestOutOfMemory + */ + +public class TestOutOfMemory { + public static void main(java.lang.String[] unused) { + final int BIG = 0x100000; + // Getting OOM breaks the unhandled oop detector + try { + int[][] X = new int[BIG][]; + for (int i = 0; i < BIG; i++) { + X[i] = new int[BIG]; + System.out.println("length = " + X.length); + } + } catch (OutOfMemoryError oom) { + System.out.println("OOM expected"); + } + } +}