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