1 /* 2 * Copyright (c) 2011, 2022, 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 #include "precompiled.hpp" 25 #include "classfile/classLoaderData.inline.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "classfile/stringTable.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "classfile/vmClasses.hpp" 31 #include "code/scopeDesc.hpp" 32 #include "compiler/compileBroker.hpp" 33 #include "compiler/compilerEvent.hpp" 34 #include "compiler/disassembler.hpp" 35 #include "compiler/oopMap.hpp" 36 #include "interpreter/linkResolver.hpp" 37 #include "interpreter/bytecodeStream.hpp" 38 #include "jfr/jfrEvents.hpp" 39 #include "jvmci/jvmciCompilerToVM.hpp" 40 #include "jvmci/jvmciCodeInstaller.hpp" 41 #include "jvmci/jvmciRuntime.hpp" 42 #include "logging/log.hpp" 43 #include "logging/logTag.hpp" 44 #include "memory/oopFactory.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/constantPool.inline.hpp" 47 #include "oops/instanceMirrorKlass.hpp" 48 #include "oops/instanceKlass.inline.hpp" 49 #include "oops/method.inline.hpp" 50 #include "oops/typeArrayOop.inline.hpp" 51 #include "prims/jvmtiExport.hpp" 52 #include "prims/methodHandles.hpp" 53 #include "prims/nativeLookup.hpp" 54 #include "runtime/atomic.hpp" 55 #include "runtime/deoptimization.hpp" 56 #include "runtime/fieldDescriptor.inline.hpp" 57 #include "runtime/frame.inline.hpp" 58 #include "runtime/globals_extension.hpp" 59 #include "runtime/interfaceSupport.inline.hpp" 60 #include "runtime/jniHandles.inline.hpp" 61 #include "runtime/reflectionUtils.hpp" 62 #include "runtime/stackFrameStream.inline.hpp" 63 #include "runtime/timerTrace.hpp" 64 #include "runtime/vframe_hp.hpp" 65 #include "runtime/vframe.inline.hpp" 66 #if INCLUDE_JFR 67 #include "jfr/jfr.hpp" 68 #endif 69 70 JVMCIKlassHandle::JVMCIKlassHandle(Thread* thread, Klass* klass) { 71 _thread = thread; 72 _klass = klass; 73 if (klass != NULL) { 74 _holder = Handle(_thread, klass->klass_holder()); 75 } 76 } 77 78 JVMCIKlassHandle& JVMCIKlassHandle::operator=(Klass* klass) { 79 _klass = klass; 80 if (klass != NULL) { 81 _holder = Handle(_thread, klass->klass_holder()); 82 } 83 return *this; 84 } 85 86 static void requireInHotSpot(const char* caller, JVMCI_TRAPS) { 87 if (!JVMCIENV->is_hotspot()) { 88 JVMCI_THROW_MSG(IllegalStateException, err_msg("Cannot call %s from JVMCI shared library", caller)); 89 } 90 } 91 92 class JVMCITraceMark : public StackObj { 93 const char* _msg; 94 public: 95 JVMCITraceMark(const char* msg) { 96 _msg = msg; 97 JVMCI_event_2("Enter %s", _msg); 98 } 99 ~JVMCITraceMark() { 100 JVMCI_event_2(" Exit %s", _msg); 101 } 102 }; 103 104 105 Handle JavaArgumentUnboxer::next_arg(BasicType expectedType) { 106 assert(_index < _args->length(), "out of bounds"); 107 oop arg=((objArrayOop) (_args))->obj_at(_index++); 108 assert(expectedType == T_OBJECT || java_lang_boxing_object::is_instance(arg, expectedType), "arg type mismatch"); 109 return Handle(Thread::current(), arg); 110 } 111 112 // Bring the JVMCI compiler thread into the VM state. 113 #define JVMCI_VM_ENTRY_MARK \ 114 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \ 115 ThreadInVMfromNative __tiv(thread); \ 116 HandleMarkCleaner __hm(thread); \ 117 JavaThread* THREAD = thread; \ 118 debug_only(VMNativeEntryWrapper __vew;) 119 120 // Native method block that transitions current thread to '_thread_in_vm'. 121 #define C2V_BLOCK(result_type, name, signature) \ 122 JVMCI_VM_ENTRY_MARK; \ 123 ResourceMark rm; \ 124 JNI_JVMCIENV(JVMCI::compilation_tick(thread), env); 125 126 static JavaThread* get_current_thread(bool allow_null=true) { 127 Thread* thread = Thread::current_or_null_safe(); 128 if (thread == NULL) { 129 assert(allow_null, "npe"); 130 return NULL; 131 } 132 return JavaThread::cast(thread); 133 } 134 135 // Entry to native method implementation that transitions 136 // current thread to '_thread_in_vm'. 137 #define C2V_VMENTRY(result_type, name, signature) \ 138 JNIEXPORT result_type JNICALL c2v_ ## name signature { \ 139 JavaThread* thread = get_current_thread(); \ 140 if (thread == NULL) { \ 141 env->ThrowNew(JNIJVMCI::InternalError::clazz(), \ 142 err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \ 143 return; \ 144 } \ 145 C2V_BLOCK(result_type, name, signature) \ 146 JVMCITraceMark jtm("CompilerToVM::" #name); 147 148 #define C2V_VMENTRY_(result_type, name, signature, result) \ 149 JNIEXPORT result_type JNICALL c2v_ ## name signature { \ 150 JavaThread* thread = get_current_thread(); \ 151 if (thread == NULL) { \ 152 env->ThrowNew(JNIJVMCI::InternalError::clazz(), \ 153 err_msg("Cannot call into HotSpot from JVMCI shared library without attaching current thread")); \ 154 return result; \ 155 } \ 156 C2V_BLOCK(result_type, name, signature) \ 157 JVMCITraceMark jtm("CompilerToVM::" #name); 158 159 #define C2V_VMENTRY_NULL(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, NULL) 160 #define C2V_VMENTRY_0(result_type, name, signature) C2V_VMENTRY_(result_type, name, signature, 0) 161 162 // Entry to native method implementation that does not transition 163 // current thread to '_thread_in_vm'. 164 #define C2V_VMENTRY_PREFIX(result_type, name, signature) \ 165 JNIEXPORT result_type JNICALL c2v_ ## name signature { \ 166 JavaThread* thread = get_current_thread(); 167 168 #define C2V_END } 169 170 #define JNI_THROW(caller, name, msg) do { \ 171 jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg); \ 172 if (__throw_res != JNI_OK) { \ 173 tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \ 174 } \ 175 return; \ 176 } while (0); 177 178 #define JNI_THROW_(caller, name, msg, result) do { \ 179 jint __throw_res = env->ThrowNew(JNIJVMCI::name::clazz(), msg); \ 180 if (__throw_res != JNI_OK) { \ 181 tty->print_cr("Throwing " #name " in " caller " returned %d", __throw_res); \ 182 } \ 183 return result; \ 184 } while (0) 185 186 jobjectArray readConfiguration0(JNIEnv *env, JVMCI_TRAPS); 187 188 C2V_VMENTRY_NULL(jobjectArray, readConfiguration, (JNIEnv* env)) 189 jobjectArray config = readConfiguration0(env, JVMCI_CHECK_NULL); 190 return config; 191 } 192 193 C2V_VMENTRY_NULL(jobject, getFlagValue, (JNIEnv* env, jobject c2vm, jobject name_handle)) 194 #define RETURN_BOXED_LONG(value) jvalue p; p.j = (jlong) (value); JVMCIObject box = JVMCIENV->create_box(T_LONG, &p, JVMCI_CHECK_NULL); return box.as_jobject(); 195 #define RETURN_BOXED_DOUBLE(value) jvalue p; p.d = (jdouble) (value); JVMCIObject box = JVMCIENV->create_box(T_DOUBLE, &p, JVMCI_CHECK_NULL); return box.as_jobject(); 196 JVMCIObject name = JVMCIENV->wrap(name_handle); 197 if (name.is_null()) { 198 JVMCI_THROW_NULL(NullPointerException); 199 } 200 const char* cstring = JVMCIENV->as_utf8_string(name); 201 const JVMFlag* flag = JVMFlag::find_declared_flag(cstring); 202 if (flag == NULL) { 203 return c2vm; 204 } 205 if (flag->is_bool()) { 206 jvalue prim; 207 prim.z = flag->get_bool(); 208 JVMCIObject box = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL); 209 return JVMCIENV->get_jobject(box); 210 } else if (flag->is_ccstr()) { 211 JVMCIObject value = JVMCIENV->create_string(flag->get_ccstr(), JVMCI_CHECK_NULL); 212 return JVMCIENV->get_jobject(value); 213 } else if (flag->is_intx()) { 214 RETURN_BOXED_LONG(flag->get_intx()); 215 } else if (flag->is_int()) { 216 RETURN_BOXED_LONG(flag->get_int()); 217 } else if (flag->is_uint()) { 218 RETURN_BOXED_LONG(flag->get_uint()); 219 } else if (flag->is_uint64_t()) { 220 RETURN_BOXED_LONG(flag->get_uint64_t()); 221 } else if (flag->is_size_t()) { 222 RETURN_BOXED_LONG(flag->get_size_t()); 223 } else if (flag->is_uintx()) { 224 RETURN_BOXED_LONG(flag->get_uintx()); 225 } else if (flag->is_double()) { 226 RETURN_BOXED_DOUBLE(flag->get_double()); 227 } else { 228 JVMCI_ERROR_NULL("VM flag %s has unsupported type %s", flag->name(), flag->type_string()); 229 } 230 #undef RETURN_BOXED_LONG 231 #undef RETURN_BOXED_DOUBLE 232 C2V_END 233 234 // Macros for argument pairs representing a wrapper object and its wrapped VM pointer 235 #define ARGUMENT_PAIR(name) jobject name ## _obj, jlong name ## _pointer 236 #define UNPACK_PAIR(type, name) ((type*) name ## _pointer) 237 238 C2V_VMENTRY_NULL(jbyteArray, getBytecode, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 239 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 240 241 int code_size = method->code_size(); 242 jbyte* reconstituted_code = NEW_RESOURCE_ARRAY(jbyte, code_size); 243 244 guarantee(method->method_holder()->is_rewritten(), "Method's holder should be rewritten"); 245 // iterate over all bytecodes and replace non-Java bytecodes 246 247 for (BytecodeStream s(method); s.next() != Bytecodes::_illegal; ) { 248 Bytecodes::Code code = s.code(); 249 Bytecodes::Code raw_code = s.raw_code(); 250 int bci = s.bci(); 251 int len = s.instruction_size(); 252 253 // Restore original byte code. 254 reconstituted_code[bci] = (jbyte) (s.is_wide()? Bytecodes::_wide : code); 255 if (len > 1) { 256 memcpy(reconstituted_code + (bci + 1), s.bcp()+1, len-1); 257 } 258 259 if (len > 1) { 260 // Restore the big-endian constant pool indexes. 261 // Cf. Rewriter::scan_method 262 switch (code) { 263 case Bytecodes::_getstatic: 264 case Bytecodes::_putstatic: 265 case Bytecodes::_getfield: 266 case Bytecodes::_putfield: 267 case Bytecodes::_invokevirtual: 268 case Bytecodes::_invokespecial: 269 case Bytecodes::_invokestatic: 270 case Bytecodes::_invokeinterface: 271 case Bytecodes::_invokehandle: { 272 int cp_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1)); 273 Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index); 274 break; 275 } 276 277 case Bytecodes::_invokedynamic: { 278 int cp_index = Bytes::get_native_u4((address) reconstituted_code + (bci + 1)); 279 Bytes::put_Java_u4((address) reconstituted_code + (bci + 1), (u4) cp_index); 280 break; 281 } 282 283 default: 284 break; 285 } 286 287 // Not all ldc byte code are rewritten. 288 switch (raw_code) { 289 case Bytecodes::_fast_aldc: { 290 int cpc_index = reconstituted_code[bci + 1] & 0xff; 291 int cp_index = method->constants()->object_to_cp_index(cpc_index); 292 assert(cp_index < method->constants()->length(), "sanity check"); 293 reconstituted_code[bci + 1] = (jbyte) cp_index; 294 break; 295 } 296 297 case Bytecodes::_fast_aldc_w: { 298 int cpc_index = Bytes::get_native_u2((address) reconstituted_code + (bci + 1)); 299 int cp_index = method->constants()->object_to_cp_index(cpc_index); 300 assert(cp_index < method->constants()->length(), "sanity check"); 301 Bytes::put_Java_u2((address) reconstituted_code + (bci + 1), (u2) cp_index); 302 break; 303 } 304 305 default: 306 break; 307 } 308 } 309 } 310 311 JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL); 312 JVMCIENV->copy_bytes_from(reconstituted_code, result, 0, code_size); 313 return JVMCIENV->get_jbyteArray(result); 314 C2V_END 315 316 C2V_VMENTRY_0(jint, getExceptionTableLength, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 317 Method* method = UNPACK_PAIR(Method, method); 318 return method->exception_table_length(); 319 C2V_END 320 321 C2V_VMENTRY_0(jlong, getExceptionTableStart, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 322 Method* method = UNPACK_PAIR(Method, method); 323 if (method->exception_table_length() == 0) { 324 return 0L; 325 } 326 return (jlong) (address) method->exception_table_start(); 327 C2V_END 328 329 C2V_VMENTRY_NULL(jobject, asResolvedJavaMethod, (JNIEnv* env, jobject, jobject executable_handle)) 330 requireInHotSpot("asResolvedJavaMethod", JVMCI_CHECK_NULL); 331 oop executable = JNIHandles::resolve(executable_handle); 332 oop mirror = NULL; 333 int slot = 0; 334 335 if (executable->klass() == vmClasses::reflect_Constructor_klass()) { 336 mirror = java_lang_reflect_Constructor::clazz(executable); 337 slot = java_lang_reflect_Constructor::slot(executable); 338 } else { 339 assert(executable->klass() == vmClasses::reflect_Method_klass(), "wrong type"); 340 mirror = java_lang_reflect_Method::clazz(executable); 341 slot = java_lang_reflect_Method::slot(executable); 342 } 343 Klass* holder = java_lang_Class::as_Klass(mirror); 344 methodHandle method (THREAD, InstanceKlass::cast(holder)->method_with_idnum(slot)); 345 JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL); 346 return JVMCIENV->get_jobject(result); 347 } 348 349 C2V_VMENTRY_NULL(jobject, getResolvedJavaMethod, (JNIEnv* env, jobject, jobject base, jlong offset)) 350 Method* method = NULL; 351 JVMCIObject base_object = JVMCIENV->wrap(base); 352 if (base_object.is_null()) { 353 method = *((Method**)(offset)); 354 } else { 355 Handle obj = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL); 356 if (obj->is_a(vmClasses::ResolvedMethodName_klass())) { 357 method = (Method*) (intptr_t) obj->long_field(offset); 358 } else { 359 JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", obj->klass()->external_name())); 360 } 361 } 362 if (method == NULL) { 363 JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected type: %s", JVMCIENV->klass_name(base_object))); 364 } 365 assert (method->is_method(), "invalid read"); 366 JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, method), JVMCI_CHECK_NULL); 367 return JVMCIENV->get_jobject(result); 368 } 369 370 C2V_VMENTRY_NULL(jobject, getConstantPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass_or_method), jboolean is_klass)) 371 ConstantPool* cp = NULL; 372 if (UNPACK_PAIR(address, klass_or_method) == 0) { 373 JVMCI_THROW_NULL(NullPointerException); 374 } 375 if (!is_klass) { 376 cp = (UNPACK_PAIR(Method, klass_or_method))->constMethod()->constants(); 377 } else { 378 cp = InstanceKlass::cast(UNPACK_PAIR(Klass, klass_or_method))->constants(); 379 } 380 381 JVMCIObject result = JVMCIENV->get_jvmci_constant_pool(constantPoolHandle(THREAD, cp), JVMCI_CHECK_NULL); 382 return JVMCIENV->get_jobject(result); 383 } 384 385 C2V_VMENTRY_NULL(jobject, getResolvedJavaType0, (JNIEnv* env, jobject, jobject base, jlong offset, jboolean compressed)) 386 JVMCIKlassHandle klass(THREAD); 387 JVMCIObject base_object = JVMCIENV->wrap(base); 388 jlong base_address = 0; 389 if (base_object.is_non_null() && offset == oopDesc::klass_offset_in_bytes()) { 390 if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) { 391 Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL); 392 klass = base_oop->klass(); 393 } else { 394 assert(false, "What types are we actually expecting here?"); 395 } 396 } else if (!compressed) { 397 if (base_object.is_non_null()) { 398 if (JVMCIENV->isa_HotSpotResolvedJavaMethodImpl(base_object)) { 399 base_address = (intptr_t) JVMCIENV->asMethod(base_object); 400 } else if (JVMCIENV->isa_HotSpotConstantPool(base_object)) { 401 base_address = (intptr_t) JVMCIENV->asConstantPool(base_object); 402 } else if (JVMCIENV->isa_HotSpotResolvedObjectTypeImpl(base_object)) { 403 base_address = (intptr_t) JVMCIENV->asKlass(base_object); 404 } else if (JVMCIENV->isa_HotSpotObjectConstantImpl(base_object)) { 405 Handle base_oop = JVMCIENV->asConstant(base_object, JVMCI_CHECK_NULL); 406 if (base_oop->is_a(vmClasses::Class_klass())) { 407 base_address = cast_from_oop<jlong>(base_oop()); 408 } 409 } 410 if (base_address == 0) { 411 JVMCI_THROW_MSG_NULL(IllegalArgumentException, 412 err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", JVMCIENV->klass_name(base_object), offset, compressed ? "true" : "false")); 413 } 414 } 415 klass = *((Klass**) (intptr_t) (base_address + offset)); 416 } else { 417 JVMCI_THROW_MSG_NULL(IllegalArgumentException, 418 err_msg("Unexpected arguments: %s " JLONG_FORMAT " %s", 419 base_object.is_non_null() ? JVMCIENV->klass_name(base_object) : "null", 420 offset, compressed ? "true" : "false")); 421 } 422 assert (klass == NULL || klass->is_klass(), "invalid read"); 423 JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL); 424 return JVMCIENV->get_jobject(result); 425 } 426 427 C2V_VMENTRY_NULL(jobject, findUniqueConcreteMethod, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), ARGUMENT_PAIR(method))) 428 methodHandle method (THREAD, UNPACK_PAIR(Method, method)); 429 InstanceKlass* holder = InstanceKlass::cast(UNPACK_PAIR(Klass, klass)); 430 if (holder->is_interface()) { 431 JVMCI_THROW_MSG_NULL(InternalError, err_msg("Interface %s should be handled in Java code", holder->external_name())); 432 } 433 if (method->can_be_statically_bound()) { 434 JVMCI_THROW_MSG_NULL(InternalError, err_msg("Effectively static method %s.%s should be handled in Java code", method->method_holder()->external_name(), method->external_name())); 435 } 436 437 methodHandle ucm; 438 { 439 MutexLocker locker(Compile_lock); 440 ucm = methodHandle(THREAD, Dependencies::find_unique_concrete_method(holder, method())); 441 } 442 JVMCIObject result = JVMCIENV->get_jvmci_method(ucm, JVMCI_CHECK_NULL); 443 return JVMCIENV->get_jobject(result); 444 C2V_END 445 446 C2V_VMENTRY_NULL(jobject, getImplementor, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 447 Klass* klass = UNPACK_PAIR(Klass, klass); 448 if (!klass->is_interface()) { 449 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 450 err_msg("Expected interface type, got %s", klass->external_name())); 451 } 452 InstanceKlass* iklass = InstanceKlass::cast(klass); 453 JVMCIKlassHandle handle(THREAD); 454 { 455 // Need Compile_lock around implementor() 456 MutexLocker locker(Compile_lock); 457 handle = iklass->implementor(); 458 } 459 JVMCIObject implementor = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL); 460 return JVMCIENV->get_jobject(implementor); 461 C2V_END 462 463 C2V_VMENTRY_0(jboolean, methodIsIgnoredBySecurityStackWalk,(JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 464 Method* method = UNPACK_PAIR(Method, method); 465 return method->is_ignored_by_security_stack_walk(); 466 C2V_END 467 468 C2V_VMENTRY_0(jboolean, isCompilable,(JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 469 Method* method = UNPACK_PAIR(Method, method); 470 // Skip redefined methods 471 if (method->is_old()) { 472 return false; 473 } 474 return !method->is_not_compilable(CompLevel_full_optimization); 475 C2V_END 476 477 C2V_VMENTRY_0(jboolean, hasNeverInlineDirective,(JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 478 methodHandle method (THREAD, UNPACK_PAIR(Method, method)); 479 return !Inline || CompilerOracle::should_not_inline(method) || method->dont_inline(); 480 C2V_END 481 482 C2V_VMENTRY_0(jboolean, shouldInlineMethod,(JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 483 methodHandle method (THREAD, UNPACK_PAIR(Method, method)); 484 return CompilerOracle::should_inline(method) || method->force_inline(); 485 C2V_END 486 487 C2V_VMENTRY_NULL(jobject, lookupType, (JNIEnv* env, jobject, jstring jname, ARGUMENT_PAIR(accessing_klass), jboolean resolve)) 488 JVMCIObject name = JVMCIENV->wrap(jname); 489 const char* str = JVMCIENV->as_utf8_string(name); 490 TempNewSymbol class_name = SymbolTable::new_symbol(str); 491 492 if (class_name->utf8_length() <= 1) { 493 JVMCI_THROW_MSG_0(InternalError, err_msg("Primitive type %s should be handled in Java code", class_name->as_C_string())); 494 } 495 496 JVMCIKlassHandle resolved_klass(THREAD); 497 Klass* accessing_klass = UNPACK_PAIR(Klass, accessing_klass); 498 Handle class_loader; 499 Handle protection_domain; 500 if (accessing_klass != nullptr) { 501 class_loader = Handle(THREAD, accessing_klass->class_loader()); 502 protection_domain = Handle(THREAD, accessing_klass->protection_domain()); 503 } else { 504 // Use the System class loader 505 class_loader = Handle(THREAD, SystemDictionary::java_system_loader()); 506 JVMCIENV->runtime()->initialize(JVMCI_CHECK_NULL); 507 } 508 509 if (resolve) { 510 resolved_klass = SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK_NULL); 511 if (resolved_klass == nullptr) { 512 JVMCI_THROW_MSG_NULL(ClassNotFoundException, str); 513 } 514 } else { 515 if (Signature::has_envelope(class_name)) { 516 // This is a name from a signature. Strip off the trimmings. 517 // Call recursive to keep scope of strippedsym. 518 TempNewSymbol strippedsym = Signature::strip_envelope(class_name); 519 resolved_klass = SystemDictionary::find_instance_klass(THREAD, strippedsym, 520 class_loader, 521 protection_domain); 522 } else if (Signature::is_array(class_name)) { 523 SignatureStream ss(class_name, false); 524 int ndim = ss.skip_array_prefix(); 525 if (ss.type() == T_OBJECT) { 526 Symbol* strippedsym = ss.as_symbol(); 527 resolved_klass = SystemDictionary::find_instance_klass(THREAD, strippedsym, 528 class_loader, 529 protection_domain); 530 if (!resolved_klass.is_null()) { 531 resolved_klass = resolved_klass->array_klass(ndim, CHECK_NULL); 532 } 533 } else { 534 resolved_klass = TypeArrayKlass::cast(Universe::typeArrayKlassObj(ss.type()))->array_klass(ndim, CHECK_NULL); 535 } 536 } else { 537 resolved_klass = SystemDictionary::find_instance_klass(THREAD, class_name, 538 class_loader, 539 protection_domain); 540 } 541 } 542 JVMCIObject result = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL); 543 return JVMCIENV->get_jobject(result); 544 C2V_END 545 546 C2V_VMENTRY_NULL(jobject, getArrayType, (JNIEnv* env, jobject, jchar type_char, ARGUMENT_PAIR(klass))) 547 JVMCIKlassHandle array_klass(THREAD); 548 Klass* klass = UNPACK_PAIR(Klass, klass); 549 if (klass == nullptr) { 550 BasicType type = JVMCIENV->typeCharToBasicType(type_char, JVMCI_CHECK_0); 551 if (type == T_VOID) { 552 return nullptr; 553 } 554 array_klass = Universe::typeArrayKlassObj(type); 555 if (array_klass == nullptr) { 556 JVMCI_THROW_MSG_NULL(InternalError, err_msg("No array klass for primitive type %s", type2name(type))); 557 } 558 } else { 559 array_klass = klass->array_klass(CHECK_NULL); 560 } 561 JVMCIObject result = JVMCIENV->get_jvmci_type(array_klass, JVMCI_CHECK_NULL); 562 return JVMCIENV->get_jobject(result); 563 C2V_END 564 565 C2V_VMENTRY_NULL(jobject, lookupClass, (JNIEnv* env, jobject, jclass mirror)) 566 requireInHotSpot("lookupClass", JVMCI_CHECK_NULL); 567 if (mirror == nullptr) { 568 return nullptr; 569 } 570 JVMCIKlassHandle klass(THREAD); 571 klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror)); 572 if (klass == nullptr) { 573 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Primitive classes are unsupported"); 574 } 575 JVMCIObject result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL); 576 return JVMCIENV->get_jobject(result); 577 C2V_END 578 579 C2V_VMENTRY_NULL(jobject, getUncachedStringInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 580 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 581 constantTag tag = cp->tag_at(index); 582 if (!tag.is_string()) { 583 JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Unexpected constant pool tag at index %d: %d", index, tag.value())); 584 } 585 oop obj = cp->uncached_string_at(index, CHECK_NULL); 586 return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(obj)); 587 C2V_END 588 589 C2V_VMENTRY_NULL(jobject, resolvePossiblyCachedConstantInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 590 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 591 oop obj = cp->resolve_possibly_cached_constant_at(index, CHECK_NULL); 592 constantTag tag = cp->tag_at(index); 593 if (tag.is_dynamic_constant() || tag.is_dynamic_constant_in_error()) { 594 if (obj == Universe::the_null_sentinel()) { 595 return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER()); 596 } 597 BasicType bt = Signature::basic_type(cp->uncached_signature_ref_at(index)); 598 if (!is_reference_type(bt)) { 599 if (!is_java_primitive(bt)) { 600 return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL()); 601 } 602 603 // Convert standard box (e.g. java.lang.Integer) to JVMCI box (e.g. jdk.vm.ci.meta.PrimitiveConstant) 604 jvalue value; 605 jlong raw_value; 606 jchar type_char; 607 BasicType bt2 = java_lang_boxing_object::get_value(obj, &value); 608 assert(bt2 == bt, ""); 609 switch (bt2) { 610 case T_LONG: type_char = 'J'; raw_value = value.j; break; 611 case T_DOUBLE: type_char = 'D'; raw_value = value.j; break; 612 case T_FLOAT: type_char = 'F'; raw_value = value.i; break; 613 case T_INT: type_char = 'I'; raw_value = value.i; break; 614 case T_SHORT: type_char = 'S'; raw_value = value.s; break; 615 case T_BYTE: type_char = 'B'; raw_value = value.b; break; 616 case T_CHAR: type_char = 'C'; raw_value = value.c; break; 617 case T_BOOLEAN: type_char = 'Z'; raw_value = value.z; break; 618 default: return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_ILLEGAL()); 619 } 620 621 JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(type_char, raw_value, JVMCI_CHECK_NULL); 622 return JVMCIENV->get_jobject(result); 623 } 624 } 625 return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(obj)); 626 C2V_END 627 628 C2V_VMENTRY_NULL(jobjectArray, resolveBootstrapMethod, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 629 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 630 constantTag tag = cp->tag_at(index); 631 bool is_indy = tag.is_invoke_dynamic(); 632 bool is_condy = tag.is_dynamic_constant(); 633 if (!(is_condy || is_indy)) { 634 JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Unexpected constant pool tag at index %d: %d", index, tag.value())); 635 } 636 // Resolve the bootstrap specifier, its name, type, and static arguments 637 BootstrapInfo bootstrap_specifier(cp, index); 638 Handle bsm = bootstrap_specifier.resolve_bsm(CHECK_NULL); 639 640 // call java.lang.invoke.MethodHandle::asFixedArity() -> MethodHandle 641 // to get a DirectMethodHandle from which we can then extract a Method* 642 JavaValue result(T_OBJECT); 643 JavaCalls::call_virtual(&result, 644 bsm, 645 vmClasses::MethodHandle_klass(), 646 vmSymbols::asFixedArity_name(), 647 vmSymbols::asFixedArity_signature(), 648 CHECK_NULL); 649 bsm = Handle(THREAD, result.get_oop()); 650 651 // Check assumption about getting a DirectMethodHandle 652 if (!java_lang_invoke_DirectMethodHandle::is_instance(bsm())) { 653 JVMCI_THROW_MSG_NULL(InternalError, err_msg("Unexpected MethodHandle subclass: %s", bsm->klass()->external_name())); 654 } 655 // Create return array describing the bootstrap method invocation (BSMI) 656 JVMCIObjectArray bsmi = JVMCIENV->new_Object_array(4, JVMCI_CHECK_NULL); 657 658 // Extract Method* and wrap it in a ResolvedJavaMethod 659 Handle member = Handle(THREAD, java_lang_invoke_DirectMethodHandle::member(bsm())); 660 JVMCIObject bsmi_method = JVMCIENV->get_jvmci_method(methodHandle(THREAD, java_lang_invoke_MemberName::vmtarget(member())), JVMCI_CHECK_NULL); 661 JVMCIENV->put_object_at(bsmi, 0, bsmi_method); 662 663 JVMCIObject bsmi_name = JVMCIENV->create_string(bootstrap_specifier.name(), JVMCI_CHECK_NULL); 664 JVMCIENV->put_object_at(bsmi, 1, bsmi_name); 665 666 Handle type_arg = bootstrap_specifier.type_arg(); 667 JVMCIObject bsmi_type = JVMCIENV->get_object_constant(type_arg()); 668 JVMCIENV->put_object_at(bsmi, 2, bsmi_type); 669 670 Handle arg_values = bootstrap_specifier.arg_values(); 671 if (arg_values.not_null()) { 672 if (!arg_values->is_array()) { 673 JVMCIENV->put_object_at(bsmi, 3, JVMCIENV->get_object_constant(arg_values())); 674 } else if (arg_values->is_objArray()) { 675 objArrayHandle args_array = objArrayHandle(THREAD, (objArrayOop) arg_values()); 676 int len = args_array->length(); 677 JVMCIObjectArray arguments = JVMCIENV->new_JavaConstant_array(len, JVMCI_CHECK_NULL); 678 JVMCIENV->put_object_at(bsmi, 3, arguments); 679 for (int i = 0; i < len; i++) { 680 oop x = args_array->obj_at(i); 681 if (x != nullptr) { 682 JVMCIENV->put_object_at(arguments, i, JVMCIENV->get_object_constant(x)); 683 } else { 684 JVMCIENV->put_object_at(arguments, i, JVMCIENV->get_JavaConstant_NULL_POINTER()); 685 } 686 } 687 } else if (arg_values->is_typeArray()) { 688 typeArrayHandle bsci = typeArrayHandle(THREAD, (typeArrayOop) arg_values()); 689 JVMCIPrimitiveArray arguments = JVMCIENV->new_intArray(bsci->length(), JVMCI_CHECK_NULL); 690 JVMCIENV->put_object_at(bsmi, 3, arguments); 691 for (int i = 0; i < bsci->length(); i++) { 692 JVMCIENV->put_int_at(arguments, i, bsci->int_at(i)); 693 } 694 } 695 } 696 return JVMCIENV->get_jobjectArray(bsmi); 697 C2V_END 698 699 C2V_VMENTRY_0(jint, lookupNameAndTypeRefIndexInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 700 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 701 return cp->name_and_type_ref_index_at(index); 702 C2V_END 703 704 C2V_VMENTRY_NULL(jobject, lookupNameInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint which)) 705 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 706 JVMCIObject sym = JVMCIENV->create_string(cp->name_ref_at(which), JVMCI_CHECK_NULL); 707 return JVMCIENV->get_jobject(sym); 708 C2V_END 709 710 C2V_VMENTRY_NULL(jobject, lookupSignatureInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint which)) 711 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 712 JVMCIObject sym = JVMCIENV->create_string(cp->signature_ref_at(which), JVMCI_CHECK_NULL); 713 return JVMCIENV->get_jobject(sym); 714 C2V_END 715 716 C2V_VMENTRY_0(jint, lookupKlassRefIndexInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 717 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 718 return cp->klass_ref_index_at(index); 719 C2V_END 720 721 C2V_VMENTRY_NULL(jobject, resolveTypeInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 722 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 723 Klass* klass = cp->klass_at(index, CHECK_NULL); 724 JVMCIKlassHandle resolved_klass(THREAD, klass); 725 if (resolved_klass->is_instance_klass()) { 726 InstanceKlass::cast(resolved_klass())->link_class(CHECK_NULL); 727 if (!InstanceKlass::cast(resolved_klass())->is_linked()) { 728 // link_class() should not return here if there is an issue. 729 JVMCI_THROW_MSG_NULL(InternalError, err_msg("Class %s must be linked", resolved_klass()->external_name())); 730 } 731 } 732 JVMCIObject klassObject = JVMCIENV->get_jvmci_type(resolved_klass, JVMCI_CHECK_NULL); 733 return JVMCIENV->get_jobject(klassObject); 734 C2V_END 735 736 C2V_VMENTRY_NULL(jobject, lookupKlassInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 737 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 738 Klass* loading_klass = cp->pool_holder(); 739 bool is_accessible = false; 740 JVMCIKlassHandle klass(THREAD, JVMCIRuntime::get_klass_by_index(cp, index, is_accessible, loading_klass)); 741 Symbol* symbol = nullptr; 742 if (klass.is_null()) { 743 constantTag tag = cp->tag_at(index); 744 if (tag.is_klass()) { 745 // The klass has been inserted into the constant pool 746 // very recently. 747 klass = cp->resolved_klass_at(index); 748 } else if (tag.is_symbol()) { 749 symbol = cp->symbol_at(index); 750 } else { 751 assert(cp->tag_at(index).is_unresolved_klass(), "wrong tag"); 752 symbol = cp->klass_name_at(index); 753 } 754 } 755 JVMCIObject result; 756 if (!klass.is_null()) { 757 result = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL); 758 } else { 759 result = JVMCIENV->create_string(symbol, JVMCI_CHECK_NULL); 760 } 761 return JVMCIENV->get_jobject(result); 762 C2V_END 763 764 C2V_VMENTRY_NULL(jobject, lookupAppendixInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 765 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 766 oop appendix_oop = ConstantPool::appendix_at_if_loaded(cp, index); 767 return JVMCIENV->get_jobject(JVMCIENV->get_object_constant(appendix_oop)); 768 C2V_END 769 770 C2V_VMENTRY_NULL(jobject, lookupMethodInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index, jbyte opcode, ARGUMENT_PAIR(caller))) 771 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 772 methodHandle caller(THREAD, UNPACK_PAIR(Method, caller)); 773 InstanceKlass* pool_holder = cp->pool_holder(); 774 Bytecodes::Code bc = (Bytecodes::Code) (((int) opcode) & 0xFF); 775 methodHandle method(THREAD, JVMCIRuntime::get_method_by_index(cp, index, bc, pool_holder)); 776 JFR_ONLY(if (method.not_null()) Jfr::on_resolution(caller(), method(), CHECK_NULL);) 777 JVMCIObject result = JVMCIENV->get_jvmci_method(method, JVMCI_CHECK_NULL); 778 return JVMCIENV->get_jobject(result); 779 C2V_END 780 781 C2V_VMENTRY_0(jint, constantPoolRemapInstructionOperandFromCache, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 782 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 783 return cp->remap_instruction_operand_from_cache(index); 784 C2V_END 785 786 C2V_VMENTRY_NULL(jobject, resolveFieldInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index, ARGUMENT_PAIR(method), jbyte opcode, jintArray info_handle)) 787 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 788 Bytecodes::Code code = (Bytecodes::Code)(((int) opcode) & 0xFF); 789 fieldDescriptor fd; 790 methodHandle mh(THREAD, UNPACK_PAIR(Method, method)); 791 LinkInfo link_info(cp, index, mh, CHECK_NULL); 792 LinkResolver::resolve_field(fd, link_info, Bytecodes::java_code(code), false, CHECK_NULL); 793 JVMCIPrimitiveArray info = JVMCIENV->wrap(info_handle); 794 if (info.is_null() || JVMCIENV->get_length(info) != 3) { 795 JVMCI_ERROR_NULL("info must not be null and have a length of 3"); 796 } 797 JVMCIENV->put_int_at(info, 0, fd.access_flags().as_int()); 798 JVMCIENV->put_int_at(info, 1, fd.offset()); 799 JVMCIENV->put_int_at(info, 2, fd.index()); 800 JVMCIKlassHandle handle(THREAD, fd.field_holder()); 801 JVMCIObject field_holder = JVMCIENV->get_jvmci_type(handle, JVMCI_CHECK_NULL); 802 return JVMCIENV->get_jobject(field_holder); 803 C2V_END 804 805 C2V_VMENTRY_0(jint, getVtableIndexForInterfaceMethod, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), ARGUMENT_PAIR(method))) 806 Klass* klass = UNPACK_PAIR(Klass, klass); 807 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 808 InstanceKlass* holder = method->method_holder(); 809 if (klass->is_interface()) { 810 JVMCI_THROW_MSG_0(InternalError, err_msg("Interface %s should be handled in Java code", klass->external_name())); 811 } 812 if (!holder->is_interface()) { 813 JVMCI_THROW_MSG_0(InternalError, err_msg("Method %s is not held by an interface, this case should be handled in Java code", method->name_and_sig_as_C_string())); 814 } 815 if (!klass->is_instance_klass()) { 816 JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name())); 817 } 818 if (!InstanceKlass::cast(klass)->is_linked()) { 819 JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be linked", klass->external_name())); 820 } 821 if (!klass->is_subtype_of(holder)) { 822 JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s does not implement interface %s", klass->external_name(), holder->external_name())); 823 } 824 return LinkResolver::vtable_index_of_interface_method(klass, method); 825 C2V_END 826 827 C2V_VMENTRY_NULL(jobject, resolveMethod, (JNIEnv* env, jobject, ARGUMENT_PAIR(receiver), ARGUMENT_PAIR(method), ARGUMENT_PAIR(caller))) 828 Klass* recv_klass = UNPACK_PAIR(Klass, receiver); 829 Klass* caller_klass = UNPACK_PAIR(Klass, caller); 830 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 831 832 Klass* resolved = method->method_holder(); 833 Symbol* h_name = method->name(); 834 Symbol* h_signature = method->signature(); 835 836 if (MethodHandles::is_signature_polymorphic_method(method())) { 837 // Signature polymorphic methods are already resolved, JVMCI just returns NULL in this case. 838 return nullptr; 839 } 840 841 if (method->name() == vmSymbols::clone_name() && 842 resolved == vmClasses::Object_klass() && 843 recv_klass->is_array_klass()) { 844 // Resolution of the clone method on arrays always returns Object.clone even though that method 845 // has protected access. There's some trickery in the access checking to make this all work out 846 // so it's necessary to pass in the array class as the resolved class to properly trigger this. 847 // Otherwise it's impossible to resolve the array clone methods through JVMCI. See 848 // LinkResolver::check_method_accessability for the matching logic. 849 resolved = recv_klass; 850 } 851 852 LinkInfo link_info(resolved, h_name, h_signature, caller_klass); 853 Method* m = nullptr; 854 // Only do exact lookup if receiver klass has been linked. Otherwise, 855 // the vtable has not been setup, and the LinkResolver will fail. 856 if (recv_klass->is_array_klass() || 857 (InstanceKlass::cast(recv_klass)->is_linked() && !recv_klass->is_interface())) { 858 if (resolved->is_interface()) { 859 m = LinkResolver::resolve_interface_call_or_null(recv_klass, link_info); 860 } else { 861 m = LinkResolver::resolve_virtual_call_or_null(recv_klass, link_info); 862 } 863 } 864 865 if (m == nullptr) { 866 // Return NULL if there was a problem with lookup (uninitialized class, etc.) 867 return nullptr; 868 } 869 870 JVMCIObject result = JVMCIENV->get_jvmci_method(methodHandle(THREAD, m), JVMCI_CHECK_NULL); 871 return JVMCIENV->get_jobject(result); 872 C2V_END 873 874 C2V_VMENTRY_0(jboolean, hasFinalizableSubclass,(JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 875 Klass* klass = UNPACK_PAIR(Klass, klass); 876 assert(klass != nullptr, "method must not be called for primitive types"); 877 if (!klass->is_instance_klass()) { 878 return false; 879 } 880 InstanceKlass* iklass = InstanceKlass::cast(klass); 881 return Dependencies::find_finalizable_subclass(iklass) != nullptr; 882 C2V_END 883 884 C2V_VMENTRY_NULL(jobject, getClassInitializer, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 885 Klass* klass = UNPACK_PAIR(Klass, klass); 886 if (!klass->is_instance_klass()) { 887 return nullptr; 888 } 889 InstanceKlass* iklass = InstanceKlass::cast(klass); 890 methodHandle clinit(THREAD, iklass->class_initializer()); 891 JVMCIObject result = JVMCIENV->get_jvmci_method(clinit, JVMCI_CHECK_NULL); 892 return JVMCIENV->get_jobject(result); 893 C2V_END 894 895 C2V_VMENTRY_0(jlong, getMaxCallTargetOffset, (JNIEnv* env, jobject, jlong addr)) 896 address target_addr = (address) addr; 897 if (target_addr != 0x0) { 898 int64_t off_low = (int64_t)target_addr - ((int64_t)CodeCache::low_bound() + sizeof(int)); 899 int64_t off_high = (int64_t)target_addr - ((int64_t)CodeCache::high_bound() + sizeof(int)); 900 return MAX2(ABS(off_low), ABS(off_high)); 901 } 902 return -1; 903 C2V_END 904 905 C2V_VMENTRY(void, setNotInlinableOrCompilable,(JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 906 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 907 method->set_not_c1_compilable(); 908 method->set_not_c2_compilable(); 909 method->set_dont_inline(true); 910 C2V_END 911 912 C2V_VMENTRY_0(jint, getInstallCodeFlags, (JNIEnv *env, jobject)) 913 int flags = 0; 914 #ifndef PRODUCT 915 flags |= 0x0001; // VM will install block comments 916 flags |= 0x0004; // Enable HotSpotJVMCIRuntime.Option.CodeSerializationTypeInfo if not explicitly set 917 #endif 918 if (JvmtiExport::can_hotswap_or_post_breakpoint()) { 919 // VM needs to track method dependencies 920 flags |= 0x0002; 921 } 922 return flags; 923 C2V_END 924 925 C2V_VMENTRY_0(jint, installCode0, (JNIEnv *env, jobject, 926 jlong compiled_code_buffer, 927 jlong serialization_ns, 928 bool with_type_info, 929 jobject compiled_code, 930 jobjectArray object_pool, 931 jobject installed_code, 932 jlong failed_speculations_address, 933 jbyteArray speculations_obj)) 934 HandleMark hm(THREAD); 935 JNIHandleMark jni_hm(thread); 936 937 JVMCIObject compiled_code_handle = JVMCIENV->wrap(compiled_code); 938 objArrayHandle object_pool_handle(thread, JVMCIENV->is_hotspot() ? (objArrayOop) JNIHandles::resolve(object_pool) : nullptr); 939 940 CodeBlob* cb = NULL; 941 JVMCIObject installed_code_handle = JVMCIENV->wrap(installed_code); 942 JVMCIPrimitiveArray speculations_handle = JVMCIENV->wrap(speculations_obj); 943 944 int speculations_len = JVMCIENV->get_length(speculations_handle); 945 char* speculations = NEW_RESOURCE_ARRAY(char, speculations_len); 946 JVMCIENV->copy_bytes_to(speculations_handle, (jbyte*) speculations, 0, speculations_len); 947 948 JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK_JNI_ERR); 949 JVMCICompiler::CodeInstallStats* stats = compiler->code_install_stats(!thread->is_Compiler_thread()); 950 elapsedTimer *timer = stats->timer(); 951 timer->add_nanoseconds(serialization_ns); 952 TraceTime install_time("installCode", timer); 953 954 CodeInstaller installer(JVMCIENV); 955 956 JVMCI::CodeInstallResult result = installer.install(compiler, 957 compiled_code_buffer, 958 with_type_info, 959 compiled_code_handle, 960 object_pool_handle, 961 cb, 962 installed_code_handle, 963 (FailedSpeculation**)(address) failed_speculations_address, 964 speculations, 965 speculations_len, 966 JVMCI_CHECK_0); 967 968 if (PrintCodeCacheOnCompilation) { 969 stringStream s; 970 // Dump code cache into a buffer before locking the tty, 971 { 972 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 973 CodeCache::print_summary(&s, false); 974 } 975 ttyLocker ttyl; 976 tty->print_raw_cr(s.freeze()); 977 } 978 979 if (result != JVMCI::ok) { 980 assert(cb == NULL, "should be"); 981 } else { 982 stats->on_install(cb); 983 if (installed_code_handle.is_non_null()) { 984 if (cb->is_nmethod()) { 985 assert(JVMCIENV->isa_HotSpotNmethod(installed_code_handle), "wrong type"); 986 // Clear the link to an old nmethod first 987 JVMCIObject nmethod_mirror = installed_code_handle; 988 JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, true, JVMCI_CHECK_0); 989 } else { 990 assert(JVMCIENV->isa_InstalledCode(installed_code_handle), "wrong type"); 991 } 992 // Initialize the link to the new code blob 993 JVMCIENV->initialize_installed_code(installed_code_handle, cb, JVMCI_CHECK_0); 994 } 995 } 996 return result; 997 C2V_END 998 999 C2V_VMENTRY(void, resetCompilationStatistics, (JNIEnv* env, jobject)) 1000 JVMCICompiler* compiler = JVMCICompiler::instance(true, CHECK); 1001 CompilerStatistics* stats = compiler->stats(); 1002 stats->_standard.reset(); 1003 stats->_osr.reset(); 1004 C2V_END 1005 1006 C2V_VMENTRY_NULL(jobject, disassembleCodeBlob, (JNIEnv* env, jobject, jobject installedCode)) 1007 HandleMark hm(THREAD); 1008 1009 if (installedCode == NULL) { 1010 JVMCI_THROW_MSG_NULL(NullPointerException, "installedCode is null"); 1011 } 1012 1013 JVMCIObject installedCodeObject = JVMCIENV->wrap(installedCode); 1014 CodeBlob* cb = JVMCIENV->get_code_blob(installedCodeObject); 1015 if (cb == NULL) { 1016 return NULL; 1017 } 1018 1019 // We don't want the stringStream buffer to resize during disassembly as it 1020 // uses scoped resource memory. If a nested function called during disassembly uses 1021 // a ResourceMark and the buffer expands within the scope of the mark, 1022 // the buffer becomes garbage when that scope is exited. Experience shows that 1023 // the disassembled code is typically about 10x the code size so a fixed buffer 1024 // sized to 20x code size plus a fixed amount for header info should be sufficient. 1025 int bufferSize = cb->code_size() * 20 + 1024; 1026 char* buffer = NEW_RESOURCE_ARRAY(char, bufferSize); 1027 stringStream st(buffer, bufferSize); 1028 Disassembler::decode(cb, &st); 1029 if (st.size() <= 0) { 1030 return NULL; 1031 } 1032 1033 JVMCIObject result = JVMCIENV->create_string(st.as_string(), JVMCI_CHECK_NULL); 1034 return JVMCIENV->get_jobject(result); 1035 C2V_END 1036 1037 C2V_VMENTRY_NULL(jobject, getStackTraceElement, (JNIEnv* env, jobject, ARGUMENT_PAIR(method), int bci)) 1038 HandleMark hm(THREAD); 1039 1040 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 1041 JVMCIObject element = JVMCIENV->new_StackTraceElement(method, bci, JVMCI_CHECK_NULL); 1042 return JVMCIENV->get_jobject(element); 1043 C2V_END 1044 1045 C2V_VMENTRY_NULL(jobject, executeHotSpotNmethod, (JNIEnv* env, jobject, jobject args, jobject hs_nmethod)) 1046 // The incoming arguments array would have to contain JavaConstants instead of regular objects 1047 // and the return value would have to be wrapped as a JavaConstant. 1048 requireInHotSpot("executeHotSpotNmethod", JVMCI_CHECK_NULL); 1049 1050 HandleMark hm(THREAD); 1051 1052 JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod); 1053 nmethod* nm = JVMCIENV->get_nmethod(nmethod_mirror); 1054 if (nm == NULL || !nm->is_in_use()) { 1055 JVMCI_THROW_NULL(InvalidInstalledCodeException); 1056 } 1057 methodHandle mh(THREAD, nm->method()); 1058 Symbol* signature = mh->signature(); 1059 JavaCallArguments jca(mh->size_of_parameters()); 1060 1061 JavaArgumentUnboxer jap(signature, &jca, (arrayOop) JNIHandles::resolve(args), mh->is_static()); 1062 JavaValue result(jap.return_type()); 1063 jca.set_alternative_target(Handle(THREAD, JNIHandles::resolve(nmethod_mirror.as_jobject()))); 1064 JavaCalls::call(&result, mh, &jca, CHECK_NULL); 1065 1066 if (jap.return_type() == T_VOID) { 1067 return NULL; 1068 } else if (is_reference_type(jap.return_type())) { 1069 return JNIHandles::make_local(THREAD, result.get_oop()); 1070 } else { 1071 jvalue *value = (jvalue *) result.get_value_addr(); 1072 // Narrow the value down if required (Important on big endian machines) 1073 switch (jap.return_type()) { 1074 case T_BOOLEAN: 1075 value->z = (jboolean) value->i; 1076 break; 1077 case T_BYTE: 1078 value->b = (jbyte) value->i; 1079 break; 1080 case T_CHAR: 1081 value->c = (jchar) value->i; 1082 break; 1083 case T_SHORT: 1084 value->s = (jshort) value->i; 1085 break; 1086 default: 1087 break; 1088 } 1089 JVMCIObject o = JVMCIENV->create_box(jap.return_type(), value, JVMCI_CHECK_NULL); 1090 return JVMCIENV->get_jobject(o); 1091 } 1092 C2V_END 1093 1094 C2V_VMENTRY_NULL(jlongArray, getLineNumberTable, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 1095 Method* method = UNPACK_PAIR(Method, method); 1096 if (!method->has_linenumber_table()) { 1097 return NULL; 1098 } 1099 u2 num_entries = 0; 1100 CompressedLineNumberReadStream streamForSize(method->compressed_linenumber_table()); 1101 while (streamForSize.read_pair()) { 1102 num_entries++; 1103 } 1104 1105 CompressedLineNumberReadStream stream(method->compressed_linenumber_table()); 1106 JVMCIPrimitiveArray result = JVMCIENV->new_longArray(2 * num_entries, JVMCI_CHECK_NULL); 1107 1108 int i = 0; 1109 jlong value; 1110 while (stream.read_pair()) { 1111 value = ((jlong) stream.bci()); 1112 JVMCIENV->put_long_at(result, i, value); 1113 value = ((jlong) stream.line()); 1114 JVMCIENV->put_long_at(result, i + 1, value); 1115 i += 2; 1116 } 1117 1118 return (jlongArray) JVMCIENV->get_jobject(result); 1119 C2V_END 1120 1121 C2V_VMENTRY_0(jlong, getLocalVariableTableStart, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 1122 Method* method = UNPACK_PAIR(Method, method); 1123 if (!method->has_localvariable_table()) { 1124 return 0; 1125 } 1126 return (jlong) (address) method->localvariable_table_start(); 1127 C2V_END 1128 1129 C2V_VMENTRY_0(jint, getLocalVariableTableLength, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 1130 Method* method = UNPACK_PAIR(Method, method); 1131 return method->localvariable_table_length(); 1132 C2V_END 1133 1134 C2V_VMENTRY(void, reprofile, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 1135 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 1136 MethodCounters* mcs = method->method_counters(); 1137 if (mcs != NULL) { 1138 mcs->clear_counters(); 1139 } 1140 NOT_PRODUCT(method->set_compiled_invocation_count(0)); 1141 1142 CompiledMethod* code = method->code(); 1143 if (code != NULL) { 1144 code->make_not_entrant(); 1145 } 1146 1147 MethodData* method_data = method->method_data(); 1148 if (method_data == NULL) { 1149 ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); 1150 method_data = MethodData::allocate(loader_data, method, CHECK); 1151 method->set_method_data(method_data); 1152 } else { 1153 method_data->initialize(); 1154 } 1155 C2V_END 1156 1157 1158 C2V_VMENTRY(void, invalidateHotSpotNmethod, (JNIEnv* env, jobject, jobject hs_nmethod, jboolean deoptimize)) 1159 JVMCIObject nmethod_mirror = JVMCIENV->wrap(hs_nmethod); 1160 JVMCIENV->invalidate_nmethod_mirror(nmethod_mirror, deoptimize, JVMCI_CHECK); 1161 C2V_END 1162 1163 C2V_VMENTRY_NULL(jlongArray, collectCounters, (JNIEnv* env, jobject)) 1164 // Returns a zero length array if counters aren't enabled 1165 JVMCIPrimitiveArray array = JVMCIENV->new_longArray(JVMCICounterSize, JVMCI_CHECK_NULL); 1166 if (JVMCICounterSize > 0) { 1167 jlong* temp_array = NEW_RESOURCE_ARRAY(jlong, JVMCICounterSize); 1168 JavaThread::collect_counters(temp_array, JVMCICounterSize); 1169 JVMCIENV->copy_longs_from(temp_array, array, 0, JVMCICounterSize); 1170 } 1171 return (jlongArray) JVMCIENV->get_jobject(array); 1172 C2V_END 1173 1174 C2V_VMENTRY_0(jint, getCountersSize, (JNIEnv* env, jobject)) 1175 return (jint) JVMCICounterSize; 1176 C2V_END 1177 1178 C2V_VMENTRY_0(jboolean, setCountersSize, (JNIEnv* env, jobject, jint new_size)) 1179 return JavaThread::resize_all_jvmci_counters(new_size); 1180 C2V_END 1181 1182 C2V_VMENTRY_0(jint, allocateCompileId, (JNIEnv* env, jobject, ARGUMENT_PAIR(method), int entry_bci)) 1183 HandleMark hm(THREAD); 1184 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 1185 if (method.is_null()) { 1186 JVMCI_THROW_0(NullPointerException); 1187 } 1188 if (entry_bci >= method->code_size() || entry_bci < -1) { 1189 JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Unexpected bci %d", entry_bci)); 1190 } 1191 return CompileBroker::assign_compile_id_unlocked(THREAD, method, entry_bci); 1192 C2V_END 1193 1194 1195 C2V_VMENTRY_0(jboolean, isMature, (JNIEnv* env, jobject, jlong method_data_pointer)) 1196 MethodData* mdo = (MethodData*) method_data_pointer; 1197 return mdo != NULL && mdo->is_mature(); 1198 C2V_END 1199 1200 C2V_VMENTRY_0(jboolean, hasCompiledCodeForOSR, (JNIEnv* env, jobject, ARGUMENT_PAIR(method), int entry_bci, int comp_level)) 1201 Method* method = UNPACK_PAIR(Method, method); 1202 return method->lookup_osr_nmethod_for(entry_bci, comp_level, true) != NULL; 1203 C2V_END 1204 1205 C2V_VMENTRY_NULL(jobject, getSymbol, (JNIEnv* env, jobject, jlong symbol)) 1206 JVMCIObject sym = JVMCIENV->create_string((Symbol*)(address)symbol, JVMCI_CHECK_NULL); 1207 return JVMCIENV->get_jobject(sym); 1208 C2V_END 1209 1210 C2V_VMENTRY_NULL(jobject, getSignatureName, (JNIEnv* env, jobject, jlong klass_pointer)) 1211 Klass* klass = UNPACK_PAIR(Klass, klass); 1212 JVMCIObject signature = JVMCIENV->create_string(klass->signature_name(), JVMCI_CHECK_NULL); 1213 return JVMCIENV->get_jobject(signature); 1214 C2V_END 1215 1216 /* 1217 * Used by matches() to convert a ResolvedJavaMethod[] to an array of Method*. 1218 */ 1219 GrowableArray<Method*>* init_resolved_methods(jobjectArray methods, JVMCIEnv* JVMCIENV) { 1220 objArrayOop methods_oop = (objArrayOop) JNIHandles::resolve(methods); 1221 GrowableArray<Method*>* resolved_methods = new GrowableArray<Method*>(methods_oop->length()); 1222 for (int i = 0; i < methods_oop->length(); i++) { 1223 oop resolved = methods_oop->obj_at(i); 1224 Method* resolved_method = NULL; 1225 if (resolved->klass() == HotSpotJVMCI::HotSpotResolvedJavaMethodImpl::klass()) { 1226 resolved_method = HotSpotJVMCI::asMethod(JVMCIENV, resolved); 1227 } 1228 resolved_methods->append(resolved_method); 1229 } 1230 return resolved_methods; 1231 } 1232 1233 /* 1234 * Used by c2v_iterateFrames to check if `method` matches one of the ResolvedJavaMethods in the `methods` array. 1235 * The ResolvedJavaMethod[] array is converted to a Method* array that is then cached in the resolved_methods_ref in/out parameter. 1236 * In case of a match, the matching ResolvedJavaMethod is returned in matched_jvmci_method_ref. 1237 */ 1238 bool matches(jobjectArray methods, Method* method, GrowableArray<Method*>** resolved_methods_ref, Handle* matched_jvmci_method_ref, Thread* THREAD, JVMCIEnv* JVMCIENV) { 1239 GrowableArray<Method*>* resolved_methods = *resolved_methods_ref; 1240 if (resolved_methods == NULL) { 1241 resolved_methods = init_resolved_methods(methods, JVMCIENV); 1242 *resolved_methods_ref = resolved_methods; 1243 } 1244 assert(method != NULL, "method should not be NULL"); 1245 assert(resolved_methods->length() == ((objArrayOop) JNIHandles::resolve(methods))->length(), "arrays must have the same length"); 1246 for (int i = 0; i < resolved_methods->length(); i++) { 1247 Method* m = resolved_methods->at(i); 1248 if (m == method) { 1249 *matched_jvmci_method_ref = Handle(THREAD, ((objArrayOop) JNIHandles::resolve(methods))->obj_at(i)); 1250 return true; 1251 } 1252 } 1253 return false; 1254 } 1255 1256 /* 1257 * Resolves an interface call to a concrete method handle. 1258 */ 1259 methodHandle resolve_interface_call(Klass* spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS) { 1260 CallInfo callinfo; 1261 Handle receiver = args->receiver(); 1262 Klass* recvrKlass = receiver.is_null() ? (Klass*)NULL : receiver->klass(); 1263 LinkInfo link_info(spec_klass, name, signature); 1264 LinkResolver::resolve_interface_call( 1265 callinfo, receiver, recvrKlass, link_info, true, CHECK_(methodHandle())); 1266 methodHandle method(THREAD, callinfo.selected_method()); 1267 assert(method.not_null(), "should have thrown exception"); 1268 return method; 1269 } 1270 1271 /* 1272 * Used by c2v_iterateFrames to make a new vframeStream at the given compiled frame id (stack pointer) and vframe id. 1273 */ 1274 void resync_vframestream_to_compiled_frame(vframeStream& vfst, intptr_t* stack_pointer, int vframe_id, JavaThread* thread, TRAPS) { 1275 vfst = vframeStream(thread); 1276 while (vfst.frame_id() != stack_pointer && !vfst.at_end()) { 1277 vfst.next(); 1278 } 1279 if (vfst.frame_id() != stack_pointer) { 1280 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "stack frame not found after deopt") 1281 } 1282 if (vfst.is_interpreted_frame()) { 1283 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "compiled stack frame expected") 1284 } 1285 while (vfst.vframe_id() != vframe_id) { 1286 if (vfst.at_end()) { 1287 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), "vframe not found after deopt") 1288 } 1289 vfst.next(); 1290 assert(!vfst.is_interpreted_frame(), "Wrong frame type"); 1291 } 1292 } 1293 1294 /* 1295 * Used by c2v_iterateFrames. Returns an array of any unallocated scope objects or NULL if none. 1296 */ 1297 GrowableArray<ScopeValue*>* get_unallocated_objects_or_null(GrowableArray<ScopeValue*>* scope_objects) { 1298 GrowableArray<ScopeValue*>* unallocated = NULL; 1299 for (int i = 0; i < scope_objects->length(); i++) { 1300 ObjectValue* sv = (ObjectValue*) scope_objects->at(i); 1301 if (sv->value().is_null()) { 1302 if (unallocated == NULL) { 1303 unallocated = new GrowableArray<ScopeValue*>(scope_objects->length()); 1304 } 1305 unallocated->append(sv); 1306 } 1307 } 1308 return unallocated; 1309 } 1310 1311 C2V_VMENTRY_NULL(jobject, iterateFrames, (JNIEnv* env, jobject compilerToVM, jobjectArray initial_methods, jobjectArray match_methods, jint initialSkip, jobject visitor_handle)) 1312 1313 if (!thread->has_last_Java_frame()) { 1314 return NULL; 1315 } 1316 Handle visitor(THREAD, JNIHandles::resolve_non_null(visitor_handle)); 1317 1318 requireInHotSpot("iterateFrames", JVMCI_CHECK_NULL); 1319 1320 HotSpotJVMCI::HotSpotStackFrameReference::klass()->initialize(CHECK_NULL); 1321 1322 vframeStream vfst(thread); 1323 jobjectArray methods = initial_methods; 1324 methodHandle visitor_method; 1325 GrowableArray<Method*>* resolved_methods = NULL; 1326 1327 while (!vfst.at_end()) { // frame loop 1328 bool realloc_called = false; 1329 intptr_t* frame_id = vfst.frame_id(); 1330 1331 // Previous compiledVFrame of this frame; use with at_scope() to reuse scope object pool. 1332 compiledVFrame* prev_cvf = NULL; 1333 1334 for (; !vfst.at_end() && vfst.frame_id() == frame_id; vfst.next()) { // vframe loop 1335 int frame_number = 0; 1336 Method *method = vfst.method(); 1337 int bci = vfst.bci(); 1338 1339 Handle matched_jvmci_method; 1340 if (methods == NULL || matches(methods, method, &resolved_methods, &matched_jvmci_method, THREAD, JVMCIENV)) { 1341 if (initialSkip > 0) { 1342 initialSkip--; 1343 continue; 1344 } 1345 javaVFrame* vf; 1346 if (prev_cvf != NULL && prev_cvf->frame_pointer()->id() == frame_id) { 1347 assert(prev_cvf->is_compiled_frame(), "expected compiled Java frame"); 1348 vf = prev_cvf->at_scope(vfst.decode_offset(), vfst.vframe_id()); 1349 } else { 1350 vf = vfst.asJavaVFrame(); 1351 } 1352 1353 StackValueCollection* locals = NULL; 1354 typeArrayHandle localIsVirtual_h; 1355 if (vf->is_compiled_frame()) { 1356 // compiled method frame 1357 compiledVFrame* cvf = compiledVFrame::cast(vf); 1358 1359 ScopeDesc* scope = cvf->scope(); 1360 // native wrappers do not have a scope 1361 if (scope != NULL && scope->objects() != NULL) { 1362 prev_cvf = cvf; 1363 1364 GrowableArray<ScopeValue*>* objects = NULL; 1365 if (!realloc_called) { 1366 objects = scope->objects(); 1367 } else { 1368 // some object might already have been re-allocated, only reallocate the non-allocated ones 1369 objects = get_unallocated_objects_or_null(scope->objects()); 1370 } 1371 1372 if (objects != NULL) { 1373 RegisterMap reg_map(vf->register_map()); 1374 bool realloc_failures = Deoptimization::realloc_objects(thread, vf->frame_pointer(), ®_map, objects, CHECK_NULL); 1375 Deoptimization::reassign_fields(vf->frame_pointer(), ®_map, objects, realloc_failures, false, CHECK_NULL); 1376 realloc_called = true; 1377 } 1378 1379 GrowableArray<ScopeValue*>* local_values = scope->locals(); 1380 for (int i = 0; i < local_values->length(); i++) { 1381 ScopeValue* value = local_values->at(i); 1382 if (value->is_object()) { 1383 if (localIsVirtual_h.is_null()) { 1384 typeArrayOop array_oop = oopFactory::new_boolArray(local_values->length(), CHECK_NULL); 1385 localIsVirtual_h = typeArrayHandle(THREAD, array_oop); 1386 } 1387 localIsVirtual_h->bool_at_put(i, true); 1388 } 1389 } 1390 } 1391 1392 locals = cvf->locals(); 1393 frame_number = cvf->vframe_id(); 1394 } else { 1395 // interpreted method frame 1396 interpretedVFrame* ivf = interpretedVFrame::cast(vf); 1397 1398 locals = ivf->locals(); 1399 } 1400 assert(bci == vf->bci(), "wrong bci"); 1401 assert(method == vf->method(), "wrong method"); 1402 1403 Handle frame_reference = HotSpotJVMCI::HotSpotStackFrameReference::klass()->allocate_instance_handle(CHECK_NULL); 1404 HotSpotJVMCI::HotSpotStackFrameReference::set_bci(JVMCIENV, frame_reference(), bci); 1405 if (matched_jvmci_method.is_null()) { 1406 methodHandle mh(THREAD, method); 1407 JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL); 1408 matched_jvmci_method = Handle(THREAD, JNIHandles::resolve(jvmci_method.as_jobject())); 1409 } 1410 HotSpotJVMCI::HotSpotStackFrameReference::set_method(JVMCIENV, frame_reference(), matched_jvmci_method()); 1411 HotSpotJVMCI::HotSpotStackFrameReference::set_localIsVirtual(JVMCIENV, frame_reference(), localIsVirtual_h()); 1412 1413 HotSpotJVMCI::HotSpotStackFrameReference::set_compilerToVM(JVMCIENV, frame_reference(), JNIHandles::resolve(compilerToVM)); 1414 HotSpotJVMCI::HotSpotStackFrameReference::set_stackPointer(JVMCIENV, frame_reference(), (jlong) frame_id); 1415 HotSpotJVMCI::HotSpotStackFrameReference::set_frameNumber(JVMCIENV, frame_reference(), frame_number); 1416 1417 // initialize the locals array 1418 objArrayOop array_oop = oopFactory::new_objectArray(locals->size(), CHECK_NULL); 1419 objArrayHandle array(THREAD, array_oop); 1420 for (int i = 0; i < locals->size(); i++) { 1421 StackValue* var = locals->at(i); 1422 if (var->type() == T_OBJECT) { 1423 array->obj_at_put(i, locals->at(i)->get_obj()()); 1424 } 1425 } 1426 HotSpotJVMCI::HotSpotStackFrameReference::set_locals(JVMCIENV, frame_reference(), array()); 1427 HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, frame_reference(), JNI_FALSE); 1428 1429 JavaValue result(T_OBJECT); 1430 JavaCallArguments args(visitor); 1431 if (visitor_method.is_null()) { 1432 visitor_method = resolve_interface_call(HotSpotJVMCI::InspectedFrameVisitor::klass(), vmSymbols::visitFrame_name(), vmSymbols::visitFrame_signature(), &args, CHECK_NULL); 1433 } 1434 1435 args.push_oop(frame_reference); 1436 JavaCalls::call(&result, visitor_method, &args, CHECK_NULL); 1437 if (result.get_oop() != NULL) { 1438 return JNIHandles::make_local(thread, result.get_oop()); 1439 } 1440 if (methods == initial_methods) { 1441 methods = match_methods; 1442 if (resolved_methods != NULL && JNIHandles::resolve(match_methods) != JNIHandles::resolve(initial_methods)) { 1443 resolved_methods = NULL; 1444 } 1445 } 1446 assert(initialSkip == 0, "There should be no match before initialSkip == 0"); 1447 if (HotSpotJVMCI::HotSpotStackFrameReference::objectsMaterialized(JVMCIENV, frame_reference()) == JNI_TRUE) { 1448 // the frame has been deoptimized, we need to re-synchronize the frame and vframe 1449 prev_cvf = NULL; 1450 intptr_t* stack_pointer = (intptr_t*) HotSpotJVMCI::HotSpotStackFrameReference::stackPointer(JVMCIENV, frame_reference()); 1451 resync_vframestream_to_compiled_frame(vfst, stack_pointer, frame_number, thread, CHECK_NULL); 1452 } 1453 } 1454 } // end of vframe loop 1455 } // end of frame loop 1456 1457 // the end was reached without finding a matching method 1458 return NULL; 1459 C2V_END 1460 1461 C2V_VMENTRY(void, resolveInvokeDynamicInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 1462 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 1463 CallInfo callInfo; 1464 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokedynamic, CHECK); 1465 ConstantPoolCacheEntry* cp_cache_entry = cp->invokedynamic_cp_cache_entry_at(index); 1466 cp_cache_entry->set_dynamic_call(cp, callInfo); 1467 C2V_END 1468 1469 C2V_VMENTRY(void, resolveInvokeHandleInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 1470 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 1471 Klass* holder = cp->klass_ref_at(index, CHECK); 1472 Symbol* name = cp->name_ref_at(index); 1473 if (MethodHandles::is_signature_polymorphic_name(holder, name)) { 1474 CallInfo callInfo; 1475 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, Bytecodes::_invokehandle, CHECK); 1476 ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index)); 1477 cp_cache_entry->set_method_handle(cp, callInfo); 1478 } 1479 C2V_END 1480 1481 C2V_VMENTRY_0(jint, isResolvedInvokeHandleInPool, (JNIEnv* env, jobject, ARGUMENT_PAIR(cp), jint index)) 1482 constantPoolHandle cp(THREAD, UNPACK_PAIR(ConstantPool, cp)); 1483 ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index)); 1484 if (cp_cache_entry->is_resolved(Bytecodes::_invokehandle)) { 1485 // MethodHandle.invoke* --> LambdaForm? 1486 ResourceMark rm; 1487 1488 LinkInfo link_info(cp, index, CATCH); 1489 1490 Klass* resolved_klass = link_info.resolved_klass(); 1491 1492 Symbol* name_sym = cp->name_ref_at(index); 1493 1494 vmassert(MethodHandles::is_method_handle_invoke_name(resolved_klass, name_sym), "!"); 1495 vmassert(MethodHandles::is_signature_polymorphic_name(resolved_klass, name_sym), "!"); 1496 1497 methodHandle adapter_method(THREAD, cp_cache_entry->f1_as_method()); 1498 1499 methodHandle resolved_method(adapter_method); 1500 1501 // Can we treat it as a regular invokevirtual? 1502 if (resolved_method->method_holder() == resolved_klass && resolved_method->name() == name_sym) { 1503 vmassert(!resolved_method->is_static(),"!"); 1504 vmassert(MethodHandles::is_signature_polymorphic_method(resolved_method()),"!"); 1505 vmassert(!MethodHandles::is_signature_polymorphic_static(resolved_method->intrinsic_id()), "!"); 1506 vmassert(cp_cache_entry->appendix_if_resolved(cp) == NULL, "!"); 1507 1508 methodHandle m(THREAD, LinkResolver::linktime_resolve_virtual_method_or_null(link_info)); 1509 vmassert(m == resolved_method, "!!"); 1510 return -1; 1511 } 1512 1513 return Bytecodes::_invokevirtual; 1514 } 1515 if (cp_cache_entry->is_resolved(Bytecodes::_invokedynamic)) { 1516 return Bytecodes::_invokedynamic; 1517 } 1518 return -1; 1519 C2V_END 1520 1521 1522 C2V_VMENTRY_NULL(jobject, getSignaturePolymorphicHolders, (JNIEnv* env, jobject)) 1523 JVMCIObjectArray holders = JVMCIENV->new_String_array(2, JVMCI_CHECK_NULL); 1524 JVMCIObject mh = JVMCIENV->create_string("Ljava/lang/invoke/MethodHandle;", JVMCI_CHECK_NULL); 1525 JVMCIObject vh = JVMCIENV->create_string("Ljava/lang/invoke/VarHandle;", JVMCI_CHECK_NULL); 1526 JVMCIENV->put_object_at(holders, 0, mh); 1527 JVMCIENV->put_object_at(holders, 1, vh); 1528 return JVMCIENV->get_jobject(holders); 1529 C2V_END 1530 1531 C2V_VMENTRY_0(jboolean, shouldDebugNonSafepoints, (JNIEnv* env, jobject)) 1532 //see compute_recording_non_safepoints in debugInfroRec.cpp 1533 if (JvmtiExport::should_post_compiled_method_load() && FLAG_IS_DEFAULT(DebugNonSafepoints)) { 1534 return true; 1535 } 1536 return DebugNonSafepoints; 1537 C2V_END 1538 1539 // public native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate); 1540 C2V_VMENTRY(void, materializeVirtualObjects, (JNIEnv* env, jobject, jobject _hs_frame, bool invalidate)) 1541 JVMCIObject hs_frame = JVMCIENV->wrap(_hs_frame); 1542 if (hs_frame.is_null()) { 1543 JVMCI_THROW_MSG(NullPointerException, "stack frame is null"); 1544 } 1545 1546 requireInHotSpot("materializeVirtualObjects", JVMCI_CHECK); 1547 1548 JVMCIENV->HotSpotStackFrameReference_initialize(JVMCI_CHECK); 1549 1550 // look for the given stack frame 1551 StackFrameStream fst(thread, false /* update */, true /* process_frames */); 1552 intptr_t* stack_pointer = (intptr_t*) JVMCIENV->get_HotSpotStackFrameReference_stackPointer(hs_frame); 1553 while (fst.current()->id() != stack_pointer && !fst.is_done()) { 1554 fst.next(); 1555 } 1556 if (fst.current()->id() != stack_pointer) { 1557 JVMCI_THROW_MSG(IllegalStateException, "stack frame not found"); 1558 } 1559 1560 if (invalidate) { 1561 if (!fst.current()->is_compiled_frame()) { 1562 JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected"); 1563 } 1564 assert(fst.current()->cb()->is_nmethod(), "nmethod expected"); 1565 ((nmethod*) fst.current()->cb())->make_not_entrant(); 1566 } 1567 Deoptimization::deoptimize(thread, *fst.current(), Deoptimization::Reason_none); 1568 // look for the frame again as it has been updated by deopt (pc, deopt state...) 1569 StackFrameStream fstAfterDeopt(thread, true /* update */, true /* process_frames */); 1570 while (fstAfterDeopt.current()->id() != stack_pointer && !fstAfterDeopt.is_done()) { 1571 fstAfterDeopt.next(); 1572 } 1573 if (fstAfterDeopt.current()->id() != stack_pointer) { 1574 JVMCI_THROW_MSG(IllegalStateException, "stack frame not found after deopt"); 1575 } 1576 1577 vframe* vf = vframe::new_vframe(fstAfterDeopt.current(), fstAfterDeopt.register_map(), thread); 1578 if (!vf->is_compiled_frame()) { 1579 JVMCI_THROW_MSG(IllegalStateException, "compiled stack frame expected"); 1580 } 1581 1582 GrowableArray<compiledVFrame*>* virtualFrames = new GrowableArray<compiledVFrame*>(10); 1583 while (true) { 1584 assert(vf->is_compiled_frame(), "Wrong frame type"); 1585 virtualFrames->push(compiledVFrame::cast(vf)); 1586 if (vf->is_top()) { 1587 break; 1588 } 1589 vf = vf->sender(); 1590 } 1591 1592 int last_frame_number = JVMCIENV->get_HotSpotStackFrameReference_frameNumber(hs_frame); 1593 if (last_frame_number >= virtualFrames->length()) { 1594 JVMCI_THROW_MSG(IllegalStateException, "invalid frame number"); 1595 } 1596 1597 // Reallocate the non-escaping objects and restore their fields. 1598 assert (virtualFrames->at(last_frame_number)->scope() != NULL,"invalid scope"); 1599 GrowableArray<ScopeValue*>* objects = virtualFrames->at(last_frame_number)->scope()->objects(); 1600 1601 if (objects == NULL) { 1602 // no objects to materialize 1603 return; 1604 } 1605 1606 bool realloc_failures = Deoptimization::realloc_objects(thread, fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, CHECK); 1607 Deoptimization::reassign_fields(fstAfterDeopt.current(), fstAfterDeopt.register_map(), objects, realloc_failures, false, THREAD); 1608 1609 for (int frame_index = 0; frame_index < virtualFrames->length(); frame_index++) { 1610 compiledVFrame* cvf = virtualFrames->at(frame_index); 1611 1612 GrowableArray<ScopeValue*>* scopedValues = cvf->scope()->locals(); 1613 StackValueCollection* locals = cvf->locals(); 1614 if (locals != NULL) { 1615 for (int i2 = 0; i2 < locals->size(); i2++) { 1616 StackValue* var = locals->at(i2); 1617 if (var->type() == T_OBJECT && scopedValues->at(i2)->is_object()) { 1618 jvalue val; 1619 val.l = cast_from_oop<jobject>(locals->at(i2)->get_obj()()); 1620 cvf->update_local(T_OBJECT, i2, val); 1621 } 1622 } 1623 } 1624 1625 GrowableArray<ScopeValue*>* scopeExpressions = cvf->scope()->expressions(); 1626 StackValueCollection* expressions = cvf->expressions(); 1627 if (expressions != NULL) { 1628 for (int i2 = 0; i2 < expressions->size(); i2++) { 1629 StackValue* var = expressions->at(i2); 1630 if (var->type() == T_OBJECT && scopeExpressions->at(i2)->is_object()) { 1631 jvalue val; 1632 val.l = cast_from_oop<jobject>(expressions->at(i2)->get_obj()()); 1633 cvf->update_stack(T_OBJECT, i2, val); 1634 } 1635 } 1636 } 1637 1638 GrowableArray<MonitorValue*>* scopeMonitors = cvf->scope()->monitors(); 1639 GrowableArray<MonitorInfo*>* monitors = cvf->monitors(); 1640 if (monitors != NULL) { 1641 for (int i2 = 0; i2 < monitors->length(); i2++) { 1642 cvf->update_monitor(i2, monitors->at(i2)); 1643 } 1644 } 1645 } 1646 1647 // all locals are materialized by now 1648 JVMCIENV->set_HotSpotStackFrameReference_localIsVirtual(hs_frame, NULL); 1649 // update the locals array 1650 JVMCIObjectArray array = JVMCIENV->get_HotSpotStackFrameReference_locals(hs_frame); 1651 StackValueCollection* locals = virtualFrames->at(last_frame_number)->locals(); 1652 for (int i = 0; i < locals->size(); i++) { 1653 StackValue* var = locals->at(i); 1654 if (var->type() == T_OBJECT) { 1655 JVMCIENV->put_object_at(array, i, HotSpotJVMCI::wrap(locals->at(i)->get_obj()())); 1656 } 1657 } 1658 HotSpotJVMCI::HotSpotStackFrameReference::set_objectsMaterialized(JVMCIENV, hs_frame, JNI_TRUE); 1659 C2V_END 1660 1661 // Use of tty does not require the current thread to be attached to the VM 1662 // so no need for a full C2V_VMENTRY transition. 1663 C2V_VMENTRY_PREFIX(void, writeDebugOutput, (JNIEnv* env, jobject, jlong buffer, jint length, bool flush)) 1664 if (length <= 8) { 1665 tty->write((char*) &buffer, length); 1666 } else { 1667 tty->write((char*) buffer, length); 1668 } 1669 if (flush) { 1670 tty->flush(); 1671 } 1672 C2V_END 1673 1674 // Use of tty does not require the current thread to be attached to the VM 1675 // so no need for a full C2V_VMENTRY transition. 1676 C2V_VMENTRY_PREFIX(void, flushDebugOutput, (JNIEnv* env, jobject)) 1677 tty->flush(); 1678 C2V_END 1679 1680 C2V_VMENTRY_0(jint, methodDataProfileDataSize, (JNIEnv* env, jobject, jlong method_data_pointer, jint position)) 1681 MethodData* mdo = (MethodData*) method_data_pointer; 1682 ProfileData* profile_data = mdo->data_at(position); 1683 if (mdo->is_valid(profile_data)) { 1684 return profile_data->size_in_bytes(); 1685 } 1686 DataLayout* data = mdo->extra_data_base(); 1687 DataLayout* end = mdo->extra_data_limit(); 1688 for (;; data = mdo->next_extra(data)) { 1689 assert(data < end, "moved past end of extra data"); 1690 profile_data = data->data_in(); 1691 if (mdo->dp_to_di(profile_data->dp()) == position) { 1692 return profile_data->size_in_bytes(); 1693 } 1694 } 1695 JVMCI_THROW_MSG_0(IllegalArgumentException, err_msg("Invalid profile data position %d", position)); 1696 C2V_END 1697 1698 C2V_VMENTRY_NULL(jobject, getInterfaces, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1699 Klass* klass = UNPACK_PAIR(Klass, klass); 1700 if (klass == nullptr) { 1701 JVMCI_THROW_0(NullPointerException); 1702 } 1703 1704 if (!klass->is_instance_klass()) { 1705 JVMCI_THROW_MSG_0(InternalError, err_msg("Class %s must be instance klass", klass->external_name())); 1706 } 1707 InstanceKlass* iklass = InstanceKlass::cast(klass); 1708 1709 // Regular instance klass, fill in all local interfaces 1710 int size = iklass->local_interfaces()->length(); 1711 JVMCIObjectArray interfaces = JVMCIENV->new_HotSpotResolvedObjectTypeImpl_array(size, JVMCI_CHECK_NULL); 1712 for (int index = 0; index < size; index++) { 1713 JVMCIKlassHandle klass(THREAD); 1714 Klass* k = iklass->local_interfaces()->at(index); 1715 klass = k; 1716 JVMCIObject type = JVMCIENV->get_jvmci_type(klass, JVMCI_CHECK_NULL); 1717 JVMCIENV->put_object_at(interfaces, index, type); 1718 } 1719 return JVMCIENV->get_jobject(interfaces); 1720 C2V_END 1721 1722 C2V_VMENTRY_NULL(jobject, getComponentType, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1723 Klass* klass = UNPACK_PAIR(Klass, klass); 1724 if (klass == nullptr) { 1725 JVMCI_THROW_0(NullPointerException); 1726 } 1727 1728 if (!klass->is_array_klass()) { 1729 return nullptr; 1730 } 1731 oop mirror = klass->java_mirror(); 1732 oop component_mirror = java_lang_Class::component_mirror(mirror); 1733 if (component_mirror == nullptr) { 1734 JVMCI_THROW_MSG_0(NullPointerException, 1735 err_msg("Component mirror for array class %s is null", klass->external_name())) 1736 } 1737 1738 Klass* component_klass = java_lang_Class::as_Klass(component_mirror); 1739 if (component_klass != NULL) { 1740 JVMCIKlassHandle klass_handle(THREAD, component_klass); 1741 JVMCIObject result = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL); 1742 return JVMCIENV->get_jobject(result); 1743 } 1744 BasicType type = java_lang_Class::primitive_type(component_mirror); 1745 JVMCIObject result = JVMCIENV->get_jvmci_primitive_type(type); 1746 return JVMCIENV->get_jobject(result); 1747 C2V_END 1748 1749 C2V_VMENTRY(void, ensureInitialized, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1750 Klass* klass = UNPACK_PAIR(Klass, klass); 1751 if (klass == nullptr) { 1752 JVMCI_THROW(NullPointerException); 1753 } 1754 if (klass->should_be_initialized()) { 1755 InstanceKlass* k = InstanceKlass::cast(klass); 1756 k->initialize(CHECK); 1757 } 1758 C2V_END 1759 1760 C2V_VMENTRY(void, ensureLinked, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1761 Klass* klass = UNPACK_PAIR(Klass, klass); 1762 if (klass == nullptr) { 1763 JVMCI_THROW(NullPointerException); 1764 } 1765 if (klass->is_instance_klass()) { 1766 InstanceKlass* k = InstanceKlass::cast(klass); 1767 k->link_class(CHECK); 1768 } 1769 C2V_END 1770 1771 C2V_VMENTRY_0(jint, interpreterFrameSize, (JNIEnv* env, jobject, jobject bytecode_frame_handle)) 1772 if (bytecode_frame_handle == NULL) { 1773 JVMCI_THROW_0(NullPointerException); 1774 } 1775 1776 JVMCIObject top_bytecode_frame = JVMCIENV->wrap(bytecode_frame_handle); 1777 JVMCIObject bytecode_frame = top_bytecode_frame; 1778 int size = 0; 1779 int callee_parameters = 0; 1780 int callee_locals = 0; 1781 Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame)); 1782 int extra_args = method->max_stack() - JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame); 1783 1784 while (bytecode_frame.is_non_null()) { 1785 int locks = JVMCIENV->get_BytecodeFrame_numLocks(bytecode_frame); 1786 int temps = JVMCIENV->get_BytecodeFrame_numStack(bytecode_frame); 1787 bool is_top_frame = (JVMCIENV->equals(bytecode_frame, top_bytecode_frame)); 1788 Method* method = JVMCIENV->asMethod(JVMCIENV->get_BytecodePosition_method(bytecode_frame)); 1789 1790 int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(), 1791 temps + callee_parameters, 1792 extra_args, 1793 locks, 1794 callee_parameters, 1795 callee_locals, 1796 is_top_frame); 1797 size += frame_size; 1798 1799 callee_parameters = method->size_of_parameters(); 1800 callee_locals = method->max_locals(); 1801 extra_args = 0; 1802 bytecode_frame = JVMCIENV->get_BytecodePosition_caller(bytecode_frame); 1803 } 1804 return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord; 1805 C2V_END 1806 1807 C2V_VMENTRY(void, compileToBytecode, (JNIEnv* env, jobject, jobject lambda_form_handle)) 1808 Handle lambda_form = JVMCIENV->asConstant(JVMCIENV->wrap(lambda_form_handle), JVMCI_CHECK); 1809 if (lambda_form->is_a(vmClasses::LambdaForm_klass())) { 1810 TempNewSymbol compileToBytecode = SymbolTable::new_symbol("compileToBytecode"); 1811 JavaValue result(T_VOID); 1812 JavaCalls::call_special(&result, lambda_form, vmClasses::LambdaForm_klass(), compileToBytecode, vmSymbols::void_method_signature(), CHECK); 1813 } else { 1814 JVMCI_THROW_MSG(IllegalArgumentException, 1815 err_msg("Unexpected type: %s", lambda_form->klass()->external_name())) 1816 } 1817 C2V_END 1818 1819 C2V_VMENTRY_0(jint, getIdentityHashCode, (JNIEnv* env, jobject, jobject object)) 1820 Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0); 1821 return obj->identity_hash(); 1822 C2V_END 1823 1824 C2V_VMENTRY_0(jboolean, isInternedString, (JNIEnv* env, jobject, jobject object)) 1825 Handle str = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0); 1826 if (!java_lang_String::is_instance(str())) { 1827 return false; 1828 } 1829 int len; 1830 jchar* name = java_lang_String::as_unicode_string(str(), len, CHECK_false); 1831 return (StringTable::lookup(name, len) != NULL); 1832 C2V_END 1833 1834 1835 C2V_VMENTRY_NULL(jobject, unboxPrimitive, (JNIEnv* env, jobject, jobject object)) 1836 if (object == NULL) { 1837 JVMCI_THROW_0(NullPointerException); 1838 } 1839 Handle box = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL); 1840 BasicType type = java_lang_boxing_object::basic_type(box()); 1841 jvalue result; 1842 if (java_lang_boxing_object::get_value(box(), &result) == T_ILLEGAL) { 1843 return NULL; 1844 } 1845 JVMCIObject boxResult = JVMCIENV->create_box(type, &result, JVMCI_CHECK_NULL); 1846 return JVMCIENV->get_jobject(boxResult); 1847 C2V_END 1848 1849 C2V_VMENTRY_NULL(jobject, boxPrimitive, (JNIEnv* env, jobject, jobject object)) 1850 if (object == NULL) { 1851 JVMCI_THROW_0(NullPointerException); 1852 } 1853 JVMCIObject box = JVMCIENV->wrap(object); 1854 BasicType type = JVMCIENV->get_box_type(box); 1855 if (type == T_ILLEGAL) { 1856 return NULL; 1857 } 1858 jvalue value = JVMCIENV->get_boxed_value(type, box); 1859 JavaValue box_result(T_OBJECT); 1860 JavaCallArguments jargs; 1861 Klass* box_klass = NULL; 1862 Symbol* box_signature = NULL; 1863 #define BOX_CASE(bt, v, argtype, name) \ 1864 case bt: \ 1865 jargs.push_##argtype(value.v); \ 1866 box_klass = vmClasses::name##_klass(); \ 1867 box_signature = vmSymbols::name##_valueOf_signature(); \ 1868 break 1869 1870 switch (type) { 1871 BOX_CASE(T_BOOLEAN, z, int, Boolean); 1872 BOX_CASE(T_BYTE, b, int, Byte); 1873 BOX_CASE(T_CHAR, c, int, Character); 1874 BOX_CASE(T_SHORT, s, int, Short); 1875 BOX_CASE(T_INT, i, int, Integer); 1876 BOX_CASE(T_LONG, j, long, Long); 1877 BOX_CASE(T_FLOAT, f, float, Float); 1878 BOX_CASE(T_DOUBLE, d, double, Double); 1879 default: 1880 ShouldNotReachHere(); 1881 } 1882 #undef BOX_CASE 1883 1884 JavaCalls::call_static(&box_result, 1885 box_klass, 1886 vmSymbols::valueOf_name(), 1887 box_signature, &jargs, CHECK_NULL); 1888 oop hotspot_box = box_result.get_oop(); 1889 JVMCIObject result = JVMCIENV->get_object_constant(hotspot_box, false); 1890 return JVMCIENV->get_jobject(result); 1891 C2V_END 1892 1893 C2V_VMENTRY_NULL(jobjectArray, getDeclaredConstructors, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1894 Klass* klass = UNPACK_PAIR(Klass, klass); 1895 if (klass == nullptr) { 1896 JVMCI_THROW_0(NullPointerException); 1897 } 1898 if (!klass->is_instance_klass()) { 1899 JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL); 1900 return JVMCIENV->get_jobjectArray(methods); 1901 } 1902 1903 InstanceKlass* iklass = InstanceKlass::cast(klass); 1904 GrowableArray<Method*> constructors_array; 1905 for (int i = 0; i < iklass->methods()->length(); i++) { 1906 Method* m = iklass->methods()->at(i); 1907 if (m->is_object_constructor()) { 1908 constructors_array.append(m); 1909 } 1910 } 1911 JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(constructors_array.length(), JVMCI_CHECK_NULL); 1912 for (int i = 0; i < constructors_array.length(); i++) { 1913 methodHandle ctor(THREAD, constructors_array.at(i)); 1914 JVMCIObject method = JVMCIENV->get_jvmci_method(ctor, JVMCI_CHECK_NULL); 1915 JVMCIENV->put_object_at(methods, i, method); 1916 } 1917 return JVMCIENV->get_jobjectArray(methods); 1918 C2V_END 1919 1920 C2V_VMENTRY_NULL(jobjectArray, getDeclaredMethods, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 1921 Klass* klass = UNPACK_PAIR(Klass, klass); 1922 if (klass == nullptr) { 1923 JVMCI_THROW_0(NullPointerException); 1924 } 1925 if (!klass->is_instance_klass()) { 1926 JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(0, JVMCI_CHECK_NULL); 1927 return JVMCIENV->get_jobjectArray(methods); 1928 } 1929 1930 InstanceKlass* iklass = InstanceKlass::cast(klass); 1931 GrowableArray<Method*> methods_array; 1932 for (int i = 0; i < iklass->methods()->length(); i++) { 1933 Method* m = iklass->methods()->at(i); 1934 if (!(m->is_object_constructor() || m->is_class_initializer()) && !m->is_overpass()) { 1935 methods_array.append(m); 1936 } 1937 } 1938 JVMCIObjectArray methods = JVMCIENV->new_ResolvedJavaMethod_array(methods_array.length(), JVMCI_CHECK_NULL); 1939 for (int i = 0; i < methods_array.length(); i++) { 1940 methodHandle mh(THREAD, methods_array.at(i)); 1941 JVMCIObject method = JVMCIENV->get_jvmci_method(mh, JVMCI_CHECK_NULL); 1942 JVMCIENV->put_object_at(methods, i, method); 1943 } 1944 return JVMCIENV->get_jobjectArray(methods); 1945 C2V_END 1946 1947 static jobject read_field_value(Handle obj, long displacement, jchar type_char, bool is_static, Thread* THREAD, JVMCIEnv* JVMCIENV) { 1948 1949 BasicType basic_type = JVMCIENV->typeCharToBasicType(type_char, JVMCI_CHECK_NULL); 1950 int basic_type_elemsize = type2aelembytes(basic_type); 1951 if (displacement < 0 || ((size_t) displacement + basic_type_elemsize > HeapWordSize * obj->size())) { 1952 // Reading outside of the object bounds 1953 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading outside object bounds"); 1954 } 1955 1956 // Perform basic sanity checks on the read. Primitive reads are permitted to read outside the 1957 // bounds of their fields but object reads must map exactly onto the underlying oop slot. 1958 bool aligned = (displacement % basic_type_elemsize) == 0; 1959 if (!aligned) { 1960 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "read is unaligned"); 1961 } 1962 if (basic_type == T_OBJECT) { 1963 if (obj->is_objArray()) { 1964 if (displacement < arrayOopDesc::base_offset_in_bytes(T_OBJECT)) { 1965 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header"); 1966 } 1967 if (displacement + heapOopSize > arrayOopDesc::base_offset_in_bytes(T_OBJECT) + arrayOop(obj())->length() * heapOopSize) { 1968 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading after last array element"); 1969 } 1970 if (((displacement - arrayOopDesc::base_offset_in_bytes(T_OBJECT)) % heapOopSize) != 0) { 1971 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "misaligned object read from array"); 1972 } 1973 } else if (obj->is_instance()) { 1974 InstanceKlass* klass = InstanceKlass::cast(is_static ? java_lang_Class::as_Klass(obj()) : obj->klass()); 1975 fieldDescriptor fd; 1976 if (!klass->find_field_from_offset(displacement, is_static, &fd)) { 1977 JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Can't find field at displacement %d in object of type %s", (int) displacement, klass->external_name())); 1978 } 1979 if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) { 1980 JVMCI_THROW_MSG_NULL(IllegalArgumentException, err_msg("Field at displacement %d in object of type %s is %s but expected %s", (int) displacement, 1981 klass->external_name(), type2name(fd.field_type()), type2name(basic_type))); 1982 } 1983 } else if (obj->is_typeArray()) { 1984 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Can't read objects from primitive array"); 1985 } else { 1986 ShouldNotReachHere(); 1987 } 1988 } else { 1989 if (obj->is_objArray()) { 1990 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "Reading primitive from object array"); 1991 } else if (obj->is_typeArray()) { 1992 if (displacement < arrayOopDesc::base_offset_in_bytes(ArrayKlass::cast(obj->klass())->element_type())) { 1993 JVMCI_THROW_MSG_NULL(IllegalArgumentException, "reading from array header"); 1994 } 1995 } 1996 } 1997 1998 jlong value = 0; 1999 2000 // Treat all reads as volatile for simplicity as this function can be used 2001 // both for reading Java fields declared as volatile as well as for constant 2002 // folding Unsafe.get* methods with volatile semantics. 2003 2004 switch (basic_type) { 2005 case T_BOOLEAN: value = obj->bool_field_acquire(displacement); break; 2006 case T_BYTE: value = obj->byte_field_acquire(displacement); break; 2007 case T_SHORT: value = obj->short_field_acquire(displacement); break; 2008 case T_CHAR: value = obj->char_field_acquire(displacement); break; 2009 case T_FLOAT: 2010 case T_INT: value = obj->int_field_acquire(displacement); break; 2011 case T_DOUBLE: 2012 case T_LONG: value = obj->long_field_acquire(displacement); break; 2013 2014 case T_OBJECT: { 2015 if (displacement == java_lang_Class::component_mirror_offset() && java_lang_Class::is_instance(obj()) && 2016 (java_lang_Class::as_Klass(obj()) == nullptr || !java_lang_Class::as_Klass(obj())->is_array_klass())) { 2017 // Class.componentType for non-array classes can transiently contain an int[] that's 2018 // used for locking so always return null to mimic Class.getComponentType() 2019 return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER()); 2020 } 2021 2022 oop value = obj->obj_field_acquire(displacement); 2023 2024 if (value == nullptr) { 2025 return JVMCIENV->get_jobject(JVMCIENV->get_JavaConstant_NULL_POINTER()); 2026 } else { 2027 if (value != nullptr && !oopDesc::is_oop(value)) { 2028 // Throw an exception to improve debuggability. This check isn't totally reliable because 2029 // is_oop doesn't try to be completety safe but for most invalid values it provides a good 2030 // enough answer. It possible to crash in the is_oop call but that just means the crash happens 2031 // closer to where things went wrong. 2032 JVMCI_THROW_MSG_NULL(InternalError, err_msg("Read bad oop " INTPTR_FORMAT " at offset " JLONG_FORMAT " in object " INTPTR_FORMAT " of type %s", 2033 p2i(value), displacement, p2i(obj()), obj->klass()->external_name())); 2034 } 2035 2036 JVMCIObject result = JVMCIENV->get_object_constant(value); 2037 return JVMCIENV->get_jobject(result); 2038 } 2039 } 2040 2041 default: 2042 ShouldNotReachHere(); 2043 } 2044 JVMCIObject result = JVMCIENV->call_JavaConstant_forPrimitive(type_char, value, JVMCI_CHECK_NULL); 2045 return JVMCIENV->get_jobject(result); 2046 } 2047 2048 C2V_VMENTRY_NULL(jobject, readStaticFieldValue, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), long displacement, jchar type_char)) 2049 Klass* klass = UNPACK_PAIR(Klass, klass); 2050 Handle obj(THREAD, klass->java_mirror()); 2051 return read_field_value(obj, displacement, type_char, true, THREAD, JVMCIENV); 2052 C2V_END 2053 2054 C2V_VMENTRY_NULL(jobject, readFieldValue, (JNIEnv* env, jobject, jobject object, ARGUMENT_PAIR(expected_type), long displacement, jchar type_char)) 2055 if (object == nullptr) { 2056 JVMCI_THROW_0(NullPointerException); 2057 } 2058 2059 // asConstant will throw an NPE if a constant contains NULL 2060 Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL); 2061 2062 Klass* expected_klass = UNPACK_PAIR(Klass, expected_type); 2063 if (expected_klass != nullptr) { 2064 InstanceKlass* expected_iklass = InstanceKlass::cast(expected_klass); 2065 if (!obj->is_a(expected_iklass)) { 2066 // Not of the expected type 2067 return nullptr; 2068 } 2069 } 2070 bool is_static = expected_klass == nullptr && java_lang_Class::is_instance(obj()) && displacement >= InstanceMirrorKlass::offset_of_static_fields(); 2071 return read_field_value(obj, displacement, type_char, is_static, THREAD, JVMCIENV); 2072 C2V_END 2073 2074 C2V_VMENTRY_0(jboolean, isInstance, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), jobject object)) 2075 Klass* klass = UNPACK_PAIR(Klass, klass); 2076 if (object == NULL || klass == nullptr) { 2077 JVMCI_THROW_0(NullPointerException); 2078 } 2079 Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_0); 2080 return obj->is_a(klass); 2081 C2V_END 2082 2083 C2V_VMENTRY_0(jboolean, isAssignableFrom, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), ARGUMENT_PAIR(subklass))) 2084 Klass* klass = UNPACK_PAIR(Klass, klass); 2085 Klass* subklass = UNPACK_PAIR(Klass, subklass); 2086 if (klass == nullptr || subklass == nullptr) { 2087 JVMCI_THROW_0(NullPointerException); 2088 } 2089 return subklass->is_subtype_of(klass); 2090 C2V_END 2091 2092 C2V_VMENTRY_0(jboolean, isTrustedForIntrinsics, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 2093 Klass* klass = UNPACK_PAIR(Klass, klass); 2094 if (klass == nullptr) { 2095 JVMCI_THROW_0(NullPointerException); 2096 } 2097 InstanceKlass* ik = InstanceKlass::cast(klass); 2098 if (ik->class_loader_data()->is_boot_class_loader_data() || ik->class_loader_data()->is_platform_class_loader_data()) { 2099 return true; 2100 } 2101 return false; 2102 C2V_END 2103 2104 C2V_VMENTRY_NULL(jobject, asJavaType, (JNIEnv* env, jobject, jobject object)) 2105 if (object == NULL) { 2106 JVMCI_THROW_0(NullPointerException); 2107 } 2108 Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL); 2109 if (java_lang_Class::is_instance(obj())) { 2110 if (java_lang_Class::is_primitive(obj())) { 2111 JVMCIObject type = JVMCIENV->get_jvmci_primitive_type(java_lang_Class::primitive_type(obj())); 2112 return JVMCIENV->get_jobject(type); 2113 } 2114 Klass* klass = java_lang_Class::as_Klass(obj()); 2115 JVMCIKlassHandle klass_handle(THREAD); 2116 klass_handle = klass; 2117 JVMCIObject type = JVMCIENV->get_jvmci_type(klass_handle, JVMCI_CHECK_NULL); 2118 return JVMCIENV->get_jobject(type); 2119 } 2120 return NULL; 2121 C2V_END 2122 2123 2124 C2V_VMENTRY_NULL(jobject, asString, (JNIEnv* env, jobject, jobject object)) 2125 if (object == NULL) { 2126 JVMCI_THROW_0(NullPointerException); 2127 } 2128 Handle obj = JVMCIENV->asConstant(JVMCIENV->wrap(object), JVMCI_CHECK_NULL); 2129 const char* str = java_lang_String::as_utf8_string(obj()); 2130 JVMCIObject result = JVMCIENV->create_string(str, JVMCI_CHECK_NULL); 2131 return JVMCIENV->get_jobject(result); 2132 C2V_END 2133 2134 2135 C2V_VMENTRY_0(jboolean, equals, (JNIEnv* env, jobject, jobject x, jlong xHandle, jobject y, jlong yHandle)) 2136 if (x == NULL || y == NULL) { 2137 JVMCI_THROW_0(NullPointerException); 2138 } 2139 return JVMCIENV->resolve_oop_handle(xHandle) == JVMCIENV->resolve_oop_handle(yHandle); 2140 C2V_END 2141 2142 C2V_VMENTRY_NULL(jobject, getJavaMirror, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass))) 2143 Klass* klass = UNPACK_PAIR(Klass, klass); 2144 if (klass == nullptr) { 2145 JVMCI_THROW_0(NullPointerException); 2146 } 2147 Handle mirror(THREAD, klass->java_mirror()); 2148 JVMCIObject result = JVMCIENV->get_object_constant(mirror()); 2149 return JVMCIENV->get_jobject(result); 2150 C2V_END 2151 2152 2153 C2V_VMENTRY_0(jint, getArrayLength, (JNIEnv* env, jobject, jobject x)) 2154 if (x == NULL) { 2155 JVMCI_THROW_0(NullPointerException); 2156 } 2157 Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_0); 2158 if (xobj->klass()->is_array_klass()) { 2159 return arrayOop(xobj())->length(); 2160 } 2161 return -1; 2162 C2V_END 2163 2164 2165 C2V_VMENTRY_NULL(jobject, readArrayElement, (JNIEnv* env, jobject, jobject x, int index)) 2166 if (x == NULL) { 2167 JVMCI_THROW_0(NullPointerException); 2168 } 2169 Handle xobj = JVMCIENV->asConstant(JVMCIENV->wrap(x), JVMCI_CHECK_NULL); 2170 if (xobj->klass()->is_array_klass()) { 2171 arrayOop array = arrayOop(xobj()); 2172 BasicType element_type = ArrayKlass::cast(array->klass())->element_type(); 2173 if (index < 0 || index >= array->length()) { 2174 return NULL; 2175 } 2176 JVMCIObject result; 2177 2178 if (element_type == T_OBJECT) { 2179 result = JVMCIENV->get_object_constant(objArrayOop(xobj())->obj_at(index)); 2180 if (result.is_null()) { 2181 result = JVMCIENV->get_JavaConstant_NULL_POINTER(); 2182 } 2183 } else { 2184 jvalue value; 2185 switch (element_type) { 2186 case T_DOUBLE: value.d = typeArrayOop(xobj())->double_at(index); break; 2187 case T_FLOAT: value.f = typeArrayOop(xobj())->float_at(index); break; 2188 case T_LONG: value.j = typeArrayOop(xobj())->long_at(index); break; 2189 case T_INT: value.i = typeArrayOop(xobj())->int_at(index); break; 2190 case T_SHORT: value.s = typeArrayOop(xobj())->short_at(index); break; 2191 case T_CHAR: value.c = typeArrayOop(xobj())->char_at(index); break; 2192 case T_BYTE: value.b = typeArrayOop(xobj())->byte_at(index); break; 2193 case T_BOOLEAN: value.z = typeArrayOop(xobj())->byte_at(index) & 1; break; 2194 default: ShouldNotReachHere(); 2195 } 2196 result = JVMCIENV->create_box(element_type, &value, JVMCI_CHECK_NULL); 2197 } 2198 assert(!result.is_null(), "must have a value"); 2199 return JVMCIENV->get_jobject(result); 2200 } 2201 return NULL;; 2202 C2V_END 2203 2204 2205 C2V_VMENTRY_0(jint, arrayBaseOffset, (JNIEnv* env, jobject, jchar type_char)) 2206 BasicType type = JVMCIENV->typeCharToBasicType(type_char, JVMCI_CHECK_0); 2207 return arrayOopDesc::header_size(type) * HeapWordSize; 2208 C2V_END 2209 2210 C2V_VMENTRY_0(jint, arrayIndexScale, (JNIEnv* env, jobject, jchar type_char)) 2211 BasicType type = JVMCIENV->typeCharToBasicType(type_char, JVMCI_CHECK_0); 2212 return type2aelembytes(type); 2213 C2V_END 2214 2215 C2V_VMENTRY(void, releaseClearedOopHandles, (JNIEnv* env, jobject)) 2216 JVMCIENV->runtime()->release_cleared_oop_handles(); 2217 C2V_END 2218 2219 static void requireJVMCINativeLibrary(JVMCI_TRAPS) { 2220 if (!UseJVMCINativeLibrary) { 2221 JVMCI_THROW_MSG(UnsupportedOperationException, "JVMCI shared library is not enabled (requires -XX:+UseJVMCINativeLibrary)"); 2222 } 2223 } 2224 2225 C2V_VMENTRY_NULL(jlongArray, registerNativeMethods, (JNIEnv* env, jobject, jclass mirror)) 2226 requireJVMCINativeLibrary(JVMCI_CHECK_NULL); 2227 requireInHotSpot("registerNativeMethods", JVMCI_CHECK_NULL); 2228 char* sl_path; 2229 void* sl_handle; 2230 JVMCIRuntime* runtime; 2231 { 2232 // Ensure the JVMCI shared library runtime is initialized. 2233 JVMCIEnv __peer_jvmci_env__(thread, false, __FILE__, __LINE__); 2234 JVMCIEnv* peerEnv = &__peer_jvmci_env__; 2235 HandleMark hm(THREAD); 2236 runtime = JVMCI::compiler_runtime(thread); 2237 if (peerEnv->has_pending_exception()) { 2238 peerEnv->describe_pending_exception(true); 2239 } 2240 sl_handle = JVMCI::get_shared_library(sl_path, false); 2241 if (sl_handle == NULL) { 2242 JVMCI_THROW_MSG_0(InternalError, err_msg("Error initializing JVMCI runtime %d", runtime->id())); 2243 } 2244 } 2245 2246 if (mirror == NULL) { 2247 JVMCI_THROW_0(NullPointerException); 2248 } 2249 Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve(mirror)); 2250 if (klass == NULL || !klass->is_instance_klass()) { 2251 JVMCI_THROW_MSG_0(IllegalArgumentException, "clazz is for primitive type"); 2252 } 2253 2254 InstanceKlass* iklass = InstanceKlass::cast(klass); 2255 for (int i = 0; i < iklass->methods()->length(); i++) { 2256 methodHandle method(THREAD, iklass->methods()->at(i)); 2257 if (method->is_native()) { 2258 2259 // Compute argument size 2260 int args_size = 1 // JNIEnv 2261 + (method->is_static() ? 1 : 0) // class for static methods 2262 + method->size_of_parameters(); // actual parameters 2263 2264 // 1) Try JNI short style 2265 stringStream st; 2266 char* pure_name = NativeLookup::pure_jni_name(method); 2267 guarantee(pure_name != NULL, "Illegal native method name encountered"); 2268 os::print_jni_name_prefix_on(&st, args_size); 2269 st.print_raw(pure_name); 2270 os::print_jni_name_suffix_on(&st, args_size); 2271 char* jni_name = st.as_string(); 2272 2273 address entry = (address) os::dll_lookup(sl_handle, jni_name); 2274 if (entry == NULL) { 2275 // 2) Try JNI long style 2276 st.reset(); 2277 char* long_name = NativeLookup::long_jni_name(method); 2278 guarantee(long_name != NULL, "Illegal native method name encountered"); 2279 os::print_jni_name_prefix_on(&st, args_size); 2280 st.print_raw(pure_name); 2281 st.print_raw(long_name); 2282 os::print_jni_name_suffix_on(&st, args_size); 2283 char* jni_long_name = st.as_string(); 2284 entry = (address) os::dll_lookup(sl_handle, jni_long_name); 2285 if (entry == NULL) { 2286 JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [neither %s nor %s exist in %s]", 2287 method->name_and_sig_as_C_string(), 2288 jni_name, jni_long_name, sl_path)); 2289 } 2290 } 2291 2292 if (method->has_native_function() && entry != method->native_function()) { 2293 JVMCI_THROW_MSG_0(UnsatisfiedLinkError, err_msg("%s [cannot re-link from " PTR_FORMAT " to " PTR_FORMAT "]", 2294 method->name_and_sig_as_C_string(), p2i(method->native_function()), p2i(entry))); 2295 } 2296 method->set_native_function(entry, Method::native_bind_event_is_interesting); 2297 log_debug(jni, resolve)("[Dynamic-linking native method %s.%s ... JNI] @ " PTR_FORMAT, 2298 method->method_holder()->external_name(), 2299 method->name()->as_C_string(), 2300 p2i((void*) entry)); 2301 } 2302 } 2303 2304 typeArrayOop info_oop = oopFactory::new_longArray(4, CHECK_0); 2305 jlongArray info = (jlongArray) JNIHandles::make_local(THREAD, info_oop); 2306 runtime->init_JavaVM_info(info, JVMCI_CHECK_0); 2307 return info; 2308 } 2309 2310 C2V_VMENTRY_PREFIX(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject c2vm)) 2311 if (thread == nullptr || thread->libjvmci_runtime() == nullptr) { 2312 // Called from unattached JVMCI shared library thread 2313 return false; 2314 } 2315 if (thread->jni_environment() == env) { 2316 C2V_BLOCK(jboolean, isCurrentThreadAttached, (JNIEnv* env, jobject)) 2317 JVMCITraceMark jtm("isCurrentThreadAttached"); 2318 requireJVMCINativeLibrary(JVMCI_CHECK_0); 2319 JVMCIRuntime* runtime = thread->libjvmci_runtime(); 2320 if (runtime == nullptr || !runtime->has_shared_library_javavm()) { 2321 JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in isCurrentThreadAttached"); 2322 } 2323 JNIEnv* peerEnv; 2324 return runtime->GetEnv(thread, (void**) &peerEnv, JNI_VERSION_1_2) == JNI_OK; 2325 } 2326 return true; 2327 C2V_END 2328 2329 C2V_VMENTRY_PREFIX(jlong, getCurrentJavaThread, (JNIEnv* env, jobject c2vm)) 2330 if (thread == NULL) { 2331 // Called from unattached JVMCI shared library thread 2332 return 0L; 2333 } 2334 return (jlong) p2i(thread); 2335 C2V_END 2336 2337 // Attaches a thread started in a JVMCI shared library to a JavaThread and JVMCI runtime. 2338 static void attachSharedLibraryThread(JNIEnv* env, jbyteArray name, jboolean as_daemon) { 2339 JavaVM* javaVM = nullptr; 2340 jint res = env->GetJavaVM(&javaVM); 2341 if (res != JNI_OK) { 2342 JNI_THROW("attachSharedLibraryThread", InternalError, err_msg("Error getting shared library JavaVM from shared library JNIEnv: %d", res)); 2343 } 2344 extern struct JavaVM_ main_vm; 2345 JNIEnv* hotspotEnv; 2346 2347 int name_len = env->GetArrayLength(name); 2348 char name_buf[64]; // Cannot use Resource heap as it requires a current thread 2349 int to_copy = MIN2(name_len, (int) sizeof(name_buf) - 1); 2350 env->GetByteArrayRegion(name, 0, to_copy, (jbyte*) name_buf); 2351 name_buf[to_copy] = '\0'; 2352 JavaVMAttachArgs attach_args; 2353 attach_args.version = JNI_VERSION_1_2; 2354 attach_args.name = name_buf; 2355 attach_args.group = nullptr; 2356 res = as_daemon ? main_vm.AttachCurrentThreadAsDaemon((void**)&hotspotEnv, &attach_args) : 2357 main_vm.AttachCurrentThread((void**)&hotspotEnv, &attach_args); 2358 if (res != JNI_OK) { 2359 JNI_THROW("attachSharedLibraryThread", InternalError, err_msg("Trying to attach thread returned %d", res)); 2360 } 2361 JavaThread* thread = get_current_thread(false); 2362 const char* attach_error; 2363 { 2364 // Transition to VM 2365 JVMCI_VM_ENTRY_MARK 2366 attach_error = JVMCIRuntime::attach_shared_library_thread(thread, javaVM); 2367 // Transition back to Native 2368 } 2369 if (attach_error != nullptr) { 2370 JNI_THROW("attachCurrentThread", InternalError, attach_error); 2371 } 2372 } 2373 2374 C2V_VMENTRY_PREFIX(jboolean, attachCurrentThread, (JNIEnv* env, jobject c2vm, jbyteArray name, jboolean as_daemon, jlongArray javaVM_info)) 2375 if (thread == nullptr) { 2376 attachSharedLibraryThread(env, name, as_daemon); 2377 return true; 2378 } 2379 if (thread->jni_environment() == env) { 2380 // Called from HotSpot 2381 C2V_BLOCK(jboolean, attachCurrentThread, (JNIEnv* env, jobject, jboolean)) 2382 JVMCITraceMark jtm("attachCurrentThread"); 2383 requireJVMCINativeLibrary(JVMCI_CHECK_0); 2384 2385 JVMCIRuntime* runtime = JVMCI::compiler_runtime(thread); 2386 JNIEnv* peerJNIEnv; 2387 if (runtime->has_shared_library_javavm()) { 2388 if (runtime->GetEnv(thread, (void**)&peerJNIEnv, JNI_VERSION_1_2) == JNI_OK) { 2389 // Already attached 2390 runtime->init_JavaVM_info(javaVM_info, JVMCI_CHECK_0); 2391 return false; 2392 } 2393 } 2394 2395 { 2396 // Ensure the JVMCI shared library runtime is initialized. 2397 JVMCIEnv __peer_jvmci_env__(thread, false, __FILE__, __LINE__); 2398 JVMCIEnv* peerJVMCIEnv = &__peer_jvmci_env__; 2399 HandleMark hm(thread); 2400 JVMCIObject receiver = runtime->get_HotSpotJVMCIRuntime(peerJVMCIEnv); 2401 if (peerJVMCIEnv->has_pending_exception()) { 2402 peerJVMCIEnv->describe_pending_exception(true); 2403 } 2404 char* sl_path; 2405 if (JVMCI::get_shared_library(sl_path, false) == nullptr) { 2406 JVMCI_THROW_MSG_0(InternalError, "Error initializing JVMCI runtime"); 2407 } 2408 } 2409 2410 JavaVMAttachArgs attach_args; 2411 attach_args.version = JNI_VERSION_1_2; 2412 attach_args.name = const_cast<char*>(thread->name()); 2413 attach_args.group = nullptr; 2414 if (runtime->GetEnv(thread, (void**) &peerJNIEnv, JNI_VERSION_1_2) == JNI_OK) { 2415 return false; 2416 } 2417 jint res = as_daemon ? runtime->AttachCurrentThreadAsDaemon(thread, (void**) &peerJNIEnv, &attach_args) : 2418 runtime->AttachCurrentThread(thread, (void**) &peerJNIEnv, &attach_args); 2419 2420 if (res == JNI_OK) { 2421 guarantee(peerJNIEnv != nullptr, "must be"); 2422 runtime->init_JavaVM_info(javaVM_info, JVMCI_CHECK_0); 2423 JVMCI_event_1("attached to JavaVM[%d] for JVMCI runtime %d", runtime->get_shared_library_javavm_id(), runtime->id()); 2424 return true; 2425 } 2426 JVMCI_THROW_MSG_0(InternalError, err_msg("Error %d while attaching %s", res, attach_args.name)); 2427 } 2428 // Called from JVMCI shared library 2429 return false; 2430 C2V_END 2431 2432 C2V_VMENTRY_PREFIX(jboolean, detachCurrentThread, (JNIEnv* env, jobject c2vm, jboolean release)) 2433 if (thread == nullptr) { 2434 // Called from unattached JVMCI shared library thread 2435 JNI_THROW_("detachCurrentThread", IllegalStateException, "Cannot detach non-attached thread", false); 2436 } 2437 if (thread->jni_environment() == env) { 2438 // Called from HotSpot 2439 C2V_BLOCK(void, detachCurrentThread, (JNIEnv* env, jobject)) 2440 JVMCITraceMark jtm("detachCurrentThread"); 2441 requireJVMCINativeLibrary(JVMCI_CHECK_0); 2442 requireInHotSpot("detachCurrentThread", JVMCI_CHECK_0); 2443 JVMCIRuntime* runtime = thread->libjvmci_runtime(); 2444 if (runtime == nullptr || !runtime->has_shared_library_javavm()) { 2445 JVMCI_THROW_MSG_0(IllegalStateException, "Require JVMCI shared library JavaVM to be initialized in detachCurrentThread"); 2446 } 2447 JNIEnv* peerEnv; 2448 2449 if (runtime->GetEnv(thread, (void**) &peerEnv, JNI_VERSION_1_2) != JNI_OK) { 2450 JVMCI_THROW_MSG_0(IllegalStateException, err_msg("Cannot detach non-attached thread: %s", thread->name())); 2451 } 2452 jint res = runtime->DetachCurrentThread(thread); 2453 if (res != JNI_OK) { 2454 JVMCI_THROW_MSG_0(InternalError, err_msg("Error %d while attaching %s", res, thread->name())); 2455 } 2456 JVMCI_event_1("detached from JavaVM[%d] for JVMCI runtime %d", 2457 runtime->get_shared_library_javavm_id(), runtime->id()); 2458 if (release) { 2459 return runtime->detach_thread(thread, "user thread detach"); 2460 } 2461 } else { 2462 // Called from attached JVMCI shared library thread 2463 if (release) { 2464 JNI_THROW_("detachCurrentThread", InternalError, "JVMCI shared library thread cannot release JVMCI shared library JavaVM", false); 2465 } 2466 JVMCIRuntime* runtime = thread->libjvmci_runtime(); 2467 if (runtime == nullptr) { 2468 JNI_THROW_("detachCurrentThread", InternalError, "JVMCI shared library thread should have a JVMCI runtime", false); 2469 } 2470 { 2471 // Transition to VM 2472 C2V_BLOCK(jboolean, detachCurrentThread, (JNIEnv* env, jobject)) 2473 // Cannot destroy shared library JavaVM as we're about to return to it. 2474 runtime->detach_thread(thread, "shared library thread detach", false); 2475 JVMCI_event_1("detaching JVMCI shared library thread from HotSpot JavaVM"); 2476 // Transition back to Native 2477 } 2478 extern struct JavaVM_ main_vm; 2479 jint res = main_vm.DetachCurrentThread(); 2480 if (res != JNI_OK) { 2481 JNI_THROW_("detachCurrentThread", InternalError, "Cannot detach non-attached thread", false); 2482 } 2483 } 2484 return false; 2485 C2V_END 2486 2487 C2V_VMENTRY_0(jlong, translate, (JNIEnv* env, jobject, jobject obj_handle, jboolean callPostTranslation)) 2488 requireJVMCINativeLibrary(JVMCI_CHECK_0); 2489 if (obj_handle == NULL) { 2490 return 0L; 2491 } 2492 JVMCIEnv __peer_jvmci_env__(thread, !JVMCIENV->is_hotspot(), __FILE__, __LINE__); 2493 JVMCIEnv* peerEnv = &__peer_jvmci_env__; 2494 JVMCIEnv* thisEnv = JVMCIENV; 2495 2496 JVMCIObject obj = thisEnv->wrap(obj_handle); 2497 JVMCIObject result; 2498 if (thisEnv->isa_HotSpotResolvedJavaMethodImpl(obj)) { 2499 methodHandle method(THREAD, thisEnv->asMethod(obj)); 2500 result = peerEnv->get_jvmci_method(method, JVMCI_CHECK_0); 2501 } else if (thisEnv->isa_HotSpotResolvedObjectTypeImpl(obj)) { 2502 Klass* klass = thisEnv->asKlass(obj); 2503 JVMCIKlassHandle klass_handle(THREAD); 2504 klass_handle = klass; 2505 result = peerEnv->get_jvmci_type(klass_handle, JVMCI_CHECK_0); 2506 } else if (thisEnv->isa_HotSpotResolvedPrimitiveType(obj)) { 2507 BasicType type = JVMCIENV->kindToBasicType(JVMCIENV->get_HotSpotResolvedPrimitiveType_kind(obj), JVMCI_CHECK_0); 2508 result = peerEnv->get_jvmci_primitive_type(type); 2509 } else if (thisEnv->isa_IndirectHotSpotObjectConstantImpl(obj) || 2510 thisEnv->isa_DirectHotSpotObjectConstantImpl(obj)) { 2511 Handle constant = thisEnv->asConstant(obj, JVMCI_CHECK_0); 2512 result = peerEnv->get_object_constant(constant()); 2513 } else if (thisEnv->isa_HotSpotNmethod(obj)) { 2514 if (peerEnv->is_hotspot()) { 2515 nmethod* nm = JVMCIENV->get_nmethod(obj); 2516 if (nm != NULL) { 2517 JVMCINMethodData* data = nm->jvmci_nmethod_data(); 2518 if (data != NULL) { 2519 // Only the mirror in the HotSpot heap is accessible 2520 // through JVMCINMethodData 2521 oop nmethod_mirror = data->get_nmethod_mirror(nm, /* phantom_ref */ true); 2522 if (nmethod_mirror != NULL) { 2523 result = HotSpotJVMCI::wrap(nmethod_mirror); 2524 } 2525 } 2526 } 2527 } 2528 2529 if (result.is_null()) { 2530 JVMCIObject methodObject = thisEnv->get_HotSpotNmethod_method(obj); 2531 methodHandle mh(THREAD, thisEnv->asMethod(methodObject)); 2532 jboolean isDefault = thisEnv->get_HotSpotNmethod_isDefault(obj); 2533 jlong compileIdSnapshot = thisEnv->get_HotSpotNmethod_compileIdSnapshot(obj); 2534 JVMCIObject name_string = thisEnv->get_InstalledCode_name(obj); 2535 const char* cstring = name_string.is_null() ? NULL : thisEnv->as_utf8_string(name_string); 2536 // Create a new HotSpotNmethod instance in the peer runtime 2537 result = peerEnv->new_HotSpotNmethod(mh, cstring, isDefault, compileIdSnapshot, JVMCI_CHECK_0); 2538 nmethod* nm = JVMCIENV->get_nmethod(obj); 2539 if (result.is_null()) { 2540 // exception occurred (e.g. OOME) creating a new HotSpotNmethod 2541 } else if (nm == NULL) { 2542 // nmethod must have been unloaded 2543 } else { 2544 // Link the new HotSpotNmethod to the nmethod 2545 peerEnv->initialize_installed_code(result, nm, JVMCI_CHECK_0); 2546 // Only HotSpotNmethod instances in the HotSpot heap are tracked directly by the runtime. 2547 if (peerEnv->is_hotspot()) { 2548 JVMCINMethodData* data = nm->jvmci_nmethod_data(); 2549 if (data == NULL) { 2550 JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot set HotSpotNmethod mirror for default nmethod"); 2551 } 2552 if (data->get_nmethod_mirror(nm, /* phantom_ref */ false) != NULL) { 2553 JVMCI_THROW_MSG_0(IllegalArgumentException, "Cannot overwrite existing HotSpotNmethod mirror for nmethod"); 2554 } 2555 oop nmethod_mirror = HotSpotJVMCI::resolve(result); 2556 data->set_nmethod_mirror(nm, nmethod_mirror); 2557 } 2558 } 2559 } 2560 } else { 2561 JVMCI_THROW_MSG_0(IllegalArgumentException, 2562 err_msg("Cannot translate object of type: %s", thisEnv->klass_name(obj))); 2563 } 2564 if (callPostTranslation) { 2565 peerEnv->call_HotSpotJVMCIRuntime_postTranslation(result, JVMCI_CHECK_0); 2566 } 2567 // Propagate any exception that occurred while creating the translated object 2568 if (peerEnv->transfer_pending_exception(thread, thisEnv)) { 2569 return 0L; 2570 } 2571 return (jlong) peerEnv->make_global(result).as_jobject(); 2572 } 2573 2574 C2V_VMENTRY_NULL(jobject, unhand, (JNIEnv* env, jobject, jlong obj_handle)) 2575 requireJVMCINativeLibrary(JVMCI_CHECK_NULL); 2576 if (obj_handle == 0L) { 2577 return NULL; 2578 } 2579 jobject global_handle = (jobject) obj_handle; 2580 JVMCIObject global_handle_obj = JVMCIENV->wrap(global_handle); 2581 jobject result = JVMCIENV->make_local(global_handle_obj).as_jobject(); 2582 2583 JVMCIENV->destroy_global(global_handle_obj); 2584 return result; 2585 } 2586 2587 C2V_VMENTRY(void, updateHotSpotNmethod, (JNIEnv* env, jobject, jobject code_handle)) 2588 JVMCIObject code = JVMCIENV->wrap(code_handle); 2589 // Execute this operation for the side effect of updating the InstalledCode state 2590 JVMCIENV->get_nmethod(code); 2591 } 2592 2593 C2V_VMENTRY_NULL(jbyteArray, getCode, (JNIEnv* env, jobject, jobject code_handle)) 2594 JVMCIObject code = JVMCIENV->wrap(code_handle); 2595 CodeBlob* cb = JVMCIENV->get_code_blob(code); 2596 if (cb == NULL) { 2597 return NULL; 2598 } 2599 // Make a resource copy of code before the allocation causes a safepoint 2600 int code_size = cb->code_size(); 2601 jbyte* code_bytes = NEW_RESOURCE_ARRAY(jbyte, code_size); 2602 memcpy(code_bytes, (jbyte*) cb->code_begin(), code_size); 2603 2604 JVMCIPrimitiveArray result = JVMCIENV->new_byteArray(code_size, JVMCI_CHECK_NULL); 2605 JVMCIENV->copy_bytes_from(code_bytes, result, 0, code_size); 2606 return JVMCIENV->get_jbyteArray(result); 2607 } 2608 2609 C2V_VMENTRY_NULL(jobject, asReflectionExecutable, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 2610 requireInHotSpot("asReflectionExecutable", JVMCI_CHECK_NULL); 2611 methodHandle m(THREAD, UNPACK_PAIR(Method, method)); 2612 oop executable; 2613 if (m->is_class_initializer()) { 2614 JVMCI_THROW_MSG_NULL(IllegalArgumentException, 2615 "Cannot create java.lang.reflect.Method for class initializer"); 2616 } 2617 else if (m->is_object_constructor() || m->is_static_vnew_factory()) { 2618 executable = Reflection::new_constructor(m, CHECK_NULL); 2619 } else { 2620 executable = Reflection::new_method(m, false, CHECK_NULL); 2621 } 2622 return JNIHandles::make_local(THREAD, executable); 2623 } 2624 2625 C2V_VMENTRY_NULL(jobject, asReflectionField, (JNIEnv* env, jobject, ARGUMENT_PAIR(klass), jint index)) 2626 requireInHotSpot("asReflectionField", JVMCI_CHECK_NULL); 2627 Klass* klass = UNPACK_PAIR(Klass, klass); 2628 if (!klass->is_instance_klass()) { 2629 JVMCI_THROW_MSG_NULL(IllegalArgumentException, 2630 err_msg("Expected non-primitive type, got %s", klass->external_name())); 2631 } 2632 InstanceKlass* iklass = InstanceKlass::cast(klass); 2633 Array<u2>* fields = iklass->fields(); 2634 if (index < 0 ||index > fields->length()) { 2635 JVMCI_THROW_MSG_NULL(IllegalArgumentException, 2636 err_msg("Field index %d out of bounds for %s", index, klass->external_name())); 2637 } 2638 fieldDescriptor fd(iklass, index); 2639 oop reflected = Reflection::new_field(&fd, CHECK_NULL); 2640 return JNIHandles::make_local(THREAD, reflected); 2641 } 2642 2643 C2V_VMENTRY_NULL(jobjectArray, getFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address, jobjectArray current)) 2644 FailedSpeculation* head = *((FailedSpeculation**)(address) failed_speculations_address); 2645 int result_length = 0; 2646 for (FailedSpeculation* fs = head; fs != NULL; fs = fs->next()) { 2647 result_length++; 2648 } 2649 int current_length = 0; 2650 JVMCIObjectArray current_array = NULL; 2651 if (current != NULL) { 2652 current_array = JVMCIENV->wrap(current); 2653 current_length = JVMCIENV->get_length(current_array); 2654 if (current_length == result_length) { 2655 // No new failures 2656 return current; 2657 } 2658 } 2659 JVMCIObjectArray result = JVMCIENV->new_byte_array_array(result_length, JVMCI_CHECK_NULL); 2660 int result_index = 0; 2661 for (FailedSpeculation* fs = head; result_index < result_length; fs = fs->next()) { 2662 assert(fs != NULL, "npe"); 2663 JVMCIPrimitiveArray entry; 2664 if (result_index < current_length) { 2665 entry = (JVMCIPrimitiveArray) JVMCIENV->get_object_at(current_array, result_index); 2666 } else { 2667 entry = JVMCIENV->new_byteArray(fs->data_len(), JVMCI_CHECK_NULL); 2668 JVMCIENV->copy_bytes_from((jbyte*) fs->data(), entry, 0, fs->data_len()); 2669 } 2670 JVMCIENV->put_object_at(result, result_index++, entry); 2671 } 2672 return JVMCIENV->get_jobjectArray(result); 2673 } 2674 2675 C2V_VMENTRY_0(jlong, getFailedSpeculationsAddress, (JNIEnv* env, jobject, ARGUMENT_PAIR(method))) 2676 methodHandle method(THREAD, UNPACK_PAIR(Method, method)); 2677 MethodData* method_data = method->method_data(); 2678 if (method_data == NULL) { 2679 ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); 2680 method_data = MethodData::allocate(loader_data, method, CHECK_0); 2681 method->set_method_data(method_data); 2682 } 2683 return (jlong) method_data->get_failed_speculations_address(); 2684 } 2685 2686 C2V_VMENTRY(void, releaseFailedSpeculations, (JNIEnv* env, jobject, jlong failed_speculations_address)) 2687 FailedSpeculation::free_failed_speculations((FailedSpeculation**)(address) failed_speculations_address); 2688 } 2689 2690 C2V_VMENTRY_0(jboolean, addFailedSpeculation, (JNIEnv* env, jobject, jlong failed_speculations_address, jbyteArray speculation_obj)) 2691 JVMCIPrimitiveArray speculation_handle = JVMCIENV->wrap(speculation_obj); 2692 int speculation_len = JVMCIENV->get_length(speculation_handle); 2693 char* speculation = NEW_RESOURCE_ARRAY(char, speculation_len); 2694 JVMCIENV->copy_bytes_to(speculation_handle, (jbyte*) speculation, 0, speculation_len); 2695 return FailedSpeculation::add_failed_speculation(NULL, (FailedSpeculation**)(address) failed_speculations_address, (address) speculation, speculation_len); 2696 } 2697 2698 C2V_VMENTRY(void, callSystemExit, (JNIEnv* env, jobject, jint status)) 2699 JavaValue result(T_VOID); 2700 JavaCallArguments jargs(1); 2701 jargs.push_int(status); 2702 JavaCalls::call_static(&result, 2703 vmClasses::System_klass(), 2704 vmSymbols::exit_method_name(), 2705 vmSymbols::int_void_signature(), 2706 &jargs, 2707 CHECK); 2708 } 2709 2710 C2V_VMENTRY_0(jlong, ticksNow, (JNIEnv* env, jobject)) 2711 return CompilerEvent::ticksNow(); 2712 } 2713 2714 C2V_VMENTRY_0(jint, registerCompilerPhase, (JNIEnv* env, jobject, jstring jphase_name)) 2715 #if INCLUDE_JFR 2716 JVMCIObject phase_name = JVMCIENV->wrap(jphase_name); 2717 const char *name = JVMCIENV->as_utf8_string(phase_name); 2718 return CompilerEvent::PhaseEvent::get_phase_id(name, true, true, true); 2719 #else 2720 return -1; 2721 #endif // !INCLUDE_JFR 2722 } 2723 2724 C2V_VMENTRY(void, notifyCompilerPhaseEvent, (JNIEnv* env, jobject, jlong startTime, jint phase, jint compileId, jint level)) 2725 EventCompilerPhase event; 2726 if (event.should_commit()) { 2727 CompilerEvent::PhaseEvent::post(event, startTime, phase, compileId, level); 2728 } 2729 } 2730 2731 C2V_VMENTRY(void, notifyCompilerInliningEvent, (JNIEnv* env, jobject, jint compileId, ARGUMENT_PAIR(caller), ARGUMENT_PAIR(callee), jboolean succeeded, jstring jmessage, jint bci)) 2732 EventCompilerInlining event; 2733 if (event.should_commit()) { 2734 Method* caller = UNPACK_PAIR(Method, caller); 2735 Method* callee = UNPACK_PAIR(Method, callee); 2736 JVMCIObject message = JVMCIENV->wrap(jmessage); 2737 CompilerEvent::InlineEvent::post(event, compileId, caller, callee, succeeded, JVMCIENV->as_utf8_string(message), bci); 2738 } 2739 } 2740 2741 C2V_VMENTRY(void, setThreadLocalObject, (JNIEnv* env, jobject, jint id, jobject value)) 2742 requireInHotSpot("setThreadLocalObject", JVMCI_CHECK); 2743 if (id == 0) { 2744 thread->set_jvmci_reserved_oop0(JNIHandles::resolve(value)); 2745 return; 2746 } 2747 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 2748 err_msg("%d is not a valid thread local id", id)); 2749 } 2750 2751 C2V_VMENTRY_NULL(jobject, getThreadLocalObject, (JNIEnv* env, jobject, jint id)) 2752 requireInHotSpot("getThreadLocalObject", JVMCI_CHECK_NULL); 2753 if (id == 0) { 2754 return JNIHandles::make_local(thread->get_jvmci_reserved_oop0()); 2755 } 2756 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 2757 err_msg("%d is not a valid thread local id", id)); 2758 } 2759 2760 C2V_VMENTRY(void, setThreadLocalLong, (JNIEnv* env, jobject, jint id, jlong value)) 2761 requireInHotSpot("setThreadLocalLong", JVMCI_CHECK); 2762 if (id == 0) { 2763 thread->set_jvmci_reserved0(value); 2764 } else if (id == 1) { 2765 thread->set_jvmci_reserved1(value); 2766 } else { 2767 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 2768 err_msg("%d is not a valid thread local id", id)); 2769 } 2770 } 2771 2772 C2V_VMENTRY_0(jlong, getThreadLocalLong, (JNIEnv* env, jobject, jint id)) 2773 requireInHotSpot("getThreadLocalLong", JVMCI_CHECK_0); 2774 if (id == 0) { 2775 return thread->get_jvmci_reserved0(); 2776 } else if (id == 1) { 2777 return thread->get_jvmci_reserved1(); 2778 } else { 2779 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), 2780 err_msg("%d is not a valid thread local id", id)); 2781 } 2782 } 2783 2784 #define CC (char*) /*cast a literal from (const char*)*/ 2785 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &(c2v_ ## f)) 2786 2787 #define STRING "Ljava/lang/String;" 2788 #define OBJECT "Ljava/lang/Object;" 2789 #define CLASS "Ljava/lang/Class;" 2790 #define OBJECTCONSTANT "Ljdk/vm/ci/hotspot/HotSpotObjectConstantImpl;" 2791 #define EXECUTABLE "Ljava/lang/reflect/Executable;" 2792 #define STACK_TRACE_ELEMENT "Ljava/lang/StackTraceElement;" 2793 #define INSTALLED_CODE "Ljdk/vm/ci/code/InstalledCode;" 2794 #define BYTECODE_FRAME "Ljdk/vm/ci/code/BytecodeFrame;" 2795 #define JAVACONSTANT "Ljdk/vm/ci/meta/JavaConstant;" 2796 #define INSPECTED_FRAME_VISITOR "Ljdk/vm/ci/code/stack/InspectedFrameVisitor;" 2797 #define RESOLVED_METHOD "Ljdk/vm/ci/meta/ResolvedJavaMethod;" 2798 #define HS_RESOLVED_TYPE "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaType;" 2799 #define HS_INSTALLED_CODE "Ljdk/vm/ci/hotspot/HotSpotInstalledCode;" 2800 #define HS_NMETHOD "Ljdk/vm/ci/hotspot/HotSpotNmethod;" 2801 #define HS_COMPILED_CODE "Ljdk/vm/ci/hotspot/HotSpotCompiledCode;" 2802 #define HS_CONFIG "Ljdk/vm/ci/hotspot/HotSpotVMConfig;" 2803 #define HS_STACK_FRAME_REF "Ljdk/vm/ci/hotspot/HotSpotStackFrameReference;" 2804 #define HS_SPECULATION_LOG "Ljdk/vm/ci/hotspot/HotSpotSpeculationLog;" 2805 #define REFLECTION_EXECUTABLE "Ljava/lang/reflect/Executable;" 2806 #define REFLECTION_FIELD "Ljava/lang/reflect/Field;" 2807 2808 // Types wrapping VM pointers. The ...2 macro is for a pair: (wrapper, pointer) 2809 #define HS_METHOD "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;" 2810 #define HS_METHOD2 "Ljdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl;J" 2811 #define HS_KLASS "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;" 2812 #define HS_KLASS2 "Ljdk/vm/ci/hotspot/HotSpotResolvedObjectTypeImpl;J" 2813 #define HS_CONSTANT_POOL "Ljdk/vm/ci/hotspot/HotSpotConstantPool;" 2814 #define HS_CONSTANT_POOL2 "Ljdk/vm/ci/hotspot/HotSpotConstantPool;J" 2815 2816 JNINativeMethod CompilerToVM::methods[] = { 2817 {CC "getBytecode", CC "(" HS_METHOD2 ")[B", FN_PTR(getBytecode)}, 2818 {CC "getExceptionTableStart", CC "(" HS_METHOD2 ")J", FN_PTR(getExceptionTableStart)}, 2819 {CC "getExceptionTableLength", CC "(" HS_METHOD2 ")I", FN_PTR(getExceptionTableLength)}, 2820 {CC "findUniqueConcreteMethod", CC "(" HS_KLASS2 HS_METHOD2 ")" HS_METHOD, FN_PTR(findUniqueConcreteMethod)}, 2821 {CC "getImplementor", CC "(" HS_KLASS2 ")" HS_KLASS, FN_PTR(getImplementor)}, 2822 {CC "getStackTraceElement", CC "(" HS_METHOD2 "I)" STACK_TRACE_ELEMENT, FN_PTR(getStackTraceElement)}, 2823 {CC "methodIsIgnoredBySecurityStackWalk", CC "(" HS_METHOD2 ")Z", FN_PTR(methodIsIgnoredBySecurityStackWalk)}, 2824 {CC "setNotInlinableOrCompilable", CC "(" HS_METHOD2 ")V", FN_PTR(setNotInlinableOrCompilable)}, 2825 {CC "isCompilable", CC "(" HS_METHOD2 ")Z", FN_PTR(isCompilable)}, 2826 {CC "hasNeverInlineDirective", CC "(" HS_METHOD2 ")Z", FN_PTR(hasNeverInlineDirective)}, 2827 {CC "shouldInlineMethod", CC "(" HS_METHOD2 ")Z", FN_PTR(shouldInlineMethod)}, 2828 {CC "lookupType", CC "(" STRING HS_KLASS2 "Z)" HS_RESOLVED_TYPE, FN_PTR(lookupType)}, 2829 {CC "getArrayType", CC "(C" HS_KLASS2 ")" HS_KLASS, FN_PTR(getArrayType)}, 2830 {CC "lookupClass", CC "(" CLASS ")" HS_RESOLVED_TYPE, FN_PTR(lookupClass)}, 2831 {CC "lookupNameInPool", CC "(" HS_CONSTANT_POOL2 "I)" STRING, FN_PTR(lookupNameInPool)}, 2832 {CC "lookupNameAndTypeRefIndexInPool", CC "(" HS_CONSTANT_POOL2 "I)I", FN_PTR(lookupNameAndTypeRefIndexInPool)}, 2833 {CC "lookupSignatureInPool", CC "(" HS_CONSTANT_POOL2 "I)" STRING, FN_PTR(lookupSignatureInPool)}, 2834 {CC "lookupKlassRefIndexInPool", CC "(" HS_CONSTANT_POOL2 "I)I", FN_PTR(lookupKlassRefIndexInPool)}, 2835 {CC "lookupKlassInPool", CC "(" HS_CONSTANT_POOL2 "I)Ljava/lang/Object;", FN_PTR(lookupKlassInPool)}, 2836 {CC "lookupAppendixInPool", CC "(" HS_CONSTANT_POOL2 "I)" OBJECTCONSTANT, FN_PTR(lookupAppendixInPool)}, 2837 {CC "lookupMethodInPool", CC "(" HS_CONSTANT_POOL2 "IB" HS_METHOD2 ")" HS_METHOD, FN_PTR(lookupMethodInPool)}, 2838 {CC "constantPoolRemapInstructionOperandFromCache", CC "(" HS_CONSTANT_POOL2 "I)I", FN_PTR(constantPoolRemapInstructionOperandFromCache)}, 2839 {CC "resolveBootstrapMethod", CC "(" HS_CONSTANT_POOL2 "I)[" OBJECT, FN_PTR(resolveBootstrapMethod)}, 2840 {CC "getUncachedStringInPool", CC "(" HS_CONSTANT_POOL2 "I)" JAVACONSTANT, FN_PTR(getUncachedStringInPool)}, 2841 {CC "resolvePossiblyCachedConstantInPool", CC "(" HS_CONSTANT_POOL2 "I)" JAVACONSTANT, FN_PTR(resolvePossiblyCachedConstantInPool)}, 2842 {CC "resolveTypeInPool", CC "(" HS_CONSTANT_POOL2 "I)" HS_KLASS, FN_PTR(resolveTypeInPool)}, 2843 {CC "resolveFieldInPool", CC "(" HS_CONSTANT_POOL2 "I" HS_METHOD2 "B[I)" HS_KLASS, FN_PTR(resolveFieldInPool)}, 2844 {CC "resolveInvokeDynamicInPool", CC "(" HS_CONSTANT_POOL2 "I)V", FN_PTR(resolveInvokeDynamicInPool)}, 2845 {CC "resolveInvokeHandleInPool", CC "(" HS_CONSTANT_POOL2 "I)V", FN_PTR(resolveInvokeHandleInPool)}, 2846 {CC "isResolvedInvokeHandleInPool", CC "(" HS_CONSTANT_POOL2 "I)I", FN_PTR(isResolvedInvokeHandleInPool)}, 2847 {CC "resolveMethod", CC "(" HS_KLASS2 HS_METHOD2 HS_KLASS2 ")" HS_METHOD, FN_PTR(resolveMethod)}, 2848 {CC "getSignaturePolymorphicHolders", CC "()[" STRING, FN_PTR(getSignaturePolymorphicHolders)}, 2849 {CC "getVtableIndexForInterfaceMethod", CC "(" HS_KLASS2 HS_METHOD2 ")I", FN_PTR(getVtableIndexForInterfaceMethod)}, 2850 {CC "getClassInitializer", CC "(" HS_KLASS2 ")" HS_METHOD, FN_PTR(getClassInitializer)}, 2851 {CC "hasFinalizableSubclass", CC "(" HS_KLASS2 ")Z", FN_PTR(hasFinalizableSubclass)}, 2852 {CC "getMaxCallTargetOffset", CC "(J)J", FN_PTR(getMaxCallTargetOffset)}, 2853 {CC "asResolvedJavaMethod", CC "(" EXECUTABLE ")" HS_METHOD, FN_PTR(asResolvedJavaMethod)}, 2854 {CC "getResolvedJavaMethod", CC "(" OBJECTCONSTANT "J)" HS_METHOD, FN_PTR(getResolvedJavaMethod)}, 2855 {CC "getConstantPool", CC "(" OBJECT "JZ)" HS_CONSTANT_POOL, FN_PTR(getConstantPool)}, 2856 {CC "getResolvedJavaType0", CC "(Ljava/lang/Object;JZ)" HS_KLASS, FN_PTR(getResolvedJavaType0)}, 2857 {CC "readConfiguration", CC "()[" OBJECT, FN_PTR(readConfiguration)}, 2858 {CC "installCode0", CC "(JJZ" HS_COMPILED_CODE "[" OBJECT INSTALLED_CODE "J[B)I", FN_PTR(installCode0)}, 2859 {CC "getInstallCodeFlags", CC "()I", FN_PTR(getInstallCodeFlags)}, 2860 {CC "resetCompilationStatistics", CC "()V", FN_PTR(resetCompilationStatistics)}, 2861 {CC "disassembleCodeBlob", CC "(" INSTALLED_CODE ")" STRING, FN_PTR(disassembleCodeBlob)}, 2862 {CC "executeHotSpotNmethod", CC "([" OBJECT HS_NMETHOD ")" OBJECT, FN_PTR(executeHotSpotNmethod)}, 2863 {CC "getLineNumberTable", CC "(" HS_METHOD2 ")[J", FN_PTR(getLineNumberTable)}, 2864 {CC "getLocalVariableTableStart", CC "(" HS_METHOD2 ")J", FN_PTR(getLocalVariableTableStart)}, 2865 {CC "getLocalVariableTableLength", CC "(" HS_METHOD2 ")I", FN_PTR(getLocalVariableTableLength)}, 2866 {CC "reprofile", CC "(" HS_METHOD2 ")V", FN_PTR(reprofile)}, 2867 {CC "invalidateHotSpotNmethod", CC "(" HS_NMETHOD "Z)V", FN_PTR(invalidateHotSpotNmethod)}, 2868 {CC "collectCounters", CC "()[J", FN_PTR(collectCounters)}, 2869 {CC "getCountersSize", CC "()I", FN_PTR(getCountersSize)}, 2870 {CC "setCountersSize", CC "(I)Z", FN_PTR(setCountersSize)}, 2871 {CC "allocateCompileId", CC "(" HS_METHOD2 "I)I", FN_PTR(allocateCompileId)}, 2872 {CC "isMature", CC "(J)Z", FN_PTR(isMature)}, 2873 {CC "hasCompiledCodeForOSR", CC "(" HS_METHOD2 "II)Z", FN_PTR(hasCompiledCodeForOSR)}, 2874 {CC "getSymbol", CC "(J)" STRING, FN_PTR(getSymbol)}, 2875 {CC "getSignatureName", CC "(J)" STRING, FN_PTR(getSignatureName)}, 2876 {CC "iterateFrames", CC "([" RESOLVED_METHOD "[" RESOLVED_METHOD "I" INSPECTED_FRAME_VISITOR ")" OBJECT, FN_PTR(iterateFrames)}, 2877 {CC "materializeVirtualObjects", CC "(" HS_STACK_FRAME_REF "Z)V", FN_PTR(materializeVirtualObjects)}, 2878 {CC "shouldDebugNonSafepoints", CC "()Z", FN_PTR(shouldDebugNonSafepoints)}, 2879 {CC "writeDebugOutput", CC "(JIZ)V", FN_PTR(writeDebugOutput)}, 2880 {CC "flushDebugOutput", CC "()V", FN_PTR(flushDebugOutput)}, 2881 {CC "methodDataProfileDataSize", CC "(JI)I", FN_PTR(methodDataProfileDataSize)}, 2882 {CC "interpreterFrameSize", CC "(" BYTECODE_FRAME ")I", FN_PTR(interpreterFrameSize)}, 2883 {CC "compileToBytecode", CC "(" OBJECTCONSTANT ")V", FN_PTR(compileToBytecode)}, 2884 {CC "getFlagValue", CC "(" STRING ")" OBJECT, FN_PTR(getFlagValue)}, 2885 {CC "getInterfaces", CC "(" HS_KLASS2 ")[" HS_KLASS, FN_PTR(getInterfaces)}, 2886 {CC "getComponentType", CC "(" HS_KLASS2 ")" HS_RESOLVED_TYPE, FN_PTR(getComponentType)}, 2887 {CC "ensureInitialized", CC "(" HS_KLASS2 ")V", FN_PTR(ensureInitialized)}, 2888 {CC "ensureLinked", CC "(" HS_KLASS2 ")V", FN_PTR(ensureLinked)}, 2889 {CC "getIdentityHashCode", CC "(" OBJECTCONSTANT ")I", FN_PTR(getIdentityHashCode)}, 2890 {CC "isInternedString", CC "(" OBJECTCONSTANT ")Z", FN_PTR(isInternedString)}, 2891 {CC "unboxPrimitive", CC "(" OBJECTCONSTANT ")" OBJECT, FN_PTR(unboxPrimitive)}, 2892 {CC "boxPrimitive", CC "(" OBJECT ")" OBJECTCONSTANT, FN_PTR(boxPrimitive)}, 2893 {CC "getDeclaredConstructors", CC "(" HS_KLASS2 ")[" RESOLVED_METHOD, FN_PTR(getDeclaredConstructors)}, 2894 {CC "getDeclaredMethods", CC "(" HS_KLASS2 ")[" RESOLVED_METHOD, FN_PTR(getDeclaredMethods)}, 2895 {CC "readStaticFieldValue", CC "(" HS_KLASS2 "JC)" JAVACONSTANT, FN_PTR(readStaticFieldValue)}, 2896 {CC "readFieldValue", CC "(" OBJECTCONSTANT HS_KLASS2 "JC)" JAVACONSTANT, FN_PTR(readFieldValue)}, 2897 {CC "isInstance", CC "(" HS_KLASS2 OBJECTCONSTANT ")Z", FN_PTR(isInstance)}, 2898 {CC "isAssignableFrom", CC "(" HS_KLASS2 HS_KLASS2 ")Z", FN_PTR(isAssignableFrom)}, 2899 {CC "isTrustedForIntrinsics", CC "(" HS_KLASS2 ")Z", FN_PTR(isTrustedForIntrinsics)}, 2900 {CC "asJavaType", CC "(" OBJECTCONSTANT ")" HS_RESOLVED_TYPE, FN_PTR(asJavaType)}, 2901 {CC "asString", CC "(" OBJECTCONSTANT ")" STRING, FN_PTR(asString)}, 2902 {CC "equals", CC "(" OBJECTCONSTANT "J" OBJECTCONSTANT "J)Z", FN_PTR(equals)}, 2903 {CC "getJavaMirror", CC "(" HS_KLASS2 ")" OBJECTCONSTANT, FN_PTR(getJavaMirror)}, 2904 {CC "getArrayLength", CC "(" OBJECTCONSTANT ")I", FN_PTR(getArrayLength)}, 2905 {CC "readArrayElement", CC "(" OBJECTCONSTANT "I)Ljava/lang/Object;", FN_PTR(readArrayElement)}, 2906 {CC "arrayBaseOffset", CC "(C)I", FN_PTR(arrayBaseOffset)}, 2907 {CC "arrayIndexScale", CC "(C)I", FN_PTR(arrayIndexScale)}, 2908 {CC "releaseClearedOopHandles", CC "()V", FN_PTR(releaseClearedOopHandles)}, 2909 {CC "registerNativeMethods", CC "(" CLASS ")[J", FN_PTR(registerNativeMethods)}, 2910 {CC "isCurrentThreadAttached", CC "()Z", FN_PTR(isCurrentThreadAttached)}, 2911 {CC "getCurrentJavaThread", CC "()J", FN_PTR(getCurrentJavaThread)}, 2912 {CC "attachCurrentThread", CC "([BZ[J)Z", FN_PTR(attachCurrentThread)}, 2913 {CC "detachCurrentThread", CC "(Z)Z", FN_PTR(detachCurrentThread)}, 2914 {CC "translate", CC "(" OBJECT "Z)J", FN_PTR(translate)}, 2915 {CC "unhand", CC "(J)" OBJECT, FN_PTR(unhand)}, 2916 {CC "updateHotSpotNmethod", CC "(" HS_NMETHOD ")V", FN_PTR(updateHotSpotNmethod)}, 2917 {CC "getCode", CC "(" HS_INSTALLED_CODE ")[B", FN_PTR(getCode)}, 2918 {CC "asReflectionExecutable", CC "(" HS_METHOD2 ")" REFLECTION_EXECUTABLE, FN_PTR(asReflectionExecutable)}, 2919 {CC "asReflectionField", CC "(" HS_KLASS2 "I)" REFLECTION_FIELD, FN_PTR(asReflectionField)}, 2920 {CC "getFailedSpeculations", CC "(J[[B)[[B", FN_PTR(getFailedSpeculations)}, 2921 {CC "getFailedSpeculationsAddress", CC "(" HS_METHOD2 ")J", FN_PTR(getFailedSpeculationsAddress)}, 2922 {CC "releaseFailedSpeculations", CC "(J)V", FN_PTR(releaseFailedSpeculations)}, 2923 {CC "addFailedSpeculation", CC "(J[B)Z", FN_PTR(addFailedSpeculation)}, 2924 {CC "callSystemExit", CC "(I)V", FN_PTR(callSystemExit)}, 2925 {CC "ticksNow", CC "()J", FN_PTR(ticksNow)}, 2926 {CC "getThreadLocalObject", CC "(I)" OBJECT, FN_PTR(getThreadLocalObject)}, 2927 {CC "setThreadLocalObject", CC "(I" OBJECT ")V", FN_PTR(setThreadLocalObject)}, 2928 {CC "getThreadLocalLong", CC "(I)J", FN_PTR(getThreadLocalLong)}, 2929 {CC "setThreadLocalLong", CC "(IJ)V", FN_PTR(setThreadLocalLong)}, 2930 {CC "registerCompilerPhase", CC "(" STRING ")I", FN_PTR(registerCompilerPhase)}, 2931 {CC "notifyCompilerPhaseEvent", CC "(JIII)V", FN_PTR(notifyCompilerPhaseEvent)}, 2932 {CC "notifyCompilerInliningEvent", CC "(I" HS_METHOD2 HS_METHOD2 "ZLjava/lang/String;I)V", FN_PTR(notifyCompilerInliningEvent)}, 2933 }; 2934 2935 int CompilerToVM::methods_count() { 2936 return sizeof(methods) / sizeof(JNINativeMethod); 2937 }