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