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     matchesInline(Executable method, String pattern);
 164   public native int     deoptimizeFrames(boolean makeNotEntrant);
 165   public native void    deoptimizeAll();
 166   public        boolean isMethodCompiled(Executable method) {
 167     return isMethodCompiled(method, false /*not osr*/);
 168   }
 169   private native boolean isMethodCompiled0(Executable method, boolean isOsr);
 170   public         boolean isMethodCompiled(Executable method, boolean isOsr){
 171     Objects.requireNonNull(method);
 172     return isMethodCompiled0(method, isOsr);
 173   }
 174   public        boolean isMethodCompilable(Executable method) {
 175     return isMethodCompilable(method, -1 /*any*/);
 176   }
 177   public        boolean isMethodCompilable(Executable method, int compLevel) {
 178     return isMethodCompilable(method, compLevel, false /*not osr*/);
 179   }
 180   private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
 181   public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
 182     Objects.requireNonNull(method);
 183     return isMethodCompilable0(method, compLevel, isOsr);
 184   }
 185   private native boolean isMethodQueuedForCompilation0(Executable method);
 186   public         boolean isMethodQueuedForCompilation(Executable method) {
 187     Objects.requireNonNull(method);
 188     return isMethodQueuedForCompilation0(method);
 189   }
 190   // Determine if the compiler corresponding to the compilation level 'compLevel'
 191   // and to the compilation context 'compilation_context' provides an intrinsic
 192   // for the method 'method'. An intrinsic is available for method 'method' if:
 193   //  - the intrinsic is enabled (by using the appropriate command-line flag) and
 194   //  - the platform on which the VM is running provides the instructions necessary
 195   //    for the compiler to generate the intrinsic code.
 196   //
 197   // The compilation context is related to using the DisableIntrinsic flag on a
 198   // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp
 199   // for more details.
 200   public boolean isIntrinsicAvailable(Executable method,
 201                                       Executable compilationContext,
 202                                       int compLevel) {
 203       Objects.requireNonNull(method);
 204       return isIntrinsicAvailable0(method, compilationContext, compLevel);
 205   }
 206   // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),
 207   // use the below method that does not require the compilation context as argument.
 208   public boolean isIntrinsicAvailable(Executable method, int compLevel) {
 209       return isIntrinsicAvailable(method, null, compLevel);
 210   }
 211   private native boolean isIntrinsicAvailable0(Executable method,
 212                                                Executable compilationContext,
 213                                                int compLevel);
 214   public        int     deoptimizeMethod(Executable method) {
 215     return deoptimizeMethod(method, false /*not osr*/);
 216   }
 217   private native int     deoptimizeMethod0(Executable method, boolean isOsr);
 218   public         int     deoptimizeMethod(Executable method, boolean isOsr) {
 219     Objects.requireNonNull(method);
 220     return deoptimizeMethod0(method, isOsr);
 221   }
 222   public        void    makeMethodNotCompilable(Executable method) {
 223     makeMethodNotCompilable(method, -1 /*any*/);
 224   }
 225   public        void    makeMethodNotCompilable(Executable method, int compLevel) {
 226     makeMethodNotCompilable(method, compLevel, false /*not osr*/);
 227   }
 228   private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
 229   public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
 230     Objects.requireNonNull(method);
 231     makeMethodNotCompilable0(method, compLevel, isOsr);
 232   }
 233   public        int     getMethodCompilationLevel(Executable method) {
 234     return getMethodCompilationLevel(method, false /*not ost*/);
 235   }
 236   private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
 237   public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
 238     Objects.requireNonNull(method);
 239     return getMethodCompilationLevel0(method, isOsr);
 240   }
 241   private native boolean testSetDontInlineMethod0(Executable method, boolean value);
 242   public         boolean testSetDontInlineMethod(Executable method, boolean value) {
 243     Objects.requireNonNull(method);
 244     return testSetDontInlineMethod0(method, value);
 245   }
 246   public        int     getCompileQueuesSize() {
 247     return getCompileQueueSize(-1 /*any*/);
 248   }
 249   public native int     getCompileQueueSize(int compLevel);
 250   private native boolean testSetForceInlineMethod0(Executable method, boolean value);
 251   public         boolean testSetForceInlineMethod(Executable method, boolean value) {
 252     Objects.requireNonNull(method);
 253     return testSetForceInlineMethod0(method, value);
 254   }
 255   public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
 256     return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
 257   }
 258   private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
 259   public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
 260     Objects.requireNonNull(method);
 261     return enqueueMethodForCompilation0(method, compLevel, entry_bci);
 262   }
 263   private native void    clearMethodState0(Executable method);
 264   public         void    clearMethodState(Executable method) {
 265     Objects.requireNonNull(method);
 266     clearMethodState0(method);
 267   }
 268   public native void    lockCompilation();
 269   public native void    unlockCompilation();
 270   private native int     getMethodEntryBci0(Executable method);
 271   public         int     getMethodEntryBci(Executable method) {
 272     Objects.requireNonNull(method);
 273     return getMethodEntryBci0(method);
 274   }
 275   private native Object[] getNMethod0(Executable method, boolean isOsr);
 276   public         Object[] getNMethod(Executable method, boolean isOsr) {
 277     Objects.requireNonNull(method);
 278     return getNMethod0(method, isOsr);
 279   }
 280   public native long    allocateCodeBlob(int size, int type);
 281   public        long    allocateCodeBlob(long size, int type) {
 282       int intSize = (int) size;
 283       if ((long) intSize != size || size < 0) {
 284           throw new IllegalArgumentException(
 285                 "size argument has illegal value " + size);
 286       }
 287       return allocateCodeBlob( intSize, type);
 288   }
 289   public native void    freeCodeBlob(long addr);
 290   public native void    forceNMethodSweep();
 291   public native Object[] getCodeHeapEntries(int type);
 292   public native int     getCompilationActivityMode();
 293   public native Object[] getCodeBlob(long addr);
 294 
 295   // Intered strings
 296   public native boolean isInStringTable(String str);
 297 
 298   // Memory
 299   public native void readReservedMemory();
 300   public native long allocateMetaspace(ClassLoader classLoader, long size);
 301   public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
 302   public native long incMetaspaceCapacityUntilGC(long increment);
 303   public native long metaspaceCapacityUntilGC();
 304 
 305   // Force Young GC
 306   public native void youngGC();
 307 
 308   // Force Full GC
 309   public native void fullGC();
 310 
 311   // Method tries to start concurrent mark cycle.
 312   // It returns false if CM Thread is always in concurrent cycle.
 313   public native boolean g1StartConcMarkCycle();
 314 
 315   // Tests on ReservedSpace/VirtualSpace classes
 316   public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
 317   public native void runMemoryUnitTests();
 318   public native void readFromNoaccessArea();
 319   public native long getThreadStackSize();
 320   public native long getThreadRemainingStackSize();
 321 
 322   // CPU features
 323   public native String getCPUFeatures();
 324 
 325   // Native extensions
 326   public native long getHeapUsageForContext(int context);
 327   public native long getHeapRegionCountForContext(int context);
 328   private native int getContextForObject0(Object obj);
 329   public         int getContextForObject(Object obj) {
 330     Objects.requireNonNull(obj);
 331     return getContextForObject0(obj);
 332   }
 333   public native void printRegionInfo(int context);
 334 
 335   // VM flags
 336   public native boolean isConstantVMFlag(String name);
 337   public native boolean isLockedVMFlag(String name);
 338   public native void    setBooleanVMFlag(String name, boolean value);
 339   public native void    setIntVMFlag(String name, long value);
 340   public native void    setUintVMFlag(String name, long value);
 341   public native void    setIntxVMFlag(String name, long value);
 342   public native void    setUintxVMFlag(String name, long value);
 343   public native void    setUint64VMFlag(String name, long value);
 344   public native void    setSizeTVMFlag(String name, long value);
 345   public native void    setStringVMFlag(String name, String value);
 346   public native void    setDoubleVMFlag(String name, double value);
 347   public native Boolean getBooleanVMFlag(String name);
 348   public native Long    getIntVMFlag(String name);
 349   public native Long    getUintVMFlag(String name);
 350   public native Long    getIntxVMFlag(String name);
 351   public native Long    getUintxVMFlag(String name);
 352   public native Long    getUint64VMFlag(String name);
 353   public native Long    getSizeTVMFlag(String name);
 354   public native String  getStringVMFlag(String name);
 355   public native Double  getDoubleVMFlag(String name);
 356   private final List<Function<String,Object>> flagsGetters = Arrays.asList(
 357     this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
 358     this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
 359     this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
 360 
 361   public Object getVMFlag(String name) {
 362     return flagsGetters.stream()
 363                        .map(f -> f.apply(name))
 364                        .filter(x -> x != null)
 365                        .findAny()
 366                        .orElse(null);
 367   }
 368   public native int getOffsetForName0(String name);
 369   public int getOffsetForName(String name) throws Exception {
 370     int offset = getOffsetForName0(name);
 371     if (offset == -1) {
 372       throw new RuntimeException(name + " not found");
 373     }
 374     return offset;
 375   }
 376   public native Boolean getMethodBooleanOption(Executable method, String name);
 377   public native Long    getMethodIntxOption(Executable method, String name);
 378   public native Long    getMethodUintxOption(Executable method, String name);
 379   public native Double  getMethodDoubleOption(Executable method, String name);
 380   public native String  getMethodStringOption(Executable method, String name);
 381   private final List<BiFunction<Executable,String,Object>> methodOptionGetters
 382       = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
 383           this::getMethodUintxOption, this::getMethodDoubleOption,
 384           this::getMethodStringOption);
 385 
 386   public Object getMethodOption(Executable method, String name) {
 387     return methodOptionGetters.stream()
 388                               .map(f -> f.apply(method, name))
 389                               .filter(x -> x != null)
 390                               .findAny()
 391                               .orElse(null);
 392   }
 393 
 394   // Safepoint Checking
 395   public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
 396 
 397   // Sharing
 398   public native boolean isShared(Object o);
 399   public native boolean areSharedStringsIgnored();
 400 }