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