1 /*
   2  * Copyright (c) 2003, 2018, 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/systemDictionary.hpp"
  27 #include "gc/shared/collectedHeap.hpp"
  28 #include "memory/universe.hpp"
  29 #include "prims/jvmtiGetLoadedClasses.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/jniHandles.inline.hpp"
  32 #include "runtime/thread.hpp"
  33 #include "utilities/stack.inline.hpp"
  34 #if INCLUDE_ALL_GCS
  35 #include "gc/g1/g1BarrierSet.hpp"
  36 #endif
  37 
  38 
  39 // The closure for GetLoadedClasses
  40 class LoadedClassesClosure : public KlassClosure {
  41 private:
  42   Stack<jclass, mtInternal> _classStack;
  43   JvmtiEnv* _env;
  44   Thread*   _cur_thread;
  45 
  46 // Tell the GC to keep this klass alive
  47 static void ensure_klass_alive(oop o) {
  48   // A klass that was previously considered dead can be looked up in the
  49   // CLD/SD, and its _java_mirror or _class_loader can be stored in a root
  50   // or a reachable object making it alive again. The SATB part of G1 needs
  51   // to get notified about this potential resurrection, otherwise the marking
  52   // might not find the object.
  53 #if INCLUDE_ALL_GCS
  54   if (UseG1GC && o != NULL) {
  55     G1BarrierSet::enqueue(o);
  56   }
  57 #endif
  58 }
  59 
  60 public:
  61   LoadedClassesClosure(Thread* thread, JvmtiEnv* env) : _cur_thread(thread), _env(env) {
  62     assert(_cur_thread == Thread::current(), "must be current thread");
  63   }
  64 
  65   void do_klass(Klass* k) {
  66     // Collect all jclasses
  67     _classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, k->java_mirror())));
  68     ensure_klass_alive(k->java_mirror());
  69   }
  70 
  71   int extract(jclass* result_list) {
  72     // The size of the Stack will be 0 after extract, so get it here
  73     int count = (int)_classStack.size();
  74     int i = count;
  75 
  76     // Pop all jclasses, fill backwards
  77     while (!_classStack.is_empty()) {
  78       result_list[--i] = _classStack.pop();
  79     }
  80 
  81     // Return the number of elements written
  82     return count;
  83   }
  84 
  85   // Return current size of the Stack
  86   int get_count() {
  87     return (int)_classStack.size();
  88   }
  89 };
  90 
  91 // The closure for GetClassLoaderClasses
  92 class JvmtiGetLoadedClassesClosure : public StackObj {
  93   // Since the ClassLoaderDataGraph::dictionary_all_entries_do callback
  94   // doesn't pass a closureData pointer,
  95   // we use a thread-local slot to hold a pointer to
  96   // a stack allocated instance of this structure.
  97  private:
  98   jobject _initiatingLoader;
  99   int     _count;
 100   Handle* _list;
 101   int     _index;
 102 
 103  private:
 104   // Getting and setting the thread local pointer
 105   static JvmtiGetLoadedClassesClosure* get_this() {
 106     JvmtiGetLoadedClassesClosure* result = NULL;
 107     JavaThread* thread = JavaThread::current();
 108     result = thread->get_jvmti_get_loaded_classes_closure();
 109     return result;
 110   }
 111   static void set_this(JvmtiGetLoadedClassesClosure* that) {
 112     JavaThread* thread = JavaThread::current();
 113     thread->set_jvmti_get_loaded_classes_closure(that);
 114   }
 115 
 116  public:
 117   // Constructor/Destructor
 118   JvmtiGetLoadedClassesClosure() {
 119     JvmtiGetLoadedClassesClosure* that = get_this();
 120     assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
 121     _initiatingLoader = NULL;
 122     _count = 0;
 123     _list = NULL;
 124     _index = 0;
 125     set_this(this);
 126   }
 127 
 128   JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
 129     JvmtiGetLoadedClassesClosure* that = get_this();
 130     assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
 131     _initiatingLoader = initiatingLoader;
 132     _count = 0;
 133     _list = NULL;
 134     _index = 0;
 135     set_this(this);
 136   }
 137 
 138   ~JvmtiGetLoadedClassesClosure() {
 139     JvmtiGetLoadedClassesClosure* that = get_this();
 140     assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
 141     set_this(NULL);
 142     _initiatingLoader = NULL;
 143     _count = 0;
 144     if (_list != NULL) {
 145       FreeHeap(_list);
 146       _list = NULL;
 147     }
 148     _index = 0;
 149   }
 150 
 151   // Accessors.
 152   jobject get_initiatingLoader() {
 153     return _initiatingLoader;
 154   }
 155 
 156   int get_count() {
 157     return _count;
 158   }
 159 
 160   void set_count(int value) {
 161     _count = value;
 162   }
 163 
 164   Handle* get_list() {
 165     return _list;
 166   }
 167 
 168   void set_list(Handle* value) {
 169     _list = value;
 170   }
 171 
 172   int get_index() {
 173     return _index;
 174   }
 175 
 176   void set_index(int value) {
 177     _index = value;
 178   }
 179 
 180   Handle get_element(int index) {
 181     if ((_list != NULL) && (index < _count)) {
 182       return _list[index];
 183     } else {
 184       assert(false, "empty get_element");
 185       return Handle();
 186     }
 187   }
 188 
 189   void set_element(int index, Handle value) {
 190     if ((_list != NULL) && (index < _count)) {
 191       _list[index] = value;
 192     } else {
 193       assert(false, "bad set_element");
 194     }
 195   }
 196 
 197   // Other predicates
 198   bool available() {
 199     return (_list != NULL);
 200   }
 201 
 202 #ifdef ASSERT
 203   // For debugging.
 204   void check(int limit) {
 205     for (int i = 0; i < limit; i += 1) {
 206       assert(Universe::heap()->is_in(get_element(i)()), "check fails");
 207     }
 208   }
 209 #endif
 210 
 211   // Public methods that get called within the scope of the closure
 212   void allocate() {
 213     _list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);
 214     assert(_list != NULL, "Out of memory");
 215     if (_list == NULL) {
 216       _count = 0;
 217     }
 218   }
 219 
 220   void extract(JvmtiEnv *env, jclass* result) {
 221     for (int index = 0; index < _count; index += 1) {
 222       result[index] = (jclass) env->jni_reference(get_element(index));
 223     }
 224   }
 225 
 226   static void increment_with_loader(InstanceKlass* k, ClassLoaderData* loader_data) {
 227     JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
 228     oop class_loader = loader_data->class_loader();
 229     if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
 230       for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
 231         that->set_count(that->get_count() + 1);
 232       }
 233     }
 234   }
 235 
 236   static void add_with_loader(InstanceKlass* k, ClassLoaderData* loader_data) {
 237     JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
 238     if (that->available()) {
 239       oop class_loader = loader_data->class_loader();
 240       if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
 241         Thread *thread = Thread::current();
 242         for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
 243           Handle mirror(thread, l->java_mirror());
 244           that->set_element(that->get_index(), mirror);
 245           that->set_index(that->get_index() + 1);
 246         }
 247       }
 248     }
 249   }
 250 
 251   // increment the count for the given basic type array class (and any
 252   // multi-dimensional arrays). For example, for [B we check for
 253   // [[B, [[[B, .. and the count is incremented for each one that exists.
 254   static void increment_for_basic_type_arrays(Klass* k) {
 255     JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
 256     assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
 257     for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
 258       that->set_count(that->get_count() + 1);
 259     }
 260   }
 261 
 262   // add the basic type array class and its multi-dimensional array classes to the list
 263   static void add_for_basic_type_arrays(Klass* k) {
 264     JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
 265     assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
 266     assert(that->available(), "no list");
 267     Thread *thread = Thread::current();
 268     for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
 269       Handle mirror(thread, l->java_mirror());
 270       that->set_element(that->get_index(), mirror);
 271       that->set_index(that->get_index() + 1);
 272     }
 273   }
 274 };
 275 
 276 
 277 jvmtiError
 278 JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
 279 
 280   LoadedClassesClosure closure(Thread::current(), env);
 281   {
 282     // To get a consistent list of classes we need MultiArray_lock to ensure
 283     // array classes aren't created.
 284     MutexLocker ma(MultiArray_lock);
 285 
 286     // Iterate through all classes in ClassLoaderDataGraph
 287     // and collect them using the LoadedClassesClosure
 288     ClassLoaderDataGraph::loaded_classes_do(&closure);
 289   }
 290 
 291   // Return results by extracting the collected contents into a list
 292   // allocated via JvmtiEnv
 293   jclass* result_list;
 294   jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),
 295                                (unsigned char**)&result_list);
 296 
 297   if (error == JVMTI_ERROR_NONE) {
 298     int count = closure.extract(result_list);
 299     *classCountPtr = count;
 300     *classesPtr = result_list;
 301   }
 302   return error;
 303 }
 304 
 305 jvmtiError
 306 JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
 307                                              jint* classCountPtr, jclass** classesPtr) {
 308   // Since ClassLoaderDataGraph::dictionary_all_entries_do only takes a function pointer
 309   // and doesn't call back with a closure data pointer,
 310   // we can only pass static methods.
 311   JvmtiGetLoadedClassesClosure closure(initiatingLoader);
 312   {
 313     // To get a consistent list of classes we need MultiArray_lock to ensure
 314     // array classes aren't created, and SystemDictionary_lock to ensure that
 315     // classes aren't added to the class loader data dictionaries.
 316     MutexLocker ma(MultiArray_lock);
 317     MutexLocker sd(SystemDictionary_lock);
 318     // First, count the classes in the class loader data dictionaries which have this loader recorded
 319     // as an initiating loader. For basic type arrays this information is not recorded
 320     // so GetClassLoaderClasses will return all of the basic type arrays. This is okay
 321     // because the defining loader for basic type arrays is always the boot class loader
 322     // and these classes are "visible" to all loaders.
 323     ClassLoaderDataGraph::dictionary_all_entries_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
 324     Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
 325     // Next, fill in the classes
 326     closure.allocate();
 327     ClassLoaderDataGraph::dictionary_all_entries_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
 328     Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
 329     // Drop the SystemDictionary_lock, so the results could be wrong from here,
 330     // but we still have a snapshot.
 331   }
 332   // Post results
 333   jclass* result_list;
 334   jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
 335                                  (unsigned char**)&result_list);
 336   if (err != JVMTI_ERROR_NONE) {
 337     return err;
 338   }
 339   closure.extract(env, result_list);
 340   *classCountPtr = closure.get_count();
 341   *classesPtr = result_list;
 342   return JVMTI_ERROR_NONE;
 343 }