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 25 #ifndef SHARE_JVMCI_JVMCIENV_HPP 26 #define SHARE_JVMCI_JVMCIENV_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "jvmci/jvmciJavaClasses.hpp" 30 #include "runtime/thread.hpp" 31 32 class CompileTask; 33 class JVMCIObject; 34 class JVMCIObjectArray; 35 class JVMCIPrimitiveArray; 36 class JVMCICompiler; 37 class JVMCIRuntime; 38 39 // Bring the JVMCI compiler thread into the VM state. 40 #define JVMCI_VM_ENTRY_MARK \ 41 JavaThread* thread = JavaThread::current(); \ 42 ThreadInVMfromNative __tiv(thread); \ 43 ResetNoHandleMark rnhm; \ 44 HandleMarkCleaner __hm(thread); \ 45 Thread* THREAD = thread; \ 46 debug_only(VMNativeEntryWrapper __vew;) 47 48 #define JVMCI_EXCEPTION_CONTEXT \ 49 JavaThread* thread=JavaThread::current(); \ 50 Thread* THREAD = thread; 51 52 // Helper to log more context on a JNI exception 53 #define JVMCI_EXCEPTION_CHECK(env, ...) \ 54 do { \ 55 if (env->ExceptionCheck()) { \ 56 if (env != JavaThread::current()->jni_environment() && JVMCIEnv::get_shared_library_path() != NULL) { \ 57 tty->print_cr("In JVMCI shared library (%s):", JVMCIEnv::get_shared_library_path()); \ 58 } \ 59 tty->print_cr(__VA_ARGS__); \ 60 return; \ 61 } \ 62 } while(0) 63 64 // Helper class to ensure that references to Klass* are kept alive for G1 65 class JVMCIKlassHandle : public StackObj { 66 private: 67 Klass* _klass; 137 _failure_reason = reason; 138 _failure_reason_on_C_heap = reason_on_C_heap; 139 _retryable = retryable; 140 } 141 }; 142 143 144 // This class is a top level wrapper around interactions between HotSpot 145 // and the JVMCI Java code. It supports both a HotSpot heap based 146 // runtime with HotSpot oop based accessors as well as a shared library 147 // based runtime that is accessed through JNI. It abstracts away all 148 // interactions with JVMCI objects so that a single version of the 149 // HotSpot C++ code can can work with either runtime. 150 class JVMCIEnv : public ResourceObj { 151 friend class JNIAccessMark; 152 153 static char* _shared_library_path; // argument to os:dll_load 154 static void* _shared_library_handle; // result of os::dll_load 155 static JavaVM* _shared_library_javavm; // result of calling JNI_CreateJavaVM in shared library 156 157 // Attaches the current thread to the JavaVM in the shared library, 158 // initializing the shared library VM first if necessary. 159 // Returns the JNI interface pointer of the current thread. 160 // The _shared_library_* fields are initialized by the first 161 // call to this method. 162 static JNIEnv* attach_shared_library(); 163 164 // Initializes the _env, _mode and _runtime fields. 165 void init_env_mode_runtime(JNIEnv* parent_env); 166 167 void init(bool is_hotspot, const char* file, int line); 168 169 JNIEnv* _env; // JNI env for calling into shared library 170 JVMCIRuntime* _runtime; // Access to a HotSpotJVMCIRuntime 171 bool _is_hotspot; // Which heap is the HotSpotJVMCIRuntime in 172 bool _throw_to_caller; // Propagate an exception raised in this env to the caller? 173 const char* _file; // The file and ... 174 int _line; // ... line where this JNIEnv was created 175 176 // Translates an exception on the HotSpot heap to an exception on 177 // the shared library heap. The translation includes the stack and 178 // causes of `throwable`. The translated exception is pending in the 179 // shared library thread upon returning. 180 void translate_hotspot_exception_to_jni_exception(JavaThread* THREAD, const Handle& throwable); 181 182 public: 183 // Opens a JVMCIEnv scope for a Java to VM call (e.g., via CompilerToVM). 184 // An exception occurring within the scope is left pending when the 185 // scope closes so that it will be propagated back to Java. 186 // The JVMCIEnv destructor translates the exception object for the 187 // Java runtime if necessary. 188 JVMCIEnv(JNIEnv* env, const char* file, int line); 189 190 // Opens a JVMCIEnv scope for a compilation scheduled by the CompileBroker. 191 // An exception occurring within the scope must not be propagated back to 192 // the CompileBroker. 193 JVMCIEnv(JVMCICompileState* compile_state, const char* file, int line); 194 195 // Opens a JNIEnv scope for a call from within the VM. An exception occurring 196 // within the scope must not be propagated back to the caller. 197 JVMCIEnv(JavaThread* env, const char* file, int line); 198 199 // Opens a JNIEnv scope for accessing `for_object`. An exception occurring 200 // within the scope must not be propagated back to the caller. 201 JVMCIEnv(JVMCIObject for_object, const char* file, int line) { 202 // A JNI call to access an object in the shared library heap 203 // can block or take a long time so do not allow such access 204 // on the VM thread. 205 assert(for_object.is_hotspot() || !Thread::current()->is_VM_thread(), 206 "cannot open JVMCIEnv scope when in the VM thread for accessing a shared library heap object"); 207 init(for_object.is_hotspot(), file, line); 208 } 209 210 // Opens a JNIEnv scope for the HotSpot runtime if `is_hotspot` is true 211 // otherwise for the shared library runtime. An exception occurring 212 // within the scope must not be propagated back to the caller. 213 JVMCIEnv(bool is_hotspot, const char* file, int line) { 214 init(is_hotspot, file, line); 215 } 216 217 ~JVMCIEnv(); 218 219 JVMCIRuntime* runtime() { 220 return _runtime; 221 } 222 223 // Initializes Services.savedProperties in the shared library by copying 224 // the values from the same field in the HotSpot heap. 225 void copy_saved_properties(); 226 227 jboolean has_pending_exception(); 228 void clear_pending_exception(); 229 230 // Prints an exception and stack trace of a pending exception. 231 void describe_pending_exception(bool clear); 232 233 int get_length(JVMCIArray array); 234 235 JVMCIObject get_object_at(JVMCIObjectArray array, int index); 236 void put_object_at(JVMCIObjectArray array, int index, JVMCIObject value); 237 238 jboolean get_bool_at(JVMCIPrimitiveArray array, int index); 239 void put_bool_at(JVMCIPrimitiveArray array, int index, jboolean value); 240 241 jbyte get_byte_at(JVMCIPrimitiveArray array, int index); 242 void put_byte_at(JVMCIPrimitiveArray array, int index, jbyte value); 243 244 jint get_int_at(JVMCIPrimitiveArray array, int index); 245 void put_int_at(JVMCIPrimitiveArray array, int index, jint value); 246 247 long get_long_at(JVMCIPrimitiveArray array, int index); 248 void put_long_at(JVMCIPrimitiveArray array, int index, jlong value); 249 250 void copy_bytes_to(JVMCIPrimitiveArray src, jbyte* dest, int offset, int size_in_bytes); 251 void copy_bytes_from(jbyte* src, JVMCIPrimitiveArray dest, int offset, int size_in_bytes); 252 253 JVMCIObjectArray initialize_intrinsics(JVMCI_TRAPS); 254 255 jboolean is_boxing_object(BasicType type, JVMCIObject object); 256 257 // Get the primitive value from a Java boxing object. It's hard error to 258 // pass a non-primitive BasicType. 259 jvalue get_boxed_value(BasicType type, JVMCIObject object); 260 261 // Return the BasicType of the object if it's a boxing object, otherwise return T_ILLEGAL. 262 BasicType get_box_type(JVMCIObject object); 263 264 // Create a boxing object of the appropriate primitive type. 265 JVMCIObject create_box(BasicType type, jvalue* value, JVMCI_TRAPS); 266 267 const char* as_utf8_string(JVMCIObject str); 268 char* as_utf8_string(JVMCIObject str, char* buf, int buflen); 269 270 JVMCIObject create_string(Symbol* str, JVMCI_TRAPS) { 271 return create_string(str->as_C_string(), JVMCI_CHECK_(JVMCIObject())); 306 JVMCIObject call_HotSpotJVMCIRuntime_getCompiler(JVMCIObject runtime, JVMCI_TRAPS); 307 308 JVMCIObject call_HotSpotJVMCIRuntime_callToString(JVMCIObject object, JVMCI_TRAPS); 309 310 JVMCIObject call_PrimitiveConstant_forTypeChar(jchar kind, jlong value, JVMCI_TRAPS); 311 JVMCIObject call_JavaConstant_forFloat(float value, JVMCI_TRAPS); 312 JVMCIObject call_JavaConstant_forDouble(double value, JVMCI_TRAPS); 313 314 BasicType kindToBasicType(JVMCIObject kind, JVMCI_TRAPS); 315 316 #define DO_THROW(name) \ 317 void throw_##name(const char* msg = NULL); 318 319 DO_THROW(InternalError) 320 DO_THROW(ArrayIndexOutOfBoundsException) 321 DO_THROW(IllegalStateException) 322 DO_THROW(NullPointerException) 323 DO_THROW(IllegalArgumentException) 324 DO_THROW(InvalidInstalledCodeException) 325 DO_THROW(UnsatisfiedLinkError) 326 327 #undef DO_THROW 328 329 void fthrow_error(const char* file, int line, const char* format, ...) ATTRIBUTE_PRINTF(4, 5); 330 331 // Given an instance of HotSpotInstalledCode return the corresponding CodeBlob* 332 CodeBlob* asCodeBlob(JVMCIObject code); 333 334 nmethod* asNmethod(JVMCIObject code) { 335 CodeBlob* cb = asCodeBlob(code); 336 if (cb == NULL) { 337 return NULL; 338 } 339 nmethod* nm = cb->as_nmethod_or_null(); 340 guarantee(nm != NULL, "not an nmethod"); 341 return nm; 342 } 343 344 MethodData* asMethodData(jlong metaspaceMethodData) { 345 return (MethodData*) (address) metaspaceMethodData; | 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 25 #ifndef SHARE_JVMCI_JVMCIENV_HPP 26 #define SHARE_JVMCI_JVMCIENV_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "jvmci/jvmciJavaClasses.hpp" 30 #include "runtime/thread.hpp" 31 32 class CompileTask; 33 class JVMCIObject; 34 class JVMCIObjectArray; 35 class JVMCIPrimitiveArray; 36 class JVMCICompiler; 37 class JVMCIRuntime; 38 39 #define JVMCI_EXCEPTION_CONTEXT \ 40 JavaThread* thread=JavaThread::current(); \ 41 Thread* THREAD = thread; 42 43 // Helper to log more context on a JNI exception 44 #define JVMCI_EXCEPTION_CHECK(env, ...) \ 45 do { \ 46 if (env->ExceptionCheck()) { \ 47 if (env != JavaThread::current()->jni_environment() && JVMCIEnv::get_shared_library_path() != NULL) { \ 48 tty->print_cr("In JVMCI shared library (%s):", JVMCIEnv::get_shared_library_path()); \ 49 } \ 50 tty->print_cr(__VA_ARGS__); \ 51 return; \ 52 } \ 53 } while(0) 54 55 // Helper class to ensure that references to Klass* are kept alive for G1 56 class JVMCIKlassHandle : public StackObj { 57 private: 58 Klass* _klass; 128 _failure_reason = reason; 129 _failure_reason_on_C_heap = reason_on_C_heap; 130 _retryable = retryable; 131 } 132 }; 133 134 135 // This class is a top level wrapper around interactions between HotSpot 136 // and the JVMCI Java code. It supports both a HotSpot heap based 137 // runtime with HotSpot oop based accessors as well as a shared library 138 // based runtime that is accessed through JNI. It abstracts away all 139 // interactions with JVMCI objects so that a single version of the 140 // HotSpot C++ code can can work with either runtime. 141 class JVMCIEnv : public ResourceObj { 142 friend class JNIAccessMark; 143 144 static char* _shared_library_path; // argument to os:dll_load 145 static void* _shared_library_handle; // result of os::dll_load 146 static JavaVM* _shared_library_javavm; // result of calling JNI_CreateJavaVM in shared library 147 148 // Initializes the shared library JavaVM if not already initialized. 149 // Returns the JNI interface pointer for the current thread 150 // if initialization was performed by this call, NULL if 151 // initialization was performed by a previous call. 152 static JNIEnv* init_shared_library(JavaThread* thread); 153 154 // Initializes the _env, _mode and _runtime fields. 155 void init_env_mode_runtime(JavaThread* thread, JNIEnv* parent_env); 156 157 void init(JavaThread* thread, bool is_hotspot, const char* file, int line); 158 159 JNIEnv* _env; // JNI env for calling into shared library 160 bool _pop_frame_on_close; // Must pop frame on close? 161 bool _detach_on_close; // Must detach on close? 162 JVMCIRuntime* _runtime; // Access to a HotSpotJVMCIRuntime 163 bool _is_hotspot; // Which heap is the HotSpotJVMCIRuntime in 164 bool _throw_to_caller; // Propagate an exception raised in this env to the caller? 165 const char* _file; // The file and ... 166 int _line; // ... line where this JNIEnv was created 167 168 // Translates an exception on the HotSpot heap to an exception on 169 // the shared library heap. The translation includes the stack and 170 // causes of `throwable`. The translated exception is pending in the 171 // shared library thread upon returning. 172 void translate_hotspot_exception_to_jni_exception(JavaThread* THREAD, const Handle& throwable); 173 174 public: 175 // Opens a JVMCIEnv scope for a Java to VM call (e.g., via CompilerToVM). 176 // An exception occurring within the scope is left pending when the 177 // scope closes so that it will be propagated back to Java. 178 // The JVMCIEnv destructor translates the exception object for the 179 // Java runtime if necessary. 180 JVMCIEnv(JavaThread* thread, JNIEnv* env, const char* file, int line); 181 182 // Opens a JVMCIEnv scope for a compilation scheduled by the CompileBroker. 183 // An exception occurring within the scope must not be propagated back to 184 // the CompileBroker. 185 JVMCIEnv(JavaThread* thread, JVMCICompileState* compile_state, const char* file, int line); 186 187 // Opens a JNIEnv scope for a call from within the VM. An exception occurring 188 // within the scope must not be propagated back to the caller. 189 JVMCIEnv(JavaThread* env, const char* file, int line); 190 191 // Opens a JNIEnv scope for accessing `for_object`. An exception occurring 192 // within the scope must not be propagated back to the caller. 193 JVMCIEnv(JavaThread* thread, JVMCIObject for_object, const char* file, int line) { 194 // A JNI call to access an object in the shared library heap 195 // can block or take a long time so do not allow such access 196 // on the VM thread. 197 assert(for_object.is_hotspot() || !Thread::current()->is_VM_thread(), 198 "cannot open JVMCIEnv scope when in the VM thread for accessing a shared library heap object"); 199 init(thread, for_object.is_hotspot(), file, line); 200 } 201 202 // Opens a JNIEnv scope for the HotSpot runtime if `is_hotspot` is true 203 // otherwise for the shared library runtime. An exception occurring 204 // within the scope must not be propagated back to the caller. 205 JVMCIEnv(JavaThread* thread, bool is_hotspot, const char* file, int line) { 206 init(thread, is_hotspot, file, line); 207 } 208 209 ~JVMCIEnv(); 210 211 JVMCIRuntime* runtime() { 212 return _runtime; 213 } 214 215 // Initializes Services.savedProperties in the shared library by copying 216 // the values from the same field in the HotSpot heap. 217 void copy_saved_properties(); 218 219 jboolean has_pending_exception(); 220 void clear_pending_exception(); 221 222 // Prints an exception and stack trace of a pending exception. 223 void describe_pending_exception(bool clear); 224 225 int get_length(JVMCIArray array); 226 227 JVMCIObject get_object_at(JVMCIObjectArray array, int index); 228 void put_object_at(JVMCIObjectArray array, int index, JVMCIObject value); 229 230 jboolean get_bool_at(JVMCIPrimitiveArray array, int index); 231 void put_bool_at(JVMCIPrimitiveArray array, int index, jboolean value); 232 233 jbyte get_byte_at(JVMCIPrimitiveArray array, int index); 234 void put_byte_at(JVMCIPrimitiveArray array, int index, jbyte value); 235 236 jint get_int_at(JVMCIPrimitiveArray array, int index); 237 void put_int_at(JVMCIPrimitiveArray array, int index, jint value); 238 239 long get_long_at(JVMCIPrimitiveArray array, int index); 240 void put_long_at(JVMCIPrimitiveArray array, int index, jlong value); 241 242 void copy_bytes_to(JVMCIPrimitiveArray src, jbyte* dest, int offset, jsize length); 243 void copy_bytes_from(jbyte* src, JVMCIPrimitiveArray dest, int offset, jsize length); 244 245 void copy_longs_from(jlong* src, JVMCIPrimitiveArray dest, int offset, jsize length); 246 247 JVMCIObjectArray initialize_intrinsics(JVMCI_TRAPS); 248 249 jboolean is_boxing_object(BasicType type, JVMCIObject object); 250 251 // Get the primitive value from a Java boxing object. It's hard error to 252 // pass a non-primitive BasicType. 253 jvalue get_boxed_value(BasicType type, JVMCIObject object); 254 255 // Return the BasicType of the object if it's a boxing object, otherwise return T_ILLEGAL. 256 BasicType get_box_type(JVMCIObject object); 257 258 // Create a boxing object of the appropriate primitive type. 259 JVMCIObject create_box(BasicType type, jvalue* value, JVMCI_TRAPS); 260 261 const char* as_utf8_string(JVMCIObject str); 262 char* as_utf8_string(JVMCIObject str, char* buf, int buflen); 263 264 JVMCIObject create_string(Symbol* str, JVMCI_TRAPS) { 265 return create_string(str->as_C_string(), JVMCI_CHECK_(JVMCIObject())); 300 JVMCIObject call_HotSpotJVMCIRuntime_getCompiler(JVMCIObject runtime, JVMCI_TRAPS); 301 302 JVMCIObject call_HotSpotJVMCIRuntime_callToString(JVMCIObject object, JVMCI_TRAPS); 303 304 JVMCIObject call_PrimitiveConstant_forTypeChar(jchar kind, jlong value, JVMCI_TRAPS); 305 JVMCIObject call_JavaConstant_forFloat(float value, JVMCI_TRAPS); 306 JVMCIObject call_JavaConstant_forDouble(double value, JVMCI_TRAPS); 307 308 BasicType kindToBasicType(JVMCIObject kind, JVMCI_TRAPS); 309 310 #define DO_THROW(name) \ 311 void throw_##name(const char* msg = NULL); 312 313 DO_THROW(InternalError) 314 DO_THROW(ArrayIndexOutOfBoundsException) 315 DO_THROW(IllegalStateException) 316 DO_THROW(NullPointerException) 317 DO_THROW(IllegalArgumentException) 318 DO_THROW(InvalidInstalledCodeException) 319 DO_THROW(UnsatisfiedLinkError) 320 DO_THROW(UnsupportedOperationException) 321 DO_THROW(ClassNotFoundException) 322 323 #undef DO_THROW 324 325 void fthrow_error(const char* file, int line, const char* format, ...) ATTRIBUTE_PRINTF(4, 5); 326 327 // Given an instance of HotSpotInstalledCode return the corresponding CodeBlob* 328 CodeBlob* asCodeBlob(JVMCIObject code); 329 330 nmethod* asNmethod(JVMCIObject code) { 331 CodeBlob* cb = asCodeBlob(code); 332 if (cb == NULL) { 333 return NULL; 334 } 335 nmethod* nm = cb->as_nmethod_or_null(); 336 guarantee(nm != NULL, "not an nmethod"); 337 return nm; 338 } 339 340 MethodData* asMethodData(jlong metaspaceMethodData) { 341 return (MethodData*) (address) metaspaceMethodData; |