Path: blob/master/src/hotspot/share/jvmci/jvmciEnv.hpp
40949 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_JVMCI_JVMCIENV_HPP25#define SHARE_JVMCI_JVMCIENV_HPP2627#include "classfile/javaClasses.hpp"28#include "jvmci/jvmciJavaClasses.hpp"29#include "runtime/jniHandles.hpp"30#include "runtime/thread.hpp"3132class CompileTask;33class JVMCIObject;34class JVMCIObjectArray;35class JVMCIPrimitiveArray;36class JVMCICompiler;37class JVMCIRuntime;38class nmethodLocker;3940#define JVMCI_EXCEPTION_CONTEXT \41JavaThread* thread = JavaThread::current(); \42JavaThread* THREAD = thread; // For exception macros.4344// Helper to log more context on a JNI exception45#define JVMCI_EXCEPTION_CHECK(env, ...) \46do { \47if (env->ExceptionCheck()) { \48if (env != JavaThread::current()->jni_environment()) { \49char* sl_path; \50if (::JVMCI::get_shared_library(sl_path, false) != NULL) { \51tty->print_cr("In JVMCI shared library (%s):", sl_path); \52} \53} \54tty->print_cr(__VA_ARGS__); \55return; \56} \57} while(0)5859// Helper class to ensure that references to Klass* are kept alive for G160class JVMCIKlassHandle : public StackObj {61private:62Klass* _klass;63Handle _holder;64Thread* _thread;6566Klass* klass() const { return _klass; }67Klass* non_null_klass() const { assert(_klass != NULL, "resolving NULL _klass"); return _klass; }6869public:70/* Constructors */71JVMCIKlassHandle (Thread* thread) : _klass(NULL), _thread(thread) {}72JVMCIKlassHandle (Thread* thread, Klass* klass);7374JVMCIKlassHandle (const JVMCIKlassHandle &h): _klass(h._klass), _holder(h._holder), _thread(h._thread) {}75JVMCIKlassHandle& operator=(const JVMCIKlassHandle &s);76JVMCIKlassHandle& operator=(Klass* klass);7778/* Operators for ease of use */79Klass* operator () () const { return klass(); }80Klass* operator -> () const { return non_null_klass(); }8182bool operator == (Klass* o) const { return klass() == o; }83bool operator == (const JVMCIKlassHandle& h) const { return klass() == h.klass(); }8485/* Null checks */86bool is_null() const { return _klass == NULL; }87bool not_null() const { return _klass != NULL; }88};8990// A class that maintains the state needed for compilations requested91// by the CompileBroker. It is created in the broker and passed through92// into the code installation step.93class JVMCICompileState : public ResourceObj {94friend class JVMCIVMStructs;95private:96CompileTask* _task;97JVMCICompiler* _compiler;9899// Cache JVMTI state. Defined as bytes so that reading them from Java100// via Unsafe is well defined (the C++ type for bool is implementation101// defined and may not be the same as a Java boolean).102uint64_t _jvmti_redefinition_count;103jbyte _jvmti_can_hotswap_or_post_breakpoint;104jbyte _jvmti_can_access_local_variables;105jbyte _jvmti_can_post_on_exceptions;106jbyte _jvmti_can_pop_frame;107bool _target_method_is_old;108109// Compilation result values.110bool _retryable;111const char* _failure_reason;112113// Specifies if _failure_reason is on the C heap. If so, it is allocated114// with the mtJVMCI NMT flag.115bool _failure_reason_on_C_heap;116117// A value indicating compilation activity during the compilation.118// If successive calls to this method return a different value, then119// some degree of JVMCI compilation occurred between the calls.120jint _compilation_ticks;121122public:123JVMCICompileState(CompileTask* task, JVMCICompiler* compiler);124125CompileTask* task() { return _task; }126127bool jvmti_state_changed() const;128uint64_t jvmti_redefinition_count() const { return _jvmti_redefinition_count; }129bool jvmti_can_hotswap_or_post_breakpoint() const { return _jvmti_can_hotswap_or_post_breakpoint != 0; }130bool jvmti_can_access_local_variables() const { return _jvmti_can_access_local_variables != 0; }131bool jvmti_can_post_on_exceptions() const { return _jvmti_can_post_on_exceptions != 0; }132bool jvmti_can_pop_frame() const { return _jvmti_can_pop_frame != 0; }133bool target_method_is_old() const { return _target_method_is_old; }134135const char* failure_reason() { return _failure_reason; }136bool failure_reason_on_C_heap() { return _failure_reason_on_C_heap; }137bool retryable() { return _retryable; }138139void set_failure(bool retryable, const char* reason, bool reason_on_C_heap = false) {140_failure_reason = reason;141_failure_reason_on_C_heap = reason_on_C_heap;142_retryable = retryable;143}144145jint compilation_ticks() const { return _compilation_ticks; }146void inc_compilation_ticks();147};148149150// This class is a top level wrapper around interactions between HotSpot151// and the JVMCI Java code. It supports both a HotSpot heap based152// runtime with HotSpot oop based accessors as well as a shared library153// based runtime that is accessed through JNI. It abstracts away all154// interactions with JVMCI objects so that a single version of the155// HotSpot C++ code can can work with either runtime.156class JVMCIEnv : public ResourceObj {157friend class JNIAccessMark;158159// Initializes the _env, _mode and _runtime fields.160void init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env);161162void init(JavaThread* thread, bool is_hotspot, const char* file, int line);163164JNIEnv* _env; // JNI env for calling into shared library165bool _pop_frame_on_close; // Must pop frame on close?166bool _detach_on_close; // Must detach on close?167JVMCIRuntime* _runtime; // Access to a HotSpotJVMCIRuntime168bool _is_hotspot; // Which heap is the HotSpotJVMCIRuntime in169bool _throw_to_caller; // Propagate an exception raised in this env to the caller?170const char* _file; // The file and ...171int _line; // ... line where this JNIEnv was created172173// Translates an exception on the HotSpot heap to an exception on174// the shared library heap. The translation includes the stack and175// causes of `throwable`. The translated exception is pending in the176// shared library thread upon returning.177void translate_hotspot_exception_to_jni_exception(JavaThread* THREAD, const Handle& throwable);178179public:180// Opens a JVMCIEnv scope for a Java to VM call (e.g., via CompilerToVM).181// An exception occurring within the scope is left pending when the182// scope closes so that it will be propagated back to Java.183// The JVMCIEnv destructor translates the exception object for the184// Java runtime if necessary.185JVMCIEnv(JavaThread* thread, JNIEnv* env, const char* file, int line);186187// Opens a JVMCIEnv scope for a compilation scheduled by the CompileBroker.188// An exception occurring within the scope must not be propagated back to189// the CompileBroker.190JVMCIEnv(JavaThread* thread, JVMCICompileState* compile_state, const char* file, int line);191192// Opens a JNIEnv scope for a call from within the VM. An exception occurring193// within the scope must not be propagated back to the caller.194JVMCIEnv(JavaThread* env, const char* file, int line);195196// Opens a JNIEnv scope for accessing `for_object`. An exception occurring197// within the scope must not be propagated back to the caller.198JVMCIEnv(JavaThread* thread, JVMCIObject for_object, const char* file, int line) {199// A JNI call to access an object in the shared library heap200// can block or take a long time so do not allow such access201// on the VM thread.202assert(for_object.is_hotspot() || !Thread::current()->is_VM_thread(),203"cannot open JVMCIEnv scope when in the VM thread for accessing a shared library heap object");204init(thread, for_object.is_hotspot(), file, line);205}206207// Opens a JNIEnv scope for the HotSpot runtime if `is_hotspot` is true208// otherwise for the shared library runtime. An exception occurring209// within the scope must not be propagated back to the caller.210JVMCIEnv(JavaThread* thread, bool is_hotspot, const char* file, int line) {211init(thread, is_hotspot, file, line);212}213214~JVMCIEnv();215216JVMCIRuntime* runtime() {217return _runtime;218}219220// Initializes Services.savedProperties in the shared library by copying221// the values from the same field in the HotSpot heap.222void copy_saved_properties();223224jboolean has_pending_exception();225void clear_pending_exception();226227// Prints an exception and stack trace of a pending exception.228void describe_pending_exception(bool clear);229230int get_length(JVMCIArray array);231232JVMCIObject get_object_at(JVMCIObjectArray array, int index);233void put_object_at(JVMCIObjectArray array, int index, JVMCIObject value);234235jboolean get_bool_at(JVMCIPrimitiveArray array, int index);236void put_bool_at(JVMCIPrimitiveArray array, int index, jboolean value);237238jbyte get_byte_at(JVMCIPrimitiveArray array, int index);239void put_byte_at(JVMCIPrimitiveArray array, int index, jbyte value);240241jint get_int_at(JVMCIPrimitiveArray array, int index);242void put_int_at(JVMCIPrimitiveArray array, int index, jint value);243244long get_long_at(JVMCIPrimitiveArray array, int index);245void put_long_at(JVMCIPrimitiveArray array, int index, jlong value);246247void copy_bytes_to(JVMCIPrimitiveArray src, jbyte* dest, int offset, jsize length);248void copy_bytes_from(jbyte* src, JVMCIPrimitiveArray dest, int offset, jsize length);249250void copy_longs_from(jlong* src, JVMCIPrimitiveArray dest, int offset, jsize length);251252JVMCIObjectArray initialize_intrinsics(JVMCI_TRAPS);253254jboolean is_boxing_object(BasicType type, JVMCIObject object);255256// Get the primitive value from a Java boxing object. It's hard error to257// pass a non-primitive BasicType.258jvalue get_boxed_value(BasicType type, JVMCIObject object);259260// Return the BasicType of the object if it's a boxing object, otherwise return T_ILLEGAL.261BasicType get_box_type(JVMCIObject object);262263// Create a boxing object of the appropriate primitive type.264JVMCIObject create_box(BasicType type, jvalue* value, JVMCI_TRAPS);265266const char* as_utf8_string(JVMCIObject str);267268JVMCIObject create_string(Symbol* str, JVMCI_TRAPS) {269JVMCIObject s = create_string(str->as_C_string(), JVMCI_CHECK_(JVMCIObject()));270return s;271}272273JVMCIObject create_string(const char* str, JVMCI_TRAPS);274275bool equals(JVMCIObject a, JVMCIObject b);276277// Convert into a JNI handle for the appropriate runtime278jobject get_jobject(JVMCIObject object) { assert(object.as_jobject() == NULL || is_hotspot() == object.is_hotspot(), "mismatch"); return object.as_jobject(); }279jarray get_jarray(JVMCIArray array) { assert(array.as_jobject() == NULL || is_hotspot() == array.is_hotspot(), "mismatch"); return array.as_jobject(); }280jobjectArray get_jobjectArray(JVMCIObjectArray objectArray) { assert(objectArray.as_jobject() == NULL || is_hotspot() == objectArray.is_hotspot(), "mismatch"); return objectArray.as_jobject(); }281jbyteArray get_jbyteArray(JVMCIPrimitiveArray primitiveArray) { assert(primitiveArray.as_jobject() == NULL || is_hotspot() == primitiveArray.is_hotspot(), "mismatch"); return primitiveArray.as_jbyteArray(); }282283JVMCIObject wrap(jobject obj);284JVMCIObjectArray wrap(jobjectArray obj) { return (JVMCIObjectArray) wrap((jobject) obj); }285JVMCIPrimitiveArray wrap(jintArray obj) { return (JVMCIPrimitiveArray) wrap((jobject) obj); }286JVMCIPrimitiveArray wrap(jbooleanArray obj) { return (JVMCIPrimitiveArray) wrap((jobject) obj); }287JVMCIPrimitiveArray wrap(jbyteArray obj) { return (JVMCIPrimitiveArray) wrap((jobject) obj); }288JVMCIPrimitiveArray wrap(jlongArray obj) { return (JVMCIPrimitiveArray) wrap((jobject) obj); }289290private:291JVMCIObject wrap(oop obj) { assert(is_hotspot(), "must be"); return wrap(JNIHandles::make_local(obj)); }292JVMCIObjectArray wrap(objArrayOop obj) { assert(is_hotspot(), "must be"); return (JVMCIObjectArray) wrap(JNIHandles::make_local(obj)); }293JVMCIPrimitiveArray wrap(typeArrayOop obj) { assert(is_hotspot(), "must be"); return (JVMCIPrimitiveArray) wrap(JNIHandles::make_local(obj)); }294295public:296// Compiles a method with the JVMCI compiler.297// Caller must handle pending exception.298JVMCIObject call_HotSpotJVMCIRuntime_compileMethod(JVMCIObject runtime, JVMCIObject method, int entry_bci,299jlong compile_state, int id);300301void call_HotSpotJVMCIRuntime_bootstrapFinished(JVMCIObject runtime, JVMCI_TRAPS);302void call_HotSpotJVMCIRuntime_shutdown(JVMCIObject runtime);303JVMCIObject call_HotSpotJVMCIRuntime_runtime(JVMCI_TRAPS);304JVMCIObject call_JVMCI_getRuntime(JVMCI_TRAPS);305JVMCIObject call_HotSpotJVMCIRuntime_getCompiler(JVMCIObject runtime, JVMCI_TRAPS);306307JVMCIObject call_HotSpotJVMCIRuntime_callToString(JVMCIObject object, JVMCI_TRAPS);308309JVMCIObject call_JavaConstant_forPrimitive(JVMCIObject kind, jlong value, JVMCI_TRAPS);310311jboolean call_HotSpotJVMCIRuntime_isGCSupported(JVMCIObject runtime, jint gcIdentifier);312313BasicType kindToBasicType(JVMCIObject kind, JVMCI_TRAPS);314315#define DO_THROW(name) \316void throw_##name(const char* msg = NULL);317318DO_THROW(InternalError)319DO_THROW(ArrayIndexOutOfBoundsException)320DO_THROW(IllegalStateException)321DO_THROW(NullPointerException)322DO_THROW(IllegalArgumentException)323DO_THROW(InvalidInstalledCodeException)324DO_THROW(UnsatisfiedLinkError)325DO_THROW(UnsupportedOperationException)326DO_THROW(ClassNotFoundException)327328#undef DO_THROW329330void fthrow_error(const char* file, int line, const char* format, ...) ATTRIBUTE_PRINTF(4, 5);331332// Given an instance of HotSpotInstalledCode return the corresponding CodeBlob*. The333// nmethodLocker is required to keep the CodeBlob alive in the case where it's an nmethod.334CodeBlob* get_code_blob(JVMCIObject code, nmethodLocker& locker);335336// Given an instance of HotSpotInstalledCode return the corresponding nmethod. The337// nmethodLocker is required to keep the nmethod alive.338nmethod* get_nmethod(JVMCIObject code, nmethodLocker& locker);339340MethodData* asMethodData(jlong metaspaceMethodData) {341return (MethodData*) (address) metaspaceMethodData;342}343344const char* klass_name(JVMCIObject object);345346// Unpack an instance of HotSpotResolvedJavaMethodImpl into the original Method*347Method* asMethod(JVMCIObject jvmci_method);348Method* asMethod(jobject jvmci_method) { return asMethod(wrap(jvmci_method)); }349350// Unpack an instance of HotSpotResolvedObjectTypeImpl into the original Klass*351Klass* asKlass(JVMCIObject jvmci_type);352Klass* asKlass(jobject jvmci_type) { return asKlass(wrap(jvmci_type)); }353354JVMCIObject get_jvmci_method(const methodHandle& method, JVMCI_TRAPS);355356JVMCIObject get_jvmci_type(const JVMCIKlassHandle& klass, JVMCI_TRAPS);357358// Unpack an instance of HotSpotConstantPool into the original ConstantPool*359ConstantPool* asConstantPool(JVMCIObject constant_pool);360ConstantPool* asConstantPool(jobject constant_pool) { return asConstantPool(wrap(constant_pool)); }361362JVMCIObject get_jvmci_constant_pool(const constantPoolHandle& cp, JVMCI_TRAPS);363JVMCIObject get_jvmci_primitive_type(BasicType type);364365Handle asConstant(JVMCIObject object, JVMCI_TRAPS);366JVMCIObject get_object_constant(oop objOop, bool compressed = false, bool dont_register = false);367368JVMCIPrimitiveArray new_booleanArray(int length, JVMCI_TRAPS);369JVMCIPrimitiveArray new_byteArray(int length, JVMCI_TRAPS);370JVMCIPrimitiveArray new_intArray(int length, JVMCI_TRAPS);371JVMCIPrimitiveArray new_longArray(int length, JVMCI_TRAPS);372373JVMCIObjectArray new_byte_array_array(int length, JVMCI_TRAPS);374375JVMCIObject new_StackTraceElement(const methodHandle& method, int bci, JVMCI_TRAPS);376JVMCIObject new_HotSpotNmethod(const methodHandle& method, const char* name, jboolean isDefault, jlong compileId, JVMCI_TRAPS);377JVMCIObject new_VMField(JVMCIObject name, JVMCIObject type, jlong offset, jlong address, JVMCIObject value, JVMCI_TRAPS);378JVMCIObject new_VMFlag(JVMCIObject name, JVMCIObject type, JVMCIObject value, JVMCI_TRAPS);379JVMCIObject new_VMIntrinsicMethod(JVMCIObject declaringClass, JVMCIObject name, JVMCIObject descriptor, int id, JVMCI_TRAPS);380JVMCIObject new_HotSpotStackFrameReference(JVMCI_TRAPS);381JVMCIObject new_JVMCIError(JVMCI_TRAPS);382383jlong make_handle(const Handle& obj);384oop resolve_handle(jlong objectHandle);385386// These are analagous to the JNI routines387JVMCIObject make_local(JVMCIObject object);388void destroy_local(JVMCIObject object);389390// Makes a JNI global handle that is not scoped by the391// lifetime of a JVMCIRuntime (cf JVMCIRuntime::make_global).392// These JNI handles are used when translating an object393// between the HotSpot and JVMCI shared library heap via394// HotSpotJVMCIRuntime.translate(Object) and395// HotSpotJVMCIRuntime.unhand(Class<T>, long). Translation396// can happen in either direction so the referenced object397// can reside in either heap which is why JVMCIRuntime scoped398// handles cannot be used (they are specific to HotSpot heap objects).399JVMCIObject make_global(JVMCIObject object);400401// Destroys a JNI global handle created by JVMCIEnv::make_global.402void destroy_global(JVMCIObject object);403404// Deoptimizes the nmethod (if any) in the HotSpotNmethod.address405// field of mirror. The field is subsequently zeroed.406void invalidate_nmethod_mirror(JVMCIObject mirror, JVMCI_TRAPS);407408void initialize_installed_code(JVMCIObject installed_code, CodeBlob* cb, JVMCI_TRAPS);409410private:411JVMCICompileState* _compile_state;412413public:414415// Determines if this is for the JVMCI runtime in the HotSpot416// heap (true) or the shared library heap (false).417bool is_hotspot() { return _is_hotspot; }418419JVMCICompileState* compile_state() { return _compile_state; }420void set_compile_state(JVMCICompileState* compile_state) {421assert(_compile_state == NULL, "set only once");422_compile_state = compile_state;423}424// Generate declarations for the initialize, new, isa, get and set methods for all the types and425// fields declared in the JVMCI_CLASSES_DO macro.426427#define START_CLASS(className, fullClassName) \428void className##_initialize(JVMCI_TRAPS); \429JVMCIObjectArray new_##className##_array(int length, JVMCI_TRAPS); \430bool isa_##className(JVMCIObject object);431432#define END_CLASS433434#define FIELD(className, name, type, accessor) \435type get_ ## className ## _ ## name(JVMCIObject obj); \436void set_ ## className ## _ ## name(JVMCIObject obj, type x);437438#define OOPISH_FIELD(className, name, type, hstype, accessor) \439FIELD(className, name, type, accessor)440441#define STATIC_FIELD(className, name, type) \442type get_ ## className ## _ ## name(); \443void set_ ## className ## _ ## name(type x);444445#define STATIC_OOPISH_FIELD(className, name, type, hstype) \446STATIC_FIELD(className, name, type)447448#define EMPTY_CAST449#define CHAR_FIELD(className, name) FIELD(className, name, jchar, char_field)450#define INT_FIELD(className, name) FIELD(className, name, jint, int_field)451#define BOOLEAN_FIELD(className, name) FIELD(className, name, jboolean, bool_field)452#define LONG_FIELD(className, name) FIELD(className, name, jlong, long_field)453#define FLOAT_FIELD(className, name) FIELD(className, name, jfloat, float_field)454#define OBJECT_FIELD(className, name, signature) OOPISH_FIELD(className, name, JVMCIObject, oop, obj_field)455#define OBJECTARRAY_FIELD(className, name, signature) OOPISH_FIELD(className, name, JVMCIObjectArray, objArrayOop, obj_field)456#define PRIMARRAY_FIELD(className, name, signature) OOPISH_FIELD(className, name, JVMCIPrimitiveArray, typeArrayOop, obj_field)457458#define STATIC_INT_FIELD(className, name) STATIC_FIELD(className, name, jint)459#define STATIC_BOOLEAN_FIELD(className, name) STATIC_FIELD(className, name, jboolean)460#define STATIC_OBJECT_FIELD(className, name, signature) STATIC_OOPISH_FIELD(className, name, JVMCIObject, oop)461#define STATIC_OBJECTARRAY_FIELD(className, name, signature) STATIC_OOPISH_FIELD(className, name, JVMCIObjectArray, objArrayOop)462#define METHOD(jniCallType, jniGetMethod, hsCallType, returnType, className, methodName, signatureSymbolName, args)463#define CONSTRUCTOR(className, signature)464465JVMCI_CLASSES_DO(START_CLASS, END_CLASS, CHAR_FIELD, INT_FIELD, BOOLEAN_FIELD, LONG_FIELD, FLOAT_FIELD, OBJECT_FIELD, PRIMARRAY_FIELD, OBJECTARRAY_FIELD, STATIC_OBJECT_FIELD, STATIC_OBJECTARRAY_FIELD, STATIC_INT_FIELD, STATIC_BOOLEAN_FIELD, METHOD, CONSTRUCTOR)466467#undef JNI_START_CLASS468#undef START_CLASS469#undef END_CLASS470#undef METHOD471#undef CONSTRUCTOR472#undef FIELD473#undef CHAR_FIELD474#undef INT_FIELD475#undef BOOLEAN_FIELD476#undef LONG_FIELD477#undef FLOAT_FIELD478#undef OBJECT_FIELD479#undef PRIMARRAY_FIELD480#undef OBJECTARRAY_FIELD481#undef FIELD482#undef OOPISH_FIELD483#undef STATIC_FIELD484#undef STATIC_OOPISH_FIELD485#undef STATIC_FIELD486#undef STATIC_OBJECT_FIELD487#undef STATIC_OBJECTARRAY_FIELD488#undef STATIC_INT_FIELD489#undef STATIC_BOOLEAN_FIELD490#undef EMPTY_CAST491492// End of JVMCIEnv493};494495#endif // SHARE_JVMCI_JVMCIENV_HPP496497498