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