1 /*
   2  * Copyright (c) 2012, 2015, 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 package sun.hotspot;
  26 
  27 import java.lang.management.MemoryUsage;
  28 import java.lang.reflect.Executable;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 import java.util.function.BiFunction;
  32 import java.util.function.Function;
  33 import java.util.stream.Stream;
  34 import java.security.BasicPermission;
  35 import java.util.Objects;
  36 
  37 import sun.hotspot.parser.DiagnosticCommand;
  38 
  39 public class WhiteBox {
  40 
  41   @SuppressWarnings("serial")
  42   public static class WhiteBoxPermission extends BasicPermission {
  43     public WhiteBoxPermission(String s) {
  44       super(s);
  45     }
  46   }
  47 
  48   private WhiteBox() {}
  49   private static final WhiteBox instance = new WhiteBox();
  50   private static native void registerNatives();
  51 
  52   /**
  53    * Returns the singleton WhiteBox instance.
  54    *
  55    * The returned WhiteBox object should be carefully guarded
  56    * by the caller, since it can be used to read and write data
  57    * at arbitrary memory addresses. It must never be passed to
  58    * untrusted code.
  59    */
  60   public synchronized static WhiteBox getWhiteBox() {
  61     SecurityManager sm = System.getSecurityManager();
  62     if (sm != null) {
  63       sm.checkPermission(new WhiteBoxPermission("getInstance"));
  64     }
  65     return instance;
  66   }
  67 
  68   static {
  69     registerNatives();
  70   }
  71 
  72   // Get the maximum heap size supporting COOPs
  73   public native long getCompressedOopsMaxHeapSize();
  74   // Arguments
  75   public native void printHeapSizes();
  76 
  77   // Memory
  78   private native long getObjectAddress0(Object o);
  79   public           long getObjectAddress(Object o) {
  80     Objects.requireNonNull(o);
  81     return getObjectAddress0(o);
  82   }
  83 
  84   public native int  getHeapOopSize();
  85   public native int  getVMPageSize();
  86   public native long getVMAllocationGranularity();
  87   public native long getVMLargePageSize();
  88 
  89   private native boolean isObjectInOldGen0(Object o);
  90   public         boolean isObjectInOldGen(Object o) {
  91     Objects.requireNonNull(o);
  92     return isObjectInOldGen0(o);
  93   }
  94 
  95   private native long getObjectSize0(Object o);
  96   public         long getObjectSize(Object o) {
  97     Objects.requireNonNull(o);
  98     return getObjectSize0(o);
  99   }
 100 
 101   // Runtime
 102   // Make sure class name is in the correct format
 103   public boolean isClassAlive(String name) {
 104     return isClassAlive0(name.replace('.', '/'));
 105   }
 106   private native boolean isClassAlive0(String name);
 107 
 108   private native boolean isMonitorInflated0(Object obj);
 109   public         boolean isMonitorInflated(Object obj) {
 110     Objects.requireNonNull(obj);
 111     return isMonitorInflated0(obj);
 112   }
 113 
 114   public native void forceSafepoint();
 115 
 116   // JVMTI
 117   private native void addToBootstrapClassLoaderSearch0(String segment);
 118   public         void addToBootstrapClassLoaderSearch(String segment){
 119     Objects.requireNonNull(segment);
 120     addToBootstrapClassLoaderSearch0(segment);
 121   }
 122 
 123   private native void addToSystemClassLoaderSearch0(String segment);
 124   public         void addToSystemClassLoaderSearch(String segment) {
 125     Objects.requireNonNull(segment);
 126     addToSystemClassLoaderSearch0(segment);
 127   }
 128 
 129   // G1
 130   public native boolean g1InConcurrentMark();
 131   private native boolean g1IsHumongous0(Object o);
 132   public         boolean g1IsHumongous(Object o) {
 133     Objects.requireNonNull(o);
 134     return g1IsHumongous0(o);
 135   }
 136 
 137   public native long    g1NumMaxRegions();
 138   public native long    g1NumFreeRegions();
 139   public native int     g1RegionSize();
 140   public native MemoryUsage g1AuxiliaryMemoryUsage();
 141   private  native Object[]    parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args);
 142   public          Object[]    parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) {
 143     Objects.requireNonNull(args);
 144     return parseCommandLine0(commandline, delim, args);
 145   }
 146 
 147   // NMT
 148   public native long NMTMalloc(long size);
 149   public native void NMTFree(long mem);
 150   public native long NMTReserveMemory(long size);
 151   public native void NMTCommitMemory(long addr, long size);
 152   public native void NMTUncommitMemory(long addr, long size);
 153   public native void NMTReleaseMemory(long addr, long size);
 154   public native long NMTMallocWithPseudoStack(long size, int index);
 155   public native boolean NMTChangeTrackingLevel();
 156   public native int NMTGetHashSize();
 157 
 158   // Compiler
 159   public native int     deoptimizeFrames(boolean makeNotEntrant);
 160   public native void    deoptimizeAll();
 161   public        boolean isMethodCompiled(Executable method) {
 162     return isMethodCompiled(method, false /*not osr*/);
 163   }
 164   private native boolean isMethodCompiled0(Executable method, boolean isOsr);
 165   public         boolean isMethodCompiled(Executable method, boolean isOsr){
 166     Objects.requireNonNull(method);
 167     return isMethodCompiled0(method, isOsr);
 168   }
 169   public        boolean isMethodCompilable(Executable method) {
 170     return isMethodCompilable(method, -1 /*any*/);
 171   }
 172   public        boolean isMethodCompilable(Executable method, int compLevel) {
 173     return isMethodCompilable(method, compLevel, false /*not osr*/);
 174   }
 175   private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
 176   public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
 177     Objects.requireNonNull(method);
 178     return isMethodCompilable0(method, compLevel, isOsr);
 179   }
 180   private native boolean isMethodQueuedForCompilation0(Executable method);
 181   public         boolean isMethodQueuedForCompilation(Executable method) {
 182     Objects.requireNonNull(method);
 183     return isMethodQueuedForCompilation0(method);
 184   }
 185   // Determine if the compiler corresponding to the compilation level 'compLevel'
 186   // and to the compilation context 'compilation_context' provides an intrinsic
 187   // for the method 'method'. An intrinsic is available for method 'method' if:
 188   //  - the intrinsic is enabled (by using the appropriate command-line flag) and
 189   //  - the platform on which the VM is running provides the instructions necessary
 190   //    for the compiler to generate the intrinsic code.
 191   //
 192   // The compilation context is related to using the DisableIntrinsic flag on a
 193   // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp
 194   // for more details.
 195   public boolean isIntrinsicAvailableForMethod(Executable method,
 196                                                Executable compilationContext,
 197                                                int compLevel) {
 198       Objects.requireNonNull(method);
 199       return isIntrinsicAvailableForMethod0(method, compilationContext, compLevel);
 200   }
 201   // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),
 202   // use the below method that does not require the compilation context as argument.
 203   public boolean isIntrinsicAvailableForMethod(Executable method, int compLevel) {
 204       return isIntrinsicAvailableForMethod(method, null, compLevel);
 205   }
 206   private native boolean isIntrinsicAvailableForMethod0(Executable method,
 207                                                         Executable compilationContext,
 208                                                         int compLevel);
 209   public        int     deoptimizeMethod(Executable method) {
 210     return deoptimizeMethod(method, false /*not osr*/);
 211   }
 212   private native int     deoptimizeMethod0(Executable method, boolean isOsr);
 213   public         int     deoptimizeMethod(Executable method, boolean isOsr) {
 214     Objects.requireNonNull(method);
 215     return deoptimizeMethod0(method, isOsr);
 216   }
 217   public        void    makeMethodNotCompilable(Executable method) {
 218     makeMethodNotCompilable(method, -1 /*any*/);
 219   }
 220   public        void    makeMethodNotCompilable(Executable method, int compLevel) {
 221     makeMethodNotCompilable(method, compLevel, false /*not osr*/);
 222   }
 223   private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
 224   public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
 225     Objects.requireNonNull(method);
 226     makeMethodNotCompilable0(method, compLevel, isOsr);
 227   }
 228   public        int     getMethodCompilationLevel(Executable method) {
 229     return getMethodCompilationLevel(method, false /*not ost*/);
 230   }
 231   private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
 232   public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
 233     Objects.requireNonNull(method);
 234     return getMethodCompilationLevel0(method, isOsr);
 235   }
 236   private native boolean testSetDontInlineMethod0(Executable method, boolean value);
 237   public         boolean testSetDontInlineMethod(Executable method, boolean value) {
 238     Objects.requireNonNull(method);
 239     return testSetDontInlineMethod0(method, value);
 240   }
 241   public        int     getCompileQueuesSize() {
 242     return getCompileQueueSize(-1 /*any*/);
 243   }
 244   public native int     getCompileQueueSize(int compLevel);
 245   private native boolean testSetForceInlineMethod0(Executable method, boolean value);
 246   public         boolean testSetForceInlineMethod(Executable method, boolean value) {
 247     Objects.requireNonNull(method);
 248     return testSetForceInlineMethod0(method, value);
 249   }
 250   public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
 251     return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
 252   }
 253   private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
 254   public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
 255     Objects.requireNonNull(method);
 256     return enqueueMethodForCompilation0(method, compLevel, entry_bci);
 257   }
 258   private native void    clearMethodState0(Executable method);
 259   public         void    clearMethodState(Executable method) {
 260     Objects.requireNonNull(method);
 261     clearMethodState0(method);
 262   }
 263   public native void    lockCompilation();
 264   public native void    unlockCompilation();
 265   private native int     getMethodEntryBci0(Executable method);
 266   public         int     getMethodEntryBci(Executable method) {
 267     Objects.requireNonNull(method);
 268     return getMethodEntryBci0(method);
 269   }
 270   private native Object[] getNMethod0(Executable method, boolean isOsr);
 271   public         Object[] getNMethod(Executable method, boolean isOsr) {
 272     Objects.requireNonNull(method);
 273     return getNMethod0(method, isOsr);
 274   }
 275   public native long    allocateCodeBlob(int size, int type);
 276   public        long    allocateCodeBlob(long size, int type) {
 277       int intSize = (int) size;
 278       if ((long) intSize != size || size < 0) {
 279           throw new IllegalArgumentException(
 280                 "size argument has illegal value " + size);
 281       }
 282       return allocateCodeBlob( intSize, type);
 283   }
 284   public native void    freeCodeBlob(long addr);
 285   public native void    forceNMethodSweep();
 286   public native Object[] getCodeHeapEntries(int type);
 287   public native int     getCompilationActivityMode();
 288   public native Object[] getCodeBlob(long addr);
 289 
 290   // Intered strings
 291   public native boolean isInStringTable(String str);
 292 
 293   // Memory
 294   public native void readReservedMemory();
 295   public native long allocateMetaspace(ClassLoader classLoader, long size);
 296   public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
 297   public native long incMetaspaceCapacityUntilGC(long increment);
 298   public native long metaspaceCapacityUntilGC();
 299 
 300   // Force Young GC
 301   public native void youngGC();
 302 
 303   // Force Full GC
 304   public native void fullGC();
 305 
 306   // Method tries to start concurrent mark cycle.
 307   // It returns false if CM Thread is always in concurrent cycle.
 308   public native boolean g1StartConcMarkCycle();
 309 
 310   // Tests on ReservedSpace/VirtualSpace classes
 311   public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
 312   public native void runMemoryUnitTests();
 313   public native void readFromNoaccessArea();
 314   public native long getThreadStackSize();
 315   public native long getThreadRemainingStackSize();
 316 
 317   // CPU features
 318   public native String getCPUFeatures();
 319 
 320   // Native extensions
 321   public native long getHeapUsageForContext(int context);
 322   public native long getHeapRegionCountForContext(int context);
 323   private native int getContextForObject0(Object obj);
 324   public         int getContextForObject(Object obj) {
 325     Objects.requireNonNull(obj);
 326     return getContextForObject0(obj);
 327   }
 328   public native void printRegionInfo(int context);
 329 
 330   // VM flags
 331   public native boolean isConstantVMFlag(String name);
 332   public native boolean isLockedVMFlag(String name);
 333   public native void    setBooleanVMFlag(String name, boolean value);
 334   public native void    setIntVMFlag(String name, long value);
 335   public native void    setUintVMFlag(String name, long value);
 336   public native void    setIntxVMFlag(String name, long value);
 337   public native void    setUintxVMFlag(String name, long value);
 338   public native void    setUint64VMFlag(String name, long value);
 339   public native void    setSizeTVMFlag(String name, long value);
 340   public native void    setStringVMFlag(String name, String value);
 341   public native void    setDoubleVMFlag(String name, double value);
 342   public native Boolean getBooleanVMFlag(String name);
 343   public native Long    getIntVMFlag(String name);
 344   public native Long    getUintVMFlag(String name);
 345   public native Long    getIntxVMFlag(String name);
 346   public native Long    getUintxVMFlag(String name);
 347   public native Long    getUint64VMFlag(String name);
 348   public native Long    getSizeTVMFlag(String name);
 349   public native String  getStringVMFlag(String name);
 350   public native Double  getDoubleVMFlag(String name);
 351   private final List<Function<String,Object>> flagsGetters = Arrays.asList(
 352     this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
 353     this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
 354     this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
 355 
 356   public Object getVMFlag(String name) {
 357     return flagsGetters.stream()
 358                        .map(f -> f.apply(name))
 359                        .filter(x -> x != null)
 360                        .findAny()
 361                        .orElse(null);
 362   }
 363   public native int getOffsetForName0(String name);
 364   public int getOffsetForName(String name) throws Exception {
 365     int offset = getOffsetForName0(name);
 366     if (offset == -1) {
 367       throw new RuntimeException(name + " not found");
 368     }
 369     return offset;
 370   }
 371   public native Boolean getMethodBooleanOption(Executable method, String name);
 372   public native Long    getMethodIntxOption(Executable method, String name);
 373   public native Long    getMethodUintxOption(Executable method, String name);
 374   public native Double  getMethodDoubleOption(Executable method, String name);
 375   public native String  getMethodStringOption(Executable method, String name);
 376   private final List<BiFunction<Executable,String,Object>> methodOptionGetters
 377       = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
 378           this::getMethodUintxOption, this::getMethodDoubleOption,
 379           this::getMethodStringOption);
 380 
 381   public Object getMethodOption(Executable method, String name) {
 382     return methodOptionGetters.stream()
 383                               .map(f -> f.apply(method, name))
 384                               .filter(x -> x != null)
 385                               .findAny()
 386                               .orElse(null);
 387   }
 388 
 389   // Safepoint Checking
 390   public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
 391 
 392   // Sharing
 393   public native boolean isShared(Object o);
 394   public native boolean areSharedStringsIgnored();
 395 }