Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_JNI_INTERNAL_H_ |
| 4 | #define ART_SRC_JNI_INTERNAL_H_ |
| 5 | |
| 6 | #include "jni.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 8 | #include "heap.h" |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 9 | #include "indirect_reference_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "macros.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 11 | #include "mutex.h" |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 12 | #include "reference_table.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 13 | #include "runtime.h" |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 14 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 17 | namespace art { |
| 18 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 19 | class ClassLoader; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 20 | class Field; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 21 | union JValue; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 22 | class Libraries; |
| 23 | class Method; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 24 | class Thread; |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 26 | void SetJniGlobalsMax(size_t max); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 27 | void JniAbort(const char* jni_function_name); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 28 | void* FindNativeMethod(Thread* thread); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame^] | 29 | jclass CacheClass(JNIEnv* env, const char* jni_class_name); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 31 | template<typename T> T Decode(JNIEnv*, jobject); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 32 | template<typename T> T AddLocalReference(JNIEnv*, const Object*); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 34 | inline Field* DecodeField(jfieldID fid) { |
| 35 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 36 | // TODO: we should make these unique weak globals if Field instances can ever move. |
| 37 | UNIMPLEMENTED(WARNING); |
| 38 | #endif |
| 39 | return reinterpret_cast<Field*>(fid); |
| 40 | } |
| 41 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 42 | inline jfieldID EncodeField(Field* field) { |
| 43 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 44 | UNIMPLEMENTED(WARNING); |
| 45 | #endif |
| 46 | return reinterpret_cast<jfieldID>(field); |
| 47 | } |
| 48 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 49 | inline Method* DecodeMethod(jmethodID mid) { |
| 50 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 51 | // TODO: we should make these unique weak globals if Method instances can ever move. |
| 52 | UNIMPLEMENTED(WARNING); |
| 53 | #endif |
| 54 | return reinterpret_cast<Method*>(mid); |
| 55 | } |
| 56 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 57 | inline jmethodID EncodeMethod(Method* method) { |
| 58 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 59 | UNIMPLEMENTED(WARNING); |
| 60 | #endif |
| 61 | return reinterpret_cast<jmethodID>(method); |
| 62 | } |
| 63 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 64 | size_t NumArgArrayBytes(const char* shorty); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 65 | JValue InvokeWithJValues(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 66 | JValue InvokeWithJValues(Thread* self, Object* receiver, Method* m, JValue* args); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 68 | struct JavaVMExt : public JavaVM { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 69 | JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options); |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 70 | ~JavaVMExt(); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 71 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Loads the given shared library. 'path' is an absolute pathname. |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 74 | * |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 75 | * Returns 'true' on success. On failure, sets 'detail' to a |
| 76 | * human-readable description of the error. |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 77 | */ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 78 | bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail); |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Returns a pointer to the code for the native method 'm', found |
| 82 | * using dlsym(3) on every native library that's been loaded so far. |
| 83 | */ |
| 84 | void* FindCodeForNativeMethod(Method* m); |
| 85 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 86 | void DumpReferenceTables(); |
| 87 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 88 | void EnableCheckJni(); |
| 89 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 90 | void VisitRoots(Heap::RootVisitor*, void*); |
| 91 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 92 | Runtime* runtime; |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 94 | // Used for testing. By default, we'll LOG(FATAL) the reason. |
| 95 | void (*check_jni_abort_hook)(const std::string& reason); |
| 96 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 97 | // Extra checking. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 98 | bool check_jni; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 99 | bool force_copy; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 100 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 101 | // Extra diagnostics. |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 102 | std::string trace; |
| 103 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 104 | // Used to provide compatibility for apps that assumed direct references. |
| 105 | bool work_around_app_jni_bugs; |
| 106 | |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 107 | // Used to hold references to pinned primitive arrays. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 108 | Mutex pins_lock; |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 109 | ReferenceTable pin_table; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 110 | |
| 111 | // JNI global references. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 112 | Mutex globals_lock; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 113 | IndirectReferenceTable globals; |
| 114 | |
| 115 | // JNI weak global references. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 116 | Mutex weak_globals_lock; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 117 | IndirectReferenceTable weak_globals; |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 118 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 119 | Mutex libraries_lock; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 120 | Libraries* libraries; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 121 | |
| 122 | // Used by -Xcheck:jni. |
| 123 | const JNIInvokeInterface* unchecked_functions; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 126 | struct JNIEnvExt : public JNIEnv { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 127 | JNIEnvExt(Thread* self, JavaVMExt* vm); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 128 | ~JNIEnvExt(); |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 129 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 130 | void DumpReferenceTables(); |
| 131 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 132 | void EnableCheckJni(); |
| 133 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 134 | void PushFrame(int capacity); |
| 135 | void PopFrame(); |
| 136 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 137 | static Offset SegmentStateOffset() { |
| 138 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) + |
| 139 | IndirectReferenceTable::SegmentStateOffset().Int32Value()); |
| 140 | } |
| 141 | |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 142 | static Offset LocalRefCookieOffset() { |
| 143 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie)); |
| 144 | } |
| 145 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 146 | Thread* const self; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 147 | JavaVMExt* vm; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 148 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 149 | // Cookie used when using the local indirect reference table. |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 150 | uint32_t local_ref_cookie; |
| 151 | |
| 152 | // JNI local references. |
| 153 | IndirectReferenceTable locals; |
| 154 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 155 | // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. |
| 156 | // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) |
| 157 | // to a native method. |
| 158 | std::vector<uint32_t> stacked_local_ref_cookies; |
| 159 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 160 | // Frequently-accessed fields cached from JavaVM. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 161 | bool check_jni; |
| 162 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 163 | // How many nested "critical" JNI calls are we in? |
| 164 | int critical; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 165 | |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 166 | // Entered JNI monitors, for bulk exit on thread detach. |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 167 | ReferenceTable monitors; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 168 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 169 | // Used by -Xcheck:jni. |
| 170 | const JNINativeInterface* unchecked_functions; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 173 | const JNINativeInterface* GetCheckJniNativeInterface(); |
| 174 | const JNIInvokeInterface* GetCheckJniInvokeInterface(); |
| 175 | |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 176 | // Used to save and restore the JNIEnvExt state when not going through code created by the JNI |
| 177 | // compiler |
| 178 | class ScopedJniEnvLocalRefState { |
| 179 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 180 | explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) { |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 181 | saved_local_ref_cookie_ = env->local_ref_cookie; |
| 182 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 183 | } |
| 184 | |
| 185 | ~ScopedJniEnvLocalRefState() { |
| 186 | env_->locals.SetSegmentState(env_->local_ref_cookie); |
| 187 | env_->local_ref_cookie = saved_local_ref_cookie_; |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | JNIEnvExt* env_; |
| 192 | uint32_t saved_local_ref_cookie_; |
| 193 | DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); |
| 194 | }; |
| 195 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 196 | } // namespace art |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 197 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 198 | std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs); |
| 199 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 200 | #endif // ART_SRC_JNI_INTERNAL_H_ |