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