1 /* 2 * Copyright (c) 2011, 2019, 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.vm.ci.hotspot; 25 26 import static jdk.vm.ci.common.InitTimer.timer; 27 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; 28 29 import java.lang.reflect.Executable; 30 import java.lang.reflect.Field; 31 32 import jdk.vm.ci.code.BytecodeFrame; 33 import jdk.vm.ci.code.InstalledCode; 34 import jdk.vm.ci.code.InvalidInstalledCodeException; 35 import jdk.vm.ci.code.TargetDescription; 36 import jdk.vm.ci.code.stack.InspectedFrameVisitor; 37 import jdk.vm.ci.common.InitTimer; 38 import jdk.vm.ci.common.JVMCIError; 39 import jdk.vm.ci.meta.Constant; 40 import jdk.vm.ci.meta.ConstantReflectionProvider; 41 import jdk.vm.ci.meta.JavaConstant; 42 import jdk.vm.ci.meta.JavaKind; 43 import jdk.vm.ci.meta.JavaType; 44 import jdk.vm.ci.meta.ResolvedJavaMethod; 45 import jdk.vm.ci.meta.ResolvedJavaType; 46 47 /** 48 * Calls from Java into HotSpot. The behavior of all the methods in this class that take a native 49 * pointer as an argument (e.g., {@link #getSymbol(long)}) is undefined if the argument does not 50 * denote a valid native object. 51 */ 52 final class CompilerToVM { 53 /** 54 * Initializes the native part of the JVMCI runtime. 55 */ 56 private static native void registerNatives(); 57 58 /** 59 * These values mirror the equivalent values from {@link Unsafe} but are approriate for the JVM 60 * being compiled against. 61 */ 62 // Checkstyle: stop 63 final int ARRAY_BOOLEAN_BASE_OFFSET; 64 final int ARRAY_BYTE_BASE_OFFSET; 65 final int ARRAY_SHORT_BASE_OFFSET; 66 final int ARRAY_CHAR_BASE_OFFSET; 67 final int ARRAY_INT_BASE_OFFSET; 68 final int ARRAY_LONG_BASE_OFFSET; 69 final int ARRAY_FLOAT_BASE_OFFSET; 70 final int ARRAY_DOUBLE_BASE_OFFSET; 71 final int ARRAY_OBJECT_BASE_OFFSET; 72 final int ARRAY_BOOLEAN_INDEX_SCALE; 73 final int ARRAY_BYTE_INDEX_SCALE; 74 final int ARRAY_SHORT_INDEX_SCALE; 75 final int ARRAY_CHAR_INDEX_SCALE; 76 final int ARRAY_INT_INDEX_SCALE; 77 final int ARRAY_LONG_INDEX_SCALE; 78 final int ARRAY_FLOAT_INDEX_SCALE; 79 final int ARRAY_DOUBLE_INDEX_SCALE; 80 final int ARRAY_OBJECT_INDEX_SCALE; 81 // Checkstyle: resume 82 83 @SuppressWarnings("try") 84 CompilerToVM() { 85 try (InitTimer t = timer("CompilerToVM.registerNatives")) { 86 registerNatives(); 87 ARRAY_BOOLEAN_BASE_OFFSET = arrayBaseOffset(JavaKind.Boolean); 88 ARRAY_BYTE_BASE_OFFSET = arrayBaseOffset(JavaKind.Byte); 89 ARRAY_SHORT_BASE_OFFSET = arrayBaseOffset(JavaKind.Short); 90 ARRAY_CHAR_BASE_OFFSET = arrayBaseOffset(JavaKind.Char); 91 ARRAY_INT_BASE_OFFSET = arrayBaseOffset(JavaKind.Int); 92 ARRAY_LONG_BASE_OFFSET = arrayBaseOffset(JavaKind.Long); 93 ARRAY_FLOAT_BASE_OFFSET = arrayBaseOffset(JavaKind.Float); 94 ARRAY_DOUBLE_BASE_OFFSET = arrayBaseOffset(JavaKind.Double); 95 ARRAY_OBJECT_BASE_OFFSET = arrayBaseOffset(JavaKind.Object); 96 ARRAY_BOOLEAN_INDEX_SCALE = arrayIndexScale(JavaKind.Boolean); 97 ARRAY_BYTE_INDEX_SCALE = arrayIndexScale(JavaKind.Byte); 98 ARRAY_SHORT_INDEX_SCALE = arrayIndexScale(JavaKind.Short); 99 ARRAY_CHAR_INDEX_SCALE = arrayIndexScale(JavaKind.Char); 100 ARRAY_INT_INDEX_SCALE = arrayIndexScale(JavaKind.Int); 101 ARRAY_LONG_INDEX_SCALE = arrayIndexScale(JavaKind.Long); 102 ARRAY_FLOAT_INDEX_SCALE = arrayIndexScale(JavaKind.Float); 103 ARRAY_DOUBLE_INDEX_SCALE = arrayIndexScale(JavaKind.Double); 104 ARRAY_OBJECT_INDEX_SCALE = arrayIndexScale(JavaKind.Object); 105 } 106 } 107 108 native int arrayBaseOffset(JavaKind kind); 109 110 native int arrayIndexScale(JavaKind kind); 111 112 /** 113 * Gets the {@link CompilerToVM} instance associated with the singleton 114 * {@link HotSpotJVMCIRuntime} instance. 115 */ 116 public static CompilerToVM compilerToVM() { 117 return runtime().getCompilerToVM(); 118 } 119 120 /** 121 * Copies the original bytecode of {@code method} into a new byte array and returns it. 122 * 123 * @return a new byte array containing the original bytecode of {@code method} 124 */ 125 native byte[] getBytecode(HotSpotResolvedJavaMethodImpl method); 126 127 /** 128 * Gets the number of entries in {@code method}'s exception handler table or 0 if it has no 129 * exception handler table. 130 */ 131 native int getExceptionTableLength(HotSpotResolvedJavaMethodImpl method); 132 133 /** 134 * Gets the address of the first entry in {@code method}'s exception handler table. 135 * 136 * Each entry is a native object described by these fields: 137 * 138 * <ul> 139 * <li>{@link HotSpotVMConfig#exceptionTableElementSize}</li> 140 * <li>{@link HotSpotVMConfig#exceptionTableElementStartPcOffset}</li> 141 * <li>{@link HotSpotVMConfig#exceptionTableElementEndPcOffset}</li> 142 * <li>{@link HotSpotVMConfig#exceptionTableElementHandlerPcOffset}</li> 143 * <li>{@link HotSpotVMConfig#exceptionTableElementCatchTypeIndexOffset} 144 * </ul> 145 * 146 * @return 0 if {@code method} has no exception handlers (i.e. 147 * {@code getExceptionTableLength(method) == 0}) 148 */ 149 native long getExceptionTableStart(HotSpotResolvedJavaMethodImpl method); 150 151 /** 152 * Determines whether {@code method} is currently compilable by the JVMCI compiler being used by 153 * the VM. This can return false if JVMCI compilation failed earlier for {@code method}, a 154 * breakpoint is currently set in {@code method} or {@code method} contains other bytecode 155 * features that require special handling by the VM. 156 */ 157 native boolean isCompilable(HotSpotResolvedJavaMethodImpl method); 158 159 /** 160 * Determines if {@code method} is targeted by a VM directive (e.g., 161 * {@code -XX:CompileCommand=dontinline,<pattern>}) or annotation (e.g., 162 * {@code jdk.internal.vm.annotation.DontInline}) that specifies it should not be inlined. 163 */ 164 native boolean hasNeverInlineDirective(HotSpotResolvedJavaMethodImpl method); 165 166 /** 167 * Determines if {@code method} should be inlined at any cost. This could be because: 168 * <ul> 169 * <li>a CompileOracle directive may forces inlining of this methods</li> 170 * <li>an annotation forces inlining of this method</li> 171 * </ul> 172 */ 173 native boolean shouldInlineMethod(HotSpotResolvedJavaMethodImpl method); 174 175 /** 176 * Used to implement {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)}. 177 * 178 * @param method the method on which to base the search 179 * @param actualHolderType the best known type of receiver 180 * @return the method result or 0 is there is no unique concrete method for {@code method} 181 */ 182 native HotSpotResolvedJavaMethodImpl findUniqueConcreteMethod(HotSpotResolvedObjectTypeImpl actualHolderType, HotSpotResolvedJavaMethodImpl method); 183 184 /** 185 * Gets the implementor for the interface class {@code type}. 186 * 187 * @return the implementor if there is a single implementor, {@code null} if there is no 188 * implementor, or {@code type} itself if there is more than one implementor 189 * @throws IllegalArgumentException if type is not an interface type 190 */ 191 native HotSpotResolvedObjectTypeImpl getImplementor(HotSpotResolvedObjectTypeImpl type); 192 193 /** 194 * Determines if {@code method} is ignored by security stack walks. 195 */ 196 native boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMethodImpl method); 197 198 /** 199 * Converts a name to a type. 200 * 201 * @param name a well formed Java type in {@linkplain JavaType#getName() internal} format 202 * @param accessingClass the context of resolution. A value of {@code null} implies that the 203 * class should be resolved with the class loader. 204 * @param resolve force resolution to a {@link ResolvedJavaType}. If true, this method will 205 * either return a {@link ResolvedJavaType} or throw an exception 206 * @return the type for {@code name} or 0 if resolution failed and {@code resolve == false} 207 * @throws ClassNotFoundException if {@code resolve == true} and the resolution failed 208 */ 209 native HotSpotResolvedJavaType lookupType(String name, HotSpotResolvedObjectTypeImpl accessingClass, boolean resolve) throws ClassNotFoundException; 210 211 native HotSpotResolvedJavaType lookupClass(Class<?> javaClass); 212 213 /** 214 * Resolves the entry at index {@code cpi} in {@code constantPool} to an object, looking in the 215 * constant pool cache first. 216 * 217 * The behavior of this method is undefined if {@code cpi} does not denote one of the following 218 * entry types: {@code JVM_CONSTANT_String}, {@code JVM_CONSTANT_MethodHandle}, 219 * {@code JVM_CONSTANT_MethodHandleInError}, {@code JVM_CONSTANT_MethodType} and 220 * {@code JVM_CONSTANT_MethodTypeInError}. 221 */ 222 native HotSpotObjectConstantImpl resolvePossiblyCachedConstantInPool(HotSpotConstantPool constantPool, int cpi); 223 224 /** 225 * Gets the {@code JVM_CONSTANT_NameAndType} index from the entry at index {@code cpi} in 226 * {@code constantPool}. 227 * 228 * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a 229 * {@code JVM_CONSTANT_NameAndType} index. 230 */ 231 native int lookupNameAndTypeRefIndexInPool(HotSpotConstantPool constantPool, int cpi); 232 233 /** 234 * Gets the name of the {@code JVM_CONSTANT_NameAndType} entry referenced by another entry 235 * denoted by {@code which} in {@code constantPool}. 236 * 237 * The behavior of this method is undefined if {@code which} does not denote a entry that 238 * references a {@code JVM_CONSTANT_NameAndType} entry. 239 */ 240 native String lookupNameInPool(HotSpotConstantPool constantPool, int which); 241 242 /** 243 * Gets the signature of the {@code JVM_CONSTANT_NameAndType} entry referenced by another entry 244 * denoted by {@code which} in {@code constantPool}. 245 * 246 * The behavior of this method is undefined if {@code which} does not denote a entry that 247 * references a {@code JVM_CONSTANT_NameAndType} entry. 248 */ 249 native String lookupSignatureInPool(HotSpotConstantPool constantPool, int which); 250 251 /** 252 * Gets the {@code JVM_CONSTANT_Class} index from the entry at index {@code cpi} in 253 * {@code constantPool}. 254 * 255 * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a 256 * {@code JVM_CONSTANT_Class} index. 257 */ 258 native int lookupKlassRefIndexInPool(HotSpotConstantPool constantPool, int cpi); 259 260 /** 261 * Looks up a class denoted by the {@code JVM_CONSTANT_Class} entry at index {@code cpi} in 262 * {@code constantPool}. This method does not perform any resolution. 263 * 264 * The behavior of this method is undefined if {@code cpi} does not denote a 265 * {@code JVM_CONSTANT_Class} entry. 266 * 267 * @return the resolved class entry or a String otherwise 268 */ 269 native Object lookupKlassInPool(HotSpotConstantPool constantPool, int cpi); 270 271 /** 272 * Looks up a method denoted by the entry at index {@code cpi} in {@code constantPool}. This 273 * method does not perform any resolution. 274 * 275 * The behavior of this method is undefined if {@code cpi} does not denote an entry representing 276 * a method. 277 * 278 * @param opcode the opcode of the instruction for which the lookup is being performed or 279 * {@code -1}. If non-negative, then resolution checks specific to the bytecode it 280 * denotes are performed if the method is already resolved. Should any of these 281 * checks fail, 0 is returned. 282 * @return the resolved method entry, 0 otherwise 283 */ 284 native HotSpotResolvedJavaMethodImpl lookupMethodInPool(HotSpotConstantPool constantPool, int cpi, byte opcode); 285 286 // TODO resolving JVM_CONSTANT_Dynamic 287 288 /** 289 * Ensures that the type referenced by the specified {@code JVM_CONSTANT_InvokeDynamic} entry at 290 * index {@code cpi} in {@code constantPool} is loaded and initialized. 291 * 292 * The behavior of this method is undefined if {@code cpi} does not denote a 293 * {@code JVM_CONSTANT_InvokeDynamic} entry. 294 */ 295 native void resolveInvokeDynamicInPool(HotSpotConstantPool constantPool, int cpi); 296 297 /** 298 * If {@code cpi} denotes an entry representing a 299 * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9">signature 300 * polymorphic</a> method, this method ensures that the type referenced by the entry is loaded 301 * and initialized. It {@code cpi} does not denote a signature polymorphic method, this method 302 * does nothing. 303 */ 304 native void resolveInvokeHandleInPool(HotSpotConstantPool constantPool, int cpi); 305 306 /** 307 * If {@code cpi} denotes an entry representing a resolved dynamic adapter (see 308 * {@link #resolveInvokeDynamicInPool} and {@link #resolveInvokeHandleInPool}), return the 309 * opcode of the instruction for which the resolution was performed ({@code invokedynamic} or 310 * {@code invokevirtual}), or {@code -1} otherwise. 311 */ 312 native int isResolvedInvokeHandleInPool(HotSpotConstantPool constantPool, int cpi); 313 314 /** 315 * Gets the list of type names (in the format of {@link JavaType#getName()}) denoting the 316 * classes that define signature polymorphic methods. 317 */ 318 native String[] getSignaturePolymorphicHolders(); 319 320 /** 321 * Gets the resolved type denoted by the entry at index {@code cpi} in {@code constantPool}. 322 * 323 * The behavior of this method is undefined if {@code cpi} does not denote an entry representing 324 * a class. 325 * 326 * @throws LinkageError if resolution failed 327 */ 328 native HotSpotResolvedObjectTypeImpl resolveTypeInPool(HotSpotConstantPool constantPool, int cpi) throws LinkageError; 329 330 /** 331 * Looks up and attempts to resolve the {@code JVM_CONSTANT_Field} entry for at index 332 * {@code cpi} in {@code constantPool}. For some opcodes, checks are performed that require the 333 * {@code method} that contains {@code opcode} to be specified. The values returned in 334 * {@code info} are: 335 * 336 * <pre> 337 * [ flags, // fieldDescriptor::access_flags() 338 * offset, // fieldDescriptor::offset() 339 * index // fieldDescriptor::index() 340 * ] 341 * </pre> 342 * 343 * The behavior of this method is undefined if {@code cpi} does not denote a 344 * {@code JVM_CONSTANT_Field} entry. 345 * 346 * @param info an array in which the details of the field are returned 347 * @return the type defining the field if resolution is successful, 0 otherwise 348 */ 349 native HotSpotResolvedObjectTypeImpl resolveFieldInPool(HotSpotConstantPool constantPool, int cpi, HotSpotResolvedJavaMethodImpl method, byte opcode, int[] info); 350 351 /** 352 * Converts {@code cpci} from an index into the cache for {@code constantPool} to an index 353 * directly into {@code constantPool}. 354 * 355 * The behavior of this method is undefined if {@code ccpi} is an invalid constant pool cache 356 * index. 357 */ 358 native int constantPoolRemapInstructionOperandFromCache(HotSpotConstantPool constantPool, int cpci); 359 360 /** 361 * Gets the appendix object (if any) associated with the entry at index {@code cpi} in 362 * {@code constantPool}. 363 */ 364 native HotSpotObjectConstantImpl lookupAppendixInPool(HotSpotConstantPool constantPool, int cpi); 365 366 /** 367 * Installs the result of a compilation into the code cache. 368 * 369 * @param target the target where this code should be installed 370 * @param compiledCode the result of a compilation 371 * @param code the details of the installed CodeBlob are written to this object 372 * @return the outcome of the installation which will be one of 373 * {@link HotSpotVMConfig#codeInstallResultOk}, 374 * {@link HotSpotVMConfig#codeInstallResultCacheFull}, 375 * {@link HotSpotVMConfig#codeInstallResultCodeTooLarge}, 376 * {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or 377 * {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}. 378 * @throws JVMCIError if there is something wrong with the compiled code or the associated 379 * metadata. 380 */ 381 native int installCode(TargetDescription target, HotSpotCompiledCode compiledCode, InstalledCode code, long failedSpeculationsAddress, byte[] speculations); 382 383 /** 384 * Generates the VM metadata for some compiled code and copies them into {@code metaData}. This 385 * method does not install anything into the code cache. 386 * 387 * @param target the target where this code would be installed 388 * @param compiledCode the result of a compilation 389 * @param metaData the metadata is written to this object 390 * @return the outcome of the installation which will be one of 391 * {@link HotSpotVMConfig#codeInstallResultOk}, 392 * {@link HotSpotVMConfig#codeInstallResultCacheFull}, 393 * {@link HotSpotVMConfig#codeInstallResultCodeTooLarge}, 394 * {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or 395 * {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}. 396 * @throws JVMCIError if there is something wrong with the compiled code or the metadata 397 */ 398 native int getMetadata(TargetDescription target, HotSpotCompiledCode compiledCode, HotSpotMetaData metaData); 399 400 /** 401 * Resets all compilation statistics. 402 */ 403 native void resetCompilationStatistics(); 404 405 /** 406 * Reads the database of VM info. The return value encodes the info in a nested object array 407 * that is described by the pseudo Java object {@code info} below: 408 * 409 * <pre> 410 * info = [ 411 * VMField[] vmFields, 412 * [String name, Long size, ...] vmTypeSizes, 413 * [String name, Long value, ...] vmConstants, 414 * [String name, Long value, ...] vmAddresses, 415 * VMFlag[] vmFlags 416 * VMIntrinsicMethod[] vmIntrinsics 417 * ] 418 * </pre> 419 * 420 * @return VM info as encoded above 421 */ 422 native Object[] readConfiguration(); 423 424 /** 425 * Resolves the implementation of {@code method} for virtual dispatches on objects of dynamic 426 * type {@code exactReceiver}. This resolution process only searches "up" the class hierarchy of 427 * {@code exactReceiver}. 428 * 429 * @param caller the caller or context type used to perform access checks 430 * @return the link-time resolved method (might be abstract) or {@code null} if it is either a 431 * signature polymorphic method or can not be linked. 432 */ 433 native HotSpotResolvedJavaMethodImpl resolveMethod(HotSpotResolvedObjectTypeImpl exactReceiver, HotSpotResolvedJavaMethodImpl method, HotSpotResolvedObjectTypeImpl caller); 434 435 /** 436 * Gets the static initializer of {@code type}. 437 * 438 * @return {@code null} if {@code type} has no static initializer 439 */ 440 native HotSpotResolvedJavaMethodImpl getClassInitializer(HotSpotResolvedObjectTypeImpl type); 441 442 /** 443 * Determines if {@code type} or any of its currently loaded subclasses overrides 444 * {@code Object.finalize()}. 445 */ 446 native boolean hasFinalizableSubclass(HotSpotResolvedObjectTypeImpl type); 447 448 /** 449 * Gets the method corresponding to {@code executable}. 450 */ 451 native HotSpotResolvedJavaMethodImpl asResolvedJavaMethod(Executable executable); 452 453 /** 454 * Gets the maximum absolute offset of a PC relative call to {@code address} from any position 455 * in the code cache. 456 * 457 * @param address an address that may be called from any code in the code cache 458 * @return -1 if {@code address == 0} 459 */ 460 native long getMaxCallTargetOffset(long address); 461 462 /** 463 * Gets a textual disassembly of {@code codeBlob}. 464 * 465 * @return a non-zero length string containing a disassembly of {@code codeBlob} or null if 466 * {@code codeBlob} could not be disassembled for some reason 467 */ 468 // The HotSpot disassembler seems not to be thread safe so it's better to synchronize its usage 469 synchronized native String disassembleCodeBlob(InstalledCode installedCode); 470 471 /** 472 * Gets a stack trace element for {@code method} at bytecode index {@code bci}. 473 */ 474 native StackTraceElement getStackTraceElement(HotSpotResolvedJavaMethodImpl method, int bci); 475 476 /** 477 * Executes some {@code installedCode} with arguments {@code args}. 478 * 479 * @return the result of executing {@code nmethodMirror} 480 * @throws InvalidInstalledCodeException if {@code nmethodMirror} has been invalidated 481 */ 482 native Object executeHotSpotNmethod(Object[] args, HotSpotNmethod nmethodMirror) throws InvalidInstalledCodeException; 483 484 /** 485 * Gets the line number table for {@code method}. The line number table is encoded as (bci, 486 * source line number) pairs. 487 * 488 * @return the line number table for {@code method} or null if it doesn't have one 489 */ 490 native long[] getLineNumberTable(HotSpotResolvedJavaMethodImpl method); 491 492 /** 493 * Gets the number of entries in the local variable table for {@code method}. 494 * 495 * @return the number of entries in the local variable table for {@code method} 496 */ 497 native int getLocalVariableTableLength(HotSpotResolvedJavaMethodImpl method); 498 499 /** 500 * Gets the address of the first entry in the local variable table for {@code method}. 501 * 502 * Each entry is a native object described by these fields: 503 * 504 * <ul> 505 * <li>{@link HotSpotVMConfig#localVariableTableElementSize}</li> 506 * <li>{@link HotSpotVMConfig#localVariableTableElementLengthOffset}</li> 507 * <li>{@link HotSpotVMConfig#localVariableTableElementNameCpIndexOffset}</li> 508 * <li>{@link HotSpotVMConfig#localVariableTableElementDescriptorCpIndexOffset}</li> 509 * <li>{@link HotSpotVMConfig#localVariableTableElementSlotOffset} 510 * <li>{@link HotSpotVMConfig#localVariableTableElementStartBciOffset} 511 * </ul> 512 * 513 * @return 0 if {@code method} does not have a local variable table 514 */ 515 native long getLocalVariableTableStart(HotSpotResolvedJavaMethodImpl method); 516 517 /** 518 * Reads an object pointer within a VM data structure. That is, any {@link VMField} whose 519 * {@link VMField#type type} is {@code "oop"} (e.g., 520 * {@code Klass::_java_mirror}, {@code JavaThread::_threadObj}). 521 * 522 * Note that {@link Unsafe#getObject(Object, long)} cannot be used for this since it does a 523 * {@code narrowOop} read if the VM is using compressed oops whereas oops within VM data 524 * structures are (currently) always uncompressed. 525 * 526 * @param address address of an oop field within a VM data structure 527 */ 528 native HotSpotObjectConstantImpl readUncompressedOop(long address); 529 530 /** 531 * Sets flags on {@code method} indicating that it should never be inlined or compiled by the 532 * VM. 533 */ 534 native void setNotInlinableOrCompilable(HotSpotResolvedJavaMethodImpl method); 535 536 /** 537 * Invalidates the profiling information for {@code method} and (re)initializes it such that 538 * profiling restarts upon its next invocation. 539 */ 540 native void reprofile(HotSpotResolvedJavaMethodImpl method); 541 542 /** 543 * Invalidates {@code nmethodMirror} such that {@link InvalidInstalledCodeException} will be 544 * raised the next time {@code nmethodMirror} is {@linkplain #executeHotSpotNmethod executed}. 545 * The {@code nmethod} associated with {@code nmethodMirror} is also made non-entrant and any 546 * current activations of the {@code nmethod} are deoptimized. 547 */ 548 native void invalidateHotSpotNmethod(HotSpotNmethod nmethodMirror); 549 550 /** 551 * Collects the current values of all JVMCI benchmark counters, summed up over all threads. 552 */ 553 native long[] collectCounters(); 554 555 /** 556 * Get the current number of counters allocated for use by JVMCI. Should be the same value as 557 * the flag {@code JVMCICounterSize}. 558 */ 559 native int getCountersSize(); 560 561 /** 562 * Attempt to change the size of the counters allocated for JVMCI. This requires a safepoint to 563 * safely reallocate the storage but it's advisable to increase the size in reasonable chunks. 564 */ 565 native boolean setCountersSize(int newSize); 566 567 /** 568 * Determines if {@code metaspaceMethodData} is mature. 569 */ 570 native boolean isMature(long metaspaceMethodData); 571 572 /** 573 * Generate a unique id to identify the result of the compile. 574 */ 575 native int allocateCompileId(HotSpotResolvedJavaMethodImpl method, int entryBCI); 576 577 /** 578 * Determines if {@code method} has OSR compiled code identified by {@code entryBCI} for 579 * compilation level {@code level}. 580 */ 581 native boolean hasCompiledCodeForOSR(HotSpotResolvedJavaMethodImpl method, int entryBCI, int level); 582 583 /** 584 * Gets the value of {@code metaspaceSymbol} as a String. 585 */ 586 native String getSymbol(long metaspaceSymbol); 587 588 /** 589 * @see jdk.vm.ci.code.stack.StackIntrospection#iterateFrames 590 */ 591 native <T> T iterateFrames(ResolvedJavaMethod[] initialMethods, ResolvedJavaMethod[] matchingMethods, int initialSkip, InspectedFrameVisitor<T> visitor); 592 593 /** 594 * Materializes all virtual objects within {@code stackFrame} and updates its locals. 595 * 596 * @param invalidate if {@code true}, the compiled method for the stack frame will be 597 * invalidated 598 */ 599 native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate); 600 601 /** 602 * Gets the v-table index for interface method {@code method} in the receiver {@code type} or 603 * {@link HotSpotVMConfig#invalidVtableIndex} if {@code method} is not in {@code type}'s 604 * v-table. 605 * 606 * @throws InternalError if {@code type} is an interface or {@code method} is not held by an 607 * interface or class represented by {@code type} is not initialized 608 */ 609 native int getVtableIndexForInterfaceMethod(HotSpotResolvedObjectTypeImpl type, HotSpotResolvedJavaMethodImpl method); 610 611 /** 612 * Determines if debug info should also be emitted at non-safepoint locations. 613 */ 614 native boolean shouldDebugNonSafepoints(); 615 616 /** 617 * Writes {@code length} bytes from {@code bytes} starting at offset {@code offset} to HotSpot's 618 * log stream. 619 * 620 * @param flush specifies if the log stream should be flushed after writing 621 * @param canThrow specifies if an error in the {@code bytes}, {@code offset} or {@code length} 622 * arguments should result in an exception or a negative return value 623 * @return 0 on success, -1 if {@code bytes == null && !canThrow}, -2 if {@code !canThrow} and 624 * copying would cause access of data outside array bounds 625 * @throws NullPointerException if {@code bytes == null} 626 * @throws IndexOutOfBoundsException if copying would cause access of data outside array bounds 627 */ 628 native int writeDebugOutput(byte[] bytes, int offset, int length, boolean flush, boolean canThrow); 629 630 /** 631 * Flush HotSpot's log stream. 632 */ 633 native void flushDebugOutput(); 634 635 /** 636 * Read a HotSpot Method* value from the memory location described by {@code base} plus 637 * {@code displacement} and return the {@link HotSpotResolvedJavaMethodImpl} wrapping it. This 638 * method does no checking that the memory location actually contains a valid pointer and may 639 * crash the VM if an invalid location is provided. If the {@code base} is null then 640 * {@code displacement} is used by itself. If {@code base} is a 641 * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or 642 * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object 643 * and added to {@code displacement}. Any other non-null object type causes an 644 * {@link IllegalArgumentException} to be thrown. 645 * 646 * @param base an object to read from or null 647 * @param displacement 648 * @return null or the resolved method for this location 649 */ 650 native HotSpotResolvedJavaMethodImpl getResolvedJavaMethod(HotSpotObjectConstantImpl base, long displacement); 651 652 /** 653 * Gets the {@code ConstantPool*} associated with {@code object} and returns a 654 * {@link HotSpotConstantPool} wrapping it. 655 * 656 * @param object a {@link HotSpotResolvedJavaMethodImpl} or 657 * {@link HotSpotResolvedObjectTypeImpl} object 658 * @return a {@link HotSpotConstantPool} wrapping the {@code ConstantPool*} associated with 659 * {@code object} 660 * @throws NullPointerException if {@code object == null} 661 * @throws IllegalArgumentException if {@code object} is neither a 662 * {@link HotSpotResolvedJavaMethodImpl} nor a {@link HotSpotResolvedObjectTypeImpl} 663 */ 664 native HotSpotConstantPool getConstantPool(MetaspaceObject object); 665 666 /** 667 * Read a HotSpot Klass* value from the memory location described by {@code base} plus 668 * {@code displacement} and return the {@link HotSpotResolvedObjectTypeImpl} wrapping it. This 669 * method does no checking that the memory location actually contains a valid pointer and may 670 * crash the VM if an invalid location is provided. If the {@code base} is null then 671 * {@code displacement} is used by itself. If {@code base} is a 672 * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or 673 * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object 674 * and added to {@code displacement}. Any other non-null object type causes an 675 * {@link IllegalArgumentException} to be thrown. 676 * 677 * @param base an object to read from or null 678 * @param displacement 679 * @param compressed true if the location contains a compressed Klass* 680 * @return null or the resolved method for this location 681 */ 682 private native HotSpotResolvedObjectTypeImpl getResolvedJavaType0(Object base, long displacement, boolean compressed); 683 684 HotSpotResolvedObjectTypeImpl getResolvedJavaType(MetaspaceObject base, long displacement, boolean compressed) { 685 return getResolvedJavaType0(base, displacement, compressed); 686 } 687 688 HotSpotResolvedObjectTypeImpl getResolvedJavaType(HotSpotObjectConstantImpl base, long displacement, boolean compressed) { 689 return getResolvedJavaType0(base, displacement, compressed); 690 } 691 692 HotSpotResolvedObjectTypeImpl getResolvedJavaType(long displacement, boolean compressed) { 693 return getResolvedJavaType0(null, displacement, compressed); 694 } 695 696 /** 697 * Return the size of the HotSpot ProfileData* pointed at by {@code position}. If 698 * {@code position} is outside the space of the MethodData then an 699 * {@link IllegalArgumentException} is thrown. A {@code position} inside the MethodData but that 700 * isn't pointing at a valid ProfileData will crash the VM. 701 * 702 * @param metaspaceMethodData 703 * @param position 704 * @return the size of the ProfileData item pointed at by {@code position} 705 * @throws IllegalArgumentException if an out of range position is given 706 */ 707 native int methodDataProfileDataSize(long metaspaceMethodData, int position); 708 709 /** 710 * Gets the fingerprint for a given Klass*. 711 * 712 * @param metaspaceKlass 713 * @return the value of the fingerprint (zero for arrays and synthetic classes). 714 */ 715 native long getFingerprint(long metaspaceKlass); 716 717 /** 718 * Return the amount of native stack required for the interpreter frames represented by 719 * {@code frame}. This is used when emitting the stack banging code to ensure that there is 720 * enough space for the frames during deoptimization. 721 * 722 * @param frame 723 * @return the number of bytes required for deoptimization of this frame state 724 */ 725 native int interpreterFrameSize(BytecodeFrame frame); 726 727 /** 728 * Invokes non-public method {@code java.lang.invoke.LambdaForm.compileToBytecode()} on 729 * {@code lambdaForm} (which must be a {@code java.lang.invoke.LambdaForm} instance). 730 */ 731 native void compileToBytecode(HotSpotObjectConstantImpl lambdaForm); 732 733 /** 734 * Gets the value of the VM flag named {@code name}. 735 * 736 * @param name name of a VM option 737 * @return {@code this} if the named VM option doesn't exist, a {@link String} or {@code null} 738 * if its type is {@code ccstr} or {@code ccstrlist}, a {@link Double} if its type is 739 * {@code double}, a {@link Boolean} if its type is {@code bool} otherwise a 740 * {@link Long} 741 */ 742 native Object getFlagValue(String name); 743 744 /** 745 * Gets the host class for {@code type}. 746 */ 747 native HotSpotResolvedObjectTypeImpl getHostClass(HotSpotResolvedObjectTypeImpl type); 748 749 /** 750 * Gets the object at the address {@code oopAddress}. 751 * 752 * @param oopAddress a valid {@code oopDesc**} value 753 */ 754 native Object getObjectAtAddress(long oopAddress); 755 756 /** 757 * @see ResolvedJavaType#getInterfaces() 758 */ 759 native HotSpotResolvedObjectTypeImpl[] getInterfaces(HotSpotResolvedObjectTypeImpl type); 760 761 /** 762 * @see ResolvedJavaType#getComponentType() 763 */ 764 native HotSpotResolvedJavaType getComponentType(HotSpotResolvedObjectTypeImpl type); 765 766 /** 767 * Get the array class for {@code type}. This can't be done symbolically since anonymous types 768 * can't be looked up by name. 769 */ 770 native HotSpotResolvedObjectTypeImpl getArrayType(HotSpotResolvedJavaType type); 771 772 /** 773 * Forces initialization of {@code type}. 774 */ 775 native void ensureInitialized(HotSpotResolvedObjectTypeImpl type); 776 777 /** 778 * Checks if {@code object} is a String and is an interned string value. 779 */ 780 native boolean isInternedString(HotSpotObjectConstantImpl object); 781 782 /** 783 * Gets the {@linkplain System#identityHashCode(Object) identity} has code for the object 784 * represented by this constant. 785 */ 786 native int getIdentityHashCode(HotSpotObjectConstantImpl object); 787 788 /** 789 * Converts a constant object representing a boxed primitive into a boxed primitive. 790 */ 791 native Object unboxPrimitive(HotSpotObjectConstantImpl object); 792 793 /** 794 * Converts a boxed primitive into a JavaConstant representing the same value. 795 */ 796 native HotSpotObjectConstantImpl boxPrimitive(Object source); 797 798 /** 799 * Gets the {@link ResolvedJavaMethod}s for all the constructors of the type {@code holder}. 800 */ 801 native ResolvedJavaMethod[] getDeclaredConstructors(HotSpotResolvedObjectTypeImpl holder); 802 803 /** 804 * Gets the {@link ResolvedJavaMethod}s for all the non-constructor methods of the type 805 * {@code holder}. 806 */ 807 native ResolvedJavaMethod[] getDeclaredMethods(HotSpotResolvedObjectTypeImpl holder); 808 809 /** 810 * Reads the current value of a static field. 811 */ 812 native JavaConstant readFieldValue(HotSpotResolvedObjectTypeImpl resolvedObjectType, HotSpotResolvedJavaField field, boolean isVolatile); 813 814 /** 815 * Reads the current value of an instance field. 816 */ 817 native JavaConstant readFieldValue(HotSpotObjectConstantImpl object, HotSpotResolvedJavaField field, boolean isVolatile); 818 819 /** 820 * @see ResolvedJavaType#isInstance(JavaConstant) 821 */ 822 native boolean isInstance(HotSpotResolvedObjectTypeImpl holder, HotSpotObjectConstantImpl object); 823 824 /** 825 * @see ResolvedJavaType#isAssignableFrom(ResolvedJavaType) 826 */ 827 native boolean isAssignableFrom(HotSpotResolvedObjectTypeImpl holder, HotSpotResolvedObjectTypeImpl otherType); 828 829 /** 830 * @see ConstantReflectionProvider#asJavaType(Constant) 831 */ 832 native HotSpotResolvedJavaType asJavaType(HotSpotObjectConstantImpl object); 833 834 /** 835 * Converts a String constant into a String. 836 */ 837 native String asString(HotSpotObjectConstantImpl object); 838 839 /** 840 * Compares the contents of {@code xHandle} and {@code yHandle} for pointer equality. 841 */ 842 native boolean equals(HotSpotObjectConstantImpl x, long xHandle, HotSpotObjectConstantImpl y, long yHandle); 843 844 /** 845 * Gets a {@link JavaConstant} wrapping the {@link java.lang.Class} mirror for {@code type}. 846 */ 847 native HotSpotObjectConstantImpl getJavaMirror(HotSpotResolvedJavaType type); 848 849 /** 850 * Returns the length of the array if {@code object} represents an array or -1 otherwise. 851 */ 852 native int getArrayLength(HotSpotObjectConstantImpl object); 853 854 /** 855 * Reads the element at {@code index} if {@code object} is an array. Elements of an object array 856 * are returned as {@link JavaConstant}s and primitives are returned as boxed values. The value 857 * {@code null} is returned if the {@code index} is out of range or object is not an array. 858 */ 859 native Object readArrayElement(HotSpotObjectConstantImpl object, int index); 860 861 /** 862 * Reads a byte sized value from {@code displacement} in {@code object}. 863 */ 864 native byte getByte(HotSpotObjectConstantImpl object, long displacement); 865 866 /** 867 * Reads a short sized value from {@code displacement} in {@code object}. 868 */ 869 native short getShort(HotSpotObjectConstantImpl object, long displacement); 870 871 /** 872 * Reads an int sized value from {@code displacement} in {@code object}. 873 */ 874 native int getInt(HotSpotObjectConstantImpl object, long displacement); 875 876 /** 877 * Reads a long sized value from {@code displacement} in {@code object}. 878 */ 879 native long getLong(HotSpotObjectConstantImpl object, long displacement); 880 881 /** 882 * Reads a Java object from {@code displacement} in {@code object}. 883 */ 884 native HotSpotObjectConstantImpl getObject(HotSpotObjectConstantImpl object, long displacement); 885 886 /** 887 * @see HotSpotJVMCIRuntime#registerNativeMethods 888 */ 889 native long[] registerNativeMethods(Class<?> clazz); 890 891 /** 892 * @see HotSpotJVMCIRuntime#translate(Object) 893 */ 894 native long translate(Object obj); 895 896 /** 897 * @see HotSpotJVMCIRuntime#unhand(Class, long) 898 */ 899 native Object unhand(long handle); 900 901 /** 902 * Updates {@code address} and {@code entryPoint} fields of {@code nmethodMirror} based on the 903 * current state of the {@code nmethod} identified by {@code address} and 904 * {@code nmethodMirror.compileId} in the code cache. 905 */ 906 native void updateHotSpotNmethod(HotSpotNmethod nmethodMirror); 907 908 /** 909 * @see InstalledCode#getCode() 910 */ 911 native byte[] getCode(HotSpotInstalledCode code); 912 913 /** 914 * Gets a {@link Executable} corresponding to {@code method}. 915 */ 916 native Executable asReflectionExecutable(HotSpotResolvedJavaMethodImpl method); 917 918 /** 919 * Gets a {@link Field} denoted by {@code holder} and {@code index}. 920 * 921 * @param holder the class in which the requested field is declared 922 * @param fieldIndex the {@code fieldDescriptor::index()} denoting the field 923 */ 924 native Field asReflectionField(HotSpotResolvedObjectTypeImpl holder, int fieldIndex); 925 926 /** 927 * @see HotSpotJVMCIRuntime#getIntrinsificationTrustPredicate(Class...) 928 */ 929 native boolean isTrustedForIntrinsics(HotSpotResolvedObjectTypeImpl type); 930 931 /** 932 * Releases the resources backing the global JNI {@code handle}. This is equivalent to the 933 * {@code DeleteGlobalRef} JNI function. 934 */ 935 native void deleteGlobalHandle(long handle); 936 937 /** 938 * Gets the failed speculations pointed to by {@code *failedSpeculationsAddress}. 939 * 940 * @param currentFailures the known failures at {@code failedSpeculationsAddress} 941 * @return the list of failed speculations with each entry being a single speculation in the 942 * format emitted by {@link HotSpotSpeculationEncoding#toByteArray()} 943 */ 944 native byte[][] getFailedSpeculations(long failedSpeculationsAddress, byte[][] currentFailures); 945 946 /** 947 * Gets the address of the {@code MethodData::_failed_speculations} field in the 948 * {@code MethodData} associated with {@code method}. This will create and install the 949 * {@code MethodData} if it didn't already exist. 950 */ 951 native long getFailedSpeculationsAddress(HotSpotResolvedJavaMethodImpl method); 952 953 /** 954 * Frees the failed speculations pointed to by {@code *failedSpeculationsAddress}. 955 */ 956 native void releaseFailedSpeculations(long failedSpeculationsAddress); 957 958 /** 959 * Adds a speculation to the failed speculations pointed to by 960 * {@code *failedSpeculationsAddress}. 961 * 962 * @return {@code false} if the speculation could not be appended to the list 963 */ 964 native boolean addFailedSpeculation(long failedSpeculationsAddress, byte[] speculation); 965 966 /** 967 * @see HotSpotJVMCIRuntime#isCurrentThreadAttached() 968 */ 969 native boolean isCurrentThreadAttached(); 970 971 /** 972 * @see HotSpotJVMCIRuntime#attachCurrentThread 973 */ 974 native boolean attachCurrentThread(boolean asDaemon); 975 976 /** 977 * @see HotSpotJVMCIRuntime#detachCurrentThread() 978 */ 979 native void detachCurrentThread(); 980 981 /** 982 * @see HotSpotJVMCIRuntime#exitHotSpot(int) 983 */ 984 native void callSystemExit(int status); 985 }