1 /* 2 * Copyright (c) 2011, 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 jdk.internal.jvmci.hotspot; 25 26 import static jdk.internal.jvmci.inittimer.InitTimer.timer; 27 28 import java.lang.reflect.Constructor; 29 import java.lang.reflect.Method; 30 31 import jdk.internal.jvmci.code.InstalledCode; 32 import jdk.internal.jvmci.code.InvalidInstalledCodeException; 33 import jdk.internal.jvmci.code.TargetDescription; 34 import jdk.internal.jvmci.hotspotvmconfig.HotSpotVMField; 35 import jdk.internal.jvmci.inittimer.InitTimer; 36 import jdk.internal.jvmci.meta.JavaType; 37 import jdk.internal.jvmci.meta.ResolvedJavaMethod; 38 import jdk.internal.jvmci.meta.ResolvedJavaType; 39 import jdk.internal.jvmci.meta.SpeculationLog; 40 import sun.misc.Unsafe; 41 42 /** 43 * Calls from Java into HotSpot. The behavior of all the methods in this class that take a native 44 * pointer as an argument (e.g., {@link #getSymbol(long)}) is undefined if the argument does not 45 * denote a valid native object. 46 */ 47 public final class CompilerToVM { 48 /** 49 * Initializes the native part of the JVMCI runtime. 50 */ 51 private static native void init(); 52 53 static { 54 initialize(); 55 } 56 57 @SuppressWarnings("try") 58 private static void initialize() { 59 try (InitTimer t = timer("CompilerToVMImpl.init")) { 60 init(); 61 } 62 } 63 64 /** 65 * Copies the original bytecode of {@code method} into a new byte array and returns it. 66 * 67 * @return a new byte array containing the original bytecode of {@code method} 68 */ 69 native byte[] getBytecode(HotSpotResolvedJavaMethodImpl method); 70 71 /** 72 * Gets the number of entries in {@code method}'s exception handler table or 0 if it has not 73 * exception handler table. 74 */ 75 native int getExceptionTableLength(HotSpotResolvedJavaMethodImpl method); 76 77 /** 78 * Gets the address of the first entry in {@code method}'s exception handler table. 79 * 80 * Each entry is a native object described by these fields: 81 * 82 * <ul> 83 * <li>{@link HotSpotVMConfig#exceptionTableElementSize}</li> 84 * <li>{@link HotSpotVMConfig#exceptionTableElementStartPcOffset}</li> 85 * <li>{@link HotSpotVMConfig#exceptionTableElementEndPcOffset}</li> 86 * <li>{@link HotSpotVMConfig#exceptionTableElementHandlerPcOffset}</li> 87 * <li>{@link HotSpotVMConfig#exceptionTableElementCatchTypeIndexOffset} 88 * </ul> 89 * 90 * @return 0 if {@code method} has no exception handlers (i.e. 91 * {@code getExceptionTableLength(method) == 0}) 92 */ 93 native long getExceptionTableStart(HotSpotResolvedJavaMethodImpl method); 94 95 /** 96 * Determines if {@code method} has balanced monitors. 97 */ 98 native boolean hasBalancedMonitors(HotSpotResolvedJavaMethodImpl method); 99 100 /** 101 * Determines if {@code method} can be inlined. A method may not be inlinable for a number of 102 * reasons such as: 103 * <ul> 104 * <li>a CompileOracle directive may prevent inlining or compilation of methods</li> 105 * <li>the method may have a bytecode breakpoint set</li> 106 * <li>the method may have other bytecode features that require special handling by the VM</li> 107 * </ul> 108 */ 109 native boolean canInlineMethod(HotSpotResolvedJavaMethodImpl method); 110 111 /** 112 * Determines if {@code method} should be inlined at any cost. This could be because: 113 * <ul> 114 * <li>a CompileOracle directive may forces inlining of this methods</li> 115 * <li>an annotation forces inlining of this method</li> 116 * </ul> 117 */ 118 native boolean shouldInlineMethod(HotSpotResolvedJavaMethodImpl method); 119 120 /** 121 * Used to implement {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)}. 122 * 123 * @param method the method on which to base the search 124 * @param actualHolderType the best known type of receiver 125 * @return the method result or 0 is there is no unique concrete method for {@code method} 126 */ 127 native HotSpotResolvedJavaMethodImpl findUniqueConcreteMethod(HotSpotResolvedObjectTypeImpl actualHolderType, HotSpotResolvedJavaMethodImpl method); 128 129 /** 130 * Gets the implementor for the interface class {@code type}. 131 * 132 * @return the implementor if there is a single implementor, 0 if there is no implementor, or 133 * {@code type} itself if there is more than one implementor 134 */ 135 native HotSpotResolvedObjectTypeImpl getImplementor(HotSpotResolvedObjectTypeImpl type); 136 137 /** 138 * Determines if {@code method} is ignored by security stack walks. 139 */ 140 native boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMethodImpl method); 141 142 /** 143 * Converts a name to a type. 144 * 145 * @param name a well formed Java type in {@linkplain JavaType#getName() internal} format 146 * @param accessingClass the context of resolution (must not be null) 147 * @param resolve force resolution to a {@link ResolvedJavaType}. If true, this method will 148 * either return a {@link ResolvedJavaType} or throw an exception 149 * @return the type for {@code name} or 0 if resolution failed and {@code resolve == false} 150 * @throws LinkageError if {@code resolve == true} and the resolution failed 151 */ 152 native HotSpotResolvedObjectTypeImpl lookupType(String name, Class<?> accessingClass, boolean resolve); 153 154 /** 155 * Resolves the entry at index {@code cpi} in {@code constantPool} to an object. 156 * 157 * The behavior of this method is undefined if {@code cpi} does not denote an entry that can be 158 * resolved to an object. 159 */ 160 native Object resolveConstantInPool(HotSpotConstantPool constantPool, int cpi); 161 162 /** 163 * Resolves the entry at index {@code cpi} in {@code constantPool} to an object, looking in the 164 * constant pool cache first. 165 * 166 * The behavior of this method is undefined if {@code cpi} does not denote an entry that can be 167 * resolved to an object. 168 */ 169 native Object resolvePossiblyCachedConstantInPool(HotSpotConstantPool constantPool, int cpi); 170 171 /** 172 * Gets the {@code JVM_CONSTANT_NameAndType} index from the entry at index {@code cpi} in 173 * {@code constantPool}. 174 * 175 * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a 176 * {@code JVM_CONSTANT_NameAndType} index. 177 */ 178 native int lookupNameAndTypeRefIndexInPool(HotSpotConstantPool constantPool, int cpi); 179 180 /** 181 * Gets the name of the {@code JVM_CONSTANT_NameAndType} entry at index {@code cpi} in 182 * {@code constantPool}. 183 * 184 * The behavior of this method is undefined if {@code cpi} does not denote a 185 * {@code JVM_CONSTANT_NameAndType} entry. 186 */ 187 native String lookupNameRefInPool(HotSpotConstantPool constantPool, int cpi); 188 189 /** 190 * Gets the signature of the {@code JVM_CONSTANT_NameAndType} entry at index {@code cpi} in 191 * {@code constantPool}. 192 * 193 * The behavior of this method is undefined if {@code cpi} does not denote a 194 * {@code JVM_CONSTANT_NameAndType} entry. 195 */ 196 native String lookupSignatureRefInPool(HotSpotConstantPool constantPool, int cpi); 197 198 /** 199 * Gets the {@code JVM_CONSTANT_Class} index from the entry at index {@code cpi} in 200 * {@code constantPool}. 201 * 202 * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a 203 * {@code JVM_CONSTANT_Class} index. 204 */ 205 native int lookupKlassRefIndexInPool(HotSpotConstantPool constantPool, int cpi); 206 207 /** 208 * Looks up a class denoted by the {@code JVM_CONSTANT_Class} entry at index {@code cpi} in 209 * {@code constantPool}. This method does not perform any resolution. 210 * 211 * The behavior of this method is undefined if {@code cpi} does not denote a 212 * {@code JVM_CONSTANT_Class} entry. 213 * 214 * @return the resolved class entry or a String otherwise 215 */ 216 native Object lookupKlassInPool(HotSpotConstantPool constantPool, int cpi); 217 218 /** 219 * Looks up a method denoted by the entry at index {@code cpi} in {@code constantPool}. This 220 * method does not perform any resolution. 221 * 222 * The behavior of this method is undefined if {@code cpi} does not denote an entry representing 223 * a method. 224 * 225 * @param opcode the opcode of the instruction for which the lookup is being performed or 226 * {@code -1}. If non-negative, then resolution checks specific to the bytecode it 227 * denotes are performed if the method is already resolved. Should any of these 228 * checks fail, 0 is returned. 229 * @return the resolved method entry, 0 otherwise 230 */ 231 native HotSpotResolvedJavaMethodImpl lookupMethodInPool(HotSpotConstantPool constantPool, int cpi, byte opcode); 232 233 /** 234 * Ensures that the type referenced by the specified {@code JVM_CONSTANT_InvokeDynamic} entry at 235 * index {@code cpi} in {@code constantPool} is loaded and initialized. 236 * 237 * The behavior of this method is undefined if {@code cpi} does not denote a 238 * {@code JVM_CONSTANT_InvokeDynamic} entry. 239 */ 240 native void resolveInvokeDynamicInPool(HotSpotConstantPool constantPool, int cpi); 241 242 /** 243 * Ensures that the type referenced by the entry for a <a 244 * href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9">signature 245 * polymorphic</a> method at index {@code cpi} in {@code constantPool} is loaded and 246 * initialized. 247 * 248 * The behavior of this method is undefined if {@code cpi} does not denote an entry representing 249 * a signature polymorphic method. 250 */ 251 native void resolveInvokeHandleInPool(HotSpotConstantPool constantPool, int cpi); 252 253 /** 254 * Gets the resolved type denoted by the entry at index {@code cpi} in {@code constantPool}. 255 * 256 * The behavior of this method is undefined if {@code cpi} does not denote an entry representing 257 * a class. 258 * 259 * @throws LinkageError if resolution failed 260 */ 261 native HotSpotResolvedObjectTypeImpl resolveTypeInPool(HotSpotConstantPool constantPool, int cpi) throws LinkageError; 262 263 /** 264 * Looks up and attempts to resolve the {@code JVM_CONSTANT_Field} entry at index {@code cpi} in 265 * {@code constantPool}. The values returned in {@code info} are: 266 * 267 * <pre> 268 * [(int) flags, // only valid if field is resolved 269 * (int) offset] // only valid if field is resolved 270 * </pre> 271 * 272 * The behavior of this method is undefined if {@code cpi} does not denote a 273 * {@code JVM_CONSTANT_Field} entry. 274 * 275 * @param info an array in which the details of the field are returned 276 * @return the type defining the field if resolution is successful, 0 otherwise 277 */ 278 native HotSpotResolvedObjectTypeImpl resolveFieldInPool(HotSpotConstantPool constantPool, int cpi, byte opcode, long[] info); 279 280 /** 281 * Converts {@code cpci} from an index into the cache for {@code constantPool} to an index 282 * directly into {@code constantPool}. 283 * 284 * The behavior of this method is undefined if {@code ccpi} is an invalid constant pool cache 285 * index. 286 */ 287 native int constantPoolRemapInstructionOperandFromCache(HotSpotConstantPool constantPool, int cpci); 288 289 /** 290 * Gets the appendix object (if any) associated with the entry at index {@code cpi} in 291 * {@code constantPool}. 292 */ 293 native Object lookupAppendixInPool(HotSpotConstantPool constantPool, int cpi); 294 295 /** 296 * Installs the result of a compilation into the code cache. 297 * 298 * @param target the target where this code should be installed 299 * @param compiledCode the result of a compilation 300 * @param code the details of the installed CodeBlob are written to this object 301 * @return the outcome of the installation which will be one of 302 * {@link HotSpotVMConfig#codeInstallResultOk}, 303 * {@link HotSpotVMConfig#codeInstallResultCacheFull}, 304 * {@link HotSpotVMConfig#codeInstallResultCodeTooLarge}, 305 * {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or 306 * {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}. 307 */ 308 public native int installCode(TargetDescription target, HotSpotCompiledCode compiledCode, InstalledCode code, SpeculationLog speculationLog); 309 310 public native int getMetadata(TargetDescription target, HotSpotCompiledCode compiledCode, HotSpotMetaData metaData); 311 312 /** 313 * Notifies the VM of statistics for a completed compilation. 314 * 315 * @param id the identifier of the compilation 316 * @param method the method compiled 317 * @param osr specifies if the compilation was for on-stack-replacement 318 * @param processedBytecodes the number of bytecodes processed during the compilation, including 319 * the bytecodes of all inlined methods 320 * @param time the amount time spent compiling {@code method} 321 * @param timeUnitsPerSecond the granularity of the units for the {@code time} value 322 * @param installedCode the nmethod installed as a result of the compilation 323 */ 324 public synchronized native void notifyCompilationStatistics(int id, HotSpotResolvedJavaMethodImpl method, boolean osr, int processedBytecodes, long time, long timeUnitsPerSecond, 325 InstalledCode installedCode); 326 327 /** 328 * Resets all compilation statistics. 329 */ 330 public native void resetCompilationStatistics(); 331 332 /** 333 * Initializes the fields of {@code config}. 334 */ 335 native long initializeConfiguration(); 336 337 /** 338 * Resolves the implementation of {@code method} for virtual dispatches on objects of dynamic 339 * type {@code exactReceiver}. This resolution process only searches "up" the class hierarchy of 340 * {@code exactReceiver}. 341 * 342 * @param caller the caller or context type used to perform access checks 343 * @return the link-time resolved method (might be abstract) or {@code 0} if it can not be 344 * linked 345 */ 346 native HotSpotResolvedJavaMethodImpl resolveMethod(HotSpotResolvedObjectTypeImpl exactReceiver, HotSpotResolvedJavaMethodImpl method, HotSpotResolvedObjectTypeImpl caller); 347 348 /** 349 * Gets the static initializer of {@code type}. 350 * 351 * @return 0 if {@code type} has no static initializer 352 */ 353 native HotSpotResolvedJavaMethodImpl getClassInitializer(HotSpotResolvedObjectTypeImpl type); 354 355 /** 356 * Determines if {@code type} or any of its currently loaded subclasses overrides 357 * {@code Object.finalize()}. 358 */ 359 native boolean hasFinalizableSubclass(HotSpotResolvedObjectTypeImpl type); 360 361 /** 362 * Gets the method corresponding to {@code holder} and slot number {@code slot} (i.e. 363 * {@link Method#slot} or {@link Constructor#slot}). 364 */ 365 native HotSpotResolvedJavaMethodImpl getResolvedJavaMethodAtSlot(Class<?> holder, int slot); 366 367 /** 368 * Gets the maximum absolute offset of a PC relative call to {@code address} from any position 369 * in the code cache. 370 * 371 * @param address an address that may be called from any code in the code cache 372 * @return -1 if {@code address == 0} 373 */ 374 public native long getMaxCallTargetOffset(long address); 375 376 /** 377 * Gets a textual disassembly of {@code codeBlob}. 378 * 379 * @return a non-zero length string containing a disassembly of {@code codeBlob} or null if 380 * {@code codeBlob} could not be disassembled for some reason 381 */ 382 // The HotSpot disassembler seems not to be thread safe so it's better to synchronize its usage 383 public synchronized native String disassembleCodeBlob(long codeBlob); 384 385 /** 386 * Gets a stack trace element for {@code method} at bytecode index {@code bci}. 387 */ 388 native StackTraceElement getStackTraceElement(HotSpotResolvedJavaMethodImpl method, int bci); 389 390 /** 391 * Executes some {@code installedCode} with arguments {@code args}. 392 * 393 * @return the result of executing {@code installedCode} 394 * @throws InvalidInstalledCodeException if {@code installedCode} has been invalidated 395 */ 396 native Object executeInstalledCode(Object[] args, InstalledCode installedCode) throws InvalidInstalledCodeException; 397 398 /** 399 * Gets the line number table for {@code method}. The line number table is encoded as (bci, 400 * source line number) pairs. 401 * 402 * @return the line number table for {@code method} or null if it doesn't have one 403 */ 404 native long[] getLineNumberTable(HotSpotResolvedJavaMethodImpl method); 405 406 /** 407 * Gets the number of entries in the local variable table for {@code method}. 408 * 409 * @return the number of entries in the local variable table for {@code method} 410 */ 411 native int getLocalVariableTableLength(HotSpotResolvedJavaMethodImpl method); 412 413 /** 414 * Gets the address of the first entry in the local variable table for {@code method}. 415 * 416 * Each entry is a native object described by these fields: 417 * 418 * <ul> 419 * <li>{@link HotSpotVMConfig#localVariableTableElementSize}</li> 420 * <li>{@link HotSpotVMConfig#localVariableTableElementLengthOffset}</li> 421 * <li>{@link HotSpotVMConfig#localVariableTableElementNameCpIndexOffset}</li> 422 * <li>{@link HotSpotVMConfig#localVariableTableElementDescriptorCpIndexOffset}</li> 423 * <li>{@link HotSpotVMConfig#localVariableTableElementSignatureCpIndexOffset} 424 * <li>{@link HotSpotVMConfig#localVariableTableElementSlotOffset} 425 * <li>{@link HotSpotVMConfig#localVariableTableElementStartBciOffset} 426 * </ul> 427 * 428 * @return 0 if {@code method} does not have a local variable table 429 */ 430 native long getLocalVariableTableStart(HotSpotResolvedJavaMethodImpl method); 431 432 /** 433 * Reads an object pointer within a VM data structure. That is, any {@link HotSpotVMField} whose 434 * {@link HotSpotVMField#type() type} is {@code "oop"} (e.g., 435 * {@code ArrayKlass::_component_mirror}, {@code Klass::_java_mirror}, 436 * {@code JavaThread::_threadObj}). 437 * 438 * Note that {@link Unsafe#getObject(Object, long)} cannot be used for this since it does a 439 * {@code narrowOop} read if the VM is using compressed oops whereas oops within VM data 440 * structures are (currently) always uncompressed. 441 * 442 * @param address address of an oop field within a VM data structure 443 */ 444 native Object readUncompressedOop(long address); 445 446 /** 447 * Determines if {@code method} should not be inlined or compiled. 448 */ 449 native void doNotInlineOrCompile(HotSpotResolvedJavaMethodImpl method); 450 451 /** 452 * Invalidates the profiling information for {@code method} and (re)initializes it such that 453 * profiling restarts upon its next invocation. 454 */ 455 native void reprofile(HotSpotResolvedJavaMethodImpl method); 456 457 /** 458 * Invalidates {@code installedCode} such that {@link InvalidInstalledCodeException} will be 459 * raised the next time {@code installedCode} is executed. 460 */ 461 public native void invalidateInstalledCode(InstalledCode installedCode); 462 463 /** 464 * Collects the current values of all JVMCI benchmark counters, summed up over all threads. 465 */ 466 public native long[] collectCounters(); 467 468 /** 469 * Determines if {@code metaspaceMethodData} is mature. 470 */ 471 native boolean isMature(long metaspaceMethodData); 472 473 /** 474 * Generate a unique id to identify the result of the compile. 475 */ 476 native int allocateCompileId(HotSpotResolvedJavaMethodImpl method, int entryBCI); 477 478 /** 479 * Determines if {@code method} has OSR compiled code identified by {@code entryBCI} for 480 * compilation level {@code level}. 481 */ 482 native boolean hasCompiledCodeForOSR(HotSpotResolvedJavaMethodImpl method, int entryBCI, int level); 483 484 /** 485 * Gets the value of {@code metaspaceSymbol} as a String. 486 */ 487 native String getSymbol(long metaspaceSymbol); 488 489 /** 490 * Looks for the next Java stack frame matching an entry in {@code methods}. 491 * 492 * @param frame the starting point of the search, where {@code null} refers to the topmost frame 493 * @param methods the methods to look for, where {@code null} means that any frame is returned 494 * @return the frame, or {@code null} if the end of the stack was reached during the search 495 */ 496 public native HotSpotStackFrameReference getNextStackFrame(HotSpotStackFrameReference frame, HotSpotResolvedJavaMethodImpl[] methods, int initialSkip); 497 498 /** 499 * Materializes all virtual objects within {@code stackFrame} updates its locals. 500 * 501 * @param invalidate if {@code true}, the compiled method for the stack frame will be 502 * invalidated. 503 */ 504 native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate); 505 506 /** 507 * Gets the v-table index for interface method {@code method} in the receiver {@code type} or 508 * {@link HotSpotVMConfig#invalidVtableIndex} if {@code method} is not in {@code type}'s 509 * v-table. 510 */ 511 native int getVtableIndexForInterface(HotSpotResolvedObjectTypeImpl type, HotSpotResolvedJavaMethodImpl method); 512 513 /** 514 * Determines if debug info should also be emitted at non-safepoint locations. 515 */ 516 public native boolean shouldDebugNonSafepoints(); 517 518 /** 519 * Writes {@code length} bytes from {@code buf} starting at offset {@code offset} to the 520 * HotSpot's log stream. No range checking is performed. 521 */ 522 public native void writeDebugOutput(byte[] bytes, int offset, int length); 523 524 /** 525 * Flush HotSpot's log stream. 526 */ 527 public native void flushDebugOutput(); 528 529 /** 530 * Read a value representing a metaspace Method* and return the 531 * {@link HotSpotResolvedJavaMethodImpl} wrapping it. This method does no checking that the 532 * location actually contains a valid Method*. If the {@code base} object is a 533 * {@link MetaspaceWrapperObject} then the metaspace pointer is fetched from that object and 534 * used as the base. Otherwise the object itself is used as the base. 535 * 536 * @param base an object to read from or null 537 * @param displacement 538 * @return null or the resolved method for this location 539 */ 540 native HotSpotResolvedJavaMethodImpl getResolvedJavaMethod(Object base, long displacement); 541 542 /** 543 * Read a value representing a metaspace ConstantPool* and return the 544 * {@link HotSpotConstantPool} wrapping it. This method does no checking that the location 545 * actually contains a valid ConstantPool*. If the {@code base} object is a 546 * {@link MetaspaceWrapperObject} then the metaspace pointer is fetched from that object and 547 * used as the base. Otherwise the object itself is used as the base. 548 * 549 * @param base an object to read from or null 550 * @param displacement 551 * @return null or the resolved method for this location 552 */ 553 native HotSpotConstantPool getConstantPool(Object base, long displacement); 554 555 /** 556 * Read a value representing a metaspace Klass* and return the 557 * {@link HotSpotResolvedObjectTypeImpl} wrapping it. The method does no checking that the 558 * location actually contains a valid Klass*. If the {@code base} object is a 559 * {@link MetaspaceWrapperObject} then the metaspace pointer is fetched from that object and 560 * used as the base. Otherwise the object itself is used as the base. 561 * 562 * @param base an object to read from or null 563 * @param displacement 564 * @param compressed true if the location contains a compressed Klass* 565 * @return null or the resolved method for this location 566 */ 567 native HotSpotResolvedObjectTypeImpl getResolvedJavaType(Object base, long displacement, boolean compressed); 568 }