Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [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 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_JNI_ENV_EXT_H_ |
| 18 | #define ART_RUNTIME_JNI_ENV_EXT_H_ |
| 19 | |
| 20 | #include <jni.h> |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
| 24 | #include "indirect_reference_table.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 25 | #include "obj_ptr.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 26 | #include "reference_table.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class JavaVMExt; |
| 31 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 32 | namespace mirror { |
| 33 | class Object; |
| 34 | } // namespace mirror |
| 35 | |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 36 | // Number of local references in the indirect reference table. The value is arbitrary but |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 37 | // low enough that it forces sanity checks. |
Andreas Gampe | a8e3b86 | 2016-10-17 20:12:52 -0700 | [diff] [blame] | 38 | static constexpr size_t kLocalsInitial = 512; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 39 | |
| 40 | struct JNIEnvExt : public JNIEnv { |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 41 | // Creates a new JNIEnvExt. Returns null on error, in which case error_msg |
| 42 | // will contain a description of the error. |
| 43 | static JNIEnvExt* Create(Thread* self, JavaVMExt* vm, std::string* error_msg); |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 44 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 45 | ~JNIEnvExt(); |
| 46 | |
| 47 | void DumpReferenceTables(std::ostream& os) |
Andreas Gampe | 8a2a1fc | 2017-09-29 17:53:18 -0700 | [diff] [blame] | 48 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 49 | REQUIRES(!Locks::alloc_tracker_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 50 | |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 51 | void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 52 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 53 | void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_); |
| 54 | void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 55 | |
| 56 | template<typename T> |
Andreas Gampe | 8a2a1fc | 2017-09-29 17:53:18 -0700 | [diff] [blame] | 57 | T AddLocalReference(ObjPtr<mirror::Object> obj) |
| 58 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 59 | REQUIRES(!Locks::alloc_tracker_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 60 | |
Andreas Gampe | 4d98c84 | 2015-12-09 15:14:04 -0800 | [diff] [blame] | 61 | static Offset SegmentStateOffset(size_t pointer_size); |
| 62 | static Offset LocalRefCookieOffset(size_t pointer_size); |
| 63 | static Offset SelfOffset(size_t pointer_size); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 64 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 65 | static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version); |
| 66 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 67 | jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
| 68 | void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 69 | |
| 70 | Thread* const self; |
| 71 | JavaVMExt* const vm; |
| 72 | |
| 73 | // Cookie used when using the local indirect reference table. |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 74 | IRTSegmentState local_ref_cookie; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 75 | |
| 76 | // JNI local references. |
| 77 | IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_); |
| 78 | |
| 79 | // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. |
| 80 | // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) |
| 81 | // to a native method. |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 82 | std::vector<IRTSegmentState> stacked_local_ref_cookies; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 83 | |
| 84 | // Frequently-accessed fields cached from JavaVM. |
| 85 | bool check_jni; |
| 86 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 87 | // If we are a JNI env for a daemon thread with a deleted runtime. |
| 88 | bool runtime_deleted; |
| 89 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 90 | // How many nested "critical" JNI calls are we in? |
| 91 | int critical; |
| 92 | |
| 93 | // Entered JNI monitors, for bulk exit on thread detach. |
| 94 | ReferenceTable monitors; |
| 95 | |
| 96 | // Used by -Xcheck:jni. |
| 97 | const JNINativeInterface* unchecked_functions; |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 99 | // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking |
| 100 | // rules in CheckJNI mode. |
| 101 | |
| 102 | // Record locking of a monitor. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 103 | void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 104 | |
| 105 | // Check the release, that is, that the release is performed in the same JNI "segment." |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 106 | void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 107 | |
| 108 | // Check that no monitors are held that have been acquired in this JNI "segment." |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 109 | void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 110 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 111 | // Set the functions to the runtime shutdown functions. |
| 112 | void SetFunctionsToRuntimeShutdownFunctions(); |
| 113 | |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 114 | // Set the function table override. This will install the override (or original table, if null) |
| 115 | // to all threads. |
| 116 | // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI. |
| 117 | // After overriding the JNI function table, CheckJNI toggling is ignored. |
| 118 | static void SetTableOverride(const JNINativeInterface* table_override) |
| 119 | REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_); |
| 120 | |
| 121 | // Return either the regular, or the CheckJNI function table. Will return table_override_ instead |
| 122 | // if it is not null. |
| 123 | static const JNINativeInterface* GetFunctionTable(bool check_jni) |
| 124 | REQUIRES(Locks::jni_function_table_lock_); |
| 125 | |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 126 | private: |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 127 | // Override of function tables. This applies to both default as well as instrumented (CheckJNI) |
| 128 | // function tables. |
| 129 | static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_); |
| 130 | |
Richard Uhler | da0a69e | 2016-10-11 15:06:38 +0100 | [diff] [blame] | 131 | // The constructor should not be called directly. It may leave the object in an erroneous state, |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 132 | // and the result needs to be checked. |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 133 | JNIEnvExt(Thread* self, JavaVMExt* vm, std::string* error_msg) |
| 134 | REQUIRES(!Locks::jni_function_table_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 135 | |
| 136 | // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI |
| 137 | // to ensure that only monitors locked in this native frame are being unlocked, and that at |
| 138 | // the end all are unlocked. |
| 139 | std::vector<std::pair<uintptr_t, jobject>> locked_objects_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // Used to save and restore the JNIEnvExt state when not going through code created by the JNI |
| 143 | // compiler. |
| 144 | class ScopedJniEnvLocalRefState { |
| 145 | public: |
| 146 | explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) { |
| 147 | saved_local_ref_cookie_ = env->local_ref_cookie; |
| 148 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 149 | } |
| 150 | |
| 151 | ~ScopedJniEnvLocalRefState() { |
| 152 | env_->locals.SetSegmentState(env_->local_ref_cookie); |
| 153 | env_->local_ref_cookie = saved_local_ref_cookie_; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | JNIEnvExt* const env_; |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 158 | IRTSegmentState saved_local_ref_cookie_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 159 | |
| 160 | DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); |
| 161 | }; |
| 162 | |
| 163 | } // namespace art |
| 164 | |
| 165 | #endif // ART_RUNTIME_JNI_ENV_EXT_H_ |