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