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