1 /* 2 * Copyright (c) 2012, 2020, 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 package sun.hotspot; 25 26 import java.lang.management.MemoryUsage; 27 import java.lang.reflect.Executable; 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.function.BiFunction; 31 import java.util.function.Function; 32 import java.security.BasicPermission; 33 import java.util.Objects; 34 35 import sun.hotspot.parser.DiagnosticCommand; 36 37 public class WhiteBox { 38 @SuppressWarnings("serial") 39 public static class WhiteBoxPermission extends BasicPermission { 40 public WhiteBoxPermission(String s) { 41 super(s); 42 } 43 } 44 45 private WhiteBox() {} 46 private static final WhiteBox instance = new WhiteBox(); 47 private static native void registerNatives(); 48 49 /** 50 * Returns the singleton WhiteBox instance. 51 * 52 * The returned WhiteBox object should be carefully guarded 53 * by the caller, since it can be used to read and write data 54 * at arbitrary memory addresses. It must never be passed to 55 * untrusted code. 56 */ 57 public synchronized static WhiteBox getWhiteBox() { 58 SecurityManager sm = System.getSecurityManager(); 59 if (sm != null) { 60 sm.checkPermission(new WhiteBoxPermission("getInstance")); 61 } 62 return instance; 63 } 64 65 static { 66 registerNatives(); 67 } 68 69 // Get the maximum heap size supporting COOPs 70 public native long getCompressedOopsMaxHeapSize(); 71 // Arguments 72 public native void printHeapSizes(); 73 74 // Memory 75 private native long getObjectAddress0(Object o); 76 public long getObjectAddress(Object o) { 77 Objects.requireNonNull(o); 78 return getObjectAddress0(o); 79 } 80 81 public native int getHeapOopSize(); 82 public native int getVMPageSize(); 83 public native long getVMAllocationGranularity(); 84 public native long getVMLargePageSize(); 85 public native long getHeapSpaceAlignment(); 86 public native long getHeapAlignment(); 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 int countAliveClasses(String name) { 103 return countAliveClasses0(name.replace('.', '/')); 104 } 105 public native int countAliveClasses0(String name); 106 107 public boolean isClassAlive(String name) { 108 return countAliveClasses(name) != 0; 109 } 110 111 public native int getSymbolRefcount(String name); 112 113 private native boolean isMonitorInflated0(Object obj); 114 public boolean isMonitorInflated(Object obj) { 115 Objects.requireNonNull(obj); 116 return isMonitorInflated0(obj); 117 } 118 119 public native void forceSafepoint(); 120 121 private native long getConstantPool0(Class<?> aClass); 122 public long getConstantPool(Class<?> aClass) { 123 Objects.requireNonNull(aClass); 124 return getConstantPool0(aClass); 125 } 126 127 private native int getConstantPoolCacheIndexTag0(); 128 public int getConstantPoolCacheIndexTag() { 129 return getConstantPoolCacheIndexTag0(); 130 } 131 132 private native int getConstantPoolCacheLength0(Class<?> aClass); 133 public int getConstantPoolCacheLength(Class<?> aClass) { 134 Objects.requireNonNull(aClass); 135 return getConstantPoolCacheLength0(aClass); 136 } 137 138 private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index); 139 public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) { 140 Objects.requireNonNull(aClass); 141 return remapInstructionOperandFromCPCache0(aClass, index); 142 } 143 144 private native int encodeConstantPoolIndyIndex0(int index); 145 public int encodeConstantPoolIndyIndex(int index) { 146 return encodeConstantPoolIndyIndex0(index); 147 } 148 149 // JVMTI 150 private native void addToBootstrapClassLoaderSearch0(String segment); 151 public void addToBootstrapClassLoaderSearch(String segment){ 152 Objects.requireNonNull(segment); 153 addToBootstrapClassLoaderSearch0(segment); 154 } 155 156 private native void addToSystemClassLoaderSearch0(String segment); 157 public void addToSystemClassLoaderSearch(String segment) { 158 Objects.requireNonNull(segment); 159 addToSystemClassLoaderSearch0(segment); 160 } 161 162 // G1 163 public native boolean g1InConcurrentMark(); 164 private native boolean g1IsHumongous0(Object o); 165 public boolean g1IsHumongous(Object o) { 166 Objects.requireNonNull(o); 167 return g1IsHumongous0(o); 168 } 169 170 private native boolean g1BelongsToHumongousRegion0(long adr); 171 public boolean g1BelongsToHumongousRegion(long adr) { 172 if (adr == 0) { 173 throw new IllegalArgumentException("adr argument should not be null"); 174 } 175 return g1BelongsToHumongousRegion0(adr); 176 } 177 178 179 private native boolean g1BelongsToFreeRegion0(long adr); 180 public boolean g1BelongsToFreeRegion(long adr) { 181 if (adr == 0) { 182 throw new IllegalArgumentException("adr argument should not be null"); 183 } 184 return g1BelongsToFreeRegion0(adr); 185 } 186 187 public native long g1NumMaxRegions(); 188 public native long g1NumFreeRegions(); 189 public native int g1RegionSize(); 190 public native long dramReservedStart(); 191 public native long dramReservedEnd(); 192 public native long nvdimmReservedStart(); 193 public native long nvdimmReservedEnd(); 194 public native MemoryUsage g1AuxiliaryMemoryUsage(); 195 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 196 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 197 Objects.requireNonNull(args); 198 return parseCommandLine0(commandline, delim, args); 199 } 200 201 public native int g1ActiveMemoryNodeCount(); 202 public native int[] g1MemoryNodeIds(); 203 204 // Parallel GC 205 public native long psVirtualSpaceAlignment(); 206 public native long psHeapGenerationAlignment(); 207 208 /** 209 * Enumerates old regions with liveness less than specified and produces some statistics 210 * @param liveness percent of region's liveness (live_objects / total_region_size * 100). 211 * @return long[3] array where long[0] - total count of old regions 212 * long[1] - total memory of old regions 213 * long[2] - lowest estimation of total memory of old regions to be freed (non-full 214 * regions are not included) 215 */ 216 public native long[] g1GetMixedGCInfo(int liveness); 217 218 // NMT 219 public native long NMTMalloc(long size); 220 public native void NMTFree(long mem); 221 public native long NMTReserveMemory(long size); 222 public native long NMTAttemptReserveMemoryAt(long addr, long size); 223 public native void NMTCommitMemory(long addr, long size); 224 public native void NMTUncommitMemory(long addr, long size); 225 public native void NMTReleaseMemory(long addr, long size); 226 public native long NMTMallocWithPseudoStack(long size, int index); 227 public native long NMTMallocWithPseudoStackAndType(long size, int index, int type); 228 public native boolean NMTChangeTrackingLevel(); 229 public native int NMTGetHashSize(); 230 public native long NMTNewArena(long initSize); 231 public native void NMTFreeArena(long arena); 232 public native void NMTArenaMalloc(long arena, long size); 233 234 // Compiler 235 public native boolean isC2OrJVMCIIncludedInVmBuild(); 236 237 public native int matchesMethod(Executable method, String pattern); 238 public native int matchesInline(Executable method, String pattern); 239 public native boolean shouldPrintAssembly(Executable method, int comp_level); 240 public native int deoptimizeFrames(boolean makeNotEntrant); 241 public native void deoptimizeAll(); 242 243 public boolean isMethodCompiled(Executable method) { 244 return isMethodCompiled(method, false /*not osr*/); 245 } 246 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 247 public boolean isMethodCompiled(Executable method, boolean isOsr){ 248 Objects.requireNonNull(method); 249 return isMethodCompiled0(method, isOsr); 250 } 251 public boolean isMethodCompilable(Executable method) { 252 return isMethodCompilable(method, -2 /*any*/); 253 } 254 public boolean isMethodCompilable(Executable method, int compLevel) { 255 return isMethodCompilable(method, compLevel, false /*not osr*/); 256 } 257 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 258 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 259 Objects.requireNonNull(method); 260 return isMethodCompilable0(method, compLevel, isOsr); 261 } 262 private native boolean isMethodQueuedForCompilation0(Executable method); 263 public boolean isMethodQueuedForCompilation(Executable method) { 264 Objects.requireNonNull(method); 265 return isMethodQueuedForCompilation0(method); 266 } 267 // Determine if the compiler corresponding to the compilation level 'compLevel' 268 // and to the compilation context 'compilation_context' provides an intrinsic 269 // for the method 'method'. An intrinsic is available for method 'method' if: 270 // - the intrinsic is enabled (by using the appropriate command-line flag) and 271 // - the platform on which the VM is running provides the instructions necessary 272 // for the compiler to generate the intrinsic code. 273 // 274 // The compilation context is related to using the DisableIntrinsic flag on a 275 // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp 276 // for more details. 277 public boolean isIntrinsicAvailable(Executable method, 278 Executable compilationContext, 279 int compLevel) { 280 Objects.requireNonNull(method); 281 return isIntrinsicAvailable0(method, compilationContext, compLevel); 282 } 283 // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored), 284 // use the below method that does not require the compilation context as argument. 285 public boolean isIntrinsicAvailable(Executable method, int compLevel) { 286 return isIntrinsicAvailable(method, null, compLevel); 287 } 288 private native boolean isIntrinsicAvailable0(Executable method, 289 Executable compilationContext, 290 int compLevel); 291 public int deoptimizeMethod(Executable method) { 292 return deoptimizeMethod(method, false /*not osr*/); 293 } 294 private native int deoptimizeMethod0(Executable method, boolean isOsr); 295 public int deoptimizeMethod(Executable method, boolean isOsr) { 296 Objects.requireNonNull(method); 297 return deoptimizeMethod0(method, isOsr); 298 } 299 public void makeMethodNotCompilable(Executable method) { 300 makeMethodNotCompilable(method, -2 /*any*/); 301 } 302 public void makeMethodNotCompilable(Executable method, int compLevel) { 303 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 304 } 305 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 306 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 307 Objects.requireNonNull(method); 308 makeMethodNotCompilable0(method, compLevel, isOsr); 309 } 310 public int getMethodCompilationLevel(Executable method) { 311 return getMethodCompilationLevel(method, false /*not ost*/); 312 } 313 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 314 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 315 Objects.requireNonNull(method); 316 return getMethodCompilationLevel0(method, isOsr); 317 } 318 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 319 public boolean testSetDontInlineMethod(Executable method, boolean value) { 320 Objects.requireNonNull(method); 321 return testSetDontInlineMethod0(method, value); 322 } 323 public int getCompileQueuesSize() { 324 return getCompileQueueSize(-2 /*any*/); 325 } 326 public native int getCompileQueueSize(int compLevel); 327 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 328 public boolean testSetForceInlineMethod(Executable method, boolean value) { 329 Objects.requireNonNull(method); 330 return testSetForceInlineMethod0(method, value); 331 } 332 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 333 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 334 } 335 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 336 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 337 Objects.requireNonNull(method); 338 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 339 } 340 private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel); 341 public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) { 342 Objects.requireNonNull(aClass); 343 return enqueueInitializerForCompilation0(aClass, compLevel); 344 } 345 private native void clearMethodState0(Executable method); 346 public native void markMethodProfiled(Executable method); 347 public void clearMethodState(Executable method) { 348 Objects.requireNonNull(method); 349 clearMethodState0(method); 350 } 351 public native void lockCompilation(); 352 public native void unlockCompilation(); 353 private native int getMethodEntryBci0(Executable method); 354 public int getMethodEntryBci(Executable method) { 355 Objects.requireNonNull(method); 356 return getMethodEntryBci0(method); 357 } 358 private native Object[] getNMethod0(Executable method, boolean isOsr); 359 public Object[] getNMethod(Executable method, boolean isOsr) { 360 Objects.requireNonNull(method); 361 return getNMethod0(method, isOsr); 362 } 363 public native long allocateCodeBlob(int size, int type); 364 public long allocateCodeBlob(long size, int type) { 365 int intSize = (int) size; 366 if ((long) intSize != size || size < 0) { 367 throw new IllegalArgumentException( 368 "size argument has illegal value " + size); 369 } 370 return allocateCodeBlob( intSize, type); 371 } 372 public native void freeCodeBlob(long addr); 373 public native void forceNMethodSweep(); 374 public native Object[] getCodeHeapEntries(int type); 375 public native int getCompilationActivityMode(); 376 private native long getMethodData0(Executable method); 377 public long getMethodData(Executable method) { 378 Objects.requireNonNull(method); 379 return getMethodData0(method); 380 } 381 public native Object[] getCodeBlob(long addr); 382 383 private native void clearInlineCaches0(boolean preserve_static_stubs); 384 public void clearInlineCaches() { 385 clearInlineCaches0(false); 386 } 387 public void clearInlineCaches(boolean preserve_static_stubs) { 388 clearInlineCaches0(preserve_static_stubs); 389 } 390 391 // Intered strings 392 public native boolean isInStringTable(String str); 393 394 // Memory 395 public native void readReservedMemory(); 396 public native long allocateMetaspace(ClassLoader classLoader, long size); 397 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 398 public native long incMetaspaceCapacityUntilGC(long increment); 399 public native long metaspaceCapacityUntilGC(); 400 public native long metaspaceReserveAlignment(); 401 402 // Don't use these methods directly 403 // Use sun.hotspot.gc.GC class instead. 404 public native boolean isGCSupported(int name); 405 public native boolean isGCSelected(int name); 406 public native boolean isGCSelectedErgonomically(); 407 408 // Force Young GC 409 public native void youngGC(); 410 411 // Force Full GC 412 public native void fullGC(); 413 414 // Returns true if the current GC supports control of its concurrent 415 // phase via requestConcurrentGCPhase(). If false, a request will 416 // always fail. 417 public native boolean supportsConcurrentGCPhaseControl(); 418 419 // Attempt to put the collector into the indicated concurrent phase, 420 // and attempt to remain in that state until a new request is made. 421 // 422 // Returns immediately if already in the requested phase. 423 // Otherwise, waits until the phase is reached. 424 // 425 // Throws IllegalStateException if unsupported by the current collector. 426 // Throws NullPointerException if phase is null. 427 // Throws IllegalArgumentException if phase is not valid for the current collector. 428 public void requestConcurrentGCPhase(String phase) { 429 if (!supportsConcurrentGCPhaseControl()) { 430 throw new IllegalStateException("Concurrent GC phase control not supported"); 431 } else if (phase == null) { 432 throw new NullPointerException("null phase"); 433 } else if (!requestConcurrentGCPhase0(phase)) { 434 throw new IllegalArgumentException("Unknown concurrent GC phase: " + phase); 435 } 436 } 437 438 // Helper for requestConcurrentGCPhase(). Returns true if request 439 // succeeded, false if the phase is invalid. 440 private native boolean requestConcurrentGCPhase0(String phase); 441 442 // Method tries to start concurrent mark cycle. 443 // It returns false if CM Thread is always in concurrent cycle. 444 public native boolean g1StartConcMarkCycle(); 445 446 // Tests on ReservedSpace/VirtualSpace classes 447 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 448 public native void runMemoryUnitTests(); 449 public native void readFromNoaccessArea(); 450 public native long getThreadStackSize(); 451 public native long getThreadRemainingStackSize(); 452 453 // CPU features 454 public native String getCPUFeatures(); 455 456 // VM flags 457 public native boolean isConstantVMFlag(String name); 458 public native boolean isLockedVMFlag(String name); 459 public native void setBooleanVMFlag(String name, boolean value); 460 public native void setIntVMFlag(String name, long value); 461 public native void setUintVMFlag(String name, long value); 462 public native void setIntxVMFlag(String name, long value); 463 public native void setUintxVMFlag(String name, long value); 464 public native void setUint64VMFlag(String name, long value); 465 public native void setSizeTVMFlag(String name, long value); 466 public native void setStringVMFlag(String name, String value); 467 public native void setDoubleVMFlag(String name, double value); 468 public native Boolean getBooleanVMFlag(String name); 469 public native Long getIntVMFlag(String name); 470 public native Long getUintVMFlag(String name); 471 public native Long getIntxVMFlag(String name); 472 public native Long getUintxVMFlag(String name); 473 public native Long getUint64VMFlag(String name); 474 public native Long getSizeTVMFlag(String name); 475 public native String getStringVMFlag(String name); 476 public native Double getDoubleVMFlag(String name); 477 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 478 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 479 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 480 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 481 482 public Object getVMFlag(String name) { 483 return flagsGetters.stream() 484 .map(f -> f.apply(name)) 485 .filter(x -> x != null) 486 .findAny() 487 .orElse(null); 488 } 489 490 // Jigsaw 491 public native void DefineModule(Object module, boolean is_open, String version, 492 String location, Object[] packages); 493 public native void AddModuleExports(Object from_module, String pkg, Object to_module); 494 public native void AddReadsModule(Object from_module, Object source_module); 495 public native void AddModuleExportsToAllUnnamed(Object module, String pkg); 496 public native void AddModuleExportsToAll(Object module, String pkg); 497 498 public native int getOffsetForName0(String name); 499 public int getOffsetForName(String name) throws Exception { 500 int offset = getOffsetForName0(name); 501 if (offset == -1) { 502 throw new RuntimeException(name + " not found"); 503 } 504 return offset; 505 } 506 public native Boolean getMethodBooleanOption(Executable method, String name); 507 public native Long getMethodIntxOption(Executable method, String name); 508 public native Long getMethodUintxOption(Executable method, String name); 509 public native Double getMethodDoubleOption(Executable method, String name); 510 public native String getMethodStringOption(Executable method, String name); 511 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 512 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 513 this::getMethodUintxOption, this::getMethodDoubleOption, 514 this::getMethodStringOption); 515 516 public Object getMethodOption(Executable method, String name) { 517 return methodOptionGetters.stream() 518 .map(f -> f.apply(method, name)) 519 .filter(x -> x != null) 520 .findAny() 521 .orElse(null); 522 } 523 524 // Safepoint Checking 525 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 526 public native void assertSpecialLock(boolean allowVMBlock, boolean safepointCheck); 527 528 // Sharing & archiving 529 public native String getDefaultArchivePath(); 530 public native boolean cdsMemoryMappingFailed(); 531 public native boolean isSharingEnabled(); 532 public native boolean isShared(Object o); 533 public native boolean isSharedClass(Class<?> c); 534 public native boolean areSharedStringsIgnored(); 535 public native boolean isCDSIncludedInVmBuild(); 536 public native boolean isJFRIncludedInVmBuild(); 537 public native boolean isJavaHeapArchiveSupported(); 538 public native Object getResolvedReferences(Class<?> c); 539 public native void linkClass(Class<?> c); 540 public native boolean areOpenArchiveHeapObjectsMapped(); 541 542 // Compiler Directive 543 public native int addCompilerDirective(String compDirect); 544 public native void removeCompilerDirective(int count); 545 546 // Handshakes 547 public native int handshakeWalkStack(Thread t, boolean all_threads); 548 549 // Returns true on linux if library has the noexecstack flag set. 550 public native boolean checkLibSpecifiesNoexecstack(String libfilename); 551 552 // Container testing 553 public native boolean isContainerized(); 554 public native void printOsInfo(); 555 556 // Decoder 557 public native void disableElfSectionCache(); 558 559 // Resolved Method Table 560 public native long resolvedMethodItemsCount(); 561 562 // Protection Domain Table 563 public native int protectionDomainRemovedCount(); 564 565 // Number of loaded AOT libraries 566 public native int aotLibrariesCount(); 567 568 public native int getKlassMetadataSize(Class<?> c); 569 }