1 /*
2 * Copyright (c) 2005, 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 "gc/shared/collectedHeap.hpp"
27 #include "memory/universe.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "runtime/thread.hpp"
30 #include "runtime/unhandledOops.hpp"
31 #include "utilities/globalDefinitions.hpp"
32
33 #ifdef CHECK_UNHANDLED_OOPS
34 const int free_list_size = 256;
35
36
UnhandledOops(Thread * thread)37 UnhandledOops::UnhandledOops(Thread* thread) {
38 _thread = thread;
39 _oop_list = new (ResourceObj::C_HEAP, mtInternal)
40 GrowableArray<UnhandledOopEntry>(free_list_size, true);
41 _level = 0;
42 }
43
~UnhandledOops()44 UnhandledOops::~UnhandledOops() {
45 delete _oop_list;
46 }
47
48
dump_oops(UnhandledOops * list)49 void UnhandledOops::dump_oops(UnhandledOops *list) {
50 for (int k = 0; k < list->_oop_list->length(); k++) {
51 UnhandledOopEntry entry = list->_oop_list->at(k);
52 tty->print(" " INTPTR_FORMAT, p2i(entry._oop_ptr));
53 }
54 tty->cr();
55 }
56
57 // For debugging unhandled oop detector _in the debugger_
58 // You don't want to turn it on in compiled code here.
59 static Thread* unhandled_oop_print = NULL;
60
register_unhandled_oop(oop * op)61 void UnhandledOops::register_unhandled_oop(oop* op) {
62 if (!_thread->is_in_stack((address)op))
63 return;
64
65 _level++;
66 if (unhandled_oop_print == _thread) {
67 for (int i=0; i < _level; i++) tty->print(" ");
68 tty->print_cr("r " INTPTR_FORMAT, p2i(op));
69 }
70 UnhandledOopEntry entry(op);
71 _oop_list->push(entry);
72 }
73
74
match_oop_entry(void * op,UnhandledOopEntry e)75 bool match_oop_entry(void *op, UnhandledOopEntry e) {
76 return (e.oop_ptr() == op);
77 }
78
79 // Mark unhandled oop as okay for GC - the containing struct has an oops_do and
80 // for some reason the oop has to be on the stack.
81 // May not be called for the current thread, as in the case of
82 // VM_GetOrSetLocal in jvmti.
allow_unhandled_oop(oop * op)83 void UnhandledOops::allow_unhandled_oop(oop* op) {
84 assert (CheckUnhandledOops, "should only be called with checking option");
85
86 int i = _oop_list->find_from_end(op, match_oop_entry);
87 assert(i!=-1, "safe for gc oop not in unhandled_oop_list");
88
89 UnhandledOopEntry entry = _oop_list->at(i);
90 assert(!entry._ok_for_gc, "duplicate entry");
91 entry._ok_for_gc = true;
92 _oop_list->at_put(i, entry);
93 }
94
95
96 // Called by the oop destructor to remove unhandled oop from the thread's
97 // oop list. All oops given are assumed to be on the list. If not,
98 // there's a bug in the unhandled oop detector.
unregister_unhandled_oop(oop * op)99 void UnhandledOops::unregister_unhandled_oop(oop* op) {
100 if (!_thread->is_in_stack((address)op)) return;
101
102 if (unhandled_oop_print == _thread) {
103 for (int i=0; i < _level; i++) tty->print(" ");
104 tty->print_cr("u " INTPTR_FORMAT, p2i(op));
105 }
106 _level--;
107
108 int i = _oop_list->find_from_end(op, match_oop_entry);
109 assert(i!=-1, "oop not in unhandled_oop_list");
110 _oop_list->remove_at(i);
111 }
112
clear_unhandled_oops()113 void UnhandledOops::clear_unhandled_oops() {
114 assert (CheckUnhandledOops, "should only be called with checking option");
115
116 for (int k = 0; k < _oop_list->length(); k++) {
117 UnhandledOopEntry entry = _oop_list->at(k);
118 // If an entry is on the unhandled oop list but isn't on the stack
119 // anymore, it must not have gotten unregistered properly and it's a bug
120 // in the unhandled oop generator.
121 if(!_thread->is_in_stack((address)entry._oop_ptr)) {
122 tty->print_cr("oop_ptr is " INTPTR_FORMAT, p2i(entry._oop_ptr));
123 tty->print_cr("thread is " INTPTR_FORMAT, p2i(_thread));
124 assert(false, "heap is corrupted by the unhandled oop detector");
125 }
126 // Set unhandled oops to a pattern that will crash distinctively
127 if (!entry._ok_for_gc) *(intptr_t*)(entry._oop_ptr) = BAD_OOP_ADDR;
128 }
129 }
130 #endif // CHECK_UNHANDLED_OOPS
131