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