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