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 getVMLargePageSize();
  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   // NMT
 147   public native long NMTMalloc(long size);
 148   public native void NMTFree(long mem);
 149   public native long NMTReserveMemory(long size);
 150   public native void NMTCommitMemory(long addr, long size);
 151   public native void NMTUncommitMemory(long addr, long size);
 152   public native void NMTReleaseMemory(long addr, long size);
 153   public native long NMTMallocWithPseudoStack(long size, int index);
 154   public native boolean NMTChangeTrackingLevel();
 155   public native int NMTGetHashSize();
 156 
 157   // Compiler
 158   public native int     deoptimizeFrames(boolean makeNotEntrant);
 159   public native void    deoptimizeAll();
 160   public        boolean isMethodCompiled(Executable method) {
 161     return isMethodCompiled(method, false /*not osr*/);
 162   }
 163   private native boolean isMethodCompiled0(Executable method, boolean isOsr);
 164   public         boolean isMethodCompiled(Executable method, boolean isOsr){
 165     Objects.requireNonNull(method);
 166     return isMethodCompiled0(method, isOsr);
 167   }
 168   public        boolean isMethodCompilable(Executable method) {
 169     return isMethodCompilable(method, -1 /*any*/);
 170   }
 171   public        boolean isMethodCompilable(Executable method, int compLevel) {
 172     return isMethodCompilable(method, compLevel, false /*not osr*/);
 173   }
 174   private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
 175   public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
 176     Objects.requireNonNull(method);
 177     return isMethodCompilable0(method, compLevel, isOsr);
 178   }
 179   private native boolean isMethodQueuedForCompilation0(Executable method);
 180   public         boolean isMethodQueuedForCompilation(Executable method) {
 181     Objects.requireNonNull(method);
 182     return isMethodQueuedForCompilation0(method);
 183   }
 184   public        int     deoptimizeMethod(Executable method) {
 185     return deoptimizeMethod(method, false /*not osr*/);
 186   }
 187   private native int     deoptimizeMethod0(Executable method, boolean isOsr);
 188   public         int     deoptimizeMethod(Executable method, boolean isOsr) {
 189     Objects.requireNonNull(method);
 190     return deoptimizeMethod0(method, isOsr);
 191   }
 192   public        void    makeMethodNotCompilable(Executable method) {
 193     makeMethodNotCompilable(method, -1 /*any*/);
 194   }
 195   public        void    makeMethodNotCompilable(Executable method, int compLevel) {
 196     makeMethodNotCompilable(method, compLevel, false /*not osr*/);
 197   }
 198   private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
 199   public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
 200     Objects.requireNonNull(method);
 201     makeMethodNotCompilable0(method, compLevel, isOsr);
 202   }
 203   public        int     getMethodCompilationLevel(Executable method) {
 204     return getMethodCompilationLevel(method, false /*not ost*/);
 205   }
 206   private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
 207   public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
 208     Objects.requireNonNull(method);
 209     return getMethodCompilationLevel0(method, isOsr);
 210   }
 211   private native boolean testSetDontInlineMethod0(Executable method, boolean value);
 212   public         boolean testSetDontInlineMethod(Executable method, boolean value) {
 213     Objects.requireNonNull(method);
 214     return testSetDontInlineMethod0(method, value);
 215   }
 216   public        int     getCompileQueuesSize() {
 217     return getCompileQueueSize(-1 /*any*/);
 218   }
 219   public native int     getCompileQueueSize(int compLevel);
 220   private native boolean testSetForceInlineMethod0(Executable method, boolean value);
 221   public         boolean testSetForceInlineMethod(Executable method, boolean value) {
 222     Objects.requireNonNull(method);
 223     return testSetForceInlineMethod0(method, value);
 224   }
 225   public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
 226     return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
 227   }
 228   private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
 229   public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
 230     Objects.requireNonNull(method);
 231     return enqueueMethodForCompilation0(method, compLevel, entry_bci);
 232   }
 233   private native void    clearMethodState0(Executable method);
 234   public         void    clearMethodState(Executable method) {
 235     Objects.requireNonNull(method);
 236     clearMethodState0(method);
 237   }
 238   public native void    lockCompilation();
 239   public native void    unlockCompilation();
 240   private native int     getMethodEntryBci0(Executable method);
 241   public         int     getMethodEntryBci(Executable method) {
 242     Objects.requireNonNull(method);
 243     return getMethodEntryBci0(method);
 244   }
 245   private native Object[] getNMethod0(Executable method, boolean isOsr);
 246   public         Object[] getNMethod(Executable method, boolean isOsr) {
 247     Objects.requireNonNull(method);
 248     return getNMethod0(method, isOsr);
 249   }
 250   public native long    allocateCodeBlob(int size, int type);
 251   public        long    allocateCodeBlob(long size, int type) {
 252       int intSize = (int) size;
 253       if ((long) intSize != size || size < 0) {
 254           throw new IllegalArgumentException(
 255                 "size argument has illegal value " + size);
 256       }
 257       return allocateCodeBlob( intSize, type);
 258   }
 259   public native void    freeCodeBlob(long addr);
 260   public native void    forceNMethodSweep();
 261   public native Object[] getCodeHeapEntries(int type);
 262   public native int     getCompilationActivityMode();
 263   public native Object[] getCodeBlob(long addr);
 264 
 265   // Intered strings
 266   public native boolean isInStringTable(String str);
 267 
 268   // Memory
 269   public native void readReservedMemory();
 270   public native long allocateMetaspace(ClassLoader classLoader, long size);
 271   public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
 272   public native long incMetaspaceCapacityUntilGC(long increment);
 273   public native long metaspaceCapacityUntilGC();
 274 
 275   // Force Young GC
 276   public native void youngGC();
 277 
 278   // Force Full GC
 279   public native void fullGC();
 280 
 281   // Method tries to start concurrent mark cycle.
 282   // It returns false if CM Thread is always in concurrent cycle.
 283   public native boolean g1StartConcMarkCycle();
 284 
 285   // Tests on ReservedSpace/VirtualSpace classes
 286   public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
 287   public native void runMemoryUnitTests();
 288   public native void readFromNoaccessArea();
 289   public native long getThreadStackSize();
 290   public native long getThreadRemainingStackSize();
 291 
 292   // CPU features
 293   public native String getCPUFeatures();
 294 
 295   // Native extensions
 296   public native long getHeapUsageForContext(int context);
 297   public native long getHeapRegionCountForContext(int context);
 298   private native int getContextForObject0(Object obj);
 299   public         int getContextForObject(Object obj) {
 300     Objects.requireNonNull(obj);
 301     return getContextForObject0(obj);
 302   }
 303   public native void printRegionInfo(int context);
 304 
 305   // VM flags
 306   public native boolean isConstantVMFlag(String name);
 307   public native boolean isLockedVMFlag(String name);
 308   public native void    setBooleanVMFlag(String name, boolean value);
 309   public native void    setIntxVMFlag(String name, long value);
 310   public native void    setUintxVMFlag(String name, long value);
 311   public native void    setUint64VMFlag(String name, long value);
 312   public native void    setSizeTVMFlag(String name, long value);
 313   public native void    setStringVMFlag(String name, String value);
 314   public native void    setDoubleVMFlag(String name, double value);
 315   public native Boolean getBooleanVMFlag(String name);
 316   public native Long    getIntxVMFlag(String name);
 317   public native Long    getUintxVMFlag(String name);
 318   public native Long    getUint64VMFlag(String name);
 319   public native Long    getSizeTVMFlag(String name);
 320   public native String  getStringVMFlag(String name);
 321   public native Double  getDoubleVMFlag(String name);
 322   private final List<Function<String,Object>> flagsGetters = Arrays.asList(
 323     this::getBooleanVMFlag, this::getIntxVMFlag, this::getUintxVMFlag,
 324     this::getUint64VMFlag, this::getSizeTVMFlag, this::getStringVMFlag,
 325     this::getDoubleVMFlag);
 326 
 327   public Object getVMFlag(String name) {
 328     return flagsGetters.stream()
 329                        .map(f -> f.apply(name))
 330                        .filter(x -> x != null)
 331                        .findAny()
 332                        .orElse(null);
 333   }
 334   public native int getOffsetForName0(String name);
 335   public int getOffsetForName(String name) throws Exception {
 336     int offset = getOffsetForName0(name);
 337     if (offset == -1) {
 338       throw new RuntimeException(name + " not found");
 339     }
 340     return offset;
 341   }
 342   public native Boolean getMethodBooleanOption(Executable method, String name);
 343   public native Long    getMethodIntxOption(Executable method, String name);
 344   public native Long    getMethodUintxOption(Executable method, String name);
 345   public native Double  getMethodDoubleOption(Executable method, String name);
 346   public native String  getMethodStringOption(Executable method, String name);
 347   private final List<BiFunction<Executable,String,Object>> methodOptionGetters
 348       = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
 349           this::getMethodUintxOption, this::getMethodDoubleOption,
 350           this::getMethodStringOption);
 351 
 352   public Object getMethodOption(Executable method, String name) {
 353     return methodOptionGetters.stream()
 354                               .map(f -> f.apply(method, name))
 355                               .filter(x -> x != null)
 356                               .findAny()
 357                               .orElse(null);
 358   }
 359 
 360   // Safepoint Checking
 361   public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
 362 }