1 /*
   2  * Copyright (c) 2012, 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 
  27 #include "memory/universe.hpp"
  28 #include "oops/oop.inline.hpp"
  29 
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/classLoaderData.hpp"
  32 
  33 #include "prims/whitebox.hpp"
  34 #include "prims/wbtestmethods/parserTests.hpp"
  35 
  36 #include "runtime/interfaceSupport.hpp"
  37 #include "runtime/os.hpp"
  38 #include "utilities/debug.hpp"
  39 
  40 #ifndef SERIALGC
  41 #include "gc_implementation/g1/concurrentMark.hpp"
  42 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  43 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  44 #endif // !SERIALGC
  45 
  46 #ifdef INCLUDE_NMT
  47 #include "services/memTracker.hpp"
  48 #endif // INCLUDE_NMT
  49 
  50 bool WhiteBox::_used = false;
  51 
  52 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
  53   return (jlong)(void*)JNIHandles::resolve(obj);
  54 WB_END
  55 
  56 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
  57   return heapOopSize;
  58 WB_END
  59 
  60 
  61 class WBIsKlassAliveClosure : public KlassClosure {
  62     Symbol* _name;
  63     bool _found;
  64 public:
  65     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
  66 
  67     void do_klass(Klass* k) {
  68       if (_found) return;
  69       Symbol* ksym = k->name();
  70       if (ksym->fast_compare(_name) == 0) {
  71         _found = true;
  72       }
  73     }
  74 
  75     bool found() const {
  76         return _found;
  77     }
  78 };
  79 
  80 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
  81   Handle h_name = JNIHandles::resolve(name);
  82   if (h_name.is_null()) return false;
  83   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
  84   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
  85 
  86   WBIsKlassAliveClosure closure(sym);
  87   ClassLoaderDataGraph::classes_do(&closure);
  88 
  89   return closure.found();
  90 WB_END
  91 
  92 #ifndef SERIALGC
  93 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
  94   G1CollectedHeap* g1 = G1CollectedHeap::heap();
  95   oop result = JNIHandles::resolve(obj);
  96   const HeapRegion* hr = g1->heap_region_containing(result);
  97   return hr->isHumongous();
  98 WB_END
  99 
 100 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 101   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 102   size_t nr = g1->free_regions();
 103   return (jlong)nr;
 104 WB_END
 105 
 106 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 107   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 108   ConcurrentMark* cm = g1->concurrent_mark();
 109   return cm->concurrent_marking_in_progress();
 110 WB_END
 111 
 112 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 113   return (jint)HeapRegion::GrainBytes;
 114 WB_END
 115 #endif // !SERIALGC
 116 
 117 #ifdef INCLUDE_NMT
 118 // Keep track of the 3 allocations in NMTAllocTest so we can free them later
 119 // on and verify that they're not visible anymore
 120 static void* nmtMtTest1 = NULL, *nmtMtTest2 = NULL, *nmtMtTest3 = NULL;
 121 
 122 // Alloc memory using the test memory type so that we can use that to see if 
 123 // NMT picks it up correctly
 124 WB_ENTRY(jboolean, WB_NMTAllocTest(JNIEnv* env)) 
 125   void *mem;
 126 
 127   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
 128     return false;
 129   }
 130 
 131   // Allocate 2 * 128k + 256k + 1024k and free the 1024k one to make sure we track 
 132   // everything correctly. Total should be 512k held alive.
 133   nmtMtTest1 = os::malloc(128 * 1024, mtTest);
 134   mem = os::malloc(1024 * 1024, mtTest);
 135   nmtMtTest2 = os::malloc(256 * 1024, mtTest);
 136   os::free(mem, mtTest);
 137   nmtMtTest3 = os::malloc(128 * 1024, mtTest);
 138 
 139   return true;
 140 WB_END
 141 
 142 // Free the memory allocated by NMTAllocTest
 143 WB_ENTRY(jboolean, WB_NMTFreeTestMemory(JNIEnv* env))
 144   
 145   if (nmtMtTest1 == NULL || nmtMtTest2 == NULL || nmtMtTest3 == NULL) {
 146     return false;
 147   }
 148 
 149   os::free(nmtMtTest1, mtTest);
 150   nmtMtTest1 = NULL;
 151   os::free(nmtMtTest2, mtTest);
 152   nmtMtTest2 = NULL;
 153   os::free(nmtMtTest3, mtTest);
 154   nmtMtTest3 = NULL;
 155 
 156   return true;
 157 WB_END
 158 
 159 // Block until the current generation of NMT data to be merged, used to reliably test the NMT feature
 160 WB_ENTRY(jboolean, WB_NMTWaitForDataMerge(JNIEnv* env))
 161   
 162   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
 163     return false;
 164   }  
 165 
 166   return MemTracker::wbtest_wait_for_data_merge();
 167 WB_END
 168 
 169 #endif // INCLUDE_NMT
 170 
 171 //Some convenience methods to deal with objects from java
 172 int WhiteBox::offset_for_field(const char* field_name, oop object,
 173     Symbol* signature_symbol) {
 174   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
 175   Thread* THREAD = Thread::current();
 176 
 177   //Get the class of our object
 178   Klass* arg_klass = object->klass();
 179   //Turn it into an instance-klass
 180   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
 181 
 182   //Create symbols to look for in the class
 183   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
 184       THREAD);
 185 
 186   //To be filled in with an offset of the field we're looking for
 187   fieldDescriptor fd;
 188 
 189   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
 190   if (res == NULL) {
 191     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
 192         name_symbol->as_C_string());
 193     fatal("Invalid layout of preloaded class");
 194   }
 195 
 196   //fetch the field at the offset we've found
 197   int dest_offset = fd.offset();
 198 
 199   return dest_offset;
 200 }
 201 
 202 
 203 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
 204   int offset = offset_for_field(field_name, object,
 205       vmSymbols::string_signature());
 206   oop string = object->obj_field(offset);
 207   if (string == NULL) {
 208     return NULL;
 209   }
 210   const char* ret = java_lang_String::as_utf8_string(string);
 211   return ret;
 212 }
 213 
 214 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
 215   int offset =
 216       offset_for_field(field_name, object, vmSymbols::bool_signature());
 217   bool ret = (object->bool_field(offset) == JNI_TRUE);
 218   return ret;
 219 }
 220 
 221 
 222 #define CC (char*)
 223 
 224 static JNINativeMethod methods[] = {
 225   {CC"getObjectAddress",   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
 226   {CC"getHeapOopSize",     CC"()I",                   (void*)&WB_GetHeapOopSize    },
 227   {CC"isClassAlive0",       CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
 228   {CC "parseCommandLine",
 229       CC "(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
 230       (void*) &WB_ParseCommandLine
 231   },
 232 #ifndef SERIALGC
 233   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
 234   {CC"g1IsHumongous",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
 235   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
 236   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
 237 #endif // !SERIALGC
 238 #ifdef INCLUDE_NMT
 239   {CC"NMTAllocTest",       CC"()Z",                   (void*)&WB_NMTAllocTest      },
 240   {CC"NMTFreeTestMemory",  CC"()Z",                   (void*)&WB_NMTFreeTestMemory },
 241   {CC"NMTWaitForDataMerge",CC"()Z",                   (void*)&WB_NMTWaitForDataMerge},
 242 #endif // INCLUDE_NMT
 243 };
 244 
 245 #undef CC
 246 
 247 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
 248   {
 249     if (WhiteBoxAPI) {
 250       // Make sure that wbclass is loaded by the null classloader
 251       instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass());
 252       Handle loader(ikh->class_loader());
 253       if (loader.is_null()) {
 254         ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
 255         jint result = env->RegisterNatives(wbclass, methods, sizeof(methods)/sizeof(methods[0]));
 256         if (result == 0) {
 257           WhiteBox::set_used();
 258         }
 259       }
 260     }
 261   }
 262 JVM_END