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 | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 29 | #include <iosfwd> |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 32 | #ifndef NATIVE_METHOD |
| 33 | #define NATIVE_METHOD(className, functionName, signature) \ |
| 34 | { #functionName, signature, reinterpret_cast<void*>(className ## _ ## functionName) } |
| 35 | #endif |
| 36 | #define REGISTER_NATIVE_METHODS(jni_class_name) \ |
| 37 | RegisterNativeMethods(env, jni_class_name, gMethods, arraysize(gMethods)) |
| 38 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 41 | class ClassLoader; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 42 | class Field; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 43 | union JValue; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 44 | class Libraries; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 45 | class AbstractMethod; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 46 | class ScopedObjectAccess; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 47 | class Thread; |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 49 | void SetJniGlobalsMax(size_t max); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 50 | void JniAbortF(const char* jni_function_name, const char* fmt, ...); |
Shih-wei Liao | 31384c5 | 2011-09-06 15:27:45 -0700 | [diff] [blame] | 51 | void* FindNativeMethod(Thread* thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 52 | void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods, |
| 53 | size_t method_count); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 54 | |
Ian Rogers | 45619fc | 2012-02-29 11:15:25 -0800 | [diff] [blame] | 55 | size_t NumArgArrayBytes(const char* shorty, uint32_t shorty_len); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | JValue InvokeWithJValues(const ScopedObjectAccess&, jobject obj, jmethodID mid, jvalue* args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 57 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 58 | JValue InvokeWithJValues(const ScopedObjectAccess&, Object* receiver, AbstractMethod* m, JValue* args) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 59 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 61 | int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause); |
| 62 | |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 63 | struct JavaVMExt : public JavaVM { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 64 | JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options); |
Elliott Hughes | de69d7f | 2011-08-18 16:49:37 -0700 | [diff] [blame] | 65 | ~JavaVMExt(); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 66 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Loads the given shared library. 'path' is an absolute pathname. |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 69 | * |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 70 | * Returns 'true' on success. On failure, sets 'detail' to a |
| 71 | * human-readable description of the error. |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 72 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | bool LoadNativeLibrary(const std::string& path, ClassLoader* class_loader, std::string& detail) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 74 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
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 | */ |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 80 | void* FindCodeForNativeMethod(AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 83 | void DumpForSigQuit(std::ostream& os); |
| 84 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | void DumpReferenceTables(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 88 | void SetCheckJniEnabled(bool enabled); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 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. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 95 | void (*check_jni_abort_hook)(void* data, const std::string& reason); |
| 96 | void* check_jni_abort_hook_data; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 98 | // Extra checking. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 99 | bool check_jni; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 100 | bool force_copy; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 102 | // Extra diagnostics. |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 103 | std::string trace; |
| 104 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 105 | // Used to provide compatibility for apps that assumed direct references. |
| 106 | bool work_around_app_jni_bugs; |
| 107 | |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 108 | // Used to hold references to pinned primitive arrays. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 109 | Mutex pins_lock DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 110 | ReferenceTable pin_table GUARDED_BY(pins_lock); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 111 | |
| 112 | // JNI global references. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | Mutex globals_lock DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 114 | IndirectReferenceTable globals GUARDED_BY(globals_lock); |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 115 | |
| 116 | // JNI weak global references. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 117 | Mutex weak_globals_lock DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 118 | IndirectReferenceTable weak_globals GUARDED_BY(weak_globals_lock); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 119 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | Mutex libraries_lock DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 121 | Libraries* libraries GUARDED_BY(libraries_lock); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 122 | |
| 123 | // Used by -Xcheck:jni. |
| 124 | const JNIInvokeInterface* unchecked_functions; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 127 | struct JNIEnvExt : public JNIEnv { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 128 | JNIEnvExt(Thread* self, JavaVMExt* vm); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 129 | ~JNIEnvExt(); |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 130 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | void DumpReferenceTables(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 132 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 134 | void SetCheckJniEnabled(bool enabled); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 135 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 136 | void PushFrame(int capacity); |
| 137 | void PopFrame(); |
| 138 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 139 | static Offset SegmentStateOffset() { |
| 140 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) + |
| 141 | IndirectReferenceTable::SegmentStateOffset().Int32Value()); |
| 142 | } |
| 143 | |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 144 | static Offset LocalRefCookieOffset() { |
| 145 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie)); |
| 146 | } |
| 147 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 148 | Thread* const self; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 149 | JavaVMExt* vm; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 151 | // Cookie used when using the local indirect reference table. |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 152 | uint32_t local_ref_cookie; |
| 153 | |
| 154 | // JNI local references. |
| 155 | IndirectReferenceTable locals; |
| 156 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 157 | // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. |
| 158 | // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) |
| 159 | // to a native method. |
| 160 | std::vector<uint32_t> stacked_local_ref_cookies; |
| 161 | |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 162 | // Frequently-accessed fields cached from JavaVM. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 163 | bool check_jni; |
| 164 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 165 | // How many nested "critical" JNI calls are we in? |
| 166 | int critical; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | bbd7671 | 2011-08-17 10:25:24 -0700 | [diff] [blame] | 168 | // Entered JNI monitors, for bulk exit on thread detach. |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 169 | ReferenceTable monitors; |
Elliott Hughes | 6c1a394 | 2011-08-17 15:00:06 -0700 | [diff] [blame] | 170 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 171 | // Used by -Xcheck:jni. |
| 172 | const JNINativeInterface* unchecked_functions; |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 175 | const JNINativeInterface* GetCheckJniNativeInterface(); |
| 176 | const JNIInvokeInterface* GetCheckJniInvokeInterface(); |
| 177 | |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 178 | // Used to save and restore the JNIEnvExt state when not going through code created by the JNI |
| 179 | // compiler |
| 180 | class ScopedJniEnvLocalRefState { |
| 181 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 182 | explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) { |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 183 | saved_local_ref_cookie_ = env->local_ref_cookie; |
| 184 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 185 | } |
| 186 | |
| 187 | ~ScopedJniEnvLocalRefState() { |
| 188 | env_->locals.SetSegmentState(env_->local_ref_cookie); |
| 189 | env_->local_ref_cookie = saved_local_ref_cookie_; |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | JNIEnvExt* env_; |
| 194 | uint32_t saved_local_ref_cookie_; |
| 195 | DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); |
| 196 | }; |
| 197 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 198 | } // namespace art |
Carl Shapiro | ea4dca8 | 2011-08-01 13:45:38 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 200 | std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs); |
| 201 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 202 | #endif // ART_SRC_JNI_INTERNAL_H_ |