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 | |
| 32 | // Maximum number of local references in the indirect reference table. The value is arbitrary but |
| 33 | // low enough that it forces sanity checks. |
| 34 | static constexpr size_t kLocalsMax = 512; |
| 35 | |
| 36 | struct JNIEnvExt : public JNIEnv { |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 37 | static JNIEnvExt* Create(Thread* self, JavaVMExt* vm); |
| 38 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 39 | ~JNIEnvExt(); |
| 40 | |
| 41 | void DumpReferenceTables(std::ostream& os) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 42 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 43 | |
| 44 | void SetCheckJniEnabled(bool enabled); |
| 45 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 46 | void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_); |
| 47 | void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 48 | |
| 49 | template<typename T> |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 50 | T AddLocalReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 51 | |
Andreas Gampe | 4d98c84 | 2015-12-09 15:14:04 -0800 | [diff] [blame] | 52 | static Offset SegmentStateOffset(size_t pointer_size); |
| 53 | static Offset LocalRefCookieOffset(size_t pointer_size); |
| 54 | static Offset SelfOffset(size_t pointer_size); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 55 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 56 | static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version); |
| 57 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 58 | jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
| 59 | void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 60 | |
| 61 | Thread* const self; |
| 62 | JavaVMExt* const vm; |
| 63 | |
| 64 | // Cookie used when using the local indirect reference table. |
| 65 | uint32_t local_ref_cookie; |
| 66 | |
| 67 | // JNI local references. |
| 68 | IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_); |
| 69 | |
| 70 | // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. |
| 71 | // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) |
| 72 | // to a native method. |
| 73 | std::vector<uint32_t> stacked_local_ref_cookies; |
| 74 | |
| 75 | // Frequently-accessed fields cached from JavaVM. |
| 76 | bool check_jni; |
| 77 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 78 | // If we are a JNI env for a daemon thread with a deleted runtime. |
| 79 | bool runtime_deleted; |
| 80 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 81 | // How many nested "critical" JNI calls are we in? |
| 82 | int critical; |
| 83 | |
| 84 | // Entered JNI monitors, for bulk exit on thread detach. |
| 85 | ReferenceTable monitors; |
| 86 | |
| 87 | // Used by -Xcheck:jni. |
| 88 | const JNINativeInterface* unchecked_functions; |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 89 | |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 90 | // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking |
| 91 | // rules in CheckJNI mode. |
| 92 | |
| 93 | // Record locking of a monitor. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 94 | void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 95 | |
| 96 | // 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] | 97 | void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 98 | |
| 99 | // 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] | 100 | void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 102 | // Set the functions to the runtime shutdown functions. |
| 103 | void SetFunctionsToRuntimeShutdownFunctions(); |
| 104 | |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 105 | private: |
| 106 | // The constructor should not be called directly. It may leave the object in an erronuous state, |
| 107 | // and the result needs to be checked. |
| 108 | JNIEnvExt(Thread* self, JavaVMExt* vm); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 109 | |
| 110 | // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI |
| 111 | // to ensure that only monitors locked in this native frame are being unlocked, and that at |
| 112 | // the end all are unlocked. |
| 113 | std::vector<std::pair<uintptr_t, jobject>> locked_objects_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // Used to save and restore the JNIEnvExt state when not going through code created by the JNI |
| 117 | // compiler. |
| 118 | class ScopedJniEnvLocalRefState { |
| 119 | public: |
| 120 | explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) { |
| 121 | saved_local_ref_cookie_ = env->local_ref_cookie; |
| 122 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 123 | } |
| 124 | |
| 125 | ~ScopedJniEnvLocalRefState() { |
| 126 | env_->locals.SetSegmentState(env_->local_ref_cookie); |
| 127 | env_->local_ref_cookie = saved_local_ref_cookie_; |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | JNIEnvExt* const env_; |
| 132 | uint32_t saved_local_ref_cookie_; |
| 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); |
| 135 | }; |
| 136 | |
| 137 | } // namespace art |
| 138 | |
| 139 | #endif // ART_RUNTIME_JNI_ENV_EXT_H_ |