1 /* 2 * Copyright (c) 2012, 2018, 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 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 private native int getConstantPoolCacheIndexTag0(); 122 public int getConstantPoolCacheIndexTag() { 123 return getConstantPoolCacheIndexTag0(); 124 } 125 126 private native int getConstantPoolCacheLength0(Class<?> aClass); 127 public int getConstantPoolCacheLength(Class<?> aClass) { 128 Objects.requireNonNull(aClass); 129 return getConstantPoolCacheLength0(aClass); 130 } 131 132 private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index); 133 public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) { 134 Objects.requireNonNull(aClass); 135 return remapInstructionOperandFromCPCache0(aClass, index); 136 } 137 138 private native int encodeConstantPoolIndyIndex0(int index); 139 public int encodeConstantPoolIndyIndex(int index) { 140 return encodeConstantPoolIndyIndex0(index); 141 } 142 143 // JVMTI 144 private native void addToBootstrapClassLoaderSearch0(String segment); 145 public void addToBootstrapClassLoaderSearch(String segment){ 146 Objects.requireNonNull(segment); 147 addToBootstrapClassLoaderSearch0(segment); 148 } 149 150 private native void addToSystemClassLoaderSearch0(String segment); 151 public void addToSystemClassLoaderSearch(String segment) { 152 Objects.requireNonNull(segment); 153 addToSystemClassLoaderSearch0(segment); 154 } 155 156 // G1 157 public native boolean g1InConcurrentMark(); 158 private native boolean g1IsHumongous0(Object o); 159 public boolean g1IsHumongous(Object o) { 160 Objects.requireNonNull(o); 161 return g1IsHumongous0(o); 162 } 163 164 private native boolean g1BelongsToHumongousRegion0(long adr); 165 public boolean g1BelongsToHumongousRegion(long adr) { 166 if (adr == 0) { 167 throw new IllegalArgumentException("adr argument should not be null"); 168 } 169 return g1BelongsToHumongousRegion0(adr); 170 } 171 172 173 private native boolean g1BelongsToFreeRegion0(long adr); 174 public boolean g1BelongsToFreeRegion(long adr) { 175 if (adr == 0) { 176 throw new IllegalArgumentException("adr argument should not be null"); 177 } 178 return g1BelongsToFreeRegion0(adr); 179 } 180 181 public native long g1NumMaxRegions(); 182 public native long g1NumFreeRegions(); 183 public native int g1RegionSize(); 184 public native MemoryUsage g1AuxiliaryMemoryUsage(); 185 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 186 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 187 Objects.requireNonNull(args); 188 return parseCommandLine0(commandline, delim, args); 189 } 190 191 // Parallel GC 192 public native long psVirtualSpaceAlignment(); 193 public native long psHeapGenerationAlignment(); 194 195 /** 196 * Enumerates old regions with liveness less than specified and produces some statistics 197 * @param liveness percent of region's liveness (live_objects / total_region_size * 100). 198 * @return long[3] array where long[0] - total count of old regions 199 * long[1] - total memory of old regions 200 * long[2] - lowest estimation of total memory of old regions to be freed (non-full 201 * regions are not included) 202 */ 203 public native long[] g1GetMixedGCInfo(int liveness); 204 205 // NMT 206 public native long NMTMalloc(long size); 207 public native void NMTFree(long mem); 208 public native long NMTReserveMemory(long size); 209 public native long NMTAttemptReserveMemoryAt(long addr, long size); 210 public native void NMTCommitMemory(long addr, long size); 211 public native void NMTUncommitMemory(long addr, long size); 212 public native void NMTReleaseMemory(long addr, long size); 213 public native long NMTMallocWithPseudoStack(long size, int index); 214 public native boolean NMTChangeTrackingLevel(); 215 public native int NMTGetHashSize(); 216 217 // Compiler 218 public native int matchesMethod(Executable method, String pattern); 219 public native int matchesInline(Executable method, String pattern); 220 public native boolean shouldPrintAssembly(Executable method, int comp_level); 221 public native int deoptimizeFrames(boolean makeNotEntrant); 222 public native void deoptimizeAll(); 223 224 public boolean isMethodCompiled(Executable method) { 225 return isMethodCompiled(method, false /*not osr*/); 226 } 227 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 228 public boolean isMethodCompiled(Executable method, boolean isOsr){ 229 Objects.requireNonNull(method); 230 return isMethodCompiled0(method, isOsr); 231 } 232 public boolean isMethodCompilable(Executable method) { 233 return isMethodCompilable(method, -2 /*any*/); 234 } 235 public boolean isMethodCompilable(Executable method, int compLevel) { 236 return isMethodCompilable(method, compLevel, false /*not osr*/); 237 } 238 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 239 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 240 Objects.requireNonNull(method); 241 return isMethodCompilable0(method, compLevel, isOsr); 242 } 243 private native boolean isMethodQueuedForCompilation0(Executable method); 244 public boolean isMethodQueuedForCompilation(Executable method) { 245 Objects.requireNonNull(method); 246 return isMethodQueuedForCompilation0(method); 247 } 248 // Determine if the compiler corresponding to the compilation level 'compLevel' 249 // and to the compilation context 'compilation_context' provides an intrinsic 250 // for the method 'method'. An intrinsic is available for method 'method' if: 251 // - the intrinsic is enabled (by using the appropriate command-line flag) and 252 // - the platform on which the VM is running provides the instructions necessary 253 // for the compiler to generate the intrinsic code. 254 // 255 // The compilation context is related to using the DisableIntrinsic flag on a 256 // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp 257 // for more details. 258 public boolean isIntrinsicAvailable(Executable method, 259 Executable compilationContext, 260 int compLevel) { 261 Objects.requireNonNull(method); 262 return isIntrinsicAvailable0(method, compilationContext, compLevel); 263 } 264 // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored), 265 // use the below method that does not require the compilation context as argument. 266 public boolean isIntrinsicAvailable(Executable method, int compLevel) { 267 return isIntrinsicAvailable(method, null, compLevel); 268 } 269 private native boolean isIntrinsicAvailable0(Executable method, 270 Executable compilationContext, 271 int compLevel); 272 public int deoptimizeMethod(Executable method) { 273 return deoptimizeMethod(method, false /*not osr*/); 274 } 275 private native int deoptimizeMethod0(Executable method, boolean isOsr); 276 public int deoptimizeMethod(Executable method, boolean isOsr) { 277 Objects.requireNonNull(method); 278 return deoptimizeMethod0(method, isOsr); 279 } 280 public void makeMethodNotCompilable(Executable method) { 281 makeMethodNotCompilable(method, -2 /*any*/); 282 } 283 public void makeMethodNotCompilable(Executable method, int compLevel) { 284 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 285 } 286 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 287 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 288 Objects.requireNonNull(method); 289 makeMethodNotCompilable0(method, compLevel, isOsr); 290 } 291 public int getMethodCompilationLevel(Executable method) { 292 return getMethodCompilationLevel(method, false /*not ost*/); 293 } 294 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 295 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 296 Objects.requireNonNull(method); 297 return getMethodCompilationLevel0(method, isOsr); 298 } 299 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 300 public boolean testSetDontInlineMethod(Executable method, boolean value) { 301 Objects.requireNonNull(method); 302 return testSetDontInlineMethod0(method, value); 303 } 304 public int getCompileQueuesSize() { 305 return getCompileQueueSize(-2 /*any*/); 306 } 307 public native int getCompileQueueSize(int compLevel); 308 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 309 public boolean testSetForceInlineMethod(Executable method, boolean value) { 310 Objects.requireNonNull(method); 311 return testSetForceInlineMethod0(method, value); 312 } 313 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 314 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 315 } 316 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 317 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 318 Objects.requireNonNull(method); 319 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 320 } 321 private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel); 322 public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) { 323 Objects.requireNonNull(aClass); 324 return enqueueInitializerForCompilation0(aClass, compLevel); 325 } 326 private native void clearMethodState0(Executable method); 327 public void clearMethodState(Executable method) { 328 Objects.requireNonNull(method); 329 clearMethodState0(method); 330 } 331 public native void lockCompilation(); 332 public native void unlockCompilation(); 333 private native int getMethodEntryBci0(Executable method); 334 public int getMethodEntryBci(Executable method) { 335 Objects.requireNonNull(method); 336 return getMethodEntryBci0(method); 337 } 338 private native Object[] getNMethod0(Executable method, boolean isOsr); 339 public Object[] getNMethod(Executable method, boolean isOsr) { 340 Objects.requireNonNull(method); 341 return getNMethod0(method, isOsr); 342 } 343 public native long allocateCodeBlob(int size, int type); 344 public long allocateCodeBlob(long size, int type) { 345 int intSize = (int) size; 346 if ((long) intSize != size || size < 0) { 347 throw new IllegalArgumentException( 348 "size argument has illegal value " + size); 349 } 350 return allocateCodeBlob( intSize, type); 351 } 352 public native void freeCodeBlob(long addr); 353 public native void forceNMethodSweep(); 354 public native Object[] getCodeHeapEntries(int type); 355 public native int getCompilationActivityMode(); 356 private native long getMethodData0(Executable method); 357 public long getMethodData(Executable method) { 358 Objects.requireNonNull(method); 359 return getMethodData0(method); 360 } 361 public native Object[] getCodeBlob(long addr); 362 363 private native void clearInlineCaches0(boolean preserve_static_stubs); 364 public void clearInlineCaches() { 365 clearInlineCaches0(false); 366 } 367 public void clearInlineCaches(boolean preserve_static_stubs) { 368 clearInlineCaches0(preserve_static_stubs); 369 } 370 371 // Intered strings 372 public native boolean isInStringTable(String str); 373 374 // Memory 375 public native void readReservedMemory(); 376 public native long allocateMetaspace(ClassLoader classLoader, long size); 377 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 378 public native long incMetaspaceCapacityUntilGC(long increment); 379 public native long metaspaceCapacityUntilGC(); 380 public native boolean metaspaceShouldConcurrentCollect(); 381 public native long metaspaceReserveAlignment(); 382 383 // Don't use these methods directly 384 // Use sun.hotspot.gc.GC class instead. 385 public native boolean isGCSupported(int name); 386 public native boolean isGCSelected(int name); 387 public native boolean isGCSelectedErgonomically(); 388 389 // Force Young GC 390 public native void youngGC(); 391 392 // Force Full GC 393 public native void fullGC(); 394 395 // Returns true if the current GC supports control of its concurrent 396 // phase via requestConcurrentGCPhase(). If false, a request will 397 // always fail. 398 public native boolean supportsConcurrentGCPhaseControl(); 399 400 // Returns an array of concurrent phase names provided by this 401 // collector. These are the names recognized by 402 // requestConcurrentGCPhase(). 403 public native String[] getConcurrentGCPhases(); 404 405 // Attempt to put the collector into the indicated concurrent phase, 406 // and attempt to remain in that state until a new request is made. 407 // 408 // Returns immediately if already in the requested phase. 409 // Otherwise, waits until the phase is reached. 410 // 411 // Throws IllegalStateException if unsupported by the current collector. 412 // Throws NullPointerException if phase is null. 413 // Throws IllegalArgumentException if phase is not valid for the current collector. 414 public void requestConcurrentGCPhase(String phase) { 415 if (!supportsConcurrentGCPhaseControl()) { 416 throw new IllegalStateException("Concurrent GC phase control not supported"); 417 } else if (phase == null) { 418 throw new NullPointerException("null phase"); 419 } else if (!requestConcurrentGCPhase0(phase)) { 420 throw new IllegalArgumentException("Unknown concurrent GC phase: " + phase); 421 } 422 } 423 424 // Helper for requestConcurrentGCPhase(). Returns true if request 425 // succeeded, false if the phase is invalid. 426 private native boolean requestConcurrentGCPhase0(String phase); 427 428 // Method tries to start concurrent mark cycle. 429 // It returns false if CM Thread is always in concurrent cycle. 430 public native boolean g1StartConcMarkCycle(); 431 432 // Tests on ReservedSpace/VirtualSpace classes 433 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 434 public native void runMemoryUnitTests(); 435 public native void readFromNoaccessArea(); 436 public native long getThreadStackSize(); 437 public native long getThreadRemainingStackSize(); 438 439 // CPU features 440 public native String getCPUFeatures(); 441 442 // VM flags 443 public native boolean isConstantVMFlag(String name); 444 public native boolean isLockedVMFlag(String name); 445 public native void setBooleanVMFlag(String name, boolean value); 446 public native void setIntVMFlag(String name, long value); 447 public native void setUintVMFlag(String name, long value); 448 public native void setIntxVMFlag(String name, long value); 449 public native void setUintxVMFlag(String name, long value); 450 public native void setUint64VMFlag(String name, long value); 451 public native void setSizeTVMFlag(String name, long value); 452 public native void setStringVMFlag(String name, String value); 453 public native void setDoubleVMFlag(String name, double value); 454 public native Boolean getBooleanVMFlag(String name); 455 public native Long getIntVMFlag(String name); 456 public native Long getUintVMFlag(String name); 457 public native Long getIntxVMFlag(String name); 458 public native Long getUintxVMFlag(String name); 459 public native Long getUint64VMFlag(String name); 460 public native Long getSizeTVMFlag(String name); 461 public native String getStringVMFlag(String name); 462 public native Double getDoubleVMFlag(String name); 463 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 464 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 465 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 466 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 467 468 public Object getVMFlag(String name) { 469 return flagsGetters.stream() 470 .map(f -> f.apply(name)) 471 .filter(x -> x != null) 472 .findAny() 473 .orElse(null); 474 } 475 476 // Jigsaw 477 public native void DefineModule(Object module, boolean is_open, String version, 478 String location, Object[] packages); 479 public native void AddModuleExports(Object from_module, String pkg, Object to_module); 480 public native void AddReadsModule(Object from_module, Object source_module); 481 public native void AddModuleExportsToAllUnnamed(Object module, String pkg); 482 public native void AddModuleExportsToAll(Object module, String pkg); 483 484 public native int getOffsetForName0(String name); 485 public int getOffsetForName(String name) throws Exception { 486 int offset = getOffsetForName0(name); 487 if (offset == -1) { 488 throw new RuntimeException(name + " not found"); 489 } 490 return offset; 491 } 492 public native Boolean getMethodBooleanOption(Executable method, String name); 493 public native Long getMethodIntxOption(Executable method, String name); 494 public native Long getMethodUintxOption(Executable method, String name); 495 public native Double getMethodDoubleOption(Executable method, String name); 496 public native String getMethodStringOption(Executable method, String name); 497 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 498 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 499 this::getMethodUintxOption, this::getMethodDoubleOption, 500 this::getMethodStringOption); 501 502 public Object getMethodOption(Executable method, String name) { 503 return methodOptionGetters.stream() 504 .map(f -> f.apply(method, name)) 505 .filter(x -> x != null) 506 .findAny() 507 .orElse(null); 508 } 509 510 // Safepoint Checking 511 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 512 513 // Sharing & archiving 514 public native boolean isShared(Object o); 515 public native boolean isSharedClass(Class<?> c); 516 public native boolean areSharedStringsIgnored(); 517 public native boolean isCDSIncludedInVmBuild(); 518 public native boolean isJFRIncludedInVmBuild(); 519 public native boolean isJavaHeapArchiveSupported(); 520 public native Object getResolvedReferences(Class<?> c); 521 public native boolean areOpenArchiveHeapObjectsMapped(); 522 523 // Compiler Directive 524 public native int addCompilerDirective(String compDirect); 525 public native void removeCompilerDirective(int count); 526 527 // Handshakes 528 public native int handshakeWalkStack(Thread t, boolean all_threads); 529 530 // Returns true on linux if library has the noexecstack flag set. 531 public native boolean checkLibSpecifiesNoexecstack(String libfilename); 532 533 // Container testing 534 public native boolean isContainerized(); 535 public native void printOsInfo(); 536 537 // Decoder 538 public native void disableElfSectionCache(); 539 }